1 ;;;; This file implements type check generation. This is a phase that
2 ;;;; runs at the very end of IR1. If a type check is too complex for
3 ;;;; the back end to directly emit in-line, then we transform the check
4 ;;;; into an explicit conditional using TYPEP.
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
19 ;;; Return some sort of guess about the cost of a call to a function.
20 ;;; If the function has some templates, we return the cost of the
21 ;;; cheapest one, otherwise we return the cost of CALL-NAMED. Calling
22 ;;; this with functions that have transforms can result in relatively
23 ;;; meaningless results (exaggerated costs.)
25 ;;; We special-case NULL, since it does have a source tranform and is
26 ;;; interesting to us.
27 (defun fun-guessed-cost (name)
28 (declare (symbol name))
29 (let ((info (info :function :info name))
30 (call-cost (template-cost (template-or-lose 'call-named))))
32 (let ((templates (fun-info-templates info)))
34 (template-cost (first templates))
36 (null (template-cost (template-or-lose 'if-eq)))
40 ;;; Return some sort of guess for the cost of doing a test against
41 ;;; TYPE. The result need not be precise as long as it isn't way out
42 ;;; in space. The units are based on the costs specified for various
43 ;;; templates in the VM definition.
44 (defun type-test-cost (type)
45 (declare (type ctype type))
46 (or (let ((check (type-check-template type)))
49 (let ((found (cdr (assoc type *backend-type-predicates*
52 (+ (fun-guessed-cost found) (fun-guessed-cost 'eq))
56 (reduce #'+ (compound-type-types type) :key 'type-test-cost))
58 (* (length (member-type-members type))
59 (fun-guessed-cost 'eq)))
61 (* (if (numeric-type-complexp type) 2 1)
63 (if (csubtypep type (specifier-type 'fixnum)) 'fixnump 'numberp))
65 (if (numeric-type-low type) 1 0)
66 (if (numeric-type-high type) 1 0))))
68 (+ (type-test-cost (specifier-type 'cons))
69 (fun-guessed-cost 'car)
70 (type-test-cost (cons-type-car-type type))
71 (fun-guessed-cost 'cdr)
72 (type-test-cost (cons-type-cdr-type type))))
74 (fun-guessed-cost 'typep)))))
76 ;;;; checking strategy determination
78 ;;; Return the type we should test for when we really want to check
79 ;;; for TYPE. If speed, space or compilation speed is more important
80 ;;; than safety, then we return a weaker type if it is easier to
81 ;;; check. First we try the defined type weakenings, then look for any
82 ;;; predicate that is cheaper.
84 ;;; If the supertype is equal in cost to the type, we prefer the
85 ;;; supertype. This produces a closer approximation of the right thing
86 ;;; in the presence of poor cost info.
87 (defun maybe-weaken-check (type cont)
88 (declare (type ctype type) (type continuation cont))
89 (cond ((policy (continuation-dest cont)
90 (and (<= speed safety)
92 (<= compilation-speed safety)))
95 (let ((min-cost (type-test-cost type))
98 (dolist (x *backend-type-predicates*)
99 (let ((stype (car x)))
100 (when (and (csubtypep type stype)
101 (not (union-type-p stype)))
102 (let ((stype-cost (type-test-cost stype)))
103 (when (or (< stype-cost min-cost)
107 min-cost stype-cost))))))
110 *universal-type*)))))
112 ;;; This is like VALUES-TYPES, only we mash any complex function types
114 (defun no-fun-values-types (type)
115 (declare (type ctype type))
116 (multiple-value-bind (res count) (values-types type)
117 (values (mapcar (lambda (type)
118 (if (fun-type-p type)
119 (specifier-type 'function)
124 ;;; Switch to disable check complementing, for evaluation.
125 (defvar *complement-type-checks* t)
127 ;;; CONT is a continuation we are doing a type check on and TYPES is a
128 ;;; list of types that we are checking its values against. If we have
129 ;;; proven that CONT generates a fixed number of values, then for each
130 ;;; value, we check whether it is cheaper to then difference between
131 ;;; the proven type and the corresponding type in TYPES. If so, we opt
132 ;;; for a :HAIRY check with that test negated. Otherwise, we try to do
133 ;;; a simple test, and if that is impossible, we do a hairy test with
134 ;;; non-negated types. If true, FORCE-HAIRY forces a hairy type check.
136 ;;; When doing a non-negated check, we call MAYBE-WEAKEN-CHECK to
137 ;;; weaken the test to a convenient supertype (conditional on policy.)
138 ;;; If SPEED is 3, or DEBUG-INFO is not particularly important (DEBUG
139 ;;; <= 1), then we allow weakened checks to be simple, resulting in
140 ;;; less informative error messages, but saving space and possibly
143 ;;; FIXME: I don't quite understand this, but it looks as though
144 ;;; that means type checks are weakened when SPEED=3 regardless of
145 ;;; the SAFETY level, which is not the right thing to do.
146 (defun maybe-negate-check (cont types force-hairy)
147 (declare (type continuation cont) (list types))
148 (multiple-value-bind (ptypes count)
149 (no-fun-values-types (continuation-proven-type cont))
150 (if (eq count :unknown)
151 (if (and (every #'type-check-template types) (not force-hairy))
152 (values :simple types)
155 (list nil (maybe-weaken-check x cont) x))
157 (let ((res (mapcar (lambda (p c)
158 (let ((diff (type-difference p c))
159 (weak (maybe-weaken-check c cont)))
161 (< (type-test-cost diff)
162 (type-test-cost weak))
163 *complement-type-checks*)
167 (cond ((or force-hairy (find-if #'first res))
169 ((every #'type-check-template types)
170 (values :simple types))
171 ((policy (continuation-dest cont)
172 (or (<= debug 1) (and (= speed 3) (/= debug 3))))
173 (let ((weakened (mapcar #'second res)))
174 (if (every #'type-check-template weakened)
175 (values :simple weakened)
176 (values :hairy res))))
178 (values :hairy res)))))))
180 ;;; Determines whether CONT's assertion is:
181 ;;; -- checkable by the back end (:SIMPLE), or
182 ;;; -- not checkable by the back end, but checkable via an explicit
183 ;;; test in type check conversion (:HAIRY), or
184 ;;; -- not reasonably checkable at all (:TOO-HAIRY).
186 ;;; A type is checkable if it either represents a fixed number of
187 ;;; values (as determined by VALUES-TYPES), or it is the assertion for
188 ;;; an MV-BIND. A type is simply checkable if all the type assertions
189 ;;; have a TYPE-CHECK-TEMPLATE. In this :SIMPLE case, the second value
190 ;;; is a list of the type restrictions specified for the leading
191 ;;; positional values.
193 ;;; We force a check to be hairy even when there are fixed values if
194 ;;; we are in a context where we may be forced to use the unknown
195 ;;; values convention anyway. This is because IR2tran can't generate
196 ;;; type checks for unknown values continuations but people could
197 ;;; still be depending on the check being done. We only care about
198 ;;; EXIT and RETURN (not MV-COMBINATION) since these are the only
199 ;;; contexts where the ultimate values receiver
201 ;;; In the :HAIRY case, the second value is a list of triples of
203 ;;; (NOT-P TYPE ORIGINAL-TYPE)
205 ;;; If true, the NOT-P flag indicates a test that the corresponding
206 ;;; value is *not* of the specified TYPE. ORIGINAL-TYPE is the type
207 ;;; asserted on this value in the continuation, for use in error
208 ;;; messages. When NOT-P is true, this will be different from TYPE.
210 ;;; This allows us to take what has been proven about CONT's type into
211 ;;; consideration. If it is cheaper to test for the difference between
212 ;;; the derived type and the asserted type, then we check for the
213 ;;; negation of this type instead.
214 (defun continuation-check-types (cont)
215 (declare (type continuation cont))
216 (let ((type (continuation-asserted-type cont))
217 (dest (continuation-dest cont)))
218 (aver (not (eq type *wild-type*)))
219 (multiple-value-bind (types count) (no-fun-values-types type)
220 (cond ((not (eq count :unknown))
221 (if (or (exit-p dest)
223 (multiple-value-bind (ignore count)
224 (values-types (return-result-type dest))
225 (declare (ignore ignore))
226 (eq count :unknown))))
227 (maybe-negate-check cont types t)
228 (maybe-negate-check cont types nil)))
229 ((and (mv-combination-p dest)
230 (eq (basic-combination-kind dest) :local))
231 (aver (values-type-p type))
232 (maybe-negate-check cont (args-type-optional type) nil))
234 (values :too-hairy nil))))))
236 ;;; Return true if CONT is a continuation whose type the back end is
237 ;;; likely to want to check. Since we don't know what template the
238 ;;; back end is going to choose to implement the continuation's DEST,
239 ;;; we use a heuristic. We always return T unless:
240 ;;; -- nobody uses the value, or
241 ;;; -- safety is totally unimportant, or
242 ;;; -- the continuation is an argument to an unknown function, or
243 ;;; -- the continuation is an argument to a known function that has
244 ;;; no IR2-CONVERT method or :FAST-SAFE templates that are
245 ;;; compatible with the call's type.
247 ;;; We must only return NIL when it is *certain* that a check will not
248 ;;; be done, since if we pass up this chance to do the check, it will
249 ;;; be too late. The penalty for being too conservative is duplicated
250 ;;; type checks. The penalty for erring by being too speculative is
251 ;;; much nastier, e.g. falling through without ever being able to find
252 ;;; an appropriate VOP.
253 (defun probable-type-check-p (cont)
254 (declare (type continuation cont))
255 (let ((dest (continuation-dest cont)))
256 (cond ((or (not dest)
257 (policy dest (zerop safety)))
259 ((basic-combination-p dest)
260 (let ((kind (basic-combination-kind dest)))
261 (cond ((eq cont (basic-combination-fun dest)) t)
263 ((mv-combination-p dest)
266 ((not (eq (continuation-asserted-type cont)
267 (continuation-externally-checkable-type cont)))
268 ;; There is an explicit assertion.
271 ;; The theory is that the type assertion is from a
272 ;; declaration in (or on) the callee, so the
273 ;; callee should be able to do the check. We want
274 ;; to let the callee do the check, because it is
275 ;; possible that by the time of call that
276 ;; declaration will be changed and we do not want
277 ;; to make people recompile all calls to a
278 ;; function when they were originally compiled
279 ;; with a bad declaration. (See also bug 35.)
282 ((eq kind :error) nil)
283 ;; :ERROR means that we have an invalid syntax of
284 ;; the call and the callee will detect it before
285 ;; thinking about types.
287 ((fun-info-ir2-convert kind) t)
289 (dolist (template (fun-info-templates kind) nil)
290 (when (eq (template-ltn-policy template) :fast-safe)
291 (multiple-value-bind (val win)
292 (valid-fun-use dest (template-type template))
293 (when (or val (not win)) (return t)))))))))
296 ;;; Return a form that we can convert to do a hairy type check of the
297 ;;; specified TYPES. TYPES is a list of the format returned by
298 ;;; CONTINUATION-CHECK-TYPES in the :HAIRY case. In place of the
299 ;;; actual value(s) we are to check, we use 'DUMMY. This constant
300 ;;; reference is later replaced with the actual values continuation.
302 ;;; Note that we don't attempt to check for required values being
303 ;;; unsupplied. Such checking is impossible to efficiently do at the
304 ;;; source level because our fixed-values conventions are optimized
305 ;;; for the common MV-BIND case.
307 ;;; We can always use MULTIPLE-VALUE-BIND, since the macro is clever
308 ;;; about binding a single variable.
309 (defun make-type-check-form (types)
310 (let ((temps (make-gensym-list (length types))))
311 `(multiple-value-bind ,temps 'dummy
312 ,@(mapcar (lambda (temp type)
314 (let ((*unparse-fun-type-simplify* t))
315 (type-specifier (second type))))
316 (test (if (first type) `(not ,spec) spec)))
317 `(unless (typep ,temp ',test)
320 ',(type-specifier (third type))))))
325 ;;; Splice in explicit type check code immediately before the node
326 ;;; which is CONT's DEST. This code receives the value(s) that were
327 ;;; being passed to CONT, checks the type(s) of the value(s), then
328 ;;; passes them on to CONT.
329 (defun convert-type-check (cont types)
330 (declare (type continuation cont) (type list types))
331 (with-ir1-environment-from-node (continuation-dest cont)
333 ;; Ensuring that CONT starts a block lets us freely manipulate its uses.
334 (ensure-block-start cont)
336 ;; Make a new continuation and move CONT's uses to it.
337 (let* ((new-start (make-continuation))
338 (dest (continuation-dest cont))
339 (prev (node-prev dest)))
340 (continuation-starts-block new-start)
341 (substitute-continuation-uses new-start cont)
343 ;; Setting TYPE-CHECK in CONT to :DELETED indicates that the
344 ;; check has been done.
345 (setf (continuation-%type-check cont) :deleted)
347 ;; Make the DEST node start its block so that we can splice in
348 ;; the type check code.
349 (when (continuation-use prev)
350 (node-ends-block (continuation-use prev)))
352 (let* ((prev-block (continuation-block prev))
353 (new-block (continuation-block new-start))
354 (dummy (make-continuation)))
356 ;; Splice in the new block before DEST, giving the new block
357 ;; all of DEST's predecessors.
358 (dolist (block (block-pred prev-block))
359 (change-block-successor block prev-block new-block))
361 ;; Convert the check form, using the new block start as START
362 ;; and a dummy continuation as CONT.
363 (ir1-convert new-start dummy (make-type-check-form types))
365 ;; TO DO: Why should this be true? -- WHN 19990601
366 (aver (eq (continuation-block dummy) new-block))
368 ;; KLUDGE: Comments at the head of this function in CMU CL
369 ;; said that somewhere in here we
370 ;; Set the new block's start and end cleanups to the *start*
371 ;; cleanup of PREV's block. This overrides the incorrect
372 ;; default from WITH-IR1-ENVIRONMENT-FROM-NODE.
373 ;; Unfortunately I can't find any code which corresponds to this.
374 ;; Perhaps it was a stale comment? Or perhaps I just don't
375 ;; understand.. -- WHN 19990521
377 (let ((node (continuation-use dummy)))
378 (setf (block-last new-block) node)
379 ;; Change the use to a use of CONT. (We need to use the
380 ;; dummy continuation to get the control transfer right,
381 ;; because we want to go to PREV's block, not CONT's.)
382 (delete-continuation-use node)
383 (add-continuation-use node cont))
384 ;; Link the new block to PREV's block.
385 (link-blocks new-block prev-block))
387 ;; MAKE-TYPE-CHECK-FORM generated a form which checked the type
388 ;; of 'DUMMY, not a real form. At this point we convert to the
389 ;; real form by finding 'DUMMY and overwriting it with the new
390 ;; continuation. (We can find 'DUMMY because no LET conversion
391 ;; has been done yet.) The [mv-]combination code from the
392 ;; mv-bind in the check form will be the use of the new check
393 ;; continuation. We substitute for the first argument of this
395 (let* ((node (continuation-use cont))
396 (args (basic-combination-args node))
397 (victim (first args)))
398 (aver (and (= (length args) 1)
401 (continuation-use victim)))
403 (substitute-continuation new-start victim)))
405 ;; Invoking local call analysis converts this call to a LET.
406 (locall-analyze-component *current-component*))
410 ;;; Emit a type warning for NODE. If the value of NODE is being used
411 ;;; for a variable binding, we figure out which one for source
412 ;;; context. If the value is a constant, we print it specially. We
413 ;;; ignore nodes whose type is NIL, since they are supposed to never
415 (defun emit-type-warning (node)
416 (declare (type node node))
417 (let* ((*compiler-error-context* node)
418 (cont (node-cont node))
419 (atype-spec (type-specifier (continuation-asserted-type cont)))
420 (dtype (node-derived-type node))
421 (dest (continuation-dest cont))
422 (what (when (and (combination-p dest)
423 (eq (combination-kind dest) :local))
424 (let ((lambda (combination-lambda dest))
425 (pos (position-or-lose cont (combination-args dest))))
426 (format nil "~:[A possible~;The~] binding of ~S"
427 (and (continuation-use cont)
428 (eq (functional-kind lambda) :let))
429 (leaf-source-name (elt (lambda-vars lambda)
431 (cond ((eq dtype *empty-type*))
432 ((and (ref-p node) (constant-p (ref-leaf node)))
433 (compiler-warn "~:[This~;~:*~A~] is not a ~<~%~9T~:;~S:~>~% ~S"
434 what atype-spec (constant-value (ref-leaf node))))
437 "~:[Result~;~:*~A~] is a ~S, ~<~%~9T~:;not a ~S.~>"
438 what (type-specifier dtype) atype-spec))))
441 ;;; Loop over all blocks in COMPONENT that have TYPE-CHECK set,
442 ;;; looking for continuations with TYPE-CHECK T. We do two mostly
443 ;;; unrelated things: detect compile-time type errors and determine if
444 ;;; and how to do run-time type checks.
446 ;;; If there is a compile-time type error, then we mark the
447 ;;; continuation and emit a warning if appropriate. This part loops
448 ;;; over all the uses of the continuation, since after we convert the
449 ;;; check, the :DELETED kind will inhibit warnings about the types of
452 ;;; If a continuation is too complex to be checked by the back end, or
453 ;;; is better checked with explicit code, then convert to an explicit
454 ;;; test. Assertions that can checked by the back end are passed
455 ;;; through. Assertions that can't be tested are flamed about and
456 ;;; marked as not needing to be checked.
458 ;;; If we determine that a type check won't be done, then we set
459 ;;; TYPE-CHECK to :NO-CHECK. In the non-hairy cases, this is just to
460 ;;; prevent us from wasting time coming to the same conclusion again
461 ;;; on a later iteration. In the hairy case, we must indicate to LTN
462 ;;; that it must choose a safe implementation, since IR2 conversion
463 ;;; will choke on the check.
465 ;;; The generation of the type checks is delayed until all the type
466 ;;; check decisions have been made because the generation of the type
467 ;;; checks creates new nodes whose derived types aren't always updated
468 ;;; which may lead to inappropriate template choices due to the
469 ;;; modification of argument types.
470 (defun generate-type-checks (component)
472 (do-blocks (block component)
473 (when (block-type-check block)
474 (do-nodes (node cont block)
475 (let ((type-check (continuation-type-check cont)))
476 (unless (member type-check '(nil :deleted))
477 (let ((atype (continuation-asserted-type cont)))
479 (unless (values-types-equal-or-intersect
480 (node-derived-type use) atype)
481 (unless (policy node (= inhibit-warnings 3))
482 (emit-type-warning use))))))
483 (when (eq type-check t)
484 (cond ((probable-type-check-p cont)
487 (setf (continuation-%type-check cont) :no-check))))))
488 (setf (block-type-check block) nil)))
489 (dolist (cont (conts))
490 (multiple-value-bind (check types) (continuation-check-types cont)
494 (convert-type-check cont types))
496 (let* ((context (continuation-dest cont))
497 (*compiler-error-context* context))
498 (when (policy context (>= safety inhibit-warnings))
500 "type assertion too complex to check:~% ~S."
501 (type-specifier (continuation-asserted-type cont)))))
502 (setf (continuation-%type-check cont) :deleted))))))