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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations)
Fri Dec 28 16:49:09 1990 UTC (23 years, 3 months ago) by ram
Branch: MAIN
Changes since 1.15: +18 -8 lines
Changed MAYBE-NEGATE-CHECK to allow weakened checks to be simple when
debug-info is not important, so that we can use CHECK-STRUCTURE, etc.
Changed TYPE-TEST-COST to penalize predicates as opposed to check templates
so that we will use a check template in favor of a "same cost" predicate.
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 *backend*))))
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 *backend*)))
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) (function-cost 'eq))
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. If true,
146 ;;; Force-Hairy forces a hairy type check.
147 ;;;
148 ;;; When doing a non-negated check, we call MAYBE-WEAKEN-CHECK to weaken the
149 ;;; test to a convenient supertype (conditional on policy.) If debug-info is
150 ;;; not particularly important (debug <= 1) or speed is 3, then we allow
151 ;;; weakened checks to be simple, resulting in less informative error messages,
152 ;;; but saving space and possibly time.
153 ;;;
154 (defun maybe-negate-check (cont types force-hairy)
155 (declare (type continuation cont) (list types))
156 (multiple-value-bind
157 (ptypes count)
158 (no-function-values-types (continuation-proven-type cont))
159 (if (eq count :unknown)
160 (if (and (every #'type-check-template types) (not force-hairy))
161 (values :simple types)
162 (values :hairy
163 (mapcar #'(lambda (x)
164 (list nil (maybe-weaken-check x cont) x))
165 types)))
166 (let ((res (mapcar #'(lambda (p c)
167 (let ((diff (type-difference p c))
168 (weak (maybe-weaken-check c cont)))
169 (if (and diff
170 (< (type-test-cost diff)
171 (type-test-cost weak)))
172 (list t diff c)
173 (list nil weak c))))
174 ptypes types)))
175 (cond ((or force-hairy (find-if #'first res))
176 (values :hairy res))
177 ((every #'type-check-template types)
178 (values :simple types))
179 ((policy (continuation-dest cont)
180 (or (<= debug 1) (and (= speed 3) (/= debug 3))))
181 (let ((weakened (mapcar #'second res)))
182 (if (every #'type-check-template weakened)
183 (values :simple weakened)
184 (values :hairy res))))
185 (t
186 (values :hairy res)))))))
187
188
189 ;;; CONTINUATION-CHECK-TYPES -- Interface
190 ;;;
191 ;;; Determines whether Cont's assertion is:
192 ;;; -- Checkable by the back end (:SIMPLE), or
193 ;;; -- Not checkable by the back end, but checkable via an explicit test in
194 ;;; type check conversion (:HAIRY), or
195 ;;; -- not reasonably checkable at all (:TOO-HAIRY).
196 ;;;
197 ;;; A type is checkable if it either represents a fixed number of values (as
198 ;;; determined by VALUES-TYPES), or it is the assertion for an MV-Bind. A type
199 ;;; is simply checkable if all the type assertions have a TYPE-CHECK-TEMPLATE.
200 ;;; In this :SIMPLE case, the second value is a list of the type restrictions
201 ;;; specified for the leading positional values.
202 ;;;
203 ;;; We force a check to be hairy even when there are fixed values if we are in
204 ;;; a context where we may be forced to use the unknown values convention
205 ;;; anyway. This is because IR2tran can't generate type checks for unknown
206 ;;; values continuations but people could still be depending on the check being
207 ;;; done. We only care about EXIT and RETURN (not MV-COMBINATION) since these
208 ;;; are the only contexts where the ultimate values receiver
209 ;;;
210 ;;; In the :HAIRY case, the second value is a list of triples of the form:
211 ;;; (Not-P Type Original-Type)
212 ;;;
213 ;;; If true, the Not-P flag indicates a test that the corresponding value is
214 ;;; *not* of the specified Type. Original-Type is the type asserted on this
215 ;;; value in the continuation, for use in error messages. When Not-P is true,
216 ;;; this will be different from Type.
217 ;;;
218 ;;; This allows us to take what has been proven about Cont's type into
219 ;;; consideration. If it is cheaper to test for the difference between the
220 ;;; derived type and the asserted type, then we check for the negation of this
221 ;;; type instead.
222 ;;;
223 (defun continuation-check-types (cont)
224 (declare (type continuation cont))
225 (let ((type (continuation-asserted-type cont))
226 (dest (continuation-dest cont)))
227 (assert (not (eq type *wild-type*)))
228 (multiple-value-bind (types count)
229 (no-function-values-types type)
230 (cond ((not (eq count :unknown))
231 (if (or (exit-p dest)
232 (and (return-p dest)
233 (multiple-value-bind
234 (ignore count)
235 (values-types (return-result-type dest))
236 (declare (ignore ignore))
237 (eq count :unknown))))
238 (maybe-negate-check cont types t)
239 (maybe-negate-check cont types nil)))
240 ((and (mv-combination-p dest)
241 (eq (basic-combination-kind dest) :local))
242 (assert (values-type-p type))
243 (maybe-negate-check cont (args-type-optional type) nil))
244 (t
245 (values :too-hairy nil))))))
246
247
248 ;;; Probable-Type-Check-P -- Internal
249 ;;;
250 ;;; Return true if Cont is a continuation whose type the back end is likely
251 ;;; to want to check. Since we don't know what template the back end is going
252 ;;; to choose to implement the continuation's DEST, we use a heuristic. We
253 ;;; always return T unless:
254 ;;; -- Nobody uses the value, or
255 ;;; -- Safety is totally unimportant, or
256 ;;; -- the continuation is an argument to an unknown function, or
257 ;;; -- the continuation is an argument to a known function that has no
258 ;;; IR2-Convert method or :fast-safe templates that are compatible with the
259 ;;; call's type.
260 ;;;
261 ;;; We must only return nil when it is *certain* that a check will not be done,
262 ;;; since if we pass up this chance to do the check, it will be too late. The
263 ;;; penalty for being too conservative is duplicated type checks.
264 ;;;
265 ;;; If there is a compile-time type error, then we always return true unless
266 ;;; the DEST is a full call. With a full call, the theory is that the type
267 ;;; error is probably from a declaration in (or on) the callee, so the callee
268 ;;; should be able to do the check. We want to let the callee do the check,
269 ;;; because it is possible that the error is really in the callee, not the
270 ;;; caller. We don't want to make people recompile all calls to a function
271 ;;; when they were originally compiled with a bad declaration (or an old type
272 ;;; assertion derived from a definition appearing after the call.)
273 ;;;
274 (defun probable-type-check-p (cont)
275 (declare (type continuation cont))
276 (let ((dest (continuation-dest cont)))
277 (cond ((eq (continuation-type-check cont) :error)
278 (if (and (combination-p dest) (eq (combination-kind dest) :full))
279 nil
280 t))
281 ((or (not dest)
282 (policy dest (zerop safety)))
283 nil)
284 ((basic-combination-p dest)
285 (let ((kind (basic-combination-kind dest)))
286 (cond ((eq cont (basic-combination-fun dest)) t)
287 ((eq kind :local) t)
288 ((eq kind :full) nil)
289 ((function-info-ir2-convert kind) t)
290 (t
291 (dolist (template (function-info-templates kind) nil)
292 (when (eq (template-policy template) :fast-safe)
293 (multiple-value-bind
294 (val win)
295 (valid-function-use dest (template-type template))
296 (when (or val (not win)) (return t)))))))))
297 (t t))))
298
299
300 ;;; Make-Type-Check-Form -- Internal
301 ;;;
302 ;;; Return a form that we can convert to do a hairy type check of the
303 ;;; specified Types. Types is a list of the format returned by
304 ;;; Continuation-Check-Types in the :HAIRY case. In place of the actual
305 ;;; value(s) we are to check, we use 'Dummy. This constant reference is later
306 ;;; replaced with the actual values continuation.
307 ;;;
308 ;;; Note that we don't attempt to check for required values being unsupplied.
309 ;;; Such checking is impossible to efficiently do at the source level because
310 ;;; our fixed-values conventions are optimized for the common MV-Bind case.
311 ;;;
312 ;;; We can always use Multiple-Value-Bind, since the macro is clever about
313 ;;; binding a single variable.
314 ;;;
315 (defun make-type-check-form (types)
316 (collect ((temps))
317 (dotimes (i (length types))
318 (temps (gensym)))
319
320 `(multiple-value-bind ,(temps)
321 'dummy
322 ,@(mapcar #'(lambda (temp type)
323 (let* ((spec
324 (let ((*unparse-function-type-simplify* t))
325 (type-specifier (second type))))
326 (test (if (first type) `(not ,spec) spec)))
327 `(unless (typep ,temp ',test)
328 (%type-check-error
329 ,temp
330 ',(type-specifier (third type))))))
331 (temps) types)
332 (values ,@(temps)))))
333
334
335 ;;; Convert-Type-Check -- Internal
336 ;;;
337 ;;; Splice in explicit type check code immediately before the node that its
338 ;;; Cont's Dest. This code receives the value(s) that were being passed to
339 ;;; Cont, checks the type(s) of the value(s), then passes them on to Cont.
340 ;;; We:
341 ;;; -- Ensure that Cont starts a block, so that we can freely manipulate its
342 ;;; uses.
343 ;;; -- Make a new continuation and move Cont's uses to it. Set type set
344 ;;; Type-Check in Cont to :DELETED to indicate that the check has been
345 ;;; done.
346 ;;; -- Make the Dest node start its block so that we can splice in the type
347 ;;; check code.
348 ;;; -- Splice in a new block before the Dest block, giving it all the Dest's
349 ;;; predecessors.
350 ;;; -- Convert the check form, using the new block start as Start and a dummy
351 ;;; continuation as Cont.
352 ;;; -- Set the new block's start and end cleanups to the *start* cleanup of
353 ;;; Prev's block. This overrides the incorrect default from
354 ;;; With-IR1-Environment.
355 ;;; -- Finish off the dummy continuation's block, and change the use to a use
356 ;;; of Cont. (we need to use the dummy continuation to get the control
357 ;;; transfer right, since we want to go to Prev's block, not Cont's.)
358 ;;; Link the new block to Prev's block.
359 ;;; -- Substitute the new continuation for the dummy placeholder argument.
360 ;;; Since no let conversion has been done yet, we can find the placeholder.
361 ;;; The [mv-]combination node from the mv-bind in the check form will be
362 ;;; the Use of the new check continuation. We substitute for the first
363 ;;; argument of this node.
364 ;;; -- Invoke local call analysis to convert the call to a let.
365 ;;;
366 (defun convert-type-check (cont types)
367 (declare (type continuation cont) (list types))
368 (with-ir1-environment (continuation-dest cont)
369 (ensure-block-start cont)
370 (let* ((new-start (make-continuation))
371 (dest (continuation-dest cont))
372 (prev (node-prev dest)))
373 (continuation-starts-block new-start)
374 (substitute-continuation-uses new-start cont)
375 (setf (continuation-%type-check cont) :deleted)
376
377 (when (continuation-use prev)
378 (node-ends-block (continuation-use prev)))
379
380 (let* ((prev-block (continuation-block prev))
381 (new-block (continuation-block new-start))
382 (dummy (make-continuation)))
383 (dolist (block (block-pred prev-block))
384 (change-block-successor block prev-block new-block))
385 (ir1-convert new-start dummy (make-type-check-form types))
386 (assert (eq (continuation-block dummy) new-block))
387
388 (let ((node (continuation-use dummy)))
389 (setf (block-last new-block) node)
390 (delete-continuation-use node)
391 (add-continuation-use node cont))
392 (link-blocks new-block prev-block))
393
394 (let* ((node (continuation-use cont))
395 (args (basic-combination-args node))
396 (victim (first args)))
397 (assert (and (= (length args) 1)
398 (eq (constant-value
399 (ref-leaf
400 (continuation-use victim)))
401 'dummy)))
402 (substitute-continuation new-start victim)))
403
404 (local-call-analyze *current-component*))
405
406 (undefined-value))
407
408
409 ;;; DO-TYPE-WARNING -- Internal
410 ;;;
411 ;;; Emit a type warning for Node. If the value of node is being used for a
412 ;;; variable binding, we figure out which one for source context. If the value
413 ;;; is a constant, we print it specially. We ignore nodes whose type is NIL,
414 ;;; since they are supposed to never return.
415 ;;;
416 (defun do-type-warning (node)
417 (declare (type node node))
418 (let* ((*compiler-error-context* node)
419 (cont (node-cont node))
420 (atype-spec (type-specifier (continuation-asserted-type cont)))
421 (dtype (node-derived-type node))
422 (dest (continuation-dest cont))
423 (what (when (and (combination-p dest)
424 (eq (combination-kind dest) :local))
425 (let ((lambda (combination-lambda dest))
426 (pos (position cont (combination-args dest))))
427 (format nil "~:[A possible~;The~] binding of ~S"
428 (and (continuation-use cont)
429 (eq (functional-kind lambda) :let))
430 (leaf-name (elt (lambda-vars lambda) pos)))))))
431 (cond ((eq dtype *empty-type*))
432 ((and (ref-p node) (constant-p (ref-leaf node)))
433 (compiler-warning "~:[This~;~:*~A~] is not a ~<~%~9T~:;~S:~>~% ~S"
434 what atype-spec (constant-value (ref-leaf node))))
435 (t
436 (compiler-warning
437 "~:[Result~;~:*~A~] is a ~S, ~<~%~9T~:;not a ~S.~>"
438 what (type-specifier dtype) atype-spec))))
439 (undefined-value))
440
441
442 ;;; MARK-ERROR-CONTINUATION -- Internal
443 ;;;
444 ;;; Mark Cont as being a continuation with a manifest type error. We set
445 ;;; the kind to :ERROR, and clear any FUNCTION-INFO if the continuation is an
446 ;;; argument to a known call. The last is done so that the back end doesn't
447 ;;; have to worry about type errors in arguments to known functions. This
448 ;;; clearing is inhibited for things with IR2-CONVERT methods, since we can't
449 ;;; do a full call to funny functions.
450 ;;;
451 (defun mark-error-continuation (cont)
452 (declare (type continuation cont))
453 (setf (continuation-%type-check cont) :error)
454 (let ((dest (continuation-dest cont)))
455 (when (and (combination-p dest)
456 (let ((info (basic-combination-kind dest)))
457 (and (function-info-p info)
458 (not (function-info-ir2-convert info)))))
459 (setf (basic-combination-kind dest) :full)))
460 (undefined-value))
461
462
463 ;;; Generate-Type-Checks -- Interface
464 ;;;
465 ;;; Loop over all blocks in Component that have TYPE-CHECK set, looking for
466 ;;; continuations with TYPE-CHECK T. We do two mostly unrelated things: detect
467 ;;; compile-time type errors and determine if and how to do run-time type
468 ;;; checks.
469 ;;;
470 ;;; If there is a compile-time type error, then we mark the continuation and
471 ;;; emit a warning if appropriate. This part loops over all the uses of the
472 ;;; continuation, since after we convert the check, the :DELETED kind will
473 ;;; inhibit warnings about the types of other uses.
474 ;;;
475 ;;; If a continuation is too complex to be checked by the back end, or is
476 ;;; better checked with explicit code, then convert to an explicit test.
477 ;;; Assertions that can checked by the back end are passed through. Assertions
478 ;;; that can't be tested are flamed about and marked as not needing to be
479 ;;; checked.
480 ;;;
481 ;;; If we determine that a type check won't be done, then we set TYPE-CHECK
482 ;;; to :NO-CHECK. In the non-hairy cases, this is just to prevent us from
483 ;;; wasting time coming to the same conclusion again on a later iteration. In
484 ;;; the hairy case, we must indicate to LTN that it must choose a safe
485 ;;; implementation, since IR2 conversion will choke on the check.
486 ;;;
487 (defun generate-type-checks (component)
488 (do-blocks (block component)
489 (when (block-type-check block)
490 (do-nodes (node cont block)
491 (let ((type-check (continuation-type-check cont)))
492 (unless (member type-check '(nil :error :deleted))
493 (let ((atype (continuation-asserted-type cont)))
494 (do-uses (use cont)
495 (unless (values-types-intersect (node-derived-type use)
496 atype)
497 (mark-error-continuation cont)
498 (unless (policy node (= brevity 3))
499 (do-type-warning use))))))
500
501 (when (eq type-check t)
502 (let ((check-p (probable-type-check-p cont)))
503 (multiple-value-bind (check types)
504 (continuation-check-types cont)
505 (ecase check
506 (:simple
507 (unless check-p
508 (setf (continuation-%type-check cont) :no-check)))
509 (:hairy
510 (if check-p
511 (convert-type-check cont types)
512 (setf (continuation-%type-check cont) :no-check)))
513 (:too-hairy
514 (let* ((context (continuation-dest cont))
515 (*compiler-error-context* context))
516 (when (policy context (>= safety brevity))
517 (compiler-note
518 "Type assertion too complex to check:~% ~S."
519 (type-specifier (continuation-asserted-type cont)))))
520 (setf (continuation-%type-check cont) :deleted))))))))
521
522 (setf (block-type-check block) nil)))
523
524 (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5