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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Wed May 16 10:56:25 1990 UTC (23 years, 11 months ago) by ram
Branch: MAIN
Changes since 1.3: +1 -1 lines
Type warnings only inhibited when brevity = 3.
1 wlott 1.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))))
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)))
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)
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 ram 1.3 ;;; 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 (csubtypep type stype)
110     (setq found-super t)
111     (let ((stype-cost (type-test-cost stype)))
112     (when (< stype-cost min-cost)
113     (setq min-type stype min-cost stype-cost))))))
114     (if found-super
115     min-type
116     *universal-type*)))))
117    
118    
119 wlott 1.1 ;;; MAYBE-NEGATE-CHECK -- Internal
120     ;;;
121     ;;; Cont is a continuation we are doing a type check on and Types is a list
122     ;;; of types that we are checking its values against. If we have proven
123     ;;; that Cont generates a fixed number of values, then for each value, we check
124     ;;; whether it is cheaper to then difference between the the proven type and
125     ;;; the corresponding type in Types. If so, we opt for a :HAIRY check with
126     ;;; that test negated. Otherwise, we try to do a simple test, and if that is
127     ;;; impossible, we do a hairy test with non-negated types.
128     ;;;
129     (defun maybe-negate-check (cont types)
130     (declare (type continuation cont) (list types))
131     (multiple-value-bind (ptypes count)
132     (values-types (continuation-proven-type cont))
133     (if (eq count :unknown)
134     (if (every #'type-check-template types)
135     (values :simple types)
136 ram 1.3 (values :hairy
137     (mapcar #'(lambda (x)
138     (list nil (maybe-weaken-check x cont) x))
139     types)))
140 wlott 1.1 (let ((res (mapcar #'(lambda (p c)
141     (let ((diff (type-difference p c)))
142     (if (and diff
143     (< (type-test-cost diff)
144     (type-test-cost c)))
145 ram 1.3 (list t (maybe-weaken-check diff cont) c)
146     (list nil (maybe-weaken-check c cont) c))))
147 wlott 1.1 ptypes types)))
148     (if (and (not (find-if #'first res))
149     (every #'type-check-template types))
150     (values :simple types)
151     (values :hairy res))))))
152    
153    
154     ;;; CONTINUATION-CHECK-TYPES -- Interface
155     ;;;
156     ;;; Determines whether Cont's assertion is:
157     ;;; -- Checkable by the back end (:SIMPLE), or
158     ;;; -- Not checkable by the back end, but checkable via an explicit test in
159     ;;; type check conversion (:HAIRY), or
160     ;;; -- not reasonably checkable at all (:TOO-HAIRY).
161     ;;;
162     ;;; A type is checkable if it either represents a fixed number of values (as
163     ;;; determined by VALUES-TYPES), or it is the assertion for an MV-Bind. A type
164     ;;; is simply checkable if all the type assertions have a TYPE-CHECK-TEMPLATE.
165     ;;; In this :SIMPLE case, the second value is a list of the type restrictions
166     ;;; specified for the leading positional values.
167     ;;;
168     ;;; In the :HAIRY case, the second value is a list of triples of the form:
169     ;;; (Not-P Type Original-Type)
170     ;;;
171     ;;; If true, the Not-P flag indicates a test that the corresponding value is
172     ;;; *not* of the specified Type. Original-Type is the type asserted on this
173     ;;; value in the continuation, for use in error messages. When Not-P is true,
174     ;;; this will be different from Type.
175     ;;;
176     ;;; This allows us to take what has been proven about Cont's type into
177     ;;; consideration. If it is cheaper to test for the difference between the
178     ;;; derived type and the asserted type, then we check for the negation of this
179     ;;; type instead.
180     ;;;
181     (defun continuation-check-types (cont)
182     (declare (type continuation cont))
183     (let ((type (continuation-asserted-type cont))
184     (dest (continuation-dest cont)))
185     (assert (not (eq type *wild-type*)))
186     (multiple-value-bind (types count)
187     (values-types type)
188     (cond ((not (eq count :unknown))
189     (maybe-negate-check cont types))
190     ((and (mv-combination-p dest)
191     (eq (basic-combination-kind dest) :local))
192     (assert (values-type-p type))
193     (maybe-negate-check cont (args-type-optional type)))
194     (t
195     (values :too-hairy nil))))))
196    
197    
198     ;;; Probable-Type-Check-P -- Internal
199     ;;;
200     ;;; Return true if Cont is a continuation whose type the back end is likely
201     ;;; to want to check. Since we don't know what template the back end is going
202     ;;; to choose to implement the continuation's DEST, we use a heuristic. We
203     ;;; always return T unless:
204     ;;; -- Nobody uses the value, or
205 ram 1.3 ;;; -- Safety is totally unimportant, or
206 wlott 1.1 ;;; -- the continuation is an argument to an unknown function, or
207     ;;; -- the continuation is an argument to a known function that has no
208     ;;; IR2-Convert method or :fast-safe templates that are compatible with the
209     ;;; call's type.
210     ;;;
211     ;;; We must only return nil when it is *certain* that a check will not be done,
212     ;;; since if we pass up this chance to do the check, it will be too late. The
213     ;;; penalty for being too conservative is duplicated type checks.
214     ;;;
215     ;;; We always return true if there is a compile-time type error on the
216     ;;; continuation, so that this error will be signalled at runtime as well.
217     ;;;
218     (defun probable-type-check-p (cont)
219     (declare (type continuation cont))
220     (let ((dest (continuation-dest cont)))
221     (cond ((eq (continuation-type-check cont) :error))
222     ((or (not dest)
223 ram 1.3 (policy dest (zerop safety)))
224 wlott 1.1 nil)
225     ((basic-combination-p dest)
226     (let ((kind (basic-combination-kind dest)))
227     (cond ((eq cont (basic-combination-fun dest)) t)
228     ((eq kind :local) t)
229     ((eq kind :full) nil)
230     ((function-info-ir2-convert kind) t)
231     (t
232     (dolist (template (function-info-templates kind) nil)
233     (when (and (eq (template-policy template) :fast-safe)
234     (valid-function-use dest
235     (template-type template)))
236     (return t)))))))
237     (t t))))
238    
239    
240     ;;; Make-Type-Check-Form -- Internal
241     ;;;
242     ;;; Return a form that we can convert to do a hairy type check of the
243     ;;; specified Types. Types is a list of the format returned by
244     ;;; Continuation-Check-Types in the :HAIRY case. In place of the actual
245     ;;; value(s) we are to check, we use 'Dummy. This constant reference is later
246     ;;; replaced with the actual values continuation.
247     ;;;
248     ;;; Note that we don't attempt to check for required values being unsupplied.
249     ;;; Such checking is impossible to efficiently do at the source level because
250     ;;; our fixed-values conventions are optimized for the common MV-Bind case.
251     ;;;
252     ;;; We can always use Multiple-Value-Bind, since the macro is clever about
253     ;;; binding a single variable.
254     ;;;
255     (defun make-type-check-form (types)
256     (collect ((temps))
257     (dotimes (i (length types))
258     (declare (ignore i))
259     (temps (gensym)))
260    
261     `(multiple-value-bind ,(temps)
262     'dummy
263     ,@(mapcar #'(lambda (temp type)
264     (let* ((spec (type-specifier (second type)))
265     (test (if (first type) `(not ,spec) spec)))
266     `(unless (typep ,temp ',test)
267     (%type-check-error
268     ,temp
269     ',(type-specifier (third type))))))
270     (temps) types)
271     (values ,@(temps)))))
272    
273    
274     ;;; Convert-Type-Check -- Internal
275     ;;;
276     ;;; Splice in explicit type check code immediately before the node that its
277     ;;; Cont's Dest. This code receives the value(s) that were being passed to
278     ;;; Cont, checks the type(s) of the value(s), then passes them on to Cont.
279     ;;; We:
280     ;;; -- Ensure that Cont starts a block, so that we can freely manipulate its
281     ;;; uses.
282     ;;; -- Make a new continuation and move Cont's uses to it. Set type set
283     ;;; Type-Check in Cont to :DELETED to indicate that the check has been
284     ;;; done.
285     ;;; -- Make the Dest node start its block so that we can splice in the type
286     ;;; check code.
287     ;;; -- Splice in a new block before the Dest block, giving it all the Dest's
288     ;;; predecessors.
289     ;;; -- Convert the check form, using the new block start as Start and a dummy
290     ;;; continuation as Cont.
291     ;;; -- Set the new block's start and end cleanups to the *start* cleanup of
292     ;;; Prev's block. This overrides the incorrect default from
293     ;;; With-IR1-Environment.
294     ;;; -- Finish off the dummy continuation's block, and change the use to a use
295     ;;; of Cont. (we need to use the dummy continuation to get the control
296     ;;; transfer right, since we want to go to Prev's block, not Cont's.)
297     ;;; Link the new block to Prev's block.
298     ;;; -- Substitute the new continuation for the dummy placeholder argument.
299     ;;; Since no let conversion has been done yet, we can find the placeholder.
300     ;;; The [mv-]combination node from the mv-bind in the check form will be
301     ;;; the Use of the new check continuation. We substitute for the first
302     ;;; argument of this node.
303     ;;; -- Invoke local call analysis to convert the call to a let.
304     ;;;
305     (defun convert-type-check (cont types)
306     (declare (type continuation cont) (list types))
307     (with-ir1-environment (continuation-dest cont)
308     (ensure-block-start cont)
309     (let* ((new-start (make-continuation))
310     (dest (continuation-dest cont))
311     (prev (node-prev dest)))
312     (continuation-starts-block new-start)
313     (substitute-continuation-uses new-start cont)
314     (setf (continuation-%type-check cont) :deleted)
315    
316     (when (continuation-use prev)
317     (node-ends-block (continuation-use prev)))
318    
319     (let* ((prev-block (continuation-block prev))
320     (prev-cleanup (block-start-cleanup prev-block))
321     (new-block (continuation-block new-start))
322     (dummy (make-continuation)))
323     (dolist (block (block-pred prev-block))
324     (change-block-successor block prev-block new-block))
325     (ir1-convert new-start dummy (make-type-check-form types))
326     (assert (eq (continuation-block dummy) new-block))
327    
328     (setf (block-start-cleanup new-block) prev-cleanup)
329     (setf (block-end-cleanup new-block) prev-cleanup)
330    
331     (let ((node (continuation-use dummy)))
332     (setf (block-last new-block) node)
333     (delete-continuation-use node)
334     (add-continuation-use node cont))
335     (link-blocks new-block prev-block))
336    
337     (let* ((node (continuation-use cont))
338     (args (basic-combination-args node))
339     (victim (first args)))
340     (assert (and (= (length args) 1)
341     (eq (constant-value
342     (ref-leaf
343     (continuation-use victim)))
344     'dummy)))
345     (substitute-continuation new-start victim)))
346    
347     (local-call-analyze *current-component*))
348    
349     (undefined-value))
350    
351    
352     ;;; Generate-Type-Checks -- Interface
353     ;;;
354     ;;; Loop over all blocks in Component that have TYPE-CHECK set, looking for
355     ;;; continuations with TYPE-CHECK T. We do two mostly unrelated things: detect
356     ;;; compile-time type errors and determine if and how to do run-time type
357     ;;; checks.
358     ;;;
359 ram 1.2 ;;; If there is a compile-time type error, then we mark the continuation
360     ;;; with a :ERROR kind, emit a warning if appropriate, and clear any
361     ;;; FUNCTION-INFO if the continuation is an argument to a known call. The last
362     ;;; is done so that the back end doesn't have to worry about type errors in
363     ;;; arguments to known functions.
364     ;;;
365 wlott 1.1 ;;; If a continuation is too complex to be checked by the back end, or is
366     ;;; better checked with explicit code, then convert to an explicit test.
367     ;;; Assertions that can checked by the back end are passed through. Assertions
368     ;;; that can't be tested are flamed about and marked as not needing to be
369     ;;; checked.
370     ;;;
371     ;;; If we determine that a type check won't be done, then we set TYPE-CHECK
372     ;;; to :NO-CHECK. In the non-hairy cases, this is just to prevent us from
373     ;;; wasting time coming to the same conclusion again on a later iteration. In
374     ;;; the hairy case, we must indicate to LTN that it must choose a safe
375     ;;; implementation, since IR2 conversion will choke on the check.
376     ;;;
377     (defun generate-type-checks (component)
378     (do-blocks (block component)
379     (when (block-type-check block)
380     (do-nodes (node cont block)
381     (when (eq (continuation-type-check cont) t)
382    
383     (let ((dtype (node-derived-type node))
384     (atype (continuation-asserted-type cont)))
385     (unless (values-types-intersect dtype atype)
386     (setf (continuation-%type-check cont) :error)
387 ram 1.2 (let ((dest (continuation-dest cont)))
388     (when (and (combination-p dest)
389     (function-info-p (basic-combination-kind dest)))
390     (setf (basic-combination-kind dest) :full)))
391 ram 1.4 (unless (policy node (= brevity 3))
392 wlott 1.1 (let ((*compiler-error-context* node))
393 ram 1.3 (if (and (ref-p node) (constant-p (ref-leaf node)))
394     (compiler-warning "This is not a ~S:~% ~S"
395     (type-specifier atype)
396     (constant-value (ref-leaf node)))
397     (compiler-warning "Result is a ~S, not a ~S."
398     (type-specifier dtype)
399     (type-specifier atype)))))))
400 wlott 1.1
401     (let ((check-p (probable-type-check-p cont)))
402     (multiple-value-bind (check types)
403     (continuation-check-types cont)
404     (ecase check
405     (:simple
406     (unless check-p
407     (setf (continuation-%type-check cont) :no-check)))
408     (:hairy
409     (if check-p
410     (convert-type-check cont types)
411     (setf (continuation-%type-check cont) :no-check)))
412     (:too-hairy
413     (let* ((context (continuation-dest cont))
414     (*compiler-error-context* context))
415     (when (policy context (>= safety brevity))
416     (compiler-note
417     "Type assertion too complex to check:~% ~S."
418     (type-specifier (continuation-asserted-type cont)))))
419     (setf (continuation-%type-check cont) :deleted)))))))
420    
421     (setf (block-type-check block) nil)))
422    
423     (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5