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
8 ;;;; This software is part of the SBCL system. See the README file for
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.
19 ;;;; interface for obtaining results of constant folding
21 ;;; Return true for an LVAR whose sole use is a reference to a
23 (defun constant-lvar-p (thing)
24 (declare (type (or lvar null) thing))
26 (or (let ((use (principal-lvar-use thing)))
27 (and (ref-p use) (constant-p (ref-leaf use))))
28 ;; check for EQL types (but not singleton numeric types)
29 (let ((type (lvar-type thing)))
30 (values (type-singleton-p type))))))
32 ;;; Return the constant value for an LVAR whose only use is a constant
34 (declaim (ftype (function (lvar) t) lvar-value))
35 (defun lvar-value (lvar)
36 (let ((use (principal-lvar-use lvar))
37 (type (lvar-type lvar))
40 (constant-p (setf leaf (ref-leaf use))))
42 (multiple-value-bind (constantp value) (type-singleton-p type)
44 (error "~S used on non-constant LVAR ~S" 'lvar-value lvar))
47 ;;;; interface for obtaining results of type inference
49 ;;; Our best guess for the type of this lvar's value. Note that this
50 ;;; may be VALUES or FUNCTION type, which cannot be passed as an
51 ;;; argument to the normal type operations. See LVAR-TYPE.
53 ;;; The result value is cached in the LVAR-%DERIVED-TYPE slot. If the
54 ;;; slot is true, just return that value, otherwise recompute and
55 ;;; stash the value there.
56 (eval-when (:compile-toplevel :execute)
57 (#+sb-xc-host cl:defmacro
58 #-sb-xc-host sb!xc:defmacro
59 lvar-type-using (lvar accessor)
60 `(let ((uses (lvar-uses ,lvar)))
61 (cond ((null uses) *empty-type*)
63 (do ((res (,accessor (first uses))
64 (values-type-union (,accessor (first current))
66 (current (rest uses) (rest current)))
67 ((or (null current) (eq res *wild-type*))
72 #!-sb-fluid (declaim (inline lvar-derived-type))
73 (defun lvar-derived-type (lvar)
74 (declare (type lvar lvar))
75 (or (lvar-%derived-type lvar)
76 (setf (lvar-%derived-type lvar)
77 (%lvar-derived-type lvar))))
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
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:
93 ;;; (let ((x (the (cons fixnum fixnum) (cons a b))))
95 ;;; (+ (car x) (cdr x)))
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
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)))
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)))
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)))))
127 ((type-needs-conservation-p derived-type)
128 (single-value-type (lvar-type-using lvar node-conservative-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)))
136 (let ((leaf (ref-leaf node)))
137 (if (and (basic-var-p leaf)
138 (cdr (leaf-refs leaf)))
140 (if (eq :declared (leaf-where-from 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)))
152 (specifier-type 'cons))
154 (if (array-type-complexp type)
156 ;; ADJUST-ARRAY may change dimensions, but rank stays same.
158 (let ((old (array-type-dimensions type)))
161 (mapcar (constantly '*) old)))
162 ;; Complexity cannot change.
163 :complexp (array-type-complexp type)
164 ;; Element type cannot change.
165 :element-type (array-type-element-type type)
166 :specialized-element-type (array-type-specialized-element-type type))
167 ;; Simple arrays cannot change at all.
170 ;; If the type contains some CONS types, the conservative type contains all
172 (when (types-equal-or-intersect type (specifier-type 'cons))
173 (setf type (type-union type (specifier-type 'cons))))
174 ;; Similarly for non-simple arrays -- it should be possible to preserve
175 ;; more information here, but really...
176 (let ((non-simple-arrays (specifier-type '(and array (not simple-array)))))
177 (when (types-equal-or-intersect type non-simple-arrays)
178 (setf type (type-union type non-simple-arrays))))
181 (defun type-needs-conservation-p (type)
182 (cond ((eq type *universal-type*)
183 ;; Excluding T is necessary, because we do want type derivation to
184 ;; be able to narrow it down in case someone (most like a macro-expansion...)
185 ;; actually declares something as having type T.
187 ((or (cons-type-p type) (and (array-type-p type) (array-type-complexp type)))
188 ;; Covered by the next case as well, but this is a quick test.
190 ((types-equal-or-intersect type (specifier-type '(or cons (and array (not simple-array)))))
193 ;;; If LVAR is an argument of a function, return a type which the
194 ;;; function checks LVAR for.
195 #!-sb-fluid (declaim (inline lvar-externally-checkable-type))
196 (defun lvar-externally-checkable-type (lvar)
197 (or (lvar-%externally-checkable-type lvar)
198 (%lvar-%externally-checkable-type lvar)))
199 (defun %lvar-%externally-checkable-type (lvar)
200 (declare (type lvar lvar))
201 (let ((dest (lvar-dest lvar)))
202 (if (not (and dest (combination-p dest)))
203 ;; TODO: MV-COMBINATION
204 (setf (lvar-%externally-checkable-type lvar) *wild-type*)
205 (let* ((fun (combination-fun dest))
206 (args (combination-args dest))
207 (fun-type (lvar-type fun)))
208 (setf (lvar-%externally-checkable-type fun) *wild-type*)
209 (if (or (not (call-full-like-p dest))
210 (not (fun-type-p fun-type))
211 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
212 (fun-type-wild-args fun-type))
215 (setf (lvar-%externally-checkable-type arg)
217 (map-combination-args-and-types
219 (setf (lvar-%externally-checkable-type arg)
220 (acond ((lvar-%externally-checkable-type arg)
221 (values-type-intersection
222 it (coerce-to-values type)))
223 (t (coerce-to-values type)))))
225 (or (lvar-%externally-checkable-type lvar) *wild-type*))
226 #!-sb-fluid(declaim (inline flush-lvar-externally-checkable-type))
227 (defun flush-lvar-externally-checkable-type (lvar)
228 (declare (type lvar lvar))
229 (setf (lvar-%externally-checkable-type lvar) nil))
231 ;;;; interface routines used by optimizers
233 (declaim (inline reoptimize-component))
234 (defun reoptimize-component (component kind)
235 (declare (type component component)
236 (type (member nil :maybe t) kind))
238 (unless (eq (component-reoptimize component) t)
239 (setf (component-reoptimize component) kind)))
241 ;;; This function is called by optimizers to indicate that something
242 ;;; interesting has happened to the value of LVAR. Optimizers must
243 ;;; make sure that they don't call for reoptimization when nothing has
244 ;;; happened, since optimization will fail to terminate.
246 ;;; We clear any cached type for the lvar and set the reoptimize flags
247 ;;; on everything in sight.
248 (defun reoptimize-lvar (lvar)
249 (declare (type (or lvar null) lvar))
251 (setf (lvar-%derived-type lvar) nil)
252 (let ((dest (lvar-dest lvar)))
254 (setf (lvar-reoptimize lvar) t)
255 (setf (node-reoptimize dest) t)
256 (binding* (;; Since this may be called during IR1 conversion,
257 ;; PREV may be missing.
258 (prev (node-prev dest) :exit-if-null)
259 (block (ctran-block prev))
260 (component (block-component block)))
261 (when (typep dest 'cif)
262 (setf (block-test-modified block) t))
263 (setf (block-reoptimize block) t)
264 (reoptimize-component component :maybe))))
266 (setf (block-type-check (node-block node)) t)))
269 (defun reoptimize-lvar-uses (lvar)
270 (declare (type lvar lvar))
272 (setf (node-reoptimize use) t)
273 (setf (block-reoptimize (node-block use)) t)
274 (reoptimize-component (node-component use) :maybe)))
276 ;;; Annotate NODE to indicate that its result has been proven to be
277 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
278 ;;; only correct way to supply information discovered about a node's
279 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
280 ;;; information may be lost and reoptimization may not happen.
282 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
283 ;;; intersection is different from the old type, then we do a
284 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
285 (defun derive-node-type (node rtype)
286 (declare (type valued-node node) (type ctype rtype))
287 (let ((node-type (node-derived-type node)))
288 (unless (eq node-type rtype)
289 (let ((int (values-type-intersection node-type rtype))
290 (lvar (node-lvar node)))
291 (when (type/= node-type int)
292 (when (and *check-consistency*
293 (eq int *empty-type*)
294 (not (eq rtype *empty-type*)))
295 (let ((*compiler-error-context* node))
297 "New inferred type ~S conflicts with old type:~
298 ~% ~S~%*** possible internal error? Please report this."
299 (type-specifier rtype) (type-specifier node-type))))
300 (setf (node-derived-type node) int)
301 ;; If the new type consists of only one object, replace the
302 ;; node with a constant reference.
303 (when (and (ref-p node)
304 (lambda-var-p (ref-leaf node)))
305 (let ((type (single-value-type int)))
306 (when (and (member-type-p type)
307 (eql 1 (member-type-size type)))
308 (change-ref-leaf node (find-constant
309 (first (member-type-members type)))))))
310 (reoptimize-lvar lvar)))))
313 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
314 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
315 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
316 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
317 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
318 (defun assert-lvar-type (lvar type policy)
319 (declare (type lvar lvar) (type ctype type))
320 (unless (values-subtypep (lvar-derived-type lvar) type)
321 (let ((internal-lvar (make-lvar))
322 (dest (lvar-dest lvar)))
323 (substitute-lvar internal-lvar lvar)
324 (let ((cast (insert-cast-before dest lvar type policy)))
325 (use-lvar cast internal-lvar)
331 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
332 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
333 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
334 ;;; we are done, then another iteration would be beneficial.
335 (defun ir1-optimize (component fastp)
336 (declare (type component component))
337 (setf (component-reoptimize component) nil)
338 (loop with block = (block-next (component-head component))
339 with tail = (component-tail component)
340 for last-block = block
341 until (eq block tail)
343 ;; We delete blocks when there is either no predecessor or the
344 ;; block is in a lambda that has been deleted. These blocks
345 ;; would eventually be deleted by DFO recomputation, but doing
346 ;; it here immediately makes the effect available to IR1
348 ((or (block-delete-p block)
349 (null (block-pred block)))
350 (delete-block-lazily block)
351 (setq block (clean-component component block)))
352 ((eq (functional-kind (block-home-lambda block)) :deleted)
353 ;; Preserve the BLOCK-SUCC invariant that almost every block has
354 ;; one successor (and a block with DELETE-P set is an acceptable
356 (mark-for-deletion block)
357 (setq block (clean-component component block)))
360 (let ((succ (block-succ block)))
361 (unless (singleton-p succ)
364 (let ((last (block-last block)))
367 (flush-dest (if-test last))
368 (when (unlink-node last)
371 (when (maybe-delete-exit last)
374 (unless (join-successor-if-possible block)
377 (when (and (not fastp) (block-reoptimize block) (block-component block))
378 (aver (not (block-delete-p block)))
379 (ir1-optimize-block block))
381 (cond ((and (block-delete-p block) (block-component block))
382 (setq block (clean-component component block)))
383 ((and (block-flush-p block) (block-component block))
384 (flush-dead-code block)))))
385 do (when (eq block last-block)
386 (setq block (block-next block))))
390 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
393 ;;; Note that although they are cleared here, REOPTIMIZE flags might
394 ;;; still be set upon return from this function, meaning that further
395 ;;; optimization is wanted (as a consequence of optimizations we did).
396 (defun ir1-optimize-block (block)
397 (declare (type cblock block))
398 ;; We clear the node and block REOPTIMIZE flags before doing the
399 ;; optimization, not after. This ensures that the node or block will
400 ;; be reoptimized if necessary.
401 (setf (block-reoptimize block) nil)
402 (do-nodes (node nil block :restart-p t)
403 (when (node-reoptimize node)
404 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
405 (setf (node-reoptimize node) nil)
409 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
410 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
411 ;; any argument changes.
412 (ir1-optimize-combination node))
414 (ir1-optimize-if node))
416 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
417 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
418 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
420 (setf (node-reoptimize node) t)
421 (ir1-optimize-return node))
423 (ir1-optimize-mv-combination node))
425 ;; With an EXIT, we derive the node's type from the VALUE's
427 (let ((value (exit-value node)))
429 (derive-node-type node (lvar-derived-type value)))))
431 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
432 ;; is accurate till the node actually has been reoptimized.
433 (setf (node-reoptimize node) t)
434 (ir1-optimize-set node))
436 (ir1-optimize-cast node)))))
440 ;;; Try to join with a successor block. If we succeed, we return true,
442 (defun join-successor-if-possible (block)
443 (declare (type cblock block))
444 (let ((next (first (block-succ block))))
445 (when (block-start next) ; NEXT is not an END-OF-COMPONENT marker
446 (cond ( ;; We cannot combine with a successor block if:
448 ;; the successor has more than one predecessor;
449 (rest (block-pred next))
450 ;; the successor is the current block (infinite loop);
452 ;; the next block has a different cleanup, and thus
453 ;; we may want to insert cleanup code between the
454 ;; two blocks at some point;
455 (not (eq (block-end-cleanup block)
456 (block-start-cleanup next)))
457 ;; the next block has a different home lambda, and
458 ;; thus the control transfer is a non-local exit.
459 (not (eq (block-home-lambda block)
460 (block-home-lambda next)))
461 ;; Stack analysis phase wants ENTRY to start a block...
462 (entry-p (block-start-node next))
463 (let ((last (block-last block)))
464 (and (valued-node-p last)
465 (awhen (node-lvar last)
467 ;; ... and a DX-allocator to end a block.
468 (lvar-dynamic-extent it)
469 ;; FIXME: This is a partial workaround for bug 303.
470 (consp (lvar-uses it)))))))
473 (join-blocks block next)
476 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
477 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
478 ;;; for the two blocks so that any indicated optimization gets done.
479 (defun join-blocks (block1 block2)
480 (declare (type cblock block1 block2))
481 (let* ((last1 (block-last block1))
482 (last2 (block-last block2))
483 (succ (block-succ block2))
484 (start2 (block-start block2)))
485 (do ((ctran start2 (node-next (ctran-next ctran))))
487 (setf (ctran-block ctran) block1))
489 (unlink-blocks block1 block2)
491 (unlink-blocks block2 block)
492 (link-blocks block1 block))
494 (setf (ctran-kind start2) :inside-block)
495 (setf (node-next last1) start2)
496 (setf (ctran-use start2) last1)
497 (setf (block-last block1) last2))
499 (setf (block-flags block1)
500 (attributes-union (block-flags block1)
502 (block-attributes type-asserted test-modified)))
504 (let ((next (block-next block2))
505 (prev (block-prev block2)))
506 (setf (block-next prev) next)
507 (setf (block-prev next) prev))
511 ;;; Delete any nodes in BLOCK whose value is unused and which have no
512 ;;; side effects. We can delete sets of lexical variables when the set
513 ;;; variable has no references.
514 (defun flush-dead-code (block)
515 (declare (type cblock block))
516 (setf (block-flush-p block) nil)
517 (do-nodes-backwards (node lvar block :restart-p t)
524 (when (flushable-combination-p node)
525 (flush-combination node)))
527 (when (eq (basic-combination-kind node) :local)
528 (let ((fun (combination-lambda node)))
529 (when (dolist (var (lambda-vars fun) t)
530 (when (or (leaf-refs var)
531 (lambda-var-sets var))
533 (flush-dest (first (basic-combination-args node)))
536 (let ((value (exit-value node)))
539 (setf (exit-value node) nil))))
541 (let ((var (set-var node)))
542 (when (and (lambda-var-p var)
543 (null (leaf-refs var)))
544 (flush-dest (set-value node))
545 (setf (basic-var-sets var)
546 (delq node (basic-var-sets var)))
547 (unlink-node node))))
549 (unless (cast-type-check node)
550 (flush-dest (cast-value node))
551 (unlink-node node))))))
555 ;;;; local call return type propagation
557 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
558 ;;; flag set. It iterates over the uses of the RESULT, looking for
559 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
560 ;;; call, then we union its type together with the types of other such
561 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
562 ;;; type with the RESULT's asserted type. We can make this
563 ;;; intersection now (potentially before type checking) because this
564 ;;; assertion on the result will eventually be checked (if
567 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
568 ;;; combination, which may change the successor of the call to be the
569 ;;; called function, and if so, checks if the call can become an
570 ;;; assignment. If we convert to an assignment, we abort, since the
571 ;;; RETURN has been deleted.
572 (defun find-result-type (node)
573 (declare (type creturn node))
574 (let ((result (return-result node)))
575 (collect ((use-union *empty-type* values-type-union))
576 (do-uses (use result)
577 (let ((use-home (node-home-lambda use)))
578 (cond ((or (eq (functional-kind use-home) :deleted)
579 (block-delete-p (node-block use))))
580 ((and (basic-combination-p use)
581 (eq (basic-combination-kind use) :local))
582 (aver (eq (lambda-tail-set use-home)
583 (lambda-tail-set (combination-lambda use))))
584 (when (combination-p use)
585 (when (nth-value 1 (maybe-convert-tail-local-call use))
586 (return-from find-result-type t))))
588 (use-union (node-derived-type use))))))
590 ;; (values-type-intersection
591 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
595 (setf (return-result-type node) int))))
598 ;;; Do stuff to realize that something has changed about the value
599 ;;; delivered to a return node. Since we consider the return values of
600 ;;; all functions in the tail set to be equivalent, this amounts to
601 ;;; bringing the entire tail set up to date. We iterate over the
602 ;;; returns for all the functions in the tail set, reanalyzing them
603 ;;; all (not treating NODE specially.)
605 ;;; When we are done, we check whether the new type is different from
606 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
607 ;;; all the lvars for references to functions in the tail set. This
608 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
609 ;;; results of the calls.
610 (defun ir1-optimize-return (node)
611 (declare (type creturn node))
614 (let* ((tails (lambda-tail-set (return-lambda node)))
615 (funs (tail-set-funs tails)))
616 (collect ((res *empty-type* values-type-union))
618 (let ((return (lambda-return fun)))
620 (when (node-reoptimize return)
621 (setf (node-reoptimize return) nil)
622 (when (find-result-type return)
624 (res (return-result-type return)))))
626 (when (type/= (res) (tail-set-type tails))
627 (setf (tail-set-type tails) (res))
628 (dolist (fun (tail-set-funs tails))
629 (dolist (ref (leaf-refs fun))
630 (reoptimize-lvar (node-lvar ref))))))))
636 ;;; Utility: return T if both argument cblocks are equivalent. For now,
637 ;;; detect only blocks that read the same leaf into the same lvar, and
638 ;;; continue to the same block.
639 (defun cblocks-equivalent-p (x y)
640 (declare (type cblock x y))
641 (and (ref-p (block-start-node x))
642 (eq (block-last x) (block-start-node x))
644 (ref-p (block-start-node y))
645 (eq (block-last y) (block-start-node y))
647 (equal (block-succ x) (block-succ y))
648 (eql (ref-lvar (block-start-node x)) (ref-lvar (block-start-node y)))
649 (eql (ref-leaf (block-start-node x)) (ref-leaf (block-start-node y)))))
651 ;;; Check whether the predicate is known to be true or false,
652 ;;; deleting the IF node in favor of the appropriate branch when this
654 ;;; Similarly, when both branches are equivalent, branch directly to either
656 ;;; Also, if the test has multiple uses, replicate the node when possible.
657 (defun ir1-optimize-if (node)
658 (declare (type cif node))
659 (let ((test (if-test node))
660 (block (node-block node)))
661 (let* ((type (lvar-type test))
662 (consequent (if-consequent node))
663 (alternative (if-alternative node))
665 (cond ((constant-lvar-p test)
666 (if (lvar-value test) alternative consequent))
667 ((not (types-equal-or-intersect type (specifier-type 'null)))
669 ((type= type (specifier-type 'null))
671 ((cblocks-equivalent-p alternative consequent)
675 (when (rest (block-succ block))
676 (unlink-blocks block victim))
677 (setf (component-reanalyze (node-component node)) t)
679 (return-from ir1-optimize-if (values))))
681 (when (and (eq (block-start-node block) node)
682 (listp (lvar-uses test)))
684 (when (immediately-used-p test use)
685 (convert-if-if use node)
686 (when (not (listp (lvar-uses test))) (return))))))
689 ;;; Create a new copy of an IF node that tests the value of the node
690 ;;; USE. The test must have >1 use, and must be immediately used by
691 ;;; USE. NODE must be the only node in its block (implying that
692 ;;; block-start = if-test).
694 ;;; This optimization has an effect semantically similar to the
695 ;;; source-to-source transformation:
696 ;;; (IF (IF A B C) D E) ==>
697 ;;; (IF A (IF B D E) (IF C D E))
699 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
700 ;;; node so that dead code deletion notes will definitely not consider
701 ;;; either node to be part of the original source. One node might
702 ;;; become unreachable, resulting in a spurious note.
703 (defun convert-if-if (use node)
704 (declare (type node use) (type cif node))
705 (with-ir1-environment-from-node node
706 (let* ((block (node-block node))
707 (test (if-test node))
708 (cblock (if-consequent node))
709 (ablock (if-alternative node))
710 (use-block (node-block use))
711 (new-ctran (make-ctran))
712 (new-lvar (make-lvar))
713 (new-node (make-if :test new-lvar
715 :alternative ablock))
716 (new-block (ctran-starts-block new-ctran)))
717 (link-node-to-previous-ctran new-node new-ctran)
718 (setf (lvar-dest new-lvar) new-node)
719 (setf (block-last new-block) new-node)
721 (unlink-blocks use-block block)
722 (%delete-lvar-use use)
723 (add-lvar-use use new-lvar)
724 (link-blocks use-block new-block)
726 (link-blocks new-block cblock)
727 (link-blocks new-block ablock)
729 (push "<IF Duplication>" (node-source-path node))
730 (push "<IF Duplication>" (node-source-path new-node))
732 (reoptimize-lvar test)
733 (reoptimize-lvar new-lvar)
734 (setf (component-reanalyze *current-component*) t)))
737 ;;;; exit IR1 optimization
739 ;;; This function attempts to delete an exit node, returning true if
740 ;;; it deletes the block as a consequence:
741 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
742 ;;; anything, since there is nothing to be done.
743 ;;; -- If the exit node and its ENTRY have the same home lambda then
744 ;;; we know the exit is local, and can delete the exit. We change
745 ;;; uses of the Exit-Value to be uses of the original lvar,
746 ;;; then unlink the node. If the exit is to a TR context, then we
747 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
748 ;;; their value to this exit.
749 ;;; -- If there is no value (as in a GO), then we skip the value
752 ;;; This function is also called by environment analysis, since it
753 ;;; wants all exits to be optimized even if normal optimization was
755 (defun maybe-delete-exit (node)
756 (declare (type exit node))
757 (let ((value (exit-value node))
758 (entry (exit-entry node)))
760 (eq (node-home-lambda node) (node-home-lambda entry)))
761 (setf (entry-exits entry) (delq node (entry-exits entry)))
763 (delete-filter node (node-lvar node) value)
764 (unlink-node node)))))
767 ;;;; combination IR1 optimization
769 ;;; Report as we try each transform?
771 (defvar *show-transforms-p* nil)
773 (defun check-important-result (node info)
774 (when (and (null (node-lvar node))
775 (ir1-attributep (fun-info-attributes info) important-result))
776 (let ((*compiler-error-context* node))
778 "The return value of ~A should not be discarded."
779 (lvar-fun-name (basic-combination-fun node))))))
781 ;;; Do IR1 optimizations on a COMBINATION node.
782 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
783 (defun ir1-optimize-combination (node)
784 (when (lvar-reoptimize (basic-combination-fun node))
785 (propagate-fun-change node)
786 (maybe-terminate-block node nil))
787 (let ((args (basic-combination-args node))
788 (kind (basic-combination-kind node))
789 (info (basic-combination-fun-info node)))
792 (let ((fun (combination-lambda node)))
793 (if (eq (functional-kind fun) :let)
794 (propagate-let-args node fun)
795 (propagate-local-call-args node fun))))
799 (setf (lvar-reoptimize arg) nil))))
803 (setf (lvar-reoptimize arg) nil)))
805 (check-important-result node info)
806 (let ((fun (fun-info-destroyed-constant-args info)))
808 (let ((destroyed-constant-args (funcall fun args)))
809 (when destroyed-constant-args
810 (let ((*compiler-error-context* node))
811 (warn 'constant-modified
812 :fun-name (lvar-fun-name
813 (basic-combination-fun node)))
814 (setf (basic-combination-kind node) :error)
815 (return-from ir1-optimize-combination))))))
816 (let ((fun (fun-info-derive-type info)))
818 (let ((res (funcall fun node)))
820 (derive-node-type node (coerce-to-values res))
821 (maybe-terminate-block node nil))))))
823 ;; Check against the DEFINED-TYPE unless TYPE is already good.
824 (let* ((fun (basic-combination-fun node))
825 (uses (lvar-uses fun))
826 (leaf (when (ref-p uses) (ref-leaf uses))))
827 (multiple-value-bind (type defined-type)
828 (if (global-var-p leaf)
829 (values (leaf-type leaf) (leaf-defined-type leaf))
831 (when (and (not (fun-type-p type)) (fun-type-p defined-type))
832 (validate-call-type node type leaf)))))))
837 (setf (lvar-reoptimize arg) nil)))
838 (check-important-result node info)
839 (let ((fun (fun-info-destroyed-constant-args info)))
841 ;; If somebody is really sure that they want to modify
842 ;; constants, let them.
843 (policy node (> check-constant-modification 0)))
844 (let ((destroyed-constant-args (funcall fun args)))
845 (when destroyed-constant-args
846 (let ((*compiler-error-context* node))
847 (warn 'constant-modified
848 :fun-name (lvar-fun-name
849 (basic-combination-fun node)))
850 (setf (basic-combination-kind node) :error)
851 (return-from ir1-optimize-combination))))))
853 (let ((attr (fun-info-attributes info)))
854 (when (and (ir1-attributep attr foldable)
855 ;; KLUDGE: The next test could be made more sensitive,
856 ;; only suppressing constant-folding of functions with
857 ;; CALL attributes when they're actually passed
858 ;; function arguments. -- WHN 19990918
859 (not (ir1-attributep attr call))
860 (every #'constant-lvar-p args)
862 (constant-fold-call node)
863 (return-from ir1-optimize-combination)))
865 (let ((fun (fun-info-derive-type info)))
867 (let ((res (funcall fun node)))
869 (derive-node-type node (coerce-to-values res))
870 (maybe-terminate-block node nil)))))
872 (let ((fun (fun-info-optimizer info)))
873 (unless (and fun (funcall fun node))
874 ;; First give the VM a peek at the call
875 (multiple-value-bind (style transform)
876 (combination-implementation-style node)
879 ;; The VM knows how to handle this.
882 ;; The VM mostly knows how to handle this. We need
883 ;; to massage the call slightly, though.
884 (transform-call node transform (combination-fun-source-name node)))
886 ;; Let transforms have a crack at it.
887 (dolist (x (fun-info-transforms info))
889 (when *show-transforms-p*
890 (let* ((lvar (basic-combination-fun node))
891 (fname (lvar-fun-name lvar t)))
892 (/show "trying transform" x (transform-function x) "for" fname)))
893 (unless (ir1-transform node x)
895 (when *show-transforms-p*
896 (/show "quitting because IR1-TRANSFORM result was NIL"))
901 (defun xep-tail-combination-p (node)
902 (and (combination-p node)
903 (let* ((lvar (combination-lvar node))
904 (dest (when (lvar-p lvar) (lvar-dest lvar)))
905 (lambda (when (return-p dest) (return-lambda dest))))
906 (and (lambda-p lambda)
907 (eq :external (lambda-kind lambda))))))
909 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
910 ;;; the block there, and link it to the component tail.
912 ;;; Except when called during IR1 convertion, we delete the
913 ;;; continuation if it has no other uses. (If it does have other uses,
916 ;;; Termination on the basis of a continuation type is
918 ;;; -- The continuation is deleted (hence the assertion is spurious), or
919 ;;; -- We are in IR1 conversion (where THE assertions are subject to
920 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
921 ;;; uses can(?) be added later. -- APD, 2003-07-17
923 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
924 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p)
925 (declare (type (or basic-combination cast ref) node))
926 (let* ((block (node-block node))
927 (lvar (node-lvar node))
928 (ctran (node-next node))
929 (tail (component-tail (block-component block)))
930 (succ (first (block-succ block))))
931 (declare (ignore lvar))
932 (unless (or (and (eq node (block-last block)) (eq succ tail))
933 (block-delete-p block))
934 ;; Even if the combination will never return, don't terminate if this
935 ;; is the tail call of a XEP: doing that would inhibit TCO.
936 (when (and (eq (node-derived-type node) *empty-type*)
937 (not (xep-tail-combination-p node)))
938 (cond (ir1-converting-not-optimizing-p
941 (aver (eq (block-last block) node)))
943 (setf (block-last block) node)
944 (setf (ctran-use ctran) nil)
945 (setf (ctran-kind ctran) :unused)
946 (setf (ctran-block ctran) nil)
947 (setf (node-next node) nil)
948 (link-blocks block (ctran-starts-block ctran)))))
950 (node-ends-block node)))
952 (let ((succ (first (block-succ block))))
953 (unlink-blocks block succ)
954 (setf (component-reanalyze (block-component block)) t)
955 (aver (not (block-succ block)))
956 (link-blocks block tail)
957 (cond (ir1-converting-not-optimizing-p
958 (%delete-lvar-use node))
959 (t (delete-lvar-use node)
960 (when (null (block-pred succ))
961 (mark-for-deletion succ)))))
964 ;;; This is called both by IR1 conversion and IR1 optimization when
965 ;;; they have verified the type signature for the call, and are
966 ;;; wondering if something should be done to special-case the call. If
967 ;;; CALL is a call to a global function, then see whether it defined
969 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
970 ;;; the expansion and change the call to call it. Expansion is
971 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
972 ;;; true, we never expand, since this function has already been
973 ;;; converted. Local call analysis will duplicate the definition
974 ;;; if necessary. We claim that the parent form is LABELS for
975 ;;; context declarations, since we don't want it to be considered
976 ;;; a real global function.
977 ;;; -- If it is a known function, mark it as such by setting the KIND.
979 ;;; We return the leaf referenced (NIL if not a leaf) and the
980 ;;; FUN-INFO assigned.
981 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
982 (declare (type combination call))
983 (let* ((ref (lvar-uses (basic-combination-fun call)))
984 (leaf (when (ref-p ref) (ref-leaf ref)))
985 (inlinep (if (defined-fun-p leaf)
986 (defined-fun-inlinep leaf)
989 ((eq inlinep :notinline)
990 (let ((info (info :function :info (leaf-source-name leaf))))
992 (setf (basic-combination-fun-info call) info))
994 ((not (and (global-var-p leaf)
995 (eq (global-var-kind leaf) :global-function)))
1000 ((nil :maybe-inline) (policy call (zerop space))))
1001 (defined-fun-p leaf)
1002 (defined-fun-inline-expansion leaf)
1003 (inline-expansion-ok call))
1004 ;; Inline: if the function has already been converted at another call
1005 ;; site in this component, we point this REF to the functional. If not,
1006 ;; we convert the expansion.
1008 ;; For :INLINE case local call analysis will copy the expansion later,
1009 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1010 ;; expansion per component.
1012 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1015 (let* ((name (leaf-source-name leaf))
1016 (res (ir1-convert-inline-expansion
1018 (defined-fun-inline-expansion leaf)
1021 (info :function :info name))))
1022 ;; Allow backward references to this function from following
1023 ;; forms. (Reused only if policy matches.)
1024 (push res (defined-fun-functionals leaf))
1025 (change-ref-leaf ref res))))
1026 (let ((fun (defined-fun-functional leaf)))
1028 (and (eq inlinep :inline) (functional-kind fun)))
1030 (if ir1-converting-not-optimizing-p
1032 (with-ir1-environment-from-node call
1034 (locall-analyze-component *current-component*)))
1035 ;; If we've already converted, change ref to the converted
1037 (change-ref-leaf ref fun))))
1038 (values (ref-leaf ref) nil))
1040 (let ((info (info :function :info (leaf-source-name leaf))))
1044 (setf (basic-combination-kind call) :known)
1045 (setf (basic-combination-fun-info call) info)))
1046 (values leaf nil)))))))
1048 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1049 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1050 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1051 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1052 ;;; syntax check, arg/result type processing, but still call
1053 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1054 ;;; and that checking is done by local call analysis.
1055 (defun validate-call-type (call type fun &optional ir1-converting-not-optimizing-p)
1056 (declare (type combination call) (type ctype type))
1057 (let* ((where (when fun (leaf-where-from fun)))
1058 (same-file-p (eq :defined-here where)))
1059 (cond ((not (fun-type-p type))
1060 (aver (multiple-value-bind (val win)
1061 (csubtypep type (specifier-type 'function))
1062 (or val (not win))))
1063 ;; Using the defined-type too early is a bit of a waste: during
1064 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1065 (when (and fun (not ir1-converting-not-optimizing-p))
1066 (let ((defined-type (leaf-defined-type fun)))
1067 (when (and (fun-type-p defined-type)
1068 (neq fun (combination-type-validated-for-leaf call)))
1069 ;; Don't validate multiple times against the same leaf --
1070 ;; it doesn't add any information, but may generate the same warning
1072 (setf (combination-type-validated-for-leaf call) fun)
1073 (when (and (valid-fun-use call defined-type
1074 :argument-test #'always-subtypep
1076 :lossage-fun (if same-file-p
1078 #'compiler-style-warn)
1079 :unwinnage-fun #'compiler-notify)
1081 (assert-call-type call defined-type nil)
1082 (maybe-terminate-block call ir1-converting-not-optimizing-p)))))
1083 (recognize-known-call call ir1-converting-not-optimizing-p))
1084 ((valid-fun-use call type
1085 :argument-test #'always-subtypep
1087 :lossage-fun #'compiler-warn
1088 :unwinnage-fun #'compiler-notify)
1089 (assert-call-type call type)
1090 (maybe-terminate-block call ir1-converting-not-optimizing-p)
1091 (recognize-known-call call ir1-converting-not-optimizing-p))
1093 (setf (combination-kind call) :error)
1094 (values nil nil)))))
1096 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1097 ;;; changed. If the call is local, we try to LET-convert it, and
1098 ;;; derive the result type. If it is a :FULL call, we validate it
1099 ;;; against the type, which recognizes known calls, does inline
1100 ;;; expansion, etc. If a call to a predicate in a non-conditional
1101 ;;; position or to a function with a source transform, then we
1102 ;;; reconvert the form to give IR1 another chance.
1103 (defun propagate-fun-change (call)
1104 (declare (type combination call))
1105 (let ((*compiler-error-context* call)
1106 (fun-lvar (basic-combination-fun call)))
1107 (setf (lvar-reoptimize fun-lvar) nil)
1108 (case (combination-kind call)
1110 (let ((fun (combination-lambda call)))
1111 (maybe-let-convert fun)
1112 (unless (member (functional-kind fun) '(:let :assignment :deleted))
1113 (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
1115 (multiple-value-bind (leaf info)
1116 (let* ((uses (lvar-uses fun-lvar))
1117 (leaf (when (ref-p uses) (ref-leaf uses))))
1118 (validate-call-type call (lvar-type fun-lvar) leaf))
1119 (cond ((functional-p leaf)
1120 (convert-call-if-possible
1121 (lvar-uses (basic-combination-fun call))
1124 ((and (global-var-p leaf)
1125 (eq (global-var-kind leaf) :global-function)
1126 (leaf-has-source-name-p leaf)
1127 (or (info :function :source-transform (leaf-source-name leaf))
1129 (ir1-attributep (fun-info-attributes info)
1131 (let ((lvar (node-lvar call)))
1132 (and lvar (not (if-p (lvar-dest lvar))))))))
1133 (let ((name (leaf-source-name leaf))
1134 (dummies (make-gensym-list
1135 (length (combination-args call)))))
1136 (transform-call call
1138 (,@(if (symbolp name)
1142 (leaf-source-name leaf)))))))))
1145 ;;;; known function optimization
1147 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1148 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1149 ;;; replace it, otherwise add a new one.
1150 (defun record-optimization-failure (node transform args)
1151 (declare (type combination node) (type transform transform)
1152 (type (or fun-type list) args))
1153 (let* ((table (component-failed-optimizations *component-being-compiled*))
1154 (found (assoc transform (gethash node table))))
1156 (setf (cdr found) args)
1157 (push (cons transform args) (gethash node table))))
1160 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1161 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1162 ;;; doing the transform for some reason and FLAME is true, then we
1163 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1164 ;;; finalize to pick up. We return true if the transform failed, and
1165 ;;; thus further transformation should be attempted. We return false
1166 ;;; if either the transform succeeded or was aborted.
1167 (defun ir1-transform (node transform)
1168 (declare (type combination node) (type transform transform))
1169 (let* ((type (transform-type transform))
1170 (fun (transform-function transform))
1171 (constrained (fun-type-p type))
1172 (table (component-failed-optimizations *component-being-compiled*))
1173 (flame (if (transform-important transform)
1174 (policy node (>= speed inhibit-warnings))
1175 (policy node (> speed inhibit-warnings))))
1176 (*compiler-error-context* node))
1177 (cond ((or (not constrained)
1178 (valid-fun-use node type))
1179 (multiple-value-bind (severity args)
1180 (catch 'give-up-ir1-transform
1181 (transform-call node
1183 (combination-fun-source-name node))
1187 (remhash node table)
1190 (setf (combination-kind node) :error)
1192 (apply #'warn args))
1193 (remhash node table)
1198 (record-optimization-failure node transform args))
1199 (setf (gethash node table)
1200 (remove transform (gethash node table) :key #'car)))
1203 (remhash node table)
1208 :argument-test #'types-equal-or-intersect
1209 :result-test #'values-types-equal-or-intersect))
1210 (record-optimization-failure node transform type)
1215 ;;; When we don't like an IR1 transform, we throw the severity/reason
1218 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1219 ;;; aborting this attempt to transform the call, but admitting the
1220 ;;; possibility that this or some other transform will later succeed.
1221 ;;; If arguments are supplied, they are format arguments for an
1222 ;;; efficiency note.
1224 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1225 ;;; force a normal call to the function at run time. No further
1226 ;;; optimizations will be attempted.
1228 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1229 ;;; delay the transform on the node until later. REASONS specifies
1230 ;;; when the transform will be later retried. The :OPTIMIZE reason
1231 ;;; causes the transform to be delayed until after the current IR1
1232 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1233 ;;; be delayed until after constraint propagation.
1235 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1236 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1237 ;;; do CASE operations on the various REASON values, it might be a
1238 ;;; good idea to go OO, representing the reasons by objects, using
1239 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1240 ;;; SIGNAL instead of THROW.
1241 (declaim (ftype (function (&rest t) nil) give-up-ir1-transform))
1242 (defun give-up-ir1-transform (&rest args)
1243 (throw 'give-up-ir1-transform (values :failure args)))
1244 (defun abort-ir1-transform (&rest args)
1245 (throw 'give-up-ir1-transform (values :aborted args)))
1246 (defun delay-ir1-transform (node &rest reasons)
1247 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1249 (setf *delayed-ir1-transforms*
1250 (acons node reasons *delayed-ir1-transforms*))
1251 (throw 'give-up-ir1-transform :delayed))
1253 (dolist (reason reasons)
1254 (pushnew reason (cdr assoc)))
1255 (throw 'give-up-ir1-transform :delayed)))))
1257 ;;; Clear any delayed transform with no reasons - these should have
1258 ;;; been tried in the last pass. Then remove the reason from the
1259 ;;; delayed transform reasons, and if any become empty then set
1260 ;;; reoptimize flags for the node. Return true if any transforms are
1262 (defun retry-delayed-ir1-transforms (reason)
1263 (setf *delayed-ir1-transforms*
1264 (remove-if-not #'cdr *delayed-ir1-transforms*))
1265 (let ((reoptimize nil))
1266 (dolist (assoc *delayed-ir1-transforms*)
1267 (let ((reasons (remove reason (cdr assoc))))
1268 (setf (cdr assoc) reasons)
1270 (let ((node (car assoc)))
1271 (unless (node-deleted node)
1273 (setf (node-reoptimize node) t)
1274 (let ((block (node-block node)))
1275 (setf (block-reoptimize block) t)
1276 (reoptimize-component (block-component block) :maybe)))))))
1279 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1280 ;;; environment, and then install it as the function for the call
1281 ;;; NODE. We do local call analysis so that the new function is
1282 ;;; integrated into the control flow.
1284 ;;; We require the original function source name in order to generate
1285 ;;; a meaningful debug name for the lambda we set up. (It'd be
1286 ;;; possible to do this starting from debug names as well as source
1287 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1288 ;;; generality, since source names are always known to our callers.)
1289 (defun transform-call (call res source-name)
1290 (declare (type combination call) (list res))
1291 (aver (and (legal-fun-name-p source-name)
1292 (not (eql source-name '.anonymous.))))
1293 (node-ends-block call)
1294 ;; The internal variables of a transform are not going to be
1295 ;; interesting to the debugger, so there's no sense in
1296 ;; suppressing the substitution of variables with only one use
1297 ;; (the extra variables can slow down constraint propagation).
1299 ;; This needs to be done before the WITH-IR1-ENVIRONMENT-FROM-NODE,
1300 ;; so that it will bind *LEXENV* to the right environment.
1301 (setf (combination-lexenv call)
1302 (make-lexenv :default (combination-lexenv call)
1303 :policy (process-optimize-decl
1305 (preserve-single-use-debug-variables 0))
1307 (combination-lexenv call)))))
1308 (with-ir1-environment-from-node call
1309 (with-component-last-block (*current-component*
1310 (block-next (node-block call)))
1312 (let ((new-fun (ir1-convert-inline-lambda
1314 :debug-name (debug-name 'lambda-inlined source-name)
1316 (ref (lvar-use (combination-fun call))))
1317 (change-ref-leaf ref new-fun)
1318 (setf (combination-kind call) :full)
1319 (locall-analyze-component *current-component*))))
1322 ;;; Replace a call to a foldable function of constant arguments with
1323 ;;; the result of evaluating the form. If there is an error during the
1324 ;;; evaluation, we give a warning and leave the call alone, making the
1325 ;;; call a :ERROR call.
1327 ;;; If there is more than one value, then we transform the call into a
1329 (defun constant-fold-call (call)
1330 (let ((args (mapcar #'lvar-value (combination-args call)))
1331 (fun-name (combination-fun-source-name call)))
1332 (multiple-value-bind (values win)
1333 (careful-call fun-name
1336 ;; Note: CMU CL had COMPILER-WARN here, and that
1337 ;; seems more natural, but it's probably not.
1339 ;; It's especially not while bug 173 exists:
1342 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1344 ;; can cause constant-folding TYPE-ERRORs (in
1345 ;; #'<=) when END can be proved to be NIL, even
1346 ;; though the code is perfectly legal and safe
1347 ;; because a NIL value of END means that the
1348 ;; #'<= will never be executed.
1350 ;; Moreover, even without bug 173,
1351 ;; quite-possibly-valid code like
1352 ;; (COND ((NONINLINED-PREDICATE END)
1353 ;; (UNLESS (<= END SIZE))
1355 ;; (where NONINLINED-PREDICATE is something the
1356 ;; compiler can't do at compile time, but which
1357 ;; turns out to make the #'<= expression
1358 ;; unreachable when END=NIL) could cause errors
1359 ;; when the compiler tries to constant-fold (<=
1362 ;; So, with or without bug 173, it'd be
1363 ;; unnecessarily evil to do a full
1364 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1365 ;; from COMPILE-FILE) for legal code, so we we
1366 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1367 #-sb-xc-host #'compiler-style-warn
1368 ;; On the other hand, for code we control, we
1369 ;; should be able to work around any bug
1370 ;; 173-related problems, and in particular we
1371 ;; want to be alerted to calls to our own
1372 ;; functions which aren't being folded away; a
1373 ;; COMPILER-WARNING is butch enough to stop the
1374 ;; SBCL build itself in its tracks.
1375 #+sb-xc-host #'compiler-warn
1378 (setf (combination-kind call) :error))
1379 ((and (proper-list-of-length-p values 1))
1380 (with-ir1-environment-from-node call
1381 (let* ((lvar (node-lvar call))
1382 (prev (node-prev call))
1383 (intermediate-ctran (make-ctran)))
1384 (%delete-lvar-use call)
1385 (setf (ctran-next prev) nil)
1386 (setf (node-prev call) nil)
1387 (reference-constant prev intermediate-ctran lvar
1389 (link-node-to-previous-ctran call intermediate-ctran)
1390 (reoptimize-lvar lvar)
1391 (flush-combination call))))
1392 (t (let ((dummies (make-gensym-list (length args))))
1396 (declare (ignore ,@dummies))
1397 (values ,@(mapcar (lambda (x) `',x) values)))
1401 ;;;; local call optimization
1403 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1405 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1406 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1409 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1410 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1411 (defun propagate-to-refs (leaf type)
1412 (declare (type leaf leaf) (type ctype type))
1413 (let ((var-type (leaf-type leaf))
1414 (refs (leaf-refs leaf)))
1415 (unless (or (fun-type-p var-type)
1417 (eq :declared (leaf-where-from leaf))
1418 (type-needs-conservation-p var-type)))
1419 (let ((int (type-approx-intersection2 var-type type)))
1420 (when (type/= int var-type)
1421 (setf (leaf-type leaf) int)
1422 (let ((s-int (make-single-value-type int)))
1424 (derive-node-type ref s-int)
1425 ;; KLUDGE: LET var substitution
1426 (let* ((lvar (node-lvar ref)))
1427 (when (and lvar (combination-p (lvar-dest lvar)))
1428 (reoptimize-lvar lvar)))))))
1431 ;;; Iteration variable: exactly one SETQ of the form:
1433 ;;; (let ((var initial))
1435 ;;; (setq var (+ var step))
1437 (defun maybe-infer-iteration-var-type (var initial-type)
1438 (binding* ((sets (lambda-var-sets var) :exit-if-null)
1440 (() (null (rest sets)) :exit-if-null)
1441 (set-use (principal-lvar-use (set-value set)))
1442 (() (and (combination-p set-use)
1443 (eq (combination-kind set-use) :known)
1444 (fun-info-p (combination-fun-info set-use))
1445 (not (node-to-be-deleted-p set-use))
1446 (or (eq (combination-fun-source-name set-use) '+)
1447 (eq (combination-fun-source-name set-use) '-)))
1449 (minusp (eq (combination-fun-source-name set-use) '-))
1450 (+-args (basic-combination-args set-use))
1451 (() (and (proper-list-of-length-p +-args 2 2)
1452 (let ((first (principal-lvar-use
1455 (eq (ref-leaf first) var))))
1457 (step-type (lvar-type (second +-args)))
1458 (set-type (lvar-type (set-value set))))
1459 (when (and (numeric-type-p initial-type)
1460 (numeric-type-p step-type)
1461 (or (numeric-type-equal initial-type step-type)
1462 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1463 ;; the initial and the step are of different types,
1464 ;; and the step is less contagious.
1465 (numeric-type-equal initial-type
1466 (numeric-contagion initial-type
1468 (labels ((leftmost (x y cmp cmp=)
1469 (cond ((eq x nil) nil)
1472 (let ((x1 (first x)))
1474 (let ((y1 (first y)))
1475 (if (funcall cmp x1 y1) x y)))
1477 (if (funcall cmp x1 y) x y)))))
1479 (let ((y1 (first y)))
1480 (if (funcall cmp= x y1) x y)))
1481 (t (if (funcall cmp x y) x y))))
1482 (max* (x y) (leftmost x y #'> #'>=))
1483 (min* (x y) (leftmost x y #'< #'<=)))
1484 (multiple-value-bind (low high)
1485 (let ((step-type-non-negative (csubtypep step-type (specifier-type
1487 (step-type-non-positive (csubtypep step-type (specifier-type
1489 (cond ((or (and step-type-non-negative (not minusp))
1490 (and step-type-non-positive minusp))
1491 (values (numeric-type-low initial-type)
1492 (when (and (numeric-type-p set-type)
1493 (numeric-type-equal set-type initial-type))
1494 (max* (numeric-type-high initial-type)
1495 (numeric-type-high set-type)))))
1496 ((or (and step-type-non-positive (not minusp))
1497 (and step-type-non-negative minusp))
1498 (values (when (and (numeric-type-p set-type)
1499 (numeric-type-equal set-type initial-type))
1500 (min* (numeric-type-low initial-type)
1501 (numeric-type-low set-type)))
1502 (numeric-type-high initial-type)))
1505 (modified-numeric-type initial-type
1508 :enumerable nil))))))
1509 (deftransform + ((x y) * * :result result)
1510 "check for iteration variable reoptimization"
1511 (let ((dest (principal-lvar-end result))
1512 (use (principal-lvar-use x)))
1513 (when (and (ref-p use)
1517 (reoptimize-lvar (set-value dest))))
1518 (give-up-ir1-transform))
1520 ;;; Figure out the type of a LET variable that has sets. We compute
1521 ;;; the union of the INITIAL-TYPE and the types of all the set
1522 ;;; values and to a PROPAGATE-TO-REFS with this type.
1523 (defun propagate-from-sets (var initial-type)
1524 (let ((changes (not (csubtypep (lambda-var-last-initial-type var) initial-type)))
1526 (dolist (set (lambda-var-sets var))
1527 (let ((type (lvar-type (set-value set))))
1529 (when (node-reoptimize set)
1530 (let ((old-type (node-derived-type set)))
1531 (unless (values-subtypep old-type type)
1532 (derive-node-type set (make-single-value-type type))
1534 (setf (node-reoptimize set) nil))))
1536 (setf (lambda-var-last-initial-type var) initial-type)
1537 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type)
1538 (apply #'type-union initial-type types))))
1539 (propagate-to-refs var res-type))))
1542 ;;; If a LET variable, find the initial value's type and do
1543 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1545 (defun ir1-optimize-set (node)
1546 (declare (type cset node))
1547 (let ((var (set-var node)))
1548 (when (and (lambda-var-p var) (leaf-refs var))
1549 (let ((home (lambda-var-home var)))
1550 (when (eq (functional-kind home) :let)
1551 (let* ((initial-value (let-var-initial-value var))
1552 (initial-type (lvar-type initial-value)))
1553 (setf (lvar-reoptimize initial-value) nil)
1554 (propagate-from-sets var initial-type))))))
1555 (derive-node-type node (make-single-value-type
1556 (lvar-type (set-value node))))
1557 (setf (node-reoptimize node) nil)
1560 ;;; Return true if the value of REF will always be the same (and is
1561 ;;; thus legal to substitute.)
1562 (defun constant-reference-p (ref)
1563 (declare (type ref ref))
1564 (let ((leaf (ref-leaf ref)))
1566 ((or constant functional) t)
1568 (null (lambda-var-sets leaf)))
1570 (not (eq (defined-fun-inlinep leaf) :notinline)))
1572 (case (global-var-kind leaf)
1574 (let ((name (leaf-source-name leaf)))
1576 (eq (symbol-package (fun-name-block-name name))
1578 (info :function :info name)))))))))
1580 ;;; If we have a non-set LET var with a single use, then (if possible)
1581 ;;; replace the variable reference's LVAR with the arg lvar.
1583 ;;; We change the REF to be a reference to NIL with unused value, and
1584 ;;; let it be flushed as dead code. A side effect of this substitution
1585 ;;; is to delete the variable.
1586 (defun substitute-single-use-lvar (arg var)
1587 (declare (type lvar arg) (type lambda-var var))
1588 (binding* ((ref (first (leaf-refs var)))
1589 (lvar (node-lvar ref) :exit-if-null)
1590 (dest (lvar-dest lvar))
1591 (dest-lvar (when (valued-node-p dest) (node-lvar dest))))
1593 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1594 ;; LVAR-USEs should not be met on one path. Another problem
1595 ;; is with dynamic-extent.
1596 (eq (lvar-uses lvar) ref)
1597 (not (block-delete-p (node-block ref)))
1598 ;; If the destinatation is dynamic extent, don't substitute unless
1599 ;; the source is as well.
1601 (not (lvar-dynamic-extent dest-lvar))
1602 (lvar-dynamic-extent lvar))
1604 ;; we should not change lifetime of unknown values lvars
1606 (and (type-single-value-p (lvar-derived-type arg))
1607 (multiple-value-bind (pdest pprev)
1608 (principal-lvar-end lvar)
1609 (declare (ignore pdest))
1610 (lvar-single-value-p pprev))))
1612 (or (eq (basic-combination-fun dest) lvar)
1613 (and (eq (basic-combination-kind dest) :local)
1614 (type-single-value-p (lvar-derived-type arg)))))
1616 ;; While CRETURN and EXIT nodes may be known-values,
1617 ;; they have their own complications, such as
1618 ;; substitution into CRETURN may create new tail calls.
1621 (aver (lvar-single-value-p lvar))
1623 (eq (node-home-lambda ref)
1624 (lambda-home (lambda-var-home var))))
1625 (let ((ref-type (single-value-type (node-derived-type ref))))
1626 (cond ((csubtypep (single-value-type (lvar-type arg)) ref-type)
1627 (substitute-lvar-uses lvar arg
1628 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1630 (delete-lvar-use ref))
1632 (let* ((value (make-lvar))
1633 (cast (insert-cast-before ref value ref-type
1634 ;; KLUDGE: it should be (TYPE-CHECK 0)
1636 (setf (cast-type-to-check cast) *wild-type*)
1637 (substitute-lvar-uses value arg
1640 (%delete-lvar-use ref)
1641 (add-lvar-use cast lvar)))))
1642 (setf (node-derived-type ref) *wild-type*)
1643 (change-ref-leaf ref (find-constant nil))
1646 (reoptimize-lvar lvar)
1649 ;;; Delete a LET, removing the call and bind nodes, and warning about
1650 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1651 ;;; along right away and delete the REF and then the lambda, since we
1652 ;;; flush the FUN lvar.
1653 (defun delete-let (clambda)
1654 (declare (type clambda clambda))
1655 (aver (functional-letlike-p clambda))
1656 (note-unreferenced-vars clambda)
1657 (let ((call (let-combination clambda)))
1658 (flush-dest (basic-combination-fun call))
1660 (unlink-node (lambda-bind clambda))
1661 (setf (lambda-bind clambda) nil))
1662 (setf (functional-kind clambda) :zombie)
1663 (let ((home (lambda-home clambda)))
1664 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1667 ;;; This function is called when one of the arguments to a LET
1668 ;;; changes. We look at each changed argument. If the corresponding
1669 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1670 ;;; consider substituting for the variable, and also propagate
1671 ;;; derived-type information for the arg to all the VAR's refs.
1673 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1674 ;;; subtype of the argument's leaf type. This prevents type checking
1675 ;;; from being defeated, and also ensures that the best representation
1676 ;;; for the variable can be used.
1678 ;;; Substitution of individual references is inhibited if the
1679 ;;; reference is in a different component from the home. This can only
1680 ;;; happen with closures over top level lambda vars. In such cases,
1681 ;;; the references may have already been compiled, and thus can't be
1682 ;;; retroactively modified.
1684 ;;; If all of the variables are deleted (have no references) when we
1685 ;;; are done, then we delete the LET.
1687 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1689 (defun propagate-let-args (call fun)
1690 (declare (type combination call) (type clambda fun))
1691 (loop for arg in (combination-args call)
1692 and var in (lambda-vars fun) do
1693 (when (and arg (lvar-reoptimize arg))
1694 (setf (lvar-reoptimize arg) nil)
1696 ((lambda-var-sets var)
1697 (propagate-from-sets var (lvar-type arg)))
1698 ((let ((use (lvar-uses arg)))
1700 (let ((leaf (ref-leaf use)))
1701 (when (and (constant-reference-p use)
1702 (csubtypep (leaf-type leaf)
1703 ;; (NODE-DERIVED-TYPE USE) would
1704 ;; be better -- APD, 2003-05-15
1706 (propagate-to-refs var (lvar-type arg))
1707 (let ((use-component (node-component use)))
1708 (prog1 (substitute-leaf-if
1710 (cond ((eq (node-component ref) use-component)
1713 (aver (lambda-toplevelish-p (lambda-home fun)))
1717 ((and (null (rest (leaf-refs var)))
1718 ;; Don't substitute single-ref variables on high-debug /
1719 ;; low speed, to improve the debugging experience.
1720 (policy call (< preserve-single-use-debug-variables 3))
1721 (substitute-single-use-lvar arg var)))
1723 (propagate-to-refs var (lvar-type arg))))))
1725 (when (every #'not (combination-args call))
1730 ;;; This function is called when one of the args to a non-LET local
1731 ;;; call changes. For each changed argument corresponding to an unset
1732 ;;; variable, we compute the union of the types across all calls and
1733 ;;; propagate this type information to the var's refs.
1735 ;;; If the function has an entry-fun, then we don't do anything: since
1736 ;;; it has a XEP we would not discover anything.
1738 ;;; If the function is an optional-entry-point, we will just make sure
1739 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1740 ;;; can erronously propagate too strict types into refs: see
1741 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1743 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1744 ;;; corresponding to changed arguments in CALL, since the only use in
1745 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1747 (defun propagate-local-call-args (call fun)
1748 (declare (type combination call) (type clambda fun))
1749 (unless (functional-entry-fun fun)
1750 (if (lambda-optional-dispatch fun)
1751 ;; We can still make sure &REST is known to be a list.
1752 (loop for var in (lambda-vars fun)
1753 do (let ((info (lambda-var-arg-info var)))
1754 (when (and info (eq :rest (arg-info-kind info)))
1755 (propagate-from-sets var (specifier-type 'list)))))
1757 (let* ((vars (lambda-vars fun))
1758 (union (mapcar (lambda (arg var)
1760 (lvar-reoptimize arg)
1761 (null (basic-var-sets var)))
1763 (basic-combination-args call)
1765 (this-ref (lvar-use (basic-combination-fun call))))
1767 (dolist (arg (basic-combination-args call))
1769 (setf (lvar-reoptimize arg) nil)))
1771 (dolist (ref (leaf-refs fun))
1772 (let ((dest (node-dest ref)))
1773 (unless (or (eq ref this-ref) (not dest))
1775 (mapcar (lambda (this-arg old)
1777 (setf (lvar-reoptimize this-arg) nil)
1778 (type-union (lvar-type this-arg) old)))
1779 (basic-combination-args dest)
1782 (loop for var in vars
1784 when type do (propagate-to-refs var type)))))
1788 ;;;; multiple values optimization
1790 ;;; Do stuff to notice a change to a MV combination node. There are
1791 ;;; two main branches here:
1792 ;;; -- If the call is local, then it is already a MV let, or should
1793 ;;; become one. Note that although all :LOCAL MV calls must eventually
1794 ;;; be converted to :MV-LETs, there can be a window when the call
1795 ;;; is local, but has not been LET converted yet. This is because
1796 ;;; the entry-point lambdas may have stray references (in other
1797 ;;; entry points) that have not been deleted yet.
1798 ;;; -- The call is full. This case is somewhat similar to the non-MV
1799 ;;; combination optimization: we propagate return type information and
1800 ;;; notice non-returning calls. We also have an optimization
1801 ;;; which tries to convert MV-CALLs into MV-binds.
1802 (defun ir1-optimize-mv-combination (node)
1803 (ecase (basic-combination-kind node)
1805 (let ((fun-lvar (basic-combination-fun node)))
1806 (when (lvar-reoptimize fun-lvar)
1807 (setf (lvar-reoptimize fun-lvar) nil)
1808 (maybe-let-convert (combination-lambda node))))
1809 (setf (lvar-reoptimize (first (basic-combination-args node))) nil)
1810 (when (eq (functional-kind (combination-lambda node)) :mv-let)
1811 (unless (convert-mv-bind-to-let node)
1812 (ir1-optimize-mv-bind node))))
1814 (let* ((fun (basic-combination-fun node))
1815 (fun-changed (lvar-reoptimize fun))
1816 (args (basic-combination-args node)))
1818 (setf (lvar-reoptimize fun) nil)
1819 (let ((type (lvar-type fun)))
1820 (when (fun-type-p type)
1821 (derive-node-type node (fun-type-returns type))))
1822 (maybe-terminate-block node nil)
1823 (let ((use (lvar-uses fun)))
1824 (when (and (ref-p use) (functional-p (ref-leaf use)))
1825 (convert-call-if-possible use node)
1826 (when (eq (basic-combination-kind node) :local)
1827 (maybe-let-convert (ref-leaf use))))))
1828 (unless (or (eq (basic-combination-kind node) :local)
1829 (eq (lvar-fun-name fun) '%throw))
1830 (ir1-optimize-mv-call node))
1832 (setf (lvar-reoptimize arg) nil))))
1836 ;;; Propagate derived type info from the values lvar to the vars.
1837 (defun ir1-optimize-mv-bind (node)
1838 (declare (type mv-combination node))
1839 (let* ((arg (first (basic-combination-args node)))
1840 (vars (lambda-vars (combination-lambda node)))
1841 (n-vars (length vars))
1842 (types (values-type-in (lvar-derived-type arg)
1844 (loop for var in vars
1846 do (if (basic-var-sets var)
1847 (propagate-from-sets var type)
1848 (propagate-to-refs var type)))
1849 (setf (lvar-reoptimize arg) nil))
1852 ;;; If possible, convert a general MV call to an MV-BIND. We can do
1854 ;;; -- The call has only one argument, and
1855 ;;; -- The function has a known fixed number of arguments, or
1856 ;;; -- The argument yields a known fixed number of values.
1858 ;;; What we do is change the function in the MV-CALL to be a lambda
1859 ;;; that "looks like an MV bind", which allows
1860 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
1861 ;;; converted (the next time around.) This new lambda just calls the
1862 ;;; actual function with the MV-BIND variables as arguments. Note that
1863 ;;; this new MV bind is not let-converted immediately, as there are
1864 ;;; going to be stray references from the entry-point functions until
1865 ;;; they get deleted.
1867 ;;; In order to avoid loss of argument count checking, we only do the
1868 ;;; transformation according to a known number of expected argument if
1869 ;;; safety is unimportant. We can always convert if we know the number
1870 ;;; of actual values, since the normal call that we build will still
1871 ;;; do any appropriate argument count checking.
1873 ;;; We only attempt the transformation if the called function is a
1874 ;;; constant reference. This allows us to just splice the leaf into
1875 ;;; the new function, instead of trying to somehow bind the function
1876 ;;; expression. The leaf must be constant because we are evaluating it
1877 ;;; again in a different place. This also has the effect of squelching
1878 ;;; multiple warnings when there is an argument count error.
1879 (defun ir1-optimize-mv-call (node)
1880 (let ((fun (basic-combination-fun node))
1881 (*compiler-error-context* node)
1882 (ref (lvar-uses (basic-combination-fun node)))
1883 (args (basic-combination-args node)))
1885 (unless (and (ref-p ref) (constant-reference-p ref)
1887 (return-from ir1-optimize-mv-call))
1889 (multiple-value-bind (min max)
1890 (fun-type-nargs (lvar-type fun))
1892 (multiple-value-bind (types nvals)
1893 (values-types (lvar-derived-type (first args)))
1894 (declare (ignore types))
1895 (if (eq nvals :unknown) nil nvals))))
1898 (when (and min (< total-nvals min))
1900 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1903 (setf (basic-combination-kind node) :error)
1904 (return-from ir1-optimize-mv-call))
1905 (when (and max (> total-nvals max))
1907 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1910 (setf (basic-combination-kind node) :error)
1911 (return-from ir1-optimize-mv-call)))
1913 (let ((count (cond (total-nvals)
1914 ((and (policy node (zerop verify-arg-count))
1919 (with-ir1-environment-from-node node
1920 (let* ((dums (make-gensym-list count))
1922 (leaf (ref-leaf ref))
1923 (fun (ir1-convert-lambda
1924 `(lambda (&optional ,@dums &rest ,ignore)
1925 (declare (ignore ,ignore))
1926 (%funcall ,leaf ,@dums))
1927 :source-name (leaf-%source-name leaf)
1928 :debug-name (leaf-%debug-name leaf))))
1929 (change-ref-leaf ref fun)
1930 (aver (eq (basic-combination-kind node) :full))
1931 (locall-analyze-component *current-component*)
1932 (aver (eq (basic-combination-kind node) :local)))))))))
1936 ;;; (multiple-value-bind
1945 ;;; What we actually do is convert the VALUES combination into a
1946 ;;; normal LET combination calling the original :MV-LET lambda. If
1947 ;;; there are extra args to VALUES, discard the corresponding
1948 ;;; lvars. If there are insufficient args, insert references to NIL.
1949 (defun convert-mv-bind-to-let (call)
1950 (declare (type mv-combination call))
1951 (let* ((arg (first (basic-combination-args call)))
1952 (use (lvar-uses arg)))
1953 (when (and (combination-p use)
1954 (eq (lvar-fun-name (combination-fun use))
1956 (let* ((fun (combination-lambda call))
1957 (vars (lambda-vars fun))
1958 (vals (combination-args use))
1959 (nvars (length vars))
1960 (nvals (length vals)))
1961 (cond ((> nvals nvars)
1962 (mapc #'flush-dest (subseq vals nvars))
1963 (setq vals (subseq vals 0 nvars)))
1965 (with-ir1-environment-from-node use
1966 (let ((node-prev (node-prev use)))
1967 (setf (node-prev use) nil)
1968 (setf (ctran-next node-prev) nil)
1969 (collect ((res vals))
1970 (loop for count below (- nvars nvals)
1971 for prev = node-prev then ctran
1972 for ctran = (make-ctran)
1973 and lvar = (make-lvar use)
1974 do (reference-constant prev ctran lvar nil)
1976 finally (link-node-to-previous-ctran
1978 (setq vals (res)))))))
1979 (setf (combination-args use) vals)
1980 (flush-dest (combination-fun use))
1981 (let ((fun-lvar (basic-combination-fun call)))
1982 (setf (lvar-dest fun-lvar) use)
1983 (setf (combination-fun use) fun-lvar)
1984 (flush-lvar-externally-checkable-type fun-lvar))
1985 (setf (combination-kind use) :local)
1986 (setf (functional-kind fun) :let)
1987 (flush-dest (first (basic-combination-args call)))
1990 (reoptimize-lvar (first vals)))
1991 ;; Propagate derived types from the VALUES call to its args:
1992 ;; transforms can leave the VALUES call with a better type
1993 ;; than its args have, so make sure not to throw that away.
1994 (let ((types (values-type-types (node-derived-type use))))
1997 (let ((type (pop types)))
1998 (assert-lvar-type val type '((type-check . 0)))))))
1999 ;; Propagate declared types of MV-BIND variables.
2000 (propagate-to-args use fun)
2001 (reoptimize-call use))
2005 ;;; (values-list (list x y z))
2010 ;;; In implementation, this is somewhat similar to
2011 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2012 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2013 ;;; (allowing the LIST to be flushed.)
2015 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2016 (defoptimizer (values-list optimizer) ((list) node)
2017 (let ((use (lvar-uses list)))
2018 (when (and (combination-p use)
2019 (eq (lvar-fun-name (combination-fun use))
2022 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2023 (change-ref-leaf (lvar-uses (combination-fun node))
2024 (find-free-fun 'values "in a strange place"))
2025 (setf (combination-kind node) :full)
2026 (let ((args (combination-args use)))
2028 (setf (lvar-dest arg) node)
2029 (flush-lvar-externally-checkable-type arg))
2030 (setf (combination-args use) nil)
2032 (setf (combination-args node) args))
2035 ;;; If VALUES appears in a non-MV context, then effectively convert it
2036 ;;; to a PROG1. This allows the computation of the additional values
2037 ;;; to become dead code.
2038 (deftransform values ((&rest vals) * * :node node)
2039 (unless (lvar-single-value-p (node-lvar node))
2040 (give-up-ir1-transform))
2041 (setf (node-derived-type node)
2042 (make-short-values-type (list (single-value-type
2043 (node-derived-type node)))))
2044 (principal-lvar-single-valuify (node-lvar node))
2046 (let ((dummies (make-gensym-list (length (cdr vals)))))
2047 `(lambda (val ,@dummies)
2048 (declare (ignore ,@dummies))
2054 (defun delete-cast (cast)
2055 (declare (type cast cast))
2056 (let ((value (cast-value cast))
2057 (lvar (node-lvar cast)))
2058 (delete-filter cast lvar value)
2060 (reoptimize-lvar lvar)
2061 (when (lvar-single-value-p lvar)
2062 (note-single-valuified-lvar lvar)))
2065 (defun ir1-optimize-cast (cast &optional do-not-optimize)
2066 (declare (type cast cast))
2067 (let ((value (cast-value cast))
2068 (atype (cast-asserted-type cast)))
2069 (when (not do-not-optimize)
2070 (let ((lvar (node-lvar cast)))
2071 (when (values-subtypep (lvar-derived-type value)
2072 (cast-asserted-type cast))
2074 (return-from ir1-optimize-cast t))
2076 (when (and (listp (lvar-uses value))
2078 ;; Pathwise removing of CAST
2079 (let ((ctran (node-next cast))
2080 (dest (lvar-dest lvar))
2083 (do-uses (use value)
2084 (when (and (values-subtypep (node-derived-type use) atype)
2085 (immediately-used-p value use))
2087 (when ctran (ensure-block-start ctran))
2088 (setq next-block (first (block-succ (node-block cast))))
2089 (ensure-block-start (node-prev cast))
2090 (reoptimize-lvar lvar)
2091 (setf (lvar-%derived-type value) nil))
2092 (%delete-lvar-use use)
2093 (add-lvar-use use lvar)
2094 (unlink-blocks (node-block use) (node-block cast))
2095 (link-blocks (node-block use) next-block)
2096 (when (and (return-p dest)
2097 (basic-combination-p use)
2098 (eq (basic-combination-kind use) :local))
2100 (dolist (use (merges))
2101 (merge-tail-sets use)))))))
2103 (let* ((value-type (lvar-derived-type value))
2104 (int (values-type-intersection value-type atype)))
2105 (derive-node-type cast int)
2106 (when (eq int *empty-type*)
2107 (unless (eq value-type *empty-type*)
2109 ;; FIXME: Do it in one step.
2110 (let ((context (cons (node-source-form cast)
2111 (lvar-source (cast-value cast)))))
2114 (if (cast-single-value-p cast)
2116 `(multiple-value-call #'list 'dummy)))
2119 ;; FIXME: Derived type.
2120 `(%compile-time-type-error 'dummy
2121 ',(type-specifier atype)
2122 ',(type-specifier value-type)
2124 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2125 ;; functions, so we declare the return type of
2126 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2128 (setq value (cast-value cast))
2129 (derive-node-type (lvar-uses value) *empty-type*)
2130 (maybe-terminate-block (lvar-uses value) nil)
2131 ;; FIXME: Is it necessary?
2132 (aver (null (block-pred (node-block cast))))
2133 (delete-block-lazily (node-block cast))
2134 (return-from ir1-optimize-cast)))
2135 (when (eq (node-derived-type cast) *empty-type*)
2136 (maybe-terminate-block cast nil))
2138 (when (and (cast-%type-check cast)
2139 (values-subtypep value-type
2140 (cast-type-to-check cast)))
2141 (setf (cast-%type-check cast) nil))))
2143 (unless do-not-optimize
2144 (setf (node-reoptimize cast) nil)))
2146 (deftransform make-symbol ((string) (simple-string))
2147 `(%make-symbol string))