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-continuation-p (thing)
24 (and (continuation-p thing)
25 (let ((use (principal-continuation-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 (continuation) t) continuation-value))
31 (defun continuation-value (cont)
32 (let ((use (principal-continuation-use cont)))
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 continuation-derived-type))
55 (defun continuation-derived-type (cont)
56 (declare (type continuation cont))
57 (or (continuation-%derived-type cont)
58 (setf (continuation-%derived-type cont)
59 (%continuation-derived-type cont))))
60 (defun %continuation-derived-type (cont)
61 (declare (type continuation cont))
62 (ecase (continuation-kind cont)
63 ((:block-start :deleted-block-start)
64 (let ((uses (block-start-uses (continuation-block cont))))
66 (do ((res (node-derived-type (first uses))
67 (values-type-union (node-derived-type (first current))
69 (current (rest uses) (rest current)))
73 (node-derived-type (continuation-use cont)))))
75 ;;; Return the derived type for CONT's first value. This is guaranteed
76 ;;; not to be a VALUES or FUNCTION type.
77 (declaim (ftype (sfunction (continuation) ctype) continuation-type))
78 (defun continuation-type (cont)
79 (single-value-type (continuation-derived-type cont)))
81 ;;; If CONT is an argument of a function, return a type which the
82 ;;; function checks CONT for.
83 #!-sb-fluid (declaim (inline continuation-externally-checkable-type))
84 (defun continuation-externally-checkable-type (cont)
85 (or (continuation-%externally-checkable-type cont)
86 (%continuation-%externally-checkable-type cont)))
87 (defun %continuation-%externally-checkable-type (cont)
88 (declare (type continuation cont))
89 (let ((dest (continuation-dest cont)))
90 (if (not (and dest (combination-p dest)))
91 ;; TODO: MV-COMBINATION
92 (setf (continuation-%externally-checkable-type cont) *wild-type*)
93 (let* ((fun (combination-fun dest))
94 (args (combination-args dest))
95 (fun-type (continuation-type fun)))
96 (setf (continuation-%externally-checkable-type fun) *wild-type*)
97 (if (or (not (fun-type-p fun-type))
98 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
99 (fun-type-wild-args fun-type))
100 (progn (dolist (arg args)
102 (setf (continuation-%externally-checkable-type arg)
105 (let* ((arg-types (append (fun-type-required fun-type)
106 (fun-type-optional fun-type)
107 (let ((rest (list (or (fun-type-rest fun-type)
109 (setf (cdr rest) rest)))))
112 for arg of-type continuation in args
113 and type of-type ctype in arg-types
115 (setf (continuation-%externally-checkable-type arg)
116 (coerce-to-values type))))
117 (continuation-%externally-checkable-type cont)))))))
118 (declaim (inline flush-continuation-externally-checkable-type))
119 (defun flush-continuation-externally-checkable-type (cont)
120 (declare (type continuation cont))
121 (setf (continuation-%externally-checkable-type cont) nil))
123 ;;;; interface routines used by optimizers
125 ;;; This function is called by optimizers to indicate that something
126 ;;; interesting has happened to the value of CONT. Optimizers must
127 ;;; make sure that they don't call for reoptimization when nothing has
128 ;;; happened, since optimization will fail to terminate.
130 ;;; We clear any cached type for the continuation and set the
131 ;;; reoptimize flags on everything in sight, unless the continuation
132 ;;; is deleted (in which case we do nothing.)
134 ;;; Since this can get called during IR1 conversion, we have to be
135 ;;; careful not to fly into space when the DEST's PREV is missing.
136 (defun reoptimize-continuation (cont)
137 (declare (type continuation cont))
138 (setf (continuation-%derived-type cont) nil)
139 (unless (member (continuation-kind cont) '(:deleted :unused))
140 (let ((dest (continuation-dest cont)))
142 (setf (continuation-reoptimize cont) t)
143 (setf (node-reoptimize dest) t)
144 (let ((prev (node-prev dest)))
146 (let* ((block (continuation-block prev))
147 (component (block-component block)))
148 (when (typep dest 'cif)
149 (setf (block-test-modified block) t))
150 (setf (block-reoptimize block) t)
151 (setf (component-reoptimize component) t))))))
153 (setf (block-type-check (node-block node)) t)))
156 (defun reoptimize-continuation-uses (cont)
157 (declare (type continuation cont))
158 (dolist (use (find-uses cont))
159 (setf (node-reoptimize use) t)
160 (setf (block-reoptimize (node-block use)) t)
161 (setf (component-reoptimize (node-component use)) t)))
163 ;;; Annotate NODE to indicate that its result has been proven to be
164 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
165 ;;; only correct way to supply information discovered about a node's
166 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
167 ;;; information may be lost and reoptimization may not happen.
169 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
170 ;;; intersection is different from the old type, then we do a
171 ;;; REOPTIMIZE-CONTINUATION on the NODE-CONT.
172 (defun derive-node-type (node rtype)
173 (declare (type node node) (type ctype rtype))
174 (let ((node-type (node-derived-type node)))
175 (unless (eq node-type rtype)
176 (let ((int (values-type-intersection node-type rtype))
177 (cont (node-cont node)))
178 (when (type/= node-type int)
179 (when (and *check-consistency*
180 (eq int *empty-type*)
181 (not (eq rtype *empty-type*)))
182 (let ((*compiler-error-context* node))
184 "New inferred type ~S conflicts with old type:~
185 ~% ~S~%*** possible internal error? Please report this."
186 (type-specifier rtype) (type-specifier node-type))))
187 (setf (node-derived-type node) int)
188 (when (and (ref-p node)
189 (lambda-var-p (ref-leaf node)))
190 (let ((type (single-value-type int)))
191 (when (and (member-type-p type)
192 (null (rest (member-type-members type))))
193 (change-ref-leaf node (find-constant
194 (first (member-type-members type)))))))
195 (reoptimize-continuation cont)))))
198 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
199 ;;; error for CONT's value not to be TYPEP to TYPE. We implement it
200 ;;; splitting off DEST a new CAST node. If we improve the assertion,
201 ;;; we set TYPE-CHECK and TYPE-ASSERTED to guarantee that the new
202 ;;; assertion will be checked. We return the new "argument"
203 ;;; continuation of DEST.
204 (defun assert-continuation-type (cont type policy)
205 (declare (type continuation cont) (type ctype type))
206 (if (values-subtypep (continuation-derived-type cont) type)
208 (let* ((dest (continuation-dest cont))
209 (prev-cont (node-prev dest)))
211 (with-ir1-environment-from-node dest
212 (let* ((cast (make-cast cont type policy))
213 (checked-value (make-continuation)))
214 (setf (continuation-next prev-cont) cast
215 (node-prev cast) prev-cont)
216 (use-continuation cast checked-value)
217 (link-node-to-previous-continuation dest checked-value)
218 (substitute-continuation checked-value cont)
219 (setf (continuation-dest cont) cast)
220 (reoptimize-continuation cont)
223 ;;; Assert that CALL is to a function of the specified TYPE. It is
224 ;;; assumed that the call is legal and has only constants in the
225 ;;; keyword positions.
226 (defun assert-call-type (call type)
227 (declare (type combination call) (type fun-type type))
228 (derive-node-type call (fun-type-returns type))
229 (let ((args (combination-args call))
230 (policy (lexenv-policy (node-lexenv call))))
231 (dolist (req (fun-type-required type))
232 (when (null args) (return-from assert-call-type))
233 (let ((arg (pop args)))
234 (assert-continuation-type arg req policy)))
235 (dolist (opt (fun-type-optional type))
236 (when (null args) (return-from assert-call-type))
237 (let ((arg (pop args)))
238 (assert-continuation-type arg opt policy)))
240 (let ((rest (fun-type-rest type)))
243 (assert-continuation-type arg rest policy))))
245 (dolist (key (fun-type-keywords type))
246 (let ((name (key-info-name key)))
247 (do ((arg args (cddr arg)))
249 (when (eq (continuation-value (first arg)) name)
250 (assert-continuation-type
251 (second arg) (key-info-type key)
257 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
258 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
259 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
260 ;;; we are done, then another iteration would be beneficial.
261 (defun ir1-optimize (component)
262 (declare (type component component))
263 (setf (component-reoptimize component) nil)
264 (do-blocks (block component)
266 ;; We delete blocks when there is either no predecessor or the
267 ;; block is in a lambda that has been deleted. These blocks
268 ;; would eventually be deleted by DFO recomputation, but doing
269 ;; it here immediately makes the effect available to IR1
271 ((or (block-delete-p block)
272 (null (block-pred block)))
273 (delete-block block))
274 ((eq (functional-kind (block-home-lambda block)) :deleted)
275 ;; Preserve the BLOCK-SUCC invariant that almost every block has
276 ;; one successor (and a block with DELETE-P set is an acceptable
278 (mark-for-deletion block)
279 (delete-block block))
282 (let ((succ (block-succ block)))
283 (unless (singleton-p succ)
286 (let ((last (block-last block)))
289 (flush-dest (if-test last))
290 (when (unlink-node last)
293 (when (maybe-delete-exit last)
296 (unless (join-successor-if-possible block)
299 (when (and (block-reoptimize block) (block-component block))
300 (aver (not (block-delete-p block)))
301 (ir1-optimize-block block))
303 (cond ((and (block-delete-p block) (block-component block))
304 (delete-block block))
305 ((and (block-flush-p block) (block-component block))
306 (flush-dead-code block))))))
310 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
313 ;;; Note that although they are cleared here, REOPTIMIZE flags might
314 ;;; still be set upon return from this function, meaning that further
315 ;;; optimization is wanted (as a consequence of optimizations we did).
316 (defun ir1-optimize-block (block)
317 (declare (type cblock block))
318 ;; We clear the node and block REOPTIMIZE flags before doing the
319 ;; optimization, not after. This ensures that the node or block will
320 ;; be reoptimized if necessary.
321 (setf (block-reoptimize block) nil)
322 (do-nodes (node cont block :restart-p t)
323 (when (node-reoptimize node)
324 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
325 (setf (node-reoptimize node) nil)
329 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
330 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
331 ;; any argument changes.
332 (ir1-optimize-combination node))
334 (ir1-optimize-if node))
336 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
337 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
338 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
340 (setf (node-reoptimize node) t)
341 (ir1-optimize-return node))
343 (ir1-optimize-mv-combination node))
345 ;; With an EXIT, we derive the node's type from the VALUE's
346 ;; type. We don't propagate CONT's assertion to the VALUE,
347 ;; since if we did, this would move the checking of CONT's
348 ;; assertion to the exit. This wouldn't work with CATCH and
349 ;; UWP, where the EXIT node is just a placeholder for the
350 ;; actual unknown exit.
351 (let ((value (exit-value node)))
353 (derive-node-type node (continuation-derived-type value)))))
355 (ir1-optimize-set node))
357 (ir1-optimize-cast node)))))
361 ;;; Try to join with a successor block. If we succeed, we return true,
363 (defun join-successor-if-possible (block)
364 (declare (type cblock block))
365 (let ((next (first (block-succ block))))
366 (when (block-start next) ; NEXT is not an END-OF-COMPONENT marker
367 (let* ((last (block-last block))
368 (last-cont (node-cont last))
369 (next-cont (block-start next)))
370 (cond (;; We cannot combine with a successor block if:
372 ;; The successor has more than one predecessor.
373 (rest (block-pred next))
374 ;; The last node's CONT is also used somewhere else.
375 ;; (as in (IF <cond> (M-V-PROG1 ...) (M-V-PROG1 ...)))
376 (not (eq (continuation-use last-cont) last))
377 ;; The successor is the current block (infinite loop).
379 ;; The next block has a different cleanup, and thus
380 ;; we may want to insert cleanup code between the
381 ;; two blocks at some point.
382 (not (eq (block-end-cleanup block)
383 (block-start-cleanup next)))
384 ;; The next block has a different home lambda, and
385 ;; thus the control transfer is a non-local exit.
386 (not (eq (block-home-lambda block)
387 (block-home-lambda next))))
389 ;; Joining is easy when the successor's START
390 ;; continuation is the same from our LAST's CONT.
391 ((eq last-cont next-cont)
392 (join-blocks block next)
394 ;; If they differ, then we can still join when the last
395 ;; continuation has no next and the next continuation
397 ((and (null (block-start-uses next))
398 (eq (continuation-kind last-cont) :inside-block))
399 ;; In this case, we replace the next
400 ;; continuation with the last before joining the blocks.
401 (let ((next-node (continuation-next next-cont)))
402 ;; If NEXT-CONT does have a dest, it must be
403 ;; unreachable, since there are no USES.
404 ;; DELETE-CONTINUATION will mark the dest block as
405 ;; DELETE-P [and also this block, unless it is no
406 ;; longer backward reachable from the dest block.]
407 (delete-continuation next-cont)
408 (setf (node-prev next-node) last-cont)
409 (setf (continuation-next last-cont) next-node)
410 (setf (block-start next) last-cont)
411 (join-blocks block next))
413 ((and (null (block-start-uses next))
414 (not (exit-p (continuation-dest last-cont)))
415 (null (continuation-lexenv-uses last-cont)))
416 (assert (null (find-uses next-cont)))
417 (when (continuation-dest last-cont)
418 (substitute-continuation next-cont last-cont))
419 (delete-continuation-use last)
420 (add-continuation-use last next-cont)
421 (setf (continuation-%derived-type next-cont) nil)
422 (join-blocks block next)
427 ;;; Join together two blocks which have the same ending/starting
428 ;;; continuation. The code in BLOCK2 is moved into BLOCK1 and BLOCK2
429 ;;; is deleted from the DFO. We combine the optimize flags for the two
430 ;;; blocks so that any indicated optimization gets done.
431 (defun join-blocks (block1 block2)
432 (declare (type cblock block1 block2))
433 (let* ((last (block-last block2))
434 (last-cont (node-cont last))
435 (succ (block-succ block2))
436 (start2 (block-start block2)))
437 (do ((cont start2 (node-cont (continuation-next cont))))
439 (when (eq (continuation-kind last-cont) :inside-block)
440 (setf (continuation-block last-cont) block1)))
441 (setf (continuation-block cont) block1))
443 (unlink-blocks block1 block2)
445 (unlink-blocks block2 block)
446 (link-blocks block1 block))
448 (setf (block-last block1) last)
449 (setf (continuation-kind start2) :inside-block))
451 (setf (block-flags block1)
452 (attributes-union (block-flags block1)
454 (block-attributes type-asserted test-modified)))
456 (let ((next (block-next block2))
457 (prev (block-prev block2)))
458 (setf (block-next prev) next)
459 (setf (block-prev next) prev))
463 ;;; Delete any nodes in BLOCK whose value is unused and which have no
464 ;;; side effects. We can delete sets of lexical variables when the set
465 ;;; variable has no references.
466 (defun flush-dead-code (block)
467 (declare (type cblock block))
468 (do-nodes-backwards (node cont block)
469 (unless (continuation-dest cont)
475 (let ((info (combination-kind node)))
476 (when (fun-info-p info)
477 (let ((attr (fun-info-attributes info)))
478 (when (and (not (ir1-attributep attr call))
479 ;; ### For now, don't delete potentially
480 ;; flushable calls when they have the CALL
481 ;; attribute. Someday we should look at the
482 ;; functional args to determine if they have
484 (if (policy node (= safety 3))
485 (ir1-attributep attr flushable)
486 (ir1-attributep attr unsafely-flushable)))
487 (flush-combination node))))))
489 (when (eq (basic-combination-kind node) :local)
490 (let ((fun (combination-lambda node)))
491 (when (dolist (var (lambda-vars fun) t)
492 (when (or (leaf-refs var)
493 (lambda-var-sets var))
495 (flush-dest (first (basic-combination-args node)))
498 (let ((value (exit-value node)))
501 (setf (exit-value node) nil))))
503 (let ((var (set-var node)))
504 (when (and (lambda-var-p var)
505 (null (leaf-refs var)))
506 (flush-dest (set-value node))
507 (setf (basic-var-sets var)
508 (delete node (basic-var-sets var)))
509 (unlink-node node))))
511 (unless (cast-type-check node)
512 (flush-dest (cast-value node))
513 (unlink-node node))))))
515 (setf (block-flush-p block) nil)
518 ;;;; local call return type propagation
520 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
521 ;;; flag set. It iterates over the uses of the RESULT, looking for
522 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
523 ;;; call, then we union its type together with the types of other such
524 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
525 ;;; type with the RESULT's asserted type. We can make this
526 ;;; intersection now (potentially before type checking) because this
527 ;;; assertion on the result will eventually be checked (if
530 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
531 ;;; combination, which may change the succesor of the call to be the
532 ;;; called function, and if so, checks if the call can become an
533 ;;; assignment. If we convert to an assignment, we abort, since the
534 ;;; RETURN has been deleted.
535 (defun find-result-type (node)
536 (declare (type creturn node))
537 (let ((result (return-result node)))
538 (collect ((use-union *empty-type* values-type-union))
539 (do-uses (use result)
540 (cond ((and (basic-combination-p use)
541 (eq (basic-combination-kind use) :local))
542 (aver (eq (lambda-tail-set (node-home-lambda use))
543 (lambda-tail-set (combination-lambda use))))
544 (when (combination-p use)
545 (when (nth-value 1 (maybe-convert-tail-local-call use))
546 (return-from find-result-type (values)))))
548 (use-union (node-derived-type use)))))
550 ;; (values-type-intersection
551 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
555 (setf (return-result-type node) int))))
558 ;;; Do stuff to realize that something has changed about the value
559 ;;; delivered to a return node. Since we consider the return values of
560 ;;; all functions in the tail set to be equivalent, this amounts to
561 ;;; bringing the entire tail set up to date. We iterate over the
562 ;;; returns for all the functions in the tail set, reanalyzing them
563 ;;; all (not treating NODE specially.)
565 ;;; When we are done, we check whether the new type is different from
566 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
567 ;;; all the continuations for references to functions in the tail set.
568 ;;; This will cause IR1-OPTIMIZE-COMBINATION to derive the new type as
569 ;;; the results of the calls.
570 (defun ir1-optimize-return (node)
571 (declare (type creturn node))
572 (let* ((tails (lambda-tail-set (return-lambda node)))
573 (funs (tail-set-funs tails)))
574 (collect ((res *empty-type* values-type-union))
576 (let ((return (lambda-return fun)))
578 (when (node-reoptimize return)
579 (setf (node-reoptimize return) nil)
580 (find-result-type return))
581 (res (return-result-type return)))))
583 (when (type/= (res) (tail-set-type tails))
584 (setf (tail-set-type tails) (res))
585 (dolist (fun (tail-set-funs tails))
586 (dolist (ref (leaf-refs fun))
587 (reoptimize-continuation (node-cont ref)))))))
593 ;;; If the test has multiple uses, replicate the node when possible.
594 ;;; Also check whether the predicate is known to be true or false,
595 ;;; deleting the IF node in favor of the appropriate branch when this
597 (defun ir1-optimize-if (node)
598 (declare (type cif node))
599 (let ((test (if-test node))
600 (block (node-block node)))
602 (when (and (eq (block-start block) test)
603 (eq (continuation-next test) node)
604 (rest (block-start-uses block)))
606 (when (immediately-used-p test use)
607 (convert-if-if use node)
608 (when (continuation-use test) (return)))))
610 (let* ((type (continuation-type test))
612 (cond ((constant-continuation-p test)
613 (if (continuation-value test)
614 (if-alternative node)
615 (if-consequent node)))
616 ((not (types-equal-or-intersect type (specifier-type 'null)))
617 (if-alternative node))
618 ((type= type (specifier-type 'null))
619 (if-consequent node)))))
622 (when (rest (block-succ block))
623 (unlink-blocks block victim))
624 (setf (component-reanalyze (node-component node)) t)
625 (unlink-node node))))
628 ;;; Create a new copy of an IF node that tests the value of the node
629 ;;; USE. The test must have >1 use, and must be immediately used by
630 ;;; USE. NODE must be the only node in its block (implying that
631 ;;; block-start = if-test).
633 ;;; This optimization has an effect semantically similar to the
634 ;;; source-to-source transformation:
635 ;;; (IF (IF A B C) D E) ==>
636 ;;; (IF A (IF B D E) (IF C D E))
638 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
639 ;;; node so that dead code deletion notes will definitely not consider
640 ;;; either node to be part of the original source. One node might
641 ;;; become unreachable, resulting in a spurious note.
642 (defun convert-if-if (use node)
643 (declare (type node use) (type cif node))
644 (with-ir1-environment-from-node node
645 (let* ((block (node-block node))
646 (test (if-test node))
647 (cblock (if-consequent node))
648 (ablock (if-alternative node))
649 (use-block (node-block use))
650 (dummy-cont (make-continuation))
651 (new-cont (make-continuation))
652 (new-node (make-if :test new-cont
654 :alternative ablock))
655 (new-block (continuation-starts-block new-cont)))
656 (link-node-to-previous-continuation new-node new-cont)
657 (setf (continuation-dest new-cont) new-node)
658 (flush-continuation-externally-checkable-type new-cont)
659 (add-continuation-use new-node dummy-cont)
660 (setf (block-last new-block) new-node)
662 (unlink-blocks use-block block)
663 (delete-continuation-use use)
664 (add-continuation-use use new-cont)
665 (link-blocks use-block new-block)
667 (link-blocks new-block cblock)
668 (link-blocks new-block ablock)
670 (push "<IF Duplication>" (node-source-path node))
671 (push "<IF Duplication>" (node-source-path new-node))
673 (reoptimize-continuation test)
674 (reoptimize-continuation new-cont)
675 (setf (component-reanalyze *current-component*) t)))
678 ;;;; exit IR1 optimization
680 ;;; This function attempts to delete an exit node, returning true if
681 ;;; it deletes the block as a consequence:
682 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
683 ;;; anything, since there is nothing to be done.
684 ;;; -- If the exit node and its ENTRY have the same home lambda then
685 ;;; we know the exit is local, and can delete the exit. We change
686 ;;; uses of the Exit-Value to be uses of the original continuation,
687 ;;; then unlink the node. If the exit is to a TR context, then we
688 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
689 ;;; their value to this exit.
690 ;;; -- If there is no value (as in a GO), then we skip the value
693 ;;; This function is also called by environment analysis, since it
694 ;;; wants all exits to be optimized even if normal optimization was
696 (defun maybe-delete-exit (node)
697 (declare (type exit node))
698 (let ((value (exit-value node))
699 (entry (exit-entry node))
700 (cont (node-cont node)))
702 (eq (node-home-lambda node) (node-home-lambda entry)))
703 (setf (entry-exits entry) (delete node (entry-exits entry)))
705 (delete-filter node cont value)
706 (unlink-node node)))))
709 ;;;; combination IR1 optimization
711 ;;; Report as we try each transform?
713 (defvar *show-transforms-p* nil)
715 ;;; Do IR1 optimizations on a COMBINATION node.
716 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
717 (defun ir1-optimize-combination (node)
718 (when (continuation-reoptimize (basic-combination-fun node))
719 (propagate-fun-change node))
720 (let ((args (basic-combination-args node))
721 (kind (basic-combination-kind node)))
724 (let ((fun (combination-lambda node)))
725 (if (eq (functional-kind fun) :let)
726 (propagate-let-args node fun)
727 (propagate-local-call-args node fun))))
731 (setf (continuation-reoptimize arg) nil))))
735 (setf (continuation-reoptimize arg) nil)))
737 (let ((attr (fun-info-attributes kind)))
738 (when (and (ir1-attributep attr foldable)
739 ;; KLUDGE: The next test could be made more sensitive,
740 ;; only suppressing constant-folding of functions with
741 ;; CALL attributes when they're actually passed
742 ;; function arguments. -- WHN 19990918
743 (not (ir1-attributep attr call))
744 (every #'constant-continuation-p args)
745 (continuation-dest (node-cont node))
746 ;; Even if the function is foldable in principle,
747 ;; it might be one of our low-level
748 ;; implementation-specific functions. Such
749 ;; functions don't necessarily exist at runtime on
750 ;; a plain vanilla ANSI Common Lisp
751 ;; cross-compilation host, in which case the
752 ;; cross-compiler can't fold it because the
753 ;; cross-compiler doesn't know how to evaluate it.
755 (fboundp (combination-fun-source-name node)))
756 (constant-fold-call node)
757 (return-from ir1-optimize-combination)))
759 (let ((fun (fun-info-derive-type kind)))
761 (let ((res (funcall fun node)))
763 (derive-node-type node (coerce-to-values res))
764 (maybe-terminate-block node nil)))))
766 (let ((fun (fun-info-optimizer kind)))
767 (unless (and fun (funcall fun node))
768 (dolist (x (fun-info-transforms kind))
770 (when *show-transforms-p*
771 (let* ((cont (basic-combination-fun node))
772 (fname (continuation-fun-name cont t)))
773 (/show "trying transform" x (transform-function x) "for" fname)))
774 (unless (ir1-transform node x)
776 (when *show-transforms-p*
777 (/show "quitting because IR1-TRANSFORM result was NIL"))
782 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
783 ;;; the block there, and link it to the component tail. We also change
784 ;;; the NODE's CONT to be a dummy continuation to prevent the use from
785 ;;; confusing things.
787 ;;; Except when called during IR1 [FIXME: What does this mean? Except
788 ;;; during IR1 conversion? What about IR1 optimization?], we delete
789 ;;; the continuation if it has no other uses. (If it does have other
790 ;;; uses, we reoptimize.)
792 ;;; Termination on the basis of a continuation type is
794 ;;; -- The continuation is deleted (hence the assertion is spurious), or
795 ;;; -- We are in IR1 conversion (where THE assertions are subject to
797 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p)
798 (declare (type (or basic-combination cast) node))
799 (let* ((block (node-block node))
800 (cont (node-cont node))
801 (tail (component-tail (block-component block)))
802 (succ (first (block-succ block))))
803 (unless (or (and (eq node (block-last block)) (eq succ tail))
804 (block-delete-p block))
805 (when (or (and (not (or ir1-converting-not-optimizing-p
806 (eq (continuation-kind cont) :deleted)))
807 (eq (continuation-derived-type cont) *empty-type*))
808 (eq (node-derived-type node) *empty-type*))
809 (cond (ir1-converting-not-optimizing-p
810 (delete-continuation-use node)
813 (aver (and (eq (block-last block) node)
814 (eq (continuation-kind cont) :block-start))))
816 (setf (block-last block) node)
817 (link-blocks block (continuation-starts-block cont)))))
819 (node-ends-block node)
820 (delete-continuation-use node)
821 (if (eq (continuation-kind cont) :unused)
822 (delete-continuation cont)
823 (reoptimize-continuation cont))))
825 (unlink-blocks block (first (block-succ block)))
826 (setf (component-reanalyze (block-component block)) t)
827 (aver (not (block-succ block)))
828 (link-blocks block tail)
829 (add-continuation-use node (make-continuation))
832 ;;; This is called both by IR1 conversion and IR1 optimization when
833 ;;; they have verified the type signature for the call, and are
834 ;;; wondering if something should be done to special-case the call. If
835 ;;; CALL is a call to a global function, then see whether it defined
837 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
838 ;;; the expansion and change the call to call it. Expansion is
839 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
840 ;;; true, we never expand, since this function has already been
841 ;;; converted. Local call analysis will duplicate the definition
842 ;;; if necessary. We claim that the parent form is LABELS for
843 ;;; context declarations, since we don't want it to be considered
844 ;;; a real global function.
845 ;;; -- If it is a known function, mark it as such by setting the KIND.
847 ;;; We return the leaf referenced (NIL if not a leaf) and the
848 ;;; FUN-INFO assigned.
850 ;;; FIXME: The IR1-CONVERTING-NOT-OPTIMIZING-P argument is what the
851 ;;; old CMU CL code called IR1-P, without explanation. My (WHN
852 ;;; 2002-01-09) tentative understanding of it is that we can call this
853 ;;; operation either in initial IR1 conversion or in later IR1
854 ;;; optimization, and it tells which is which. But it would be good
855 ;;; for someone who really understands it to check whether this is
857 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
858 (declare (type combination call))
859 (let* ((ref (continuation-use (basic-combination-fun call)))
860 (leaf (when (ref-p ref) (ref-leaf ref)))
861 (inlinep (if (defined-fun-p leaf)
862 (defined-fun-inlinep leaf)
865 ((eq inlinep :notinline) (values nil nil))
866 ((not (and (global-var-p leaf)
867 (eq (global-var-kind leaf) :global-function)))
872 ((nil :maybe-inline) (policy call (zerop space))))
874 (defined-fun-inline-expansion leaf)
875 (let ((fun (defined-fun-functional leaf)))
877 (and (eq inlinep :inline) (functional-kind fun))))
878 (inline-expansion-ok call))
879 (flet (;; FIXME: Is this what the old CMU CL internal documentation
880 ;; called semi-inlining? A more descriptive name would
881 ;; be nice. -- WHN 2002-01-07
883 (let ((res (ir1-convert-lambda-for-defun
884 (defined-fun-inline-expansion leaf)
886 #'ir1-convert-inline-lambda)))
887 (setf (defined-fun-functional leaf) res)
888 (change-ref-leaf ref res))))
889 (if ir1-converting-not-optimizing-p
891 (with-ir1-environment-from-node call
893 (locall-analyze-component *current-component*))))
895 (values (ref-leaf (continuation-use (basic-combination-fun call)))
898 (let ((info (info :function :info (leaf-source-name leaf))))
900 (values leaf (setf (basic-combination-kind call) info))
901 (values leaf nil)))))))
903 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
904 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
905 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
906 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
907 ;;; syntax check, arg/result type processing, but still call
908 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
909 ;;; and that checking is done by local call analysis.
910 (defun validate-call-type (call type ir1-converting-not-optimizing-p)
911 (declare (type combination call) (type ctype type))
912 (cond ((not (fun-type-p type))
913 (aver (multiple-value-bind (val win)
914 (csubtypep type (specifier-type 'function))
916 (recognize-known-call call ir1-converting-not-optimizing-p))
917 ((valid-fun-use call type
918 :argument-test #'always-subtypep
919 :result-test #'always-subtypep
920 ;; KLUDGE: Common Lisp is such a dynamic
921 ;; language that all we can do here in
922 ;; general is issue a STYLE-WARNING. It
923 ;; would be nice to issue a full WARNING
924 ;; in the special case of of type
925 ;; mismatches within a compilation unit
926 ;; (as in section 3.2.2.3 of the spec)
927 ;; but at least as of sbcl-0.6.11, we
928 ;; don't keep track of whether the
929 ;; mismatched data came from the same
930 ;; compilation unit, so we can't do that.
933 ;; FIXME: Actually, I think we could
934 ;; issue a full WARNING if the call
935 ;; violates a DECLAIM FTYPE.
936 :lossage-fun #'compiler-style-warn
937 :unwinnage-fun #'compiler-note)
938 (assert-call-type call type)
939 (maybe-terminate-block call ir1-converting-not-optimizing-p)
940 (recognize-known-call call ir1-converting-not-optimizing-p))
942 (setf (combination-kind call) :error)
945 ;;; This is called by IR1-OPTIMIZE when the function for a call has
946 ;;; changed. If the call is local, we try to LET-convert it, and
947 ;;; derive the result type. If it is a :FULL call, we validate it
948 ;;; against the type, which recognizes known calls, does inline
949 ;;; expansion, etc. If a call to a predicate in a non-conditional
950 ;;; position or to a function with a source transform, then we
951 ;;; reconvert the form to give IR1 another chance.
952 (defun propagate-fun-change (call)
953 (declare (type combination call))
954 (let ((*compiler-error-context* call)
955 (fun-cont (basic-combination-fun call)))
956 (setf (continuation-reoptimize fun-cont) nil)
957 (case (combination-kind call)
959 (let ((fun (combination-lambda call)))
960 (maybe-let-convert fun)
961 (unless (member (functional-kind fun) '(:let :assignment :deleted))
962 (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
964 (multiple-value-bind (leaf info)
965 (validate-call-type call (continuation-type fun-cont) nil)
966 (cond ((functional-p leaf)
967 (convert-call-if-possible
968 (continuation-use (basic-combination-fun call))
971 ((and (leaf-has-source-name-p leaf)
972 (or (info :function :source-transform (leaf-source-name leaf))
974 (ir1-attributep (fun-info-attributes info)
976 (let ((dest (continuation-dest (node-cont call))))
977 (and dest (not (if-p dest)))))))
978 (let ((name (leaf-source-name leaf))
979 (dummies (make-gensym-list
980 (length (combination-args call)))))
983 (,@(if (symbolp name)
987 (leaf-source-name leaf)))))))))
990 ;;;; known function optimization
992 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
993 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
994 ;;; replace it, otherwise add a new one.
995 (defun record-optimization-failure (node transform args)
996 (declare (type combination node) (type transform transform)
997 (type (or fun-type list) args))
998 (let* ((table (component-failed-optimizations *component-being-compiled*))
999 (found (assoc transform (gethash node table))))
1001 (setf (cdr found) args)
1002 (push (cons transform args) (gethash node table))))
1005 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1006 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1007 ;;; doing the transform for some reason and FLAME is true, then we
1008 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1009 ;;; finalize to pick up. We return true if the transform failed, and
1010 ;;; thus further transformation should be attempted. We return false
1011 ;;; if either the transform succeeded or was aborted.
1012 (defun ir1-transform (node transform)
1013 (declare (type combination node) (type transform transform))
1014 (let* ((type (transform-type transform))
1015 (fun (transform-function transform))
1016 (constrained (fun-type-p type))
1017 (table (component-failed-optimizations *component-being-compiled*))
1018 (flame (if (transform-important transform)
1019 (policy node (>= speed inhibit-warnings))
1020 (policy node (> speed inhibit-warnings))))
1021 (*compiler-error-context* node))
1022 (cond ((or (not constrained)
1023 (valid-fun-use node type))
1024 (multiple-value-bind (severity args)
1025 (catch 'give-up-ir1-transform
1026 (transform-call node
1028 (combination-fun-source-name node))
1032 (remhash node table)
1035 (setf (combination-kind node) :error)
1037 (apply #'compiler-warn args))
1038 (remhash node table)
1043 (record-optimization-failure node transform args))
1044 (setf (gethash node table)
1045 (remove transform (gethash node table) :key #'car)))
1048 (remhash node table)
1053 :argument-test #'types-equal-or-intersect
1054 :result-test #'values-types-equal-or-intersect))
1055 (record-optimization-failure node transform type)
1060 ;;; When we don't like an IR1 transform, we throw the severity/reason
1063 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1064 ;;; aborting this attempt to transform the call, but admitting the
1065 ;;; possibility that this or some other transform will later succeed.
1066 ;;; If arguments are supplied, they are format arguments for an
1067 ;;; efficiency note.
1069 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1070 ;;; force a normal call to the function at run time. No further
1071 ;;; optimizations will be attempted.
1073 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1074 ;;; delay the transform on the node until later. REASONS specifies
1075 ;;; when the transform will be later retried. The :OPTIMIZE reason
1076 ;;; causes the transform to be delayed until after the current IR1
1077 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1078 ;;; be delayed until after constraint propagation.
1080 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1081 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1082 ;;; do CASE operations on the various REASON values, it might be a
1083 ;;; good idea to go OO, representing the reasons by objects, using
1084 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1085 ;;; SIGNAL instead of THROW.
1086 (declaim (ftype (function (&rest t) nil) give-up-ir1-transform))
1087 (defun give-up-ir1-transform (&rest args)
1088 (throw 'give-up-ir1-transform (values :failure args)))
1089 (defun abort-ir1-transform (&rest args)
1090 (throw 'give-up-ir1-transform (values :aborted args)))
1091 (defun delay-ir1-transform (node &rest reasons)
1092 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1094 (setf *delayed-ir1-transforms*
1095 (acons node reasons *delayed-ir1-transforms*))
1096 (throw 'give-up-ir1-transform :delayed))
1098 (dolist (reason reasons)
1099 (pushnew reason (cdr assoc)))
1100 (throw 'give-up-ir1-transform :delayed)))))
1102 ;;; Clear any delayed transform with no reasons - these should have
1103 ;;; been tried in the last pass. Then remove the reason from the
1104 ;;; delayed transform reasons, and if any become empty then set
1105 ;;; reoptimize flags for the node. Return true if any transforms are
1107 (defun retry-delayed-ir1-transforms (reason)
1108 (setf *delayed-ir1-transforms*
1109 (remove-if-not #'cdr *delayed-ir1-transforms*))
1110 (let ((reoptimize nil))
1111 (dolist (assoc *delayed-ir1-transforms*)
1112 (let ((reasons (remove reason (cdr assoc))))
1113 (setf (cdr assoc) reasons)
1115 (let ((node (car assoc)))
1116 (unless (node-deleted node)
1118 (setf (node-reoptimize node) t)
1119 (let ((block (node-block node)))
1120 (setf (block-reoptimize block) t)
1121 (setf (component-reoptimize (block-component block)) t)))))))
1124 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1125 ;;; environment, and then install it as the function for the call
1126 ;;; NODE. We do local call analysis so that the new function is
1127 ;;; integrated into the control flow.
1129 ;;; We require the original function source name in order to generate
1130 ;;; a meaningful debug name for the lambda we set up. (It'd be
1131 ;;; possible to do this starting from debug names as well as source
1132 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1133 ;;; generality, since source names are always known to our callers.)
1134 (defun transform-call (call res source-name)
1135 (declare (type combination call) (list res))
1136 (aver (and (legal-fun-name-p source-name)
1137 (not (eql source-name '.anonymous.))))
1138 (node-ends-block call)
1139 (with-ir1-environment-from-node call
1140 (with-component-last-block (*current-component*
1141 (block-next (node-block call)))
1142 (let ((new-fun (ir1-convert-inline-lambda
1144 :debug-name (debug-namify "LAMBDA-inlined ~A"
1147 "<unknown function>"))))
1148 (ref (continuation-use (combination-fun call))))
1149 (change-ref-leaf ref new-fun)
1150 (setf (combination-kind call) :full)
1151 (locall-analyze-component *current-component*))))
1154 ;;; Replace a call to a foldable function of constant arguments with
1155 ;;; the result of evaluating the form. If there is an error during the
1156 ;;; evaluation, we give a warning and leave the call alone, making the
1157 ;;; call a :ERROR call.
1159 ;;; If there is more than one value, then we transform the call into a
1162 ;;; An old commentary also said:
1164 ;;; We insert the resulting constant node after the call, stealing
1165 ;;; the call's continuation. We give the call a continuation with no
1166 ;;; DEST, which should cause it and its arguments to go away.
1168 ;;; This seems to be more efficient, than the current code. Maybe we
1169 ;;; should really implement it? -- APD, 2002-12-23
1170 (defun constant-fold-call (call)
1171 (let ((args (mapcar #'continuation-value (combination-args call)))
1172 (fun-name (combination-fun-source-name call)))
1173 (multiple-value-bind (values win)
1174 (careful-call fun-name
1177 ;; Note: CMU CL had COMPILER-WARN here, and that
1178 ;; seems more natural, but it's probably not.
1180 ;; It's especially not while bug 173 exists:
1183 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1185 ;; can cause constant-folding TYPE-ERRORs (in
1186 ;; #'<=) when END can be proved to be NIL, even
1187 ;; though the code is perfectly legal and safe
1188 ;; because a NIL value of END means that the
1189 ;; #'<= will never be executed.
1191 ;; Moreover, even without bug 173,
1192 ;; quite-possibly-valid code like
1193 ;; (COND ((NONINLINED-PREDICATE END)
1194 ;; (UNLESS (<= END SIZE))
1196 ;; (where NONINLINED-PREDICATE is something the
1197 ;; compiler can't do at compile time, but which
1198 ;; turns out to make the #'<= expression
1199 ;; unreachable when END=NIL) could cause errors
1200 ;; when the compiler tries to constant-fold (<=
1203 ;; So, with or without bug 173, it'd be
1204 ;; unnecessarily evil to do a full
1205 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1206 ;; from COMPILE-FILE) for legal code, so we we
1207 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1208 #'compiler-style-warn
1211 (setf (combination-kind call) :error))
1212 ((and (proper-list-of-length-p values 1)
1213 (eq (continuation-kind (node-cont call)) :inside-block))
1214 (with-ir1-environment-from-node call
1215 (let* ((cont (node-cont call))
1216 (next (continuation-next cont))
1217 (prev (make-continuation)))
1218 (delete-continuation-use call)
1219 (add-continuation-use call prev)
1220 (reference-constant prev cont (first values))
1221 (setf (continuation-next cont) next)
1222 ;; FIXME: type checking?
1223 (reoptimize-continuation cont)
1224 (reoptimize-continuation prev)
1225 (flush-combination call))))
1226 (t (let ((dummies (make-gensym-list (length args))))
1230 (declare (ignore ,@dummies))
1231 (values ,@(mapcar (lambda (x) `',x) values)))
1235 ;;;; local call optimization
1237 ;;; Propagate TYPE to LEAF and its REFS, marking things changed. If
1238 ;;; the leaf type is a function type, then just leave it alone, since
1239 ;;; TYPE is never going to be more specific than that (and
1240 ;;; TYPE-INTERSECTION would choke.)
1241 (defun propagate-to-refs (leaf type)
1242 (declare (type leaf leaf) (type ctype type))
1243 (let ((var-type (leaf-type leaf)))
1244 (unless (fun-type-p var-type)
1245 (let ((int (type-approx-intersection2 var-type type)))
1246 (when (type/= int var-type)
1247 (setf (leaf-type leaf) int)
1248 (dolist (ref (leaf-refs leaf))
1249 (derive-node-type ref (make-single-value-type int))
1250 (let* ((cont (node-cont ref))
1251 (dest (continuation-dest cont)))
1252 ;; KLUDGE: LET var substitution
1253 (when (combination-p dest)
1254 (reoptimize-continuation cont))))))
1257 ;;; Figure out the type of a LET variable that has sets. We compute
1258 ;;; the union of the initial value TYPE and the types of all the set
1259 ;;; values and to a PROPAGATE-TO-REFS with this type.
1260 (defun propagate-from-sets (var type)
1261 (collect ((res type type-union))
1262 (dolist (set (basic-var-sets var))
1263 (let ((type (continuation-type (set-value set))))
1265 (when (node-reoptimize set)
1266 (derive-node-type set (make-single-value-type type))
1267 (setf (node-reoptimize set) nil))))
1268 (propagate-to-refs var (res)))
1271 ;;; If a LET variable, find the initial value's type and do
1272 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1274 (defun ir1-optimize-set (node)
1275 (declare (type cset node))
1276 (let ((var (set-var node)))
1277 (when (and (lambda-var-p var) (leaf-refs var))
1278 (let ((home (lambda-var-home var)))
1279 (when (eq (functional-kind home) :let)
1280 (let ((iv (let-var-initial-value var)))
1281 (setf (continuation-reoptimize iv) nil)
1282 (propagate-from-sets var (continuation-type iv)))))))
1284 (derive-node-type node (make-single-value-type
1285 (continuation-type (set-value node))))
1288 ;;; Return true if the value of REF will always be the same (and is
1289 ;;; thus legal to substitute.)
1290 (defun constant-reference-p (ref)
1291 (declare (type ref ref))
1292 (let ((leaf (ref-leaf ref)))
1294 ((or constant functional) t)
1296 (null (lambda-var-sets leaf)))
1298 (not (eq (defined-fun-inlinep leaf) :notinline)))
1300 (case (global-var-kind leaf)
1302 (let ((name (leaf-source-name leaf)))
1304 (eq (symbol-package (fun-name-block-name name))
1306 (info :function :info name)))))))))
1308 ;;; If we have a non-set LET var with a single use, then (if possible)
1309 ;;; replace the variable reference's CONT with the arg continuation.
1310 ;;; This is inhibited when:
1311 ;;; -- CONT has other uses, or
1312 ;;; -- the reference is in a different environment from the variable, or
1313 ;;; -- CONT carries unknown number of values, or
1314 ;;; -- DEST is return or exit, or
1315 ;;; -- DEST is sensitive to the number of values and ARG return non-one value.
1317 ;;; We change the REF to be a reference to NIL with unused value, and
1318 ;;; let it be flushed as dead code. A side effect of this substitution
1319 ;;; is to delete the variable.
1320 (defun substitute-single-use-continuation (arg var)
1321 (declare (type continuation arg) (type lambda-var var))
1322 (let* ((ref (first (leaf-refs var)))
1323 (cont (node-cont ref))
1324 (dest (continuation-dest cont)))
1325 (when (and (eq (continuation-use cont) ref)
1329 (and (type-single-value-p (continuation-derived-type arg))
1330 (multiple-value-bind (pdest pprev)
1331 (principal-continuation-end cont)
1332 (declare (ignore pdest))
1333 (continuation-single-value-p pprev))))
1335 (or (eq (basic-combination-fun dest) cont)
1336 (and (eq (basic-combination-kind dest) :local)
1337 (type-single-value-p (continuation-derived-type arg)))))
1341 ;; (AVER (CONTINUATION-SINGLE-VALUE-P CONT))
1343 (eq (node-home-lambda ref)
1344 (lambda-home (lambda-var-home var))))
1345 (aver (member (continuation-kind arg)
1346 '(:block-start :deleted-block-start :inside-block)))
1347 (setf (node-derived-type ref) *wild-type*)
1348 (change-ref-leaf ref (find-constant nil))
1349 (substitute-continuation arg cont)
1350 (reoptimize-continuation arg)
1353 ;;; Delete a LET, removing the call and bind nodes, and warning about
1354 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1355 ;;; along right away and delete the REF and then the lambda, since we
1356 ;;; flush the FUN continuation.
1357 (defun delete-let (clambda)
1358 (declare (type clambda clambda))
1359 (aver (functional-letlike-p clambda))
1360 (note-unreferenced-vars clambda)
1361 (let ((call (let-combination clambda)))
1362 (flush-dest (basic-combination-fun call))
1364 (unlink-node (lambda-bind clambda))
1365 (setf (lambda-bind clambda) nil))
1368 ;;; This function is called when one of the arguments to a LET
1369 ;;; changes. We look at each changed argument. If the corresponding
1370 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1371 ;;; consider substituting for the variable, and also propagate
1372 ;;; derived-type information for the arg to all the VAR's refs.
1374 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1375 ;;; subtype of the argument's leaf type. This prevents type checking
1376 ;;; from being defeated, and also ensures that the best representation
1377 ;;; for the variable can be used.
1379 ;;; Substitution of individual references is inhibited if the
1380 ;;; reference is in a different component from the home. This can only
1381 ;;; happen with closures over top level lambda vars. In such cases,
1382 ;;; the references may have already been compiled, and thus can't be
1383 ;;; retroactively modified.
1385 ;;; If all of the variables are deleted (have no references) when we
1386 ;;; are done, then we delete the LET.
1388 ;;; Note that we are responsible for clearing the
1389 ;;; CONTINUATION-REOPTIMIZE flags.
1390 (defun propagate-let-args (call fun)
1391 (declare (type combination call) (type clambda fun))
1392 (loop for arg in (combination-args call)
1393 and var in (lambda-vars fun) do
1394 (when (and arg (continuation-reoptimize arg))
1395 (setf (continuation-reoptimize arg) nil)
1397 ((lambda-var-sets var)
1398 (propagate-from-sets var (continuation-type arg)))
1399 ((let ((use (continuation-use arg)))
1401 (let ((leaf (ref-leaf use)))
1402 (when (and (constant-reference-p use)
1403 (csubtypep (leaf-type leaf)
1404 ;; (NODE-DERIVED-TYPE USE) would
1405 ;; be better -- APD, 2003-05-15
1407 (propagate-to-refs var (continuation-type arg))
1408 (let ((use-component (node-component use)))
1411 (cond ((eq (node-component ref) use-component)
1414 (aver (lambda-toplevelish-p (lambda-home fun)))
1418 ((and (null (rest (leaf-refs var)))
1419 (substitute-single-use-continuation arg var)))
1421 (propagate-to-refs var (continuation-type arg))))))
1423 (when (every #'not (combination-args call))
1428 ;;; This function is called when one of the args to a non-LET local
1429 ;;; call changes. For each changed argument corresponding to an unset
1430 ;;; variable, we compute the union of the types across all calls and
1431 ;;; propagate this type information to the var's refs.
1433 ;;; If the function has an XEP, then we don't do anything, since we
1434 ;;; won't discover anything.
1436 ;;; We can clear the CONTINUATION-REOPTIMIZE flags for arguments in
1437 ;;; all calls corresponding to changed arguments in CALL, since the
1438 ;;; only use in IR1 optimization of the REOPTIMIZE flag for local call
1439 ;;; args is right here.
1440 (defun propagate-local-call-args (call fun)
1441 (declare (type combination call) (type clambda fun))
1443 (unless (or (functional-entry-fun fun)
1444 (lambda-optional-dispatch fun))
1445 (let* ((vars (lambda-vars fun))
1446 (union (mapcar (lambda (arg var)
1448 (continuation-reoptimize arg)
1449 (null (basic-var-sets var)))
1450 (continuation-type arg)))
1451 (basic-combination-args call)
1453 (this-ref (continuation-use (basic-combination-fun call))))
1455 (dolist (arg (basic-combination-args call))
1457 (setf (continuation-reoptimize arg) nil)))
1459 (dolist (ref (leaf-refs fun))
1460 (let ((dest (continuation-dest (node-cont ref))))
1461 (unless (or (eq ref this-ref) (not dest))
1463 (mapcar (lambda (this-arg old)
1465 (setf (continuation-reoptimize this-arg) nil)
1466 (type-union (continuation-type this-arg) old)))
1467 (basic-combination-args dest)
1470 (mapc (lambda (var type)
1472 (propagate-to-refs var type)))
1477 ;;;; multiple values optimization
1479 ;;; Do stuff to notice a change to a MV combination node. There are
1480 ;;; two main branches here:
1481 ;;; -- If the call is local, then it is already a MV let, or should
1482 ;;; become one. Note that although all :LOCAL MV calls must eventually
1483 ;;; be converted to :MV-LETs, there can be a window when the call
1484 ;;; is local, but has not been LET converted yet. This is because
1485 ;;; the entry-point lambdas may have stray references (in other
1486 ;;; entry points) that have not been deleted yet.
1487 ;;; -- The call is full. This case is somewhat similar to the non-MV
1488 ;;; combination optimization: we propagate return type information and
1489 ;;; notice non-returning calls. We also have an optimization
1490 ;;; which tries to convert MV-CALLs into MV-binds.
1491 (defun ir1-optimize-mv-combination (node)
1492 (ecase (basic-combination-kind node)
1494 (let ((fun-cont (basic-combination-fun node)))
1495 (when (continuation-reoptimize fun-cont)
1496 (setf (continuation-reoptimize fun-cont) nil)
1497 (maybe-let-convert (combination-lambda node))))
1498 (setf (continuation-reoptimize (first (basic-combination-args node))) nil)
1499 (when (eq (functional-kind (combination-lambda node)) :mv-let)
1500 (unless (convert-mv-bind-to-let node)
1501 (ir1-optimize-mv-bind node))))
1503 (let* ((fun (basic-combination-fun node))
1504 (fun-changed (continuation-reoptimize fun))
1505 (args (basic-combination-args node)))
1507 (setf (continuation-reoptimize fun) nil)
1508 (let ((type (continuation-type fun)))
1509 (when (fun-type-p type)
1510 (derive-node-type node (fun-type-returns type))))
1511 (maybe-terminate-block node nil)
1512 (let ((use (continuation-use fun)))
1513 (when (and (ref-p use) (functional-p (ref-leaf use)))
1514 (convert-call-if-possible use node)
1515 (when (eq (basic-combination-kind node) :local)
1516 (maybe-let-convert (ref-leaf use))))))
1517 (unless (or (eq (basic-combination-kind node) :local)
1518 (eq (continuation-fun-name fun) '%throw))
1519 (ir1-optimize-mv-call node))
1521 (setf (continuation-reoptimize arg) nil))))
1525 ;;; Propagate derived type info from the values continuation to the
1527 (defun ir1-optimize-mv-bind (node)
1528 (declare (type mv-combination node))
1529 (let ((arg (first (basic-combination-args node)))
1530 (vars (lambda-vars (combination-lambda node))))
1531 (multiple-value-bind (types nvals)
1532 (values-types (continuation-derived-type arg))
1533 (unless (eq nvals :unknown)
1534 (mapc (lambda (var type)
1535 (if (basic-var-sets var)
1536 (propagate-from-sets var type)
1537 (propagate-to-refs var type)))
1541 (specifier-type 'null)))))
1542 (setf (continuation-reoptimize arg) nil))
1545 ;;; If possible, convert a general MV call to an MV-BIND. We can do
1547 ;;; -- The call has only one argument, and
1548 ;;; -- The function has a known fixed number of arguments, or
1549 ;;; -- The argument yields a known fixed number of values.
1551 ;;; What we do is change the function in the MV-CALL to be a lambda
1552 ;;; that "looks like an MV bind", which allows
1553 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
1554 ;;; converted (the next time around.) This new lambda just calls the
1555 ;;; actual function with the MV-BIND variables as arguments. Note that
1556 ;;; this new MV bind is not let-converted immediately, as there are
1557 ;;; going to be stray references from the entry-point functions until
1558 ;;; they get deleted.
1560 ;;; In order to avoid loss of argument count checking, we only do the
1561 ;;; transformation according to a known number of expected argument if
1562 ;;; safety is unimportant. We can always convert if we know the number
1563 ;;; of actual values, since the normal call that we build will still
1564 ;;; do any appropriate argument count checking.
1566 ;;; We only attempt the transformation if the called function is a
1567 ;;; constant reference. This allows us to just splice the leaf into
1568 ;;; the new function, instead of trying to somehow bind the function
1569 ;;; expression. The leaf must be constant because we are evaluating it
1570 ;;; again in a different place. This also has the effect of squelching
1571 ;;; multiple warnings when there is an argument count error.
1572 (defun ir1-optimize-mv-call (node)
1573 (let ((fun (basic-combination-fun node))
1574 (*compiler-error-context* node)
1575 (ref (continuation-use (basic-combination-fun node)))
1576 (args (basic-combination-args node)))
1578 (unless (and (ref-p ref) (constant-reference-p ref)
1580 (return-from ir1-optimize-mv-call))
1582 (multiple-value-bind (min max)
1583 (fun-type-nargs (continuation-type fun))
1585 (multiple-value-bind (types nvals)
1586 (values-types (continuation-derived-type (first args)))
1587 (declare (ignore types))
1588 (if (eq nvals :unknown) nil nvals))))
1591 (when (and min (< total-nvals min))
1593 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1596 (setf (basic-combination-kind node) :error)
1597 (return-from ir1-optimize-mv-call))
1598 (when (and max (> total-nvals max))
1600 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1603 (setf (basic-combination-kind node) :error)
1604 (return-from ir1-optimize-mv-call)))
1606 (let ((count (cond (total-nvals)
1607 ((and (policy node (zerop verify-arg-count))
1612 (with-ir1-environment-from-node node
1613 (let* ((dums (make-gensym-list count))
1615 (fun (ir1-convert-lambda
1616 `(lambda (&optional ,@dums &rest ,ignore)
1617 (declare (ignore ,ignore))
1618 (funcall ,(ref-leaf ref) ,@dums)))))
1619 (change-ref-leaf ref fun)
1620 (aver (eq (basic-combination-kind node) :full))
1621 (locall-analyze-component *current-component*)
1622 (aver (eq (basic-combination-kind node) :local)))))))))
1626 ;;; (multiple-value-bind
1635 ;;; What we actually do is convert the VALUES combination into a
1636 ;;; normal LET combination calling the original :MV-LET lambda. If
1637 ;;; there are extra args to VALUES, discard the corresponding
1638 ;;; continuations. If there are insufficient args, insert references
1640 (defun convert-mv-bind-to-let (call)
1641 (declare (type mv-combination call))
1642 (let* ((arg (first (basic-combination-args call)))
1643 (use (continuation-use arg)))
1644 (when (and (combination-p use)
1645 (eq (continuation-fun-name (combination-fun use))
1647 (let* ((fun (combination-lambda call))
1648 (vars (lambda-vars fun))
1649 (vals (combination-args use))
1650 (nvars (length vars))
1651 (nvals (length vals)))
1652 (cond ((> nvals nvars)
1653 (mapc #'flush-dest (subseq vals nvars))
1654 (setq vals (subseq vals 0 nvars)))
1656 (with-ir1-environment-from-node use
1657 (let ((node-prev (node-prev use)))
1658 (setf (node-prev use) nil)
1659 (setf (continuation-next node-prev) nil)
1660 (collect ((res vals))
1661 (loop for cont = (make-continuation use)
1662 and prev = node-prev then cont
1663 repeat (- nvars nvals)
1664 do (reference-constant prev cont nil)
1667 (link-node-to-previous-continuation use
1668 (car (last vals)))))))
1669 (setf (combination-args use) vals)
1670 (flush-dest (combination-fun use))
1671 (let ((fun-cont (basic-combination-fun call)))
1672 (setf (continuation-dest fun-cont) use)
1673 (setf (combination-fun use) fun-cont)
1674 (flush-continuation-externally-checkable-type fun-cont))
1675 (setf (combination-kind use) :local)
1676 (setf (functional-kind fun) :let)
1677 (flush-dest (first (basic-combination-args call)))
1680 (reoptimize-continuation (first vals)))
1681 (propagate-to-args use fun)
1682 (reoptimize-call use))
1686 ;;; (values-list (list x y z))
1691 ;;; In implementation, this is somewhat similar to
1692 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
1693 ;;; args of the VALUES-LIST call, flushing the old argument
1694 ;;; continuation (allowing the LIST to be flushed.)
1696 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
1697 (defoptimizer (values-list optimizer) ((list) node)
1698 (let ((use (continuation-use list)))
1699 (when (and (combination-p use)
1700 (eq (continuation-fun-name (combination-fun use))
1703 ;; FIXME: VALUES might not satisfy an assertion on NODE-CONT.
1704 (change-ref-leaf (continuation-use (combination-fun node))
1705 (find-free-fun 'values "in a strange place"))
1706 (setf (combination-kind node) :full)
1707 (let ((args (combination-args use)))
1709 (setf (continuation-dest arg) node)
1710 (flush-continuation-externally-checkable-type arg))
1711 (setf (combination-args use) nil)
1713 (setf (combination-args node) args))
1716 ;;; If VALUES appears in a non-MV context, then effectively convert it
1717 ;;; to a PROG1. This allows the computation of the additional values
1718 ;;; to become dead code.
1719 (deftransform values ((&rest vals) * * :node node)
1720 (unless (continuation-single-value-p (node-cont node))
1721 (give-up-ir1-transform))
1722 (setf (node-derived-type node) *wild-type*)
1724 (let ((dummies (make-gensym-list (length (cdr vals)))))
1725 `(lambda (val ,@dummies)
1726 (declare (ignore ,@dummies))
1732 (defun ir1-optimize-cast (cast &optional do-not-optimize)
1733 (declare (type cast cast))
1734 (let* ((value (cast-value cast))
1735 (value-type (continuation-derived-type value))
1736 (atype (cast-asserted-type cast))
1737 (int (values-type-intersection value-type atype)))
1738 (derive-node-type cast int)
1739 (when (eq int *empty-type*)
1740 (unless (eq value-type *empty-type*)
1742 ;; FIXME: Do it in one step.
1743 (filter-continuation
1745 `(multiple-value-call #'list 'dummy))
1746 (filter-continuation
1748 ;; FIXME: Derived type.
1749 `(%compile-time-type-error 'dummy
1750 ',(type-specifier (coerce-to-values atype))
1751 ',(type-specifier value-type)))
1752 ;; KLUDGE: FILTER-CONTINUATION does not work for
1753 ;; non-returning functions, so we declare the return type of
1754 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
1756 (derive-node-type (continuation-use value) *empty-type*)
1757 (maybe-terminate-block (continuation-use value) nil)
1758 ;; FIXME: Is it necessary?
1759 (aver (null (block-pred (node-block cast))))
1760 (setf (block-delete-p (node-block cast)) t)
1761 (return-from ir1-optimize-cast)))
1762 (when (eq (node-derived-type cast) *empty-type*)
1763 (maybe-terminate-block cast nil))
1765 (flet ((delete-cast ()
1766 (let ((cont (node-cont cast)))
1767 (delete-filter cast cont value)
1768 (reoptimize-continuation cont)
1769 (when (continuation-single-value-p cont)
1770 (note-single-valuified-continuation cont))
1771 (when (not (continuation-dest cont))
1772 (reoptimize-continuation-uses cont)))))
1774 ((and (not do-not-optimize)
1775 (values-subtypep value-type
1776 (cast-asserted-type cast)))
1778 (return-from ir1-optimize-cast t))
1779 ((and (cast-%type-check cast)
1780 (values-subtypep value-type
1781 (cast-type-to-check cast)))
1782 (setf (cast-%type-check cast) nil)))))
1784 (unless do-not-optimize
1785 (setf (node-reoptimize cast) nil)))