/[cmucl]/src/compiler/checkgen.lisp
ViewVC logotype

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10.1.1 - (show annotations) (vendor branch)
Tue Jun 12 11:54:01 1990 UTC (23 years, 10 months ago) by ram
Branch: eval_debug
Changes since 1.10: +0 -0 lines
*** empty log message ***
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the Spice Lisp project at
5 ;;; Carnegie-Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of Spice Lisp, please contact
7 ;;; Scott Fahlman (FAHLMAN@CMUC).
8 ;;; **********************************************************************
9 ;;;
10 ;;; This file implements type check generation. This is a phase that runs
11 ;;; at the very end of IR1. If a type check is too complex for the back end to
12 ;;; directly emit in-line, then we transform the check into an explicit
13 ;;; conditional using TYPEP.
14 ;;;
15 ;;; Written by Rob MacLachlan
16 ;;;
17 (in-package 'c)
18
19
20 ;;;; Cost estimation:
21
22
23 ;;; Function-Cost -- Internal
24 ;;;
25 ;;; Return some sort of guess about the cost of a call to a function. If
26 ;;; the function has some templates, we return the cost of the cheapest one,
27 ;;; otherwise we return the cost of CALL-NAMED. Calling this with functions
28 ;;; that have transforms can result in relatively meaningless results
29 ;;; (exaggerated costs.)
30 ;;;
31 ;;; We randomly special-case NULL, since it does have a source tranform and is
32 ;;; interesting to us.
33 ;;;
34 (defun function-cost (name)
35 (declare (symbol name))
36 (let ((info (info function info name))
37 (call-cost (template-cost (template-or-lose 'call-named))))
38 (if info
39 (let ((templates (function-info-templates info)))
40 (if templates
41 (template-cost (first templates))
42 (case name
43 (null (template-cost (template-or-lose 'if-eq)))
44 (t call-cost))))
45 call-cost)))
46
47
48 ;;; Type-Test-Cost -- Internal
49 ;;;
50 ;;; Return some sort of guess for the cost of doing a test against TYPE.
51 ;;; The result need not be precise as long as it isn't way out in space. The
52 ;;; units are based on the costs specified for various templates in the VM
53 ;;; definition.
54 ;;;
55 (defun type-test-cost (type)
56 (declare (type ctype type))
57 (or (let ((check (type-check-template type)))
58 (if check
59 (template-cost check)
60 (let ((found (cdr (assoc type *type-predicates* :test #'type=))))
61 (if found
62 (function-cost found)
63 nil))))
64 (typecase type
65 (union-type
66 (collect ((res 0 +))
67 (dolist (mem (union-type-types type))
68 (res (type-test-cost mem)))
69 (res)))
70 (member-type
71 (* (length (member-type-members type))
72 (function-cost 'eq)))
73 (numeric-type
74 (* (if (numeric-type-complexp type) 2 1)
75 (function-cost
76 (if (csubtypep type (specifier-type 'fixnum)) 'fixnump 'numberp))
77 (+ 1
78 (if (numeric-type-low type) 1 0)
79 (if (numeric-type-high type) 1 0))))
80 (t
81 (function-cost 'typep)))))
82
83
84 ;;;; Checking strategy determination:
85
86
87 ;;; MAYBE-WEAKEN-CHECK -- Internal
88 ;;;
89 ;;; Return the type we should test for when we really want to check for
90 ;;; Type. If speed, space or compilation speed is more important than safety,
91 ;;; then we return a weaker type if it is easier to check. First we try the
92 ;;; defined type weakenings, then look for any predicate that is cheaper.
93 ;;;
94 ;;; If the supertype is equal in cost to the type, we prefer the supertype.
95 ;;; This produces a closer approximation of the right thing in the presence of
96 ;;; poor cost info.
97 ;;;
98 (defun maybe-weaken-check (type cont)
99 (declare (type ctype type) (type continuation cont))
100 (cond ((policy (continuation-dest cont)
101 (<= speed safety) (<= space safety) (<= cspeed safety))
102 type)
103 (t
104 (let ((min-cost (type-test-cost type))
105 (min-type type)
106 (found-super nil))
107 (dolist (x *type-predicates*)
108 (let ((stype (car x)))
109 (when (and (csubtypep type stype)
110 (not (union-type-p stype))) ;Not #!% COMMON type.
111 (let ((stype-cost (type-test-cost stype)))
112 (when (or (< stype-cost min-cost)
113 (type= stype type))
114 (setq found-super t)
115 (setq min-type stype min-cost stype-cost))))))
116 (if found-super
117 min-type
118 *universal-type*)))))
119
120
121 ;;; NO-FUNCTION-VALUES-TYPES -- Internal
122 ;;;
123 ;;; Like VALUES-TYPES, only mash any complex function types to FUNCTION.
124 ;;;
125 (defun no-function-values-types (type)
126 (declare (type ctype type))
127 (multiple-value-bind (res count)
128 (values-types type)
129 (values (mapcar #'(lambda (type)
130 (if (function-type-p type)
131 (specifier-type 'function)
132 type))
133 res)
134 count)))
135
136
137 ;;; MAYBE-NEGATE-CHECK -- Internal
138 ;;;
139 ;;; Cont is a continuation we are doing a type check on and Types is a list
140 ;;; of types that we are checking its values against. If we have proven
141 ;;; that Cont generates a fixed number of values, then for each value, we check
142 ;;; whether it is cheaper to then difference between the the proven type and
143 ;;; the corresponding type in Types. If so, we opt for a :HAIRY check with
144 ;;; that test negated. Otherwise, we try to do a simple test, and if that is
145 ;;; impossible, we do a hairy test with non-negated types.
146 ;;;
147 ;;; When doing a non-negated hairy check, we call MAYBE-WEAKEN-CHECK to
148 ;;; weaken the test to a convenient supertype (conditional on policy.)
149 ;;;
150 (defun maybe-negate-check (cont types)
151 (declare (type continuation cont) (list types))
152 (multiple-value-bind
153 (ptypes count)
154 (no-function-values-types (continuation-proven-type cont))
155 (if (eq count :unknown)
156 (if (every #'type-check-template types)
157 (values :simple types)
158 (values :hairy
159 (mapcar #'(lambda (x)
160 (list nil (maybe-weaken-check x cont) x))
161 types)))
162 (let ((res (mapcar #'(lambda (p c)
163 (let ((diff (type-difference p c))
164 (weak (maybe-weaken-check c cont)))
165 (if (and diff
166 (< (type-test-cost diff)
167 (type-test-cost weak)))
168 (list t diff c)
169 (list nil weak c))))
170 ptypes types)))
171 (if (and (not (find-if #'first res))
172 (every #'type-check-template types))
173 (values :simple types)
174 (values :hairy res))))))
175
176
177 ;;; CONTINUATION-CHECK-TYPES -- Interface
178 ;;;
179 ;;; Determines whether Cont's assertion is:
180 ;;; -- Checkable by the back end (:SIMPLE), or
181 ;;; -- Not checkable by the back end, but checkable via an explicit test in
182 ;;; type check conversion (:HAIRY), or
183 ;;; -- not reasonably checkable at all (:TOO-HAIRY).
184 ;;;
185 ;;; A type is checkable if it either represents a fixed number of values (as
186 ;;; determined by VALUES-TYPES), or it is the assertion for an MV-Bind. A type
187 ;;; is simply checkable if all the type assertions have a TYPE-CHECK-TEMPLATE.
188 ;;; In this :SIMPLE case, the second value is a list of the type restrictions
189 ;;; specified for the leading positional values.
190 ;;;
191 ;;; In the :HAIRY case, the second value is a list of triples of the form:
192 ;;; (Not-P Type Original-Type)
193 ;;;
194 ;;; If true, the Not-P flag indicates a test that the corresponding value is
195 ;;; *not* of the specified Type. Original-Type is the type asserted on this
196 ;;; value in the continuation, for use in error messages. When Not-P is true,
197 ;;; this will be different from Type.
198 ;;;
199 ;;; This allows us to take what has been proven about Cont's type into
200 ;;; consideration. If it is cheaper to test for the difference between the
201 ;;; derived type and the asserted type, then we check for the negation of this
202 ;;; type instead.
203 ;;;
204 (defun continuation-check-types (cont)
205 (declare (type continuation cont))
206 (let ((type (continuation-asserted-type cont))
207 (dest (continuation-dest cont)))
208 (assert (not (eq type *wild-type*)))
209 (multiple-value-bind (types count)
210 (no-function-values-types type)
211 (cond ((not (eq count :unknown))
212 (maybe-negate-check cont types))
213 ((and (mv-combination-p dest)
214 (eq (basic-combination-kind dest) :local))
215 (assert (values-type-p type))
216 (maybe-negate-check cont (args-type-optional type)))
217 (t
218 (values :too-hairy nil))))))
219
220
221 ;;; Probable-Type-Check-P -- Internal
222 ;;;
223 ;;; Return true if Cont is a continuation whose type the back end is likely
224 ;;; to want to check. Since we don't know what template the back end is going
225 ;;; to choose to implement the continuation's DEST, we use a heuristic. We
226 ;;; always return T unless:
227 ;;; -- Nobody uses the value, or
228 ;;; -- Safety is totally unimportant, or
229 ;;; -- the continuation is an argument to an unknown function, or
230 ;;; -- the continuation is an argument to a known function that has no
231 ;;; IR2-Convert method or :fast-safe templates that are compatible with the
232 ;;; call's type.
233 ;;;
234 ;;; We must only return nil when it is *certain* that a check will not be done,
235 ;;; since if we pass up this chance to do the check, it will be too late. The
236 ;;; penalty for being too conservative is duplicated type checks.
237 ;;;
238 ;;; We always return true if there is a compile-time type error on the
239 ;;; continuation, so that this error will be signalled at runtime as well.
240 ;;;
241 (defun probable-type-check-p (cont)
242 (declare (type continuation cont))
243 (let ((dest (continuation-dest cont)))
244 (cond ((eq (continuation-type-check cont) :error))
245 ((or (not dest)
246 (policy dest (zerop safety)))
247 nil)
248 ((basic-combination-p dest)
249 (let ((kind (basic-combination-kind dest)))
250 (cond ((eq cont (basic-combination-fun dest)) t)
251 ((eq kind :local) t)
252 ((eq kind :full) nil)
253 ((function-info-ir2-convert kind) t)
254 (t
255 (dolist (template (function-info-templates kind) nil)
256 (when (eq (template-policy template) :fast-safe)
257 (multiple-value-bind
258 (val win)
259 (valid-function-use dest (template-type template))
260 (when (or val (not win)) (return t)))))))))
261 (t t))))
262
263
264 ;;; Make-Type-Check-Form -- Internal
265 ;;;
266 ;;; Return a form that we can convert to do a hairy type check of the
267 ;;; specified Types. Types is a list of the format returned by
268 ;;; Continuation-Check-Types in the :HAIRY case. In place of the actual
269 ;;; value(s) we are to check, we use 'Dummy. This constant reference is later
270 ;;; replaced with the actual values continuation.
271 ;;;
272 ;;; Note that we don't attempt to check for required values being unsupplied.
273 ;;; Such checking is impossible to efficiently do at the source level because
274 ;;; our fixed-values conventions are optimized for the common MV-Bind case.
275 ;;;
276 ;;; We can always use Multiple-Value-Bind, since the macro is clever about
277 ;;; binding a single variable.
278 ;;;
279 (defun make-type-check-form (types)
280 (collect ((temps))
281 (dotimes (i (length types))
282 (temps (gensym)))
283
284 `(multiple-value-bind ,(temps)
285 'dummy
286 ,@(mapcar #'(lambda (temp type)
287 (let* ((spec
288 (let ((*unparse-function-type-simplify* t))
289 (type-specifier (second type))))
290 (test (if (first type) `(not ,spec) spec)))
291 `(unless (typep ,temp ',test)
292 (%type-check-error
293 ,temp
294 ',(type-specifier (third type))))))
295 (temps) types)
296 (values ,@(temps)))))
297
298
299 ;;; Convert-Type-Check -- Internal
300 ;;;
301 ;;; Splice in explicit type check code immediately before the node that its
302 ;;; Cont's Dest. This code receives the value(s) that were being passed to
303 ;;; Cont, checks the type(s) of the value(s), then passes them on to Cont.
304 ;;; We:
305 ;;; -- Ensure that Cont starts a block, so that we can freely manipulate its
306 ;;; uses.
307 ;;; -- Make a new continuation and move Cont's uses to it. Set type set
308 ;;; Type-Check in Cont to :DELETED to indicate that the check has been
309 ;;; done.
310 ;;; -- Make the Dest node start its block so that we can splice in the type
311 ;;; check code.
312 ;;; -- Splice in a new block before the Dest block, giving it all the Dest's
313 ;;; predecessors.
314 ;;; -- Convert the check form, using the new block start as Start and a dummy
315 ;;; continuation as Cont.
316 ;;; -- Set the new block's start and end cleanups to the *start* cleanup of
317 ;;; Prev's block. This overrides the incorrect default from
318 ;;; With-IR1-Environment.
319 ;;; -- Finish off the dummy continuation's block, and change the use to a use
320 ;;; of Cont. (we need to use the dummy continuation to get the control
321 ;;; transfer right, since we want to go to Prev's block, not Cont's.)
322 ;;; Link the new block to Prev's block.
323 ;;; -- Substitute the new continuation for the dummy placeholder argument.
324 ;;; Since no let conversion has been done yet, we can find the placeholder.
325 ;;; The [mv-]combination node from the mv-bind in the check form will be
326 ;;; the Use of the new check continuation. We substitute for the first
327 ;;; argument of this node.
328 ;;; -- Invoke local call analysis to convert the call to a let.
329 ;;;
330 (defun convert-type-check (cont types)
331 (declare (type continuation cont) (list types))
332 (with-ir1-environment (continuation-dest cont)
333 (ensure-block-start cont)
334 (let* ((new-start (make-continuation))
335 (dest (continuation-dest cont))
336 (prev (node-prev dest)))
337 (continuation-starts-block new-start)
338 (substitute-continuation-uses new-start cont)
339 (setf (continuation-%type-check cont) :deleted)
340
341 (when (continuation-use prev)
342 (node-ends-block (continuation-use prev)))
343
344 (let* ((prev-block (continuation-block prev))
345 (prev-cleanup (block-start-cleanup prev-block))
346 (new-block (continuation-block new-start))
347 (dummy (make-continuation)))
348 (dolist (block (block-pred prev-block))
349 (change-block-successor block prev-block new-block))
350 (ir1-convert new-start dummy (make-type-check-form types))
351 (assert (eq (continuation-block dummy) new-block))
352
353 (setf (block-start-cleanup new-block) prev-cleanup)
354 (setf (block-end-cleanup new-block) prev-cleanup)
355
356 (let ((node (continuation-use dummy)))
357 (setf (block-last new-block) node)
358 (delete-continuation-use node)
359 (add-continuation-use node cont))
360 (link-blocks new-block prev-block))
361
362 (let* ((node (continuation-use cont))
363 (args (basic-combination-args node))
364 (victim (first args)))
365 (assert (and (= (length args) 1)
366 (eq (constant-value
367 (ref-leaf
368 (continuation-use victim)))
369 'dummy)))
370 (substitute-continuation new-start victim)))
371
372 (local-call-analyze *current-component*))
373
374 (undefined-value))
375
376
377 ;;; Generate-Type-Checks -- Interface
378 ;;;
379 ;;; Loop over all blocks in Component that have TYPE-CHECK set, looking for
380 ;;; continuations with TYPE-CHECK T. We do two mostly unrelated things: detect
381 ;;; compile-time type errors and determine if and how to do run-time type
382 ;;; checks.
383 ;;;
384 ;;; If there is a compile-time type error, then we mark the continuation
385 ;;; with a :ERROR kind, emit a warning if appropriate, and clear any
386 ;;; FUNCTION-INFO if the continuation is an argument to a known call. The last
387 ;;; is done so that the back end doesn't have to worry about type errors in
388 ;;; arguments to known functions.
389 ;;;
390 ;;; If a continuation is too complex to be checked by the back end, or is
391 ;;; better checked with explicit code, then convert to an explicit test.
392 ;;; Assertions that can checked by the back end are passed through. Assertions
393 ;;; that can't be tested are flamed about and marked as not needing to be
394 ;;; checked.
395 ;;;
396 ;;; If we determine that a type check won't be done, then we set TYPE-CHECK
397 ;;; to :NO-CHECK. In the non-hairy cases, this is just to prevent us from
398 ;;; wasting time coming to the same conclusion again on a later iteration. In
399 ;;; the hairy case, we must indicate to LTN that it must choose a safe
400 ;;; implementation, since IR2 conversion will choke on the check.
401 ;;;
402 (defun generate-type-checks (component)
403 (do-blocks (block component)
404 (when (block-type-check block)
405 (do-nodes (node cont block)
406 (when (eq (continuation-type-check cont) t)
407
408 (let ((dtype (node-derived-type node))
409 (atype (continuation-asserted-type cont)))
410 (unless (values-types-intersect dtype atype)
411 (setf (continuation-%type-check cont) :error)
412 (let ((dest (continuation-dest cont)))
413 (when (and (combination-p dest)
414 (function-info-p (basic-combination-kind dest)))
415 (setf (basic-combination-kind dest) :full)))
416 (unless (policy node (= brevity 3))
417 (let ((*compiler-error-context* node))
418 (if (and (ref-p node) (constant-p (ref-leaf node)))
419 (compiler-warning "This is not a ~S:~% ~S"
420 (type-specifier atype)
421 (constant-value (ref-leaf node)))
422 (compiler-warning "Result is a ~S, not a ~S."
423 (type-specifier dtype)
424 (type-specifier atype)))))))
425
426 (let ((check-p (probable-type-check-p cont)))
427 (multiple-value-bind (check types)
428 (continuation-check-types cont)
429 (ecase check
430 (:simple
431 (unless check-p
432 (setf (continuation-%type-check cont) :no-check)))
433 (:hairy
434 (if check-p
435 (convert-type-check cont types)
436 (setf (continuation-%type-check cont) :no-check)))
437 (:too-hairy
438 (let* ((context (continuation-dest cont))
439 (*compiler-error-context* context))
440 (when (policy context (>= safety brevity))
441 (compiler-note
442 "Type assertion too complex to check:~% ~S."
443 (type-specifier (continuation-asserted-type cont)))))
444 (setf (continuation-%type-check cont) :deleted)))))))
445
446 (setf (block-type-check block) nil)))
447
448 (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5