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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5