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 (let ((use (principal-lvar-use thing)))
27 (and (ref-p use) (constant-p (ref-leaf use))))))
29 ;;; Return the constant value for an LVAR whose only use is a constant
31 (declaim (ftype (function (lvar) t) lvar-value))
32 (defun lvar-value (lvar)
33 (let ((use (principal-lvar-use lvar)))
34 (constant-value (ref-leaf use))))
36 ;;;; interface for obtaining results of type inference
38 ;;; Our best guess for the type of this lvar's value. Note that this
39 ;;; may be VALUES or FUNCTION type, which cannot be passed as an
40 ;;; argument to the normal type operations. See LVAR-TYPE.
42 ;;; The result value is cached in the LVAR-%DERIVED-TYPE slot. If the
43 ;;; slot is true, just return that value, otherwise recompute and
44 ;;; stash the value there.
45 #!-sb-fluid (declaim (inline lvar-derived-type))
46 (defun lvar-derived-type (lvar)
47 (declare (type lvar lvar))
48 (or (lvar-%derived-type lvar)
49 (setf (lvar-%derived-type lvar)
50 (%lvar-derived-type lvar))))
51 (defun %lvar-derived-type (lvar)
52 (declare (type lvar lvar))
53 (let ((uses (lvar-uses lvar)))
54 (cond ((null uses) *empty-type*)
56 (do ((res (node-derived-type (first uses))
57 (values-type-union (node-derived-type (first current))
59 (current (rest uses) (rest current)))
60 ((null current) res)))
62 (node-derived-type (lvar-uses lvar))))))
64 ;;; Return the derived type for LVAR's first value. This is guaranteed
65 ;;; not to be a VALUES or FUNCTION type.
66 (declaim (ftype (sfunction (lvar) ctype) lvar-type))
67 (defun lvar-type (lvar)
68 (single-value-type (lvar-derived-type lvar)))
70 ;;; If LVAR is an argument of a function, return a type which the
71 ;;; function checks LVAR for.
72 #!-sb-fluid (declaim (inline lvar-externally-checkable-type))
73 (defun lvar-externally-checkable-type (lvar)
74 (or (lvar-%externally-checkable-type lvar)
75 (%lvar-%externally-checkable-type lvar)))
76 (defun %lvar-%externally-checkable-type (lvar)
77 (declare (type lvar lvar))
78 (let ((dest (lvar-dest lvar)))
79 (if (not (and dest (combination-p dest)))
80 ;; TODO: MV-COMBINATION
81 (setf (lvar-%externally-checkable-type lvar) *wild-type*)
82 (let* ((fun (combination-fun dest))
83 (args (combination-args dest))
84 (fun-type (lvar-type fun)))
85 (setf (lvar-%externally-checkable-type fun) *wild-type*)
86 (if (or (not (call-full-like-p dest))
87 (not (fun-type-p fun-type))
88 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
89 (fun-type-wild-args fun-type))
92 (setf (lvar-%externally-checkable-type arg)
94 (map-combination-args-and-types
96 (setf (lvar-%externally-checkable-type arg)
97 (acond ((lvar-%externally-checkable-type arg)
98 (values-type-intersection
99 it (coerce-to-values type)))
100 (t (coerce-to-values type)))))
102 (lvar-%externally-checkable-type lvar))
103 #!-sb-fluid(declaim (inline flush-lvar-externally-checkable-type))
104 (defun flush-lvar-externally-checkable-type (lvar)
105 (declare (type lvar lvar))
106 (setf (lvar-%externally-checkable-type lvar) nil))
108 ;;;; interface routines used by optimizers
110 ;;; This function is called by optimizers to indicate that something
111 ;;; interesting has happened to the value of LVAR. Optimizers must
112 ;;; make sure that they don't call for reoptimization when nothing has
113 ;;; happened, since optimization will fail to terminate.
115 ;;; We clear any cached type for the lvar and set the reoptimize flags
116 ;;; on everything in sight.
117 (defun reoptimize-lvar (lvar)
118 (declare (type (or lvar null) lvar))
120 (setf (lvar-%derived-type lvar) nil)
121 (let ((dest (lvar-dest lvar)))
123 (setf (lvar-reoptimize lvar) t)
124 (setf (node-reoptimize dest) t)
125 (binding* (;; Since this may be called during IR1 conversion,
126 ;; PREV may be missing.
127 (prev (node-prev dest) :exit-if-null)
128 (block (ctran-block prev))
129 (component (block-component block)))
130 (when (typep dest 'cif)
131 (setf (block-test-modified block) t))
132 (setf (block-reoptimize block) t)
133 (setf (component-reoptimize component) t))))
135 (setf (block-type-check (node-block node)) t)))
138 (defun reoptimize-lvar-uses (lvar)
139 (declare (type lvar lvar))
141 (setf (node-reoptimize use) t)
142 (setf (block-reoptimize (node-block use)) t)
143 (setf (component-reoptimize (node-component use)) t)))
145 ;;; Annotate NODE to indicate that its result has been proven to be
146 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
147 ;;; only correct way to supply information discovered about a node's
148 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
149 ;;; information may be lost and reoptimization may not happen.
151 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
152 ;;; intersection is different from the old type, then we do a
153 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
154 (defun derive-node-type (node rtype)
155 (declare (type valued-node node) (type ctype rtype))
156 (let ((node-type (node-derived-type node)))
157 (unless (eq node-type rtype)
158 (let ((int (values-type-intersection node-type rtype))
159 (lvar (node-lvar node)))
160 (when (type/= node-type int)
161 (when (and *check-consistency*
162 (eq int *empty-type*)
163 (not (eq rtype *empty-type*)))
164 (let ((*compiler-error-context* node))
166 "New inferred type ~S conflicts with old type:~
167 ~% ~S~%*** possible internal error? Please report this."
168 (type-specifier rtype) (type-specifier node-type))))
169 (setf (node-derived-type node) int)
170 ;; If the new type consists of only one object, replace the
171 ;; node with a constant reference.
172 (when (and (ref-p node)
173 (lambda-var-p (ref-leaf node)))
174 (let ((type (single-value-type int)))
175 (when (and (member-type-p type)
176 (null (rest (member-type-members type))))
177 (change-ref-leaf node (find-constant
178 (first (member-type-members type)))))))
179 (reoptimize-lvar lvar)))))
182 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
183 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
184 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
185 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
186 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
187 (defun assert-lvar-type (lvar type policy)
188 (declare (type lvar lvar) (type ctype type))
189 (unless (values-subtypep (lvar-derived-type lvar) type)
190 (let* ((dest (lvar-dest lvar))
191 (ctran (node-prev dest)))
192 (with-ir1-environment-from-node dest
193 (let* ((cast (make-cast lvar type policy))
194 (internal-lvar (make-lvar))
195 (internal-ctran (make-ctran)))
196 (setf (ctran-next ctran) cast
197 (node-prev cast) ctran)
198 (use-continuation cast internal-ctran internal-lvar)
199 (link-node-to-previous-ctran dest internal-ctran)
200 (substitute-lvar internal-lvar lvar)
201 (setf (lvar-dest lvar) cast)
202 (reoptimize-lvar lvar)
203 (when (return-p dest)
204 (node-ends-block cast))
205 (setf (block-attributep (block-flags (node-block cast))
206 type-check type-asserted)
212 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
213 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
214 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
215 ;;; we are done, then another iteration would be beneficial.
216 (defun ir1-optimize (component)
217 (declare (type component component))
218 (setf (component-reoptimize component) nil)
219 (loop with block = (block-next (component-head component))
220 with tail = (component-tail component)
221 for last-block = block
222 until (eq block tail)
224 ;; We delete blocks when there is either no predecessor or the
225 ;; block is in a lambda that has been deleted. These blocks
226 ;; would eventually be deleted by DFO recomputation, but doing
227 ;; it here immediately makes the effect available to IR1
229 ((or (block-delete-p block)
230 (null (block-pred block)))
231 (delete-block-lazily block)
232 (setq block (clean-component component block)))
233 ((eq (functional-kind (block-home-lambda block)) :deleted)
234 ;; Preserve the BLOCK-SUCC invariant that almost every block has
235 ;; one successor (and a block with DELETE-P set is an acceptable
237 (mark-for-deletion block)
238 (setq block (clean-component component block)))
241 (let ((succ (block-succ block)))
242 (unless (singleton-p succ)
245 (let ((last (block-last block)))
248 (flush-dest (if-test last))
249 (when (unlink-node last)
252 (when (maybe-delete-exit last)
255 (unless (join-successor-if-possible block)
258 (when (and (block-reoptimize block) (block-component block))
259 (aver (not (block-delete-p block)))
260 (ir1-optimize-block block))
262 (cond ((and (block-delete-p block) (block-component block))
263 (setq block (clean-component component block)))
264 ((and (block-flush-p block) (block-component block))
265 (flush-dead-code block)))))
266 do (when (eq block last-block)
267 (setq block (block-next block))))
271 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
274 ;;; Note that although they are cleared here, REOPTIMIZE flags might
275 ;;; still be set upon return from this function, meaning that further
276 ;;; optimization is wanted (as a consequence of optimizations we did).
277 (defun ir1-optimize-block (block)
278 (declare (type cblock block))
279 ;; We clear the node and block REOPTIMIZE flags before doing the
280 ;; optimization, not after. This ensures that the node or block will
281 ;; be reoptimized if necessary.
282 (setf (block-reoptimize block) nil)
283 (do-nodes (node nil block :restart-p t)
284 (when (node-reoptimize node)
285 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
286 (setf (node-reoptimize node) nil)
290 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
291 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
292 ;; any argument changes.
293 (ir1-optimize-combination node))
295 (ir1-optimize-if node))
297 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
298 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
299 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
301 (setf (node-reoptimize node) t)
302 (ir1-optimize-return node))
304 (ir1-optimize-mv-combination node))
306 ;; With an EXIT, we derive the node's type from the VALUE's
308 (let ((value (exit-value node)))
310 (derive-node-type node (lvar-derived-type value)))))
312 (ir1-optimize-set node))
314 (ir1-optimize-cast node)))))
318 ;;; Try to join with a successor block. If we succeed, we return true,
320 (defun join-successor-if-possible (block)
321 (declare (type cblock block))
322 (let ((next (first (block-succ block))))
323 (when (block-start next) ; NEXT is not an END-OF-COMPONENT marker
324 (cond ( ;; We cannot combine with a successor block if:
326 ;; the successor has more than one predecessor;
327 (rest (block-pred next))
328 ;; the successor is the current block (infinite loop);
330 ;; the next block has a different cleanup, and thus
331 ;; we may want to insert cleanup code between the
332 ;; two blocks at some point;
333 (not (eq (block-end-cleanup block)
334 (block-start-cleanup next)))
335 ;; the next block has a different home lambda, and
336 ;; thus the control transfer is a non-local exit.
337 (not (eq (block-home-lambda block)
338 (block-home-lambda next)))
339 ;; Stack analysis phase wants ENTRY to start a block.
340 (entry-p (block-start-node next)))
343 (join-blocks block next)
346 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
347 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
348 ;;; for the two blocks so that any indicated optimization gets done.
349 (defun join-blocks (block1 block2)
350 (declare (type cblock block1 block2))
351 (let* ((last1 (block-last block1))
352 (last2 (block-last block2))
353 (succ (block-succ block2))
354 (start2 (block-start block2)))
355 (do ((ctran start2 (node-next (ctran-next ctran))))
357 (setf (ctran-block ctran) block1))
359 (unlink-blocks block1 block2)
361 (unlink-blocks block2 block)
362 (link-blocks block1 block))
364 (setf (ctran-kind start2) :inside-block)
365 (setf (node-next last1) start2)
366 (setf (ctran-use start2) last1)
367 (setf (block-last block1) last2))
369 (setf (block-flags block1)
370 (attributes-union (block-flags block1)
372 (block-attributes type-asserted test-modified)))
374 (let ((next (block-next block2))
375 (prev (block-prev block2)))
376 (setf (block-next prev) next)
377 (setf (block-prev next) prev))
381 ;;; Delete any nodes in BLOCK whose value is unused and which have no
382 ;;; side effects. We can delete sets of lexical variables when the set
383 ;;; variable has no references.
384 (defun flush-dead-code (block)
385 (declare (type cblock block))
386 (setf (block-flush-p block) nil)
387 (do-nodes-backwards (node lvar block :restart-p t)
394 (let ((info (combination-kind node)))
395 (when (fun-info-p info)
396 (let ((attr (fun-info-attributes info)))
397 (when (and (not (ir1-attributep attr call))
398 ;; ### For now, don't delete potentially
399 ;; flushable calls when they have the CALL
400 ;; attribute. Someday we should look at the
401 ;; functional args to determine if they have
403 (if (policy node (= safety 3))
404 (ir1-attributep attr flushable)
405 (ir1-attributep attr unsafely-flushable)))
406 (flush-combination node))))))
408 (when (eq (basic-combination-kind node) :local)
409 (let ((fun (combination-lambda node)))
410 (when (dolist (var (lambda-vars fun) t)
411 (when (or (leaf-refs var)
412 (lambda-var-sets var))
414 (flush-dest (first (basic-combination-args node)))
417 (let ((value (exit-value node)))
420 (setf (exit-value node) nil))))
422 (let ((var (set-var node)))
423 (when (and (lambda-var-p var)
424 (null (leaf-refs var)))
425 (flush-dest (set-value node))
426 (setf (basic-var-sets var)
427 (delq node (basic-var-sets var)))
428 (unlink-node node))))
430 (unless (cast-type-check node)
431 (flush-dest (cast-value node))
432 (unlink-node node))))))
436 ;;;; local call return type propagation
438 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
439 ;;; flag set. It iterates over the uses of the RESULT, looking for
440 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
441 ;;; call, then we union its type together with the types of other such
442 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
443 ;;; type with the RESULT's asserted type. We can make this
444 ;;; intersection now (potentially before type checking) because this
445 ;;; assertion on the result will eventually be checked (if
448 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
449 ;;; combination, which may change the succesor of the call to be the
450 ;;; called function, and if so, checks if the call can become an
451 ;;; assignment. If we convert to an assignment, we abort, since the
452 ;;; RETURN has been deleted.
453 (defun find-result-type (node)
454 (declare (type creturn node))
455 (let ((result (return-result node)))
456 (collect ((use-union *empty-type* values-type-union))
457 (do-uses (use result)
458 (let ((use-home (node-home-lambda use)))
459 (cond ((or (eq (functional-kind use-home) :deleted)
460 (block-delete-p (node-block use))))
461 ((and (basic-combination-p use)
462 (eq (basic-combination-kind use) :local))
463 (aver (eq (lambda-tail-set use-home)
464 (lambda-tail-set (combination-lambda use))))
465 (when (combination-p use)
466 (when (nth-value 1 (maybe-convert-tail-local-call use))
467 (return-from find-result-type t))))
469 (use-union (node-derived-type use))))))
471 ;; (values-type-intersection
472 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
476 (setf (return-result-type node) int))))
479 ;;; Do stuff to realize that something has changed about the value
480 ;;; delivered to a return node. Since we consider the return values of
481 ;;; all functions in the tail set to be equivalent, this amounts to
482 ;;; bringing the entire tail set up to date. We iterate over the
483 ;;; returns for all the functions in the tail set, reanalyzing them
484 ;;; all (not treating NODE specially.)
486 ;;; When we are done, we check whether the new type is different from
487 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
488 ;;; all the lvars for references to functions in the tail set. This
489 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
490 ;;; results of the calls.
491 (defun ir1-optimize-return (node)
492 (declare (type creturn node))
495 (let* ((tails (lambda-tail-set (return-lambda node)))
496 (funs (tail-set-funs tails)))
497 (collect ((res *empty-type* values-type-union))
499 (let ((return (lambda-return fun)))
501 (when (node-reoptimize return)
502 (setf (node-reoptimize return) nil)
503 (when (find-result-type return)
505 (res (return-result-type return)))))
507 (when (type/= (res) (tail-set-type tails))
508 (setf (tail-set-type tails) (res))
509 (dolist (fun (tail-set-funs tails))
510 (dolist (ref (leaf-refs fun))
511 (reoptimize-lvar (node-lvar ref))))))))
517 ;;; If the test has multiple uses, replicate the node when possible.
518 ;;; Also check whether the predicate is known to be true or false,
519 ;;; deleting the IF node in favor of the appropriate branch when this
521 (defun ir1-optimize-if (node)
522 (declare (type cif node))
523 (let ((test (if-test node))
524 (block (node-block node)))
526 (when (and (eq (block-start-node block) node)
527 (listp (lvar-uses test)))
529 (when (immediately-used-p test use)
530 (convert-if-if use node)
531 (when (not (listp (lvar-uses test))) (return)))))
533 (let* ((type (lvar-type test))
535 (cond ((constant-lvar-p test)
536 (if (lvar-value test)
537 (if-alternative node)
538 (if-consequent node)))
539 ((not (types-equal-or-intersect type (specifier-type 'null)))
540 (if-alternative node))
541 ((type= type (specifier-type 'null))
542 (if-consequent node)))))
545 (when (rest (block-succ block))
546 (unlink-blocks block victim))
547 (setf (component-reanalyze (node-component node)) t)
548 (unlink-node node))))
551 ;;; Create a new copy of an IF node that tests the value of the node
552 ;;; USE. The test must have >1 use, and must be immediately used by
553 ;;; USE. NODE must be the only node in its block (implying that
554 ;;; block-start = if-test).
556 ;;; This optimization has an effect semantically similar to the
557 ;;; source-to-source transformation:
558 ;;; (IF (IF A B C) D E) ==>
559 ;;; (IF A (IF B D E) (IF C D E))
561 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
562 ;;; node so that dead code deletion notes will definitely not consider
563 ;;; either node to be part of the original source. One node might
564 ;;; become unreachable, resulting in a spurious note.
565 (defun convert-if-if (use node)
566 (declare (type node use) (type cif node))
567 (with-ir1-environment-from-node node
568 (let* ((block (node-block node))
569 (test (if-test node))
570 (cblock (if-consequent node))
571 (ablock (if-alternative node))
572 (use-block (node-block use))
573 (new-ctran (make-ctran))
574 (new-lvar (make-lvar))
575 (new-node (make-if :test new-lvar
577 :alternative ablock))
578 (new-block (ctran-starts-block new-ctran)))
579 (link-node-to-previous-ctran new-node new-ctran)
580 (setf (lvar-dest new-lvar) new-node)
581 (setf (block-last new-block) new-node)
583 (unlink-blocks use-block block)
584 (%delete-lvar-use use)
585 (add-lvar-use use new-lvar)
586 (link-blocks use-block new-block)
588 (link-blocks new-block cblock)
589 (link-blocks new-block ablock)
591 (push "<IF Duplication>" (node-source-path node))
592 (push "<IF Duplication>" (node-source-path new-node))
594 (reoptimize-lvar test)
595 (reoptimize-lvar new-lvar)
596 (setf (component-reanalyze *current-component*) t)))
599 ;;;; exit IR1 optimization
601 ;;; This function attempts to delete an exit node, returning true if
602 ;;; it deletes the block as a consequence:
603 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
604 ;;; anything, since there is nothing to be done.
605 ;;; -- If the exit node and its ENTRY have the same home lambda then
606 ;;; we know the exit is local, and can delete the exit. We change
607 ;;; uses of the Exit-Value to be uses of the original lvar,
608 ;;; then unlink the node. If the exit is to a TR context, then we
609 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
610 ;;; their value to this exit.
611 ;;; -- If there is no value (as in a GO), then we skip the value
614 ;;; This function is also called by environment analysis, since it
615 ;;; wants all exits to be optimized even if normal optimization was
617 (defun maybe-delete-exit (node)
618 (declare (type exit node))
619 (let ((value (exit-value node))
620 (entry (exit-entry node)))
622 (eq (node-home-lambda node) (node-home-lambda entry)))
623 (setf (entry-exits entry) (delq node (entry-exits entry)))
625 (delete-filter node (node-lvar node) value)
626 (unlink-node node)))))
629 ;;;; combination IR1 optimization
631 ;;; Report as we try each transform?
633 (defvar *show-transforms-p* nil)
635 ;;; Do IR1 optimizations on a COMBINATION node.
636 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
637 (defun ir1-optimize-combination (node)
638 (when (lvar-reoptimize (basic-combination-fun node))
639 (propagate-fun-change node)
640 (maybe-terminate-block node nil))
641 (let ((args (basic-combination-args node))
642 (kind (basic-combination-kind node)))
645 (let ((fun (combination-lambda node)))
646 (if (eq (functional-kind fun) :let)
647 (propagate-let-args node fun)
648 (propagate-local-call-args node fun))))
652 (setf (lvar-reoptimize arg) nil))))
656 (setf (lvar-reoptimize arg) nil)))
658 (let ((attr (fun-info-attributes kind)))
659 (when (and (ir1-attributep attr foldable)
660 ;; KLUDGE: The next test could be made more sensitive,
661 ;; only suppressing constant-folding of functions with
662 ;; CALL attributes when they're actually passed
663 ;; function arguments. -- WHN 19990918
664 (not (ir1-attributep attr call))
665 (every #'constant-lvar-p args)
667 ;; Even if the function is foldable in principle,
668 ;; it might be one of our low-level
669 ;; implementation-specific functions. Such
670 ;; functions don't necessarily exist at runtime on
671 ;; a plain vanilla ANSI Common Lisp
672 ;; cross-compilation host, in which case the
673 ;; cross-compiler can't fold it because the
674 ;; cross-compiler doesn't know how to evaluate it.
676 (or (fboundp (combination-fun-source-name node))
677 (progn (format t ";;; !!! Unbound fun: (~S~{ ~S~})~%"
678 (combination-fun-source-name node)
679 (mapcar #'lvar-value args))
681 (constant-fold-call node)
682 (return-from ir1-optimize-combination)))
684 (let ((fun (fun-info-derive-type kind)))
686 (let ((res (funcall fun node)))
688 (derive-node-type node (coerce-to-values res))
689 (maybe-terminate-block node nil)))))
691 (let ((fun (fun-info-optimizer kind)))
692 (unless (and fun (funcall fun node))
693 (dolist (x (fun-info-transforms kind))
695 (when *show-transforms-p*
696 (let* ((lvar (basic-combination-fun node))
697 (fname (lvar-fun-name lvar t)))
698 (/show "trying transform" x (transform-function x) "for" fname)))
699 (unless (ir1-transform node x)
701 (when *show-transforms-p*
702 (/show "quitting because IR1-TRANSFORM result was NIL"))
707 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
708 ;;; the block there, and link it to the component tail.
710 ;;; Except when called during IR1 convertion, we delete the
711 ;;; continuation if it has no other uses. (If it does have other uses,
714 ;;; Termination on the basis of a continuation type is
716 ;;; -- The continuation is deleted (hence the assertion is spurious), or
717 ;;; -- We are in IR1 conversion (where THE assertions are subject to
718 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
719 ;;; uses can(?) be added later. -- APD, 2003-07-17
721 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
722 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p)
723 (declare (type (or basic-combination cast) node))
724 (let* ((block (node-block node))
725 (lvar (node-lvar node))
726 (ctran (node-next node))
727 (tail (component-tail (block-component block)))
728 (succ (first (block-succ block))))
729 (unless (or (and (eq node (block-last block)) (eq succ tail))
730 (block-delete-p block))
731 (when (eq (node-derived-type node) *empty-type*)
732 (cond (ir1-converting-not-optimizing-p
735 (aver (eq (block-last block) node)))
737 (setf (block-last block) node)
738 (setf (ctran-use ctran) nil)
739 (setf (ctran-kind ctran) :unused)
740 (setf (ctran-block ctran) nil)
741 (setf (node-next node) nil)
742 (link-blocks block (ctran-starts-block ctran)))))
744 (node-ends-block node)))
746 (unlink-blocks block (first (block-succ block)))
747 (setf (component-reanalyze (block-component block)) t)
748 (aver (not (block-succ block)))
749 (link-blocks block tail)
750 (if ir1-converting-not-optimizing-p
751 (%delete-lvar-use node)
752 (delete-lvar-use node))
755 ;;; This is called both by IR1 conversion and IR1 optimization when
756 ;;; they have verified the type signature for the call, and are
757 ;;; wondering if something should be done to special-case the call. If
758 ;;; CALL is a call to a global function, then see whether it defined
760 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
761 ;;; the expansion and change the call to call it. Expansion is
762 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
763 ;;; true, we never expand, since this function has already been
764 ;;; converted. Local call analysis will duplicate the definition
765 ;;; if necessary. We claim that the parent form is LABELS for
766 ;;; context declarations, since we don't want it to be considered
767 ;;; a real global function.
768 ;;; -- If it is a known function, mark it as such by setting the KIND.
770 ;;; We return the leaf referenced (NIL if not a leaf) and the
771 ;;; FUN-INFO assigned.
772 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
773 (declare (type combination call))
774 (let* ((ref (lvar-uses (basic-combination-fun call)))
775 (leaf (when (ref-p ref) (ref-leaf ref)))
776 (inlinep (if (defined-fun-p leaf)
777 (defined-fun-inlinep leaf)
780 ((eq inlinep :notinline) (values nil nil))
781 ((not (and (global-var-p leaf)
782 (eq (global-var-kind leaf) :global-function)))
787 ((nil :maybe-inline) (policy call (zerop space))))
789 (defined-fun-inline-expansion leaf)
790 (let ((fun (defined-fun-functional leaf)))
792 (and (eq inlinep :inline) (functional-kind fun))))
793 (inline-expansion-ok call))
794 (flet (;; FIXME: Is this what the old CMU CL internal documentation
795 ;; called semi-inlining? A more descriptive name would
796 ;; be nice. -- WHN 2002-01-07
798 (let ((res (ir1-convert-lambda-for-defun
799 (defined-fun-inline-expansion leaf)
801 #'ir1-convert-inline-lambda)))
802 (setf (defined-fun-functional leaf) res)
803 (change-ref-leaf ref res))))
804 (if ir1-converting-not-optimizing-p
806 (with-ir1-environment-from-node call
808 (locall-analyze-component *current-component*))))
810 (values (ref-leaf (lvar-uses (basic-combination-fun call)))
813 (let ((info (info :function :info (leaf-source-name leaf))))
815 (values leaf (setf (basic-combination-kind call) info))
816 (values leaf nil)))))))
818 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
819 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
820 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
821 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
822 ;;; syntax check, arg/result type processing, but still call
823 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
824 ;;; and that checking is done by local call analysis.
825 (defun validate-call-type (call type ir1-converting-not-optimizing-p)
826 (declare (type combination call) (type ctype type))
827 (cond ((not (fun-type-p type))
828 (aver (multiple-value-bind (val win)
829 (csubtypep type (specifier-type 'function))
831 (recognize-known-call call ir1-converting-not-optimizing-p))
832 ((valid-fun-use call type
833 :argument-test #'always-subtypep
835 ;; KLUDGE: Common Lisp is such a dynamic
836 ;; language that all we can do here in
837 ;; general is issue a STYLE-WARNING. It
838 ;; would be nice to issue a full WARNING
839 ;; in the special case of of type
840 ;; mismatches within a compilation unit
841 ;; (as in section 3.2.2.3 of the spec)
842 ;; but at least as of sbcl-0.6.11, we
843 ;; don't keep track of whether the
844 ;; mismatched data came from the same
845 ;; compilation unit, so we can't do that.
848 ;; FIXME: Actually, I think we could
849 ;; issue a full WARNING if the call
850 ;; violates a DECLAIM FTYPE.
851 :lossage-fun #'compiler-style-warn
852 :unwinnage-fun #'compiler-notify)
853 (assert-call-type call type)
854 (maybe-terminate-block call ir1-converting-not-optimizing-p)
855 (recognize-known-call call ir1-converting-not-optimizing-p))
857 (setf (combination-kind call) :error)
860 ;;; This is called by IR1-OPTIMIZE when the function for a call has
861 ;;; changed. If the call is local, we try to LET-convert it, and
862 ;;; derive the result type. If it is a :FULL call, we validate it
863 ;;; against the type, which recognizes known calls, does inline
864 ;;; expansion, etc. If a call to a predicate in a non-conditional
865 ;;; position or to a function with a source transform, then we
866 ;;; reconvert the form to give IR1 another chance.
867 (defun propagate-fun-change (call)
868 (declare (type combination call))
869 (let ((*compiler-error-context* call)
870 (fun-lvar (basic-combination-fun call)))
871 (setf (lvar-reoptimize fun-lvar) nil)
872 (case (combination-kind call)
874 (let ((fun (combination-lambda call)))
875 (maybe-let-convert fun)
876 (unless (member (functional-kind fun) '(:let :assignment :deleted))
877 (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
879 (multiple-value-bind (leaf info)
880 (validate-call-type call (lvar-type fun-lvar) nil)
881 (cond ((functional-p leaf)
882 (convert-call-if-possible
883 (lvar-uses (basic-combination-fun call))
886 ((and (leaf-has-source-name-p leaf)
887 (or (info :function :source-transform (leaf-source-name leaf))
889 (ir1-attributep (fun-info-attributes info)
891 (let ((lvar (node-lvar call)))
892 (and lvar (not (if-p (lvar-dest lvar))))))))
893 (let ((name (leaf-source-name leaf))
894 (dummies (make-gensym-list
895 (length (combination-args call)))))
898 (,@(if (symbolp name)
902 (leaf-source-name leaf)))))))))
905 ;;;; known function optimization
907 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
908 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
909 ;;; replace it, otherwise add a new one.
910 (defun record-optimization-failure (node transform args)
911 (declare (type combination node) (type transform transform)
912 (type (or fun-type list) args))
913 (let* ((table (component-failed-optimizations *component-being-compiled*))
914 (found (assoc transform (gethash node table))))
916 (setf (cdr found) args)
917 (push (cons transform args) (gethash node table))))
920 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
921 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
922 ;;; doing the transform for some reason and FLAME is true, then we
923 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
924 ;;; finalize to pick up. We return true if the transform failed, and
925 ;;; thus further transformation should be attempted. We return false
926 ;;; if either the transform succeeded or was aborted.
927 (defun ir1-transform (node transform)
928 (declare (type combination node) (type transform transform))
929 (let* ((type (transform-type transform))
930 (fun (transform-function transform))
931 (constrained (fun-type-p type))
932 (table (component-failed-optimizations *component-being-compiled*))
933 (flame (if (transform-important transform)
934 (policy node (>= speed inhibit-warnings))
935 (policy node (> speed inhibit-warnings))))
936 (*compiler-error-context* node))
937 (cond ((or (not constrained)
938 (valid-fun-use node type))
939 (multiple-value-bind (severity args)
940 (catch 'give-up-ir1-transform
943 (combination-fun-source-name node))
950 (setf (combination-kind node) :error)
952 (apply #'compiler-warn args))
958 (record-optimization-failure node transform args))
959 (setf (gethash node table)
960 (remove transform (gethash node table) :key #'car)))
968 :argument-test #'types-equal-or-intersect
969 :result-test #'values-types-equal-or-intersect))
970 (record-optimization-failure node transform type)
975 ;;; When we don't like an IR1 transform, we throw the severity/reason
978 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
979 ;;; aborting this attempt to transform the call, but admitting the
980 ;;; possibility that this or some other transform will later succeed.
981 ;;; If arguments are supplied, they are format arguments for an
984 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
985 ;;; force a normal call to the function at run time. No further
986 ;;; optimizations will be attempted.
988 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
989 ;;; delay the transform on the node until later. REASONS specifies
990 ;;; when the transform will be later retried. The :OPTIMIZE reason
991 ;;; causes the transform to be delayed until after the current IR1
992 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
993 ;;; be delayed until after constraint propagation.
995 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
996 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
997 ;;; do CASE operations on the various REASON values, it might be a
998 ;;; good idea to go OO, representing the reasons by objects, using
999 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1000 ;;; SIGNAL instead of THROW.
1001 (declaim (ftype (function (&rest t) nil) give-up-ir1-transform))
1002 (defun give-up-ir1-transform (&rest args)
1003 (throw 'give-up-ir1-transform (values :failure args)))
1004 (defun abort-ir1-transform (&rest args)
1005 (throw 'give-up-ir1-transform (values :aborted args)))
1006 (defun delay-ir1-transform (node &rest reasons)
1007 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1009 (setf *delayed-ir1-transforms*
1010 (acons node reasons *delayed-ir1-transforms*))
1011 (throw 'give-up-ir1-transform :delayed))
1013 (dolist (reason reasons)
1014 (pushnew reason (cdr assoc)))
1015 (throw 'give-up-ir1-transform :delayed)))))
1017 ;;; Clear any delayed transform with no reasons - these should have
1018 ;;; been tried in the last pass. Then remove the reason from the
1019 ;;; delayed transform reasons, and if any become empty then set
1020 ;;; reoptimize flags for the node. Return true if any transforms are
1022 (defun retry-delayed-ir1-transforms (reason)
1023 (setf *delayed-ir1-transforms*
1024 (remove-if-not #'cdr *delayed-ir1-transforms*))
1025 (let ((reoptimize nil))
1026 (dolist (assoc *delayed-ir1-transforms*)
1027 (let ((reasons (remove reason (cdr assoc))))
1028 (setf (cdr assoc) reasons)
1030 (let ((node (car assoc)))
1031 (unless (node-deleted node)
1033 (setf (node-reoptimize node) t)
1034 (let ((block (node-block node)))
1035 (setf (block-reoptimize block) t)
1036 (setf (component-reoptimize (block-component block)) t)))))))
1039 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1040 ;;; environment, and then install it as the function for the call
1041 ;;; NODE. We do local call analysis so that the new function is
1042 ;;; integrated into the control flow.
1044 ;;; We require the original function source name in order to generate
1045 ;;; a meaningful debug name for the lambda we set up. (It'd be
1046 ;;; possible to do this starting from debug names as well as source
1047 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1048 ;;; generality, since source names are always known to our callers.)
1049 (defun transform-call (call res source-name)
1050 (declare (type combination call) (list res))
1051 (aver (and (legal-fun-name-p source-name)
1052 (not (eql source-name '.anonymous.))))
1053 (node-ends-block call)
1054 (with-ir1-environment-from-node call
1055 (with-component-last-block (*current-component*
1056 (block-next (node-block call)))
1057 (let ((new-fun (ir1-convert-inline-lambda
1059 :debug-name (debug-namify "LAMBDA-inlined ~A"
1062 "<unknown function>"))))
1063 (ref (lvar-use (combination-fun call))))
1064 (change-ref-leaf ref new-fun)
1065 (setf (combination-kind call) :full)
1066 (locall-analyze-component *current-component*))))
1069 ;;; Replace a call to a foldable function of constant arguments with
1070 ;;; the result of evaluating the form. If there is an error during the
1071 ;;; evaluation, we give a warning and leave the call alone, making the
1072 ;;; call a :ERROR call.
1074 ;;; If there is more than one value, then we transform the call into a
1076 (defun constant-fold-call (call)
1077 (let ((args (mapcar #'lvar-value (combination-args call)))
1078 (fun-name (combination-fun-source-name call)))
1079 (multiple-value-bind (values win)
1080 (careful-call fun-name
1083 ;; Note: CMU CL had COMPILER-WARN here, and that
1084 ;; seems more natural, but it's probably not.
1086 ;; It's especially not while bug 173 exists:
1089 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1091 ;; can cause constant-folding TYPE-ERRORs (in
1092 ;; #'<=) when END can be proved to be NIL, even
1093 ;; though the code is perfectly legal and safe
1094 ;; because a NIL value of END means that the
1095 ;; #'<= will never be executed.
1097 ;; Moreover, even without bug 173,
1098 ;; quite-possibly-valid code like
1099 ;; (COND ((NONINLINED-PREDICATE END)
1100 ;; (UNLESS (<= END SIZE))
1102 ;; (where NONINLINED-PREDICATE is something the
1103 ;; compiler can't do at compile time, but which
1104 ;; turns out to make the #'<= expression
1105 ;; unreachable when END=NIL) could cause errors
1106 ;; when the compiler tries to constant-fold (<=
1109 ;; So, with or without bug 173, it'd be
1110 ;; unnecessarily evil to do a full
1111 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1112 ;; from COMPILE-FILE) for legal code, so we we
1113 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1114 #'compiler-style-warn
1117 (setf (combination-kind call) :error))
1118 ((and (proper-list-of-length-p values 1))
1119 (with-ir1-environment-from-node call
1120 (let* ((lvar (node-lvar call))
1121 (prev (node-prev call))
1122 (intermediate-ctran (make-ctran)))
1123 (%delete-lvar-use call)
1124 (setf (ctran-next prev) nil)
1125 (setf (node-prev call) nil)
1126 (reference-constant prev intermediate-ctran lvar
1128 (link-node-to-previous-ctran call intermediate-ctran)
1129 (reoptimize-lvar lvar)
1130 (flush-combination call))))
1131 (t (let ((dummies (make-gensym-list (length args))))
1135 (declare (ignore ,@dummies))
1136 (values ,@(mapcar (lambda (x) `',x) values)))
1140 ;;;; local call optimization
1142 ;;; Propagate TYPE to LEAF and its REFS, marking things changed. If
1143 ;;; the leaf type is a function type, then just leave it alone, since
1144 ;;; TYPE is never going to be more specific than that (and
1145 ;;; TYPE-INTERSECTION would choke.)
1146 (defun propagate-to-refs (leaf type)
1147 (declare (type leaf leaf) (type ctype type))
1148 (let ((var-type (leaf-type leaf)))
1149 (unless (fun-type-p var-type)
1150 (let ((int (type-approx-intersection2 var-type type)))
1151 (when (type/= int var-type)
1152 (setf (leaf-type leaf) int)
1153 (dolist (ref (leaf-refs leaf))
1154 (derive-node-type ref (make-single-value-type int))
1155 ;; KLUDGE: LET var substitution
1156 (let* ((lvar (node-lvar ref)))
1157 (when (and lvar (combination-p (lvar-dest lvar)))
1158 (reoptimize-lvar lvar))))))
1161 ;;; Iteration variable: exactly one SETQ of the form:
1163 ;;; (let ((var initial))
1165 ;;; (setq var (+ var step))
1167 (defun maybe-infer-iteration-var-type (var initial-type)
1168 (binding* ((sets (lambda-var-sets var) :exit-if-null)
1170 (() (null (rest sets)) :exit-if-null)
1171 (set-use (principal-lvar-use (set-value set)))
1172 (() (and (combination-p set-use)
1173 (fun-info-p (combination-kind set-use))
1174 (not (node-to-be-deleted-p set-use))
1175 (eq (combination-fun-source-name set-use) '+))
1177 (+-args (basic-combination-args set-use))
1178 (() (and (proper-list-of-length-p +-args 2 2)
1179 (let ((first (principal-lvar-use
1182 (eq (ref-leaf first) var))))
1184 (step-type (lvar-type (second +-args)))
1185 (set-type (lvar-type (set-value set))))
1186 (when (and (numeric-type-p initial-type)
1187 (numeric-type-p step-type)
1188 (numeric-type-equal initial-type step-type))
1189 (multiple-value-bind (low high)
1190 (cond ((csubtypep step-type (specifier-type '(real 0 *)))
1191 (values (numeric-type-low initial-type)
1192 (when (and (numeric-type-p set-type)
1193 (numeric-type-equal set-type initial-type))
1194 (numeric-type-high set-type))))
1195 ((csubtypep step-type (specifier-type '(real * 0)))
1196 (values (when (and (numeric-type-p set-type)
1197 (numeric-type-equal set-type initial-type))
1198 (numeric-type-low set-type))
1199 (numeric-type-high initial-type)))
1202 (modified-numeric-type initial-type
1205 :enumerable nil)))))
1206 (deftransform + ((x y) * * :result result)
1207 "check for iteration variable reoptimization"
1208 (let ((dest (principal-lvar-end result))
1209 (use (principal-lvar-use x)))
1210 (when (and (ref-p use)
1214 (reoptimize-lvar (set-value dest))))
1215 (give-up-ir1-transform))
1217 ;;; Figure out the type of a LET variable that has sets. We compute
1218 ;;; the union of the INITIAL-TYPE and the types of all the set
1219 ;;; values and to a PROPAGATE-TO-REFS with this type.
1220 (defun propagate-from-sets (var initial-type)
1221 (collect ((res initial-type type-union))
1222 (dolist (set (basic-var-sets var))
1223 (let ((type (lvar-type (set-value set))))
1225 (when (node-reoptimize set)
1226 (derive-node-type set (make-single-value-type type))
1227 (setf (node-reoptimize set) nil))))
1229 (awhen (maybe-infer-iteration-var-type var initial-type)
1231 (propagate-to-refs var res)))
1234 ;;; If a LET variable, find the initial value's type and do
1235 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1237 (defun ir1-optimize-set (node)
1238 (declare (type cset node))
1239 (let ((var (set-var node)))
1240 (when (and (lambda-var-p var) (leaf-refs var))
1241 (let ((home (lambda-var-home var)))
1242 (when (eq (functional-kind home) :let)
1243 (let* ((initial-value (let-var-initial-value var))
1244 (initial-type (lvar-type initial-value)))
1245 (setf (lvar-reoptimize initial-value) nil)
1246 (propagate-from-sets var initial-type))))))
1248 (derive-node-type node (make-single-value-type
1249 (lvar-type (set-value node))))
1252 ;;; Return true if the value of REF will always be the same (and is
1253 ;;; thus legal to substitute.)
1254 (defun constant-reference-p (ref)
1255 (declare (type ref ref))
1256 (let ((leaf (ref-leaf ref)))
1258 ((or constant functional) t)
1260 (null (lambda-var-sets leaf)))
1262 (not (eq (defined-fun-inlinep leaf) :notinline)))
1264 (case (global-var-kind leaf)
1266 (let ((name (leaf-source-name leaf)))
1268 (eq (symbol-package (fun-name-block-name name))
1270 (info :function :info name)))))))))
1272 ;;; If we have a non-set LET var with a single use, then (if possible)
1273 ;;; replace the variable reference's LVAR with the arg lvar.
1275 ;;; We change the REF to be a reference to NIL with unused value, and
1276 ;;; let it be flushed as dead code. A side effect of this substitution
1277 ;;; is to delete the variable.
1278 (defun substitute-single-use-lvar (arg var)
1279 (declare (type lvar arg) (type lambda-var var))
1280 (binding* ((ref (first (leaf-refs var)))
1281 (lvar (node-lvar ref) :exit-if-null)
1282 (dest (lvar-dest lvar)))
1284 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1285 ;; LVAR-USEs should not be met on one path.
1286 (eq (lvar-uses lvar) ref)
1288 ;; we should not change lifetime of unknown values lvars
1290 (and (type-single-value-p (lvar-derived-type arg))
1291 (multiple-value-bind (pdest pprev)
1292 (principal-lvar-end lvar)
1293 (declare (ignore pdest))
1294 (lvar-single-value-p pprev))))
1296 (or (eq (basic-combination-fun dest) lvar)
1297 (and (eq (basic-combination-kind dest) :local)
1298 (type-single-value-p (lvar-derived-type arg)))))
1300 ;; While CRETURN and EXIT nodes may be known-values,
1301 ;; they have their own complications, such as
1302 ;; substitution into CRETURN may create new tail calls.
1305 (aver (lvar-single-value-p lvar))
1307 (eq (node-home-lambda ref)
1308 (lambda-home (lambda-var-home var))))
1309 (setf (node-derived-type ref) *wild-type*)
1310 (substitute-lvar-uses lvar arg)
1311 (delete-lvar-use ref)
1312 (change-ref-leaf ref (find-constant nil))
1315 (reoptimize-lvar lvar)
1318 ;;; Delete a LET, removing the call and bind nodes, and warning about
1319 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1320 ;;; along right away and delete the REF and then the lambda, since we
1321 ;;; flush the FUN lvar.
1322 (defun delete-let (clambda)
1323 (declare (type clambda clambda))
1324 (aver (functional-letlike-p clambda))
1325 (note-unreferenced-vars clambda)
1326 (let ((call (let-combination clambda)))
1327 (flush-dest (basic-combination-fun call))
1329 (unlink-node (lambda-bind clambda))
1330 (setf (lambda-bind clambda) nil))
1331 (setf (functional-kind clambda) :zombie)
1332 (let ((home (lambda-home clambda)))
1333 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1336 ;;; This function is called when one of the arguments to a LET
1337 ;;; changes. We look at each changed argument. If the corresponding
1338 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1339 ;;; consider substituting for the variable, and also propagate
1340 ;;; derived-type information for the arg to all the VAR's refs.
1342 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1343 ;;; subtype of the argument's leaf type. This prevents type checking
1344 ;;; from being defeated, and also ensures that the best representation
1345 ;;; for the variable can be used.
1347 ;;; Substitution of individual references is inhibited if the
1348 ;;; reference is in a different component from the home. This can only
1349 ;;; happen with closures over top level lambda vars. In such cases,
1350 ;;; the references may have already been compiled, and thus can't be
1351 ;;; retroactively modified.
1353 ;;; If all of the variables are deleted (have no references) when we
1354 ;;; are done, then we delete the LET.
1356 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1358 (defun propagate-let-args (call fun)
1359 (declare (type combination call) (type clambda fun))
1360 (loop for arg in (combination-args call)
1361 and var in (lambda-vars fun) do
1362 (when (and arg (lvar-reoptimize arg))
1363 (setf (lvar-reoptimize arg) nil)
1365 ((lambda-var-sets var)
1366 (propagate-from-sets var (lvar-type arg)))
1367 ((let ((use (lvar-uses arg)))
1369 (let ((leaf (ref-leaf use)))
1370 (when (and (constant-reference-p use)
1371 (csubtypep (leaf-type leaf)
1372 ;; (NODE-DERIVED-TYPE USE) would
1373 ;; be better -- APD, 2003-05-15
1375 (propagate-to-refs var (lvar-type arg))
1376 (let ((use-component (node-component use)))
1377 (prog1 (substitute-leaf-if
1379 (cond ((eq (node-component ref) use-component)
1382 (aver (lambda-toplevelish-p (lambda-home fun)))
1386 ((and (null (rest (leaf-refs var)))
1387 (substitute-single-use-lvar arg var)))
1389 (propagate-to-refs var (lvar-type arg))))))
1391 (when (every #'not (combination-args call))
1396 ;;; This function is called when one of the args to a non-LET local
1397 ;;; call changes. For each changed argument corresponding to an unset
1398 ;;; variable, we compute the union of the types across all calls and
1399 ;;; propagate this type information to the var's refs.
1401 ;;; If the function has an XEP, then we don't do anything, since we
1402 ;;; won't discover anything.
1404 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1405 ;;; corresponding to changed arguments in CALL, since the only use in
1406 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1408 (defun propagate-local-call-args (call fun)
1409 (declare (type combination call) (type clambda fun))
1411 (unless (or (functional-entry-fun fun)
1412 (lambda-optional-dispatch fun))
1413 (let* ((vars (lambda-vars fun))
1414 (union (mapcar (lambda (arg var)
1416 (lvar-reoptimize arg)
1417 (null (basic-var-sets var)))
1419 (basic-combination-args call)
1421 (this-ref (lvar-use (basic-combination-fun call))))
1423 (dolist (arg (basic-combination-args call))
1425 (setf (lvar-reoptimize arg) nil)))
1427 (dolist (ref (leaf-refs fun))
1428 (let ((dest (node-dest ref)))
1429 (unless (or (eq ref this-ref) (not dest))
1431 (mapcar (lambda (this-arg old)
1433 (setf (lvar-reoptimize this-arg) nil)
1434 (type-union (lvar-type this-arg) old)))
1435 (basic-combination-args dest)
1438 (loop for var in vars
1440 when type do (propagate-to-refs var type))))
1444 ;;;; multiple values optimization
1446 ;;; Do stuff to notice a change to a MV combination node. There are
1447 ;;; two main branches here:
1448 ;;; -- If the call is local, then it is already a MV let, or should
1449 ;;; become one. Note that although all :LOCAL MV calls must eventually
1450 ;;; be converted to :MV-LETs, there can be a window when the call
1451 ;;; is local, but has not been LET converted yet. This is because
1452 ;;; the entry-point lambdas may have stray references (in other
1453 ;;; entry points) that have not been deleted yet.
1454 ;;; -- The call is full. This case is somewhat similar to the non-MV
1455 ;;; combination optimization: we propagate return type information and
1456 ;;; notice non-returning calls. We also have an optimization
1457 ;;; which tries to convert MV-CALLs into MV-binds.
1458 (defun ir1-optimize-mv-combination (node)
1459 (ecase (basic-combination-kind node)
1461 (let ((fun-lvar (basic-combination-fun node)))
1462 (when (lvar-reoptimize fun-lvar)
1463 (setf (lvar-reoptimize fun-lvar) nil)
1464 (maybe-let-convert (combination-lambda node))))
1465 (setf (lvar-reoptimize (first (basic-combination-args node))) nil)
1466 (when (eq (functional-kind (combination-lambda node)) :mv-let)
1467 (unless (convert-mv-bind-to-let node)
1468 (ir1-optimize-mv-bind node))))
1470 (let* ((fun (basic-combination-fun node))
1471 (fun-changed (lvar-reoptimize fun))
1472 (args (basic-combination-args node)))
1474 (setf (lvar-reoptimize fun) nil)
1475 (let ((type (lvar-type fun)))
1476 (when (fun-type-p type)
1477 (derive-node-type node (fun-type-returns type))))
1478 (maybe-terminate-block node nil)
1479 (let ((use (lvar-uses fun)))
1480 (when (and (ref-p use) (functional-p (ref-leaf use)))
1481 (convert-call-if-possible use node)
1482 (when (eq (basic-combination-kind node) :local)
1483 (maybe-let-convert (ref-leaf use))))))
1484 (unless (or (eq (basic-combination-kind node) :local)
1485 (eq (lvar-fun-name fun) '%throw))
1486 (ir1-optimize-mv-call node))
1488 (setf (lvar-reoptimize arg) nil))))
1492 ;;; Propagate derived type info from the values lvar to the vars.
1493 (defun ir1-optimize-mv-bind (node)
1494 (declare (type mv-combination node))
1495 (let* ((arg (first (basic-combination-args node)))
1496 (vars (lambda-vars (combination-lambda node)))
1497 (n-vars (length vars))
1498 (types (values-type-in (lvar-derived-type arg)
1500 (loop for var in vars
1502 do (if (basic-var-sets var)
1503 (propagate-from-sets var type)
1504 (propagate-to-refs var type)))
1505 (setf (lvar-reoptimize arg) nil))
1508 ;;; If possible, convert a general MV call to an MV-BIND. We can do
1510 ;;; -- The call has only one argument, and
1511 ;;; -- The function has a known fixed number of arguments, or
1512 ;;; -- The argument yields a known fixed number of values.
1514 ;;; What we do is change the function in the MV-CALL to be a lambda
1515 ;;; that "looks like an MV bind", which allows
1516 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
1517 ;;; converted (the next time around.) This new lambda just calls the
1518 ;;; actual function with the MV-BIND variables as arguments. Note that
1519 ;;; this new MV bind is not let-converted immediately, as there are
1520 ;;; going to be stray references from the entry-point functions until
1521 ;;; they get deleted.
1523 ;;; In order to avoid loss of argument count checking, we only do the
1524 ;;; transformation according to a known number of expected argument if
1525 ;;; safety is unimportant. We can always convert if we know the number
1526 ;;; of actual values, since the normal call that we build will still
1527 ;;; do any appropriate argument count checking.
1529 ;;; We only attempt the transformation if the called function is a
1530 ;;; constant reference. This allows us to just splice the leaf into
1531 ;;; the new function, instead of trying to somehow bind the function
1532 ;;; expression. The leaf must be constant because we are evaluating it
1533 ;;; again in a different place. This also has the effect of squelching
1534 ;;; multiple warnings when there is an argument count error.
1535 (defun ir1-optimize-mv-call (node)
1536 (let ((fun (basic-combination-fun node))
1537 (*compiler-error-context* node)
1538 (ref (lvar-uses (basic-combination-fun node)))
1539 (args (basic-combination-args node)))
1541 (unless (and (ref-p ref) (constant-reference-p ref)
1543 (return-from ir1-optimize-mv-call))
1545 (multiple-value-bind (min max)
1546 (fun-type-nargs (lvar-type fun))
1548 (multiple-value-bind (types nvals)
1549 (values-types (lvar-derived-type (first args)))
1550 (declare (ignore types))
1551 (if (eq nvals :unknown) nil nvals))))
1554 (when (and min (< total-nvals min))
1556 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1559 (setf (basic-combination-kind node) :error)
1560 (return-from ir1-optimize-mv-call))
1561 (when (and max (> total-nvals max))
1563 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1566 (setf (basic-combination-kind node) :error)
1567 (return-from ir1-optimize-mv-call)))
1569 (let ((count (cond (total-nvals)
1570 ((and (policy node (zerop verify-arg-count))
1575 (with-ir1-environment-from-node node
1576 (let* ((dums (make-gensym-list count))
1578 (fun (ir1-convert-lambda
1579 `(lambda (&optional ,@dums &rest ,ignore)
1580 (declare (ignore ,ignore))
1581 (funcall ,(ref-leaf ref) ,@dums)))))
1582 (change-ref-leaf ref fun)
1583 (aver (eq (basic-combination-kind node) :full))
1584 (locall-analyze-component *current-component*)
1585 (aver (eq (basic-combination-kind node) :local)))))))))
1589 ;;; (multiple-value-bind
1598 ;;; What we actually do is convert the VALUES combination into a
1599 ;;; normal LET combination calling the original :MV-LET lambda. If
1600 ;;; there are extra args to VALUES, discard the corresponding
1601 ;;; lvars. If there are insufficient args, insert references to NIL.
1602 (defun convert-mv-bind-to-let (call)
1603 (declare (type mv-combination call))
1604 (let* ((arg (first (basic-combination-args call)))
1605 (use (lvar-uses arg)))
1606 (when (and (combination-p use)
1607 (eq (lvar-fun-name (combination-fun use))
1609 (let* ((fun (combination-lambda call))
1610 (vars (lambda-vars fun))
1611 (vals (combination-args use))
1612 (nvars (length vars))
1613 (nvals (length vals)))
1614 (cond ((> nvals nvars)
1615 (mapc #'flush-dest (subseq vals nvars))
1616 (setq vals (subseq vals 0 nvars)))
1618 (with-ir1-environment-from-node use
1619 (let ((node-prev (node-prev use)))
1620 (setf (node-prev use) nil)
1621 (setf (ctran-next node-prev) nil)
1622 (collect ((res vals))
1623 (loop for count below (- nvars nvals)
1624 for prev = node-prev then ctran
1625 for ctran = (make-ctran)
1626 and lvar = (make-lvar use)
1627 do (reference-constant prev ctran lvar nil)
1629 finally (link-node-to-previous-ctran
1631 (setq vals (res)))))))
1632 (setf (combination-args use) vals)
1633 (flush-dest (combination-fun use))
1634 (let ((fun-lvar (basic-combination-fun call)))
1635 (setf (lvar-dest fun-lvar) use)
1636 (setf (combination-fun use) fun-lvar)
1637 (flush-lvar-externally-checkable-type fun-lvar))
1638 (setf (combination-kind use) :local)
1639 (setf (functional-kind fun) :let)
1640 (flush-dest (first (basic-combination-args call)))
1643 (reoptimize-lvar (first vals)))
1644 (propagate-to-args use fun)
1645 (reoptimize-call use))
1649 ;;; (values-list (list x y z))
1654 ;;; In implementation, this is somewhat similar to
1655 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
1656 ;;; args of the VALUES-LIST call, flushing the old argument lvar
1657 ;;; (allowing the LIST to be flushed.)
1659 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
1660 (defoptimizer (values-list optimizer) ((list) node)
1661 (let ((use (lvar-uses list)))
1662 (when (and (combination-p use)
1663 (eq (lvar-fun-name (combination-fun use))
1666 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
1667 (change-ref-leaf (lvar-uses (combination-fun node))
1668 (find-free-fun 'values "in a strange place"))
1669 (setf (combination-kind node) :full)
1670 (let ((args (combination-args use)))
1672 (setf (lvar-dest arg) node)
1673 (flush-lvar-externally-checkable-type arg))
1674 (setf (combination-args use) nil)
1676 (setf (combination-args node) args))
1679 ;;; If VALUES appears in a non-MV context, then effectively convert it
1680 ;;; to a PROG1. This allows the computation of the additional values
1681 ;;; to become dead code.
1682 (deftransform values ((&rest vals) * * :node node)
1683 (unless (lvar-single-value-p (node-lvar node))
1684 (give-up-ir1-transform))
1685 (setf (node-derived-type node)
1686 (make-short-values-type (list (single-value-type
1687 (node-derived-type node)))))
1688 (principal-lvar-single-valuify (node-lvar node))
1690 (let ((dummies (make-gensym-list (length (cdr vals)))))
1691 `(lambda (val ,@dummies)
1692 (declare (ignore ,@dummies))
1698 (defun ir1-optimize-cast (cast &optional do-not-optimize)
1699 (declare (type cast cast))
1700 (let ((value (cast-value cast))
1701 (atype (cast-asserted-type cast)))
1702 (when (not do-not-optimize)
1703 (let ((lvar (node-lvar cast)))
1704 (when (values-subtypep (lvar-derived-type value)
1705 (cast-asserted-type cast))
1706 (delete-filter cast lvar value)
1708 (reoptimize-lvar lvar)
1709 (when (lvar-single-value-p lvar)
1710 (note-single-valuified-lvar lvar)))
1711 (return-from ir1-optimize-cast t))
1713 (when (and (listp (lvar-uses value))
1715 ;; Pathwise removing of CAST
1716 (let ((ctran (node-next cast))
1717 (dest (lvar-dest lvar))
1720 (do-uses (use value)
1721 (when (and (values-subtypep (node-derived-type use) atype)
1722 (immediately-used-p value use))
1724 (when ctran (ensure-block-start ctran))
1725 (setq next-block (first (block-succ (node-block cast))))
1726 (ensure-block-start (node-prev cast)))
1727 (%delete-lvar-use use)
1728 (add-lvar-use use lvar)
1729 (unlink-blocks (node-block use) (node-block cast))
1730 (link-blocks (node-block use) next-block)
1731 (when (and (return-p dest)
1732 (basic-combination-p use)
1733 (eq (basic-combination-kind use) :local))
1735 (dolist (use (merges))
1736 (merge-tail-sets use)))))))
1738 (let* ((value-type (lvar-derived-type value))
1739 (int (values-type-intersection value-type atype)))
1740 (derive-node-type cast int)
1741 (when (eq int *empty-type*)
1742 (unless (eq value-type *empty-type*)
1744 ;; FIXME: Do it in one step.
1747 `(multiple-value-call #'list 'dummy))
1750 ;; FIXME: Derived type.
1751 `(%compile-time-type-error 'dummy
1752 ',(type-specifier atype)
1753 ',(type-specifier value-type)))
1754 ;; KLUDGE: FILTER-LVAR does not work for non-returning
1755 ;; functions, so we declare the return type of
1756 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
1758 (setq value (cast-value cast))
1759 (derive-node-type (lvar-uses value) *empty-type*)
1760 (maybe-terminate-block (lvar-uses value) nil)
1761 ;; FIXME: Is it necessary?
1762 (aver (null (block-pred (node-block cast))))
1763 (delete-block-lazily (node-block cast))
1764 (return-from ir1-optimize-cast)))
1765 (when (eq (node-derived-type cast) *empty-type*)
1766 (maybe-terminate-block cast nil))
1768 (when (and (cast-%type-check cast)
1769 (values-subtypep value-type
1770 (cast-type-to-check cast)))
1771 (setf (cast-%type-check cast) nil))))
1773 (unless do-not-optimize
1774 (setf (node-reoptimize cast) nil)))