Provide context for type errors during variable binding.
[sbcl.git] / src / compiler / ir1opt.lisp
blob132b3da0f94ef0a373ae141d8c7d320be5efd2a7
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 (let* ((type (transform-type transform))
1309 (fun (transform-function transform))
1310 (constrained (fun-type-p type))
1311 (table (component-failed-optimizations *component-being-compiled*))
1312 (flame (case (transform-important transform)
1313 ((t) (policy node (>= speed inhibit-warnings)))
1314 (:slightly (policy node (> speed inhibit-warnings)))))
1315 (*compiler-error-context* node)
1316 (policy-test (transform-policy transform)))
1317 (cond ((and policy-test
1318 (not (funcall policy-test node))))
1319 ((or (not constrained)
1320 (valid-fun-use node type))
1321 (multiple-value-bind (severity args)
1322 (catch 'give-up-ir1-transform
1323 (transform-call node
1324 (funcall fun node)
1325 (combination-fun-source-name node))
1326 (values :none nil))
1327 (ecase severity
1328 (:none
1329 (remhash node table)
1330 nil)
1331 (:aborted
1332 (setf (combination-kind node) :error)
1333 (when args
1334 (apply #'warn args))
1335 (remhash node table)
1336 nil)
1337 (:failure
1338 (if args
1339 (when flame
1340 (record-optimization-failure node transform args))
1341 (setf (gethash node table)
1342 (remove transform (gethash node table) :key #'car)))
1344 (:delayed
1345 (remhash node table)
1346 nil))))
1347 ((and flame
1348 (valid-fun-use node
1349 type
1350 :argument-test #'types-equal-or-intersect
1351 :result-test #'values-types-equal-or-intersect))
1352 (record-optimization-failure node transform type)
1355 t))))
1357 ;;; When we don't like an IR1 transform, we throw the severity/reason
1358 ;;; and args.
1360 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1361 ;;; aborting this attempt to transform the call, but admitting the
1362 ;;; possibility that this or some other transform will later succeed.
1363 ;;; If arguments are supplied, they are format arguments for an
1364 ;;; efficiency note.
1366 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1367 ;;; force a normal call to the function at run time. No further
1368 ;;; optimizations will be attempted.
1370 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1371 ;;; delay the transform on the node until later. REASONS specifies
1372 ;;; when the transform will be later retried. The :OPTIMIZE reason
1373 ;;; causes the transform to be delayed until after the current IR1
1374 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1375 ;;; be delayed until after constraint propagation.
1377 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1378 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1379 ;;; do CASE operations on the various REASON values, it might be a
1380 ;;; good idea to go OO, representing the reasons by objects, using
1381 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1382 ;;; SIGNAL instead of THROW.
1383 (declaim (ftype (function (&rest t) #+(and sb-xc-host ccl) *
1384 #-(and sb-xc-host ccl) nil) give-up-ir1-transform))
1385 (defun give-up-ir1-transform (&rest args)
1386 (throw 'give-up-ir1-transform (values :failure args)))
1387 (defun abort-ir1-transform (&rest args)
1388 (throw 'give-up-ir1-transform (values :aborted args)))
1389 (defun delay-ir1-transform (node &rest reasons)
1390 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1391 (cond ((not assoc)
1392 (setf *delayed-ir1-transforms*
1393 (acons node reasons *delayed-ir1-transforms*))
1394 (throw 'give-up-ir1-transform :delayed))
1395 ((cdr assoc)
1396 (dolist (reason reasons)
1397 (pushnew reason (cdr assoc)))
1398 (throw 'give-up-ir1-transform :delayed)))))
1400 ;;; Poor man's catching and resignalling
1401 ;;; Implicit %GIVE-UP macrolet will resignal the give-up "condition"
1402 (defmacro catch-give-up-ir1-transform ((form &optional args) &body gave-up-body)
1403 (let ((block (gensym "BLOCK"))
1404 (kind (gensym "KIND"))
1405 (args (or args (gensym "ARGS"))))
1406 `(block ,block
1407 (multiple-value-bind (,kind ,args)
1408 (catch 'give-up-ir1-transform
1409 (return-from ,block ,form))
1410 (ecase ,kind
1411 (:delayed
1412 (throw 'give-up-ir1-transform :delayed))
1413 ((:failure :aborted)
1414 (macrolet ((%give-up ()
1415 `(throw 'give-up-ir1-transform (values ,',kind
1416 ,',args))))
1417 ,@gave-up-body)))))))
1419 ;;; Clear any delayed transform with no reasons - these should have
1420 ;;; been tried in the last pass. Then remove the reason from the
1421 ;;; delayed transform reasons, and if any become empty then set
1422 ;;; reoptimize flags for the node. Return true if any transforms are
1423 ;;; to be retried.
1424 (defun retry-delayed-ir1-transforms (reason)
1425 (setf *delayed-ir1-transforms*
1426 (remove-if-not #'cdr *delayed-ir1-transforms*))
1427 (let ((reoptimize nil))
1428 (dolist (assoc *delayed-ir1-transforms*)
1429 (let ((reasons (remove reason (cdr assoc))))
1430 (setf (cdr assoc) reasons)
1431 (unless reasons
1432 (let ((node (car assoc)))
1433 (unless (node-deleted node)
1434 (setf reoptimize t)
1435 (setf (node-reoptimize node) t)
1436 (let ((block (node-block node)))
1437 (setf (block-reoptimize block) t)
1438 (reoptimize-component (block-component block) :maybe)))))))
1439 reoptimize))
1441 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1442 ;;; environment, and then install it as the function for the call
1443 ;;; NODE. We do local call analysis so that the new function is
1444 ;;; integrated into the control flow.
1446 ;;; We require the original function source name in order to generate
1447 ;;; a meaningful debug name for the lambda we set up. (It'd be
1448 ;;; possible to do this starting from debug names as well as source
1449 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1450 ;;; generality, since source names are always known to our callers.)
1451 (defun transform-call (call res source-name)
1452 (declare (type combination call) (list res))
1453 (aver (and (legal-fun-name-p source-name)
1454 (not (eql source-name '.anonymous.))))
1455 (node-ends-block call)
1456 ;; The internal variables of a transform are not going to be
1457 ;; interesting to the debugger, so there's no sense in
1458 ;; suppressing the substitution of variables with only one use
1459 ;; (the extra variables can slow down constraint propagation).
1461 ;; This needs to be done before the WITH-IR1-ENVIRONMENT-FROM-NODE,
1462 ;; so that it will bind *LEXENV* to the right environment.
1463 (setf (combination-lexenv call)
1464 (make-lexenv :default (combination-lexenv call)
1465 :policy (process-optimize-decl
1466 '(optimize
1467 (preserve-single-use-debug-variables 0))
1468 (lexenv-policy
1469 (combination-lexenv call)))))
1470 (with-ir1-environment-from-node call
1471 (with-component-last-block (*current-component*
1472 (block-next (node-block call)))
1474 (let ((new-fun (ir1-convert-inline-lambda
1476 :debug-name (debug-name 'lambda-inlined source-name)
1477 :system-lambda t))
1478 (ref (lvar-use (combination-fun call))))
1479 (change-ref-leaf ref new-fun)
1480 (setf (combination-kind call) :full)
1481 (locall-analyze-component *current-component*))))
1482 (values))
1484 (defun constant-fold-arg-p (name)
1485 (typecase name
1486 (null
1488 ((or symbol cons)
1489 (let* ((info (info :function :info name))
1490 (attributes (and info
1491 (fun-info-attributes info))))
1492 (and info
1493 (ir1-attributep attributes foldable)
1494 (not (ir1-attributep attributes call)))))))
1496 ;;; Return T if the function is foldable and if it's marked as CALL
1497 ;;; all function arguments are FOLDABLE too.
1498 (defun constant-fold-call-p (combination)
1499 (let* ((info (basic-combination-fun-info combination))
1500 (attr (fun-info-attributes info))
1501 (args (basic-combination-args combination)))
1502 (cond ((not (ir1-attributep attr foldable))
1503 nil)
1504 ((ir1-attributep attr call)
1505 (apply (fun-info-foldable-call-check info)
1506 (mapcar (lambda (lvar)
1507 (or (lvar-fun-name lvar t)
1508 (if (constant-lvar-p lvar)
1509 (lvar-value lvar)
1510 (return-from constant-fold-call-p nil))))
1511 args)))
1513 (every #'constant-lvar-p args)))))
1515 ;;; Replace a call to a foldable function of constant arguments with
1516 ;;; the result of evaluating the form. If there is an error during the
1517 ;;; evaluation, we give a warning and leave the call alone, making the
1518 ;;; call a :ERROR call.
1520 ;;; If there is more than one value, then we transform the call into a
1521 ;;; VALUES form.
1522 (defun constant-fold-call (call)
1523 (let ((args (mapcar (lambda (lvar)
1524 (let ((name (lvar-fun-name lvar t)))
1525 (if name
1526 (fdefinition name)
1527 (lvar-value lvar))))
1528 (combination-args call)))
1529 (fun-name (combination-fun-source-name call)))
1530 (multiple-value-bind (values win)
1531 (careful-call fun-name
1532 args
1533 call
1534 ;; Note: CMU CL had COMPILER-WARN here, and that
1535 ;; seems more natural, but it's probably not.
1537 ;; It's especially not while bug 173 exists:
1538 ;; Expressions like
1539 ;; (COND (END
1540 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1541 ;; ...))
1542 ;; can cause constant-folding TYPE-ERRORs (in
1543 ;; #'<=) when END can be proved to be NIL, even
1544 ;; though the code is perfectly legal and safe
1545 ;; because a NIL value of END means that the
1546 ;; #'<= will never be executed.
1548 ;; Moreover, even without bug 173,
1549 ;; quite-possibly-valid code like
1550 ;; (COND ((NONINLINED-PREDICATE END)
1551 ;; (UNLESS (<= END SIZE))
1552 ;; ...))
1553 ;; (where NONINLINED-PREDICATE is something the
1554 ;; compiler can't do at compile time, but which
1555 ;; turns out to make the #'<= expression
1556 ;; unreachable when END=NIL) could cause errors
1557 ;; when the compiler tries to constant-fold (<=
1558 ;; END SIZE).
1560 ;; So, with or without bug 173, it'd be
1561 ;; unnecessarily evil to do a full
1562 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1563 ;; from COMPILE-FILE) for legal code, so we we
1564 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1565 #-sb-xc-host #'compiler-style-warn
1566 ;; On the other hand, for code we control, we
1567 ;; should be able to work around any bug
1568 ;; 173-related problems, and in particular we
1569 ;; want to be alerted to calls to our own
1570 ;; functions which aren't being folded away; a
1571 ;; COMPILER-WARNING is butch enough to stop the
1572 ;; SBCL build itself in its tracks.
1573 #+sb-xc-host #'compiler-warn
1574 "constant folding")
1575 (cond ((not win)
1576 (setf (combination-kind call) :error))
1577 ((and (proper-list-of-length-p values 1))
1578 (with-ir1-environment-from-node call
1579 (let* ((lvar (node-lvar call))
1580 (prev (node-prev call))
1581 (intermediate-ctran (make-ctran)))
1582 (%delete-lvar-use call)
1583 (setf (ctran-next prev) nil)
1584 (setf (node-prev call) nil)
1585 (reference-constant prev intermediate-ctran lvar
1586 (first values))
1587 (link-node-to-previous-ctran call intermediate-ctran)
1588 (reoptimize-lvar lvar)
1589 (flush-combination call))))
1590 (t (let ((dummies (make-gensym-list (length args))))
1591 (transform-call
1592 call
1593 `(lambda ,dummies
1594 (declare (ignore ,@dummies))
1595 (values ,@(mapcar (lambda (x) `',x) values)))
1596 fun-name))))))
1597 (values))
1599 ;;;; local call optimization
1601 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1603 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1604 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1605 ;;; choke.)
1607 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1608 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1609 (defun propagate-to-refs (leaf type)
1610 (declare (type leaf leaf) (type ctype type))
1611 (let ((var-type (leaf-type leaf))
1612 (refs (leaf-refs leaf)))
1613 (unless (or (fun-type-p var-type)
1614 (and (cdr refs)
1615 (eq :declared (leaf-where-from leaf))
1616 (type-needs-conservation-p var-type)))
1617 (let ((int (type-approx-intersection2 var-type type)))
1618 (when (type/= int var-type)
1619 (setf (leaf-type leaf) int)
1620 (let ((s-int (make-single-value-type int)))
1621 (dolist (ref refs)
1622 (derive-node-type ref s-int)
1623 ;; KLUDGE: LET var substitution
1624 (let* ((lvar (node-lvar ref)))
1625 (when (and lvar (combination-p (lvar-dest lvar)))
1626 (reoptimize-lvar lvar)))))))
1627 (values))))
1629 ;;; Iteration variable: exactly one SETQ of the form:
1631 ;;; (let ((var initial))
1632 ;;; ...
1633 ;;; (setq var (+ var step))
1634 ;;; ...)
1635 (defun maybe-infer-iteration-var-type (var initial-type)
1636 (binding* ((sets (lambda-var-sets var) :exit-if-null)
1637 (set (first sets))
1638 (() (null (rest sets)) :exit-if-null)
1639 (set-use (principal-lvar-use (set-value set)))
1640 (() (and (combination-p set-use)
1641 (eq (combination-kind set-use) :known)
1642 (fun-info-p (combination-fun-info set-use))
1643 (not (node-to-be-deleted-p set-use))
1644 (or (eq (combination-fun-source-name set-use) '+)
1645 (eq (combination-fun-source-name set-use) '-)))
1646 :exit-if-null)
1647 (minusp (eq (combination-fun-source-name set-use) '-))
1648 (+-args (basic-combination-args set-use))
1649 (() (and (proper-list-of-length-p +-args 2 2)
1650 (let ((first (principal-lvar-use
1651 (first +-args))))
1652 (and (ref-p first)
1653 (eq (ref-leaf first) var))))
1654 :exit-if-null)
1655 (step-type (lvar-type (second +-args)))
1656 (set-type (lvar-type (set-value set))))
1657 (when (and (numeric-type-p initial-type)
1658 (numeric-type-p step-type)
1659 (or (numeric-type-equal initial-type step-type)
1660 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1661 ;; the initial and the step are of different types,
1662 ;; and the step is less contagious.
1663 (numeric-type-equal initial-type
1664 (numeric-contagion initial-type
1665 step-type))))
1666 (labels ((leftmost (x y cmp cmp=)
1667 (cond ((eq x nil) nil)
1668 ((eq y nil) nil)
1669 ((listp x)
1670 (let ((x1 (first x)))
1671 (cond ((listp y)
1672 (let ((y1 (first y)))
1673 (if (funcall cmp x1 y1) x y)))
1675 (if (funcall cmp x1 y) x y)))))
1676 ((listp y)
1677 (let ((y1 (first y)))
1678 (if (funcall cmp= x y1) x y)))
1679 (t (if (funcall cmp x y) x y))))
1680 (max* (x y) (leftmost x y #'> #'>=))
1681 (min* (x y) (leftmost x y #'< #'<=)))
1682 (multiple-value-bind (low high)
1683 (let ((step-type-non-negative (csubtypep step-type (specifier-type
1684 '(real 0 *))))
1685 (step-type-non-positive (csubtypep step-type (specifier-type
1686 '(real * 0)))))
1687 (cond ((or (and step-type-non-negative (not minusp))
1688 (and step-type-non-positive minusp))
1689 (values (numeric-type-low initial-type)
1690 (when (and (numeric-type-p set-type)
1691 (numeric-type-equal set-type initial-type))
1692 (max* (numeric-type-high initial-type)
1693 (numeric-type-high set-type)))))
1694 ((or (and step-type-non-positive (not minusp))
1695 (and step-type-non-negative minusp))
1696 (values (when (and (numeric-type-p set-type)
1697 (numeric-type-equal set-type initial-type))
1698 (min* (numeric-type-low initial-type)
1699 (numeric-type-low set-type)))
1700 (numeric-type-high initial-type)))
1702 (values nil nil))))
1703 (modified-numeric-type initial-type
1704 :low low
1705 :high high
1706 :enumerable nil))))))
1707 (deftransform + ((x y) * * :result result)
1708 "check for iteration variable reoptimization"
1709 (let ((dest (principal-lvar-end result))
1710 (use (principal-lvar-use x)))
1711 (when (and (ref-p use)
1712 (set-p dest)
1713 (eq (ref-leaf use)
1714 (set-var dest)))
1715 (reoptimize-lvar (set-value dest))))
1716 (give-up-ir1-transform))
1718 ;;; Figure out the type of a LET variable that has sets. We compute
1719 ;;; the union of the INITIAL-TYPE and the types of all the set
1720 ;;; values and to a PROPAGATE-TO-REFS with this type.
1721 (defun propagate-from-sets (var initial-type)
1722 (let ((changes (not (csubtypep (lambda-var-last-initial-type var) initial-type)))
1723 (types nil))
1724 (dolist (set (lambda-var-sets var))
1725 (let ((type (lvar-type (set-value set))))
1726 (push type types)
1727 (when (node-reoptimize set)
1728 (let ((old-type (node-derived-type set)))
1729 (unless (values-subtypep old-type type)
1730 (derive-node-type set (make-single-value-type type))
1731 (setf changes t)))
1732 (setf (node-reoptimize set) nil))))
1733 (when changes
1734 (setf (lambda-var-last-initial-type var) initial-type)
1735 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type)
1736 (apply #'type-union initial-type types))))
1737 (propagate-to-refs var res-type))))
1738 (values))
1740 ;;; If a LET variable, find the initial value's type and do
1741 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1742 ;;; type.
1743 (defun ir1-optimize-set (node)
1744 (declare (type cset node))
1745 (let ((var (set-var node)))
1746 (when (and (lambda-var-p var) (leaf-refs var))
1747 (let ((home (lambda-var-home var)))
1748 (when (eq (functional-kind home) :let)
1749 (let* ((initial-value (let-var-initial-value var))
1750 (initial-type (lvar-type initial-value)))
1751 (setf (lvar-reoptimize initial-value) nil)
1752 (propagate-from-sets var initial-type))))))
1753 (derive-node-type node (make-single-value-type
1754 (lvar-type (set-value node))))
1755 (setf (node-reoptimize node) nil)
1756 (values))
1758 ;;; Return true if the value of REF will always be the same (and is
1759 ;;; thus legal to substitute.)
1760 (defun constant-reference-p (ref)
1761 (declare (type ref ref))
1762 (let ((leaf (ref-leaf ref)))
1763 (typecase leaf
1764 ((or constant functional) t)
1765 (lambda-var
1766 (null (lambda-var-sets leaf)))
1767 (defined-fun
1768 (not (eq (defined-fun-inlinep leaf) :notinline)))
1769 (global-var
1770 (case (global-var-kind leaf)
1771 (:global-function
1772 (let ((name (leaf-source-name leaf)))
1773 (or #-sb-xc-host
1774 (eq (symbol-package (fun-name-block-name name))
1775 *cl-package*)
1776 (info :function :info name)))))))))
1778 ;;; If we have a non-set LET var with a single use, then (if possible)
1779 ;;; replace the variable reference's LVAR with the arg lvar.
1781 ;;; We change the REF to be a reference to NIL with unused value, and
1782 ;;; let it be flushed as dead code. A side effect of this substitution
1783 ;;; is to delete the variable.
1784 (defun substitute-single-use-lvar (arg var)
1785 (declare (type lvar arg) (type lambda-var var))
1786 (binding* ((ref (first (leaf-refs var)))
1787 (lvar (node-lvar ref) :exit-if-null)
1788 (dest (lvar-dest lvar))
1789 (dest-lvar (when (valued-node-p dest) (node-lvar dest))))
1790 (when (and
1791 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1792 ;; LVAR-USEs should not be met on one path. Another problem
1793 ;; is with dynamic-extent.
1794 (eq (lvar-uses lvar) ref)
1795 (not (block-delete-p (node-block ref)))
1796 ;; If the destinatation is dynamic extent, don't substitute unless
1797 ;; the source is as well.
1798 (or (not dest-lvar)
1799 (not (lvar-dynamic-extent dest-lvar))
1800 (lvar-dynamic-extent lvar))
1801 (typecase dest
1802 ;; we should not change lifetime of unknown values lvars
1803 (cast
1804 (and (type-single-value-p (lvar-derived-type arg))
1805 (multiple-value-bind (pdest pprev)
1806 (principal-lvar-end lvar)
1807 (declare (ignore pdest))
1808 (lvar-single-value-p pprev))
1809 ;; CASTs can disappear, don't substitute if
1810 ;; DEST-LVAR has other uses (this will be
1811 ;; insufficient if we have a CAST-CAST chain, but
1812 ;; works well for a single CAST)
1813 (or (null dest-lvar)
1814 (atom (lvar-uses dest-lvar)))))
1815 (mv-combination
1816 (or (eq (basic-combination-fun dest) lvar)
1817 (and (eq (basic-combination-kind dest) :local)
1818 (type-single-value-p (lvar-derived-type arg)))))
1819 ((or creturn exit)
1820 ;; While CRETURN and EXIT nodes may be known-values,
1821 ;; they have their own complications, such as
1822 ;; substitution into CRETURN may create new tail calls.
1823 nil)
1825 (aver (lvar-single-value-p lvar))
1827 (eq (node-home-lambda ref)
1828 (lambda-home (lambda-var-home var))))
1829 (let ((ref-type (single-value-type (node-derived-type ref))))
1830 (cond ((csubtypep (single-value-type (lvar-type arg)) ref-type)
1831 (substitute-lvar-uses lvar arg
1832 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1834 (delete-lvar-use ref))
1836 (let* ((value (make-lvar))
1837 (cast (insert-cast-before ref value ref-type
1838 ;; KLUDGE: it should be (TYPE-CHECK 0)
1839 *policy*)))
1840 (setf (cast-type-to-check cast) *wild-type*)
1841 (substitute-lvar-uses value arg
1842 ;; FIXME
1844 (%delete-lvar-use ref)
1845 (add-lvar-use cast lvar)))))
1846 (setf (node-derived-type ref) *wild-type*)
1847 (change-ref-leaf ref (find-constant nil))
1848 (delete-ref ref)
1849 (unlink-node ref)
1850 (reoptimize-lvar lvar)
1851 t)))
1853 ;;; Delete a LET, removing the call and bind nodes, and warning about
1854 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1855 ;;; along right away and delete the REF and then the lambda, since we
1856 ;;; flush the FUN lvar.
1857 (defun delete-let (clambda)
1858 (declare (type clambda clambda))
1859 (aver (functional-letlike-p clambda))
1860 (note-unreferenced-fun-vars clambda)
1861 (let ((call (let-combination clambda)))
1862 (flush-dest (basic-combination-fun call))
1863 (unlink-node call)
1864 (unlink-node (lambda-bind clambda))
1865 (setf (lambda-bind clambda) nil))
1866 (setf (functional-kind clambda) :zombie)
1867 (let ((home (lambda-home clambda)))
1868 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1869 (values))
1871 ;;; This function is called when one of the arguments to a LET
1872 ;;; changes. We look at each changed argument. If the corresponding
1873 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1874 ;;; consider substituting for the variable, and also propagate
1875 ;;; derived-type information for the arg to all the VAR's refs.
1877 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1878 ;;; subtype of the argument's leaf type. This prevents type checking
1879 ;;; from being defeated, and also ensures that the best representation
1880 ;;; for the variable can be used.
1882 ;;; Substitution of individual references is inhibited if the
1883 ;;; reference is in a different component from the home. This can only
1884 ;;; happen with closures over top level lambda vars. In such cases,
1885 ;;; the references may have already been compiled, and thus can't be
1886 ;;; retroactively modified.
1888 ;;; If all of the variables are deleted (have no references) when we
1889 ;;; are done, then we delete the LET.
1891 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1892 ;;; flags.
1893 (defun propagate-let-args (call fun)
1894 (declare (type combination call) (type clambda fun))
1895 (loop for arg in (combination-args call)
1896 and var in (lambda-vars fun) do
1897 (when (and arg (lvar-reoptimize arg))
1898 (setf (lvar-reoptimize arg) nil)
1899 (cond
1900 ((lambda-var-sets var)
1901 (propagate-from-sets var (lvar-type arg)))
1902 ((let ((use (lvar-uses arg)))
1903 (when (ref-p use)
1904 (let ((leaf (ref-leaf use)))
1905 (when (and (constant-reference-p use)
1906 (csubtypep (leaf-type leaf)
1907 ;; (NODE-DERIVED-TYPE USE) would
1908 ;; be better -- APD, 2003-05-15
1909 (leaf-type var)))
1910 (propagate-to-refs var (lvar-type arg))
1911 (let ((use-component (node-component use)))
1912 (prog1 (substitute-leaf-if
1913 (lambda (ref)
1914 (cond ((eq (node-component ref) use-component)
1917 (aver (lambda-toplevelish-p (lambda-home fun)))
1918 nil)))
1919 leaf var)))
1920 t)))))
1921 ((and (null (rest (leaf-refs var)))
1922 (not (preserve-single-use-debug-var-p call var))
1923 (substitute-single-use-lvar arg var)))
1925 (propagate-to-refs var (lvar-type arg))))))
1927 (when (every #'not (combination-args call))
1928 (delete-let fun))
1930 (values))
1932 ;;; This function is called when one of the args to a non-LET local
1933 ;;; call changes. For each changed argument corresponding to an unset
1934 ;;; variable, we compute the union of the types across all calls and
1935 ;;; propagate this type information to the var's refs.
1937 ;;; If the function has an entry-fun, then we don't do anything: since
1938 ;;; it has a XEP we would not discover anything.
1940 ;;; If the function is an optional-entry-point, we will just make sure
1941 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1942 ;;; can erronously propagate too strict types into refs: see
1943 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1945 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1946 ;;; corresponding to changed arguments in CALL, since the only use in
1947 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1948 ;;; right here.
1949 (defun propagate-local-call-args (call fun)
1950 (declare (type combination call) (type clambda fun))
1951 (unless (functional-entry-fun fun)
1952 (if (lambda-optional-dispatch fun)
1953 ;; We can still make sure &REST is known to be a list.
1954 (loop for var in (lambda-vars fun)
1955 do (let ((info (lambda-var-arg-info var)))
1956 (when (and info (eq :rest (arg-info-kind info)))
1957 (propagate-from-sets var (specifier-type 'list)))))
1958 ;; The normal case.
1959 (let* ((vars (lambda-vars fun))
1960 (union (mapcar (lambda (arg var)
1961 (when (and arg
1962 (lvar-reoptimize arg)
1963 (null (basic-var-sets var)))
1964 (lvar-type arg)))
1965 (basic-combination-args call)
1966 vars))
1967 (this-ref (lvar-use (basic-combination-fun call))))
1969 (dolist (arg (basic-combination-args call))
1970 (when arg
1971 (setf (lvar-reoptimize arg) nil)))
1973 (dolist (ref (leaf-refs fun))
1974 (let ((dest (node-dest ref)))
1975 (unless (or (eq ref this-ref) (not dest))
1976 (setq union
1977 (mapcar (lambda (this-arg old)
1978 (when old
1979 (setf (lvar-reoptimize this-arg) nil)
1980 (type-union (lvar-type this-arg) old)))
1981 (basic-combination-args dest)
1982 union)))))
1984 (loop for var in vars
1985 and type in union
1986 when type do (propagate-to-refs var type)))))
1988 (values))
1990 ;;;; multiple values optimization
1992 ;;; Do stuff to notice a change to a MV combination node. There are
1993 ;;; two main branches here:
1994 ;;; -- If the call is local, then it is already a MV let, or should
1995 ;;; become one. Note that although all :LOCAL MV calls must eventually
1996 ;;; be converted to :MV-LETs, there can be a window when the call
1997 ;;; is local, but has not been LET converted yet. This is because
1998 ;;; the entry-point lambdas may have stray references (in other
1999 ;;; entry points) that have not been deleted yet.
2000 ;;; -- The call is full. This case is somewhat similar to the non-MV
2001 ;;; combination optimization: we propagate return type information and
2002 ;;; notice non-returning calls. We also have an optimization
2003 ;;; which tries to convert MV-CALLs into MV-binds.
2004 (defun ir1-optimize-mv-combination (node)
2005 (let ((fun (basic-combination-fun node)))
2006 (unless (and (node-p (lvar-uses fun))
2007 (node-to-be-deleted-p (lvar-uses fun)))
2008 (ecase (basic-combination-kind node)
2009 (:local
2010 (when (lvar-reoptimize fun)
2011 (setf (lvar-reoptimize fun) nil)
2012 (maybe-let-convert (combination-lambda node)))
2013 (loop for arg in (basic-combination-args node)
2015 (setf (lvar-reoptimize arg) nil))
2016 (when (eq (functional-kind (combination-lambda node)) :mv-let)
2017 (unless (convert-mv-bind-to-let node)
2018 (ir1-optimize-mv-bind node))))
2019 (:full
2020 (let* ((fun-changed (lvar-reoptimize fun))
2021 (args (basic-combination-args node)))
2022 (when fun-changed
2023 (setf (lvar-reoptimize fun) nil)
2024 (let ((type (lvar-type fun)))
2025 (when (fun-type-p type)
2026 (derive-node-type node (fun-type-returns type))))
2027 (maybe-terminate-block node nil)
2028 (let ((use (lvar-uses fun)))
2029 (when (and (ref-p use) (functional-p (ref-leaf use)))
2030 (convert-call-if-possible use node)
2031 (when (eq (basic-combination-kind node) :local)
2032 (maybe-let-convert (ref-leaf use))))))
2033 (unless (or (eq (basic-combination-kind node) :local)
2034 (eq (lvar-fun-name fun) '%throw))
2035 (ir1-optimize-mv-call node))
2036 (dolist (arg args)
2037 (setf (lvar-reoptimize arg) nil))))
2038 (:error))))
2039 (values))
2041 ;;; Propagate derived type info from the values lvar to the vars.
2042 (defun ir1-optimize-mv-bind (node)
2043 (declare (type mv-combination node))
2044 (let* ((args (basic-combination-args node))
2045 (vars (lambda-vars (combination-lambda node)))
2046 (types (if (singleton-p args)
2047 (values-type-in (lvar-derived-type (first args))
2048 (length vars))
2049 (loop for arg in args
2050 append (values-types (lvar-derived-type arg))))))
2051 (loop for var in vars
2052 and type in types
2053 do (if (basic-var-sets var)
2054 (propagate-from-sets var type)
2055 (propagate-to-refs var type)))
2056 (loop for arg in args
2058 (setf (lvar-reoptimize arg) nil)))
2059 (values))
2061 (defun ir1-optimize-mv-call (node)
2062 (let ((fun (basic-combination-fun node))
2063 (*compiler-error-context* node)
2064 (ref (lvar-uses (basic-combination-fun node)))
2065 (args (basic-combination-args node)))
2066 (when (ref-p ref)
2067 (multiple-value-bind (min max) (fun-type-nargs (lvar-type fun))
2068 (let ((total-nvals
2069 (loop for arg in args
2070 for nvals = (nth-value 1 (values-types (lvar-derived-type arg)))
2071 when (eq nvals :unknown) return nil
2072 sum nvals)))
2073 (when total-nvals
2074 (when (and min (< total-nvals min))
2075 (compiler-warn
2076 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2077 at least ~R."
2078 total-nvals min)
2079 (setf (basic-combination-kind node) :error)
2080 (return-from ir1-optimize-mv-call))
2081 (when (and max (> total-nvals max))
2082 (compiler-warn
2083 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2084 at most ~R."
2085 total-nvals max)
2086 (setf (basic-combination-kind node) :error)
2087 (return-from ir1-optimize-mv-call)))
2088 (let ((count (cond (total-nvals)
2089 ((and (policy node (zerop verify-arg-count))
2090 (eql min max))
2091 min)
2092 (t nil))))
2093 (when count
2094 (with-ir1-environment-from-node node
2095 (let* ((dums (make-gensym-list count))
2096 (ignore (gensym))
2097 (leaf (ref-leaf ref))
2098 (fun (ir1-convert-lambda
2099 `(lambda (&optional ,@dums &rest ,ignore)
2100 (declare (ignore ,ignore))
2101 (%funcall ,leaf ,@dums))
2102 :debug-name (leaf-%debug-name leaf))))
2103 (change-ref-leaf ref fun)
2104 (aver (eq (basic-combination-kind node) :full))
2105 (locall-analyze-component *current-component*)
2106 (aver (eq (basic-combination-kind node) :local))))))))))
2107 (values))
2109 ;;; If we see:
2110 ;;; (multiple-value-bind
2111 ;;; (x y)
2112 ;;; (values xx yy)
2113 ;;; ...)
2114 ;;; Convert to:
2115 ;;; (let ((x xx)
2116 ;;; (y yy))
2117 ;;; ...)
2119 ;;; What we actually do is convert the VALUES combination into a
2120 ;;; normal LET combination calling the original :MV-LET lambda. If
2121 ;;; there are extra args to VALUES, discard the corresponding
2122 ;;; lvars. If there are insufficient args, insert references to NIL.
2123 (defun convert-mv-bind-to-let (call)
2124 (declare (type mv-combination call))
2125 (let* ((args (basic-combination-args call))
2126 (use (lvar-uses (first args))))
2127 (when (and (singleton-p args)
2128 (combination-p use)
2129 (eq (lvar-fun-name (combination-fun use))
2130 'values))
2131 (let* ((fun (combination-lambda call))
2132 (vars (lambda-vars fun))
2133 (vals (combination-args use))
2134 (nvars (length vars))
2135 (nvals (length vals)))
2136 (cond ((> nvals nvars)
2137 (mapc #'flush-dest (subseq vals nvars))
2138 (setq vals (subseq vals 0 nvars)))
2139 ((< nvals nvars)
2140 (with-ir1-environment-from-node use
2141 (let ((node-prev (node-prev use)))
2142 (setf (node-prev use) nil)
2143 (setf (ctran-next node-prev) nil)
2144 (collect ((res vals))
2145 (loop for count below (- nvars nvals)
2146 for prev = node-prev then ctran
2147 for ctran = (make-ctran)
2148 and lvar = (make-lvar use)
2149 do (reference-constant prev ctran lvar nil)
2150 (res lvar)
2151 finally (link-node-to-previous-ctran
2152 use ctran))
2153 (setq vals (res)))))))
2154 (setf (combination-args use) vals)
2155 (flush-dest (combination-fun use))
2156 (let ((fun-lvar (basic-combination-fun call)))
2157 (setf (lvar-dest fun-lvar) use)
2158 (setf (combination-fun use) fun-lvar)
2159 (flush-lvar-externally-checkable-type fun-lvar))
2160 (setf (combination-kind use) :local)
2161 (setf (functional-kind fun) :let)
2162 (flush-dest (first (basic-combination-args call)))
2163 (unlink-node call)
2164 (when vals
2165 (reoptimize-lvar (first vals)))
2166 ;; Propagate derived types from the VALUES call to its args:
2167 ;; transforms can leave the VALUES call with a better type
2168 ;; than its args have, so make sure not to throw that away.
2169 (let ((types (values-type-types (node-derived-type use))))
2170 (dolist (val vals)
2171 (when types
2172 (let ((type (pop types)))
2173 (assert-lvar-type val type **zero-typecheck-policy**)))))
2174 ;; Propagate declared types of MV-BIND variables.
2175 (propagate-to-args use fun)
2176 (reoptimize-call use))
2177 t)))
2179 ;;; If we see:
2180 ;;; (values-list (list x y z))
2182 ;;; Convert to:
2183 ;;; (values x y z)
2185 ;;; In implementation, this is somewhat similar to
2186 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2187 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2188 ;;; (allowing the LIST to be flushed.)
2190 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2191 (defoptimizer (values-list optimizer) ((list) node)
2192 (let ((use (lvar-uses list)))
2193 (when (and (combination-p use)
2194 (eq (lvar-fun-name (combination-fun use))
2195 'list))
2197 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2198 (change-ref-leaf (lvar-uses (combination-fun node))
2199 (find-free-fun 'values "in a strange place"))
2200 (setf (combination-kind node) :full)
2201 (let ((args (combination-args use)))
2202 (dolist (arg args)
2203 (setf (lvar-dest arg) node)
2204 (flush-lvar-externally-checkable-type arg))
2205 (setf (combination-args use) nil)
2206 (flush-dest list)
2207 (flush-combination use)
2208 (setf (combination-args node) args))
2209 t)))
2211 ;;; If VALUES appears in a non-MV context, then effectively convert it
2212 ;;; to a PROG1. This allows the computation of the additional values
2213 ;;; to become dead code.
2214 (deftransform values ((&rest vals) * * :node node)
2215 (unless (lvar-single-value-p (node-lvar node))
2216 (give-up-ir1-transform))
2217 (setf (node-derived-type node)
2218 (make-short-values-type (list (single-value-type
2219 (node-derived-type node)))))
2220 (principal-lvar-single-valuify (node-lvar node))
2221 (if vals
2222 (let ((dummies (make-gensym-list (length (cdr vals)))))
2223 `(lambda (val ,@dummies)
2224 (declare (ignore ,@dummies))
2225 val))
2226 nil))
2228 ;;; TODO:
2229 ;;; - CAST chains;
2230 (defun delete-cast (cast)
2231 (declare (type cast cast))
2232 (let ((value (cast-value cast))
2233 (lvar (cast-lvar cast)))
2234 (when (and (bound-cast-p cast)
2235 (bound-cast-check cast))
2236 (flush-combination (bound-cast-check cast))
2237 (setf (bound-cast-check cast) nil))
2238 (delete-filter cast lvar value)
2239 (when lvar
2240 (reoptimize-lvar lvar)
2241 (when (lvar-single-value-p lvar)
2242 (note-single-valuified-lvar lvar)))
2243 (values)))
2245 (defun may-delete-vestigial-exit (cast)
2246 (let ((exit-lexenv (cast-vestigial-exit-lexenv cast)))
2247 (when exit-lexenv
2248 ;; Vestigial exits are only introduced when eliminating a local
2249 ;; RETURN-FROM. We may delete them only when we can show that
2250 ;; there are no other code paths that use the entry LVAR that
2251 ;; are live from within the block that contained the deleted
2252 ;; EXIT (our predecessor block) and that all uses of the entry
2253 ;; LVAR have the same dynamic-extent environment. The
2254 ;; conservative version of this is that there are no EXITs for
2255 ;; any ENTRY introduced between the LEXENV of the deleted EXIT
2256 ;; and the LEXENV of the target ENTRY and that both the LEXENV
2257 ;; of the deleted EXIT and the LEXENV of the ENTRY have the same
2258 ;; set of :DYNAMIC-EXTENT variables and functions.
2259 (let* ((entry-lexenv (cast-vestigial-exit-entry-lexenv cast))
2260 (entry-blocks (lexenv-blocks entry-lexenv))
2261 (entry-tags (lexenv-tags entry-lexenv)))
2262 (do ((current-block (lexenv-blocks exit-lexenv) (cdr current-block)))
2263 ((eq current-block entry-blocks))
2264 (when (entry-exits (cadar current-block))
2265 (return-from may-delete-vestigial-exit nil)))
2266 (do ((current-tag (lexenv-tags exit-lexenv) (cdr current-tag)))
2267 ((eq current-tag entry-tags))
2268 (when (entry-exits (cadar current-tag))
2269 (return-from may-delete-vestigial-exit nil)))
2270 (do ((vars (lexenv-vars exit-lexenv) (cdr vars)))
2271 ((eq vars (lexenv-vars entry-lexenv)))
2272 ;; (CDAR VARS) is the actual variable we're looking at,
2273 ;; except that if it's a symbol-macro then it's (MACRO
2274 ;; . <form>), and that's not a leaf, so ignore the
2275 ;; non-LEAF-P variables.
2276 (when (and (leaf-p (cdar vars))
2277 (leaf-dynamic-extent (cdar vars)))
2278 (return-from may-delete-vestigial-exit nil)))
2279 (do ((funs (lexenv-funs exit-lexenv) (cdr funs)))
2280 ((eq funs (lexenv-funs entry-lexenv)))
2281 ;; Like with VARS, we can have FUNS of the form (MACRO
2282 ;; . <something>), though in this case it's an expander
2283 ;; function. Again, ignore the non-LEAF-P functions.
2284 (when (and (leaf-p (cdar funs))
2285 (leaf-dynamic-extent (cdar funs)))
2286 (return-from may-delete-vestigial-exit nil))))))
2287 (values t))
2289 (defun compile-time-type-error-context (context)
2290 #+sb-xc-host context
2291 #-sb-xc-host (source-to-string context))
2293 (defun ir1-optimize-cast (cast &optional do-not-optimize)
2294 (declare (type cast cast))
2295 (let ((value (cast-value cast))
2296 (atype (cast-asserted-type cast)))
2297 (unless (or do-not-optimize
2298 (not (may-delete-vestigial-exit cast)))
2299 (when (and (bound-cast-p cast)
2300 (bound-cast-check cast)
2301 (constant-lvar-p (bound-cast-bound cast)))
2302 (setf atype
2303 (specifier-type `(integer 0 (,(lvar-value (bound-cast-bound cast)))))
2304 (cast-asserted-type cast) atype
2305 (bound-cast-derived cast) t))
2306 (let ((lvar (node-lvar cast)))
2307 (when (and (or (not (bound-cast-p cast))
2308 (bound-cast-derived cast))
2309 (values-subtypep (lvar-derived-type value)
2310 (cast-asserted-type cast)))
2311 (when (function-designator-cast-p cast)
2312 (let ((*valid-fun-use-name* (function-designator-cast-caller cast))
2313 (*lossage-fun* #'compiler-warn)
2314 (*compiler-error-context* cast))
2315 (valid-callable-argument lvar
2316 (function-designator-cast-arg-count cast))))
2318 (delete-cast cast)
2319 (return-from ir1-optimize-cast t))
2321 (when (and (listp (lvar-uses value))
2322 lvar)
2323 ;; Pathwise removing of CAST
2324 (let ((ctran (node-next cast))
2325 (dest (lvar-dest lvar))
2326 next-block)
2327 (collect ((merges))
2328 (do-uses (use value)
2329 (when (and (values-subtypep (node-derived-type use) atype)
2330 (immediately-used-p value use))
2331 (unless next-block
2332 (when ctran (ensure-block-start ctran))
2333 (setq next-block (first (block-succ (node-block cast))))
2334 (ensure-block-start (node-prev cast))
2335 (reoptimize-lvar lvar)
2336 (setf (lvar-%derived-type value) nil))
2337 (%delete-lvar-use use)
2338 (add-lvar-use use lvar)
2339 (unlink-blocks (node-block use) (node-block cast))
2340 (link-blocks (node-block use) next-block)
2341 (when (and (return-p dest)
2342 (basic-combination-p use)
2343 (eq (basic-combination-kind use) :local))
2344 (merges use))))
2345 (dolist (use (merges))
2346 (merge-tail-sets use))))))
2348 (when (and (bound-cast-p cast)
2349 (bound-cast-check cast)
2350 (policy cast (= insert-array-bounds-checks 0)))
2351 (flush-combination (bound-cast-check cast))
2352 (setf (bound-cast-check cast) nil)))
2354 (let* ((value-type (lvar-derived-type value))
2355 (int (values-type-intersection value-type atype)))
2356 (derive-node-type cast int)
2357 (cond ((or
2358 (neq int *empty-type*)
2359 (eq value-type *empty-type*)))
2360 ;; No need to transform into an analog of
2361 ;; %COMPILE-TIME-TYPE-ERROR, %CHECK-BOUND will signal at
2362 ;; run-time and %CHECK-BOUND ir2-converter will signal at
2363 ;; compile-time if it survives further stages of ir1
2364 ;; optimization.
2365 ((bound-cast-p cast))
2367 ;; FIXME: Do it in one step.
2368 (let ((context (node-source-form cast))
2369 (detail (lvar-all-sources (cast-value cast))))
2370 (filter-lvar
2371 value
2372 (if (cast-single-value-p cast)
2373 `(list 'dummy)
2374 `(multiple-value-call #'list 'dummy)))
2375 (filter-lvar
2376 (cast-value cast)
2377 ;; FIXME: Derived type.
2378 `(%compile-time-type-error 'dummy
2379 ',(type-specifier atype)
2380 ',(type-specifier value-type)
2381 ',detail
2382 ',(compile-time-type-error-context context))))
2383 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2384 ;; functions, so we declare the return type of
2385 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2386 ;; here.
2387 (setq value (cast-value cast))
2388 (derive-node-type (lvar-uses value) *empty-type*)
2389 (maybe-terminate-block (lvar-uses value) nil)
2390 ;; FIXME: Is it necessary?
2391 (aver (null (block-pred (node-block cast))))
2392 (delete-block-lazily (node-block cast))
2393 (return-from ir1-optimize-cast)))
2394 (when (eq (node-derived-type cast) *empty-type*)
2395 (maybe-terminate-block cast nil))
2397 (when (and (cast-%type-check cast)
2398 (values-subtypep value-type
2399 (cast-type-to-check cast)))
2400 (setf (cast-%type-check cast) nil))))
2402 (unless do-not-optimize
2403 (setf (node-reoptimize cast) nil)))