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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24.2.4 - (show annotations)
Sun Jul 9 14:00:18 2000 UTC (13 years, 9 months ago) by dtc
Branch: RELENG_18
Changes since 1.24.2.3: +8 -2 lines
Enhancement to continuation-check-types: even when the proven type
represents an unknown number of values, if Cont's destination receives
only a single value, generate a :hairy type check for the
single-values-type of the asserted type.
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/checkgen.lisp,v 1.24.2.4 2000/07/09 14:00:18 dtc Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; This file implements type check generation. This is a phase that runs
13 ;;; at the very end of IR1. If a type check is too complex for the back end to
14 ;;; directly emit in-line, then we transform the check into an explicit
15 ;;; conditional using TYPEP.
16 ;;;
17 ;;; Written by Rob MacLachlan
18 ;;;
19 (in-package "C")
20
21
22 ;;;; Cost estimation:
23
24
25 ;;; Function-Cost -- Internal
26 ;;;
27 ;;; Return some sort of guess about the cost of a call to a function. If
28 ;;; the function has some templates, we return the cost of the cheapest one,
29 ;;; otherwise we return the cost of CALL-NAMED. Calling this with functions
30 ;;; that have transforms can result in relatively meaningless results
31 ;;; (exaggerated costs.)
32 ;;;
33 ;;; We randomly special-case NULL, since it does have a source tranform and is
34 ;;; interesting to us.
35 ;;;
36 (defun function-cost (name)
37 (declare (symbol name))
38 (let ((info (info function info name))
39 (call-cost (template-cost (template-or-lose 'call-named *backend*))))
40 (if info
41 (let ((templates (function-info-templates info)))
42 (if templates
43 (template-cost (first templates))
44 (case name
45 (null (template-cost (template-or-lose 'if-eq *backend*)))
46 (t call-cost))))
47 call-cost)))
48
49
50 ;;; Type-Test-Cost -- Internal
51 ;;;
52 ;;; Return some sort of guess for the cost of doing a test against TYPE.
53 ;;; The result need not be precise as long as it isn't way out in space. The
54 ;;; units are based on the costs specified for various templates in the VM
55 ;;; definition.
56 ;;;
57 (defun type-test-cost (type)
58 (declare (type ctype type))
59 (or (let ((check (type-check-template type)))
60 (if check
61 (template-cost check)
62 (let ((found (cdr (assoc type (backend-type-predicates *backend*)
63 :test #'type=))))
64 (if found
65 (+ (function-cost found) (function-cost 'eq))
66 nil))))
67 (typecase type
68 (union-type
69 (collect ((res 0 +))
70 (dolist (mem (union-type-types type))
71 (res (type-test-cost mem)))
72 (res)))
73 (member-type
74 (* (length (member-type-members type))
75 (function-cost 'eq)))
76 (numeric-type
77 (* (if (numeric-type-complexp type) 2 1)
78 (function-cost
79 (if (csubtypep type (specifier-type 'fixnum)) 'fixnump 'numberp))
80 (+ 1
81 (if (numeric-type-low type) 1 0)
82 (if (numeric-type-high type) 1 0))))
83 (cons-type
84 (+ (type-test-cost (specifier-type 'cons))
85 (function-cost 'car)
86 (type-test-cost (cons-type-car-type type))
87 (function-cost 'cdr)
88 (type-test-cost (cons-type-cdr-type type))))
89 (t
90 (function-cost 'typep)))))
91
92
93 ;;;; Checking strategy determination:
94
95
96 ;;; MAYBE-WEAKEN-CHECK -- Internal
97 ;;;
98 ;;; Return the type we should test for when we really want to check for
99 ;;; Type. If speed, space or compilation speed is more important than safety,
100 ;;; then we return a weaker type if it is easier to check. First we try the
101 ;;; defined type weakenings, then look for any predicate that is cheaper.
102 ;;;
103 ;;; If the supertype is equal in cost to the type, we prefer the supertype.
104 ;;; This produces a closer approximation of the right thing in the presence of
105 ;;; poor cost info.
106 ;;;
107 (defun maybe-weaken-check (type cont)
108 (declare (type ctype type) (type continuation cont))
109 (cond ((policy (continuation-dest cont)
110 (<= speed safety) (<= space safety) (<= cspeed safety))
111 type)
112 (t
113 (let ((min-cost (type-test-cost type))
114 (min-type type)
115 (found-super nil))
116 (dolist (x (backend-type-predicates *backend*))
117 (let ((stype (car x)))
118 (when (and (csubtypep type stype)
119 (not (union-type-p stype))) ;Not #!% COMMON type.
120 (let ((stype-cost (type-test-cost stype)))
121 (when (or (< stype-cost min-cost)
122 (type= stype type))
123 (setq found-super t)
124 (setq min-type stype min-cost stype-cost))))))
125 (if found-super
126 min-type
127 *universal-type*)))))
128
129
130 ;;; NO-FUNCTION-TYPES -- Internal
131 ;;;
132 ;;; Mash any complex function types to FUNCTION.
133 ;;;
134 (defun no-function-types (types)
135 (declare (type list types))
136 (mapcar #'(lambda (type)
137 (if (function-type-p type)
138 (specifier-type 'function)
139 type))
140 types))
141
142
143 ;;; Switch to disable check complementing, for evaluation.
144 ;;;
145 (defvar *complement-type-checks* t)
146
147 ;;; MAYBE-NEGATE-CHECK -- Internal
148 ;;;
149 ;;; Cont is a continuation we are doing a type check on and Types is a list
150 ;;; of types that we are checking its values against. If we have proven
151 ;;; that Cont generates a fixed number of values, then for each value, we check
152 ;;; whether it is cheaper to then difference between the proven type and
153 ;;; the corresponding type in Types. If so, we opt for a :HAIRY check with
154 ;;; that test negated. Otherwise, we try to do a simple test, and if that is
155 ;;; impossible, we do a hairy test with non-negated types. If true,
156 ;;; Force-Hairy forces a hairy type check.
157 ;;;
158 ;;; When doing a non-negated check, we call MAYBE-WEAKEN-CHECK to weaken the
159 ;;; test to a convenient supertype (conditional on policy.) If debug-info is
160 ;;; not particularly important (debug <= 1) or speed is 3, then we allow
161 ;;; weakened checks to be simple, resulting in less informative error messages,
162 ;;; but saving space and possibly time.
163 ;;;
164 (defun maybe-negate-check (cont types force-hairy)
165 (declare (type continuation cont) (list types))
166 (multiple-value-bind
167 (ptypes count)
168 (values-types (continuation-proven-type cont))
169 (if (eq count :unknown)
170 (if (and (every #'type-check-template types) (not force-hairy))
171 (values :simple types)
172 (values :hairy
173 (mapcar #'(lambda (x)
174 (list nil (maybe-weaken-check x cont) x))
175 types)))
176 (let ((res (mapcar #'(lambda (p c)
177 (if (csubtypep p c)
178 (list nil *universal-type* c)
179 (let ((diff (type-difference p c))
180 (weak (maybe-weaken-check c cont)))
181 (if (and diff
182 (< (type-test-cost diff)
183 (type-test-cost weak))
184 *complement-type-checks*)
185 (list t diff c)
186 (list nil weak c)))))
187 (no-function-types ptypes) types)))
188 (cond ((or force-hairy (find-if #'first res))
189 (values :hairy res))
190 ((every #'type-check-template types)
191 (values :simple types))
192 ((policy (continuation-dest cont)
193 (or (<= debug 1) (and (= speed 3) (/= debug 3))))
194 (let ((weakened (mapcar #'second res)))
195 (if (every #'type-check-template weakened)
196 (values :simple weakened)
197 (values :hairy res))))
198 (t
199 (values :hairy res)))))))
200
201
202 ;;; CONTINUATION-CHECK-TYPES -- Interface
203 ;;;
204 ;;; Determines whether Cont's assertion is:
205 ;;; -- Checkable by the back end (:SIMPLE), or
206 ;;; -- Not checkable by the back end, but checkable via an explicit test in
207 ;;; type check conversion (:HAIRY), or
208 ;;; -- not reasonably checkable at all (:TOO-HAIRY).
209 ;;;
210 ;;; A type is checkable if it either represents a fixed number of values (as
211 ;;; determined by VALUES-TYPES), or it is the assertion for an MV-Bind. A type
212 ;;; is simply checkable if all the type assertions have a TYPE-CHECK-TEMPLATE.
213 ;;; In this :SIMPLE case, the second value is a list of the type restrictions
214 ;;; specified for the leading positional values.
215 ;;;
216 ;;; We force a check to be hairy even when there are fixed values if we are in
217 ;;; a context where we may be forced to use the unknown values convention
218 ;;; anyway. This is because IR2tran can't generate type checks for unknown
219 ;;; values continuations but people could still be depending on the check being
220 ;;; done. We only care about EXIT and RETURN (not MV-COMBINATION) since these
221 ;;; are the only contexts where the ultimate values receiver
222 ;;;
223 ;;; In the :HAIRY case, the second value is a list of triples of the form:
224 ;;; (Not-P Type Original-Type)
225 ;;;
226 ;;; If true, the Not-P flag indicates a test that the corresponding value is
227 ;;; *not* of the specified Type. Original-Type is the type asserted on this
228 ;;; value in the continuation, for use in error messages. When Not-P is true,
229 ;;; this will be different from Type.
230 ;;;
231 ;;; This allows us to take what has been proven about Cont's type into
232 ;;; consideration. If it is cheaper to test for the difference between the
233 ;;; derived type and the asserted type, then we check for the negation of this
234 ;;; type instead.
235 ;;;
236 ;;; When the proven type represents an unknown number of values, but Cont's
237 ;;; destination receives only a single value, a :hairy type check is
238 ;;; generated for the single-values-type of the asserted type.
239 ;;;
240 (defun continuation-check-types (cont)
241 (declare (type continuation cont))
242 (let ((atype (continuation-asserted-type cont))
243 (dest (continuation-dest cont)))
244 (assert (not (eq atype *wild-type*)))
245 (multiple-value-bind (types count)
246 (values-types atype)
247 (cond ((not (eq count :unknown))
248 (let ((types (no-function-types types)))
249 (if (or (exit-p dest)
250 (and (return-p dest)
251 (multiple-value-bind
252 (ignore count)
253 (values-types (return-result-type dest))
254 (declare (ignore ignore))
255 (eq count :unknown))))
256 (maybe-negate-check cont types t)
257 (maybe-negate-check cont types nil))))
258 ((and (mv-combination-p dest)
259 (eq (basic-combination-kind dest) :local))
260 (assert (values-type-p atype))
261 (assert (null (args-type-required atype)))
262 (maybe-negate-check cont (args-type-optional atype) nil))
263 ((or (exit-p dest) (return-p dest) (mv-combination-p dest))
264 (values :too-hairy nil))
265 (t
266 (maybe-negate-check cont (list (single-value-type atype)) t))))))
267
268
269 ;;; Probable-Type-Check-P -- Internal
270 ;;;
271 ;;; Return true if Cont is a continuation whose type the back end is likely
272 ;;; to want to check. Since we don't know what template the back end is going
273 ;;; to choose to implement the continuation's DEST, we use a heuristic. We
274 ;;; always return T unless:
275 ;;; -- Nobody uses the value, or
276 ;;; -- Safety is totally unimportant, or
277 ;;; -- the continuation is an argument to an unknown function, or
278 ;;; -- the continuation is an argument to a known function that has no
279 ;;; IR2-Convert method or :fast-safe templates that are compatible with the
280 ;;; call's type.
281 ;;;
282 ;;; We must only return nil when it is *certain* that a check will not be done,
283 ;;; since if we pass up this chance to do the check, it will be too late. The
284 ;;; penalty for being too conservative is duplicated type checks.
285 ;;;
286 ;;; If there is a compile-time type error, then we always return true unless
287 ;;; the DEST is a full call. With a full call, the theory is that the type
288 ;;; error is probably from a declaration in (or on) the callee, so the callee
289 ;;; should be able to do the check. We want to let the callee do the check,
290 ;;; because it is possible that the error is really in the callee, not the
291 ;;; caller. We don't want to make people recompile all calls to a function
292 ;;; when they were originally compiled with a bad declaration (or an old type
293 ;;; assertion derived from a definition appearing after the call.)
294 ;;;
295 (defun probable-type-check-p (cont)
296 (declare (type continuation cont))
297 (let ((dest (continuation-dest cont)))
298 (cond ((eq (continuation-type-check cont) :error)
299 (if (and (combination-p dest) (eq (combination-kind dest) :error))
300 nil
301 t))
302 ((or (not dest)
303 (policy dest (zerop safety)))
304 nil)
305 ((basic-combination-p dest)
306 (let ((kind (basic-combination-kind dest)))
307 (cond ((eq cont (basic-combination-fun dest)) t)
308 ((eq kind :local) t)
309 ((member kind '(:full :error)) nil)
310 ((function-info-ir2-convert kind) t)
311 (t
312 (dolist (template (function-info-templates kind) nil)
313 (when (eq (template-policy template) :fast-safe)
314 (multiple-value-bind
315 (val win)
316 (valid-function-use dest (template-type template))
317 (when (or val (not win)) (return t)))))))))
318 (t t))))
319
320
321 ;;; Make-Type-Check-Form -- Internal
322 ;;;
323 ;;; Return a form that we can convert to do a hairy type check of the
324 ;;; specified Types. Types is a list of the format returned by
325 ;;; Continuation-Check-Types in the :HAIRY case. In place of the actual
326 ;;; value(s) we are to check, we use 'Dummy. This constant reference is later
327 ;;; replaced with the actual values continuation.
328 ;;;
329 ;;; Note that we don't attempt to check for required values being unsupplied.
330 ;;; Such checking is impossible to efficiently do at the source level because
331 ;;; our fixed-values conventions are optimized for the common MV-Bind case.
332 ;;;
333 ;;; We can always use Multiple-Value-Bind, since the macro is clever about
334 ;;; binding a single variable.
335 ;;;
336 (defun make-type-check-form (types)
337 (collect ((temps))
338 (dotimes (i (length types))
339 (temps (gensym)))
340
341 `(multiple-value-bind ,(temps)
342 'dummy
343 ,@(mapcar #'(lambda (temp type)
344 (let* ((spec
345 (let ((*unparse-function-type-simplify* t))
346 (type-specifier (second type))))
347 (test (if (first type) `(not ,spec) spec)))
348 `(unless (typep ,temp ',test)
349 (%type-check-error
350 ,temp
351 ',(type-specifier (third type))))))
352 (temps) types)
353 (values ,@(temps)))))
354
355
356 ;;; Convert-Type-Check -- Internal
357 ;;;
358 ;;; Splice in explicit type check code immediately before the node that its
359 ;;; Cont's Dest. This code receives the value(s) that were being passed to
360 ;;; Cont, checks the type(s) of the value(s), then passes them on to Cont.
361 ;;; We:
362 ;;; -- Ensure that Cont starts a block, so that we can freely manipulate its
363 ;;; uses.
364 ;;; -- Make a new continuation and move Cont's uses to it. Set type set
365 ;;; Type-Check in Cont to :DELETED to indicate that the check has been
366 ;;; done.
367 ;;; -- Make the Dest node start its block so that we can splice in the type
368 ;;; check code.
369 ;;; -- Splice in a new block before the Dest block, giving it all the Dest's
370 ;;; predecessors.
371 ;;; -- Convert the check form, using the new block start as Start and a dummy
372 ;;; continuation as Cont.
373 ;;; -- Set the new block's start and end cleanups to the *start* cleanup of
374 ;;; Prev's block. This overrides the incorrect default from
375 ;;; With-IR1-Environment.
376 ;;; -- Finish off the dummy continuation's block, and change the use to a use
377 ;;; of Cont. (we need to use the dummy continuation to get the control
378 ;;; transfer right, since we want to go to Prev's block, not Cont's.)
379 ;;; Link the new block to Prev's block.
380 ;;; -- Substitute the new continuation for the dummy placeholder argument.
381 ;;; Since no let conversion has been done yet, we can find the placeholder.
382 ;;; The [mv-]combination node from the mv-bind in the check form will be
383 ;;; the Use of the new check continuation. We substitute for the first
384 ;;; argument of this node.
385 ;;; -- Invoke local call analysis to convert the call to a let.
386 ;;;
387 (defun convert-type-check (cont types)
388 (declare (type continuation cont) (list types))
389 (with-ir1-environment (continuation-dest cont)
390 (ensure-block-start cont)
391 (let* ((new-start (make-continuation))
392 (dest (continuation-dest cont))
393 (prev (node-prev dest)))
394 (continuation-starts-block new-start)
395 (substitute-continuation-uses new-start cont)
396 (setf (continuation-%type-check cont) :deleted)
397
398 (when (continuation-use prev)
399 (node-ends-block (continuation-use prev)))
400
401 (let* ((prev-block (continuation-block prev))
402 (new-block (continuation-block new-start))
403 (dummy (make-continuation)))
404 (dolist (block (block-pred prev-block))
405 (change-block-successor block prev-block new-block))
406 (ir1-convert new-start dummy (make-type-check-form types))
407 (assert (eq (continuation-block dummy) new-block))
408
409 (let ((node (continuation-use dummy)))
410 (setf (block-last new-block) node)
411 (delete-continuation-use node)
412 (add-continuation-use node cont))
413 (link-blocks new-block prev-block))
414
415 (let* ((node (continuation-use cont))
416 (args (basic-combination-args node))
417 (victim (first args)))
418 (assert (and (= (length args) 1)
419 (eq (constant-value
420 (ref-leaf
421 (continuation-use victim)))
422 'dummy)))
423 (substitute-continuation new-start victim)))
424
425 (local-call-analyze *current-component*))
426
427 (undefined-value))
428
429
430 ;;; DO-TYPE-WARNING -- Internal
431 ;;;
432 ;;; Emit a type warning for Node. If the value of node is being used for a
433 ;;; variable binding, we figure out which one for source context. If the value
434 ;;; is a constant, we print it specially. We ignore nodes whose type is NIL,
435 ;;; since they are supposed to never return.
436 ;;;
437 (defun do-type-warning (node)
438 (declare (type node node))
439 (let* ((*compiler-error-context* node)
440 (cont (node-cont node))
441 (atype-spec (type-specifier (continuation-asserted-type cont)))
442 (dtype (node-derived-type node))
443 (dest (continuation-dest cont))
444 (what (when (and (combination-p dest)
445 (eq (combination-kind dest) :local))
446 (let ((lambda (combination-lambda dest))
447 (pos (eposition cont (combination-args dest))))
448 (format nil "~:[A possible~;The~] binding of ~S"
449 (and (continuation-use cont)
450 (eq (functional-kind lambda) :let))
451 (leaf-name (elt (lambda-vars lambda) pos)))))))
452 (cond ((eq dtype *empty-type*))
453 ((and (ref-p node) (constant-p (ref-leaf node)))
454 (compiler-warning "~:[This~;~:*~A~] is not a ~<~%~9T~:;~S:~>~% ~S"
455 what atype-spec (constant-value (ref-leaf node))))
456 (t
457 (compiler-warning
458 "~:[Result~;~:*~A~] is a ~S, ~<~%~9T~:;not a ~S.~>"
459 what (type-specifier dtype) atype-spec))))
460 (undefined-value))
461
462
463 ;;; MARK-ERROR-CONTINUATION -- Internal
464 ;;;
465 ;;; Mark Cont as being a continuation with a manifest type error. We set
466 ;;; the kind to :ERROR, and clear any FUNCTION-INFO if the continuation is an
467 ;;; argument to a known call. The last is done so that the back end doesn't
468 ;;; have to worry about type errors in arguments to known functions. This
469 ;;; clearing is inhibited for things with IR2-CONVERT methods, since we can't
470 ;;; do a full call to funny functions.
471 ;;;
472 (defun mark-error-continuation (cont)
473 (declare (type continuation cont))
474 (setf (continuation-%type-check cont) :error)
475 (let ((dest (continuation-dest cont)))
476 (when (and (combination-p dest)
477 (let ((kind (basic-combination-kind dest)))
478 (or (eq kind :full)
479 (and (function-info-p kind)
480 (not (function-info-ir2-convert kind))))))
481 (setf (basic-combination-kind dest) :error)))
482 (undefined-value))
483
484
485 ;;; Generate-Type-Checks -- Interface
486 ;;;
487 ;;; Loop over all blocks in Component that have TYPE-CHECK set, looking for
488 ;;; continuations with TYPE-CHECK T. We do two mostly unrelated things: detect
489 ;;; compile-time type errors and determine if and how to do run-time type
490 ;;; checks.
491 ;;;
492 ;;; If there is a compile-time type error, then we mark the continuation and
493 ;;; emit a warning if appropriate. This part loops over all the uses of the
494 ;;; continuation, since after we convert the check, the :DELETED kind will
495 ;;; inhibit warnings about the types of other uses.
496 ;;;
497 ;;; If a continuation is too complex to be checked by the back end, or is
498 ;;; better checked with explicit code, then convert to an explicit test.
499 ;;; Assertions that can checked by the back end are passed through. Assertions
500 ;;; that can't be tested are flamed about and marked as not needing to be
501 ;;; checked.
502 ;;;
503 ;;; If we determine that a type check won't be done, then we set TYPE-CHECK
504 ;;; to :NO-CHECK. In the non-hairy cases, this is just to prevent us from
505 ;;; wasting time coming to the same conclusion again on a later iteration. In
506 ;;; the hairy case, we must indicate to LTN that it must choose a safe
507 ;;; implementation, since IR2 conversion will choke on the check.
508 ;;;
509 ;;; The generation of the type checks is delayed until all the type
510 ;;; check decisions have been made because the generation of the type
511 ;;; checks creates new nodes who's derived types aren't always updated
512 ;;; which may lead to inappropriate template choices due to the
513 ;;; modification of argument types.
514 ;;;
515 (defun generate-type-checks (component)
516 (collect ((conts))
517 (do-blocks (block component)
518 (when (block-type-check block)
519 (do-nodes (node cont block)
520 (let ((type-check (continuation-type-check cont)))
521 (unless (member type-check '(nil :error :deleted))
522 (let ((atype (continuation-asserted-type cont)))
523 (do-uses (use cont)
524 (unless (values-types-intersect (node-derived-type use)
525 atype)
526 (mark-error-continuation cont)
527 (unless (policy node (= brevity 3))
528 (do-type-warning use))))))
529 (when (and (eq type-check t)
530 (not *byte-compiling*))
531 (if (probable-type-check-p cont)
532 (conts cont)
533 (setf (continuation-%type-check cont) :no-check)))))
534
535 (setf (block-type-check block) nil)))
536
537 (dolist (cont (conts))
538 (multiple-value-bind (check types)
539 (continuation-check-types cont)
540 (ecase check
541 (:simple)
542 (:hairy
543 (convert-type-check cont types))
544 (:too-hairy
545 (let* ((context (continuation-dest cont))
546 (*compiler-error-context* context))
547 (when (policy context (>= safety brevity))
548 (compiler-note
549 "Type assertion too complex to check:~% ~S."
550 (type-specifier (continuation-asserted-type cont)))))
551 (setf (continuation-%type-check cont) :deleted))))))
552
553 (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5