Fix comment about *code-coverage-info*.
[sbcl.git] / src / compiler / checkgen.lisp
blobd9892cec06b01fa8cb2b5eb61e320cf6a209fb3f
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
7 ;;;; more information.
8 ;;;;
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.
15 (in-package "SB!C")
17 ;;;; cost estimation
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.)
24 ;;;
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))))
31 (if info
32 (let ((templates (fun-info-templates info)))
33 (if templates
34 (template-cost (first templates))
35 (case name
36 (null (template-cost (template-or-lose 'if-eq)))
37 (t call-cost))))
38 call-cost)))
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*
51 :test #'type=))))
52 (if found
53 (+ (fun-guessed-cost found) (fun-guessed-cost 'eq))
54 nil))
55 (typecase type
56 (compound-type
57 (reduce #'+ (compound-type-types type) :key 'type-test-cost))
58 (member-type
59 (* (member-type-size type)
60 (fun-guessed-cost 'eq)))
61 (numeric-type
62 (* (if (numeric-type-complexp type) 2 1)
63 (fun-guessed-cost
64 (if (csubtypep type (specifier-type 'fixnum)) 'fixnump 'numberp))
65 (+ 1
66 (if (numeric-type-low type) 1 0)
67 (if (numeric-type-high type) 1 0))))
68 (cons-type
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
79 ;; types such as:
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))
94 (when (if range-only
95 (numeric-type-p part)
96 (not (unknown-type-p part)))
97 (setf new (type-intersection
98 new (weaken-integer-type-part part t)))))
99 new))
100 ((union-type-p type)
101 (let ((low t) (high t) (rest *empty-type*))
102 (flet ((maximize (bound)
103 (if (and bound high)
104 (setf high (if (eq t high)
105 bound
106 (max high bound)))
107 (setf high nil)))
108 (minimize (bound)
109 (if (and bound low)
110 (setf low (if (eq t low)
111 bound
112 (min low bound)))
113 (setf low nil))))
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)))
119 ((not range-only)
120 (setf rest (type-union rest weak)))))))
121 (if (eq t low)
122 rest
123 (type-union rest
124 (specifier-type
125 `(integer ,(or low '*) ,(or high '*)))))))
127 type))))
128 (weaken-integer-type-part type 'integer)))
130 (defun-cached
131 (weaken-type :hash-bits 7 :hash-function #'type-hash-value)
132 ((type eq))
133 (declare (type ctype type))
134 (cond ((named-type-p type)
135 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
140 ;; ranges into one.
141 (weaken-integer-type type))
143 (let ((min-cost (type-test-cost type))
144 (min-type type)
145 (found-super nil))
146 (dolist (x *backend-type-predicates*)
147 (let* ((stype (car x))
148 (samep (type= stype type)))
149 (when (or samep
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)
154 samep)
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
158 ;; poor cost info.
159 (setq found-super t
160 min-type stype
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.
167 (if found-super
168 min-type
169 type)))))
171 (defun weaken-values-type (type)
172 (declare (type ctype type))
173 (cond ((eq type *wild-type*) type)
174 ((not (values-type-p type))
175 (weaken-type 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)
193 (0 *wild-type*)
194 (2 (weaken-values-type type))
195 (3 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
209 and c in types
210 and a in original-types
211 and i from 0
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)))
219 (list t diff a)
220 (list nil cc a)))))
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
227 ;;; performed.
229 ;;; In the types are checkable it returns :SIMPLE and the second value
230 ;;; of the form:
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)
251 nil)
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
263 (values :simple
264 (maybe-negate-check value
265 (values-type-out ctype n-required)
266 (values-type-out atype n-required)
267 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)
273 (car it))
274 ((args-type-optional type)
275 (car it))
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)
281 n-required)))))
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)
287 n-consumed
288 *universal-type*)
289 (adjust-list (values-type-types atype)
290 n-consumed
291 *universal-type*)
292 n-required)))
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
314 ;; order.
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**
333 (let* ((entries
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.
337 #.(map 'vector
338 (lambda (entry)
339 (cons (type-specifier (specifier-type (car entry)))
340 (cdr entry)))
341 (remove-if #'stringp sb!c:+backend-internal-errors+
342 :key #'car)))
343 ;; This is effectively a compact read-only binned hashtable.
344 (hashtable (make-array (logior (length entries) 1)
345 :initial-element nil)))
346 (map nil
347 (lambda (entry)
348 (let* ((canon-type (car entry))
349 (bucket (mod (sxhash canon-type) (length hashtable))))
350 (push entry (svref hashtable bucket))))
351 entries)
352 hashtable))
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)))
356 :test #'equal))))
357 #+nil ; some meta-analysis to decide what types should be in "generic/interr"
358 (progn
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)
364 (if meta
365 (incf (car meta))
366 (setf (gethash spec *checkgen-used-types*) (cons 1 answer)))
367 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)
381 (let* ((spec
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)))
386 (interr-symbol
387 (%interr-symbol-for-type-spec external-spec)))
388 `(unless (typep ,temp ',test)
389 ,(if interr-symbol
390 `(%type-check-error/c ,temp ',interr-symbol)
391 `(%type-check-error ,temp ',external-spec)))))
392 temps
393 types)
394 (values ,@temps))))
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))
409 atype)
410 ((= length 1)
411 (single-value-type atype))
413 (make-values-type
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)))
421 (values))
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)
434 (not-ok-uses '()))
435 (do-uses (use value)
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 (what (when (and (combination-p dest)
444 (eq (combination-kind dest) :local))
445 (let ((lambda (combination-lambda dest))
446 (pos (position-or-lose
447 lvar (combination-args dest))))
448 (format nil "~:[A possible~;The~] binding of ~S"
449 (and (lvar-has-single-use-p lvar)
450 (eq (functional-kind lambda) :let))
451 (leaf-source-name (elt (lambda-vars lambda)
452 pos)))))))
453 (cond ((and (ref-p use) (constant-p (ref-leaf use)))
454 (warn condition
455 :format-control "~:[This~;~:*~A~] is not a ~
456 ~<~%~9T~:;~/sb!impl:print-type/:~>~% ~S"
457 :format-arguments
458 (list what atype (constant-value (ref-leaf use)))))
460 (warn condition
461 :format-control
462 "~:[Result~;~:*~A~] is a ~/sb!impl:print-type/, ~
463 ~<~%~9T~:;not a ~/sb!impl:print-type/.~>"
464 :format-arguments (list what dtype atype)))))))
465 (values))
467 ;;; Loop over all blocks in COMPONENT that have TYPE-CHECK set,
468 ;;; looking for CASTs with TYPE-CHECK T. We do two mostly unrelated
469 ;;; things: detect compile-time type errors and determine if and how
470 ;;; to do run-time type checks.
472 ;;; If there is a compile-time type error, then we mark the CAST and
473 ;;; emit a warning if appropriate. This part loops over all the uses
474 ;;; of the continuation, since after we convert the check, the
475 ;;; :DELETED kind will inhibit warnings about the types of other uses.
477 ;;; If the cast is too complex to be checked by the back end, or is
478 ;;; better checked with explicit code, then convert to an explicit
479 ;;; test. Assertions that can checked by the back end are passed
480 ;;; through. Assertions that can't be tested are flamed about and
481 ;;; marked as not needing to be checked.
483 ;;; If we determine that a type check won't be done, then we set
484 ;;; TYPE-CHECK to :NO-CHECK. In the non-hairy cases, this is just to
485 ;;; prevent us from wasting time coming to the same conclusion again
486 ;;; on a later iteration. In the hairy case, we must indicate to LTN
487 ;;; that it must choose a safe implementation, since IR2 conversion
488 ;;; will choke on the check.
490 ;;; The generation of the type checks is delayed until all the type
491 ;;; check decisions have been made because the generation of the type
492 ;;; checks creates new nodes whose derived types aren't always updated
493 ;;; which may lead to inappropriate template choices due to the
494 ;;; modification of argument types.
495 (defun generate-type-checks (component)
496 (collect ((casts))
497 (do-blocks (block component)
498 (when (and (block-type-check block)
499 (not (block-delete-p block)))
500 ;; CAST-EXTERNALLY-CHECKABLE-P wants the backward pass
501 (do-nodes-backwards (node nil block)
502 (when (and (cast-p node)
503 (cast-type-check node))
504 (cast-check-uses node)
505 (cond ((cast-externally-checkable-p node)
506 (setf (cast-%type-check node) :external))
508 ;; it is possible that NODE was marked :EXTERNAL by
509 ;; the previous pass
510 (setf (cast-%type-check node) t)
511 (casts node)))))
512 (setf (block-type-check block) nil)))
513 (dolist (cast (casts))
514 (unless (bound-cast-p cast)
515 (multiple-value-bind (check types) (cast-check-types cast)
516 (ecase check
517 (:simple
518 (convert-type-check cast types))
519 (:too-hairy
520 (let ((*compiler-error-context* cast))
521 (when (policy cast (>= safety inhibit-warnings))
522 (compiler-notify
523 "type assertion too complex to check:~%~
524 ~/sb!impl:print-type/."
525 (coerce-to-values (cast-asserted-type cast)))))
526 (setf (cast-type-to-check cast) *wild-type*)
527 (setf (cast-%type-check cast) nil)))))))
528 (values))