Add MAKE-DUMMY-FDEFN function.
[sbcl.git] / src / compiler / ir1opt.lisp
blob6c061c485856071216e844371bbb55fb75a78ab3
1 ;;;; This file implements the IR1 optimization phase of the compiler.
2 ;;;; IR1 optimization is a grab-bag of optimizations that don't make
3 ;;;; major changes to the block-level control flow and don't use flow
4 ;;;; analysis. These optimizations can mostly be classified as
5 ;;;; "meta-evaluation", but there is a sizable top-down component as
6 ;;;; well.
8 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; more information.
10 ;;;;
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!C")
19 ;;;; interface for obtaining results of constant folding
21 ;;; Return true for an LVAR whose sole use is a reference to a
22 ;;; constant leaf.
23 (defun constant-lvar-p (thing)
24 (declare (type (or lvar null) thing))
25 (and (lvar-p thing)
26 (or (let ((use (principal-lvar-use thing)))
27 (and (ref-p use) (constant-p (ref-leaf use))))
28 ;; check for EQL types and singleton numeric types
29 (values (type-singleton-p (lvar-type thing))))))
31 ;;; Same as above except it doesn't consider EQL types
32 (defun strictly-constant-lvar-p (thing)
33 (declare (type (or lvar null) thing))
34 (and (lvar-p thing)
35 (let ((use (principal-lvar-use thing)))
36 (and (ref-p use) (constant-p (ref-leaf use))))))
38 ;;; Return the constant value for an LVAR whose only use is a constant
39 ;;; node.
40 (declaim (ftype (function (lvar) t) lvar-value))
41 (defun lvar-value (lvar)
42 (let ((use (principal-lvar-use lvar))
43 (type (lvar-type lvar))
44 leaf)
45 (if (and (ref-p use)
46 (constant-p (setf leaf (ref-leaf use))))
47 (constant-value leaf)
48 (multiple-value-bind (constantp value) (type-singleton-p type)
49 (unless constantp
50 (error "~S used on non-constant LVAR ~S" 'lvar-value lvar))
51 value))))
53 ;;;; interface for obtaining results of type inference
55 ;;; Our best guess for the type of this lvar's value. Note that this
56 ;;; may be VALUES or FUNCTION type, which cannot be passed as an
57 ;;; argument to the normal type operations. See LVAR-TYPE.
58 ;;;
59 ;;; The result value is cached in the LVAR-%DERIVED-TYPE slot. If the
60 ;;; slot is true, just return that value, otherwise recompute and
61 ;;; stash the value there.
62 (eval-when (:compile-toplevel :execute)
63 (#+sb-xc-host cl:defmacro
64 #-sb-xc-host sb!xc:defmacro
65 lvar-type-using (lvar accessor)
66 `(let ((uses (lvar-uses ,lvar)))
67 (cond ((null uses) *empty-type*)
68 ((listp uses)
69 (do ((res (,accessor (first uses))
70 (values-type-union (,accessor (first current))
71 res))
72 (current (rest uses) (rest current)))
73 ((or (null current) (eq res *wild-type*))
74 res)))
76 (,accessor uses))))))
78 (defun %lvar-derived-type (lvar)
79 (lvar-type-using lvar node-derived-type))
81 ;;; Return the derived type for LVAR's first value. This is guaranteed
82 ;;; not to be a VALUES or FUNCTION type.
83 (declaim (ftype (sfunction (lvar) ctype) lvar-type))
84 (defun lvar-type (lvar)
85 (single-value-type (lvar-derived-type lvar)))
87 ;;; LVAR-CONSERVATIVE-TYPE
88 ;;;
89 ;;; Certain types refer to the contents of an object, which can
90 ;;; change without type derivation noticing: CONS types and ARRAY
91 ;;; types suffer from this:
92 ;;;
93 ;;; (let ((x (the (cons fixnum fixnum) (cons a b))))
94 ;;; (setf (car x) c)
95 ;;; (+ (car x) (cdr x)))
96 ;;;
97 ;;; Python doesn't realize that the SETF CAR can change the type of X -- so we
98 ;;; cannot use LVAR-TYPE which gets the derived results. Worse, still, instead
99 ;;; of (SETF CAR) we might have a call to a user-defined function FOO which
100 ;;; does the same -- so there is no way to use the derived information in
101 ;;; general.
103 ;;; So, the conservative option is to use the derived type if the leaf has
104 ;;; only a single ref -- in which case there cannot be a prior call that
105 ;;; mutates it. Otherwise we use the declared type or punt to the most general
106 ;;; type we know to be correct for sure.
107 (defun lvar-conservative-type (lvar)
108 (let ((derived-type (lvar-type lvar))
109 (t-type *universal-type*))
110 ;; Recompute using NODE-CONSERVATIVE-TYPE instead of derived type if
111 ;; necessary -- picking off some easy cases up front.
112 (cond ((or (eq derived-type t-type)
113 ;; Can't use CSUBTYPEP!
114 (type= derived-type (specifier-type 'list))
115 (type= derived-type (specifier-type 'null)))
116 derived-type)
117 ((and (cons-type-p derived-type)
118 (eq t-type (cons-type-car-type derived-type))
119 (eq t-type (cons-type-cdr-type derived-type)))
120 derived-type)
121 ((and (array-type-p derived-type)
122 (or (not (array-type-complexp derived-type))
123 (let ((dimensions (array-type-dimensions derived-type)))
124 (or (eq '* dimensions)
125 (every (lambda (dim) (eq '* dim)) dimensions)))))
126 derived-type)
127 ((type-needs-conservation-p derived-type)
128 (single-value-type (lvar-type-using lvar node-conservative-type)))
130 derived-type))))
132 (defun node-conservative-type (node)
133 (let* ((derived-values-type (node-derived-type node))
134 (derived-type (single-value-type derived-values-type)))
135 (if (ref-p node)
136 (let ((leaf (ref-leaf node)))
137 (if (and (basic-var-p leaf)
138 (cdr (leaf-refs leaf)))
139 (coerce-to-values
140 (if (eq :declared (leaf-where-from leaf))
141 (leaf-type leaf)
142 (conservative-type derived-type)))
143 derived-values-type))
144 derived-values-type)))
146 (defun conservative-type (type)
147 (cond ((or (eq type *universal-type*)
148 (eq type (specifier-type 'list))
149 (eq type (specifier-type 'null)))
150 type)
151 ((cons-type-p type)
152 (specifier-type 'cons))
153 ((array-type-p type)
154 (if (array-type-complexp type)
155 (make-array-type
156 ;; ADJUST-ARRAY may change dimensions, but rank stays same.
157 (let ((old (array-type-dimensions type)))
158 (if (eq '* old)
160 (mapcar (constantly '*) old)))
161 ;; Complexity cannot change.
162 :complexp (array-type-complexp type)
163 ;; Element type cannot change.
164 :element-type (array-type-element-type type)
165 :specialized-element-type (array-type-specialized-element-type type))
166 ;; Simple arrays cannot change at all.
167 type))
168 ((union-type-p type)
169 ;; Conservative union type is an union of conservative types.
170 (let ((res *empty-type*))
171 (dolist (part (union-type-types type) res)
172 (setf res (type-union res (conservative-type part))))))
174 ;; Catch-all.
176 ;; If the type contains some CONS types, the conservative type contains all
177 ;; of them.
178 (when (types-equal-or-intersect type (specifier-type 'cons))
179 (setf type (type-union type (specifier-type 'cons))))
180 ;; Similarly for non-simple arrays -- it should be possible to preserve
181 ;; more information here, but really...
182 (let ((non-simple-arrays (specifier-type '(and array (not simple-array)))))
183 (when (types-equal-or-intersect type non-simple-arrays)
184 (setf type (type-union type non-simple-arrays))))
185 type)))
187 (defun type-needs-conservation-p (type)
188 (cond ((eq type *universal-type*)
189 ;; Excluding T is necessary, because we do want type derivation to
190 ;; be able to narrow it down in case someone (most like a macro-expansion...)
191 ;; actually declares something as having type T.
192 nil)
193 ((or (cons-type-p type) (and (array-type-p type) (array-type-complexp type)))
194 ;; Covered by the next case as well, but this is a quick test.
196 ((types-equal-or-intersect type (specifier-type '(or cons (and array (not simple-array)))))
197 t)))
199 ;;; If LVAR is an argument of a function, return a type which the
200 ;;; function checks LVAR for.
201 #!-sb-fluid (declaim (inline lvar-externally-checkable-type))
202 (defun lvar-externally-checkable-type (lvar)
203 (or (lvar-%externally-checkable-type lvar)
204 (%lvar-%externally-checkable-type lvar)))
205 (defun %lvar-%externally-checkable-type (lvar)
206 (declare (type lvar lvar))
207 (let ((dest (lvar-dest lvar)))
208 (if (not (and dest (combination-p dest)))
209 ;; TODO: MV-COMBINATION
210 (setf (lvar-%externally-checkable-type lvar) *wild-type*)
211 (let* ((fun (combination-fun dest))
212 (args (combination-args dest))
213 (fun-type (lvar-type fun)))
214 (setf (lvar-%externally-checkable-type fun) *wild-type*)
215 (if (or (not (call-full-like-p dest))
216 (not (fun-type-p fun-type))
217 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
218 (fun-type-wild-args fun-type))
219 (dolist (arg args)
220 (when arg
221 (setf (lvar-%externally-checkable-type arg)
222 *wild-type*)))
223 (map-combination-args-and-types
224 (lambda (arg type)
225 (setf (lvar-%externally-checkable-type arg)
226 (acond ((lvar-%externally-checkable-type arg)
227 (values-type-intersection
228 it (coerce-to-values type)))
229 (t (coerce-to-values type)))))
230 dest)))))
231 (or (lvar-%externally-checkable-type lvar) *wild-type*))
233 ;;;; interface routines used by optimizers
235 ;;; This function is called by optimizers to indicate that something
236 ;;; interesting has happened to the value of LVAR. Optimizers must
237 ;;; make sure that they don't call for reoptimization when nothing has
238 ;;; happened, since optimization will fail to terminate.
240 ;;; We clear any cached type for the lvar and set the reoptimize flags
241 ;;; on everything in sight.
242 (defun reoptimize-lvar (lvar)
243 (declare (type (or lvar null) lvar))
244 (when lvar
245 (setf (lvar-%derived-type lvar) nil)
246 (let ((dest (lvar-dest lvar)))
247 (when dest
248 (setf (lvar-reoptimize lvar) t)
249 (setf (node-reoptimize dest) t)
250 (binding* (;; Since this may be called during IR1 conversion,
251 ;; PREV may be missing.
252 (prev (node-prev dest) :exit-if-null)
253 (block (ctran-block prev))
254 (component (block-component block)))
255 (when (typep dest 'cif)
256 (setf (block-test-modified block) t))
257 (setf (block-reoptimize block) t)
258 (reoptimize-component component :maybe))))
259 (do-uses (node lvar)
260 (setf (block-type-check (node-block node)) t)))
261 (values))
263 (defun reoptimize-lvar-uses (lvar)
264 (declare (type lvar lvar))
265 (do-uses (use lvar)
266 (setf (node-reoptimize use) t)
267 (setf (block-reoptimize (node-block use)) t)
268 (reoptimize-component (node-component use) :maybe)))
270 ;;; Annotate NODE to indicate that its result has been proven to be
271 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
272 ;;; only correct way to supply information discovered about a node's
273 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
274 ;;; information may be lost and reoptimization may not happen.
276 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
277 ;;; intersection is different from the old type, then we do a
278 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
279 (defun derive-node-type (node rtype &key from-scratch)
280 (declare (type valued-node node) (type ctype rtype))
281 (let* ((initial-type (node-derived-type node))
282 (node-type (if from-scratch
283 *wild-type*
284 initial-type)))
285 (unless (eq initial-type rtype)
286 (let ((int (values-type-intersection node-type rtype))
287 (lvar (node-lvar node)))
288 (when (type/= initial-type int)
289 (when (and *check-consistency*
290 (eq int *empty-type*)
291 (not (eq rtype *empty-type*)))
292 (aver (not from-scratch))
293 (let ((*compiler-error-context* node))
294 (compiler-warn
295 "New inferred type ~S conflicts with old type:~
296 ~% ~S~%*** possible internal error? Please report this."
297 (type-specifier rtype) (type-specifier node-type))))
298 (setf (node-derived-type node) int)
299 ;; If the new type consists of only one object, replace the
300 ;; node with a constant reference.
301 (when (and (ref-p node)
302 (lambda-var-p (ref-leaf node)))
303 (let ((type (single-value-type int)))
304 (when (and (member-type-p type)
305 (eql 1 (member-type-size type)))
306 (change-ref-leaf node (find-constant
307 (first (member-type-members type)))))))
308 (reoptimize-lvar lvar)))))
309 (values))
311 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
312 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
313 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
314 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
315 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
316 (defun assert-lvar-type (lvar type policy &optional context)
317 (declare (type lvar lvar) (type ctype type))
318 (unless (values-subtypep (lvar-derived-type lvar) type)
319 (let ((internal-lvar (make-lvar))
320 (dest (lvar-dest lvar)))
321 (substitute-lvar internal-lvar lvar)
322 (let ((cast (insert-cast-before dest lvar type policy
323 context)))
324 (use-lvar cast internal-lvar)
325 t))))
327 (defun insert-function-designator-cast-before (next lvar type arg-count caller policy)
328 (declare (type node next) (type lvar lvar) (type ctype type))
329 (with-ir1-environment-from-node next
330 (let* ((ctran (node-prev next))
331 (cast (make-function-designator-cast :asserted-type type
332 :type-to-check (maybe-weaken-check type policy)
333 :value lvar
334 :derived-type (coerce-to-values type)
335 :arg-count arg-count
336 :caller caller))
337 (internal-ctran (make-ctran)))
338 (setf (ctran-next ctran) cast
339 (node-prev cast) ctran)
340 (use-ctran cast internal-ctran)
341 (link-node-to-previous-ctran next internal-ctran)
342 (setf (lvar-dest lvar) cast)
343 (reoptimize-lvar lvar)
344 (when (return-p next)
345 (node-ends-block cast))
346 (setf (block-attributep (block-flags (node-block cast))
347 type-check type-asserted)
349 cast)))
351 (defun assert-function-designator-lvar-type (lvar type arg-count caller policy)
352 (declare (type lvar lvar) (type ctype type))
353 (let ((internal-lvar (make-lvar))
354 (dest (lvar-dest lvar)))
355 (substitute-lvar internal-lvar lvar)
356 (let ((cast (insert-function-designator-cast-before dest lvar type arg-count
357 caller policy)))
358 (use-lvar cast internal-lvar))))
361 ;;;; IR1-OPTIMIZE
363 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
364 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
365 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
366 ;;; we are done, then another iteration would be beneficial.
367 (defun ir1-optimize (component fastp)
368 (declare (type component component))
369 (setf (component-reoptimize component) nil)
370 (loop with block = (block-next (component-head component))
371 with tail = (component-tail component)
372 for last-block = block
373 until (eq block tail)
374 do (cond
375 ;; We delete blocks when there is either no predecessor or the
376 ;; block is in a lambda that has been deleted. These blocks
377 ;; would eventually be deleted by DFO recomputation, but doing
378 ;; it here immediately makes the effect available to IR1
379 ;; optimization.
380 ((or (block-delete-p block)
381 (null (block-pred block)))
382 (delete-block-lazily block)
383 (setq block (clean-component component block)))
384 ((eq (functional-kind (block-home-lambda block)) :deleted)
385 ;; Preserve the BLOCK-SUCC invariant that almost every block has
386 ;; one successor (and a block with DELETE-P set is an acceptable
387 ;; exception).
388 (mark-for-deletion block)
389 (setq block (clean-component component block)))
391 (loop
392 (let ((succ (block-succ block)))
393 (unless (singleton-p succ)
394 (return)))
396 (let ((last (block-last block)))
397 (typecase last
398 (cif
399 (flush-dest (if-test last))
400 (when (unlink-node last)
401 (return)))
402 (exit
403 (when (maybe-delete-exit last)
404 (return)))))
406 (unless (join-successor-if-possible block)
407 (return)))
409 (when (and (not fastp) (block-reoptimize block) (block-component block))
410 (aver (not (block-delete-p block)))
411 (ir1-optimize-block block))
413 (cond ((and (block-delete-p block) (block-component block))
414 (setq block (clean-component component block)))
415 ((and (block-flush-p block) (block-component block))
416 (flush-dead-code block)))))
417 do (when (eq block last-block)
418 (setq block (block-next block))))
420 (values))
422 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
423 ;;; flags.
425 ;;; Note that although they are cleared here, REOPTIMIZE flags might
426 ;;; still be set upon return from this function, meaning that further
427 ;;; optimization is wanted (as a consequence of optimizations we did).
428 (defun ir1-optimize-block (block)
429 (declare (type cblock block))
430 ;; We clear the node and block REOPTIMIZE flags before doing the
431 ;; optimization, not after. This ensures that the node or block will
432 ;; be reoptimized if necessary.
433 (setf (block-reoptimize block) nil)
434 (do-nodes (node nil block :restart-p t)
435 (when (node-reoptimize node)
436 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
437 (setf (node-reoptimize node) nil)
438 (typecase node
439 (ref)
440 (combination
441 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
442 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
443 ;; any argument changes.
444 (ir1-optimize-combination node))
445 (cif
446 (ir1-optimize-if node))
447 (creturn
448 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
449 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
450 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
451 ;; CMU CL comments
452 (setf (node-reoptimize node) t)
453 (ir1-optimize-return node))
454 (mv-combination
455 (ir1-optimize-mv-combination node))
456 (exit
457 ;; With an EXIT, we derive the node's type from the VALUE's
458 ;; type.
459 (let ((value (exit-value node)))
460 (when value
461 (derive-node-type node (lvar-derived-type value)))))
462 (cset
463 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
464 ;; is accurate till the node actually has been reoptimized.
465 (setf (node-reoptimize node) t)
466 (ir1-optimize-set node))
467 (cast
468 (ir1-optimize-cast node)))))
470 (values))
472 ;;; Try to join with a successor block. If we succeed, we return true,
473 ;;; otherwise false.
474 (defun join-successor-if-possible (block)
475 (declare (type cblock block))
476 (let ((next (first (block-succ block))))
477 (when (block-start next) ; NEXT is not an END-OF-COMPONENT marker
478 (cond ( ;; We cannot combine with a successor block if:
480 ;; the successor has more than one predecessor;
481 (rest (block-pred next))
482 ;; the successor is the current block (infinite loop);
483 (eq next block)
484 ;; the next block has a different cleanup, and thus
485 ;; we may want to insert cleanup code between the
486 ;; two blocks at some point;
487 (not (eq (block-end-cleanup block)
488 (block-start-cleanup next)))
489 ;; the next block has a different home lambda, and
490 ;; thus the control transfer is a non-local exit.
491 (not (eq (block-home-lambda block)
492 (block-home-lambda next)))
493 ;; Stack analysis phase wants ENTRY to start a block...
494 (entry-p (block-start-node next))
495 (let ((last (block-last block)))
496 (and (valued-node-p last)
497 (awhen (node-lvar last)
499 ;; ... and a DX-allocator to end a block.
500 (lvar-dynamic-extent it)
501 ;; FIXME: This is a partial workaround for bug 303.
502 (consp (lvar-uses it)))))))
503 nil)
505 (join-blocks block next)
506 t)))))
508 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
509 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
510 ;;; for the two blocks so that any indicated optimization gets done.
511 (defun join-blocks (block1 block2)
512 (declare (type cblock block1 block2))
513 (let* ((last1 (block-last block1))
514 (last2 (block-last block2))
515 (succ (block-succ block2))
516 (start2 (block-start block2)))
517 (do ((ctran start2 (node-next (ctran-next ctran))))
518 ((not ctran))
519 (setf (ctran-block ctran) block1))
521 (unlink-blocks block1 block2)
522 (dolist (block succ)
523 (unlink-blocks block2 block)
524 (link-blocks block1 block))
526 (setf (ctran-kind start2) :inside-block)
527 (setf (node-next last1) start2)
528 (setf (ctran-use start2) last1)
529 (setf (block-last block1) last2))
531 (setf (block-flags block1)
532 (attributes-union (block-flags block1)
533 (block-flags block2)
534 (block-attributes type-asserted test-modified)))
536 (let ((next (block-next block2))
537 (prev (block-prev block2)))
538 (setf (block-next prev) next)
539 (setf (block-prev next) prev))
541 (values))
543 ;;; Delete any nodes in BLOCK whose value is unused and which have no
544 ;;; side effects. We can delete sets of lexical variables when the set
545 ;;; variable has no references.
546 (defun flush-dead-code (block &aux victim)
547 (declare (type cblock block))
548 (setf (block-flush-p block) nil)
549 (do-nodes-backwards (node lvar block :restart-p t)
550 (unless lvar
551 (typecase node
552 (ref
553 (setf victim node)
554 (delete-ref node)
555 (unlink-node node))
556 (combination
557 (when (flushable-combination-p node)
558 (setf victim node)
559 (flush-combination node)))
560 (mv-combination
561 (when (eq (basic-combination-kind node) :local)
562 (let ((fun (combination-lambda node)))
563 (when (dolist (var (lambda-vars fun) t)
564 (when (or (leaf-refs var)
565 (lambda-var-sets var))
566 (return nil)))
567 (setf victim node)
568 (mapc #'flush-dest (basic-combination-args node))
569 (delete-let fun)))))
570 (exit
571 (let ((value (exit-value node)))
572 (when value
573 (setf victim node)
574 (flush-dest value)
575 (setf (exit-value node) nil))))
576 (cset
577 (let ((var (set-var node)))
578 (when (and (lambda-var-p var)
579 (null (leaf-refs var)))
580 (setf victim node)
581 (flush-dest (set-value node))
582 (setf (basic-var-sets var)
583 (delq node (basic-var-sets var)))
584 (unlink-node node))))
585 (cast
586 (unless (cast-type-check node)
587 (setf victim node)
588 (flush-dest (cast-value node))
589 (unlink-node node))))))
591 victim)
593 ;;;; local call return type propagation
595 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
596 ;;; flag set. It iterates over the uses of the RESULT, looking for
597 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
598 ;;; call, then we union its type together with the types of other such
599 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
600 ;;; type with the RESULT's asserted type. We can make this
601 ;;; intersection now (potentially before type checking) because this
602 ;;; assertion on the result will eventually be checked (if
603 ;;; appropriate.)
605 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
606 ;;; combination, which may change the successor of the call to be the
607 ;;; called function, and if so, checks if the call can become an
608 ;;; assignment. If we convert to an assignment, we abort, since the
609 ;;; RETURN has been deleted.
610 (defun find-result-type (node)
611 (declare (type creturn node))
612 (let ((result (return-result node)))
613 (collect ((use-union *empty-type* values-type-union))
614 (do-uses (use result)
615 (let ((use-home (node-home-lambda use)))
616 (cond ((or (eq (functional-kind use-home) :deleted)
617 (block-delete-p (node-block use))))
618 ((and (basic-combination-p use)
619 (eq (basic-combination-kind use) :local))
620 (aver (eq (lambda-tail-set use-home)
621 (lambda-tail-set (combination-lambda use))))
622 (when (combination-p use)
623 (when (nth-value 1 (maybe-convert-tail-local-call use))
624 (return-from find-result-type t))))
626 (use-union (node-derived-type use))))))
627 (let ((int
628 ;; (values-type-intersection
629 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
630 (use-union)
631 ;; )
633 (setf (return-result-type node) int))))
634 nil)
636 ;;; Do stuff to realize that something has changed about the value
637 ;;; delivered to a return node. Since we consider the return values of
638 ;;; all functions in the tail set to be equivalent, this amounts to
639 ;;; bringing the entire tail set up to date. We iterate over the
640 ;;; returns for all the functions in the tail set, reanalyzing them
641 ;;; all (not treating NODE specially.)
643 ;;; When we are done, we check whether the new type is different from
644 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
645 ;;; all the lvars for references to functions in the tail set. This
646 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
647 ;;; results of the calls.
648 (defun ir1-optimize-return (node)
649 (declare (type creturn node))
650 (tagbody
651 :restart
652 (let* ((tails (lambda-tail-set (return-lambda node)))
653 (funs (tail-set-funs tails)))
654 (collect ((res *empty-type* values-type-union))
655 (dolist (fun funs)
656 (let ((return (lambda-return fun)))
657 (when return
658 (when (node-reoptimize return)
659 (setf (node-reoptimize return) nil)
660 (when (find-result-type return)
661 (go :restart)))
662 (res (return-result-type return)))))
664 (when (type/= (res) (tail-set-type tails))
665 (setf (tail-set-type tails) (res))
666 (dolist (fun (tail-set-funs tails))
667 (dolist (ref (leaf-refs fun))
668 (reoptimize-lvar (node-lvar ref))))))))
670 (values))
672 ;;;; IF optimization
674 ;;; Utility: return T if both argument cblocks are equivalent. For now,
675 ;;; detect only blocks that read the same leaf into the same lvar, and
676 ;;; continue to the same block.
677 (defun cblocks-equivalent-p (x y)
678 (declare (type cblock x y))
679 (and (ref-p (block-start-node x))
680 (eq (block-last x) (block-start-node x))
682 (ref-p (block-start-node y))
683 (eq (block-last y) (block-start-node y))
685 (equal (block-succ x) (block-succ y))
686 (eql (ref-lvar (block-start-node x)) (ref-lvar (block-start-node y)))
687 (eql (ref-leaf (block-start-node x)) (ref-leaf (block-start-node y)))))
689 ;;; Check whether the predicate is known to be true or false,
690 ;;; deleting the IF node in favor of the appropriate branch when this
691 ;;; is the case.
692 ;;; Similarly, when both branches are equivalent, branch directly to either
693 ;;; of them.
694 ;;; Also, if the test has multiple uses, replicate the node when possible...
695 ;;; in fact, splice in direct jumps to the right branch if possible.
696 (defun ir1-optimize-if (node)
697 (declare (type cif node))
698 (let ((test (if-test node))
699 (block (node-block node)))
700 (let* ((type (lvar-type test))
701 (consequent (if-consequent node))
702 (alternative (if-alternative node))
703 (victim
704 (cond ((constant-lvar-p test)
705 (if (lvar-value test) alternative consequent))
706 ((not (types-equal-or-intersect type (specifier-type 'null)))
707 alternative)
708 ((type= type (specifier-type 'null))
709 consequent)
710 ((or (eq consequent alternative) ; Can this happen?
711 (cblocks-equivalent-p alternative consequent))
712 alternative))))
713 (when victim
714 (kill-if-branch-1 node test block victim)
715 (return-from ir1-optimize-if (values))))
716 (tension-if-if-1 node test block)
717 (duplicate-if-if-1 node test block)
718 (values)))
720 ;; When we know that we only have a single successor, kill the victim
721 ;; ... unless the victim and the remaining successor are the same.
722 (defun kill-if-branch-1 (node test block victim)
723 (declare (type cif node))
724 (flush-dest test)
725 (when (rest (block-succ block))
726 (unlink-blocks block victim))
727 (setf (component-reanalyze (node-component node)) t)
728 (unlink-node node))
730 ;; When if/if conversion would leave (if ... (if nil ...)) or
731 ;; (if ... (if not-nil ...)), splice the correct successor right
732 ;; in.
733 (defun tension-if-if-1 (node test block)
734 (when (and (eq (block-start-node block) node)
735 (listp (lvar-uses test)))
736 (do-uses (use test)
737 (when (immediately-used-p test use)
738 (let* ((type (single-value-type (node-derived-type use)))
739 (target (if (type= type (specifier-type 'null))
740 (if-alternative node)
741 (multiple-value-bind (typep surep)
742 (ctypep nil type)
743 (and (not typep) surep
744 (if-consequent node))))))
745 (when target
746 (let ((pred (node-block use)))
747 (cond ((listp (lvar-uses test))
748 (change-block-successor pred block target)
749 (delete-lvar-use use))
751 ;; only one use left. Just kill the now-useless
752 ;; branch to avoid spurious code deletion notes.
753 (aver (rest (block-succ block)))
754 (kill-if-branch-1
755 node test block
756 (if (eql target (if-alternative node))
757 (if-consequent node)
758 (if-alternative node)))
759 (return-from tension-if-if-1))))))))))
761 ;; Finally, duplicate EQ-nil tests
762 (defun duplicate-if-if-1 (node test block)
763 (when (and (eq (block-start-node block) node)
764 (listp (lvar-uses test)))
765 (do-uses (use test)
766 (when (immediately-used-p test use)
767 (convert-if-if use node)
768 ;; leave the last use as is, instead of replacing
769 ;; the (singly-referenced) CIF node with a duplicate.
770 (when (not (listp (lvar-uses test))) (return))))))
772 ;;; Create a new copy of an IF node that tests the value of the node
773 ;;; USE. The test must have >1 use, and must be immediately used by
774 ;;; USE. NODE must be the only node in its block (implying that
775 ;;; block-start = if-test).
777 ;;; This optimization has an effect semantically similar to the
778 ;;; source-to-source transformation:
779 ;;; (IF (IF A B C) D E) ==>
780 ;;; (IF A (IF B D E) (IF C D E))
782 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
783 ;;; node so that dead code deletion notes will definitely not consider
784 ;;; either node to be part of the original source. One node might
785 ;;; become unreachable, resulting in a spurious note.
786 (defun convert-if-if (use node)
787 (declare (type node use) (type cif node))
788 (with-ir1-environment-from-node node
789 (let* ((block (node-block node))
790 (test (if-test node))
791 (cblock (if-consequent node))
792 (ablock (if-alternative node))
793 (use-block (node-block use))
794 (new-ctran (make-ctran))
795 (new-lvar (make-lvar))
796 (new-node (make-if :test new-lvar
797 :consequent cblock
798 :alternative ablock))
799 (new-block (ctran-starts-block new-ctran)))
800 (link-node-to-previous-ctran new-node new-ctran)
801 (setf (lvar-dest new-lvar) new-node)
802 (setf (block-last new-block) new-node)
804 (unlink-blocks use-block block)
805 (%delete-lvar-use use)
806 (add-lvar-use use new-lvar)
807 (link-blocks use-block new-block)
809 (link-blocks new-block cblock)
810 (link-blocks new-block ablock)
812 (push "<IF Duplication>" (node-source-path node))
813 (push "<IF Duplication>" (node-source-path new-node))
815 (reoptimize-lvar test)
816 (reoptimize-lvar new-lvar)
817 (setf (component-reanalyze *current-component*) t)))
818 (values))
820 ;;;; exit IR1 optimization
822 ;;; This function attempts to delete an exit node, returning true if
823 ;;; it deletes the block as a consequence:
824 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
825 ;;; anything, since there is nothing to be done.
826 ;;; -- If the exit node and its ENTRY have the same home lambda then
827 ;;; we know the exit is local, and can delete the exit. We change
828 ;;; uses of the Exit-Value to be uses of the original lvar,
829 ;;; then unlink the node. If the exit is to a TR context, then we
830 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
831 ;;; their value to this exit.
832 ;;; -- If there is no value (as in a GO), then we skip the value
833 ;;; semantics.
835 ;;; This function is also called by environment analysis, since it
836 ;;; wants all exits to be optimized even if normal optimization was
837 ;;; omitted.
838 (defun maybe-delete-exit (node)
839 (declare (type exit node))
840 (let ((value (exit-value node))
841 (entry (exit-entry node)))
842 (when (and entry
843 (eq (node-home-lambda node) (node-home-lambda entry)))
844 (setf (entry-exits entry) (delq node (entry-exits entry)))
845 (if value
846 (with-ir1-environment-from-node entry
847 ;; We can't simply use DELETE-FILTER to unlink the node
848 ;; and substitute some LVAR magic, as this can confuse the
849 ;; stack analysis if there's another EXIT to the same
850 ;; continuation. Instead, we fabricate a new block (in
851 ;; the same lexenv as the ENTRY, so it can't be merged
852 ;; backwards), insert a gimmicked CAST node to link up the
853 ;; LVAR holding the value being returned to the LVAR which
854 ;; is expecting to accept the value, thus placing the
855 ;; return value where it needs to be while still providing
856 ;; the hook required for stack analysis.
857 ;; -- AJB, 2014-Mar-03
858 (let* ((exit-block (node-block node))
859 (new-ctran (make-ctran))
860 (new-block (ctran-starts-block new-ctran))
861 (cast-node
862 (%make-cast
863 :asserted-type *wild-type*
864 :type-to-check *wild-type*
865 :value value
866 :vestigial-exit-lexenv (node-lexenv node)
867 :vestigial-exit-entry-lexenv (node-lexenv entry)
868 :%type-check nil)))
869 ;; We only expect a single successor to EXIT-BLOCK,
870 ;; because it contains an EXIT node (which must end its
871 ;; block) and the only blocks that have more than once
872 ;; successor are those with IF nodes (which also must
873 ;; end their blocks). Still, just to be sure, we use a
874 ;; construct that guarantees an error if this
875 ;; expectation is violated.
876 (destructuring-bind
877 (entry-block)
878 (block-succ exit-block)
880 ;; Finish creating the new block.
881 (link-node-to-previous-ctran cast-node new-ctran)
882 (setf (block-last new-block) cast-node)
884 ;; Link the new block into the control sequence.
885 (unlink-blocks exit-block entry-block)
886 (link-blocks exit-block new-block)
887 (link-blocks new-block entry-block)
889 ;; Finish re-pointing the value-holding LVAR to the
890 ;; CAST node.
891 (setf (lvar-dest value) cast-node)
892 (setf (exit-value node) nil)
893 (reoptimize-lvar value)
895 ;; Register the CAST node as providing a value to the
896 ;; LVAR for the continuation.
897 (add-lvar-use cast-node (node-lvar node))
898 (reoptimize-lvar (node-lvar node))
900 ;; Remove the EXIT node.
901 (unlink-node node)
903 ;; And, because we created a new block, we need to
904 ;; force component reanalysis (to assign a DFO number
905 ;; to the block if nothing else).
906 (setf (component-reanalyze *current-component*) t))))
907 (unlink-node node)))))
910 ;;;; combination IR1 optimization
912 ;;; Report as we try each transform?
913 #!+sb-show
914 (defvar *show-transforms-p* nil)
916 (defun check-important-result (node info)
917 (when (and (null (node-lvar node))
918 (ir1-attributep (fun-info-attributes info) important-result))
919 (let ((*compiler-error-context* node))
920 (compiler-style-warn
921 "The return value of ~A should not be discarded."
922 (lvar-fun-name (basic-combination-fun node) t)))))
924 ;;; Do IR1 optimizations on a COMBINATION node.
925 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
926 (defun ir1-optimize-combination (node)
927 (when (lvar-reoptimize (basic-combination-fun node))
928 (propagate-fun-change node)
929 (maybe-terminate-block node nil))
930 (let ((args (basic-combination-args node))
931 (kind (basic-combination-kind node))
932 (info (basic-combination-fun-info node)))
933 (ecase kind
934 (:local
935 (let ((fun (combination-lambda node)))
936 (if (eq (functional-kind fun) :let)
937 (propagate-let-args node fun)
938 (propagate-local-call-args node fun))))
939 (:error
940 (dolist (arg args)
941 (when arg
942 (setf (lvar-reoptimize arg) nil))))
943 (:full
944 (dolist (arg args)
945 (when arg
946 (setf (lvar-reoptimize arg) nil)))
947 (cond (info
948 (check-important-result node info)
949 (let ((fun (fun-info-destroyed-constant-args info)))
950 (when (and fun (funcall fun args))
951 (let ((*compiler-error-context* node))
952 (warn 'constant-modified
953 :fun-name (lvar-fun-name
954 (basic-combination-fun node) t))
955 (setf (basic-combination-kind node) :error)
956 (return-from ir1-optimize-combination))))
957 (let ((fun (fun-info-derive-type info)))
958 (when fun
959 (let ((res (funcall fun node)))
960 (when res
961 (derive-node-type node (coerce-to-values res))
962 (maybe-terminate-block node nil))))))
964 ;; Check against the DEFINED-TYPE unless TYPE is already good.
965 (let* ((fun (basic-combination-fun node))
966 (uses (lvar-uses fun))
967 (leaf (when (ref-p uses) (ref-leaf uses))))
968 (multiple-value-bind (type defined-type)
969 (if (global-var-p leaf)
970 (values (leaf-type leaf) (leaf-defined-type leaf))
971 (values nil nil))
972 (when (and (not (fun-type-p type)) (fun-type-p defined-type))
973 (validate-call-type node type leaf)))))))
974 (:known
975 (aver info)
976 (dolist (arg args)
977 (when arg
978 (setf (lvar-reoptimize arg) nil)))
979 (check-important-result node info)
980 (let ((fun (fun-info-destroyed-constant-args info)))
981 (when (and fun
982 ;; If somebody is really sure that they want to modify
983 ;; constants, let them.
984 (policy node (> check-constant-modification 0)))
985 (let ((destroyed-constant-args (funcall fun args)))
986 (when destroyed-constant-args
987 (let ((*compiler-error-context* node))
988 (warn 'constant-modified
989 :fun-name (lvar-fun-name
990 (basic-combination-fun node)))
991 (setf (basic-combination-kind node) :error)
992 (return-from ir1-optimize-combination))))))
994 (let ((attr (fun-info-attributes info)))
995 (when (constant-fold-call-p node)
996 (constant-fold-call node)
997 (return-from ir1-optimize-combination))
998 (when (and (ir1-attributep attr commutative)
999 (= (length args) 2)
1000 (constant-lvar-p (first args))
1001 (not (constant-lvar-p (second args))))
1002 (setf (basic-combination-args node) (nreverse args))))
1003 (let ((fun (fun-info-derive-type info)))
1004 (when fun
1005 (let ((res (funcall fun node)))
1006 (when res
1007 (derive-node-type node (coerce-to-values res))
1008 (maybe-terminate-block node nil)))))
1010 (let ((fun (fun-info-optimizer info)))
1011 (unless (and fun (funcall fun node))
1012 ;; First give the VM a peek at the call
1013 (multiple-value-bind (style transform)
1014 (combination-implementation-style node)
1015 (ecase style
1016 (:direct
1017 ;; The VM knows how to handle this.
1019 (:transform
1020 ;; The VM mostly knows how to handle this. We need
1021 ;; to massage the call slightly, though.
1022 (transform-call node transform (combination-fun-source-name node)))
1023 ((:default :maybe)
1024 ;; Let transforms have a crack at it.
1025 (dolist (x (fun-info-transforms info))
1026 #!+sb-show
1027 (when *show-transforms-p*
1028 (let* ((lvar (basic-combination-fun node))
1029 (fname (lvar-fun-name lvar t)))
1030 (/show "trying transform" x (transform-function x) "for" fname)))
1031 (unless (ir1-transform node x)
1032 #!+sb-show
1033 (when *show-transforms-p*
1034 (/show "quitting because IR1-TRANSFORM result was NIL"))
1035 (return)))))))))))
1037 (values))
1039 (defun xep-tail-combination-p (node)
1040 (and (combination-p node)
1041 (let* ((lvar (combination-lvar node))
1042 (dest (when (lvar-p lvar) (lvar-dest lvar)))
1043 (lambda (when (return-p dest) (return-lambda dest))))
1044 (and (lambda-p lambda)
1045 (eq :external (lambda-kind lambda))))))
1047 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
1048 ;;; the block there, and link it to the component tail.
1050 ;;; Except when called during IR1 convertion, we delete the
1051 ;;; continuation if it has no other uses. (If it does have other uses,
1052 ;;; we reoptimize.)
1054 ;;; Termination on the basis of a continuation type is
1055 ;;; inhibited when:
1056 ;;; -- The continuation is deleted (hence the assertion is spurious), or
1057 ;;; -- We are in IR1 conversion (where THE assertions are subject to
1058 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
1059 ;;; uses can(?) be added later. -- APD, 2003-07-17
1061 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
1062 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p)
1063 (declare (type (or basic-combination cast ref) node))
1064 (let* ((block (node-block node))
1065 (lvar (node-lvar node))
1066 (ctran (node-next node))
1067 (tail (component-tail (block-component block)))
1068 (succ (first (block-succ block))))
1069 (declare (ignore lvar))
1070 (unless (or (and (eq node (block-last block)) (eq succ tail))
1071 (block-delete-p block))
1072 ;; Even if the combination will never return, don't terminate if this
1073 ;; is the tail call of a XEP: doing that would inhibit TCO.
1074 (when (and (eq (node-derived-type node) *empty-type*)
1075 (not (xep-tail-combination-p node)))
1076 (cond (ir1-converting-not-optimizing-p
1077 (cond
1078 ((block-last block)
1079 (aver (eq (block-last block) node)))
1081 (setf (block-last block) node)
1082 (setf (ctran-use ctran) nil)
1083 (setf (ctran-kind ctran) :unused)
1084 (setf (ctran-block ctran) nil)
1085 (setf (node-next node) nil)
1086 (link-blocks block (ctran-starts-block ctran)))))
1088 (node-ends-block node)))
1090 (let ((succ (first (block-succ block))))
1091 (unlink-blocks block succ)
1092 (setf (component-reanalyze (block-component block)) t)
1093 (aver (not (block-succ block)))
1094 (link-blocks block tail)
1095 (cond (ir1-converting-not-optimizing-p
1096 (%delete-lvar-use node))
1097 (t (delete-lvar-use node)
1098 (when (null (block-pred succ))
1099 (mark-for-deletion succ)))))
1100 t))))
1102 ;;; This is called both by IR1 conversion and IR1 optimization when
1103 ;;; they have verified the type signature for the call, and are
1104 ;;; wondering if something should be done to special-case the call. If
1105 ;;; CALL is a call to a global function, then see whether it defined
1106 ;;; or known:
1107 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
1108 ;;; the expansion and change the call to call it. Expansion is
1109 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
1110 ;;; true, we never expand, since this function has already been
1111 ;;; converted. Local call analysis will duplicate the definition
1112 ;;; if necessary. We claim that the parent form is LABELS for
1113 ;;; context declarations, since we don't want it to be considered
1114 ;;; a real global function.
1115 ;;; -- If it is a known function, mark it as such by setting the KIND.
1117 ;;; We return the leaf referenced (NIL if not a leaf) and the
1118 ;;; FUN-INFO assigned.
1119 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
1120 (declare (type combination call))
1121 (let* ((ref (lvar-uses (basic-combination-fun call)))
1122 (leaf (when (ref-p ref) (ref-leaf ref)))
1123 (inlinep (if (defined-fun-p leaf)
1124 (defined-fun-inlinep leaf)
1125 :no-chance)))
1126 (cond
1127 ((eq inlinep :notinline)
1128 (let ((info (info :function :info (leaf-source-name leaf))))
1129 (when info
1130 (setf (basic-combination-fun-info call) info))
1131 (values nil nil)))
1132 ((not (and (global-var-p leaf)
1133 (eq (global-var-kind leaf) :global-function)))
1134 (values leaf nil))
1135 ((and (ecase inlinep
1136 (:inline t)
1137 (:no-chance nil)
1138 ((nil :maybe-inline) (policy call (zerop space))))
1139 (defined-fun-p leaf)
1140 (defined-fun-inline-expansion leaf)
1141 (inline-expansion-ok call))
1142 ;; Inline: if the function has already been converted at another call
1143 ;; site in this component, we point this REF to the functional. If not,
1144 ;; we convert the expansion.
1146 ;; For :INLINE case local call analysis will copy the expansion later,
1147 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1148 ;; expansion per component.
1150 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1151 ;; is it for?
1152 (flet ((frob ()
1153 (let* ((name (leaf-source-name leaf))
1154 (res (ir1-convert-inline-expansion
1155 name
1156 (defined-fun-inline-expansion leaf)
1157 leaf
1158 inlinep
1159 (info :function :info name))))
1160 ;; Allow backward references to this function from following
1161 ;; forms. (Reused only if policy matches.)
1162 (push res (defined-fun-functionals leaf))
1163 (change-ref-leaf ref res))))
1164 (let ((fun (defined-fun-functional leaf)))
1165 (if (or (not fun)
1166 (and (eq inlinep :inline) (functional-kind fun)))
1167 ;; Convert.
1168 (if ir1-converting-not-optimizing-p
1169 (frob)
1170 (with-ir1-environment-from-node call
1171 (frob)
1172 (locall-analyze-component *current-component*)))
1173 ;; If we've already converted, change ref to the converted
1174 ;; functional.
1175 (change-ref-leaf ref fun))))
1176 (values (ref-leaf ref) nil))
1178 (let ((info (info :function :info (leaf-source-name leaf))))
1179 (if info
1180 (values leaf
1181 (progn
1182 (setf (basic-combination-kind call) :known)
1183 (setf (basic-combination-fun-info call) info)))
1184 (values leaf nil)))))))
1186 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1187 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1188 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1189 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1190 ;;; syntax check, arg/result type processing, but still call
1191 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1192 ;;; and that checking is done by local call analysis.
1193 (defun validate-call-type (call type fun &optional ir1-converting-not-optimizing-p)
1194 (declare (type combination call) (type ctype type))
1195 (let* ((where (when fun (leaf-where-from fun)))
1196 (same-file-p (eq :defined-here where)))
1197 (cond ((not (fun-type-p type))
1198 (aver (multiple-value-bind (val win)
1199 (csubtypep type (specifier-type 'function))
1200 (or val (not win))))
1201 ;; Using the defined-type too early is a bit of a waste: during
1202 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1203 (when (and fun (not ir1-converting-not-optimizing-p))
1204 (let ((defined-type (leaf-defined-type fun)))
1205 (when (and (fun-type-p defined-type)
1206 (neq fun (combination-type-validated-for-leaf call)))
1207 ;; Don't validate multiple times against the same leaf --
1208 ;; it doesn't add any information, but may generate the same warning
1209 ;; multiple times.
1210 (setf (combination-type-validated-for-leaf call) fun)
1211 (when (and (valid-fun-use call defined-type
1212 :argument-test #'always-subtypep
1213 :result-test nil
1214 :lossage-fun (if same-file-p
1215 #'compiler-warn
1216 #'compiler-style-warn)
1217 :unwinnage-fun #'compiler-notify)
1218 same-file-p)
1219 (assert-call-type call defined-type nil)
1220 (maybe-terminate-block call ir1-converting-not-optimizing-p)))))
1221 (recognize-known-call call ir1-converting-not-optimizing-p))
1222 ((let ((*valid-callable-argument-assert-unknown-lvars* t))
1223 (valid-fun-use call type
1224 :argument-test #'always-subtypep
1225 :result-test nil
1226 :lossage-fun #'compiler-warn
1227 :unwinnage-fun #'compiler-notify))
1228 (assert-call-type call type)
1229 (maybe-terminate-block call ir1-converting-not-optimizing-p)
1230 (recognize-known-call call ir1-converting-not-optimizing-p))
1232 (setf (combination-kind call) :error)
1233 (values nil nil)))))
1235 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1236 ;;; changed. If the call is local, we try to LET-convert it, and
1237 ;;; derive the result type. If it is a :FULL call, we validate it
1238 ;;; against the type, which recognizes known calls, does inline
1239 ;;; expansion, etc. If a call to a predicate in a non-conditional
1240 ;;; position or to a function with a source transform, then we
1241 ;;; reconvert the form to give IR1 another chance.
1242 (defun propagate-fun-change (call)
1243 (declare (type combination call))
1244 (let ((*compiler-error-context* call)
1245 (fun-lvar (basic-combination-fun call)))
1246 (setf (lvar-reoptimize fun-lvar) nil)
1247 (case (combination-kind call)
1248 (:local
1249 (let ((fun (combination-lambda call)))
1250 (maybe-let-convert fun)
1251 (unless (member (functional-kind fun) '(:let :assignment :deleted))
1252 (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
1253 (:full
1254 (multiple-value-bind (leaf info)
1255 (let* ((uses (lvar-uses fun-lvar))
1256 (leaf (when (ref-p uses) (ref-leaf uses))))
1257 (validate-call-type call (lvar-type fun-lvar) leaf))
1258 (cond ((functional-p leaf)
1259 (convert-call-if-possible
1260 (lvar-uses (basic-combination-fun call))
1261 call))
1262 ((not leaf))
1263 ((and (global-var-p leaf)
1264 (eq (global-var-kind leaf) :global-function)
1265 (leaf-has-source-name-p leaf)
1266 (or (info :function :source-transform (leaf-source-name leaf))
1267 (and info
1268 (ir1-attributep (fun-info-attributes info)
1269 predicate)
1270 (let ((lvar (node-lvar call)))
1271 (and lvar (not (if-p (lvar-dest lvar))))))))
1272 (let ((name (leaf-source-name leaf))
1273 (dummies (make-gensym-list
1274 (length (combination-args call)))))
1275 (transform-call call
1276 `(lambda ,dummies
1277 (,@(if (symbolp name)
1278 `(,name)
1279 `(funcall #',name))
1280 ,@dummies))
1281 (leaf-source-name leaf)))))))))
1282 (values))
1284 ;;;; known function optimization
1286 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1287 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1288 ;;; replace it, otherwise add a new one.
1289 (defun record-optimization-failure (node transform args)
1290 (declare (type combination node) (type transform transform)
1291 (type (or fun-type list) args))
1292 (let* ((table (component-failed-optimizations *component-being-compiled*))
1293 (found (assoc transform (gethash node table))))
1294 (if found
1295 (setf (cdr found) args)
1296 (push (cons transform args) (gethash node table))))
1297 (values))
1299 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1300 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1301 ;;; doing the transform for some reason and FLAME is true, then we
1302 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1303 ;;; finalize to pick up. We return true if the transform failed, and
1304 ;;; thus further transformation should be attempted. We return false
1305 ;;; if either the transform succeeded or was aborted.
1306 (defun ir1-transform (node transform)
1307 (declare (type combination node) (type transform transform))
1308 (declare (notinline warn)) ; See COMPILER-WARN for rationale
1309 (let* ((type (transform-type transform))
1310 (fun (transform-function transform))
1311 (constrained (fun-type-p type))
1312 (table (component-failed-optimizations *component-being-compiled*))
1313 (flame (case (transform-important transform)
1314 ((t) (policy node (>= speed inhibit-warnings)))
1315 (:slightly (policy node (> speed inhibit-warnings)))))
1316 (*compiler-error-context* node)
1317 (policy-test (transform-policy transform)))
1318 (cond ((and policy-test
1319 (not (funcall policy-test node))))
1320 ((or (not constrained)
1321 (valid-fun-use node type))
1322 (multiple-value-bind (severity args)
1323 (catch 'give-up-ir1-transform
1324 (transform-call node
1325 (funcall fun node)
1326 (combination-fun-source-name node))
1327 (values :none nil))
1328 (ecase severity
1329 (:none
1330 (remhash node table)
1331 nil)
1332 (:aborted
1333 (setf (combination-kind node) :error)
1334 (when args
1335 (apply #'warn args))
1336 (remhash node table)
1337 nil)
1338 (:failure
1339 (if args
1340 (when flame
1341 (record-optimization-failure node transform args))
1342 (setf (gethash node table)
1343 (remove transform (gethash node table) :key #'car)))
1345 (:delayed
1346 (remhash node table)
1347 nil))))
1348 ((and flame
1349 (valid-fun-use node
1350 type
1351 :argument-test #'types-equal-or-intersect
1352 :result-test #'values-types-equal-or-intersect))
1353 (record-optimization-failure node transform type)
1356 t))))
1358 ;;; When we don't like an IR1 transform, we throw the severity/reason
1359 ;;; and args.
1361 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1362 ;;; aborting this attempt to transform the call, but admitting the
1363 ;;; possibility that this or some other transform will later succeed.
1364 ;;; If arguments are supplied, they are format arguments for an
1365 ;;; efficiency note.
1367 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1368 ;;; force a normal call to the function at run time. No further
1369 ;;; optimizations will be attempted.
1371 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1372 ;;; delay the transform on the node until later. REASONS specifies
1373 ;;; when the transform will be later retried. The :OPTIMIZE reason
1374 ;;; causes the transform to be delayed until after the current IR1
1375 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1376 ;;; be delayed until after constraint propagation.
1378 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1379 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1380 ;;; do CASE operations on the various REASON values, it might be a
1381 ;;; good idea to go OO, representing the reasons by objects, using
1382 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1383 ;;; SIGNAL instead of THROW.
1384 (declaim (ftype (function (&rest t) #+(and sb-xc-host ccl) *
1385 #-(and sb-xc-host ccl) nil) give-up-ir1-transform))
1386 (defun give-up-ir1-transform (&rest args)
1387 (throw 'give-up-ir1-transform (values :failure args)))
1388 (defun abort-ir1-transform (&rest args)
1389 (throw 'give-up-ir1-transform (values :aborted args)))
1390 (defun delay-ir1-transform (node &rest reasons)
1391 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1392 (cond ((not assoc)
1393 (setf *delayed-ir1-transforms*
1394 (acons node reasons *delayed-ir1-transforms*))
1395 (throw 'give-up-ir1-transform :delayed))
1396 ((cdr assoc)
1397 (dolist (reason reasons)
1398 (pushnew reason (cdr assoc)))
1399 (throw 'give-up-ir1-transform :delayed)))))
1401 ;;; Poor man's catching and resignalling
1402 ;;; Implicit %GIVE-UP macrolet will resignal the give-up "condition"
1403 (defmacro catch-give-up-ir1-transform ((form &optional args) &body gave-up-body)
1404 (let ((block (gensym "BLOCK"))
1405 (kind (gensym "KIND"))
1406 (args (or args (gensym "ARGS"))))
1407 `(block ,block
1408 (multiple-value-bind (,kind ,args)
1409 (catch 'give-up-ir1-transform
1410 (return-from ,block ,form))
1411 (ecase ,kind
1412 (:delayed
1413 (throw 'give-up-ir1-transform :delayed))
1414 ((:failure :aborted)
1415 (macrolet ((%give-up ()
1416 `(throw 'give-up-ir1-transform (values ,',kind
1417 ,',args))))
1418 ,@gave-up-body)))))))
1420 ;;; Clear any delayed transform with no reasons - these should have
1421 ;;; been tried in the last pass. Then remove the reason from the
1422 ;;; delayed transform reasons, and if any become empty then set
1423 ;;; reoptimize flags for the node. Return true if any transforms are
1424 ;;; to be retried.
1425 (defun retry-delayed-ir1-transforms (reason)
1426 (setf *delayed-ir1-transforms*
1427 (remove-if-not #'cdr *delayed-ir1-transforms*))
1428 (let ((reoptimize nil))
1429 (dolist (assoc *delayed-ir1-transforms*)
1430 (let ((reasons (remove reason (cdr assoc))))
1431 (setf (cdr assoc) reasons)
1432 (unless reasons
1433 (let ((node (car assoc)))
1434 (unless (node-deleted node)
1435 (setf reoptimize t)
1436 (setf (node-reoptimize node) t)
1437 (let ((block (node-block node)))
1438 (setf (block-reoptimize block) t)
1439 (reoptimize-component (block-component block) :maybe)))))))
1440 reoptimize))
1442 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1443 ;;; environment, and then install it as the function for the call
1444 ;;; NODE. We do local call analysis so that the new function is
1445 ;;; integrated into the control flow.
1447 ;;; We require the original function source name in order to generate
1448 ;;; a meaningful debug name for the lambda we set up. (It'd be
1449 ;;; possible to do this starting from debug names as well as source
1450 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1451 ;;; generality, since source names are always known to our callers.)
1452 (defun transform-call (call res source-name)
1453 (declare (type combination call) (list res))
1454 (aver (and (legal-fun-name-p source-name)
1455 (not (eql source-name '.anonymous.))))
1456 (node-ends-block call)
1457 ;; The internal variables of a transform are not going to be
1458 ;; interesting to the debugger, so there's no sense in
1459 ;; suppressing the substitution of variables with only one use
1460 ;; (the extra variables can slow down constraint propagation).
1462 ;; This needs to be done before the WITH-IR1-ENVIRONMENT-FROM-NODE,
1463 ;; so that it will bind *LEXENV* to the right environment.
1464 (setf (combination-lexenv call)
1465 (make-lexenv :default (combination-lexenv call)
1466 :policy (process-optimize-decl
1467 '(optimize
1468 (preserve-single-use-debug-variables 0))
1469 (lexenv-policy
1470 (combination-lexenv call)))))
1471 (with-ir1-environment-from-node call
1472 (with-component-last-block (*current-component*
1473 (block-next (node-block call)))
1475 (let ((new-fun (ir1-convert-inline-lambda
1477 :debug-name (debug-name 'lambda-inlined source-name)
1478 :system-lambda t))
1479 (ref (lvar-use (combination-fun call))))
1480 (change-ref-leaf ref new-fun)
1481 (setf (combination-kind call) :full)
1482 (locall-analyze-component *current-component*))))
1483 (values))
1485 (defun constant-fold-arg-p (name)
1486 (typecase name
1487 (null
1489 ((or symbol cons)
1490 (let* ((info (info :function :info name))
1491 (attributes (and info
1492 (fun-info-attributes info))))
1493 (and info
1494 (ir1-attributep attributes foldable)
1495 (not (ir1-attributep attributes call)))))))
1497 ;;; Return T if the function is foldable and if it's marked as CALL
1498 ;;; all function arguments are FOLDABLE too.
1499 (defun constant-fold-call-p (combination)
1500 (let* ((info (basic-combination-fun-info combination))
1501 (attr (fun-info-attributes info))
1502 (args (basic-combination-args combination)))
1503 (cond ((not (ir1-attributep attr foldable))
1504 nil)
1505 ((ir1-attributep attr call)
1506 (apply (fun-info-foldable-call-check info)
1507 (mapcar (lambda (lvar)
1508 (or (lvar-fun-name lvar t)
1509 (if (constant-lvar-p lvar)
1510 (lvar-value lvar)
1511 (return-from constant-fold-call-p nil))))
1512 args)))
1514 (every #'constant-lvar-p args)))))
1516 ;;; Replace a call to a foldable function of constant arguments with
1517 ;;; the result of evaluating the form. If there is an error during the
1518 ;;; evaluation, we give a warning and leave the call alone, making the
1519 ;;; call a :ERROR call.
1521 ;;; If there is more than one value, then we transform the call into a
1522 ;;; VALUES form.
1523 (defun constant-fold-call (call)
1524 (let ((args (mapcar (lambda (lvar)
1525 (let ((name (lvar-fun-name lvar t)))
1526 (if name
1527 (fdefinition name)
1528 (lvar-value lvar))))
1529 (combination-args call)))
1530 (fun-name (combination-fun-source-name call)))
1531 (multiple-value-bind (values win)
1532 (careful-call fun-name
1533 args
1534 call
1535 ;; Note: CMU CL had COMPILER-WARN here, and that
1536 ;; seems more natural, but it's probably not.
1538 ;; It's especially not while bug 173 exists:
1539 ;; Expressions like
1540 ;; (COND (END
1541 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1542 ;; ...))
1543 ;; can cause constant-folding TYPE-ERRORs (in
1544 ;; #'<=) when END can be proved to be NIL, even
1545 ;; though the code is perfectly legal and safe
1546 ;; because a NIL value of END means that the
1547 ;; #'<= will never be executed.
1549 ;; Moreover, even without bug 173,
1550 ;; quite-possibly-valid code like
1551 ;; (COND ((NONINLINED-PREDICATE END)
1552 ;; (UNLESS (<= END SIZE))
1553 ;; ...))
1554 ;; (where NONINLINED-PREDICATE is something the
1555 ;; compiler can't do at compile time, but which
1556 ;; turns out to make the #'<= expression
1557 ;; unreachable when END=NIL) could cause errors
1558 ;; when the compiler tries to constant-fold (<=
1559 ;; END SIZE).
1561 ;; So, with or without bug 173, it'd be
1562 ;; unnecessarily evil to do a full
1563 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1564 ;; from COMPILE-FILE) for legal code, so we we
1565 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1566 #-sb-xc-host #'compiler-style-warn
1567 ;; On the other hand, for code we control, we
1568 ;; should be able to work around any bug
1569 ;; 173-related problems, and in particular we
1570 ;; want to be alerted to calls to our own
1571 ;; functions which aren't being folded away; a
1572 ;; COMPILER-WARNING is butch enough to stop the
1573 ;; SBCL build itself in its tracks.
1574 #+sb-xc-host #'compiler-warn
1575 "constant folding")
1576 (cond ((not win)
1577 (setf (combination-kind call) :error))
1578 ((and (proper-list-of-length-p values 1))
1579 (with-ir1-environment-from-node call
1580 (let* ((lvar (node-lvar call))
1581 (prev (node-prev call))
1582 (intermediate-ctran (make-ctran)))
1583 (%delete-lvar-use call)
1584 (setf (ctran-next prev) nil)
1585 (setf (node-prev call) nil)
1586 (reference-constant prev intermediate-ctran lvar
1587 (first values))
1588 (link-node-to-previous-ctran call intermediate-ctran)
1589 (reoptimize-lvar lvar)
1590 (flush-combination call))))
1591 (t (let ((dummies (make-gensym-list (length args))))
1592 (transform-call
1593 call
1594 `(lambda ,dummies
1595 (declare (ignore ,@dummies))
1596 (values ,@(mapcar (lambda (x) `',x) values)))
1597 fun-name))))))
1598 (values))
1600 ;;;; local call optimization
1602 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1604 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1605 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1606 ;;; choke.)
1608 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1609 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1610 (defun propagate-to-refs (leaf type)
1611 (declare (type leaf leaf) (type ctype type))
1612 (let ((var-type (leaf-type leaf))
1613 (refs (leaf-refs leaf)))
1614 (unless (or (fun-type-p var-type)
1615 (and (cdr refs)
1616 (eq :declared (leaf-where-from leaf))
1617 (type-needs-conservation-p var-type)))
1618 (let ((int (type-approx-intersection2 var-type type)))
1619 (when (type/= int var-type)
1620 (setf (leaf-type leaf) int)
1621 (let ((s-int (make-single-value-type int)))
1622 (dolist (ref refs)
1623 (derive-node-type ref s-int)
1624 ;; KLUDGE: LET var substitution
1625 (let* ((lvar (node-lvar ref)))
1626 (when (and lvar (combination-p (lvar-dest lvar)))
1627 (reoptimize-lvar lvar)))))))
1628 (values))))
1630 ;;; Iteration variable: exactly one SETQ of the form:
1632 ;;; (let ((var initial))
1633 ;;; ...
1634 ;;; (setq var (+ var step))
1635 ;;; ...)
1636 (defun maybe-infer-iteration-var-type (var initial-type)
1637 (binding* ((sets (lambda-var-sets var) :exit-if-null)
1638 (set (first sets))
1639 (() (null (rest sets)) :exit-if-null)
1640 (set-use (principal-lvar-use (set-value set)))
1641 (() (and (combination-p set-use)
1642 (eq (combination-kind set-use) :known)
1643 (fun-info-p (combination-fun-info set-use))
1644 (not (node-to-be-deleted-p set-use))
1645 (or (eq (combination-fun-source-name set-use) '+)
1646 (eq (combination-fun-source-name set-use) '-)))
1647 :exit-if-null)
1648 (minusp (eq (combination-fun-source-name set-use) '-))
1649 (+-args (basic-combination-args set-use))
1650 (() (and (proper-list-of-length-p +-args 2 2)
1651 (let ((first (principal-lvar-use
1652 (first +-args))))
1653 (and (ref-p first)
1654 (eq (ref-leaf first) var))))
1655 :exit-if-null)
1656 (step-type (lvar-type (second +-args)))
1657 (set-type (lvar-type (set-value set))))
1658 (when (and (numeric-type-p initial-type)
1659 (numeric-type-p step-type)
1660 (or (numeric-type-equal initial-type step-type)
1661 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1662 ;; the initial and the step are of different types,
1663 ;; and the step is less contagious.
1664 (numeric-type-equal initial-type
1665 (numeric-contagion initial-type
1666 step-type))))
1667 (labels ((leftmost (x y cmp cmp=)
1668 (cond ((eq x nil) nil)
1669 ((eq y nil) nil)
1670 ((listp x)
1671 (let ((x1 (first x)))
1672 (cond ((listp y)
1673 (let ((y1 (first y)))
1674 (if (funcall cmp x1 y1) x y)))
1676 (if (funcall cmp x1 y) x y)))))
1677 ((listp y)
1678 (let ((y1 (first y)))
1679 (if (funcall cmp= x y1) x y)))
1680 (t (if (funcall cmp x y) x y))))
1681 (max* (x y) (leftmost x y #'> #'>=))
1682 (min* (x y) (leftmost x y #'< #'<=)))
1683 (multiple-value-bind (low high)
1684 (let ((step-type-non-negative (csubtypep step-type (specifier-type
1685 '(real 0 *))))
1686 (step-type-non-positive (csubtypep step-type (specifier-type
1687 '(real * 0)))))
1688 (cond ((or (and step-type-non-negative (not minusp))
1689 (and step-type-non-positive minusp))
1690 (values (numeric-type-low initial-type)
1691 (when (and (numeric-type-p set-type)
1692 (numeric-type-equal set-type initial-type))
1693 (max* (numeric-type-high initial-type)
1694 (numeric-type-high set-type)))))
1695 ((or (and step-type-non-positive (not minusp))
1696 (and step-type-non-negative minusp))
1697 (values (when (and (numeric-type-p set-type)
1698 (numeric-type-equal set-type initial-type))
1699 (min* (numeric-type-low initial-type)
1700 (numeric-type-low set-type)))
1701 (numeric-type-high initial-type)))
1703 (values nil nil))))
1704 (modified-numeric-type initial-type
1705 :low low
1706 :high high
1707 :enumerable nil))))))
1708 (deftransform + ((x y) * * :result result)
1709 "check for iteration variable reoptimization"
1710 (let ((dest (principal-lvar-end result))
1711 (use (principal-lvar-use x)))
1712 (when (and (ref-p use)
1713 (set-p dest)
1714 (eq (ref-leaf use)
1715 (set-var dest)))
1716 (reoptimize-lvar (set-value dest))))
1717 (give-up-ir1-transform))
1719 ;;; Figure out the type of a LET variable that has sets. We compute
1720 ;;; the union of the INITIAL-TYPE and the types of all the set
1721 ;;; values and to a PROPAGATE-TO-REFS with this type.
1722 (defun propagate-from-sets (var initial-type)
1723 (let ((changes (not (csubtypep (lambda-var-last-initial-type var) initial-type)))
1724 (types nil))
1725 (dolist (set (lambda-var-sets var))
1726 (let ((type (lvar-type (set-value set))))
1727 (push type types)
1728 (when (node-reoptimize set)
1729 (let ((old-type (node-derived-type set)))
1730 (unless (values-subtypep old-type type)
1731 (derive-node-type set (make-single-value-type type))
1732 (setf changes t)))
1733 (setf (node-reoptimize set) nil))))
1734 (when changes
1735 (setf (lambda-var-last-initial-type var) initial-type)
1736 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type)
1737 (apply #'type-union initial-type types))))
1738 (propagate-to-refs var res-type))))
1739 (values))
1741 ;;; If a LET variable, find the initial value's type and do
1742 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1743 ;;; type.
1744 (defun ir1-optimize-set (node)
1745 (declare (type cset node))
1746 (let ((var (set-var node)))
1747 (when (and (lambda-var-p var) (leaf-refs var))
1748 (let ((home (lambda-var-home var)))
1749 (when (eq (functional-kind home) :let)
1750 (let* ((initial-value (let-var-initial-value var))
1751 (initial-type (lvar-type initial-value)))
1752 (setf (lvar-reoptimize initial-value) nil)
1753 (propagate-from-sets var initial-type))))))
1754 (derive-node-type node (make-single-value-type
1755 (lvar-type (set-value node))))
1756 (setf (node-reoptimize node) nil)
1757 (values))
1759 ;;; Return true if the value of REF will always be the same (and is
1760 ;;; thus legal to substitute.)
1761 (defun constant-reference-p (ref)
1762 (declare (type ref ref))
1763 (let ((leaf (ref-leaf ref)))
1764 (typecase leaf
1765 ((or constant functional) t)
1766 (lambda-var
1767 (null (lambda-var-sets leaf)))
1768 (defined-fun
1769 (not (eq (defined-fun-inlinep leaf) :notinline)))
1770 (global-var
1771 (case (global-var-kind leaf)
1772 (:global-function
1773 (let ((name (leaf-source-name leaf)))
1774 (or #-sb-xc-host
1775 (eq (symbol-package (fun-name-block-name name))
1776 *cl-package*)
1777 (info :function :info name)))))))))
1779 ;;; If we have a non-set LET var with a single use, then (if possible)
1780 ;;; replace the variable reference's LVAR with the arg lvar.
1782 ;;; We change the REF to be a reference to NIL with unused value, and
1783 ;;; let it be flushed as dead code. A side effect of this substitution
1784 ;;; is to delete the variable.
1785 (defun substitute-single-use-lvar (arg var)
1786 (declare (type lvar arg) (type lambda-var var))
1787 (binding* ((ref (first (leaf-refs var)))
1788 (lvar (node-lvar ref) :exit-if-null)
1789 (dest (lvar-dest lvar))
1790 (dest-lvar (when (valued-node-p dest) (node-lvar dest))))
1791 (when (and
1792 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1793 ;; LVAR-USEs should not be met on one path. Another problem
1794 ;; is with dynamic-extent.
1795 (eq (lvar-uses lvar) ref)
1796 (not (block-delete-p (node-block ref)))
1797 ;; If the destinatation is dynamic extent, don't substitute unless
1798 ;; the source is as well.
1799 (or (not dest-lvar)
1800 (not (lvar-dynamic-extent dest-lvar))
1801 (lvar-dynamic-extent lvar))
1802 (typecase dest
1803 ;; we should not change lifetime of unknown values lvars
1804 (cast
1805 (and (type-single-value-p (lvar-derived-type arg))
1806 (multiple-value-bind (pdest pprev)
1807 (principal-lvar-end lvar)
1808 (declare (ignore pdest))
1809 (lvar-single-value-p pprev))
1810 ;; CASTs can disappear, don't substitute if
1811 ;; DEST-LVAR has other uses (this will be
1812 ;; insufficient if we have a CAST-CAST chain, but
1813 ;; works well for a single CAST)
1814 (or (null dest-lvar)
1815 (atom (lvar-uses dest-lvar)))))
1816 (mv-combination
1817 (or (eq (basic-combination-fun dest) lvar)
1818 (and (eq (basic-combination-kind dest) :local)
1819 (type-single-value-p (lvar-derived-type arg)))))
1820 ((or creturn exit)
1821 ;; While CRETURN and EXIT nodes may be known-values,
1822 ;; they have their own complications, such as
1823 ;; substitution into CRETURN may create new tail calls.
1824 nil)
1826 (aver (lvar-single-value-p lvar))
1828 (eq (node-home-lambda ref)
1829 (lambda-home (lambda-var-home var))))
1830 (let ((ref-type (single-value-type (node-derived-type ref))))
1831 (cond ((csubtypep (single-value-type (lvar-type arg)) ref-type)
1832 (substitute-lvar-uses lvar arg
1833 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1835 (delete-lvar-use ref))
1837 (let* ((value (make-lvar))
1838 (cast (insert-cast-before ref value ref-type
1839 ;; KLUDGE: it should be (TYPE-CHECK 0)
1840 *policy*)))
1841 (setf (cast-type-to-check cast) *wild-type*)
1842 (substitute-lvar-uses value arg
1843 ;; FIXME
1845 (%delete-lvar-use ref)
1846 (add-lvar-use cast lvar)))))
1847 (setf (node-derived-type ref) *wild-type*)
1848 (change-ref-leaf ref (find-constant nil))
1849 (delete-ref ref)
1850 (unlink-node ref)
1851 (reoptimize-lvar lvar)
1852 t)))
1854 ;;; Delete a LET, removing the call and bind nodes, and warning about
1855 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1856 ;;; along right away and delete the REF and then the lambda, since we
1857 ;;; flush the FUN lvar.
1858 (defun delete-let (clambda)
1859 (declare (type clambda clambda))
1860 (aver (functional-letlike-p clambda))
1861 (note-unreferenced-fun-vars clambda)
1862 (let ((call (let-combination clambda)))
1863 (flush-dest (basic-combination-fun call))
1864 (unlink-node call)
1865 (unlink-node (lambda-bind clambda))
1866 (setf (lambda-bind clambda) nil))
1867 (setf (functional-kind clambda) :zombie)
1868 (let ((home (lambda-home clambda)))
1869 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1870 (values))
1872 ;;; This function is called when one of the arguments to a LET
1873 ;;; changes. We look at each changed argument. If the corresponding
1874 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1875 ;;; consider substituting for the variable, and also propagate
1876 ;;; derived-type information for the arg to all the VAR's refs.
1878 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1879 ;;; subtype of the argument's leaf type. This prevents type checking
1880 ;;; from being defeated, and also ensures that the best representation
1881 ;;; for the variable can be used.
1883 ;;; Substitution of individual references is inhibited if the
1884 ;;; reference is in a different component from the home. This can only
1885 ;;; happen with closures over top level lambda vars. In such cases,
1886 ;;; the references may have already been compiled, and thus can't be
1887 ;;; retroactively modified.
1889 ;;; If all of the variables are deleted (have no references) when we
1890 ;;; are done, then we delete the LET.
1892 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1893 ;;; flags.
1894 (defun propagate-let-args (call fun)
1895 (declare (type combination call) (type clambda fun))
1896 (loop for arg in (combination-args call)
1897 and var in (lambda-vars fun) do
1898 (when (and arg (lvar-reoptimize arg))
1899 (setf (lvar-reoptimize arg) nil)
1900 (cond
1901 ((lambda-var-sets var)
1902 (propagate-from-sets var (lvar-type arg)))
1903 ((let ((use (lvar-uses arg)))
1904 (when (ref-p use)
1905 (let ((leaf (ref-leaf use)))
1906 (when (and (constant-reference-p use)
1907 (csubtypep (leaf-type leaf)
1908 ;; (NODE-DERIVED-TYPE USE) would
1909 ;; be better -- APD, 2003-05-15
1910 (leaf-type var)))
1911 (propagate-to-refs var (lvar-type arg))
1912 (let ((use-component (node-component use)))
1913 (prog1 (substitute-leaf-if
1914 (lambda (ref)
1915 (cond ((eq (node-component ref) use-component)
1918 (aver (lambda-toplevelish-p (lambda-home fun)))
1919 nil)))
1920 leaf var)))
1921 t)))))
1922 ((and (null (rest (leaf-refs var)))
1923 (not (preserve-single-use-debug-var-p call var))
1924 (substitute-single-use-lvar arg var)))
1926 (propagate-to-refs var (lvar-type arg))))))
1928 (when (every #'not (combination-args call))
1929 (delete-let fun))
1931 (values))
1933 ;;; This function is called when one of the args to a non-LET local
1934 ;;; call changes. For each changed argument corresponding to an unset
1935 ;;; variable, we compute the union of the types across all calls and
1936 ;;; propagate this type information to the var's refs.
1938 ;;; If the function has an entry-fun, then we don't do anything: since
1939 ;;; it has a XEP we would not discover anything.
1941 ;;; If the function is an optional-entry-point, we will just make sure
1942 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1943 ;;; can erronously propagate too strict types into refs: see
1944 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1946 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1947 ;;; corresponding to changed arguments in CALL, since the only use in
1948 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1949 ;;; right here.
1950 (defun propagate-local-call-args (call fun)
1951 (declare (type combination call) (type clambda fun))
1952 (unless (functional-entry-fun fun)
1953 (if (lambda-optional-dispatch fun)
1954 ;; We can still make sure &REST is known to be a list.
1955 (loop for var in (lambda-vars fun)
1956 do (let ((info (lambda-var-arg-info var)))
1957 (when (and info (eq :rest (arg-info-kind info)))
1958 (propagate-from-sets var (specifier-type 'list)))))
1959 ;; The normal case.
1960 (let* ((vars (lambda-vars fun))
1961 (union (mapcar (lambda (arg var)
1962 (when (and arg
1963 (lvar-reoptimize arg)
1964 (null (basic-var-sets var)))
1965 (lvar-type arg)))
1966 (basic-combination-args call)
1967 vars))
1968 (this-ref (lvar-use (basic-combination-fun call))))
1970 (dolist (arg (basic-combination-args call))
1971 (when arg
1972 (setf (lvar-reoptimize arg) nil)))
1974 (dolist (ref (leaf-refs fun))
1975 (let ((dest (node-dest ref)))
1976 (unless (or (eq ref this-ref) (not dest))
1977 (setq union
1978 (mapcar (lambda (this-arg old)
1979 (when old
1980 (setf (lvar-reoptimize this-arg) nil)
1981 (type-union (lvar-type this-arg) old)))
1982 (basic-combination-args dest)
1983 union)))))
1985 (loop for var in vars
1986 and type in union
1987 when type do (propagate-to-refs var type)))))
1989 (values))
1991 ;;;; multiple values optimization
1993 ;;; Do stuff to notice a change to a MV combination node. There are
1994 ;;; two main branches here:
1995 ;;; -- If the call is local, then it is already a MV let, or should
1996 ;;; become one. Note that although all :LOCAL MV calls must eventually
1997 ;;; be converted to :MV-LETs, there can be a window when the call
1998 ;;; is local, but has not been LET converted yet. This is because
1999 ;;; the entry-point lambdas may have stray references (in other
2000 ;;; entry points) that have not been deleted yet.
2001 ;;; -- The call is full. This case is somewhat similar to the non-MV
2002 ;;; combination optimization: we propagate return type information and
2003 ;;; notice non-returning calls. We also have an optimization
2004 ;;; which tries to convert MV-CALLs into MV-binds.
2005 (defun ir1-optimize-mv-combination (node)
2006 (let ((fun (basic-combination-fun node)))
2007 (unless (and (node-p (lvar-uses fun))
2008 (node-to-be-deleted-p (lvar-uses fun)))
2009 (ecase (basic-combination-kind node)
2010 (:local
2011 (when (lvar-reoptimize fun)
2012 (setf (lvar-reoptimize fun) nil)
2013 (maybe-let-convert (combination-lambda node)))
2014 (loop for arg in (basic-combination-args node)
2016 (setf (lvar-reoptimize arg) nil))
2017 (when (eq (functional-kind (combination-lambda node)) :mv-let)
2018 (unless (convert-mv-bind-to-let node)
2019 (ir1-optimize-mv-bind node))))
2020 (:full
2021 (let* ((fun-changed (lvar-reoptimize fun))
2022 (args (basic-combination-args node)))
2023 (when fun-changed
2024 (setf (lvar-reoptimize fun) nil)
2025 (let ((type (lvar-type fun)))
2026 (when (fun-type-p type)
2027 (derive-node-type node (fun-type-returns type))))
2028 (maybe-terminate-block node nil)
2029 (let ((use (lvar-uses fun)))
2030 (when (and (ref-p use) (functional-p (ref-leaf use)))
2031 (convert-call-if-possible use node)
2032 (when (eq (basic-combination-kind node) :local)
2033 (maybe-let-convert (ref-leaf use))))))
2034 (unless (or (eq (basic-combination-kind node) :local)
2035 (eq (lvar-fun-name fun) '%throw))
2036 (ir1-optimize-mv-call node))
2037 (dolist (arg args)
2038 (setf (lvar-reoptimize arg) nil))))
2039 (:error))))
2040 (values))
2042 ;;; Propagate derived type info from the values lvar to the vars.
2043 (defun ir1-optimize-mv-bind (node)
2044 (declare (type mv-combination node))
2045 (let* ((args (basic-combination-args node))
2046 (vars (lambda-vars (combination-lambda node)))
2047 (types (if (singleton-p args)
2048 (values-type-in (lvar-derived-type (first args))
2049 (length vars))
2050 (loop for arg in args
2051 append (values-types (lvar-derived-type arg))))))
2052 (loop for var in vars
2053 and type in types
2054 do (if (basic-var-sets var)
2055 (propagate-from-sets var type)
2056 (propagate-to-refs var type)))
2057 (loop for arg in args
2059 (setf (lvar-reoptimize arg) nil)))
2060 (values))
2062 (defun ir1-optimize-mv-call (node)
2063 (let ((fun (basic-combination-fun node))
2064 (*compiler-error-context* node)
2065 (ref (lvar-uses (basic-combination-fun node)))
2066 (args (basic-combination-args node)))
2067 (when (ref-p ref)
2068 (multiple-value-bind (min max) (fun-type-nargs (lvar-type fun))
2069 (let ((total-nvals
2070 (loop for arg in args
2071 for nvals = (nth-value 1 (values-types (lvar-derived-type arg)))
2072 when (eq nvals :unknown) return nil
2073 sum nvals)))
2074 (when total-nvals
2075 (when (and min (< total-nvals min))
2076 (compiler-warn
2077 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2078 at least ~R."
2079 total-nvals min)
2080 (setf (basic-combination-kind node) :error)
2081 (return-from ir1-optimize-mv-call))
2082 (when (and max (> total-nvals max))
2083 (compiler-warn
2084 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2085 at most ~R."
2086 total-nvals max)
2087 (setf (basic-combination-kind node) :error)
2088 (return-from ir1-optimize-mv-call)))
2089 (let ((count (cond (total-nvals)
2090 ((and (policy node (zerop verify-arg-count))
2091 (eql min max))
2092 min)
2093 (t nil))))
2094 (when count
2095 (with-ir1-environment-from-node node
2096 (let* ((dums (make-gensym-list count))
2097 (ignore (gensym))
2098 (leaf (ref-leaf ref))
2099 (fun (ir1-convert-lambda
2100 `(lambda (&optional ,@dums &rest ,ignore)
2101 (declare (ignore ,ignore))
2102 (%funcall ,leaf ,@dums))
2103 :debug-name (leaf-%debug-name leaf))))
2104 (change-ref-leaf ref fun)
2105 (aver (eq (basic-combination-kind node) :full))
2106 (locall-analyze-component *current-component*)
2107 (aver (eq (basic-combination-kind node) :local))))))))))
2108 (values))
2110 ;;; If we see:
2111 ;;; (multiple-value-bind
2112 ;;; (x y)
2113 ;;; (values xx yy)
2114 ;;; ...)
2115 ;;; Convert to:
2116 ;;; (let ((x xx)
2117 ;;; (y yy))
2118 ;;; ...)
2120 ;;; What we actually do is convert the VALUES combination into a
2121 ;;; normal LET combination calling the original :MV-LET lambda. If
2122 ;;; there are extra args to VALUES, discard the corresponding
2123 ;;; lvars. If there are insufficient args, insert references to NIL.
2124 (defun convert-mv-bind-to-let (call)
2125 (declare (type mv-combination call))
2126 (let* ((args (basic-combination-args call))
2127 (use (lvar-uses (first args))))
2128 (when (and (singleton-p args)
2129 (combination-p use)
2130 (eq (lvar-fun-name (combination-fun use))
2131 'values))
2132 (let* ((fun (combination-lambda call))
2133 (vars (lambda-vars fun))
2134 (vals (combination-args use))
2135 (nvars (length vars))
2136 (nvals (length vals)))
2137 (cond ((> nvals nvars)
2138 (mapc #'flush-dest (subseq vals nvars))
2139 (setq vals (subseq vals 0 nvars)))
2140 ((< nvals nvars)
2141 (with-ir1-environment-from-node use
2142 (let ((node-prev (node-prev use)))
2143 (setf (node-prev use) nil)
2144 (setf (ctran-next node-prev) nil)
2145 (collect ((res vals))
2146 (loop for count below (- nvars nvals)
2147 for prev = node-prev then ctran
2148 for ctran = (make-ctran)
2149 and lvar = (make-lvar use)
2150 do (reference-constant prev ctran lvar nil)
2151 (res lvar)
2152 finally (link-node-to-previous-ctran
2153 use ctran))
2154 (setq vals (res)))))))
2155 (setf (combination-args use) vals)
2156 (flush-dest (combination-fun use))
2157 (let ((fun-lvar (basic-combination-fun call)))
2158 (setf (lvar-dest fun-lvar) use)
2159 (setf (combination-fun use) fun-lvar)
2160 (flush-lvar-externally-checkable-type fun-lvar))
2161 (setf (combination-kind use) :local)
2162 (setf (functional-kind fun) :let)
2163 (flush-dest (first (basic-combination-args call)))
2164 (unlink-node call)
2165 (when vals
2166 (reoptimize-lvar (first vals)))
2167 ;; Propagate derived types from the VALUES call to its args:
2168 ;; transforms can leave the VALUES call with a better type
2169 ;; than its args have, so make sure not to throw that away.
2170 (let ((types (values-type-types (node-derived-type use))))
2171 (dolist (val vals)
2172 (when types
2173 (let ((type (pop types)))
2174 (assert-lvar-type val type **zero-typecheck-policy**)))))
2175 ;; Propagate declared types of MV-BIND variables.
2176 (propagate-to-args use fun)
2177 (reoptimize-call use))
2178 t)))
2180 ;;; If we see:
2181 ;;; (values-list (list x y z))
2183 ;;; Convert to:
2184 ;;; (values x y z)
2186 ;;; In implementation, this is somewhat similar to
2187 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2188 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2189 ;;; (allowing the LIST to be flushed.)
2191 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2192 (defoptimizer (values-list optimizer) ((list) node)
2193 (let ((use (lvar-uses list)))
2194 (when (and (combination-p use)
2195 (eq (lvar-fun-name (combination-fun use))
2196 'list))
2198 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2199 (change-ref-leaf (lvar-uses (combination-fun node))
2200 (find-free-fun 'values "in a strange place"))
2201 (setf (combination-kind node) :full)
2202 (let ((args (combination-args use)))
2203 (dolist (arg args)
2204 (setf (lvar-dest arg) node)
2205 (flush-lvar-externally-checkable-type arg))
2206 (setf (combination-args use) nil)
2207 (flush-dest list)
2208 (flush-combination use)
2209 (setf (combination-args node) args))
2210 t)))
2212 ;;; If VALUES appears in a non-MV context, then effectively convert it
2213 ;;; to a PROG1. This allows the computation of the additional values
2214 ;;; to become dead code.
2215 (deftransform values ((&rest vals) * * :node node)
2216 (unless (lvar-single-value-p (node-lvar node))
2217 (give-up-ir1-transform))
2218 (setf (node-derived-type node)
2219 (make-short-values-type (list (single-value-type
2220 (node-derived-type node)))))
2221 (principal-lvar-single-valuify (node-lvar node))
2222 (if vals
2223 (let ((dummies (make-gensym-list (length (cdr vals)))))
2224 `(lambda (val ,@dummies)
2225 (declare (ignore ,@dummies))
2226 val))
2227 nil))
2229 ;;; TODO:
2230 ;;; - CAST chains;
2231 (defun delete-cast (cast)
2232 (declare (type cast cast))
2233 (let ((value (cast-value cast))
2234 (lvar (cast-lvar cast)))
2235 (when (and (bound-cast-p cast)
2236 (bound-cast-check cast))
2237 (flush-combination (bound-cast-check cast))
2238 (setf (bound-cast-check cast) nil))
2239 (delete-filter cast lvar value)
2240 (when lvar
2241 (reoptimize-lvar lvar)
2242 (when (lvar-single-value-p lvar)
2243 (note-single-valuified-lvar lvar)))
2244 (values)))
2246 (defun may-delete-vestigial-exit (cast)
2247 (let ((exit-lexenv (cast-vestigial-exit-lexenv cast)))
2248 (when exit-lexenv
2249 ;; Vestigial exits are only introduced when eliminating a local
2250 ;; RETURN-FROM. We may delete them only when we can show that
2251 ;; there are no other code paths that use the entry LVAR that
2252 ;; are live from within the block that contained the deleted
2253 ;; EXIT and that all uses of the entry LVAR have the same
2254 ;; dynamic-extent environment. The conservative version of this
2255 ;; is that there are no EXITs for any ENTRY introduced between
2256 ;; the LEXENV of the deleted EXIT and the LEXENV of the target
2257 ;; ENTRY and that there is no :DYNAMIC-EXTENT cleanup between
2258 ;; the deleted EXIT and the target ENTRY (which the CAST shares
2259 ;; a lexenv with). See ONLY-HARMLESS-CLEANUPS in locall.lisp
2260 ;; for a similar analysis.
2261 (let ((entry-cleanup (block-start-cleanup (node-block cast))))
2262 (do-nested-cleanups (cleanup exit-lexenv)
2263 (when (eq cleanup entry-cleanup)
2264 (return))
2265 (case (cleanup-kind cleanup)
2266 ((:block :tagbody)
2267 (when (entry-exits (cleanup-mess-up cleanup))
2268 (return-from may-delete-vestigial-exit nil)))
2269 ((:dynamic-extent)
2270 (return-from may-delete-vestigial-exit nil)))))))
2271 (values t))
2273 (defun compile-time-type-error-context (context)
2274 #+sb-xc-host context
2275 #-sb-xc-host (source-to-string context))
2277 (defun ir1-optimize-cast (cast &optional do-not-optimize)
2278 (declare (type cast cast))
2279 (let ((value (cast-value cast))
2280 (atype (cast-asserted-type cast)))
2281 (unless (or do-not-optimize
2282 (not (may-delete-vestigial-exit cast)))
2283 (when (and (bound-cast-p cast)
2284 (bound-cast-check cast)
2285 (constant-lvar-p (bound-cast-bound cast)))
2286 (setf atype
2287 (specifier-type `(integer 0 (,(lvar-value (bound-cast-bound cast)))))
2288 (cast-asserted-type cast) atype
2289 (bound-cast-derived cast) t))
2290 (let ((lvar (node-lvar cast)))
2291 (when (and (or (not (bound-cast-p cast))
2292 (bound-cast-derived cast))
2293 (values-subtypep (lvar-derived-type value)
2294 (cast-asserted-type cast)))
2295 (when (function-designator-cast-p cast)
2296 (let ((*valid-fun-use-name* (function-designator-cast-caller cast))
2297 (*lossage-fun* #'compiler-warn)
2298 (*compiler-error-context* cast))
2299 (valid-callable-argument lvar
2300 (function-designator-cast-arg-count cast))))
2302 (delete-cast cast)
2303 (return-from ir1-optimize-cast t))
2305 (when (and (listp (lvar-uses value))
2306 lvar)
2307 ;; Pathwise removing of CAST
2308 (let ((ctran (node-next cast))
2309 (dest (lvar-dest lvar))
2310 next-block)
2311 (collect ((merges))
2312 (do-uses (use value)
2313 (when (and (values-subtypep (node-derived-type use) atype)
2314 (immediately-used-p value use))
2315 (unless next-block
2316 (when ctran (ensure-block-start ctran))
2317 (setq next-block (first (block-succ (node-block cast))))
2318 (ensure-block-start (node-prev cast))
2319 (reoptimize-lvar lvar)
2320 (setf (lvar-%derived-type value) nil))
2321 (%delete-lvar-use use)
2322 (add-lvar-use use lvar)
2323 (unlink-blocks (node-block use) (node-block cast))
2324 (link-blocks (node-block use) next-block)
2325 (when (and (return-p dest)
2326 (basic-combination-p use)
2327 (eq (basic-combination-kind use) :local))
2328 (merges use))))
2329 (dolist (use (merges))
2330 (merge-tail-sets use))))))
2332 (when (and (bound-cast-p cast)
2333 (bound-cast-check cast)
2334 (policy cast (= insert-array-bounds-checks 0)))
2335 (flush-combination (bound-cast-check cast))
2336 (setf (bound-cast-check cast) nil)))
2338 (let* ((value-type (lvar-derived-type value))
2339 (int (values-type-intersection value-type atype)))
2340 (derive-node-type cast int)
2341 (cond ((or
2342 (neq int *empty-type*)
2343 (eq value-type *empty-type*)))
2344 ;; No need to transform into an analog of
2345 ;; %COMPILE-TIME-TYPE-ERROR, %CHECK-BOUND will signal at
2346 ;; run-time and %CHECK-BOUND ir2-converter will signal at
2347 ;; compile-time if it survives further stages of ir1
2348 ;; optimization.
2349 ((bound-cast-p cast))
2351 ;; FIXME: Do it in one step.
2352 (let ((context (node-source-form cast))
2353 (detail (lvar-all-sources (cast-value cast))))
2354 (filter-lvar
2355 value
2356 (if (cast-single-value-p cast)
2357 `(list 'dummy)
2358 `(multiple-value-call #'list 'dummy)))
2359 (filter-lvar
2360 (cast-value cast)
2361 ;; FIXME: Derived type.
2362 `(%compile-time-type-error 'dummy
2363 ',(type-specifier atype)
2364 ',(type-specifier value-type)
2365 ',detail
2366 ',(compile-time-type-error-context context))))
2367 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2368 ;; functions, so we declare the return type of
2369 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2370 ;; here.
2371 (setq value (cast-value cast))
2372 (derive-node-type (lvar-uses value) *empty-type*)
2373 (maybe-terminate-block (lvar-uses value) nil)
2374 ;; FIXME: Is it necessary?
2375 (aver (null (block-pred (node-block cast))))
2376 (delete-block-lazily (node-block cast))
2377 (return-from ir1-optimize-cast)))
2378 (when (eq (node-derived-type cast) *empty-type*)
2379 (maybe-terminate-block cast nil))
2381 (when (and (cast-%type-check cast)
2382 (values-subtypep value-type
2383 (cast-type-to-check cast)))
2384 (setf (cast-%type-check cast) nil))))
2386 (unless do-not-optimize
2387 (setf (node-reoptimize cast) nil)))