1 ;;;; This file implements the IR1 optimization phase of the compiler.
2 ;;;; IR1 optimization is a grab-bag of optimizations that don't make
3 ;;;; major changes to the block-level control flow and don't use flow
4 ;;;; analysis. These optimizations can mostly be classified as
5 ;;;; "meta-evaluation", but there is a sizable top-down component as
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
19 ;;;; interface for obtaining results of constant folding
21 ;;; Return true for an LVAR whose sole use is a reference to a
23 (defun constant-lvar-p (thing)
24 (declare (type (or lvar null) thing))
26 (let ((use (principal-lvar-use thing)))
27 (and (ref-p use) (constant-p (ref-leaf use))))))
29 ;;; Return the constant value for an LVAR whose only use is a constant
31 (declaim (ftype (function (lvar) t) lvar-value))
32 (defun lvar-value (lvar)
33 (let ((use (principal-lvar-use lvar)))
34 (constant-value (ref-leaf use))))
36 ;;;; interface for obtaining results of type inference
38 ;;; Our best guess for the type of this lvar's value. Note that this
39 ;;; may be VALUES or FUNCTION type, which cannot be passed as an
40 ;;; argument to the normal type operations. See LVAR-TYPE.
42 ;;; The result value is cached in the LVAR-%DERIVED-TYPE slot. If the
43 ;;; slot is true, just return that value, otherwise recompute and
44 ;;; stash the value there.
45 (eval-when (:compile-toplevel :execute)
46 (#+sb-xc-host cl:defmacro
47 #-sb-xc-host sb!xc:defmacro
48 lvar-type-using (lvar accessor)
49 `(let ((uses (lvar-uses ,lvar)))
50 (cond ((null uses) *empty-type*)
52 (do ((res (,accessor (first uses))
53 (values-type-union (,accessor (first current))
55 (current (rest uses) (rest current)))
56 ((or (null current) (eq res *wild-type*))
61 #!-sb-fluid (declaim (inline lvar-derived-type))
62 (defun lvar-derived-type (lvar)
63 (declare (type lvar lvar))
64 (or (lvar-%derived-type lvar)
65 (setf (lvar-%derived-type lvar)
66 (%lvar-derived-type lvar))))
67 (defun %lvar-derived-type (lvar)
68 (lvar-type-using lvar node-derived-type))
70 ;;; Return the derived type for LVAR's first value. This is guaranteed
71 ;;; not to be a VALUES or FUNCTION type.
72 (declaim (ftype (sfunction (lvar) ctype) lvar-type))
73 (defun lvar-type (lvar)
74 (single-value-type (lvar-derived-type lvar)))
76 ;;; LVAR-CONSERVATIVE-TYPE
78 ;;; Certain types refer to the contents of an object, which can
79 ;;; change without type derivation noticing: CONS types and ARRAY
80 ;;; types suffer from this:
82 ;;; (let ((x (the (cons fixnum fixnum) (cons a b))))
84 ;;; (+ (car x) (cdr x)))
86 ;;; Python doesn't realize that the SETF CAR can change the type of X -- so we
87 ;;; cannot use LVAR-TYPE which gets the derived results. Worse, still, instead
88 ;;; of (SETF CAR) we might have a call to a user-defined function FOO which
89 ;;; does the same -- so there is no way to use the derived information in
92 ;;; So, the conservative option is to use the derived type if the leaf has
93 ;;; only a single ref -- in which case there cannot be a prior call that
94 ;;; mutates it. Otherwise we use the declared type or punt to the most general
95 ;;; type we know to be correct for sure.
96 (defun lvar-conservative-type (lvar)
97 (let ((derived-type (lvar-type lvar))
98 (t-type *universal-type*))
99 ;; Recompute using NODE-CONSERVATIVE-TYPE instead of derived type if
100 ;; necessary -- picking off some easy cases up front.
101 (cond ((or (eq derived-type t-type)
102 ;; Can't use CSUBTYPEP!
103 (type= derived-type (specifier-type 'list))
104 (type= derived-type (specifier-type 'null)))
106 ((and (cons-type-p derived-type)
107 (eq t-type (cons-type-car-type derived-type))
108 (eq t-type (cons-type-cdr-type derived-type)))
110 ((and (array-type-p derived-type)
111 (or (not (array-type-complexp derived-type))
112 (let ((dimensions (array-type-dimensions derived-type)))
113 (or (eq '* dimensions)
114 (every (lambda (dim) (eq '* dim)) dimensions)))))
116 ((type-needs-conservation-p derived-type)
117 (single-value-type (lvar-type-using lvar node-conservative-type)))
121 (defun node-conservative-type (node)
122 (let* ((derived-values-type (node-derived-type node))
123 (derived-type (single-value-type derived-values-type)))
125 (let ((leaf (ref-leaf node)))
126 (if (and (basic-var-p leaf)
127 (cdr (leaf-refs leaf)))
129 (if (eq :declared (leaf-where-from leaf))
131 (conservative-type derived-type)))
132 derived-values-type))
133 derived-values-type)))
135 (defun conservative-type (type)
136 (cond ((or (eq type *universal-type*)
137 (eq type (specifier-type 'list))
138 (eq type (specifier-type 'null)))
141 (specifier-type 'cons))
143 (if (array-type-complexp type)
145 ;; ADJUST-ARRAY may change dimensions, but rank stays same.
147 (let ((old (array-type-dimensions type)))
150 (mapcar (constantly '*) old)))
151 ;; Complexity cannot change.
152 :complexp (array-type-complexp type)
153 ;; Element type cannot change.
154 :element-type (array-type-element-type type)
155 :specialized-element-type (array-type-specialized-element-type type))
156 ;; Simple arrays cannot change at all.
159 ;; If the type contains some CONS types, the conservative type contains all
161 (when (types-equal-or-intersect type (specifier-type 'cons))
162 (setf type (type-union type (specifier-type 'cons))))
163 ;; Similarly for non-simple arrays -- it should be possible to preserve
164 ;; more information here, but really...
165 (let ((non-simple-arrays (specifier-type '(and array (not simple-array)))))
166 (when (types-equal-or-intersect type non-simple-arrays)
167 (setf type (type-union type non-simple-arrays))))
170 (defun type-needs-conservation-p (type)
171 (cond ((eq type *universal-type*)
172 ;; Excluding T is necessary, because we do want type derivation to
173 ;; be able to narrow it down in case someone (most like a macro-expansion...)
174 ;; actually declares something as having type T.
176 ((or (cons-type-p type) (and (array-type-p type) (array-type-complexp type)))
177 ;; Covered by the next case as well, but this is a quick test.
179 ((types-equal-or-intersect type (specifier-type '(or cons (and array (not simple-array)))))
182 ;;; If LVAR is an argument of a function, return a type which the
183 ;;; function checks LVAR for.
184 #!-sb-fluid (declaim (inline lvar-externally-checkable-type))
185 (defun lvar-externally-checkable-type (lvar)
186 (or (lvar-%externally-checkable-type lvar)
187 (%lvar-%externally-checkable-type lvar)))
188 (defun %lvar-%externally-checkable-type (lvar)
189 (declare (type lvar lvar))
190 (let ((dest (lvar-dest lvar)))
191 (if (not (and dest (combination-p dest)))
192 ;; TODO: MV-COMBINATION
193 (setf (lvar-%externally-checkable-type lvar) *wild-type*)
194 (let* ((fun (combination-fun dest))
195 (args (combination-args dest))
196 (fun-type (lvar-type fun)))
197 (setf (lvar-%externally-checkable-type fun) *wild-type*)
198 (if (or (not (call-full-like-p dest))
199 (not (fun-type-p fun-type))
200 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
201 (fun-type-wild-args fun-type))
204 (setf (lvar-%externally-checkable-type arg)
206 (map-combination-args-and-types
208 (setf (lvar-%externally-checkable-type arg)
209 (acond ((lvar-%externally-checkable-type arg)
210 (values-type-intersection
211 it (coerce-to-values type)))
212 (t (coerce-to-values type)))))
214 (lvar-%externally-checkable-type lvar))
215 #!-sb-fluid(declaim (inline flush-lvar-externally-checkable-type))
216 (defun flush-lvar-externally-checkable-type (lvar)
217 (declare (type lvar lvar))
218 (setf (lvar-%externally-checkable-type lvar) nil))
220 ;;;; interface routines used by optimizers
222 (declaim (inline reoptimize-component))
223 (defun reoptimize-component (component kind)
224 (declare (type component component)
225 (type (member nil :maybe t) kind))
227 (unless (eq (component-reoptimize component) t)
228 (setf (component-reoptimize component) kind)))
230 ;;; This function is called by optimizers to indicate that something
231 ;;; interesting has happened to the value of LVAR. Optimizers must
232 ;;; make sure that they don't call for reoptimization when nothing has
233 ;;; happened, since optimization will fail to terminate.
235 ;;; We clear any cached type for the lvar and set the reoptimize flags
236 ;;; on everything in sight.
237 (defun reoptimize-lvar (lvar)
238 (declare (type (or lvar null) lvar))
240 (setf (lvar-%derived-type lvar) nil)
241 (let ((dest (lvar-dest lvar)))
243 (setf (lvar-reoptimize lvar) t)
244 (setf (node-reoptimize dest) t)
245 (binding* (;; Since this may be called during IR1 conversion,
246 ;; PREV may be missing.
247 (prev (node-prev dest) :exit-if-null)
248 (block (ctran-block prev))
249 (component (block-component block)))
250 (when (typep dest 'cif)
251 (setf (block-test-modified block) t))
252 (setf (block-reoptimize block) t)
253 (reoptimize-component component :maybe))))
255 (setf (block-type-check (node-block node)) t)))
258 (defun reoptimize-lvar-uses (lvar)
259 (declare (type lvar lvar))
261 (setf (node-reoptimize use) t)
262 (setf (block-reoptimize (node-block use)) t)
263 (reoptimize-component (node-component use) :maybe)))
265 ;;; Annotate NODE to indicate that its result has been proven to be
266 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
267 ;;; only correct way to supply information discovered about a node's
268 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
269 ;;; information may be lost and reoptimization may not happen.
271 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
272 ;;; intersection is different from the old type, then we do a
273 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
274 (defun derive-node-type (node rtype)
275 (declare (type valued-node node) (type ctype rtype))
276 (let ((node-type (node-derived-type node)))
277 (unless (eq node-type rtype)
278 (let ((int (values-type-intersection node-type rtype))
279 (lvar (node-lvar node)))
280 (when (type/= node-type int)
281 (when (and *check-consistency*
282 (eq int *empty-type*)
283 (not (eq rtype *empty-type*)))
284 (let ((*compiler-error-context* node))
286 "New inferred type ~S conflicts with old type:~
287 ~% ~S~%*** possible internal error? Please report this."
288 (type-specifier rtype) (type-specifier node-type))))
289 (setf (node-derived-type node) int)
290 ;; If the new type consists of only one object, replace the
291 ;; node with a constant reference.
292 (when (and (ref-p node)
293 (lambda-var-p (ref-leaf node)))
294 (let ((type (single-value-type int)))
295 (when (and (member-type-p type)
296 (eql 1 (member-type-size type)))
297 (change-ref-leaf node (find-constant
298 (first (member-type-members type)))))))
299 (reoptimize-lvar lvar)))))
302 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
303 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
304 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
305 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
306 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
307 (defun assert-lvar-type (lvar type policy)
308 (declare (type lvar lvar) (type ctype type))
309 (unless (values-subtypep (lvar-derived-type lvar) type)
310 (let ((internal-lvar (make-lvar))
311 (dest (lvar-dest lvar)))
312 (substitute-lvar internal-lvar lvar)
313 (let ((cast (insert-cast-before dest lvar type policy)))
314 (use-lvar cast internal-lvar))))
320 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
321 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
322 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
323 ;;; we are done, then another iteration would be beneficial.
324 (defun ir1-optimize (component fastp)
325 (declare (type component component))
326 (setf (component-reoptimize component) nil)
327 (loop with block = (block-next (component-head component))
328 with tail = (component-tail component)
329 for last-block = block
330 until (eq block tail)
332 ;; We delete blocks when there is either no predecessor or the
333 ;; block is in a lambda that has been deleted. These blocks
334 ;; would eventually be deleted by DFO recomputation, but doing
335 ;; it here immediately makes the effect available to IR1
337 ((or (block-delete-p block)
338 (null (block-pred block)))
339 (delete-block-lazily block)
340 (setq block (clean-component component block)))
341 ((eq (functional-kind (block-home-lambda block)) :deleted)
342 ;; Preserve the BLOCK-SUCC invariant that almost every block has
343 ;; one successor (and a block with DELETE-P set is an acceptable
345 (mark-for-deletion block)
346 (setq block (clean-component component block)))
349 (let ((succ (block-succ block)))
350 (unless (singleton-p succ)
353 (let ((last (block-last block)))
356 (flush-dest (if-test last))
357 (when (unlink-node last)
360 (when (maybe-delete-exit last)
363 (unless (join-successor-if-possible block)
366 (when (and (not fastp) (block-reoptimize block) (block-component block))
367 (aver (not (block-delete-p block)))
368 (ir1-optimize-block block))
370 (cond ((and (block-delete-p block) (block-component block))
371 (setq block (clean-component component block)))
372 ((and (block-flush-p block) (block-component block))
373 (flush-dead-code block)))))
374 do (when (eq block last-block)
375 (setq block (block-next block))))
379 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
382 ;;; Note that although they are cleared here, REOPTIMIZE flags might
383 ;;; still be set upon return from this function, meaning that further
384 ;;; optimization is wanted (as a consequence of optimizations we did).
385 (defun ir1-optimize-block (block)
386 (declare (type cblock block))
387 ;; We clear the node and block REOPTIMIZE flags before doing the
388 ;; optimization, not after. This ensures that the node or block will
389 ;; be reoptimized if necessary.
390 (setf (block-reoptimize block) nil)
391 (do-nodes (node nil block :restart-p t)
392 (when (node-reoptimize node)
393 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
394 (setf (node-reoptimize node) nil)
398 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
399 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
400 ;; any argument changes.
401 (ir1-optimize-combination node))
403 (ir1-optimize-if node))
405 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
406 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
407 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
409 (setf (node-reoptimize node) t)
410 (ir1-optimize-return node))
412 (ir1-optimize-mv-combination node))
414 ;; With an EXIT, we derive the node's type from the VALUE's
416 (let ((value (exit-value node)))
418 (derive-node-type node (lvar-derived-type value)))))
420 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
421 ;; is accurate till the node actually has been reoptimized.
422 (setf (node-reoptimize node) t)
423 (ir1-optimize-set node))
425 (ir1-optimize-cast node)))))
429 ;;; Try to join with a successor block. If we succeed, we return true,
431 (defun join-successor-if-possible (block)
432 (declare (type cblock block))
433 (let ((next (first (block-succ block))))
434 (when (block-start next) ; NEXT is not an END-OF-COMPONENT marker
435 (cond ( ;; We cannot combine with a successor block if:
437 ;; the successor has more than one predecessor;
438 (rest (block-pred next))
439 ;; the successor is the current block (infinite loop);
441 ;; the next block has a different cleanup, and thus
442 ;; we may want to insert cleanup code between the
443 ;; two blocks at some point;
444 (not (eq (block-end-cleanup block)
445 (block-start-cleanup next)))
446 ;; the next block has a different home lambda, and
447 ;; thus the control transfer is a non-local exit.
448 (not (eq (block-home-lambda block)
449 (block-home-lambda next)))
450 ;; Stack analysis phase wants ENTRY to start a block...
451 (entry-p (block-start-node next))
452 (let ((last (block-last block)))
453 (and (valued-node-p last)
454 (awhen (node-lvar last)
456 ;; ... and a DX-allocator to end a block.
457 (lvar-dynamic-extent it)
458 ;; FIXME: This is a partial workaround for bug 303.
459 (consp (lvar-uses it)))))))
462 (join-blocks block next)
465 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
466 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
467 ;;; for the two blocks so that any indicated optimization gets done.
468 (defun join-blocks (block1 block2)
469 (declare (type cblock block1 block2))
470 (let* ((last1 (block-last block1))
471 (last2 (block-last block2))
472 (succ (block-succ block2))
473 (start2 (block-start block2)))
474 (do ((ctran start2 (node-next (ctran-next ctran))))
476 (setf (ctran-block ctran) block1))
478 (unlink-blocks block1 block2)
480 (unlink-blocks block2 block)
481 (link-blocks block1 block))
483 (setf (ctran-kind start2) :inside-block)
484 (setf (node-next last1) start2)
485 (setf (ctran-use start2) last1)
486 (setf (block-last block1) last2))
488 (setf (block-flags block1)
489 (attributes-union (block-flags block1)
491 (block-attributes type-asserted test-modified)))
493 (let ((next (block-next block2))
494 (prev (block-prev block2)))
495 (setf (block-next prev) next)
496 (setf (block-prev next) prev))
500 ;;; Delete any nodes in BLOCK whose value is unused and which have no
501 ;;; side effects. We can delete sets of lexical variables when the set
502 ;;; variable has no references.
503 (defun flush-dead-code (block)
504 (declare (type cblock block))
505 (setf (block-flush-p block) nil)
506 (do-nodes-backwards (node lvar block :restart-p t)
513 (when (flushable-combination-p node)
514 (flush-combination node)))
516 (when (eq (basic-combination-kind node) :local)
517 (let ((fun (combination-lambda node)))
518 (when (dolist (var (lambda-vars fun) t)
519 (when (or (leaf-refs var)
520 (lambda-var-sets var))
522 (flush-dest (first (basic-combination-args node)))
525 (let ((value (exit-value node)))
528 (setf (exit-value node) nil))))
530 (let ((var (set-var node)))
531 (when (and (lambda-var-p var)
532 (null (leaf-refs var)))
533 (flush-dest (set-value node))
534 (setf (basic-var-sets var)
535 (delq node (basic-var-sets var)))
536 (unlink-node node))))
538 (unless (cast-type-check node)
539 (flush-dest (cast-value node))
540 (unlink-node node))))))
544 ;;;; local call return type propagation
546 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
547 ;;; flag set. It iterates over the uses of the RESULT, looking for
548 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
549 ;;; call, then we union its type together with the types of other such
550 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
551 ;;; type with the RESULT's asserted type. We can make this
552 ;;; intersection now (potentially before type checking) because this
553 ;;; assertion on the result will eventually be checked (if
556 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
557 ;;; combination, which may change the successor of the call to be the
558 ;;; called function, and if so, checks if the call can become an
559 ;;; assignment. If we convert to an assignment, we abort, since the
560 ;;; RETURN has been deleted.
561 (defun find-result-type (node)
562 (declare (type creturn node))
563 (let ((result (return-result node)))
564 (collect ((use-union *empty-type* values-type-union))
565 (do-uses (use result)
566 (let ((use-home (node-home-lambda use)))
567 (cond ((or (eq (functional-kind use-home) :deleted)
568 (block-delete-p (node-block use))))
569 ((and (basic-combination-p use)
570 (eq (basic-combination-kind use) :local))
571 (aver (eq (lambda-tail-set use-home)
572 (lambda-tail-set (combination-lambda use))))
573 (when (combination-p use)
574 (when (nth-value 1 (maybe-convert-tail-local-call use))
575 (return-from find-result-type t))))
577 (use-union (node-derived-type use))))))
579 ;; (values-type-intersection
580 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
584 (setf (return-result-type node) int))))
587 ;;; Do stuff to realize that something has changed about the value
588 ;;; delivered to a return node. Since we consider the return values of
589 ;;; all functions in the tail set to be equivalent, this amounts to
590 ;;; bringing the entire tail set up to date. We iterate over the
591 ;;; returns for all the functions in the tail set, reanalyzing them
592 ;;; all (not treating NODE specially.)
594 ;;; When we are done, we check whether the new type is different from
595 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
596 ;;; all the lvars for references to functions in the tail set. This
597 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
598 ;;; results of the calls.
599 (defun ir1-optimize-return (node)
600 (declare (type creturn node))
603 (let* ((tails (lambda-tail-set (return-lambda node)))
604 (funs (tail-set-funs tails)))
605 (collect ((res *empty-type* values-type-union))
607 (let ((return (lambda-return fun)))
609 (when (node-reoptimize return)
610 (setf (node-reoptimize return) nil)
611 (when (find-result-type return)
613 (res (return-result-type return)))))
615 (when (type/= (res) (tail-set-type tails))
616 (setf (tail-set-type tails) (res))
617 (dolist (fun (tail-set-funs tails))
618 (dolist (ref (leaf-refs fun))
619 (reoptimize-lvar (node-lvar ref))))))))
625 ;;; If the test has multiple uses, replicate the node when possible.
626 ;;; Also check whether the predicate is known to be true or false,
627 ;;; deleting the IF node in favor of the appropriate branch when this
629 (defun ir1-optimize-if (node)
630 (declare (type cif node))
631 (let ((test (if-test node))
632 (block (node-block node)))
634 (when (and (eq (block-start-node block) node)
635 (listp (lvar-uses test)))
637 (when (immediately-used-p test use)
638 (convert-if-if use node)
639 (when (not (listp (lvar-uses test))) (return)))))
641 (let* ((type (lvar-type test))
643 (cond ((constant-lvar-p test)
644 (if (lvar-value test)
645 (if-alternative node)
646 (if-consequent node)))
647 ((not (types-equal-or-intersect type (specifier-type 'null)))
648 (if-alternative node))
649 ((type= type (specifier-type 'null))
650 (if-consequent node)))))
653 (when (rest (block-succ block))
654 (unlink-blocks block victim))
655 (setf (component-reanalyze (node-component node)) t)
656 (unlink-node node))))
659 ;;; Create a new copy of an IF node that tests the value of the node
660 ;;; USE. The test must have >1 use, and must be immediately used by
661 ;;; USE. NODE must be the only node in its block (implying that
662 ;;; block-start = if-test).
664 ;;; This optimization has an effect semantically similar to the
665 ;;; source-to-source transformation:
666 ;;; (IF (IF A B C) D E) ==>
667 ;;; (IF A (IF B D E) (IF C D E))
669 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
670 ;;; node so that dead code deletion notes will definitely not consider
671 ;;; either node to be part of the original source. One node might
672 ;;; become unreachable, resulting in a spurious note.
673 (defun convert-if-if (use node)
674 (declare (type node use) (type cif node))
675 (with-ir1-environment-from-node node
676 (let* ((block (node-block node))
677 (test (if-test node))
678 (cblock (if-consequent node))
679 (ablock (if-alternative node))
680 (use-block (node-block use))
681 (new-ctran (make-ctran))
682 (new-lvar (make-lvar))
683 (new-node (make-if :test new-lvar
685 :alternative ablock))
686 (new-block (ctran-starts-block new-ctran)))
687 (link-node-to-previous-ctran new-node new-ctran)
688 (setf (lvar-dest new-lvar) new-node)
689 (setf (block-last new-block) new-node)
691 (unlink-blocks use-block block)
692 (%delete-lvar-use use)
693 (add-lvar-use use new-lvar)
694 (link-blocks use-block new-block)
696 (link-blocks new-block cblock)
697 (link-blocks new-block ablock)
699 (push "<IF Duplication>" (node-source-path node))
700 (push "<IF Duplication>" (node-source-path new-node))
702 (reoptimize-lvar test)
703 (reoptimize-lvar new-lvar)
704 (setf (component-reanalyze *current-component*) t)))
707 ;;;; exit IR1 optimization
709 ;;; This function attempts to delete an exit node, returning true if
710 ;;; it deletes the block as a consequence:
711 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
712 ;;; anything, since there is nothing to be done.
713 ;;; -- If the exit node and its ENTRY have the same home lambda then
714 ;;; we know the exit is local, and can delete the exit. We change
715 ;;; uses of the Exit-Value to be uses of the original lvar,
716 ;;; then unlink the node. If the exit is to a TR context, then we
717 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
718 ;;; their value to this exit.
719 ;;; -- If there is no value (as in a GO), then we skip the value
722 ;;; This function is also called by environment analysis, since it
723 ;;; wants all exits to be optimized even if normal optimization was
725 (defun maybe-delete-exit (node)
726 (declare (type exit node))
727 (let ((value (exit-value node))
728 (entry (exit-entry node)))
730 (eq (node-home-lambda node) (node-home-lambda entry)))
731 (setf (entry-exits entry) (delq node (entry-exits entry)))
733 (delete-filter node (node-lvar node) value)
734 (unlink-node node)))))
737 ;;;; combination IR1 optimization
739 ;;; Report as we try each transform?
741 (defvar *show-transforms-p* nil)
743 (defun check-important-result (node info)
744 (when (and (null (node-lvar node))
745 (ir1-attributep (fun-info-attributes info) important-result))
746 (let ((*compiler-error-context* node))
748 "The return value of ~A should not be discarded."
749 (lvar-fun-name (basic-combination-fun node))))))
751 ;;; Do IR1 optimizations on a COMBINATION node.
752 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
753 (defun ir1-optimize-combination (node)
754 (when (lvar-reoptimize (basic-combination-fun node))
755 (propagate-fun-change node)
756 (maybe-terminate-block node nil))
757 (let ((args (basic-combination-args node))
758 (kind (basic-combination-kind node))
759 (info (basic-combination-fun-info node)))
762 (let ((fun (combination-lambda node)))
763 (if (eq (functional-kind fun) :let)
764 (propagate-let-args node fun)
765 (propagate-local-call-args node fun))))
769 (setf (lvar-reoptimize arg) nil))))
773 (setf (lvar-reoptimize arg) nil)))
775 (check-important-result node info)
776 (let ((fun (fun-info-destroyed-constant-args info)))
778 (let ((destroyed-constant-args (funcall fun args)))
779 (when destroyed-constant-args
780 (let ((*compiler-error-context* node))
781 (warn 'constant-modified
782 :fun-name (lvar-fun-name
783 (basic-combination-fun node)))
784 (setf (basic-combination-kind node) :error)
785 (return-from ir1-optimize-combination))))))
786 (let ((fun (fun-info-derive-type info)))
788 (let ((res (funcall fun node)))
790 (derive-node-type node (coerce-to-values res))
791 (maybe-terminate-block node nil)))))))
796 (setf (lvar-reoptimize arg) nil)))
797 (check-important-result node info)
798 (let ((fun (fun-info-destroyed-constant-args info)))
800 ;; If somebody is really sure that they want to modify
801 ;; constants, let them.
802 (policy node (> check-constant-modification 0)))
803 (let ((destroyed-constant-args (funcall fun args)))
804 (when destroyed-constant-args
805 (let ((*compiler-error-context* node))
806 (warn 'constant-modified
807 :fun-name (lvar-fun-name
808 (basic-combination-fun node)))
809 (setf (basic-combination-kind node) :error)
810 (return-from ir1-optimize-combination))))))
812 (let ((attr (fun-info-attributes info)))
813 (when (and (ir1-attributep attr foldable)
814 ;; KLUDGE: The next test could be made more sensitive,
815 ;; only suppressing constant-folding of functions with
816 ;; CALL attributes when they're actually passed
817 ;; function arguments. -- WHN 19990918
818 (not (ir1-attributep attr call))
819 (every #'constant-lvar-p args)
821 (constant-fold-call node)
822 (return-from ir1-optimize-combination)))
824 (let ((fun (fun-info-derive-type info)))
826 (let ((res (funcall fun node)))
828 (derive-node-type node (coerce-to-values res))
829 (maybe-terminate-block node nil)))))
831 (let ((fun (fun-info-optimizer info)))
832 (unless (and fun (funcall fun node))
833 ;; First give the VM a peek at the call
834 (multiple-value-bind (style transform)
835 (combination-implementation-style node)
838 ;; The VM knows how to handle this.
841 ;; The VM mostly knows how to handle this. We need
842 ;; to massage the call slightly, though.
843 (transform-call node transform (combination-fun-source-name node)))
845 ;; Let transforms have a crack at it.
846 (dolist (x (fun-info-transforms info))
848 (when *show-transforms-p*
849 (let* ((lvar (basic-combination-fun node))
850 (fname (lvar-fun-name lvar t)))
851 (/show "trying transform" x (transform-function x) "for" fname)))
852 (unless (ir1-transform node x)
854 (when *show-transforms-p*
855 (/show "quitting because IR1-TRANSFORM result was NIL"))
860 (defun xep-tail-combination-p (node)
861 (and (combination-p node)
862 (let* ((lvar (combination-lvar node))
863 (dest (when (lvar-p lvar) (lvar-dest lvar)))
864 (lambda (when (return-p dest) (return-lambda dest))))
865 (and (lambda-p lambda)
866 (eq :external (lambda-kind lambda))))))
868 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
869 ;;; the block there, and link it to the component tail.
871 ;;; Except when called during IR1 convertion, we delete the
872 ;;; continuation if it has no other uses. (If it does have other uses,
875 ;;; Termination on the basis of a continuation type is
877 ;;; -- The continuation is deleted (hence the assertion is spurious), or
878 ;;; -- We are in IR1 conversion (where THE assertions are subject to
879 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
880 ;;; uses can(?) be added later. -- APD, 2003-07-17
882 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
883 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p)
884 (declare (type (or basic-combination cast ref) node))
885 (let* ((block (node-block node))
886 (lvar (node-lvar node))
887 (ctran (node-next node))
888 (tail (component-tail (block-component block)))
889 (succ (first (block-succ block))))
890 (declare (ignore lvar))
891 (unless (or (and (eq node (block-last block)) (eq succ tail))
892 (block-delete-p block))
893 ;; Even if the combination will never return, don't terminate if this
894 ;; is the tail call of a XEP: doing that would inhibit TCO.
895 (when (and (eq (node-derived-type node) *empty-type*)
896 (not (xep-tail-combination-p node)))
897 (cond (ir1-converting-not-optimizing-p
900 (aver (eq (block-last block) node)))
902 (setf (block-last block) node)
903 (setf (ctran-use ctran) nil)
904 (setf (ctran-kind ctran) :unused)
905 (setf (ctran-block ctran) nil)
906 (setf (node-next node) nil)
907 (link-blocks block (ctran-starts-block ctran)))))
909 (node-ends-block node)))
911 (let ((succ (first (block-succ block))))
912 (unlink-blocks block succ)
913 (setf (component-reanalyze (block-component block)) t)
914 (aver (not (block-succ block)))
915 (link-blocks block tail)
916 (cond (ir1-converting-not-optimizing-p
917 (%delete-lvar-use node))
918 (t (delete-lvar-use node)
919 (when (null (block-pred succ))
920 (mark-for-deletion succ)))))
923 ;;; This is called both by IR1 conversion and IR1 optimization when
924 ;;; they have verified the type signature for the call, and are
925 ;;; wondering if something should be done to special-case the call. If
926 ;;; CALL is a call to a global function, then see whether it defined
928 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
929 ;;; the expansion and change the call to call it. Expansion is
930 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
931 ;;; true, we never expand, since this function has already been
932 ;;; converted. Local call analysis will duplicate the definition
933 ;;; if necessary. We claim that the parent form is LABELS for
934 ;;; context declarations, since we don't want it to be considered
935 ;;; a real global function.
936 ;;; -- If it is a known function, mark it as such by setting the KIND.
938 ;;; We return the leaf referenced (NIL if not a leaf) and the
939 ;;; FUN-INFO assigned.
940 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
941 (declare (type combination call))
942 (let* ((ref (lvar-uses (basic-combination-fun call)))
943 (leaf (when (ref-p ref) (ref-leaf ref)))
944 (inlinep (if (defined-fun-p leaf)
945 (defined-fun-inlinep leaf)
948 ((eq inlinep :notinline)
949 (let ((info (info :function :info (leaf-source-name leaf))))
951 (setf (basic-combination-fun-info call) info))
953 ((not (and (global-var-p leaf)
954 (eq (global-var-kind leaf) :global-function)))
959 ((nil :maybe-inline) (policy call (zerop space))))
961 (defined-fun-inline-expansion leaf)
962 (inline-expansion-ok call))
963 ;; Inline: if the function has already been converted at another call
964 ;; site in this component, we point this REF to the functional. If not,
965 ;; we convert the expansion.
967 ;; For :INLINE case local call analysis will copy the expansion later,
968 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
969 ;; expansion per component.
971 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
974 (let* ((name (leaf-source-name leaf))
975 (res (ir1-convert-inline-expansion
977 (defined-fun-inline-expansion leaf)
980 (info :function :info name))))
981 ;; Allow backward references to this function from following
982 ;; forms. (Reused only if policy matches.)
983 (push res (defined-fun-functionals leaf))
984 (change-ref-leaf ref res))))
985 (let ((fun (defined-fun-functional leaf)))
987 (and (eq inlinep :inline) (functional-kind fun)))
989 (if ir1-converting-not-optimizing-p
991 (with-ir1-environment-from-node call
993 (locall-analyze-component *current-component*)))
994 ;; If we've already converted, change ref to the converted
996 (change-ref-leaf ref fun))))
997 (values (ref-leaf ref) nil))
999 (let ((info (info :function :info (leaf-source-name leaf))))
1003 (setf (basic-combination-kind call) :known)
1004 (setf (basic-combination-fun-info call) info)))
1005 (values leaf nil)))))))
1007 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1008 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1009 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1010 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1011 ;;; syntax check, arg/result type processing, but still call
1012 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1013 ;;; and that checking is done by local call analysis.
1014 (defun validate-call-type (call type defined-type ir1-converting-not-optimizing-p)
1015 (declare (type combination call) (type ctype type))
1016 (cond ((not (fun-type-p type))
1017 (aver (multiple-value-bind (val win)
1018 (csubtypep type (specifier-type 'function))
1019 (or val (not win))))
1020 ;; In the commonish case where the function has been defined
1021 ;; in another file, we only get FUNCTION for the type; but we
1022 ;; can check whether the current call is valid for the
1023 ;; existing definition, even if only to STYLE-WARN about it.
1025 (valid-fun-use call defined-type
1026 :argument-test #'always-subtypep
1028 :lossage-fun #'compiler-style-warn
1029 :unwinnage-fun #'compiler-notify))
1030 (recognize-known-call call ir1-converting-not-optimizing-p))
1031 ((valid-fun-use call type
1032 :argument-test #'always-subtypep
1034 ;; KLUDGE: Common Lisp is such a dynamic
1035 ;; language that all we can do here in
1036 ;; general is issue a STYLE-WARNING. It
1037 ;; would be nice to issue a full WARNING
1038 ;; in the special case of of type
1039 ;; mismatches within a compilation unit
1040 ;; (as in section 3.2.2.3 of the spec)
1041 ;; but at least as of sbcl-0.6.11, we
1042 ;; don't keep track of whether the
1043 ;; mismatched data came from the same
1044 ;; compilation unit, so we can't do that.
1045 ;; -- WHN 2001-02-11
1047 ;; FIXME: Actually, I think we could
1048 ;; issue a full WARNING if the call
1049 ;; violates a DECLAIM FTYPE.
1050 :lossage-fun #'compiler-style-warn
1051 :unwinnage-fun #'compiler-notify)
1052 (assert-call-type call type)
1053 (maybe-terminate-block call ir1-converting-not-optimizing-p)
1054 (recognize-known-call call ir1-converting-not-optimizing-p))
1056 (setf (combination-kind call) :error)
1059 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1060 ;;; changed. If the call is local, we try to LET-convert it, and
1061 ;;; derive the result type. If it is a :FULL call, we validate it
1062 ;;; against the type, which recognizes known calls, does inline
1063 ;;; expansion, etc. If a call to a predicate in a non-conditional
1064 ;;; position or to a function with a source transform, then we
1065 ;;; reconvert the form to give IR1 another chance.
1066 (defun propagate-fun-change (call)
1067 (declare (type combination call))
1068 (let ((*compiler-error-context* call)
1069 (fun-lvar (basic-combination-fun call)))
1070 (setf (lvar-reoptimize fun-lvar) nil)
1071 (case (combination-kind call)
1073 (let ((fun (combination-lambda call)))
1074 (maybe-let-convert fun)
1075 (unless (member (functional-kind fun) '(:let :assignment :deleted))
1076 (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
1078 (multiple-value-bind (leaf info)
1079 (validate-call-type call (lvar-type fun-lvar) nil nil)
1080 (cond ((functional-p leaf)
1081 (convert-call-if-possible
1082 (lvar-uses (basic-combination-fun call))
1085 ((and (global-var-p leaf)
1086 (eq (global-var-kind leaf) :global-function)
1087 (leaf-has-source-name-p leaf)
1088 (or (info :function :source-transform (leaf-source-name leaf))
1090 (ir1-attributep (fun-info-attributes info)
1092 (let ((lvar (node-lvar call)))
1093 (and lvar (not (if-p (lvar-dest lvar))))))))
1094 (let ((name (leaf-source-name leaf))
1095 (dummies (make-gensym-list
1096 (length (combination-args call)))))
1097 (transform-call call
1099 (,@(if (symbolp name)
1103 (leaf-source-name leaf)))))))))
1106 ;;;; known function optimization
1108 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1109 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1110 ;;; replace it, otherwise add a new one.
1111 (defun record-optimization-failure (node transform args)
1112 (declare (type combination node) (type transform transform)
1113 (type (or fun-type list) args))
1114 (let* ((table (component-failed-optimizations *component-being-compiled*))
1115 (found (assoc transform (gethash node table))))
1117 (setf (cdr found) args)
1118 (push (cons transform args) (gethash node table))))
1121 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1122 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1123 ;;; doing the transform for some reason and FLAME is true, then we
1124 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1125 ;;; finalize to pick up. We return true if the transform failed, and
1126 ;;; thus further transformation should be attempted. We return false
1127 ;;; if either the transform succeeded or was aborted.
1128 (defun ir1-transform (node transform)
1129 (declare (type combination node) (type transform transform))
1130 (let* ((type (transform-type transform))
1131 (fun (transform-function transform))
1132 (constrained (fun-type-p type))
1133 (table (component-failed-optimizations *component-being-compiled*))
1134 (flame (if (transform-important transform)
1135 (policy node (>= speed inhibit-warnings))
1136 (policy node (> speed inhibit-warnings))))
1137 (*compiler-error-context* node))
1138 (cond ((or (not constrained)
1139 (valid-fun-use node type))
1140 (multiple-value-bind (severity args)
1141 (catch 'give-up-ir1-transform
1142 (transform-call node
1144 (combination-fun-source-name node))
1148 (remhash node table)
1151 (setf (combination-kind node) :error)
1153 (apply #'warn args))
1154 (remhash node table)
1159 (record-optimization-failure node transform args))
1160 (setf (gethash node table)
1161 (remove transform (gethash node table) :key #'car)))
1164 (remhash node table)
1169 :argument-test #'types-equal-or-intersect
1170 :result-test #'values-types-equal-or-intersect))
1171 (record-optimization-failure node transform type)
1176 ;;; When we don't like an IR1 transform, we throw the severity/reason
1179 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1180 ;;; aborting this attempt to transform the call, but admitting the
1181 ;;; possibility that this or some other transform will later succeed.
1182 ;;; If arguments are supplied, they are format arguments for an
1183 ;;; efficiency note.
1185 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1186 ;;; force a normal call to the function at run time. No further
1187 ;;; optimizations will be attempted.
1189 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1190 ;;; delay the transform on the node until later. REASONS specifies
1191 ;;; when the transform will be later retried. The :OPTIMIZE reason
1192 ;;; causes the transform to be delayed until after the current IR1
1193 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1194 ;;; be delayed until after constraint propagation.
1196 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1197 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1198 ;;; do CASE operations on the various REASON values, it might be a
1199 ;;; good idea to go OO, representing the reasons by objects, using
1200 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1201 ;;; SIGNAL instead of THROW.
1202 (declaim (ftype (function (&rest t) nil) give-up-ir1-transform))
1203 (defun give-up-ir1-transform (&rest args)
1204 (throw 'give-up-ir1-transform (values :failure args)))
1205 (defun abort-ir1-transform (&rest args)
1206 (throw 'give-up-ir1-transform (values :aborted args)))
1207 (defun delay-ir1-transform (node &rest reasons)
1208 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1210 (setf *delayed-ir1-transforms*
1211 (acons node reasons *delayed-ir1-transforms*))
1212 (throw 'give-up-ir1-transform :delayed))
1214 (dolist (reason reasons)
1215 (pushnew reason (cdr assoc)))
1216 (throw 'give-up-ir1-transform :delayed)))))
1218 ;;; Clear any delayed transform with no reasons - these should have
1219 ;;; been tried in the last pass. Then remove the reason from the
1220 ;;; delayed transform reasons, and if any become empty then set
1221 ;;; reoptimize flags for the node. Return true if any transforms are
1223 (defun retry-delayed-ir1-transforms (reason)
1224 (setf *delayed-ir1-transforms*
1225 (remove-if-not #'cdr *delayed-ir1-transforms*))
1226 (let ((reoptimize nil))
1227 (dolist (assoc *delayed-ir1-transforms*)
1228 (let ((reasons (remove reason (cdr assoc))))
1229 (setf (cdr assoc) reasons)
1231 (let ((node (car assoc)))
1232 (unless (node-deleted node)
1234 (setf (node-reoptimize node) t)
1235 (let ((block (node-block node)))
1236 (setf (block-reoptimize block) t)
1237 (reoptimize-component (block-component block) :maybe)))))))
1240 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1241 ;;; environment, and then install it as the function for the call
1242 ;;; NODE. We do local call analysis so that the new function is
1243 ;;; integrated into the control flow.
1245 ;;; We require the original function source name in order to generate
1246 ;;; a meaningful debug name for the lambda we set up. (It'd be
1247 ;;; possible to do this starting from debug names as well as source
1248 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1249 ;;; generality, since source names are always known to our callers.)
1250 (defun transform-call (call res source-name)
1251 (declare (type combination call) (list res))
1252 (aver (and (legal-fun-name-p source-name)
1253 (not (eql source-name '.anonymous.))))
1254 (node-ends-block call)
1255 ;; The internal variables of a transform are not going to be
1256 ;; interesting to the debugger, so there's no sense in
1257 ;; suppressing the substitution of variables with only one use
1258 ;; (the extra variables can slow down constraint propagation).
1260 ;; This needs to be done before the WITH-IR1-ENVIRONMENT-FROM-NODE,
1261 ;; so that it will bind *LEXENV* to the right environment.
1262 (setf (combination-lexenv call)
1263 (make-lexenv :default (combination-lexenv call)
1264 :policy (process-optimize-decl
1266 (preserve-single-use-debug-variables 0))
1268 (combination-lexenv call)))))
1269 (with-ir1-environment-from-node call
1270 (with-component-last-block (*current-component*
1271 (block-next (node-block call)))
1273 (let ((new-fun (ir1-convert-inline-lambda
1275 :debug-name (debug-name 'lambda-inlined source-name)
1277 (ref (lvar-use (combination-fun call))))
1278 (change-ref-leaf ref new-fun)
1279 (setf (combination-kind call) :full)
1280 (maybe-propagate-dynamic-extent call new-fun)
1281 (locall-analyze-component *current-component*))))
1284 ;;; Replace a call to a foldable function of constant arguments with
1285 ;;; the result of evaluating the form. If there is an error during the
1286 ;;; evaluation, we give a warning and leave the call alone, making the
1287 ;;; call a :ERROR call.
1289 ;;; If there is more than one value, then we transform the call into a
1291 (defun constant-fold-call (call)
1292 (let ((args (mapcar #'lvar-value (combination-args call)))
1293 (fun-name (combination-fun-source-name call)))
1294 (multiple-value-bind (values win)
1295 (careful-call fun-name
1298 ;; Note: CMU CL had COMPILER-WARN here, and that
1299 ;; seems more natural, but it's probably not.
1301 ;; It's especially not while bug 173 exists:
1304 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1306 ;; can cause constant-folding TYPE-ERRORs (in
1307 ;; #'<=) when END can be proved to be NIL, even
1308 ;; though the code is perfectly legal and safe
1309 ;; because a NIL value of END means that the
1310 ;; #'<= will never be executed.
1312 ;; Moreover, even without bug 173,
1313 ;; quite-possibly-valid code like
1314 ;; (COND ((NONINLINED-PREDICATE END)
1315 ;; (UNLESS (<= END SIZE))
1317 ;; (where NONINLINED-PREDICATE is something the
1318 ;; compiler can't do at compile time, but which
1319 ;; turns out to make the #'<= expression
1320 ;; unreachable when END=NIL) could cause errors
1321 ;; when the compiler tries to constant-fold (<=
1324 ;; So, with or without bug 173, it'd be
1325 ;; unnecessarily evil to do a full
1326 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1327 ;; from COMPILE-FILE) for legal code, so we we
1328 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1329 #-sb-xc-host #'compiler-style-warn
1330 ;; On the other hand, for code we control, we
1331 ;; should be able to work around any bug
1332 ;; 173-related problems, and in particular we
1333 ;; want to be alerted to calls to our own
1334 ;; functions which aren't being folded away; a
1335 ;; COMPILER-WARNING is butch enough to stop the
1336 ;; SBCL build itself in its tracks.
1337 #+sb-xc-host #'compiler-warn
1340 (setf (combination-kind call) :error))
1341 ((and (proper-list-of-length-p values 1))
1342 (with-ir1-environment-from-node call
1343 (let* ((lvar (node-lvar call))
1344 (prev (node-prev call))
1345 (intermediate-ctran (make-ctran)))
1346 (%delete-lvar-use call)
1347 (setf (ctran-next prev) nil)
1348 (setf (node-prev call) nil)
1349 (reference-constant prev intermediate-ctran lvar
1351 (link-node-to-previous-ctran call intermediate-ctran)
1352 (reoptimize-lvar lvar)
1353 (flush-combination call))))
1354 (t (let ((dummies (make-gensym-list (length args))))
1358 (declare (ignore ,@dummies))
1359 (values ,@(mapcar (lambda (x) `',x) values)))
1363 ;;;; local call optimization
1365 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1367 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1368 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1371 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1372 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1373 (defun propagate-to-refs (leaf type)
1374 (declare (type leaf leaf) (type ctype type))
1375 (let ((var-type (leaf-type leaf))
1376 (refs (leaf-refs leaf)))
1377 (unless (or (fun-type-p var-type)
1379 (eq :declared (leaf-where-from leaf))
1380 (type-needs-conservation-p var-type)))
1381 (let ((int (type-approx-intersection2 var-type type)))
1382 (when (type/= int var-type)
1383 (setf (leaf-type leaf) int)
1384 (let ((s-int (make-single-value-type int)))
1386 (derive-node-type ref s-int)
1387 ;; KLUDGE: LET var substitution
1388 (let* ((lvar (node-lvar ref)))
1389 (when (and lvar (combination-p (lvar-dest lvar)))
1390 (reoptimize-lvar lvar)))))))
1393 ;;; Iteration variable: exactly one SETQ of the form:
1395 ;;; (let ((var initial))
1397 ;;; (setq var (+ var step))
1399 (defun maybe-infer-iteration-var-type (var initial-type)
1400 (binding* ((sets (lambda-var-sets var) :exit-if-null)
1402 (() (null (rest sets)) :exit-if-null)
1403 (set-use (principal-lvar-use (set-value set)))
1404 (() (and (combination-p set-use)
1405 (eq (combination-kind set-use) :known)
1406 (fun-info-p (combination-fun-info set-use))
1407 (not (node-to-be-deleted-p set-use))
1408 (or (eq (combination-fun-source-name set-use) '+)
1409 (eq (combination-fun-source-name set-use) '-)))
1411 (minusp (eq (combination-fun-source-name set-use) '-))
1412 (+-args (basic-combination-args set-use))
1413 (() (and (proper-list-of-length-p +-args 2 2)
1414 (let ((first (principal-lvar-use
1417 (eq (ref-leaf first) var))))
1419 (step-type (lvar-type (second +-args)))
1420 (set-type (lvar-type (set-value set))))
1421 (when (and (numeric-type-p initial-type)
1422 (numeric-type-p step-type)
1423 (or (numeric-type-equal initial-type step-type)
1424 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1425 ;; the initial and the step are of different types,
1426 ;; and the step is less contagious.
1427 (numeric-type-equal initial-type
1428 (numeric-contagion initial-type
1430 (labels ((leftmost (x y cmp cmp=)
1431 (cond ((eq x nil) nil)
1434 (let ((x1 (first x)))
1436 (let ((y1 (first y)))
1437 (if (funcall cmp x1 y1) x y)))
1439 (if (funcall cmp x1 y) x y)))))
1441 (let ((y1 (first y)))
1442 (if (funcall cmp= x y1) x y)))
1443 (t (if (funcall cmp x y) x y))))
1444 (max* (x y) (leftmost x y #'> #'>=))
1445 (min* (x y) (leftmost x y #'< #'<=)))
1446 (multiple-value-bind (low high)
1447 (let ((step-type-non-negative (csubtypep step-type (specifier-type
1449 (step-type-non-positive (csubtypep step-type (specifier-type
1451 (cond ((or (and step-type-non-negative (not minusp))
1452 (and step-type-non-positive minusp))
1453 (values (numeric-type-low initial-type)
1454 (when (and (numeric-type-p set-type)
1455 (numeric-type-equal set-type initial-type))
1456 (max* (numeric-type-high initial-type)
1457 (numeric-type-high set-type)))))
1458 ((or (and step-type-non-positive (not minusp))
1459 (and step-type-non-negative minusp))
1460 (values (when (and (numeric-type-p set-type)
1461 (numeric-type-equal set-type initial-type))
1462 (min* (numeric-type-low initial-type)
1463 (numeric-type-low set-type)))
1464 (numeric-type-high initial-type)))
1467 (modified-numeric-type initial-type
1470 :enumerable nil))))))
1471 (deftransform + ((x y) * * :result result)
1472 "check for iteration variable reoptimization"
1473 (let ((dest (principal-lvar-end result))
1474 (use (principal-lvar-use x)))
1475 (when (and (ref-p use)
1479 (reoptimize-lvar (set-value dest))))
1480 (give-up-ir1-transform))
1482 ;;; Figure out the type of a LET variable that has sets. We compute
1483 ;;; the union of the INITIAL-TYPE and the types of all the set
1484 ;;; values and to a PROPAGATE-TO-REFS with this type.
1485 (defun propagate-from-sets (var initial-type)
1486 (let ((changes (not (csubtypep (lambda-var-last-initial-type var) initial-type)))
1488 (dolist (set (lambda-var-sets var))
1489 (let ((type (lvar-type (set-value set))))
1491 (when (node-reoptimize set)
1492 (let ((old-type (node-derived-type set)))
1493 (unless (values-subtypep old-type type)
1494 (derive-node-type set (make-single-value-type type))
1496 (setf (node-reoptimize set) nil))))
1498 (setf (lambda-var-last-initial-type var) initial-type)
1499 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type)
1500 (apply #'type-union initial-type types))))
1501 (propagate-to-refs var res-type))))
1504 ;;; If a LET variable, find the initial value's type and do
1505 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1507 (defun ir1-optimize-set (node)
1508 (declare (type cset node))
1509 (let ((var (set-var node)))
1510 (when (and (lambda-var-p var) (leaf-refs var))
1511 (let ((home (lambda-var-home var)))
1512 (when (eq (functional-kind home) :let)
1513 (let* ((initial-value (let-var-initial-value var))
1514 (initial-type (lvar-type initial-value)))
1515 (setf (lvar-reoptimize initial-value) nil)
1516 (propagate-from-sets var initial-type))))))
1517 (derive-node-type node (make-single-value-type
1518 (lvar-type (set-value node))))
1519 (setf (node-reoptimize node) nil)
1522 ;;; Return true if the value of REF will always be the same (and is
1523 ;;; thus legal to substitute.)
1524 (defun constant-reference-p (ref)
1525 (declare (type ref ref))
1526 (let ((leaf (ref-leaf ref)))
1528 ((or constant functional) t)
1530 (null (lambda-var-sets leaf)))
1532 (not (eq (defined-fun-inlinep leaf) :notinline)))
1534 (case (global-var-kind leaf)
1536 (let ((name (leaf-source-name leaf)))
1538 (eq (symbol-package (fun-name-block-name name))
1540 (info :function :info name)))))))))
1542 ;;; If we have a non-set LET var with a single use, then (if possible)
1543 ;;; replace the variable reference's LVAR with the arg lvar.
1545 ;;; We change the REF to be a reference to NIL with unused value, and
1546 ;;; let it be flushed as dead code. A side effect of this substitution
1547 ;;; is to delete the variable.
1548 (defun substitute-single-use-lvar (arg var)
1549 (declare (type lvar arg) (type lambda-var var))
1550 (binding* ((ref (first (leaf-refs var)))
1551 (lvar (node-lvar ref) :exit-if-null)
1552 (dest (lvar-dest lvar)))
1554 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1555 ;; LVAR-USEs should not be met on one path. Another problem
1556 ;; is with dynamic-extent.
1557 (eq (lvar-uses lvar) ref)
1558 (not (block-delete-p (node-block ref)))
1560 ;; we should not change lifetime of unknown values lvars
1562 (and (type-single-value-p (lvar-derived-type arg))
1563 (multiple-value-bind (pdest pprev)
1564 (principal-lvar-end lvar)
1565 (declare (ignore pdest))
1566 (lvar-single-value-p pprev))))
1568 (or (eq (basic-combination-fun dest) lvar)
1569 (and (eq (basic-combination-kind dest) :local)
1570 (type-single-value-p (lvar-derived-type arg)))))
1572 ;; While CRETURN and EXIT nodes may be known-values,
1573 ;; they have their own complications, such as
1574 ;; substitution into CRETURN may create new tail calls.
1577 (aver (lvar-single-value-p lvar))
1579 (eq (node-home-lambda ref)
1580 (lambda-home (lambda-var-home var))))
1581 (let ((ref-type (single-value-type (node-derived-type ref))))
1582 (cond ((csubtypep (single-value-type (lvar-type arg)) ref-type)
1583 (substitute-lvar-uses lvar arg
1584 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1586 (delete-lvar-use ref))
1588 (let* ((value (make-lvar))
1589 (cast (insert-cast-before ref value ref-type
1590 ;; KLUDGE: it should be (TYPE-CHECK 0)
1592 (setf (cast-type-to-check cast) *wild-type*)
1593 (substitute-lvar-uses value arg
1596 (%delete-lvar-use ref)
1597 (add-lvar-use cast lvar)))))
1598 (setf (node-derived-type ref) *wild-type*)
1599 (change-ref-leaf ref (find-constant nil))
1602 (reoptimize-lvar lvar)
1605 ;;; Delete a LET, removing the call and bind nodes, and warning about
1606 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1607 ;;; along right away and delete the REF and then the lambda, since we
1608 ;;; flush the FUN lvar.
1609 (defun delete-let (clambda)
1610 (declare (type clambda clambda))
1611 (aver (functional-letlike-p clambda))
1612 (note-unreferenced-vars clambda)
1613 (let ((call (let-combination clambda)))
1614 (flush-dest (basic-combination-fun call))
1616 (unlink-node (lambda-bind clambda))
1617 (setf (lambda-bind clambda) nil))
1618 (setf (functional-kind clambda) :zombie)
1619 (let ((home (lambda-home clambda)))
1620 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1623 ;;; This function is called when one of the arguments to a LET
1624 ;;; changes. We look at each changed argument. If the corresponding
1625 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1626 ;;; consider substituting for the variable, and also propagate
1627 ;;; derived-type information for the arg to all the VAR's refs.
1629 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1630 ;;; subtype of the argument's leaf type. This prevents type checking
1631 ;;; from being defeated, and also ensures that the best representation
1632 ;;; for the variable can be used.
1634 ;;; Substitution of individual references is inhibited if the
1635 ;;; reference is in a different component from the home. This can only
1636 ;;; happen with closures over top level lambda vars. In such cases,
1637 ;;; the references may have already been compiled, and thus can't be
1638 ;;; retroactively modified.
1640 ;;; If all of the variables are deleted (have no references) when we
1641 ;;; are done, then we delete the LET.
1643 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1645 (defun propagate-let-args (call fun)
1646 (declare (type combination call) (type clambda fun))
1647 (loop for arg in (combination-args call)
1648 and var in (lambda-vars fun) do
1649 (when (and arg (lvar-reoptimize arg))
1650 (setf (lvar-reoptimize arg) nil)
1652 ((lambda-var-sets var)
1653 (propagate-from-sets var (lvar-type arg)))
1654 ((let ((use (lvar-uses arg)))
1656 (let ((leaf (ref-leaf use)))
1657 (when (and (constant-reference-p use)
1658 (csubtypep (leaf-type leaf)
1659 ;; (NODE-DERIVED-TYPE USE) would
1660 ;; be better -- APD, 2003-05-15
1662 (propagate-to-refs var (lvar-type arg))
1663 (let ((use-component (node-component use)))
1664 (prog1 (substitute-leaf-if
1666 (cond ((eq (node-component ref) use-component)
1669 (aver (lambda-toplevelish-p (lambda-home fun)))
1673 ((and (null (rest (leaf-refs var)))
1674 ;; Don't substitute single-ref variables on high-debug /
1675 ;; low speed, to improve the debugging experience.
1676 (policy call (< preserve-single-use-debug-variables 3))
1677 (substitute-single-use-lvar arg var)))
1679 (propagate-to-refs var (lvar-type arg))))))
1681 (when (every #'not (combination-args call))
1686 ;;; This function is called when one of the args to a non-LET local
1687 ;;; call changes. For each changed argument corresponding to an unset
1688 ;;; variable, we compute the union of the types across all calls and
1689 ;;; propagate this type information to the var's refs.
1691 ;;; If the function has an XEP, then we don't do anything, since we
1692 ;;; won't discover anything.
1694 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1695 ;;; corresponding to changed arguments in CALL, since the only use in
1696 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1698 (defun propagate-local-call-args (call fun)
1699 (declare (type combination call) (type clambda fun))
1700 (unless (or (functional-entry-fun fun)
1701 (lambda-optional-dispatch fun))
1702 (let* ((vars (lambda-vars fun))
1703 (union (mapcar (lambda (arg var)
1705 (lvar-reoptimize arg)
1706 (null (basic-var-sets var)))
1708 (basic-combination-args call)
1710 (this-ref (lvar-use (basic-combination-fun call))))
1712 (dolist (arg (basic-combination-args call))
1714 (setf (lvar-reoptimize arg) nil)))
1716 (dolist (ref (leaf-refs fun))
1717 (let ((dest (node-dest ref)))
1718 (unless (or (eq ref this-ref) (not dest))
1720 (mapcar (lambda (this-arg old)
1722 (setf (lvar-reoptimize this-arg) nil)
1723 (type-union (lvar-type this-arg) old)))
1724 (basic-combination-args dest)
1727 (loop for var in vars
1729 when type do (propagate-to-refs var type))))
1733 ;;;; multiple values optimization
1735 ;;; Do stuff to notice a change to a MV combination node. There are
1736 ;;; two main branches here:
1737 ;;; -- If the call is local, then it is already a MV let, or should
1738 ;;; become one. Note that although all :LOCAL MV calls must eventually
1739 ;;; be converted to :MV-LETs, there can be a window when the call
1740 ;;; is local, but has not been LET converted yet. This is because
1741 ;;; the entry-point lambdas may have stray references (in other
1742 ;;; entry points) that have not been deleted yet.
1743 ;;; -- The call is full. This case is somewhat similar to the non-MV
1744 ;;; combination optimization: we propagate return type information and
1745 ;;; notice non-returning calls. We also have an optimization
1746 ;;; which tries to convert MV-CALLs into MV-binds.
1747 (defun ir1-optimize-mv-combination (node)
1748 (ecase (basic-combination-kind node)
1750 (let ((fun-lvar (basic-combination-fun node)))
1751 (when (lvar-reoptimize fun-lvar)
1752 (setf (lvar-reoptimize fun-lvar) nil)
1753 (maybe-let-convert (combination-lambda node))))
1754 (setf (lvar-reoptimize (first (basic-combination-args node))) nil)
1755 (when (eq (functional-kind (combination-lambda node)) :mv-let)
1756 (unless (convert-mv-bind-to-let node)
1757 (ir1-optimize-mv-bind node))))
1759 (let* ((fun (basic-combination-fun node))
1760 (fun-changed (lvar-reoptimize fun))
1761 (args (basic-combination-args node)))
1763 (setf (lvar-reoptimize fun) nil)
1764 (let ((type (lvar-type fun)))
1765 (when (fun-type-p type)
1766 (derive-node-type node (fun-type-returns type))))
1767 (maybe-terminate-block node nil)
1768 (let ((use (lvar-uses fun)))
1769 (when (and (ref-p use) (functional-p (ref-leaf use)))
1770 (convert-call-if-possible use node)
1771 (when (eq (basic-combination-kind node) :local)
1772 (maybe-let-convert (ref-leaf use))))))
1773 (unless (or (eq (basic-combination-kind node) :local)
1774 (eq (lvar-fun-name fun) '%throw))
1775 (ir1-optimize-mv-call node))
1777 (setf (lvar-reoptimize arg) nil))))
1781 ;;; Propagate derived type info from the values lvar to the vars.
1782 (defun ir1-optimize-mv-bind (node)
1783 (declare (type mv-combination node))
1784 (let* ((arg (first (basic-combination-args node)))
1785 (vars (lambda-vars (combination-lambda node)))
1786 (n-vars (length vars))
1787 (types (values-type-in (lvar-derived-type arg)
1789 (loop for var in vars
1791 do (if (basic-var-sets var)
1792 (propagate-from-sets var type)
1793 (propagate-to-refs var type)))
1794 (setf (lvar-reoptimize arg) nil))
1797 ;;; If possible, convert a general MV call to an MV-BIND. We can do
1799 ;;; -- The call has only one argument, and
1800 ;;; -- The function has a known fixed number of arguments, or
1801 ;;; -- The argument yields a known fixed number of values.
1803 ;;; What we do is change the function in the MV-CALL to be a lambda
1804 ;;; that "looks like an MV bind", which allows
1805 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
1806 ;;; converted (the next time around.) This new lambda just calls the
1807 ;;; actual function with the MV-BIND variables as arguments. Note that
1808 ;;; this new MV bind is not let-converted immediately, as there are
1809 ;;; going to be stray references from the entry-point functions until
1810 ;;; they get deleted.
1812 ;;; In order to avoid loss of argument count checking, we only do the
1813 ;;; transformation according to a known number of expected argument if
1814 ;;; safety is unimportant. We can always convert if we know the number
1815 ;;; of actual values, since the normal call that we build will still
1816 ;;; do any appropriate argument count checking.
1818 ;;; We only attempt the transformation if the called function is a
1819 ;;; constant reference. This allows us to just splice the leaf into
1820 ;;; the new function, instead of trying to somehow bind the function
1821 ;;; expression. The leaf must be constant because we are evaluating it
1822 ;;; again in a different place. This also has the effect of squelching
1823 ;;; multiple warnings when there is an argument count error.
1824 (defun ir1-optimize-mv-call (node)
1825 (let ((fun (basic-combination-fun node))
1826 (*compiler-error-context* node)
1827 (ref (lvar-uses (basic-combination-fun node)))
1828 (args (basic-combination-args node)))
1830 (unless (and (ref-p ref) (constant-reference-p ref)
1832 (return-from ir1-optimize-mv-call))
1834 (multiple-value-bind (min max)
1835 (fun-type-nargs (lvar-type fun))
1837 (multiple-value-bind (types nvals)
1838 (values-types (lvar-derived-type (first args)))
1839 (declare (ignore types))
1840 (if (eq nvals :unknown) nil nvals))))
1843 (when (and min (< total-nvals min))
1845 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1848 (setf (basic-combination-kind node) :error)
1849 (return-from ir1-optimize-mv-call))
1850 (when (and max (> total-nvals max))
1852 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1855 (setf (basic-combination-kind node) :error)
1856 (return-from ir1-optimize-mv-call)))
1858 (let ((count (cond (total-nvals)
1859 ((and (policy node (zerop verify-arg-count))
1864 (with-ir1-environment-from-node node
1865 (let* ((dums (make-gensym-list count))
1867 (leaf (ref-leaf ref))
1868 (fun (ir1-convert-lambda
1869 `(lambda (&optional ,@dums &rest ,ignore)
1870 (declare (ignore ,ignore))
1871 (%funcall ,leaf ,@dums))
1872 :source-name (leaf-%source-name leaf)
1873 :debug-name (leaf-%debug-name leaf))))
1874 (change-ref-leaf ref fun)
1875 (aver (eq (basic-combination-kind node) :full))
1876 (locall-analyze-component *current-component*)
1877 (aver (eq (basic-combination-kind node) :local)))))))))
1881 ;;; (multiple-value-bind
1890 ;;; What we actually do is convert the VALUES combination into a
1891 ;;; normal LET combination calling the original :MV-LET lambda. If
1892 ;;; there are extra args to VALUES, discard the corresponding
1893 ;;; lvars. If there are insufficient args, insert references to NIL.
1894 (defun convert-mv-bind-to-let (call)
1895 (declare (type mv-combination call))
1896 (let* ((arg (first (basic-combination-args call)))
1897 (use (lvar-uses arg)))
1898 (when (and (combination-p use)
1899 (eq (lvar-fun-name (combination-fun use))
1901 (let* ((fun (combination-lambda call))
1902 (vars (lambda-vars fun))
1903 (vals (combination-args use))
1904 (nvars (length vars))
1905 (nvals (length vals)))
1906 (cond ((> nvals nvars)
1907 (mapc #'flush-dest (subseq vals nvars))
1908 (setq vals (subseq vals 0 nvars)))
1910 (with-ir1-environment-from-node use
1911 (let ((node-prev (node-prev use)))
1912 (setf (node-prev use) nil)
1913 (setf (ctran-next node-prev) nil)
1914 (collect ((res vals))
1915 (loop for count below (- nvars nvals)
1916 for prev = node-prev then ctran
1917 for ctran = (make-ctran)
1918 and lvar = (make-lvar use)
1919 do (reference-constant prev ctran lvar nil)
1921 finally (link-node-to-previous-ctran
1923 (setq vals (res)))))))
1924 (setf (combination-args use) vals)
1925 (flush-dest (combination-fun use))
1926 (let ((fun-lvar (basic-combination-fun call)))
1927 (setf (lvar-dest fun-lvar) use)
1928 (setf (combination-fun use) fun-lvar)
1929 (flush-lvar-externally-checkable-type fun-lvar))
1930 (setf (combination-kind use) :local)
1931 (setf (functional-kind fun) :let)
1932 (flush-dest (first (basic-combination-args call)))
1935 (reoptimize-lvar (first vals)))
1936 (propagate-to-args use fun)
1937 (reoptimize-call use))
1941 ;;; (values-list (list x y z))
1946 ;;; In implementation, this is somewhat similar to
1947 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
1948 ;;; args of the VALUES-LIST call, flushing the old argument lvar
1949 ;;; (allowing the LIST to be flushed.)
1951 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
1952 (defoptimizer (values-list optimizer) ((list) node)
1953 (let ((use (lvar-uses list)))
1954 (when (and (combination-p use)
1955 (eq (lvar-fun-name (combination-fun use))
1958 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
1959 (change-ref-leaf (lvar-uses (combination-fun node))
1960 (find-free-fun 'values "in a strange place"))
1961 (setf (combination-kind node) :full)
1962 (let ((args (combination-args use)))
1964 (setf (lvar-dest arg) node)
1965 (flush-lvar-externally-checkable-type arg))
1966 (setf (combination-args use) nil)
1968 (setf (combination-args node) args))
1971 ;;; If VALUES appears in a non-MV context, then effectively convert it
1972 ;;; to a PROG1. This allows the computation of the additional values
1973 ;;; to become dead code.
1974 (deftransform values ((&rest vals) * * :node node)
1975 (unless (lvar-single-value-p (node-lvar node))
1976 (give-up-ir1-transform))
1977 (setf (node-derived-type node)
1978 (make-short-values-type (list (single-value-type
1979 (node-derived-type node)))))
1980 (principal-lvar-single-valuify (node-lvar node))
1982 (let ((dummies (make-gensym-list (length (cdr vals)))))
1983 `(lambda (val ,@dummies)
1984 (declare (ignore ,@dummies))
1990 (defun delete-cast (cast)
1991 (declare (type cast cast))
1992 (let ((value (cast-value cast))
1993 (lvar (node-lvar cast)))
1994 (delete-filter cast lvar value)
1996 (reoptimize-lvar lvar)
1997 (when (lvar-single-value-p lvar)
1998 (note-single-valuified-lvar lvar)))
2001 (defun ir1-optimize-cast (cast &optional do-not-optimize)
2002 (declare (type cast cast))
2003 (let ((value (cast-value cast))
2004 (atype (cast-asserted-type cast)))
2005 (when (not do-not-optimize)
2006 (let ((lvar (node-lvar cast)))
2007 (when (values-subtypep (lvar-derived-type value)
2008 (cast-asserted-type cast))
2010 (return-from ir1-optimize-cast t))
2012 (when (and (listp (lvar-uses value))
2014 ;; Pathwise removing of CAST
2015 (let ((ctran (node-next cast))
2016 (dest (lvar-dest lvar))
2019 (do-uses (use value)
2020 (when (and (values-subtypep (node-derived-type use) atype)
2021 (immediately-used-p value use))
2023 (when ctran (ensure-block-start ctran))
2024 (setq next-block (first (block-succ (node-block cast))))
2025 (ensure-block-start (node-prev cast))
2026 (reoptimize-lvar lvar)
2027 (setf (lvar-%derived-type value) nil))
2028 (%delete-lvar-use use)
2029 (add-lvar-use use lvar)
2030 (unlink-blocks (node-block use) (node-block cast))
2031 (link-blocks (node-block use) next-block)
2032 (when (and (return-p dest)
2033 (basic-combination-p use)
2034 (eq (basic-combination-kind use) :local))
2036 (dolist (use (merges))
2037 (merge-tail-sets use)))))))
2039 (let* ((value-type (lvar-derived-type value))
2040 (int (values-type-intersection value-type atype)))
2041 (derive-node-type cast int)
2042 (when (eq int *empty-type*)
2043 (unless (eq value-type *empty-type*)
2045 ;; FIXME: Do it in one step.
2048 (if (cast-single-value-p cast)
2050 `(multiple-value-call #'list 'dummy)))
2053 ;; FIXME: Derived type.
2054 `(%compile-time-type-error 'dummy
2055 ',(type-specifier atype)
2056 ',(type-specifier value-type)))
2057 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2058 ;; functions, so we declare the return type of
2059 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2061 (setq value (cast-value cast))
2062 (derive-node-type (lvar-uses value) *empty-type*)
2063 (maybe-terminate-block (lvar-uses value) nil)
2064 ;; FIXME: Is it necessary?
2065 (aver (null (block-pred (node-block cast))))
2066 (delete-block-lazily (node-block cast))
2067 (return-from ir1-optimize-cast)))
2068 (when (eq (node-derived-type cast) *empty-type*)
2069 (maybe-terminate-block cast nil))
2071 (when (and (cast-%type-check cast)
2072 (values-subtypep value-type
2073 (cast-type-to-check cast)))
2074 (setf (cast-%type-check cast) nil))))
2076 (unless do-not-optimize
2077 (setf (node-reoptimize cast) nil)))
2079 (deftransform make-symbol ((string) (simple-string))
2080 `(%make-symbol string))