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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5