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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5