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 (when (eq type
*universal-type
*)
48 (when (eq type
*empty-type
*)
50 (let ((found (cdr (assoc type
*backend-type-predicates
*
53 (+ (fun-guessed-cost found
) (fun-guessed-cost 'eq
))
57 (reduce #'+ (compound-type-types type
) :key
'type-test-cost
))
59 (* (member-type-size type
)
60 (fun-guessed-cost 'eq
)))
62 (* (if (numeric-type-complexp type
) 2 1)
64 (if (csubtypep type
(specifier-type 'fixnum
)) 'fixnump
'numberp
))
66 (if (numeric-type-low type
) 1 0)
67 (if (numeric-type-high type
) 1 0))))
69 (+ (type-test-cost (specifier-type 'cons
))
70 (fun-guessed-cost 'car
)
71 (type-test-cost (cons-type-car-type type
))
72 (fun-guessed-cost 'cdr
)
73 (type-test-cost (cons-type-cdr-type type
))))
75 (fun-guessed-cost 'typep
)))))
77 (defun weaken-integer-type (type &key range-only
)
78 ;; FIXME: Our canonicalization isn't quite ideal for this. We get
81 ;; (OR (AND (SATISFIES FOO) (INTEGER -100 -50))
82 ;; (AND (SATISFIES FOO) (INTEGER 100 200)))
84 ;; here, and weakening that into
86 ;; (AND (SATISFIES FOO) (INTEGER -100 200))
88 ;; is too much work to do here ... but if we canonicalized things
89 ;; differently, we could get it for free with trivial changes here.
90 (labels ((weaken-integer-type-part (type base
)
91 (cond ((intersection-type-p type
)
92 (let ((new (specifier-type base
)))
93 (dolist (part (intersection-type-types type
))
96 (not (unknown-type-p part
)))
97 (setf new
(type-intersection
98 new
(weaken-integer-type-part part t
)))))
101 (let ((low t
) (high t
) (rest *empty-type
*))
102 (flet ((maximize (bound)
104 (setf high
(if (eq t high
)
110 (setf low
(if (eq t low
)
114 (dolist (part (union-type-types type
))
115 (let ((weak (weaken-integer-type-part part t
)))
116 (cond ((numeric-type-p weak
)
117 (minimize (numeric-type-low weak
))
118 (maximize (numeric-type-high weak
)))
120 (setf rest
(type-union rest weak
)))))))
125 `(integer ,(or low
'*) ,(or high
'*)))))))
128 (weaken-integer-type-part type
'integer
)))
131 (weaken-type :hash-bits
7 :hash-function
#'type-hash-value
)
133 (declare (type ctype type
))
134 (cond ((named-type-p type
)
136 ((csubtypep type
(specifier-type 'integer
))
137 ;; Simple range checks are not that expensive, and we *don't*
138 ;; want to accidentally lose eg. array bounds checks due to
139 ;; weakening, so for integer types we simply collapse all
141 (weaken-integer-type type
))
143 (let ((min-cost (type-test-cost type
))
146 (dolist (x *backend-type-predicates
*)
147 (let* ((stype (car x
))
148 (samep (type= stype type
)))
150 (and (csubtypep type stype
)
151 (not (union-type-p stype
))))
152 (let ((stype-cost (type-test-cost stype
)))
153 (when (or (< stype-cost min-cost
)
155 ;; If the supertype is equal in cost to the type, we
156 ;; prefer the supertype. This produces a closer
157 ;; approximation of the right thing in the presence of
161 min-cost stype-cost
))))))
162 ;; This used to return the *UNIVERSAL-TYPE* if no supertype was found,
163 ;; but that's too liberal: it's far too easy for the user to create
164 ;; a union type (which are excluded above), and then trick the compiler
165 ;; into trusting the union type... and finally ending up corrupting the
166 ;; heap once a bad object sneaks past the missing type check.
171 (defun weaken-values-type (type)
172 (declare (type ctype type
))
173 (cond ((eq type
*wild-type
*) type
)
174 ((not (values-type-p type
))
177 (make-values-type :required
(mapcar #'weaken-type
178 (values-type-required type
))
179 :optional
(mapcar #'weaken-type
180 (values-type-optional type
))
181 :rest
(acond ((values-type-rest type
)
182 (weaken-type it
)))))))
184 ;;;; checking strategy determination
186 ;;; Return the type we should test for when we really want to check
187 ;;; for TYPE. If type checking policy is "fast", then we return a
188 ;;; weaker type if it is easier to check. First we try the defined
189 ;;; type weakenings, then look for any predicate that is cheaper.
190 (defun maybe-weaken-check (type policy
)
191 (declare (type ctype type
))
192 (ecase (policy policy type-check
)
194 (2 (weaken-values-type type
))
197 ;;; LVAR is an lvar we are doing a type check on and TYPES is a list
198 ;;; of types that we are checking its values against. If we have
199 ;;; proven that LVAR generates a fixed number of values, then for each
200 ;;; value, we check whether it is cheaper to then difference between
201 ;;; the proven type and the corresponding type in TYPES. If so, we opt
202 ;;; for a :HAIRY check with that test negated. Otherwise, we try to do
203 ;;; a simple test, and if that is impossible, we do a hairy test with
204 ;;; non-negated types. If true, FORCE-HAIRY forces a hairy type check.
205 (defun maybe-negate-check (lvar types original-types n-required
)
206 (declare (type lvar lvar
) (list types original-types
))
207 (let ((ptypes (values-type-out (lvar-derived-type lvar
) (length types
))))
208 (loop for p in ptypes
210 and a in original-types
212 for cc
= (if (>= i n-required
)
213 (type-union c
(specifier-type 'null
))
215 for diff
= (type-difference p cc
)
216 collect
(if (and diff
217 (< (type-test-cost diff
)
218 (type-test-cost cc
)))
222 ;;; Determine if CAST can be checked.
223 ;;; We may check only fixed number of values; in any case the number
224 ;;; of generated values is trusted. If we know the number of produced
225 ;;; values, all of them are checked; otherwise if we know the number
226 ;;; of consumed -- only they are checked; otherwise the check is not
229 ;;; In the types are checkable it returns :SIMPLE and the second value
231 ;;; (NOT-P TYPE ORIGINAL-TYPE)
233 ;;; If true, the NOT-P flag indicates a test that the corresponding
234 ;;; value is *not* of the specified TYPE. ORIGINAL-TYPE is the type
235 ;;; asserted on this value in the lvar, for use in error
236 ;;; messages. When NOT-P is true, this will be different from TYPE.
238 ;;; This allows us to take what has been proven about CAST's argument
239 ;;; type into consideration. If it is cheaper to test for the
240 ;;; difference between the derived type and the asserted type, then we
241 ;;; check for the negation of this type instead.
242 (defun cast-check-types (cast)
243 (declare (type cast cast
))
244 (let* ((ctype (coerce-to-values (cast-type-to-check cast
)))
245 (atype (coerce-to-values (cast-asserted-type cast
)))
246 (dtype (node-derived-type cast
))
247 (value (cast-value cast
))
248 (lvar (node-lvar cast
))
249 (dest (and lvar
(lvar-dest lvar
)))
250 (n-consumed (cond ((not lvar
)
252 ((lvar-single-value-p lvar
)
254 ((and (mv-combination-p dest
)
255 (eq (mv-combination-kind dest
) :local
))
256 (let ((fun-ref (lvar-use (mv-combination-fun dest
))))
257 (length (lambda-vars (ref-leaf fun-ref
)))))))
258 (n-required (length (values-type-required dtype
))))
259 (aver (not (eq ctype
*wild-type
*)))
260 (cond ((and (null (values-type-optional dtype
))
261 (not (values-type-rest dtype
)))
262 ;; we [almost] know how many values are produced
264 (maybe-negate-check value
265 (values-type-out ctype n-required
)
266 (values-type-out atype n-required
)
268 ((lvar-single-value-p lvar
)
269 ;; exactly one value is consumed
270 (principal-lvar-single-valuify lvar
)
271 (flet ((get-type (type)
272 (acond ((args-type-required type
)
274 ((args-type-optional type
)
276 (t (bug "type ~S is too hairy" type
)))))
277 (multiple-value-bind (ctype atype
)
278 (values (get-type ctype
) (get-type atype
))
279 (values :simple
(maybe-negate-check value
280 (list ctype
) (list atype
)
282 ((and (mv-combination-p dest
)
283 (eq (mv-combination-kind dest
) :local
))
284 ;; we know the number of consumed values
285 (values :simple
(maybe-negate-check value
286 (adjust-list (values-type-types ctype
)
289 (adjust-list (values-type-types atype
)
294 (values :too-hairy nil
)))))
296 ;;; Return T is the cast appears to be from the declaration of the callee,
297 ;;; and should be checked externally -- that is, by the callee and not the caller.
298 (defun cast-externally-checkable-p (cast)
299 (declare (type cast cast
))
300 (let* ((lvar (node-lvar cast
))
301 (dest (and lvar
(lvar-dest lvar
))))
302 (and (combination-p dest
)
303 ;; The theory is that the type assertion is from a declaration on the
304 ;; callee, so the callee should be able to do the check. We want to
305 ;; let the callee do the check, because it is possible that by the
306 ;; time of call that declaration will be changed and we do not want
307 ;; to make people recompile all calls to a function when they were
308 ;; originally compiled with a bad declaration.
310 ;; ALMOST-IMMEDIATELY-USED-P ensures that we don't delegate casts
311 ;; that occur before nodes that can cause observable side effects --
312 ;; most commonly other non-external casts: so the order in which
313 ;; possible type errors are signalled matches with the evaluation
316 ;; FIXME: We should let more cases be handled by the callee then we
317 ;; currently do, see: https://bugs.launchpad.net/sbcl/+bug/309104
318 ;; This is not fixable quite here, though, because flow-analysis has
319 ;; deleted the LVAR of the cast by the time we get here, so there is
320 ;; no destination. Perhaps we should mark cases inserted by
321 ;; ASSERT-CALL-TYPE explicitly, and delete those whose destination is
322 ;; deemed unreachable?
323 (almost-immediately-used-p lvar cast
)
324 (values (values-subtypep (lvar-externally-checkable-type lvar
)
325 (cast-type-to-check cast
))))))
327 ;; Type specifiers handled by the general-purpose MAKE-TYPE-CHECK-FORM are often
328 ;; trivial enough to have an internal error number assigned to them that can be
329 ;; used in lieu of OBJECT-NOT-TYPE-ERROR. On x86-64 this saves 16 bytes: 1 word
330 ;; for the symbol in the function's constant area, a MOV instruction to load it,
331 ;; and an sc-offset in the error trap.
332 (defglobal **type-spec-interr-symbols
**
334 ;; read-time-eval so that during cold-init we can recreate the
335 ;; table using the target's sxhash function, but without relying
336 ;; on readiness of the type system for parsing/unparsing specifiers.
339 (cons (type-specifier (specifier-type (car entry
)))
341 (remove-if #'stringp sb
!c
:+backend-internal-errors
+
343 ;; This is effectively a compact read-only binned hashtable.
344 (hashtable (make-array (logior (length entries
) 1)
345 :initial-element nil
)))
348 (let* ((canon-type (car entry
))
349 (bucket (mod (sxhash canon-type
) (length hashtable
))))
350 (push entry
(svref hashtable bucket
))))
353 (defun %interr-symbol-for-type-spec
(spec)
354 (let ((table **type-spec-interr-symbols
**))
355 (cdr (assoc spec
(svref table
(rem (sxhash spec
) (length table
)))
357 #+nil
; some meta-analysis to decide what types should be in "generic/interr"
359 (defvar *checkgen-used-types
* (make-hash-table :test
'equal
))
360 (defun interr-symbol-for-type-spec (spec)
361 (let ((answer (%interr-symbol-for-type-spec spec
))
362 (meta (gethash spec
*checkgen-used-types
*)))
363 ;; spec -> (count . primitive-p)
366 (setf (gethash spec
*checkgen-used-types
*) (cons 1 answer
)))
369 ;;; Return a lambda form that we can convert to do a type check
370 ;;; of the specified TYPES. TYPES is a list of the format returned by
371 ;;; CAST-CHECK-TYPES.
373 ;;; Note that we don't attempt to check for required values being
374 ;;; unsupplied. Such checking is impossible to efficiently do at the
375 ;;; source level because our fixed-values conventions are optimized
376 ;;; for the common MV-BIND case.
377 (defun make-type-check-form (types)
378 (let ((temps (make-gensym-list (length types
))))
379 `(multiple-value-bind ,temps
'dummy
380 ,@(mapcar (lambda (temp type
)
382 (let ((*unparse-fun-type-simplify
* t
))
383 (type-specifier (second type
))))
384 (test (if (first type
) `(not ,spec
) spec
))
385 (external-spec (type-specifier (third type
)))
387 (%interr-symbol-for-type-spec external-spec
)))
388 `(unless (typep ,temp
',test
)
390 `(%type-check-error
/c
,temp
',interr-symbol
)
391 `(%type-check-error
,temp
',external-spec
)))))
396 ;;; Splice in explicit type check code immediately before CAST. This
397 ;;; code receives the value(s) that were being passed to CAST-VALUE,
398 ;;; checks the type(s) of the value(s), then passes them further.
399 (defun convert-type-check (cast types
)
400 (declare (type cast cast
) (type list types
))
401 (let ((value (cast-value cast
))
402 (length (length types
)))
403 (filter-lvar value
(make-type-check-form types
))
404 (reoptimize-lvar (cast-value cast
))
405 (setf (cast-type-to-check cast
) *wild-type
*)
406 (setf (cast-%type-check cast
) nil
)
407 (let* ((atype (cast-asserted-type cast
))
408 (atype (cond ((not (values-type-p atype
))
411 (single-value-type atype
))
414 :required
(values-type-out atype length
)))))
415 (dtype (node-derived-type cast
))
416 (dtype (make-values-type
417 :required
(values-type-out dtype length
))))
418 (setf (cast-asserted-type cast
) atype
)
419 (setf (node-derived-type cast
) dtype
)))
423 ;;; Check all possible arguments of CAST and emit type warnings for
424 ;;; those with type errors. If the value of USE is being used for a
425 ;;; variable binding, we figure out which one for source context. If
426 ;;; the value is a constant, we print it specially.
427 (defun cast-check-uses (cast)
428 (declare (type cast cast
))
429 (let* ((lvar (node-lvar cast
))
430 (dest (and lvar
(lvar-dest lvar
)))
431 (value (cast-value cast
))
432 (atype (cast-asserted-type cast
))
433 (condition 'type-warning
)
436 (let ((dtype (node-derived-type use
)))
437 (if (values-types-equal-or-intersect dtype atype
)
438 (setf condition
'type-style-warning
)
439 (push use not-ok-uses
))))
440 (dolist (use (nreverse not-ok-uses
))
441 (let* ((*compiler-error-context
* use
)
442 (dtype (node-derived-type use
))
443 (atype-spec (type-specifier atype
))
444 (what (when (and (combination-p dest
)
445 (eq (combination-kind dest
) :local
))
446 (let ((lambda (combination-lambda dest
))
447 (pos (position-or-lose
448 lvar
(combination-args dest
))))
449 (format nil
"~:[A possible~;The~] binding of ~S"
450 (and (lvar-has-single-use-p lvar
)
451 (eq (functional-kind lambda
) :let
))
452 (leaf-source-name (elt (lambda-vars lambda
)
454 (cond ((and (ref-p use
) (constant-p (ref-leaf use
)))
457 "~:[This~;~:*~A~] is not a ~<~%~9T~:;~S:~>~% ~S"
459 (list what atype-spec
460 (constant-value (ref-leaf use
)))))
464 "~:[Result~;~:*~A~] is a ~S, ~<~%~9T~:;not a ~S.~>"
466 (list what
(type-specifier dtype
) atype-spec
)))))))
469 ;;; Loop over all blocks in COMPONENT that have TYPE-CHECK set,
470 ;;; looking for CASTs with TYPE-CHECK T. We do two mostly unrelated
471 ;;; things: detect compile-time type errors and determine if and how
472 ;;; to do run-time type checks.
474 ;;; If there is a compile-time type error, then we mark the CAST and
475 ;;; emit a warning if appropriate. This part loops over all the uses
476 ;;; of the continuation, since after we convert the check, the
477 ;;; :DELETED kind will inhibit warnings about the types of other uses.
479 ;;; If the cast is too complex to be checked by the back end, or is
480 ;;; better checked with explicit code, then convert to an explicit
481 ;;; test. Assertions that can checked by the back end are passed
482 ;;; through. Assertions that can't be tested are flamed about and
483 ;;; marked as not needing to be checked.
485 ;;; If we determine that a type check won't be done, then we set
486 ;;; TYPE-CHECK to :NO-CHECK. In the non-hairy cases, this is just to
487 ;;; prevent us from wasting time coming to the same conclusion again
488 ;;; on a later iteration. In the hairy case, we must indicate to LTN
489 ;;; that it must choose a safe implementation, since IR2 conversion
490 ;;; will choke on the check.
492 ;;; The generation of the type checks is delayed until all the type
493 ;;; check decisions have been made because the generation of the type
494 ;;; checks creates new nodes whose derived types aren't always updated
495 ;;; which may lead to inappropriate template choices due to the
496 ;;; modification of argument types.
497 (defun generate-type-checks (component)
499 (do-blocks (block component
)
500 (when (and (block-type-check block
)
501 (not (block-delete-p block
)))
502 ;; CAST-EXTERNALLY-CHECKABLE-P wants the backward pass
503 (do-nodes-backwards (node nil block
)
504 (when (and (cast-p node
)
505 (cast-type-check node
))
506 (cast-check-uses node
)
507 (cond ((cast-externally-checkable-p node
)
508 (setf (cast-%type-check node
) :external
))
510 ;; it is possible that NODE was marked :EXTERNAL by
512 (setf (cast-%type-check node
) t
)
514 (setf (block-type-check block
) nil
)))
515 (dolist (cast (casts))
516 (unless (bound-cast-p cast
)
517 (multiple-value-bind (check types
) (cast-check-types cast
)
520 (convert-type-check cast types
))
522 (let ((*compiler-error-context
* cast
))
523 (when (policy cast
(>= safety inhibit-warnings
))
525 "type assertion too complex to check:~% ~S."
526 (type-specifier (coerce-to-values (cast-asserted-type cast
))))))
527 (setf (cast-type-to-check cast
) *wild-type
*)
528 (setf (cast-%type-check cast
) nil
)))))))