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 a CONTINUATION whose sole use is a reference to a
23 (defun constant-lvar-p (thing)
25 (let ((use (principal-lvar-use thing)))
26 (and (ref-p use) (constant-p (ref-leaf use))))))
28 ;;; Return the constant value for a continuation whose only use is a
30 (declaim (ftype (function (lvar) t) lvar-value))
31 (defun lvar-value (lvar)
32 (let ((use (principal-lvar-use lvar)))
33 (constant-value (ref-leaf use))))
35 ;;;; interface for obtaining results of type inference
37 ;;; Our best guess for the type of this continuation's value. Note
38 ;;; that this may be VALUES or FUNCTION type, which cannot be passed
39 ;;; as an argument to the normal type operations. See
40 ;;; CONTINUATION-TYPE. This may be called on deleted continuations,
41 ;;; always returning *.
43 ;;; What we do is call CONTINUATION-PROVEN-TYPE and check whether the
44 ;;; result is a subtype of the assertion. If so, return the proven
45 ;;; type and set TYPE-CHECK to NIL. Otherwise, return the intersection
46 ;;; of the asserted and proven types, and set TYPE-CHECK T. If
47 ;;; TYPE-CHECK already has a non-null value, then preserve it. Only in
48 ;;; the somewhat unusual circumstance of a newly discovered assertion
49 ;;; will we change TYPE-CHECK from NIL to T.
51 ;;; The result value is cached in the CONTINUATION-%DERIVED-TYPE slot.
52 ;;; If the slot is true, just return that value, otherwise recompute
53 ;;; and stash the value there.
54 #!-sb-fluid (declaim (inline lvar-derived-type))
55 (defun lvar-derived-type (lvar)
56 (declare (type lvar lvar))
57 (or (lvar-%derived-type lvar)
58 (setf (lvar-%derived-type lvar)
59 (%lvar-derived-type lvar))))
60 (defun %lvar-derived-type (lvar)
61 (declare (type lvar lvar))
62 (let ((uses (lvar-uses lvar)))
63 (cond ((null uses) *empty-type*)
65 (do ((res (node-derived-type (first uses))
66 (values-type-union (node-derived-type (first current))
68 (current (rest uses) (rest current)))
69 ((null current) res)))
71 (node-derived-type (lvar-uses lvar))))))
73 ;;; Return the derived type for CONT's first value. This is guaranteed
74 ;;; not to be a VALUES or FUNCTION type.
75 (declaim (ftype (sfunction (lvar) ctype) lvar-type))
76 (defun lvar-type (lvar)
77 (single-value-type (lvar-derived-type lvar)))
79 ;;; If CONT is an argument of a function, return a type which the
80 ;;; function checks CONT for.
81 #!-sb-fluid (declaim (inline lvar-externally-checkable-type))
82 (defun lvar-externally-checkable-type (lvar)
83 (or (lvar-%externally-checkable-type lvar)
84 (%lvar-%externally-checkable-type lvar)))
85 (defun %lvar-%externally-checkable-type (lvar)
86 (declare (type lvar lvar))
87 (let ((dest (lvar-dest lvar)))
88 (if (not (and dest (combination-p dest)))
89 ;; TODO: MV-COMBINATION
90 (setf (lvar-%externally-checkable-type lvar) *wild-type*)
91 (let* ((fun (combination-fun dest))
92 (args (combination-args dest))
93 (fun-type (lvar-type fun)))
94 (setf (lvar-%externally-checkable-type fun) *wild-type*)
95 (if (or (not (call-full-like-p dest))
96 (not (fun-type-p fun-type))
97 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
98 (fun-type-wild-args fun-type))
101 (setf (lvar-%externally-checkable-type arg)
103 (map-combination-args-and-types
105 (setf (lvar-%externally-checkable-type arg)
106 (acond ((lvar-%externally-checkable-type arg)
107 (values-type-intersection
108 it (coerce-to-values type)))
109 (t (coerce-to-values type)))))
111 (lvar-%externally-checkable-type lvar))
112 #!-sb-fluid(declaim (inline flush-lvar-externally-checkable-type))
113 (defun flush-lvar-externally-checkable-type (lvar)
114 (declare (type lvar lvar))
115 (setf (lvar-%externally-checkable-type lvar) nil))
117 ;;;; interface routines used by optimizers
119 ;;; This function is called by optimizers to indicate that something
120 ;;; interesting has happened to the value of LVAR. Optimizers must
121 ;;; make sure that they don't call for reoptimization when nothing has
122 ;;; happened, since optimization will fail to terminate.
124 ;;; We clear any cached type for the lvar and set the reoptimize flags
125 ;;; on everything in sight.
126 (defun reoptimize-lvar (lvar)
127 (declare (type (or lvar null) lvar))
129 (setf (lvar-%derived-type lvar) nil)
130 (let ((dest (lvar-dest lvar)))
132 (setf (lvar-reoptimize lvar) t)
133 (setf (node-reoptimize dest) t)
134 (binding* (;; Since this may be called during IR1 conversion,
135 ;; PREV may be missing.
136 (prev (node-prev dest) :exit-if-null)
137 (block (ctran-block prev))
138 (component (block-component block)))
139 (when (typep dest 'cif)
140 (setf (block-test-modified block) t))
141 (setf (block-reoptimize block) t)
142 (setf (component-reoptimize component) t))))
144 (setf (block-type-check (node-block node)) t)))
147 (defun reoptimize-lvar-uses (lvar)
148 (declare (type lvar lvar))
150 (setf (node-reoptimize use) t)
151 (setf (block-reoptimize (node-block use)) t)
152 (setf (component-reoptimize (node-component use)) t)))
154 ;;; Annotate NODE to indicate that its result has been proven to be
155 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
156 ;;; only correct way to supply information discovered about a node's
157 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
158 ;;; information may be lost and reoptimization may not happen.
160 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
161 ;;; intersection is different from the old type, then we do a
162 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
163 (defun derive-node-type (node rtype)
164 (declare (type valued-node node) (type ctype rtype))
165 (let ((node-type (node-derived-type node)))
166 (unless (eq node-type rtype)
167 (let ((int (values-type-intersection node-type rtype))
168 (lvar (node-lvar node)))
169 (when (type/= node-type int)
170 (when (and *check-consistency*
171 (eq int *empty-type*)
172 (not (eq rtype *empty-type*)))
173 (let ((*compiler-error-context* node))
175 "New inferred type ~S conflicts with old type:~
176 ~% ~S~%*** possible internal error? Please report this."
177 (type-specifier rtype) (type-specifier node-type))))
178 (setf (node-derived-type node) int)
179 ;; If the new type consists of only one object, replace the
180 ;; node with a constant reference.
181 (when (and (ref-p node)
182 (lambda-var-p (ref-leaf node)))
183 (let ((type (single-value-type int)))
184 (when (and (member-type-p type)
185 (null (rest (member-type-members type))))
186 (change-ref-leaf node (find-constant
187 (first (member-type-members type)))))))
188 (reoptimize-lvar lvar)))))
191 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
192 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
193 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
194 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
195 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
196 (defun assert-lvar-type (lvar type policy)
197 (declare (type lvar lvar) (type ctype type))
198 (unless (values-subtypep (lvar-derived-type lvar) type)
199 (let* ((dest (lvar-dest lvar))
200 (ctran (node-prev dest)))
201 (with-ir1-environment-from-node dest
202 (let* ((cast (make-cast lvar type policy))
203 (internal-lvar (make-lvar))
204 (internal-ctran (make-ctran)))
205 (setf (ctran-next ctran) cast
206 (node-prev cast) ctran)
207 (use-continuation cast internal-ctran internal-lvar)
208 (link-node-to-previous-ctran dest internal-ctran)
209 (substitute-lvar internal-lvar lvar)
210 (setf (lvar-dest lvar) cast)
211 (reoptimize-lvar lvar)
212 (when (return-p dest)
213 (node-ends-block cast))
214 (setf (block-attributep (block-flags (node-block cast))
215 type-check type-asserted)
221 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
222 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
223 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
224 ;;; we are done, then another iteration would be beneficial.
225 (defun ir1-optimize (component)
226 (declare (type component component))
227 (setf (component-reoptimize component) nil)
228 (do-blocks (block component)
230 ;; We delete blocks when there is either no predecessor or the
231 ;; block is in a lambda that has been deleted. These blocks
232 ;; would eventually be deleted by DFO recomputation, but doing
233 ;; it here immediately makes the effect available to IR1
235 ((or (block-delete-p block)
236 (null (block-pred block)))
237 (delete-block block))
238 ((eq (functional-kind (block-home-lambda block)) :deleted)
239 ;; Preserve the BLOCK-SUCC invariant that almost every block has
240 ;; one successor (and a block with DELETE-P set is an acceptable
242 (mark-for-deletion block)
243 (delete-block block))
246 (let ((succ (block-succ block)))
247 (unless (singleton-p succ)
250 (let ((last (block-last block)))
253 (flush-dest (if-test last))
254 (when (unlink-node last)
257 (when (maybe-delete-exit last)
260 (unless (join-successor-if-possible block)
263 (when (and (block-reoptimize block) (block-component block))
264 (aver (not (block-delete-p block)))
265 (ir1-optimize-block block))
267 (cond ((and (block-delete-p block) (block-component block))
268 (delete-block block))
269 ((and (block-flush-p block) (block-component block))
270 (flush-dead-code block))))))
274 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
277 ;;; Note that although they are cleared here, REOPTIMIZE flags might
278 ;;; still be set upon return from this function, meaning that further
279 ;;; optimization is wanted (as a consequence of optimizations we did).
280 (defun ir1-optimize-block (block)
281 (declare (type cblock block))
282 ;; We clear the node and block REOPTIMIZE flags before doing the
283 ;; optimization, not after. This ensures that the node or block will
284 ;; be reoptimized if necessary.
285 (setf (block-reoptimize block) nil)
286 (do-nodes (node nil block :restart-p t)
287 (when (node-reoptimize node)
288 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
289 (setf (node-reoptimize node) nil)
293 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
294 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
295 ;; any argument changes.
296 (ir1-optimize-combination node))
298 (ir1-optimize-if node))
300 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
301 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
302 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
304 (setf (node-reoptimize node) t)
305 (ir1-optimize-return node))
307 (ir1-optimize-mv-combination node))
309 ;; With an EXIT, we derive the node's type from the VALUE's
311 (let ((value (exit-value node)))
313 (derive-node-type node (lvar-derived-type value)))))
315 (ir1-optimize-set node))
317 (ir1-optimize-cast node)))))
321 ;;; Try to join with a successor block. If we succeed, we return true,
323 (defun join-successor-if-possible (block)
324 (declare (type cblock block))
325 (let ((next (first (block-succ block))))
326 (when (block-start next) ; NEXT is not an END-OF-COMPONENT marker
327 (cond ( ;; We cannot combine with a successor block if:
329 ;; The successor has more than one predecessor.
330 (rest (block-pred next))
331 ;; The successor is the current block (infinite loop).
333 ;; The next block has a different cleanup, and thus
334 ;; we may want to insert cleanup code between the
335 ;; two blocks at some point.
336 (not (eq (block-end-cleanup block)
337 (block-start-cleanup next)))
338 ;; The next block has a different home lambda, and
339 ;; thus the control transfer is a non-local exit.
340 (not (eq (block-home-lambda block)
341 (block-home-lambda next))))
344 (join-blocks block next)
347 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
348 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
349 ;;; for the two blocks so that any indicated optimization gets done.
350 (defun join-blocks (block1 block2)
351 (declare (type cblock block1 block2))
352 (let* ((last1 (block-last block1))
353 (last2 (block-last block2))
354 (succ (block-succ block2))
355 (start2 (block-start block2)))
356 (do ((ctran start2 (node-next (ctran-next ctran))))
358 (setf (ctran-block ctran) block1))
360 (unlink-blocks block1 block2)
362 (unlink-blocks block2 block)
363 (link-blocks block1 block))
365 (setf (ctran-kind start2) :inside-block)
366 (setf (node-next last1) start2)
367 (setf (ctran-use start2) last1)
368 (setf (block-last block1) last2))
370 (setf (block-flags block1)
371 (attributes-union (block-flags block1)
373 (block-attributes type-asserted test-modified)))
375 (let ((next (block-next block2))
376 (prev (block-prev block2)))
377 (setf (block-next prev) next)
378 (setf (block-prev next) prev))
382 ;;; Delete any nodes in BLOCK whose value is unused and which have no
383 ;;; side effects. We can delete sets of lexical variables when the set
384 ;;; variable has no references.
385 (defun flush-dead-code (block)
386 (declare (type cblock block))
387 (setf (block-flush-p block) nil)
388 (do-nodes-backwards (node lvar block)
395 (let ((info (combination-kind node)))
396 (when (fun-info-p info)
397 (let ((attr (fun-info-attributes info)))
398 (when (and (not (ir1-attributep attr call))
399 ;; ### For now, don't delete potentially
400 ;; flushable calls when they have the CALL
401 ;; attribute. Someday we should look at the
402 ;; functional args to determine if they have
404 (if (policy node (= safety 3))
405 (ir1-attributep attr flushable)
406 (ir1-attributep attr unsafely-flushable)))
407 (flush-combination node))))))
409 (when (eq (basic-combination-kind node) :local)
410 (let ((fun (combination-lambda node)))
411 (when (dolist (var (lambda-vars fun) t)
412 (when (or (leaf-refs var)
413 (lambda-var-sets var))
415 (flush-dest (first (basic-combination-args node)))
418 (let ((value (exit-value node)))
421 (setf (exit-value node) nil))))
423 (let ((var (set-var node)))
424 (when (and (lambda-var-p var)
425 (null (leaf-refs var)))
426 (flush-dest (set-value node))
427 (setf (basic-var-sets var)
428 (delq node (basic-var-sets var)))
429 (unlink-node node))))
431 (unless (cast-type-check node)
432 (flush-dest (cast-value node))
433 (unlink-node node))))))
437 ;;;; local call return type propagation
439 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
440 ;;; flag set. It iterates over the uses of the RESULT, looking for
441 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
442 ;;; call, then we union its type together with the types of other such
443 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
444 ;;; type with the RESULT's asserted type. We can make this
445 ;;; intersection now (potentially before type checking) because this
446 ;;; assertion on the result will eventually be checked (if
449 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
450 ;;; combination, which may change the succesor of the call to be the
451 ;;; called function, and if so, checks if the call can become an
452 ;;; assignment. If we convert to an assignment, we abort, since the
453 ;;; RETURN has been deleted.
454 (defun find-result-type (node)
455 (declare (type creturn node))
456 (let ((result (return-result node)))
457 (collect ((use-union *empty-type* values-type-union))
458 (do-uses (use result)
459 (cond ((and (basic-combination-p use)
460 (eq (basic-combination-kind use) :local))
461 (aver (eq (lambda-tail-set (node-home-lambda use))
462 (lambda-tail-set (combination-lambda use))))
463 (when (combination-p use)
464 (when (nth-value 1 (maybe-convert-tail-local-call use))
465 (return-from find-result-type (values)))))
467 (use-union (node-derived-type use)))))
469 ;; (values-type-intersection
470 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
474 (setf (return-result-type node) int))))
477 ;;; Do stuff to realize that something has changed about the value
478 ;;; delivered to a return node. Since we consider the return values of
479 ;;; all functions in the tail set to be equivalent, this amounts to
480 ;;; bringing the entire tail set up to date. We iterate over the
481 ;;; returns for all the functions in the tail set, reanalyzing them
482 ;;; all (not treating NODE specially.)
484 ;;; When we are done, we check whether the new type is different from
485 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
486 ;;; all the continuations for references to functions in the tail set.
487 ;;; This will cause IR1-OPTIMIZE-COMBINATION to derive the new type as
488 ;;; the results of the calls.
489 (defun ir1-optimize-return (node)
490 (declare (type creturn node))
491 (let* ((tails (lambda-tail-set (return-lambda node)))
492 (funs (tail-set-funs tails)))
493 (collect ((res *empty-type* values-type-union))
495 (let ((return (lambda-return fun)))
497 (when (node-reoptimize return)
498 (setf (node-reoptimize return) nil)
499 (find-result-type return))
500 (res (return-result-type return)))))
502 (when (type/= (res) (tail-set-type tails))
503 (setf (tail-set-type tails) (res))
504 (dolist (fun (tail-set-funs tails))
505 (dolist (ref (leaf-refs fun))
506 (reoptimize-lvar (node-lvar ref)))))))
512 ;;; If the test has multiple uses, replicate the node when possible.
513 ;;; Also check whether the predicate is known to be true or false,
514 ;;; deleting the IF node in favor of the appropriate branch when this
516 (defun ir1-optimize-if (node)
517 (declare (type cif node))
518 (let ((test (if-test node))
519 (block (node-block node)))
521 (when (and (eq (block-start-node block) node)
522 (listp (lvar-uses test)))
524 (when (immediately-used-p test use)
525 (convert-if-if use node)
526 (when (not (listp (lvar-uses test))) (return)))))
528 (let* ((type (lvar-type test))
530 (cond ((constant-lvar-p test)
531 (if (lvar-value test)
532 (if-alternative node)
533 (if-consequent node)))
534 ((not (types-equal-or-intersect type (specifier-type 'null)))
535 (if-alternative node))
536 ((type= type (specifier-type 'null))
537 (if-consequent node)))))
540 (when (rest (block-succ block))
541 (unlink-blocks block victim))
542 (setf (component-reanalyze (node-component node)) t)
543 (unlink-node node))))
546 ;;; Create a new copy of an IF node that tests the value of the node
547 ;;; USE. The test must have >1 use, and must be immediately used by
548 ;;; USE. NODE must be the only node in its block (implying that
549 ;;; block-start = if-test).
551 ;;; This optimization has an effect semantically similar to the
552 ;;; source-to-source transformation:
553 ;;; (IF (IF A B C) D E) ==>
554 ;;; (IF A (IF B D E) (IF C D E))
556 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
557 ;;; node so that dead code deletion notes will definitely not consider
558 ;;; either node to be part of the original source. One node might
559 ;;; become unreachable, resulting in a spurious note.
560 (defun convert-if-if (use node)
561 (declare (type node use) (type cif node))
562 (with-ir1-environment-from-node node
563 (let* ((block (node-block node))
564 (test (if-test node))
565 (cblock (if-consequent node))
566 (ablock (if-alternative node))
567 (use-block (node-block use))
568 (new-ctran (make-ctran))
569 (new-lvar (make-lvar))
570 (new-node (make-if :test new-lvar
572 :alternative ablock))
573 (new-block (ctran-starts-block new-ctran)))
574 (link-node-to-previous-ctran new-node new-ctran)
575 (setf (lvar-dest new-lvar) new-node)
576 (setf (block-last new-block) new-node)
578 (unlink-blocks use-block block)
579 (%delete-lvar-use use)
580 (add-lvar-use use new-lvar)
581 (link-blocks use-block new-block)
583 (link-blocks new-block cblock)
584 (link-blocks new-block ablock)
586 (push "<IF Duplication>" (node-source-path node))
587 (push "<IF Duplication>" (node-source-path new-node))
589 (reoptimize-lvar test)
590 (reoptimize-lvar new-lvar)
591 (setf (component-reanalyze *current-component*) t)))
594 ;;;; exit IR1 optimization
596 ;;; This function attempts to delete an exit node, returning true if
597 ;;; it deletes the block as a consequence:
598 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
599 ;;; anything, since there is nothing to be done.
600 ;;; -- If the exit node and its ENTRY have the same home lambda then
601 ;;; we know the exit is local, and can delete the exit. We change
602 ;;; uses of the Exit-Value to be uses of the original lvar,
603 ;;; then unlink the node. If the exit is to a TR context, then we
604 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
605 ;;; their value to this exit.
606 ;;; -- If there is no value (as in a GO), then we skip the value
609 ;;; This function is also called by environment analysis, since it
610 ;;; wants all exits to be optimized even if normal optimization was
612 (defun maybe-delete-exit (node)
613 (declare (type exit node))
614 (let ((value (exit-value node))
615 (entry (exit-entry node)))
617 (eq (node-home-lambda node) (node-home-lambda entry)))
618 (setf (entry-exits entry) (delq node (entry-exits entry)))
620 (delete-filter node (node-lvar node) value)
621 (unlink-node node)))))
624 ;;;; combination IR1 optimization
626 ;;; Report as we try each transform?
628 (defvar *show-transforms-p* nil)
630 ;;; Do IR1 optimizations on a COMBINATION node.
631 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
632 (defun ir1-optimize-combination (node)
633 (when (lvar-reoptimize (basic-combination-fun node))
634 (propagate-fun-change node))
635 (let ((args (basic-combination-args node))
636 (kind (basic-combination-kind node)))
639 (let ((fun (combination-lambda node)))
640 (if (eq (functional-kind fun) :let)
641 (propagate-let-args node fun)
642 (propagate-local-call-args node fun))))
646 (setf (lvar-reoptimize arg) nil))))
650 (setf (lvar-reoptimize arg) nil)))
652 (let ((attr (fun-info-attributes kind)))
653 (when (and (ir1-attributep attr foldable)
654 ;; KLUDGE: The next test could be made more sensitive,
655 ;; only suppressing constant-folding of functions with
656 ;; CALL attributes when they're actually passed
657 ;; function arguments. -- WHN 19990918
658 (not (ir1-attributep attr call))
659 (every #'constant-lvar-p args)
661 ;; Even if the function is foldable in principle,
662 ;; it might be one of our low-level
663 ;; implementation-specific functions. Such
664 ;; functions don't necessarily exist at runtime on
665 ;; a plain vanilla ANSI Common Lisp
666 ;; cross-compilation host, in which case the
667 ;; cross-compiler can't fold it because the
668 ;; cross-compiler doesn't know how to evaluate it.
670 (or (fboundp (combination-fun-source-name node))
671 (progn (format t ";;; !!! Unbound fun: (~S~{ ~S~})~%"
672 (combination-fun-source-name node)
673 (mapcar #'lvar-value args))
675 (constant-fold-call node)
676 (return-from ir1-optimize-combination)))
678 (let ((fun (fun-info-derive-type kind)))
680 (let ((res (funcall fun node)))
682 (derive-node-type node (coerce-to-values res))
683 (maybe-terminate-block node nil)))))
685 (let ((fun (fun-info-optimizer kind)))
686 (unless (and fun (funcall fun node))
687 (dolist (x (fun-info-transforms kind))
689 (when *show-transforms-p*
690 (let* ((lvar (basic-combination-fun node))
691 (fname (lvar-fun-name lvar t)))
692 (/show "trying transform" x (transform-function x) "for" fname)))
693 (unless (ir1-transform node x)
695 (when *show-transforms-p*
696 (/show "quitting because IR1-TRANSFORM result was NIL"))
701 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
702 ;;; the block there, and link it to the component tail. We also change
703 ;;; the NODE's CONT to be a dummy continuation to prevent the use from
704 ;;; confusing things.
706 ;;; Except when called during IR1 convertion, we delete the
707 ;;; continuation if it has no other uses. (If it does have other uses,
710 ;;; Termination on the basis of a continuation type is
712 ;;; -- The continuation is deleted (hence the assertion is spurious), or
713 ;;; -- We are in IR1 conversion (where THE assertions are subject to
714 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
715 ;;; uses can(?) be added later. -- APD, 2003-07-17
717 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
718 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p)
719 (declare (type (or basic-combination cast) node))
720 (let* ((block (node-block node))
721 (lvar (node-lvar node))
722 (ctran (node-next node))
723 (tail (component-tail (block-component block)))
724 (succ (first (block-succ block))))
725 (unless (or (and (eq node (block-last block)) (eq succ tail))
726 (block-delete-p block))
727 (when (eq (node-derived-type node) *empty-type*)
728 (cond (ir1-converting-not-optimizing-p
731 (aver (eq (block-last block) node)))
733 (setf (block-last block) node)
734 (setf (ctran-use ctran) nil)
735 (setf (ctran-kind ctran) :unused)
736 (setf (ctran-block ctran) nil)
737 (setf (node-next node) nil)
738 (link-blocks block (ctran-starts-block ctran)))))
740 (node-ends-block node)))
742 (unlink-blocks block (first (block-succ block)))
743 (setf (component-reanalyze (block-component block)) t)
744 (aver (not (block-succ block)))
745 (link-blocks block tail)
746 (if ir1-converting-not-optimizing-p
747 (%delete-lvar-use node)
748 (delete-lvar-use node))
751 ;;; This is called both by IR1 conversion and IR1 optimization when
752 ;;; they have verified the type signature for the call, and are
753 ;;; wondering if something should be done to special-case the call. If
754 ;;; CALL is a call to a global function, then see whether it defined
756 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
757 ;;; the expansion and change the call to call it. Expansion is
758 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
759 ;;; true, we never expand, since this function has already been
760 ;;; converted. Local call analysis will duplicate the definition
761 ;;; if necessary. We claim that the parent form is LABELS for
762 ;;; context declarations, since we don't want it to be considered
763 ;;; a real global function.
764 ;;; -- If it is a known function, mark it as such by setting the KIND.
766 ;;; We return the leaf referenced (NIL if not a leaf) and the
767 ;;; FUN-INFO assigned.
768 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
769 (declare (type combination call))
770 (let* ((ref (lvar-uses (basic-combination-fun call)))
771 (leaf (when (ref-p ref) (ref-leaf ref)))
772 (inlinep (if (defined-fun-p leaf)
773 (defined-fun-inlinep leaf)
776 ((eq inlinep :notinline) (values nil nil))
777 ((not (and (global-var-p leaf)
778 (eq (global-var-kind leaf) :global-function)))
783 ((nil :maybe-inline) (policy call (zerop space))))
785 (defined-fun-inline-expansion leaf)
786 (let ((fun (defined-fun-functional leaf)))
788 (and (eq inlinep :inline) (functional-kind fun))))
789 (inline-expansion-ok call))
790 (flet (;; FIXME: Is this what the old CMU CL internal documentation
791 ;; called semi-inlining? A more descriptive name would
792 ;; be nice. -- WHN 2002-01-07
794 (let ((res (ir1-convert-lambda-for-defun
795 (defined-fun-inline-expansion leaf)
797 #'ir1-convert-inline-lambda)))
798 (setf (defined-fun-functional leaf) res)
799 (change-ref-leaf ref res))))
800 (if ir1-converting-not-optimizing-p
802 (with-ir1-environment-from-node call
804 (locall-analyze-component *current-component*))))
806 (values (ref-leaf (lvar-uses (basic-combination-fun call)))
809 (let ((info (info :function :info (leaf-source-name leaf))))
811 (values leaf (setf (basic-combination-kind call) info))
812 (values leaf nil)))))))
814 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
815 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
816 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
817 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
818 ;;; syntax check, arg/result type processing, but still call
819 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
820 ;;; and that checking is done by local call analysis.
821 (defun validate-call-type (call type ir1-converting-not-optimizing-p)
822 (declare (type combination call) (type ctype type))
823 (cond ((not (fun-type-p type))
824 (aver (multiple-value-bind (val win)
825 (csubtypep type (specifier-type 'function))
827 (recognize-known-call call ir1-converting-not-optimizing-p))
828 ((valid-fun-use call type
829 :argument-test #'always-subtypep
830 :result-test #'always-subtypep
831 ;; KLUDGE: Common Lisp is such a dynamic
832 ;; language that all we can do here in
833 ;; general is issue a STYLE-WARNING. It
834 ;; would be nice to issue a full WARNING
835 ;; in the special case of of type
836 ;; mismatches within a compilation unit
837 ;; (as in section 3.2.2.3 of the spec)
838 ;; but at least as of sbcl-0.6.11, we
839 ;; don't keep track of whether the
840 ;; mismatched data came from the same
841 ;; compilation unit, so we can't do that.
844 ;; FIXME: Actually, I think we could
845 ;; issue a full WARNING if the call
846 ;; violates a DECLAIM FTYPE.
847 :lossage-fun #'compiler-style-warn
848 :unwinnage-fun #'compiler-notify)
849 (assert-call-type call type)
850 (maybe-terminate-block call ir1-converting-not-optimizing-p)
851 (recognize-known-call call ir1-converting-not-optimizing-p))
853 (setf (combination-kind call) :error)
856 ;;; This is called by IR1-OPTIMIZE when the function for a call has
857 ;;; changed. If the call is local, we try to LET-convert it, and
858 ;;; derive the result type. If it is a :FULL call, we validate it
859 ;;; against the type, which recognizes known calls, does inline
860 ;;; expansion, etc. If a call to a predicate in a non-conditional
861 ;;; position or to a function with a source transform, then we
862 ;;; reconvert the form to give IR1 another chance.
863 (defun propagate-fun-change (call)
864 (declare (type combination call))
865 (let ((*compiler-error-context* call)
866 (fun-lvar (basic-combination-fun call)))
867 (setf (lvar-reoptimize fun-lvar) nil)
868 (case (combination-kind call)
870 (let ((fun (combination-lambda call)))
871 (maybe-let-convert fun)
872 (unless (member (functional-kind fun) '(:let :assignment :deleted))
873 (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
875 (multiple-value-bind (leaf info)
876 (validate-call-type call (lvar-type fun-lvar) nil)
877 (cond ((functional-p leaf)
878 (convert-call-if-possible
879 (lvar-uses (basic-combination-fun call))
882 ((and (leaf-has-source-name-p leaf)
883 (or (info :function :source-transform (leaf-source-name leaf))
885 (ir1-attributep (fun-info-attributes info)
887 (let ((lvar (node-lvar call)))
888 (and lvar (not (if-p (lvar-dest lvar))))))))
889 (let ((name (leaf-source-name leaf))
890 (dummies (make-gensym-list
891 (length (combination-args call)))))
894 (,@(if (symbolp name)
898 (leaf-source-name leaf)))))))))
901 ;;;; known function optimization
903 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
904 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
905 ;;; replace it, otherwise add a new one.
906 (defun record-optimization-failure (node transform args)
907 (declare (type combination node) (type transform transform)
908 (type (or fun-type list) args))
909 (let* ((table (component-failed-optimizations *component-being-compiled*))
910 (found (assoc transform (gethash node table))))
912 (setf (cdr found) args)
913 (push (cons transform args) (gethash node table))))
916 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
917 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
918 ;;; doing the transform for some reason and FLAME is true, then we
919 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
920 ;;; finalize to pick up. We return true if the transform failed, and
921 ;;; thus further transformation should be attempted. We return false
922 ;;; if either the transform succeeded or was aborted.
923 (defun ir1-transform (node transform)
924 (declare (type combination node) (type transform transform))
925 (let* ((type (transform-type transform))
926 (fun (transform-function transform))
927 (constrained (fun-type-p type))
928 (table (component-failed-optimizations *component-being-compiled*))
929 (flame (if (transform-important transform)
930 (policy node (>= speed inhibit-warnings))
931 (policy node (> speed inhibit-warnings))))
932 (*compiler-error-context* node))
933 (cond ((or (not constrained)
934 (valid-fun-use node type))
935 (multiple-value-bind (severity args)
936 (catch 'give-up-ir1-transform
939 (combination-fun-source-name node))
946 (setf (combination-kind node) :error)
948 (apply #'compiler-warn args))
954 (record-optimization-failure node transform args))
955 (setf (gethash node table)
956 (remove transform (gethash node table) :key #'car)))
964 :argument-test #'types-equal-or-intersect
965 :result-test #'values-types-equal-or-intersect))
966 (record-optimization-failure node transform type)
971 ;;; When we don't like an IR1 transform, we throw the severity/reason
974 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
975 ;;; aborting this attempt to transform the call, but admitting the
976 ;;; possibility that this or some other transform will later succeed.
977 ;;; If arguments are supplied, they are format arguments for an
980 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
981 ;;; force a normal call to the function at run time. No further
982 ;;; optimizations will be attempted.
984 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
985 ;;; delay the transform on the node until later. REASONS specifies
986 ;;; when the transform will be later retried. The :OPTIMIZE reason
987 ;;; causes the transform to be delayed until after the current IR1
988 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
989 ;;; be delayed until after constraint propagation.
991 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
992 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
993 ;;; do CASE operations on the various REASON values, it might be a
994 ;;; good idea to go OO, representing the reasons by objects, using
995 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
996 ;;; SIGNAL instead of THROW.
997 (declaim (ftype (function (&rest t) nil) give-up-ir1-transform))
998 (defun give-up-ir1-transform (&rest args)
999 (throw 'give-up-ir1-transform (values :failure args)))
1000 (defun abort-ir1-transform (&rest args)
1001 (throw 'give-up-ir1-transform (values :aborted args)))
1002 (defun delay-ir1-transform (node &rest reasons)
1003 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1005 (setf *delayed-ir1-transforms*
1006 (acons node reasons *delayed-ir1-transforms*))
1007 (throw 'give-up-ir1-transform :delayed))
1009 (dolist (reason reasons)
1010 (pushnew reason (cdr assoc)))
1011 (throw 'give-up-ir1-transform :delayed)))))
1013 ;;; Clear any delayed transform with no reasons - these should have
1014 ;;; been tried in the last pass. Then remove the reason from the
1015 ;;; delayed transform reasons, and if any become empty then set
1016 ;;; reoptimize flags for the node. Return true if any transforms are
1018 (defun retry-delayed-ir1-transforms (reason)
1019 (setf *delayed-ir1-transforms*
1020 (remove-if-not #'cdr *delayed-ir1-transforms*))
1021 (let ((reoptimize nil))
1022 (dolist (assoc *delayed-ir1-transforms*)
1023 (let ((reasons (remove reason (cdr assoc))))
1024 (setf (cdr assoc) reasons)
1026 (let ((node (car assoc)))
1027 (unless (node-deleted node)
1029 (setf (node-reoptimize node) t)
1030 (let ((block (node-block node)))
1031 (setf (block-reoptimize block) t)
1032 (setf (component-reoptimize (block-component block)) t)))))))
1035 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1036 ;;; environment, and then install it as the function for the call
1037 ;;; NODE. We do local call analysis so that the new function is
1038 ;;; integrated into the control flow.
1040 ;;; We require the original function source name in order to generate
1041 ;;; a meaningful debug name for the lambda we set up. (It'd be
1042 ;;; possible to do this starting from debug names as well as source
1043 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1044 ;;; generality, since source names are always known to our callers.)
1045 (defun transform-call (call res source-name)
1046 (declare (type combination call) (list res))
1047 (aver (and (legal-fun-name-p source-name)
1048 (not (eql source-name '.anonymous.))))
1049 (node-ends-block call)
1050 (with-ir1-environment-from-node call
1051 (with-component-last-block (*current-component*
1052 (block-next (node-block call)))
1053 (let ((new-fun (ir1-convert-inline-lambda
1055 :debug-name (debug-namify "LAMBDA-inlined ~A"
1058 "<unknown function>"))))
1059 (ref (lvar-use (combination-fun call))))
1060 (change-ref-leaf ref new-fun)
1061 (setf (combination-kind call) :full)
1062 (locall-analyze-component *current-component*))))
1065 ;;; Replace a call to a foldable function of constant arguments with
1066 ;;; the result of evaluating the form. If there is an error during the
1067 ;;; evaluation, we give a warning and leave the call alone, making the
1068 ;;; call a :ERROR call.
1070 ;;; If there is more than one value, then we transform the call into a
1072 (defun constant-fold-call (call)
1073 (let ((args (mapcar #'lvar-value (combination-args call)))
1074 (fun-name (combination-fun-source-name call)))
1075 (multiple-value-bind (values win)
1076 (careful-call fun-name
1079 ;; Note: CMU CL had COMPILER-WARN here, and that
1080 ;; seems more natural, but it's probably not.
1082 ;; It's especially not while bug 173 exists:
1085 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1087 ;; can cause constant-folding TYPE-ERRORs (in
1088 ;; #'<=) when END can be proved to be NIL, even
1089 ;; though the code is perfectly legal and safe
1090 ;; because a NIL value of END means that the
1091 ;; #'<= will never be executed.
1093 ;; Moreover, even without bug 173,
1094 ;; quite-possibly-valid code like
1095 ;; (COND ((NONINLINED-PREDICATE END)
1096 ;; (UNLESS (<= END SIZE))
1098 ;; (where NONINLINED-PREDICATE is something the
1099 ;; compiler can't do at compile time, but which
1100 ;; turns out to make the #'<= expression
1101 ;; unreachable when END=NIL) could cause errors
1102 ;; when the compiler tries to constant-fold (<=
1105 ;; So, with or without bug 173, it'd be
1106 ;; unnecessarily evil to do a full
1107 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1108 ;; from COMPILE-FILE) for legal code, so we we
1109 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1110 #'compiler-style-warn
1113 (setf (combination-kind call) :error))
1114 ((and (proper-list-of-length-p values 1))
1115 (with-ir1-environment-from-node call
1116 (let* ((lvar (node-lvar call))
1117 (prev (node-prev call))
1118 (intermediate-ctran (make-ctran)))
1119 (%delete-lvar-use call)
1120 (setf (ctran-next prev) nil)
1121 (setf (node-prev call) nil)
1122 (reference-constant prev intermediate-ctran lvar
1124 (link-node-to-previous-ctran call intermediate-ctran)
1125 (reoptimize-lvar lvar)
1126 (flush-combination call))))
1127 (t (let ((dummies (make-gensym-list (length args))))
1131 (declare (ignore ,@dummies))
1132 (values ,@(mapcar (lambda (x) `',x) values)))
1136 ;;;; local call optimization
1138 ;;; Propagate TYPE to LEAF and its REFS, marking things changed. If
1139 ;;; the leaf type is a function type, then just leave it alone, since
1140 ;;; TYPE is never going to be more specific than that (and
1141 ;;; TYPE-INTERSECTION would choke.)
1142 (defun propagate-to-refs (leaf type)
1143 (declare (type leaf leaf) (type ctype type))
1144 (let ((var-type (leaf-type leaf)))
1145 (unless (fun-type-p var-type)
1146 (let ((int (type-approx-intersection2 var-type type)))
1147 (when (type/= int var-type)
1148 (setf (leaf-type leaf) int)
1149 (dolist (ref (leaf-refs leaf))
1150 (derive-node-type ref (make-single-value-type int))
1151 ;; KLUDGE: LET var substitution
1152 (let* ((lvar (node-lvar ref)))
1153 (when (and lvar (combination-p (lvar-dest lvar)))
1154 (reoptimize-lvar lvar))))))
1157 ;;; Iteration variable: exactly one SETQ of the form:
1159 ;;; (let ((var initial))
1161 ;;; (setq var (+ var step))
1163 (defun maybe-infer-iteration-var-type (var initial-type)
1164 (binding* ((sets (lambda-var-sets var) :exit-if-null)
1166 (() (null (rest sets)) :exit-if-null)
1167 (set-use (principal-lvar-use (set-value set)))
1168 (() (and (combination-p set-use)
1169 (fun-info-p (combination-kind set-use))
1170 (eq (combination-fun-source-name set-use) '+))
1172 (+-args (basic-combination-args set-use))
1173 (() (and (proper-list-of-length-p +-args 2 2)
1174 (let ((first (principal-lvar-use
1177 (eq (ref-leaf first) var))))
1179 (step-type (lvar-type (second +-args)))
1180 (set-type (lvar-type (set-value set))))
1181 (when (and (numeric-type-p initial-type)
1182 (numeric-type-p step-type)
1183 (numeric-type-equal initial-type step-type))
1184 (multiple-value-bind (low high)
1185 (cond ((csubtypep step-type (specifier-type '(real 0 *)))
1186 (values (numeric-type-low initial-type)
1187 (when (and (numeric-type-p set-type)
1188 (numeric-type-equal set-type initial-type))
1189 (numeric-type-high set-type))))
1190 ((csubtypep step-type (specifier-type '(real * 0)))
1191 (values (when (and (numeric-type-p set-type)
1192 (numeric-type-equal set-type initial-type))
1193 (numeric-type-low set-type))
1194 (numeric-type-high initial-type)))
1197 (modified-numeric-type initial-type
1200 :enumerable nil)))))
1201 (deftransform + ((x y) * * :result result)
1202 "check for iteration variable reoptimization"
1203 (let ((dest (principal-lvar-end result))
1204 (use (principal-lvar-use x)))
1205 (when (and (ref-p use)
1209 (reoptimize-lvar (set-value dest))))
1210 (give-up-ir1-transform))
1212 ;;; Figure out the type of a LET variable that has sets. We compute
1213 ;;; the union of the INITIAL-TYPE and the types of all the set
1214 ;;; values and to a PROPAGATE-TO-REFS with this type.
1215 (defun propagate-from-sets (var initial-type)
1216 (collect ((res initial-type type-union))
1217 (dolist (set (basic-var-sets var))
1218 (let ((type (lvar-type (set-value set))))
1220 (when (node-reoptimize set)
1221 (derive-node-type set (make-single-value-type type))
1222 (setf (node-reoptimize set) nil))))
1224 (awhen (maybe-infer-iteration-var-type var initial-type)
1226 (propagate-to-refs var res)))
1229 ;;; If a LET variable, find the initial value's type and do
1230 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1232 (defun ir1-optimize-set (node)
1233 (declare (type cset node))
1234 (let ((var (set-var node)))
1235 (when (and (lambda-var-p var) (leaf-refs var))
1236 (let ((home (lambda-var-home var)))
1237 (when (eq (functional-kind home) :let)
1238 (let* ((initial-value (let-var-initial-value var))
1239 (initial-type (lvar-type initial-value)))
1240 (setf (lvar-reoptimize initial-value) nil)
1241 (propagate-from-sets var initial-type))))))
1243 (derive-node-type node (make-single-value-type
1244 (lvar-type (set-value node))))
1247 ;;; Return true if the value of REF will always be the same (and is
1248 ;;; thus legal to substitute.)
1249 (defun constant-reference-p (ref)
1250 (declare (type ref ref))
1251 (let ((leaf (ref-leaf ref)))
1253 ((or constant functional) t)
1255 (null (lambda-var-sets leaf)))
1257 (not (eq (defined-fun-inlinep leaf) :notinline)))
1259 (case (global-var-kind leaf)
1261 (let ((name (leaf-source-name leaf)))
1263 (eq (symbol-package (fun-name-block-name name))
1265 (info :function :info name)))))))))
1267 ;;; If we have a non-set LET var with a single use, then (if possible)
1268 ;;; replace the variable reference's CONT with the arg continuation.
1270 ;;; We change the REF to be a reference to NIL with unused value, and
1271 ;;; let it be flushed as dead code. A side effect of this substitution
1272 ;;; is to delete the variable.
1273 (defun substitute-single-use-lvar (arg var)
1274 (declare (type lvar arg) (type lambda-var var))
1275 (binding* ((ref (first (leaf-refs var)))
1276 (lvar (node-lvar ref) :exit-if-null)
1277 (dest (lvar-dest lvar)))
1279 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1280 ;; LVAR-USEs should not be met on one path.
1281 (eq (lvar-uses lvar) ref)
1283 ;; we should not change lifetime of unknown values lvars
1285 (and (type-single-value-p (lvar-derived-type arg))
1286 (multiple-value-bind (pdest pprev)
1287 (principal-lvar-end lvar)
1288 (declare (ignore pdest))
1289 (lvar-single-value-p pprev))))
1291 (or (eq (basic-combination-fun dest) lvar)
1292 (and (eq (basic-combination-kind dest) :local)
1293 (type-single-value-p (lvar-derived-type arg)))))
1295 ;; While CRETURN and EXIT nodes may be known-values,
1296 ;; they have their own complications, such as
1297 ;; substitution into CRETURN may create new tail calls.
1300 (aver (lvar-single-value-p lvar))
1302 (eq (node-home-lambda ref)
1303 (lambda-home (lambda-var-home var))))
1304 (setf (node-derived-type ref) *wild-type*)
1305 (substitute-lvar-uses lvar arg)
1306 (delete-lvar-use ref)
1307 (change-ref-leaf ref (find-constant nil))
1310 (reoptimize-lvar lvar)
1313 ;;; Delete a LET, removing the call and bind nodes, and warning about
1314 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1315 ;;; along right away and delete the REF and then the lambda, since we
1316 ;;; flush the FUN continuation.
1317 (defun delete-let (clambda)
1318 (declare (type clambda clambda))
1319 (aver (functional-letlike-p clambda))
1320 (note-unreferenced-vars clambda)
1321 (let ((call (let-combination clambda)))
1322 (flush-dest (basic-combination-fun call))
1324 (unlink-node (lambda-bind clambda))
1325 (setf (lambda-bind clambda) nil))
1328 ;;; This function is called when one of the arguments to a LET
1329 ;;; changes. We look at each changed argument. If the corresponding
1330 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1331 ;;; consider substituting for the variable, and also propagate
1332 ;;; derived-type information for the arg to all the VAR's refs.
1334 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1335 ;;; subtype of the argument's leaf type. This prevents type checking
1336 ;;; from being defeated, and also ensures that the best representation
1337 ;;; for the variable can be used.
1339 ;;; Substitution of individual references is inhibited if the
1340 ;;; reference is in a different component from the home. This can only
1341 ;;; happen with closures over top level lambda vars. In such cases,
1342 ;;; the references may have already been compiled, and thus can't be
1343 ;;; retroactively modified.
1345 ;;; If all of the variables are deleted (have no references) when we
1346 ;;; are done, then we delete the LET.
1348 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1350 (defun propagate-let-args (call fun)
1351 (declare (type combination call) (type clambda fun))
1352 (loop for arg in (combination-args call)
1353 and var in (lambda-vars fun) do
1354 (when (and arg (lvar-reoptimize arg))
1355 (setf (lvar-reoptimize arg) nil)
1357 ((lambda-var-sets var)
1358 (propagate-from-sets var (lvar-type arg)))
1359 ((let ((use (lvar-uses arg)))
1361 (let ((leaf (ref-leaf use)))
1362 (when (and (constant-reference-p use)
1363 (csubtypep (leaf-type leaf)
1364 ;; (NODE-DERIVED-TYPE USE) would
1365 ;; be better -- APD, 2003-05-15
1367 (propagate-to-refs var (lvar-type arg))
1368 (let ((use-component (node-component use)))
1369 (prog1 (substitute-leaf-if
1371 (cond ((eq (node-component ref) use-component)
1374 (aver (lambda-toplevelish-p (lambda-home fun)))
1378 ((and (null (rest (leaf-refs var)))
1379 (substitute-single-use-lvar arg var)))
1381 (propagate-to-refs var (lvar-type arg))))))
1383 (when (every #'not (combination-args call))
1388 ;;; This function is called when one of the args to a non-LET local
1389 ;;; call changes. For each changed argument corresponding to an unset
1390 ;;; variable, we compute the union of the types across all calls and
1391 ;;; propagate this type information to the var's refs.
1393 ;;; If the function has an XEP, then we don't do anything, since we
1394 ;;; won't discover anything.
1396 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1397 ;;; corresponding to changed arguments in CALL, since the only use in
1398 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1400 (defun propagate-local-call-args (call fun)
1401 (declare (type combination call) (type clambda fun))
1403 (unless (or (functional-entry-fun fun)
1404 (lambda-optional-dispatch fun))
1405 (let* ((vars (lambda-vars fun))
1406 (union (mapcar (lambda (arg var)
1408 (lvar-reoptimize arg)
1409 (null (basic-var-sets var)))
1411 (basic-combination-args call)
1413 (this-ref (lvar-use (basic-combination-fun call))))
1415 (dolist (arg (basic-combination-args call))
1417 (setf (lvar-reoptimize arg) nil)))
1419 (dolist (ref (leaf-refs fun))
1420 (let ((dest (node-dest ref)))
1421 (unless (or (eq ref this-ref) (not dest))
1423 (mapcar (lambda (this-arg old)
1425 (setf (lvar-reoptimize this-arg) nil)
1426 (type-union (lvar-type this-arg) old)))
1427 (basic-combination-args dest)
1430 (loop for var in vars
1432 when type do (propagate-to-refs var type))))
1436 ;;;; multiple values optimization
1438 ;;; Do stuff to notice a change to a MV combination node. There are
1439 ;;; two main branches here:
1440 ;;; -- If the call is local, then it is already a MV let, or should
1441 ;;; become one. Note that although all :LOCAL MV calls must eventually
1442 ;;; be converted to :MV-LETs, there can be a window when the call
1443 ;;; is local, but has not been LET converted yet. This is because
1444 ;;; the entry-point lambdas may have stray references (in other
1445 ;;; entry points) that have not been deleted yet.
1446 ;;; -- The call is full. This case is somewhat similar to the non-MV
1447 ;;; combination optimization: we propagate return type information and
1448 ;;; notice non-returning calls. We also have an optimization
1449 ;;; which tries to convert MV-CALLs into MV-binds.
1450 (defun ir1-optimize-mv-combination (node)
1451 (ecase (basic-combination-kind node)
1453 (let ((fun-lvar (basic-combination-fun node)))
1454 (when (lvar-reoptimize fun-lvar)
1455 (setf (lvar-reoptimize fun-lvar) nil)
1456 (maybe-let-convert (combination-lambda node))))
1457 (setf (lvar-reoptimize (first (basic-combination-args node))) nil)
1458 (when (eq (functional-kind (combination-lambda node)) :mv-let)
1459 (unless (convert-mv-bind-to-let node)
1460 (ir1-optimize-mv-bind node))))
1462 (let* ((fun (basic-combination-fun node))
1463 (fun-changed (lvar-reoptimize fun))
1464 (args (basic-combination-args node)))
1466 (setf (lvar-reoptimize fun) nil)
1467 (let ((type (lvar-type fun)))
1468 (when (fun-type-p type)
1469 (derive-node-type node (fun-type-returns type))))
1470 (maybe-terminate-block node nil)
1471 (let ((use (lvar-uses fun)))
1472 (when (and (ref-p use) (functional-p (ref-leaf use)))
1473 (convert-call-if-possible use node)
1474 (when (eq (basic-combination-kind node) :local)
1475 (maybe-let-convert (ref-leaf use))))))
1476 (unless (or (eq (basic-combination-kind node) :local)
1477 (eq (lvar-fun-name fun) '%throw))
1478 (ir1-optimize-mv-call node))
1480 (setf (lvar-reoptimize arg) nil))))
1484 ;;; Propagate derived type info from the values continuation to the
1486 (defun ir1-optimize-mv-bind (node)
1487 (declare (type mv-combination node))
1488 (let* ((arg (first (basic-combination-args node)))
1489 (vars (lambda-vars (combination-lambda node)))
1490 (n-vars (length vars))
1491 (types (values-type-in (lvar-derived-type arg)
1493 (loop for var in vars
1495 do (if (basic-var-sets var)
1496 (propagate-from-sets var type)
1497 (propagate-to-refs var type)))
1498 (setf (lvar-reoptimize arg) nil))
1501 ;;; If possible, convert a general MV call to an MV-BIND. We can do
1503 ;;; -- The call has only one argument, and
1504 ;;; -- The function has a known fixed number of arguments, or
1505 ;;; -- The argument yields a known fixed number of values.
1507 ;;; What we do is change the function in the MV-CALL to be a lambda
1508 ;;; that "looks like an MV bind", which allows
1509 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
1510 ;;; converted (the next time around.) This new lambda just calls the
1511 ;;; actual function with the MV-BIND variables as arguments. Note that
1512 ;;; this new MV bind is not let-converted immediately, as there are
1513 ;;; going to be stray references from the entry-point functions until
1514 ;;; they get deleted.
1516 ;;; In order to avoid loss of argument count checking, we only do the
1517 ;;; transformation according to a known number of expected argument if
1518 ;;; safety is unimportant. We can always convert if we know the number
1519 ;;; of actual values, since the normal call that we build will still
1520 ;;; do any appropriate argument count checking.
1522 ;;; We only attempt the transformation if the called function is a
1523 ;;; constant reference. This allows us to just splice the leaf into
1524 ;;; the new function, instead of trying to somehow bind the function
1525 ;;; expression. The leaf must be constant because we are evaluating it
1526 ;;; again in a different place. This also has the effect of squelching
1527 ;;; multiple warnings when there is an argument count error.
1528 (defun ir1-optimize-mv-call (node)
1529 (let ((fun (basic-combination-fun node))
1530 (*compiler-error-context* node)
1531 (ref (lvar-uses (basic-combination-fun node)))
1532 (args (basic-combination-args node)))
1534 (unless (and (ref-p ref) (constant-reference-p ref)
1536 (return-from ir1-optimize-mv-call))
1538 (multiple-value-bind (min max)
1539 (fun-type-nargs (lvar-type fun))
1541 (multiple-value-bind (types nvals)
1542 (values-types (lvar-derived-type (first args)))
1543 (declare (ignore types))
1544 (if (eq nvals :unknown) nil nvals))))
1547 (when (and min (< total-nvals min))
1549 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1552 (setf (basic-combination-kind node) :error)
1553 (return-from ir1-optimize-mv-call))
1554 (when (and max (> total-nvals max))
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)))
1562 (let ((count (cond (total-nvals)
1563 ((and (policy node (zerop verify-arg-count))
1568 (with-ir1-environment-from-node node
1569 (let* ((dums (make-gensym-list count))
1571 (fun (ir1-convert-lambda
1572 `(lambda (&optional ,@dums &rest ,ignore)
1573 (declare (ignore ,ignore))
1574 (funcall ,(ref-leaf ref) ,@dums)))))
1575 (change-ref-leaf ref fun)
1576 (aver (eq (basic-combination-kind node) :full))
1577 (locall-analyze-component *current-component*)
1578 (aver (eq (basic-combination-kind node) :local)))))))))
1582 ;;; (multiple-value-bind
1591 ;;; What we actually do is convert the VALUES combination into a
1592 ;;; normal LET combination calling the original :MV-LET lambda. If
1593 ;;; there are extra args to VALUES, discard the corresponding
1594 ;;; lvars. If there are insufficient args, insert references to NIL.
1595 (defun convert-mv-bind-to-let (call)
1596 (declare (type mv-combination call))
1597 (let* ((arg (first (basic-combination-args call)))
1598 (use (lvar-uses arg)))
1599 (when (and (combination-p use)
1600 (eq (lvar-fun-name (combination-fun use))
1602 (let* ((fun (combination-lambda call))
1603 (vars (lambda-vars fun))
1604 (vals (combination-args use))
1605 (nvars (length vars))
1606 (nvals (length vals)))
1607 (cond ((> nvals nvars)
1608 (mapc #'flush-dest (subseq vals nvars))
1609 (setq vals (subseq vals 0 nvars)))
1611 (with-ir1-environment-from-node use
1612 (let ((node-prev (node-prev use)))
1613 (setf (node-prev use) nil)
1614 (setf (ctran-next node-prev) nil)
1615 (collect ((res vals))
1616 (loop for count below (- nvars nvals)
1617 for prev = node-prev then ctran
1618 for ctran = (make-ctran)
1619 and lvar = (make-lvar use)
1620 do (reference-constant prev ctran lvar nil)
1622 finally (link-node-to-previous-ctran
1624 (setq vals (res)))))))
1625 (setf (combination-args use) vals)
1626 (flush-dest (combination-fun use))
1627 (let ((fun-lvar (basic-combination-fun call)))
1628 (setf (lvar-dest fun-lvar) use)
1629 (setf (combination-fun use) fun-lvar)
1630 (flush-lvar-externally-checkable-type fun-lvar))
1631 (setf (combination-kind use) :local)
1632 (setf (functional-kind fun) :let)
1633 (flush-dest (first (basic-combination-args call)))
1636 (reoptimize-lvar (first vals)))
1637 (propagate-to-args use fun)
1638 (reoptimize-call use))
1642 ;;; (values-list (list x y z))
1647 ;;; In implementation, this is somewhat similar to
1648 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
1649 ;;; args of the VALUES-LIST call, flushing the old argument lvar
1650 ;;; (allowing the LIST to be flushed.)
1652 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
1653 (defoptimizer (values-list optimizer) ((list) node)
1654 (let ((use (lvar-uses list)))
1655 (when (and (combination-p use)
1656 (eq (lvar-fun-name (combination-fun use))
1659 ;; FIXME: VALUES might not satisfy an assertion on NODE-CONT.
1660 (change-ref-leaf (lvar-uses (combination-fun node))
1661 (find-free-fun 'values "in a strange place"))
1662 (setf (combination-kind node) :full)
1663 (let ((args (combination-args use)))
1665 (setf (lvar-dest arg) node)
1666 (flush-lvar-externally-checkable-type arg))
1667 (setf (combination-args use) nil)
1669 (setf (combination-args node) args))
1672 ;;; If VALUES appears in a non-MV context, then effectively convert it
1673 ;;; to a PROG1. This allows the computation of the additional values
1674 ;;; to become dead code.
1675 (deftransform values ((&rest vals) * * :node node)
1676 (unless (lvar-single-value-p (node-lvar node))
1677 (give-up-ir1-transform))
1678 (setf (node-derived-type node) *wild-type*)
1679 (principal-lvar-single-valuify (node-lvar node))
1681 (let ((dummies (make-gensym-list (length (cdr vals)))))
1682 `(lambda (val ,@dummies)
1683 (declare (ignore ,@dummies))
1689 (defun ir1-optimize-cast (cast &optional do-not-optimize)
1690 (declare (type cast cast))
1691 (let* ((value (cast-value cast))
1692 (value-type (lvar-derived-type value))
1693 (atype (cast-asserted-type cast))
1694 (int (values-type-intersection value-type atype)))
1695 (derive-node-type cast int)
1696 (when (eq int *empty-type*)
1697 (unless (eq value-type *empty-type*)
1699 ;; FIXME: Do it in one step.
1702 `(multiple-value-call #'list 'dummy))
1705 ;; FIXME: Derived type.
1706 `(%compile-time-type-error 'dummy
1707 ',(type-specifier atype)
1708 ',(type-specifier value-type)))
1709 ;; KLUDGE: FILTER-CONTINUATION does not work for
1710 ;; non-returning functions, so we declare the return type of
1711 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
1713 (setq value (cast-value cast))
1714 (derive-node-type (lvar-uses value) *empty-type*)
1715 (maybe-terminate-block (lvar-uses value) nil)
1716 ;; FIXME: Is it necessary?
1717 (aver (null (block-pred (node-block cast))))
1718 (setf (block-delete-p (node-block cast)) t)
1719 (return-from ir1-optimize-cast)))
1720 (when (eq (node-derived-type cast) *empty-type*)
1721 (maybe-terminate-block cast nil))
1723 (when (not do-not-optimize)
1724 (let ((lvar (node-lvar cast)))
1725 (when (values-subtypep value-type (cast-asserted-type cast))
1726 (delete-filter cast lvar value)
1728 (reoptimize-lvar lvar)
1729 (when (lvar-single-value-p lvar)
1730 (note-single-valuified-lvar lvar)))
1731 (return-from ir1-optimize-cast t))
1733 (when (and (listp (lvar-uses value))
1735 ;; Pathwise removing of CAST
1736 (let ((ctran (node-next cast))
1737 (dest (lvar-dest lvar))
1740 (do-uses (use value)
1741 (when (and (values-subtypep (node-derived-type use) atype)
1742 (immediately-used-p value use))
1744 (when ctran (ensure-block-start ctran))
1745 (setq next-block (first (block-succ (node-block cast)))))
1746 (%delete-lvar-use use)
1747 (add-lvar-use use lvar)
1748 (unlink-blocks (node-block use) (node-block cast))
1749 (link-blocks (node-block use) next-block)
1750 (when (and (return-p dest)
1751 (basic-combination-p use)
1752 (eq (basic-combination-kind use) :local))
1754 (dolist (use (merges))
1755 (merge-tail-sets use)))))))
1757 (when (and (cast-%type-check cast)
1758 (values-subtypep value-type
1759 (cast-type-to-check cast)))
1760 (setf (cast-%type-check cast) nil)))
1762 (unless do-not-optimize
1763 (setf (node-reoptimize cast) nil)))