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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5