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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5