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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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