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* ((type (lvar-type thing
))
27 (principal-lvar (principal-lvar thing
))
28 (principal-use (lvar-uses principal-lvar
))
30 (or (and (ref-p principal-use
)
31 (constant-p (setf leaf
(ref-leaf principal-use
)))
32 ;; LEAF may be a CONSTANT behind a cast that will
33 ;; later turn out to be of the wrong type.
34 ;; And ir1-transforms suffer from this because
35 ;; they expect LVAR-VALUE to be of a restricted type.
36 (or (not (lvar-reoptimize principal-lvar
))
37 (ctypep (constant-value leaf
) type
)))
38 ;; check for EQL types and singleton numeric types
39 (values (type-singleton-p type
))))))
41 ;;; Return the constant value for an LVAR whose only use is a constant
43 (declaim (ftype (function (lvar) t
) lvar-value
))
44 (defun lvar-value (lvar)
45 (let ((use (principal-lvar-use lvar
))
46 (type (lvar-type lvar
))
49 (constant-p (setf leaf
(ref-leaf use
))))
51 (multiple-value-bind (constantp value
) (type-singleton-p type
)
53 (error "~S used on non-constant LVAR ~S" 'lvar-value lvar
))
56 ;;;; interface for obtaining results of type inference
58 ;;; Our best guess for the type of this lvar's value. Note that this
59 ;;; may be VALUES or FUNCTION type, which cannot be passed as an
60 ;;; argument to the normal type operations. See LVAR-TYPE.
62 ;;; The result value is cached in the LVAR-%DERIVED-TYPE slot. If the
63 ;;; slot is true, just return that value, otherwise recompute and
64 ;;; stash the value there.
65 (eval-when (:compile-toplevel
:execute
)
66 (#+sb-xc-host cl
:defmacro
67 #-sb-xc-host sb
!xc
:defmacro
68 lvar-type-using
(lvar accessor
)
69 `(let ((uses (lvar-uses ,lvar
)))
70 (cond ((null uses
) *empty-type
*)
72 (do ((res (,accessor
(first uses
))
73 (values-type-union (,accessor
(first current
))
75 (current (rest uses
) (rest current
)))
76 ((or (null current
) (eq res
*wild-type
*))
81 (defun %lvar-derived-type
(lvar)
82 (lvar-type-using lvar node-derived-type
))
84 ;;; Return the derived type for LVAR's first value. This is guaranteed
85 ;;; not to be a VALUES or FUNCTION type.
86 (declaim (ftype (sfunction (lvar) ctype
) lvar-type
))
87 (defun lvar-type (lvar)
88 (single-value-type (lvar-derived-type lvar
)))
90 ;;; LVAR-CONSERVATIVE-TYPE
92 ;;; Certain types refer to the contents of an object, which can
93 ;;; change without type derivation noticing: CONS types and ARRAY
94 ;;; types suffer from this:
96 ;;; (let ((x (the (cons fixnum fixnum) (cons a b))))
98 ;;; (+ (car x) (cdr x)))
100 ;;; Python doesn't realize that the SETF CAR can change the type of X -- so we
101 ;;; cannot use LVAR-TYPE which gets the derived results. Worse, still, instead
102 ;;; of (SETF CAR) we might have a call to a user-defined function FOO which
103 ;;; does the same -- so there is no way to use the derived information in
106 ;;; So, the conservative option is to use the derived type if the leaf has
107 ;;; only a single ref -- in which case there cannot be a prior call that
108 ;;; mutates it. Otherwise we use the declared type or punt to the most general
109 ;;; type we know to be correct for sure.
110 (defun lvar-conservative-type (lvar)
111 (let ((derived-type (lvar-type lvar
))
112 (t-type *universal-type
*))
113 ;; Recompute using NODE-CONSERVATIVE-TYPE instead of derived type if
114 ;; necessary -- picking off some easy cases up front.
115 (cond ((or (eq derived-type t-type
)
116 ;; Can't use CSUBTYPEP!
117 (type= derived-type
(specifier-type 'list
))
118 (type= derived-type
(specifier-type 'null
)))
120 ((and (cons-type-p derived-type
)
121 (eq t-type
(cons-type-car-type derived-type
))
122 (eq t-type
(cons-type-cdr-type derived-type
)))
124 ((and (array-type-p derived-type
)
125 (or (not (array-type-complexp derived-type
))
126 (let ((dimensions (array-type-dimensions derived-type
)))
127 (or (eq '* dimensions
)
128 (every (lambda (dim) (eq '* dim
)) dimensions
)))))
130 ((type-needs-conservation-p derived-type
)
131 (single-value-type (lvar-type-using lvar node-conservative-type
)))
135 (defun node-conservative-type (node)
136 (let* ((derived-values-type (node-derived-type node
))
137 (derived-type (single-value-type derived-values-type
)))
139 (let ((leaf (ref-leaf node
)))
140 (if (and (basic-var-p leaf
)
141 (cdr (leaf-refs leaf
)))
143 (if (eq :declared
(leaf-where-from leaf
))
145 (conservative-type derived-type
)))
146 derived-values-type
))
147 derived-values-type
)))
149 (defun conservative-type (type)
150 (cond ((or (eq type
*universal-type
*)
151 (eq type
(specifier-type 'list
))
152 (eq type
(specifier-type 'null
)))
155 (specifier-type 'cons
))
157 (if (array-type-complexp type
)
159 ;; ADJUST-ARRAY may change dimensions, but rank stays same.
160 (let ((old (array-type-dimensions type
)))
163 (mapcar (constantly '*) old
)))
164 ;; Complexity cannot change.
165 :complexp
(array-type-complexp type
)
166 ;; Element type cannot change.
167 :element-type
(array-type-element-type type
)
168 :specialized-element-type
(array-type-specialized-element-type type
))
169 ;; Simple arrays cannot change at all.
172 ;; Conservative union type is an union of conservative types.
173 (let ((res *empty-type
*))
174 (dolist (part (union-type-types type
) res
)
175 (setf res
(type-union res
(conservative-type part
))))))
179 ;; If the type contains some CONS types, the conservative type contains all
181 (when (types-equal-or-intersect type
(specifier-type 'cons
))
182 (setf type
(type-union type
(specifier-type 'cons
))))
183 ;; Similarly for non-simple arrays -- it should be possible to preserve
184 ;; more information here, but really...
185 (let ((non-simple-arrays (specifier-type '(and array
(not simple-array
)))))
186 (when (types-equal-or-intersect type non-simple-arrays
)
187 (setf type
(type-union type non-simple-arrays
))))
190 (defun type-needs-conservation-p (type)
191 (cond ((eq type
*universal-type
*)
192 ;; Excluding T is necessary, because we do want type derivation to
193 ;; be able to narrow it down in case someone (most like a macro-expansion...)
194 ;; actually declares something as having type T.
196 ((or (cons-type-p type
) (and (array-type-p type
) (array-type-complexp type
)))
197 ;; Covered by the next case as well, but this is a quick test.
199 ((types-equal-or-intersect type
(specifier-type '(or cons
(and array
(not simple-array
)))))
202 ;;; If LVAR is an argument of a function, return a type which the
203 ;;; function checks LVAR for.
204 #!-sb-fluid
(declaim (inline lvar-externally-checkable-type
))
205 (defun lvar-externally-checkable-type (lvar)
206 (or (lvar-%externally-checkable-type lvar
)
207 (%lvar-%externally-checkable-type lvar
)))
208 (defun %lvar-%externally-checkable-type
(lvar)
209 (declare (type lvar lvar
))
210 (let ((dest (lvar-dest lvar
)))
211 (if (not (and dest
(combination-p dest
)))
212 ;; TODO: MV-COMBINATION
213 (setf (lvar-%externally-checkable-type lvar
) *wild-type
*)
214 (let* ((fun (combination-fun dest
))
215 (args (combination-args dest
))
216 (fun-type (lvar-type fun
)))
217 (setf (lvar-%externally-checkable-type fun
) *wild-type
*)
218 (if (or (not (call-full-like-p dest
))
219 (not (fun-type-p fun-type
))
220 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
221 (fun-type-wild-args fun-type
))
224 (setf (lvar-%externally-checkable-type arg
)
226 (map-combination-args-and-types
228 (setf (lvar-%externally-checkable-type arg
)
229 (acond ((lvar-%externally-checkable-type arg
)
230 (values-type-intersection
231 it
(coerce-to-values type
)))
232 (t (coerce-to-values type
)))))
234 (or (lvar-%externally-checkable-type lvar
) *wild-type
*))
236 ;;;; interface routines used by optimizers
238 ;;; This function is called by optimizers to indicate that something
239 ;;; interesting has happened to the value of LVAR. Optimizers must
240 ;;; make sure that they don't call for reoptimization when nothing has
241 ;;; happened, since optimization will fail to terminate.
243 ;;; We clear any cached type for the lvar and set the reoptimize flags
244 ;;; on everything in sight.
245 (defun reoptimize-lvar (lvar)
246 (declare (type (or lvar null
) lvar
))
248 (setf (lvar-%derived-type lvar
) nil
)
249 (let ((dest (lvar-dest lvar
)))
251 (setf (lvar-reoptimize lvar
) t
)
252 (setf (node-reoptimize dest
) t
)
253 (binding* (;; Since this may be called during IR1 conversion,
254 ;; PREV may be missing.
255 (prev (node-prev dest
) :exit-if-null
)
256 (block (ctran-block prev
))
257 (component (block-component block
)))
258 (when (typep dest
'cif
)
259 (setf (block-test-modified block
) t
))
260 (setf (block-reoptimize block
) t
)
261 (reoptimize-component component
:maybe
))))
263 (setf (block-type-check (node-block node
)) t
)))
266 (defun reoptimize-lvar-uses (lvar)
267 (declare (type lvar lvar
))
269 (setf (node-reoptimize use
) t
)
270 (setf (block-reoptimize (node-block use
)) t
)
271 (reoptimize-component (node-component use
) :maybe
)))
273 ;;; Annotate NODE to indicate that its result has been proven to be
274 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
275 ;;; only correct way to supply information discovered about a node's
276 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
277 ;;; information may be lost and reoptimization may not happen.
279 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
280 ;;; intersection is different from the old type, then we do a
281 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
282 (defun derive-node-type (node rtype
&key from-scratch
)
283 (declare (type valued-node node
) (type ctype rtype
))
284 (let* ((initial-type (node-derived-type node
))
285 (node-type (if from-scratch
288 (unless (eq initial-type rtype
)
289 (let ((int (values-type-intersection node-type rtype
))
290 (lvar (node-lvar node
)))
291 (when (type/= initial-type int
)
292 (when (and *check-consistency
*
293 (eq int
*empty-type
*)
294 (not (eq rtype
*empty-type
*)))
295 (aver (not from-scratch
))
296 (let ((*compiler-error-context
* node
))
298 "New inferred type ~S conflicts with old type:~
299 ~% ~S~%*** possible internal error? Please report this."
300 (type-specifier rtype
) (type-specifier node-type
))))
301 (setf (node-derived-type node
) int
)
302 ;; If the new type consists of only one object, replace the
303 ;; node with a constant reference.
304 (when (and (ref-p node
)
305 (lambda-var-p (ref-leaf node
)))
306 (let ((type (single-value-type int
)))
307 (when (and (member-type-p type
)
308 (eql 1 (member-type-size type
)))
309 (change-ref-leaf node
(find-constant
310 (first (member-type-members type
)))))))
311 (reoptimize-lvar lvar
)))))
314 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
315 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
316 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
317 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
318 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
319 (defun assert-lvar-type (lvar type policy
&optional context
)
320 (declare (type lvar lvar
) (type ctype type
))
321 (unless (values-subtypep (lvar-derived-type lvar
) type
)
322 (let ((internal-lvar (make-lvar))
323 (dest (lvar-dest lvar
)))
324 (substitute-lvar internal-lvar lvar
)
325 (let ((cast (insert-cast-before dest lvar type policy
327 (use-lvar cast internal-lvar
)
330 (defun insert-function-designator-cast-before (next lvar type arg-count caller policy
)
331 (declare (type node next
) (type lvar lvar
) (type ctype type
))
332 (with-ir1-environment-from-node next
333 (let* ((ctran (node-prev next
))
334 (cast (make-function-designator-cast :asserted-type type
335 :type-to-check
(maybe-weaken-check type policy
)
337 :derived-type
(coerce-to-values type
)
340 (internal-ctran (make-ctran)))
341 (setf (ctran-next ctran
) cast
342 (node-prev cast
) ctran
)
343 (use-ctran cast internal-ctran
)
344 (link-node-to-previous-ctran next internal-ctran
)
345 (setf (lvar-dest lvar
) cast
)
346 (reoptimize-lvar lvar
)
347 (when (return-p next
)
348 (node-ends-block cast
))
349 (setf (block-attributep (block-flags (node-block cast
))
350 type-check type-asserted
)
354 (defun assert-function-designator-lvar-type (lvar type arg-count caller policy
)
355 (declare (type lvar lvar
) (type ctype type
))
356 (let ((internal-lvar (make-lvar))
357 (dest (lvar-dest lvar
)))
358 (substitute-lvar internal-lvar lvar
)
359 (let ((cast (insert-function-designator-cast-before dest lvar type arg-count
361 (use-lvar cast internal-lvar
))))
366 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
367 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
368 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
369 ;;; we are done, then another iteration would be beneficial.
370 (defun ir1-optimize (component fastp
)
371 (declare (type component component
))
372 (setf (component-reoptimize component
) nil
)
373 (loop with block
= (block-next (component-head component
))
374 with tail
= (component-tail component
)
375 for last-block
= block
376 until
(eq block tail
)
378 ;; We delete blocks when there is either no predecessor or the
379 ;; block is in a lambda that has been deleted. These blocks
380 ;; would eventually be deleted by DFO recomputation, but doing
381 ;; it here immediately makes the effect available to IR1
383 ((or (block-delete-p block
)
384 (null (block-pred block
)))
385 (delete-block-lazily block
)
386 (setq block
(clean-component component block
)))
387 ((eq (functional-kind (block-home-lambda block
)) :deleted
)
388 ;; Preserve the BLOCK-SUCC invariant that almost every block has
389 ;; one successor (and a block with DELETE-P set is an acceptable
391 (mark-for-deletion block
)
392 (setq block
(clean-component component block
)))
395 (let ((succ (block-succ block
)))
396 (unless (singleton-p succ
)
399 (let ((last (block-last block
)))
402 (flush-dest (if-test last
))
403 (when (unlink-node last
)
406 (when (maybe-delete-exit last
)
409 (unless (join-successor-if-possible block
)
412 (when (and (not fastp
) (block-reoptimize block
) (block-component block
))
413 (aver (not (block-delete-p block
)))
414 (ir1-optimize-block block
))
416 (cond ((and (block-delete-p block
) (block-component block
))
417 (setq block
(clean-component component block
)))
418 ((and (block-flush-p block
) (block-component block
))
419 (flush-dead-code block
)))))
420 do
(when (eq block last-block
)
421 (setq block
(block-next block
))))
425 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
428 ;;; Note that although they are cleared here, REOPTIMIZE flags might
429 ;;; still be set upon return from this function, meaning that further
430 ;;; optimization is wanted (as a consequence of optimizations we did).
431 (defun ir1-optimize-block (block)
432 (declare (type cblock block
))
433 ;; We clear the node and block REOPTIMIZE flags before doing the
434 ;; optimization, not after. This ensures that the node or block will
435 ;; be reoptimized if necessary.
436 (setf (block-reoptimize block
) nil
)
437 (do-nodes (node nil block
:restart-p t
)
438 (when (node-reoptimize node
)
439 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
440 (setf (node-reoptimize node
) nil
)
444 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
445 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
446 ;; any argument changes.
447 (ir1-optimize-combination node
))
449 (ir1-optimize-if node
))
451 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
452 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
453 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
455 (setf (node-reoptimize node
) t
)
456 (ir1-optimize-return node
))
458 (ir1-optimize-mv-combination node
))
460 ;; With an EXIT, we derive the node's type from the VALUE's
462 (let ((value (exit-value node
)))
464 (derive-node-type node
(lvar-derived-type value
)))))
466 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
467 ;; is accurate till the node actually has been reoptimized.
468 (setf (node-reoptimize node
) t
)
469 (ir1-optimize-set node
))
471 (ir1-optimize-cast node
)))))
475 ;;; Try to join with a successor block. If we succeed, we return true,
477 (defun join-successor-if-possible (block)
478 (declare (type cblock block
))
479 (let ((next (first (block-succ block
))))
480 (when (block-start next
) ; NEXT is not an END-OF-COMPONENT marker
481 (cond ( ;; We cannot combine with a successor block if:
483 ;; the successor has more than one predecessor;
484 (rest (block-pred next
))
485 ;; the successor is the current block (infinite loop);
487 ;; the next block has a different cleanup, and thus
488 ;; we may want to insert cleanup code between the
489 ;; two blocks at some point;
490 (not (eq (block-end-cleanup block
)
491 (block-start-cleanup next
)))
492 ;; the next block has a different home lambda, and
493 ;; thus the control transfer is a non-local exit.
494 (not (eq (block-home-lambda block
)
495 (block-home-lambda next
)))
496 ;; Stack analysis phase wants ENTRY to start a block...
497 (entry-p (block-start-node next
))
498 (let ((last (block-last block
)))
499 (and (valued-node-p last
)
500 (awhen (node-lvar last
)
502 ;; ... and a DX-allocator to end a block.
503 (lvar-dynamic-extent it
)
504 ;; ... and for there to be no chance of there
505 ;; being two successive USEs of the same
506 ;; multi-valued LVAR in the same block (since
507 ;; we can only insert cleanup code at block
508 ;; boundaries, but need to discard
509 ;; multi-valued LVAR contents before they are
511 (and (consp (lvar-uses it
))
512 (not (lvar-single-value-p it
))))))))
515 (join-blocks block next
)
518 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
519 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
520 ;;; for the two blocks so that any indicated optimization gets done.
521 (defun join-blocks (block1 block2
)
522 (declare (type cblock block1 block2
))
523 (let* ((last1 (block-last block1
))
524 (last2 (block-last block2
))
525 (succ (block-succ block2
))
526 (start2 (block-start block2
)))
527 (do ((ctran start2
(node-next (ctran-next ctran
))))
529 (setf (ctran-block ctran
) block1
))
531 (unlink-blocks block1 block2
)
533 (unlink-blocks block2 block
)
534 (link-blocks block1 block
))
536 (setf (ctran-kind start2
) :inside-block
)
537 (setf (node-next last1
) start2
)
538 (setf (ctran-use start2
) last1
)
539 (setf (block-last block1
) last2
))
541 (setf (block-flags block1
)
542 (attributes-union (block-flags block1
)
544 (block-attributes type-asserted test-modified
)))
546 (let ((next (block-next block2
))
547 (prev (block-prev block2
)))
548 (setf (block-next prev
) next
)
549 (setf (block-prev next
) prev
))
553 ;;; Delete any nodes in BLOCK whose value is unused and which have no
554 ;;; side effects. We can delete sets of lexical variables when the set
555 ;;; variable has no references.
556 (defun flush-dead-code (block &aux victim
)
557 (declare (type cblock block
))
558 (setf (block-flush-p block
) nil
)
559 (do-nodes-backwards (node lvar block
:restart-p t
)
567 (when (flushable-combination-p node
)
569 (flush-combination node
)))
571 (when (eq (basic-combination-kind node
) :local
)
572 (let ((fun (combination-lambda node
)))
573 (when (dolist (var (lambda-vars fun
) t
)
574 (when (or (leaf-refs var
)
575 (lambda-var-sets var
))
578 (mapc #'flush-dest
(basic-combination-args node
))
581 (let ((value (exit-value node
)))
585 (setf (exit-value node
) nil
))))
587 (let ((var (set-var node
)))
588 (when (and (lambda-var-p var
)
589 (null (leaf-refs var
)))
591 (flush-dest (set-value node
))
592 (setf (basic-var-sets var
)
593 (delq node
(basic-var-sets var
)))
594 (unlink-node node
))))
596 (unless (cast-type-check node
)
598 (flush-dest (cast-value node
))
599 (unlink-node node
))))))
603 ;;;; local call return type propagation
605 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
606 ;;; flag set. It iterates over the uses of the RESULT, looking for
607 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
608 ;;; call, then we union its type together with the types of other such
609 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
610 ;;; type with the RESULT's asserted type. We can make this
611 ;;; intersection now (potentially before type checking) because this
612 ;;; assertion on the result will eventually be checked (if
615 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
616 ;;; combination, which may change the successor of the call to be the
617 ;;; called function, and if so, checks if the call can become an
618 ;;; assignment. If we convert to an assignment, we abort, since the
619 ;;; RETURN has been deleted.
620 (defun find-result-type (node)
621 (declare (type creturn node
))
622 (let ((result (return-result node
)))
623 (collect ((use-union *empty-type
* values-type-union
))
624 (do-uses (use result
)
625 (let ((use-home (node-home-lambda use
)))
626 (cond ((or (eq (functional-kind use-home
) :deleted
)
627 (block-delete-p (node-block use
))))
628 ((and (basic-combination-p use
)
629 (eq (basic-combination-kind use
) :local
))
630 (aver (eq (lambda-tail-set use-home
)
631 (lambda-tail-set (combination-lambda use
))))
632 (when (combination-p use
)
633 (when (nth-value 1 (maybe-convert-tail-local-call use
))
634 (return-from find-result-type t
))))
636 (use-union (node-derived-type use
))))))
638 ;; (values-type-intersection
639 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
643 (setf (return-result-type node
) int
))))
646 ;;; Do stuff to realize that something has changed about the value
647 ;;; delivered to a return node. Since we consider the return values of
648 ;;; all functions in the tail set to be equivalent, this amounts to
649 ;;; bringing the entire tail set up to date. We iterate over the
650 ;;; returns for all the functions in the tail set, reanalyzing them
651 ;;; all (not treating NODE specially.)
653 ;;; When we are done, we check whether the new type is different from
654 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
655 ;;; all the lvars for references to functions in the tail set. This
656 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
657 ;;; results of the calls.
658 (defun ir1-optimize-return (node)
659 (declare (type creturn node
))
662 (let* ((tails (lambda-tail-set (return-lambda node
)))
663 (funs (tail-set-funs tails
)))
664 (collect ((res *empty-type
* values-type-union
))
666 (let ((return (lambda-return fun
)))
668 (when (node-reoptimize return
)
669 (setf (node-reoptimize return
) nil
)
670 (when (find-result-type return
)
672 (res (return-result-type return
)))))
674 (when (type/= (res) (tail-set-type tails
))
675 (setf (tail-set-type tails
) (res))
676 (dolist (fun (tail-set-funs tails
))
677 (dolist (ref (leaf-refs fun
))
678 (reoptimize-lvar (node-lvar ref
))))))))
684 ;;; Utility: return T if both argument cblocks are equivalent. For now,
685 ;;; detect only blocks that read the same leaf into the same lvar, and
686 ;;; continue to the same block.
687 (defun cblocks-equivalent-p (x y
)
688 (declare (type cblock x y
))
689 (and (ref-p (block-start-node x
))
690 (eq (block-last x
) (block-start-node x
))
692 (ref-p (block-start-node y
))
693 (eq (block-last y
) (block-start-node y
))
695 (equal (block-succ x
) (block-succ y
))
696 (eql (ref-lvar (block-start-node x
)) (ref-lvar (block-start-node y
)))
697 (eql (ref-leaf (block-start-node x
)) (ref-leaf (block-start-node y
)))))
699 ;;; Check whether the predicate is known to be true or false,
700 ;;; deleting the IF node in favor of the appropriate branch when this
702 ;;; Similarly, when both branches are equivalent, branch directly to either
704 ;;; Also, if the test has multiple uses, replicate the node when possible...
705 ;;; in fact, splice in direct jumps to the right branch if possible.
706 (defun ir1-optimize-if (node)
707 (declare (type cif node
))
708 (let ((test (if-test node
))
709 (block (node-block node
)))
710 (let* ((type (lvar-type test
))
711 (consequent (if-consequent node
))
712 (alternative (if-alternative node
))
714 (cond ((constant-lvar-p test
)
715 (if (lvar-value test
) alternative consequent
))
716 ((not (types-equal-or-intersect type
(specifier-type 'null
)))
718 ((type= type
(specifier-type 'null
))
720 ((or (eq consequent alternative
) ; Can this happen?
721 (cblocks-equivalent-p alternative consequent
))
724 (kill-if-branch-1 node test block victim
)
725 (return-from ir1-optimize-if
(values))))
726 (tension-if-if-1 node test block
)
727 (duplicate-if-if-1 node test block
)
730 ;; When we know that we only have a single successor, kill the victim
731 ;; ... unless the victim and the remaining successor are the same.
732 (defun kill-if-branch-1 (node test block victim
)
733 (declare (type cif node
))
735 (when (rest (block-succ block
))
736 (unlink-blocks block victim
))
737 (setf (component-reanalyze (node-component node
)) t
)
740 ;; When if/if conversion would leave (if ... (if nil ...)) or
741 ;; (if ... (if not-nil ...)), splice the correct successor right
743 (defun tension-if-if-1 (node test block
)
744 (when (and (eq (block-start-node block
) node
)
745 (listp (lvar-uses test
)))
747 (when (immediately-used-p test use
)
748 (let* ((type (single-value-type (node-derived-type use
)))
749 (target (if (type= type
(specifier-type 'null
))
750 (if-alternative node
)
751 (multiple-value-bind (typep surep
)
753 (and (not typep
) surep
754 (if-consequent node
))))))
756 (let ((pred (node-block use
)))
757 (cond ((listp (lvar-uses test
))
758 (change-block-successor pred block target
)
759 (delete-lvar-use use
))
761 ;; only one use left. Just kill the now-useless
762 ;; branch to avoid spurious code deletion notes.
763 (aver (rest (block-succ block
)))
766 (if (eql target
(if-alternative node
))
768 (if-alternative node
)))
769 (return-from tension-if-if-1
))))))))))
771 ;; Finally, duplicate EQ-nil tests
772 (defun duplicate-if-if-1 (node test block
)
773 (when (and (eq (block-start-node block
) node
)
774 (listp (lvar-uses test
)))
776 (when (immediately-used-p test use
)
777 (convert-if-if use node
)
778 ;; leave the last use as is, instead of replacing
779 ;; the (singly-referenced) CIF node with a duplicate.
780 (when (not (listp (lvar-uses test
))) (return))))))
782 ;;; Create a new copy of an IF node that tests the value of the node
783 ;;; USE. The test must have >1 use, and must be immediately used by
784 ;;; USE. NODE must be the only node in its block (implying that
785 ;;; block-start = if-test).
787 ;;; This optimization has an effect semantically similar to the
788 ;;; source-to-source transformation:
789 ;;; (IF (IF A B C) D E) ==>
790 ;;; (IF A (IF B D E) (IF C D E))
792 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
793 ;;; node so that dead code deletion notes will definitely not consider
794 ;;; either node to be part of the original source. One node might
795 ;;; become unreachable, resulting in a spurious note.
796 (defun convert-if-if (use node
)
797 (declare (type node use
) (type cif node
))
798 (with-ir1-environment-from-node node
799 (let* ((block (node-block node
))
800 (test (if-test node
))
801 (cblock (if-consequent node
))
802 (ablock (if-alternative node
))
803 (use-block (node-block use
))
804 (new-ctran (make-ctran))
805 (new-lvar (make-lvar))
806 (new-node (make-if :test new-lvar
808 :alternative ablock
))
809 (new-block (ctran-starts-block new-ctran
)))
810 (link-node-to-previous-ctran new-node new-ctran
)
811 (setf (lvar-dest new-lvar
) new-node
)
812 (setf (block-last new-block
) new-node
)
814 (unlink-blocks use-block block
)
815 (%delete-lvar-use use
)
816 (add-lvar-use use new-lvar
)
817 (link-blocks use-block new-block
)
819 (link-blocks new-block cblock
)
820 (link-blocks new-block ablock
)
822 (push "<IF Duplication>" (node-source-path node
))
823 (push "<IF Duplication>" (node-source-path new-node
))
825 (reoptimize-lvar test
)
826 (reoptimize-lvar new-lvar
)
827 (setf (component-reanalyze *current-component
*) t
)))
830 ;;;; exit IR1 optimization
832 ;;; This function attempts to delete an exit node, returning true if
833 ;;; it deletes the block as a consequence:
834 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
835 ;;; anything, since there is nothing to be done.
836 ;;; -- If the exit node and its ENTRY have the same home lambda then
837 ;;; we know the exit is local, and can delete the exit. We change
838 ;;; uses of the Exit-Value to be uses of the original lvar,
839 ;;; then unlink the node. If the exit is to a TR context, then we
840 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
841 ;;; their value to this exit.
842 ;;; -- If there is no value (as in a GO), then we skip the value
845 ;;; This function is also called by environment analysis, since it
846 ;;; wants all exits to be optimized even if normal optimization was
848 (defun maybe-delete-exit (node)
849 (declare (type exit node
))
850 (let ((value (exit-value node
))
851 (entry (exit-entry node
)))
853 (eq (node-home-lambda node
) (node-home-lambda entry
)))
854 (setf (entry-exits entry
) (delq node
(entry-exits entry
)))
856 (with-ir1-environment-from-node entry
857 ;; We can't simply use DELETE-FILTER to unlink the node
858 ;; and substitute some LVAR magic, as this can confuse the
859 ;; stack analysis if there's another EXIT to the same
860 ;; continuation. Instead, we fabricate a new block (in
861 ;; the same lexenv as the ENTRY, so it can't be merged
862 ;; backwards), insert a gimmicked CAST node to link up the
863 ;; LVAR holding the value being returned to the LVAR which
864 ;; is expecting to accept the value, thus placing the
865 ;; return value where it needs to be while still providing
866 ;; the hook required for stack analysis.
867 ;; -- AJB, 2014-Mar-03
868 (let* ((exit-block (node-block node
))
869 (new-ctran (make-ctran))
870 (new-block (ctran-starts-block new-ctran
))
873 :asserted-type
*wild-type
*
874 :type-to-check
*wild-type
*
876 :vestigial-exit-lexenv
(node-lexenv node
)
877 :vestigial-exit-entry-lexenv
(node-lexenv entry
)
879 ;; We only expect a single successor to EXIT-BLOCK,
880 ;; because it contains an EXIT node (which must end its
881 ;; block) and the only blocks that have more than once
882 ;; successor are those with IF nodes (which also must
883 ;; end their blocks). Still, just to be sure, we use a
884 ;; construct that guarantees an error if this
885 ;; expectation is violated.
888 (block-succ exit-block
)
890 ;; Finish creating the new block.
891 (link-node-to-previous-ctran cast-node new-ctran
)
892 (setf (block-last new-block
) cast-node
)
894 ;; Link the new block into the control sequence.
895 (unlink-blocks exit-block entry-block
)
896 (link-blocks exit-block new-block
)
897 (link-blocks new-block entry-block
)
899 ;; Finish re-pointing the value-holding LVAR to the
901 (setf (lvar-dest value
) cast-node
)
902 (setf (exit-value node
) nil
)
903 (reoptimize-lvar value
)
905 ;; Register the CAST node as providing a value to the
906 ;; LVAR for the continuation.
907 (add-lvar-use cast-node
(node-lvar node
))
908 (reoptimize-lvar (node-lvar node
))
910 ;; Remove the EXIT node.
913 ;; And, because we created a new block, we need to
914 ;; force component reanalysis (to assign a DFO number
915 ;; to the block if nothing else).
916 (setf (component-reanalyze *current-component
*) t
))))
917 (unlink-node node
)))))
920 ;;;; combination IR1 optimization
922 ;;; Report as we try each transform?
924 (defvar *show-transforms-p
* nil
)
926 (defun check-important-result (node info
)
927 (when (and (null (node-lvar node
))
928 (ir1-attributep (fun-info-attributes info
) important-result
))
929 (let ((*compiler-error-context
* node
))
931 "The return value of ~A should not be discarded."
932 (lvar-fun-name (basic-combination-fun node
) t
)))))
934 ;;; Do IR1 optimizations on a COMBINATION node.
935 (declaim (ftype (function (combination) (values)) ir1-optimize-combination
))
936 (defun ir1-optimize-combination (node)
937 (when (lvar-reoptimize (basic-combination-fun node
))
938 (propagate-fun-change node
)
939 (maybe-terminate-block node nil
))
940 (let ((args (basic-combination-args node
))
941 (kind (basic-combination-kind node
))
942 (info (basic-combination-fun-info node
)))
945 (let ((fun (combination-lambda node
)))
946 (if (eq (functional-kind fun
) :let
)
947 (propagate-let-args node fun
)
948 (propagate-local-call-args node fun
))))
952 (setf (lvar-reoptimize arg
) nil
))))
956 (setf (lvar-reoptimize arg
) nil
)))
958 (check-important-result node info
)
959 (let ((fun (fun-info-destroyed-constant-args info
)))
960 (when (and fun
(funcall fun args
))
961 (let ((*compiler-error-context
* node
))
962 (warn 'constant-modified
963 :fun-name
(lvar-fun-name
964 (basic-combination-fun node
) t
))
965 (setf (basic-combination-kind node
) :error
)
966 (return-from ir1-optimize-combination
))))
967 (let ((fun (fun-info-derive-type info
)))
969 (let ((res (funcall fun node
)))
971 (derive-node-type node
(coerce-to-values res
))
972 (maybe-terminate-block node nil
))))))
974 ;; Check against the DEFINED-TYPE unless TYPE is already good.
975 (let* ((fun (basic-combination-fun node
))
976 (uses (lvar-uses fun
))
977 (leaf (when (ref-p uses
) (ref-leaf uses
))))
978 (multiple-value-bind (type defined-type
)
979 (if (global-var-p leaf
)
980 (values (leaf-type leaf
) (leaf-defined-type leaf
))
982 (when (and (not (fun-type-p type
)) (fun-type-p defined-type
))
983 (validate-call-type node type leaf
)))))))
988 (setf (lvar-reoptimize arg
) nil
)))
989 (check-important-result node info
)
990 (let ((fun (fun-info-destroyed-constant-args info
)))
992 ;; If somebody is really sure that they want to modify
993 ;; constants, let them.
994 (policy node
(> check-constant-modification
0)))
995 (let ((destroyed-constant-args (funcall fun args
)))
996 (when destroyed-constant-args
997 (let ((*compiler-error-context
* node
))
998 (warn 'constant-modified
999 :fun-name
(lvar-fun-name
1000 (basic-combination-fun node
)))
1001 (setf (basic-combination-kind node
) :error
)
1002 (return-from ir1-optimize-combination
))))))
1004 (let ((attr (fun-info-attributes info
)))
1005 (when (constant-fold-call-p node
)
1006 (constant-fold-call node
)
1007 (return-from ir1-optimize-combination
))
1008 (when (and (ir1-attributep attr commutative
)
1010 (constant-lvar-p (first args
))
1011 (not (constant-lvar-p (second args
))))
1012 (setf (basic-combination-args node
) (nreverse args
))))
1013 (let ((fun (fun-info-derive-type info
)))
1015 (let ((res (funcall fun node
)))
1017 (derive-node-type node
(coerce-to-values res
))
1018 (maybe-terminate-block node nil
)))))
1020 (let ((fun (fun-info-optimizer info
)))
1021 (unless (and fun
(funcall fun node
))
1022 ;; First give the VM a peek at the call
1023 (multiple-value-bind (style transform
)
1024 (combination-implementation-style node
)
1027 ;; The VM knows how to handle this.
1030 ;; The VM mostly knows how to handle this. We need
1031 ;; to massage the call slightly, though.
1032 (transform-call node transform
(combination-fun-source-name node
)))
1034 ;; Let transforms have a crack at it.
1035 (dolist (x (fun-info-transforms info
))
1037 (when *show-transforms-p
*
1038 (let* ((lvar (basic-combination-fun node
))
1039 (fname (lvar-fun-name lvar t
)))
1040 (/show
"trying transform" x
(transform-function x
) "for" fname
)))
1041 (unless (ir1-transform node x
)
1043 (when *show-transforms-p
*
1044 (/show
"quitting because IR1-TRANSFORM result was NIL"))
1049 (defun xep-tail-combination-p (node)
1050 (and (combination-p node
)
1051 (let* ((lvar (combination-lvar node
))
1052 (dest (when (lvar-p lvar
) (lvar-dest lvar
)))
1053 (lambda (when (return-p dest
) (return-lambda dest
))))
1054 (and (lambda-p lambda
)
1055 (eq :external
(lambda-kind lambda
))))))
1057 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
1058 ;;; the block there, and link it to the component tail.
1060 ;;; Except when called during IR1 convertion, we delete the
1061 ;;; continuation if it has no other uses. (If it does have other uses,
1064 ;;; Termination on the basis of a continuation type is
1066 ;;; -- The continuation is deleted (hence the assertion is spurious), or
1067 ;;; -- We are in IR1 conversion (where THE assertions are subject to
1068 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
1069 ;;; uses can(?) be added later. -- APD, 2003-07-17
1071 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
1072 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p
)
1073 (declare (type (or basic-combination cast ref
) node
))
1074 (let* ((block (node-block node
))
1075 (lvar (node-lvar node
))
1076 (ctran (node-next node
))
1077 (tail (component-tail (block-component block
)))
1078 (succ (first (block-succ block
))))
1079 (declare (ignore lvar
))
1080 (unless (or (and (eq node
(block-last block
)) (eq succ tail
))
1081 (block-delete-p block
))
1082 ;; Even if the combination will never return, don't terminate if this
1083 ;; is the tail call of a XEP: doing that would inhibit TCO.
1084 (when (and (eq (node-derived-type node
) *empty-type
*)
1085 (not (xep-tail-combination-p node
)))
1086 (cond (ir1-converting-not-optimizing-p
1089 (aver (eq (block-last block
) node
)))
1091 (setf (block-last block
) node
)
1092 (setf (ctran-use ctran
) nil
)
1093 (setf (ctran-kind ctran
) :unused
)
1094 (setf (ctran-block ctran
) nil
)
1095 (setf (node-next node
) nil
)
1096 (link-blocks block
(ctran-starts-block ctran
)))))
1098 (node-ends-block node
)))
1100 (let ((succ (first (block-succ block
))))
1101 (unlink-blocks block succ
)
1102 (setf (component-reanalyze (block-component block
)) t
)
1103 (aver (not (block-succ block
)))
1104 (link-blocks block tail
)
1105 (cond (ir1-converting-not-optimizing-p
1106 (%delete-lvar-use node
))
1107 (t (delete-lvar-use node
)
1108 (when (null (block-pred succ
))
1109 (mark-for-deletion succ
)))))
1112 ;;; This is called both by IR1 conversion and IR1 optimization when
1113 ;;; they have verified the type signature for the call, and are
1114 ;;; wondering if something should be done to special-case the call. If
1115 ;;; CALL is a call to a global function, then see whether it defined
1117 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
1118 ;;; the expansion and change the call to call it. Expansion is
1119 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
1120 ;;; true, we never expand, since this function has already been
1121 ;;; converted. Local call analysis will duplicate the definition
1122 ;;; if necessary. We claim that the parent form is LABELS for
1123 ;;; context declarations, since we don't want it to be considered
1124 ;;; a real global function.
1125 ;;; -- If it is a known function, mark it as such by setting the KIND.
1127 ;;; We return the leaf referenced (NIL if not a leaf) and the
1128 ;;; FUN-INFO assigned.
1129 (defun recognize-known-call (call ir1-converting-not-optimizing-p
)
1130 (declare (type combination call
))
1131 (let* ((ref (lvar-uses (basic-combination-fun call
)))
1132 (leaf (when (ref-p ref
) (ref-leaf ref
)))
1133 (inlinep (if (defined-fun-p leaf
)
1134 (defined-fun-inlinep leaf
)
1137 ((eq inlinep
:notinline
)
1138 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1140 (setf (basic-combination-fun-info call
) info
))
1142 ((not (and (global-var-p leaf
)
1143 (eq (global-var-kind leaf
) :global-function
)))
1145 ((and (ecase inlinep
1148 ((nil :maybe-inline
) (policy call
(zerop space
))))
1149 (defined-fun-p leaf
)
1150 (defined-fun-inline-expansion leaf
)
1151 (inline-expansion-ok call
))
1152 ;; Inline: if the function has already been converted at another call
1153 ;; site in this component, we point this REF to the functional. If not,
1154 ;; we convert the expansion.
1156 ;; For :INLINE case local call analysis will copy the expansion later,
1157 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1158 ;; expansion per component.
1160 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1162 (with-ir1-environment-from-node call
1163 (let ((fun (defined-fun-functional leaf
)))
1165 (and (eq inlinep
:inline
) (functional-kind fun
)))
1167 (let* ((name (leaf-source-name leaf
))
1168 (res (ir1-convert-inline-expansion
1170 (defined-fun-inline-expansion leaf
)
1173 (info :function
:info name
))))
1174 ;; Allow backward references to this function from following
1175 ;; forms. (Reused only if policy matches.)
1176 (push res
(defined-fun-functionals leaf
))
1177 (change-ref-leaf ref res
)
1178 (unless ir1-converting-not-optimizing-p
1179 (locall-analyze-component *current-component
*)))
1180 ;; If we've already converted, change ref to the converted
1182 (change-ref-leaf ref fun
))))
1183 (values (ref-leaf ref
) nil
))
1185 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1189 (setf (basic-combination-kind call
) :known
)
1190 (setf (basic-combination-fun-info call
) info
)))
1191 (values leaf nil
)))))))
1193 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1194 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1195 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1196 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1197 ;;; syntax check, arg/result type processing, but still call
1198 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1199 ;;; and that checking is done by local call analysis.
1200 (defun validate-call-type (call type fun
&optional ir1-converting-not-optimizing-p
)
1201 (declare (type combination call
) (type ctype type
))
1202 (let* ((where (when fun
(leaf-where-from fun
)))
1203 (same-file-p (eq :defined-here where
)))
1204 (cond ((not (fun-type-p type
))
1205 (aver (multiple-value-bind (val win
)
1206 (csubtypep type
(specifier-type 'function
))
1207 (or val
(not win
))))
1208 ;; Using the defined-type too early is a bit of a waste: during
1209 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1210 (when (and fun
(not ir1-converting-not-optimizing-p
))
1211 (let ((defined-type (leaf-defined-type fun
)))
1212 (when (and (fun-type-p defined-type
)
1213 (neq fun
(combination-type-validated-for-leaf call
)))
1214 ;; Don't validate multiple times against the same leaf --
1215 ;; it doesn't add any information, but may generate the same warning
1217 (setf (combination-type-validated-for-leaf call
) fun
)
1218 (when (and (valid-fun-use call defined-type
1219 :argument-test
#'always-subtypep
1221 :lossage-fun
(if same-file-p
1223 #'compiler-style-warn
)
1224 :unwinnage-fun
#'compiler-notify
)
1226 (assert-call-type call defined-type nil
)
1227 (maybe-terminate-block call ir1-converting-not-optimizing-p
)))))
1228 (recognize-known-call call ir1-converting-not-optimizing-p
))
1229 ((let ((*valid-callable-argument-assert-unknown-lvars
* t
))
1230 (valid-fun-use call type
1231 :argument-test
#'always-subtypep
1233 :lossage-fun
#'compiler-warn
1234 :unwinnage-fun
#'compiler-notify
))
1235 (assert-call-type call type
)
1236 (maybe-terminate-block call ir1-converting-not-optimizing-p
)
1237 (recognize-known-call call ir1-converting-not-optimizing-p
))
1239 (setf (combination-kind call
) :error
)
1240 (values nil nil
)))))
1242 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1243 ;;; changed. If the call is local, we try to LET-convert it, and
1244 ;;; derive the result type. If it is a :FULL call, we validate it
1245 ;;; against the type, which recognizes known calls, does inline
1246 ;;; expansion, etc. If a call to a predicate in a non-conditional
1247 ;;; position or to a function with a source transform, then we
1248 ;;; reconvert the form to give IR1 another chance.
1249 (defun propagate-fun-change (call)
1250 (declare (type combination call
))
1251 (let ((*compiler-error-context
* call
)
1252 (fun-lvar (basic-combination-fun call
)))
1253 (setf (lvar-reoptimize fun-lvar
) nil
)
1254 (case (combination-kind call
)
1256 (let ((fun (combination-lambda call
)))
1257 (maybe-let-convert fun
)
1258 (unless (member (functional-kind fun
) '(:let
:assignment
:deleted
))
1259 (derive-node-type call
(tail-set-type (lambda-tail-set fun
))))))
1261 (multiple-value-bind (leaf info
)
1262 (let* ((uses (lvar-uses fun-lvar
))
1263 (leaf (when (ref-p uses
) (ref-leaf uses
))))
1264 (validate-call-type call
(lvar-type fun-lvar
) leaf
))
1265 (cond ((functional-p leaf
)
1266 (convert-call-if-possible
1267 (lvar-uses (basic-combination-fun call
))
1270 ((and (global-var-p leaf
)
1271 (eq (global-var-kind leaf
) :global-function
)
1272 (leaf-has-source-name-p leaf
)
1273 (or (info :function
:source-transform
(leaf-source-name leaf
))
1275 (ir1-attributep (fun-info-attributes info
)
1277 (let ((lvar (node-lvar call
)))
1278 (and lvar
(not (if-p (lvar-dest lvar
))))))))
1279 (let ((name (leaf-source-name leaf
))
1280 (dummies (make-gensym-list
1281 (length (combination-args call
)))))
1282 (transform-call call
1284 (,@(if (symbolp name
)
1288 (leaf-source-name leaf
)))))))))
1291 ;;;; known function optimization
1293 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1294 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1295 ;;; replace it, otherwise add a new one.
1296 (defun record-optimization-failure (node transform args
)
1297 (declare (type combination node
) (type transform transform
)
1298 (type (or fun-type list
) args
))
1299 (let* ((table (component-failed-optimizations *component-being-compiled
*))
1300 (found (assoc transform
(gethash node table
))))
1302 (setf (cdr found
) args
)
1303 (push (cons transform args
) (gethash node table
))))
1306 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1307 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1308 ;;; doing the transform for some reason and FLAME is true, then we
1309 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1310 ;;; finalize to pick up. We return true if the transform failed, and
1311 ;;; thus further transformation should be attempted. We return false
1312 ;;; if either the transform succeeded or was aborted.
1313 (defun ir1-transform (node transform
)
1314 (declare (type combination node
) (type transform transform
))
1315 (declare (notinline warn
)) ; See COMPILER-WARN for rationale
1316 (let* ((type (transform-type transform
))
1317 (fun (transform-function transform
))
1318 (constrained (fun-type-p type
))
1319 (table (component-failed-optimizations *component-being-compiled
*))
1320 (flame (case (transform-important transform
)
1321 ((t) (policy node
(>= speed inhibit-warnings
)))
1322 (:slightly
(policy node
(> speed inhibit-warnings
)))))
1323 (*compiler-error-context
* node
)
1324 (policy-test (transform-policy transform
)))
1325 (cond ((and policy-test
1326 (not (funcall policy-test node
))))
1327 ((or (not constrained
)
1328 (valid-fun-use node type
))
1329 (multiple-value-bind (severity args
)
1330 (catch 'give-up-ir1-transform
1331 (transform-call node
1333 (combination-fun-source-name node
))
1337 (remhash node table
)
1340 (setf (combination-kind node
) :error
)
1342 (apply #'warn args
))
1343 (remhash node table
)
1348 (record-optimization-failure node transform args
))
1349 (setf (gethash node table
)
1350 (remove transform
(gethash node table
) :key
#'car
)))
1353 (remhash node table
)
1358 :argument-test
#'types-equal-or-intersect
1359 :result-test
#'values-types-equal-or-intersect
))
1360 (record-optimization-failure node transform type
)
1365 ;;; When we don't like an IR1 transform, we throw the severity/reason
1368 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1369 ;;; aborting this attempt to transform the call, but admitting the
1370 ;;; possibility that this or some other transform will later succeed.
1371 ;;; If arguments are supplied, they are format arguments for an
1372 ;;; efficiency note.
1374 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1375 ;;; force a normal call to the function at run time. No further
1376 ;;; optimizations will be attempted.
1378 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1379 ;;; delay the transform on the node until later. REASONS specifies
1380 ;;; when the transform will be later retried. The :OPTIMIZE reason
1381 ;;; causes the transform to be delayed until after the current IR1
1382 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1383 ;;; be delayed until after constraint propagation.
1385 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1386 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1387 ;;; do CASE operations on the various REASON values, it might be a
1388 ;;; good idea to go OO, representing the reasons by objects, using
1389 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1390 ;;; SIGNAL instead of THROW.
1391 (declaim (ftype (function (&rest t
) #+(and sb-xc-host ccl
) *
1392 #-
(and sb-xc-host ccl
) nil
) give-up-ir1-transform
))
1393 (defun give-up-ir1-transform (&rest args
)
1394 (throw 'give-up-ir1-transform
(values :failure args
)))
1395 (defun abort-ir1-transform (&rest args
)
1396 (throw 'give-up-ir1-transform
(values :aborted args
)))
1397 (defun delay-ir1-transform (node &rest reasons
)
1398 (let ((assoc (assoc node
*delayed-ir1-transforms
*)))
1400 (setf *delayed-ir1-transforms
*
1401 (acons node reasons
*delayed-ir1-transforms
*))
1402 (throw 'give-up-ir1-transform
:delayed
))
1404 (dolist (reason reasons
)
1405 (pushnew reason
(cdr assoc
)))
1406 (throw 'give-up-ir1-transform
:delayed
)))))
1408 ;;; Poor man's catching and resignalling
1409 ;;; Implicit %GIVE-UP macrolet will resignal the give-up "condition"
1410 (defmacro catch-give-up-ir1-transform
((form &optional args
) &body gave-up-body
)
1411 (let ((block (gensym "BLOCK"))
1412 (kind (gensym "KIND"))
1413 (args (or args
(gensym "ARGS"))))
1415 (multiple-value-bind (,kind
,args
)
1416 (catch 'give-up-ir1-transform
1417 (return-from ,block
,form
))
1420 (throw 'give-up-ir1-transform
:delayed
))
1421 ((:failure
:aborted
)
1422 (macrolet ((%give-up
()
1423 `(throw 'give-up-ir1-transform
(values ,',kind
1425 ,@gave-up-body
)))))))
1427 ;;; Clear any delayed transform with no reasons - these should have
1428 ;;; been tried in the last pass. Then remove the reason from the
1429 ;;; delayed transform reasons, and if any become empty then set
1430 ;;; reoptimize flags for the node. Return true if any transforms are
1432 (defun retry-delayed-ir1-transforms (reason)
1433 (setf *delayed-ir1-transforms
*
1434 (remove-if-not #'cdr
*delayed-ir1-transforms
*))
1435 (let ((reoptimize nil
))
1436 (dolist (assoc *delayed-ir1-transforms
*)
1437 (let ((reasons (remove reason
(cdr assoc
))))
1438 (setf (cdr assoc
) reasons
)
1440 (let ((node (car assoc
)))
1441 (unless (node-deleted node
)
1443 (setf (node-reoptimize node
) t
)
1444 (let ((block (node-block node
)))
1445 (setf (block-reoptimize block
) t
)
1446 (reoptimize-component (block-component block
) :maybe
)))))))
1449 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1450 ;;; environment, and then install it as the function for the call
1451 ;;; NODE. We do local call analysis so that the new function is
1452 ;;; integrated into the control flow.
1454 ;;; We require the original function source name in order to generate
1455 ;;; a meaningful debug name for the lambda we set up. (It'd be
1456 ;;; possible to do this starting from debug names as well as source
1457 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1458 ;;; generality, since source names are always known to our callers.)
1459 (defun transform-call (call res source-name
)
1460 (declare (type combination call
) (list res
))
1461 (aver (and (legal-fun-name-p source-name
)
1462 (not (eql source-name
'.anonymous.
))))
1463 (node-ends-block call
)
1464 (setf (combination-lexenv call
)
1465 (make-lexenv :default
(combination-lexenv call
)
1467 ;; The internal variables of a transform are not going to be
1468 ;; interesting to the debugger, so there's no sense in
1469 ;; suppressing the substitution of variables with only one use
1470 ;; (the extra variables can slow down constraint propagation).
1472 preserve-single-use-debug-variables
1475 (combination-lexenv call
)))))
1476 (with-ir1-environment-from-node call
1477 (with-component-last-block (*current-component
*
1478 (block-next (node-block call
)))
1479 (let ((new-fun (ir1-convert-inline-lambda
1481 :debug-name
(debug-name 'lambda-inlined source-name
)
1483 (ref (lvar-use (combination-fun call
))))
1484 (change-ref-leaf ref new-fun
)
1485 (setf (combination-kind call
) :full
)
1486 (locall-analyze-component *current-component
*))))
1489 (defun constant-fold-arg-p (name)
1494 (let* ((info (info :function
:info name
))
1495 (attributes (and info
1496 (fun-info-attributes info
))))
1498 (ir1-attributep attributes foldable
)
1499 (not (ir1-attributep attributes call
)))))))
1501 ;;; Return T if the function is foldable and if it's marked as CALL
1502 ;;; all function arguments are FOLDABLE too.
1503 (defun constant-fold-call-p (combination)
1504 (let* ((info (basic-combination-fun-info combination
))
1505 (attr (fun-info-attributes info
))
1506 (args (basic-combination-args combination
)))
1507 (cond ((not (ir1-attributep attr foldable
))
1509 ((ir1-attributep attr call
)
1510 (and (every (lambda (lvar)
1511 (or (lvar-fun-name lvar t
)
1512 (constant-lvar-p lvar
)))
1514 (map-callable-arguments
1515 (lambda (lvar &key
&allow-other-keys
)
1516 (constant-fold-arg-p (or (lvar-fun-name lvar t
)
1517 (lvar-value lvar
))))
1520 (every #'constant-lvar-p args
)))))
1522 ;;; Replace a call to a foldable function of constant arguments with
1523 ;;; the result of evaluating the form. If there is an error during the
1524 ;;; evaluation, we give a warning and leave the call alone, making the
1525 ;;; call a :ERROR call.
1527 ;;; If there is more than one value, then we transform the call into a
1529 (defun constant-fold-call (call)
1530 (let ((args (mapcar (lambda (lvar)
1531 (let ((name (lvar-fun-name lvar t
)))
1534 (lvar-value lvar
))))
1535 (combination-args call
)))
1536 (fun-name (combination-fun-source-name call
)))
1537 (multiple-value-bind (values win
)
1538 (careful-call fun-name
1541 ;; Note: CMU CL had COMPILER-WARN here, and that
1542 ;; seems more natural, but it's probably not.
1544 ;; It's especially not while bug 173 exists:
1547 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1549 ;; can cause constant-folding TYPE-ERRORs (in
1550 ;; #'<=) when END can be proved to be NIL, even
1551 ;; though the code is perfectly legal and safe
1552 ;; because a NIL value of END means that the
1553 ;; #'<= will never be executed.
1555 ;; Moreover, even without bug 173,
1556 ;; quite-possibly-valid code like
1557 ;; (COND ((NONINLINED-PREDICATE END)
1558 ;; (UNLESS (<= END SIZE))
1560 ;; (where NONINLINED-PREDICATE is something the
1561 ;; compiler can't do at compile time, but which
1562 ;; turns out to make the #'<= expression
1563 ;; unreachable when END=NIL) could cause errors
1564 ;; when the compiler tries to constant-fold (<=
1567 ;; So, with or without bug 173, it'd be
1568 ;; unnecessarily evil to do a full
1569 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1570 ;; from COMPILE-FILE) for legal code, so we we
1571 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1572 #-sb-xc-host
#'compiler-style-warn
1573 ;; On the other hand, for code we control, we
1574 ;; should be able to work around any bug
1575 ;; 173-related problems, and in particular we
1576 ;; want to be alerted to calls to our own
1577 ;; functions which aren't being folded away; a
1578 ;; COMPILER-WARNING is butch enough to stop the
1579 ;; SBCL build itself in its tracks.
1580 #+sb-xc-host
#'compiler-warn
1583 (setf (combination-kind call
) :error
))
1584 ((and (proper-list-of-length-p values
1))
1585 (with-ir1-environment-from-node call
1586 (let* ((lvar (node-lvar call
))
1587 (prev (node-prev call
))
1588 (intermediate-ctran (make-ctran)))
1589 (%delete-lvar-use call
)
1590 (setf (ctran-next prev
) nil
)
1591 (setf (node-prev call
) nil
)
1592 (reference-constant prev intermediate-ctran lvar
1594 (link-node-to-previous-ctran call intermediate-ctran
)
1595 (reoptimize-lvar lvar
)
1596 (flush-combination call
))))
1597 (t (let ((dummies (make-gensym-list (length args
))))
1601 (declare (ignore ,@dummies
))
1602 (values ,@(mapcar (lambda (x) `',x
) values
)))
1606 ;;;; local call optimization
1608 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1610 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1611 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1614 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1615 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1616 (defun propagate-to-refs (leaf type
)
1617 (declare (type leaf leaf
) (type ctype type
))
1618 (let ((var-type (leaf-type leaf
))
1619 (refs (leaf-refs leaf
)))
1620 (unless (or (fun-type-p var-type
)
1622 (eq :declared
(leaf-where-from leaf
))
1623 (type-needs-conservation-p var-type
)))
1624 (let ((int (type-approx-intersection2 var-type type
)))
1625 (when (type/= int var-type
)
1626 (setf (leaf-type leaf
) int
)
1627 (let ((s-int (make-single-value-type int
)))
1629 (derive-node-type ref s-int
)
1630 ;; KLUDGE: LET var substitution
1631 (let* ((lvar (node-lvar ref
)))
1632 (when (and lvar
(combination-p (lvar-dest lvar
)))
1633 (reoptimize-lvar lvar
)))))))
1636 ;;; Iteration variable: exactly one SETQ of the form:
1638 ;;; (let ((var initial))
1640 ;;; (setq var (+ var step))
1642 (defun maybe-infer-iteration-var-type (var initial-type
)
1643 (binding* ((sets (lambda-var-sets var
) :exit-if-null
)
1645 (() (null (rest sets
)) :exit-if-null
)
1646 (set-use (principal-lvar-use (set-value set
)))
1647 (() (and (combination-p set-use
)
1648 (eq (combination-kind set-use
) :known
)
1649 (fun-info-p (combination-fun-info set-use
))
1650 (not (node-to-be-deleted-p set-use
))
1651 (or (eq (combination-fun-source-name set-use
) '+)
1652 (eq (combination-fun-source-name set-use
) '-
)))
1654 (minusp (eq (combination-fun-source-name set-use
) '-
))
1655 (+-args
(basic-combination-args set-use
))
1656 (() (and (proper-list-of-length-p +-args
2 2)
1657 (let ((first (principal-lvar-use
1660 (eq (ref-leaf first
) var
))))
1662 (step-type (lvar-type (second +-args
)))
1663 (set-type (lvar-type (set-value set
))))
1664 (when (and (numeric-type-p initial-type
)
1665 (numeric-type-p step-type
)
1666 (or (numeric-type-equal initial-type step-type
)
1667 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1668 ;; the initial and the step are of different types,
1669 ;; and the step is less contagious.
1670 (numeric-type-equal initial-type
1671 (numeric-contagion initial-type
1673 (labels ((leftmost (x y cmp cmp
=)
1674 (cond ((eq x nil
) nil
)
1677 (let ((x1 (first x
)))
1679 (let ((y1 (first y
)))
1680 (if (funcall cmp x1 y1
) x y
)))
1682 (if (funcall cmp x1 y
) x y
)))))
1684 (let ((y1 (first y
)))
1685 (if (funcall cmp
= x y1
) x y
)))
1686 (t (if (funcall cmp x y
) x y
))))
1687 (max* (x y
) (leftmost x y
#'> #'>=))
1688 (min* (x y
) (leftmost x y
#'< #'<=)))
1689 (multiple-value-bind (low high
)
1690 (let ((step-type-non-negative (csubtypep step-type
(specifier-type
1692 (step-type-non-positive (csubtypep step-type
(specifier-type
1694 (cond ((or (and step-type-non-negative
(not minusp
))
1695 (and step-type-non-positive minusp
))
1696 (values (numeric-type-low initial-type
)
1697 (when (and (numeric-type-p set-type
)
1698 (numeric-type-equal set-type initial-type
))
1699 (max* (numeric-type-high initial-type
)
1700 (numeric-type-high set-type
)))))
1701 ((or (and step-type-non-positive
(not minusp
))
1702 (and step-type-non-negative minusp
))
1703 (values (when (and (numeric-type-p set-type
)
1704 (numeric-type-equal set-type initial-type
))
1705 (min* (numeric-type-low initial-type
)
1706 (numeric-type-low set-type
)))
1707 (numeric-type-high initial-type
)))
1710 (modified-numeric-type initial-type
1713 :enumerable nil
))))))
1714 (deftransform + ((x y
) * * :result result
)
1715 "check for iteration variable reoptimization"
1716 (let ((dest (principal-lvar-end result
))
1717 (use (principal-lvar-use x
)))
1718 (when (and (ref-p use
)
1722 (reoptimize-lvar (set-value dest
))))
1723 (give-up-ir1-transform))
1725 ;;; Figure out the type of a LET variable that has sets. We compute
1726 ;;; the union of the INITIAL-TYPE and the types of all the set
1727 ;;; values and to a PROPAGATE-TO-REFS with this type.
1728 (defun propagate-from-sets (var initial-type
)
1729 (let ((changes (not (csubtypep (lambda-var-last-initial-type var
) initial-type
)))
1731 (dolist (set (lambda-var-sets var
))
1732 (let ((type (lvar-type (set-value set
))))
1734 (when (node-reoptimize set
)
1735 (let ((old-type (node-derived-type set
)))
1736 (unless (values-subtypep old-type type
)
1737 (derive-node-type set
(make-single-value-type type
))
1739 (setf (node-reoptimize set
) nil
))))
1741 (setf (lambda-var-last-initial-type var
) initial-type
)
1742 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type
)
1743 (apply #'type-union initial-type types
))))
1744 (propagate-to-refs var res-type
))))
1747 ;;; If a LET variable, find the initial value's type and do
1748 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1750 (defun ir1-optimize-set (node)
1751 (declare (type cset node
))
1752 (let ((var (set-var node
)))
1753 (when (and (lambda-var-p var
) (leaf-refs var
))
1754 (let ((home (lambda-var-home var
)))
1755 (when (eq (functional-kind home
) :let
)
1756 (let* ((initial-value (let-var-initial-value var
))
1757 (initial-type (lvar-type initial-value
)))
1758 (setf (lvar-reoptimize initial-value
) nil
)
1759 (propagate-from-sets var initial-type
))))))
1760 (derive-node-type node
(make-single-value-type
1761 (lvar-type (set-value node
))))
1762 (setf (node-reoptimize node
) nil
)
1765 ;;; Return true if the value of REF will always be the same (and is
1766 ;;; thus legal to substitute.)
1767 (defun constant-reference-p (ref)
1768 (declare (type ref ref
))
1769 (let ((leaf (ref-leaf ref
)))
1771 ((or constant functional
) t
)
1773 (null (lambda-var-sets leaf
)))
1775 (not (eq (defined-fun-inlinep leaf
) :notinline
)))
1777 (case (global-var-kind leaf
)
1779 (let ((name (leaf-source-name leaf
)))
1781 (eq (symbol-package (fun-name-block-name name
))
1783 (info :function
:info name
)))))))))
1785 ;;; If we have a non-set LET var with a single use, then (if possible)
1786 ;;; replace the variable reference's LVAR with the arg lvar.
1788 ;;; We change the REF to be a reference to NIL with unused value, and
1789 ;;; let it be flushed as dead code. A side effect of this substitution
1790 ;;; is to delete the variable.
1791 (defun substitute-single-use-lvar (arg var
)
1792 (declare (type lvar arg
) (type lambda-var var
))
1793 (binding* ((ref (first (leaf-refs var
)))
1794 (lvar (node-lvar ref
) :exit-if-null
)
1795 (dest (lvar-dest lvar
))
1796 (dest-lvar (when (valued-node-p dest
) (node-lvar dest
))))
1798 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1799 ;; LVAR-USEs should not be met on one path. Another problem
1800 ;; is with dynamic-extent.
1801 (eq (lvar-uses lvar
) ref
)
1802 (not (block-delete-p (node-block ref
)))
1803 ;; If the destinatation is dynamic extent, don't substitute unless
1804 ;; the source is as well.
1806 (not (lvar-dynamic-extent dest-lvar
))
1807 (lvar-dynamic-extent lvar
))
1809 ;; we should not change lifetime of unknown values lvars
1811 (and (type-single-value-p (lvar-derived-type arg
))
1812 (multiple-value-bind (pdest pprev
)
1813 (principal-lvar-end lvar
)
1814 (declare (ignore pdest
))
1815 (lvar-single-value-p pprev
))
1816 ;; CASTs can disappear, don't substitute if
1817 ;; DEST-LVAR has other uses (this will be
1818 ;; insufficient if we have a CAST-CAST chain, but
1819 ;; works well for a single CAST)
1820 (or (null dest-lvar
)
1821 (atom (lvar-uses dest-lvar
)))))
1823 (or (eq (basic-combination-fun dest
) lvar
)
1824 (and (eq (basic-combination-kind dest
) :local
)
1825 (type-single-value-p (lvar-derived-type arg
)))))
1827 ;; While CRETURN and EXIT nodes may be known-values,
1828 ;; they have their own complications, such as
1829 ;; substitution into CRETURN may create new tail calls.
1832 (aver (lvar-single-value-p lvar
))
1834 (eq (node-home-lambda ref
)
1835 (lambda-home (lambda-var-home var
))))
1836 (let ((ref-type (single-value-type (node-derived-type ref
))))
1837 (cond ((csubtypep (single-value-type (lvar-type arg
)) ref-type
)
1838 (substitute-lvar-uses lvar arg
1839 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1841 (delete-lvar-use ref
))
1843 (let* ((value (make-lvar))
1844 (cast (insert-cast-before ref value ref-type
1845 ;; KLUDGE: it should be (TYPE-CHECK 0)
1847 (setf (cast-type-to-check cast
) *wild-type
*)
1848 (substitute-lvar-uses value arg
1851 (%delete-lvar-use ref
)
1852 (add-lvar-use cast lvar
)))))
1853 (setf (node-derived-type ref
) *wild-type
*)
1854 (change-ref-leaf ref
(find-constant nil
))
1857 (reoptimize-lvar lvar
)
1860 ;;; Delete a LET, removing the call and bind nodes, and warning about
1861 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1862 ;;; along right away and delete the REF and then the lambda, since we
1863 ;;; flush the FUN lvar.
1864 (defun delete-let (clambda)
1865 (declare (type clambda clambda
))
1866 (aver (functional-letlike-p clambda
))
1867 (note-unreferenced-fun-vars clambda
)
1868 (let ((call (let-combination clambda
)))
1869 (flush-dest (basic-combination-fun call
))
1871 (unlink-node (lambda-bind clambda
))
1872 (setf (lambda-bind clambda
) nil
))
1873 (setf (functional-kind clambda
) :zombie
)
1874 (let ((home (lambda-home clambda
)))
1875 (setf (lambda-lets home
) (delete clambda
(lambda-lets home
))))
1878 ;;; This function is called when one of the arguments to a LET
1879 ;;; changes. We look at each changed argument. If the corresponding
1880 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1881 ;;; consider substituting for the variable, and also propagate
1882 ;;; derived-type information for the arg to all the VAR's refs.
1884 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1885 ;;; subtype of the argument's leaf type. This prevents type checking
1886 ;;; from being defeated, and also ensures that the best representation
1887 ;;; for the variable can be used.
1889 ;;; Substitution of individual references is inhibited if the
1890 ;;; reference is in a different component from the home. This can only
1891 ;;; happen with closures over top level lambda vars. In such cases,
1892 ;;; the references may have already been compiled, and thus can't be
1893 ;;; retroactively modified.
1895 ;;; If all of the variables are deleted (have no references) when we
1896 ;;; are done, then we delete the LET.
1898 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1900 (defun propagate-let-args (call fun
)
1901 (declare (type combination call
) (type clambda fun
))
1902 (loop for arg in
(combination-args call
)
1903 and var in
(lambda-vars fun
) do
1904 (when (and arg
(lvar-reoptimize arg
))
1905 (setf (lvar-reoptimize arg
) nil
)
1907 ((lambda-var-sets var
)
1908 (propagate-from-sets var
(lvar-type arg
)))
1909 ((let ((use (lvar-uses arg
)))
1911 (let ((leaf (ref-leaf use
)))
1912 (when (and (constant-reference-p use
)
1913 (csubtypep (leaf-type leaf
)
1914 ;; (NODE-DERIVED-TYPE USE) would
1915 ;; be better -- APD, 2003-05-15
1917 (propagate-to-refs var
(lvar-type arg
))
1918 (unless (preserve-single-use-debug-var-p call var
)
1919 (let ((use-component (node-component use
)))
1922 (cond ((eq (node-component ref
) use-component
)
1925 (aver (lambda-toplevelish-p (lambda-home fun
)))
1929 ((and (null (rest (leaf-refs var
)))
1930 (not (preserve-single-use-debug-var-p call var
))
1931 (substitute-single-use-lvar arg var
)))
1933 (propagate-to-refs var
(lvar-type arg
))))))
1935 (when (every #'not
(combination-args call
))
1940 ;;; This function is called when one of the args to a non-LET local
1941 ;;; call changes. For each changed argument corresponding to an unset
1942 ;;; variable, we compute the union of the types across all calls and
1943 ;;; propagate this type information to the var's refs.
1945 ;;; If the function has an entry-fun, then we don't do anything: since
1946 ;;; it has a XEP we would not discover anything.
1948 ;;; If the function is an optional-entry-point, we will just make sure
1949 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1950 ;;; can erronously propagate too strict types into refs: see
1951 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1953 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1954 ;;; corresponding to changed arguments in CALL, since the only use in
1955 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1957 (defun propagate-local-call-args (call fun
)
1958 (declare (type combination call
) (type clambda fun
))
1959 (unless (functional-entry-fun fun
)
1960 (if (lambda-optional-dispatch fun
)
1961 ;; We can still make sure &REST is known to be a list.
1962 (loop for var in
(lambda-vars fun
)
1963 do
(let ((info (lambda-var-arg-info var
)))
1964 (when (and info
(eq :rest
(arg-info-kind info
)))
1965 (propagate-from-sets var
(specifier-type 'list
)))))
1967 (let* ((vars (lambda-vars fun
))
1968 (union (mapcar (lambda (arg var
)
1970 (lvar-reoptimize arg
)
1971 (null (basic-var-sets var
)))
1973 (basic-combination-args call
)
1975 (this-ref (lvar-use (basic-combination-fun call
))))
1977 (dolist (arg (basic-combination-args call
))
1979 (setf (lvar-reoptimize arg
) nil
)))
1981 (dolist (ref (leaf-refs fun
))
1982 (let ((dest (node-dest ref
)))
1983 (unless (or (eq ref this-ref
) (not dest
))
1985 (mapcar (lambda (this-arg old
)
1987 (setf (lvar-reoptimize this-arg
) nil
)
1988 (type-union (lvar-type this-arg
) old
)))
1989 (basic-combination-args dest
)
1992 (loop for var in vars
1994 when type do
(propagate-to-refs var type
)))))
1998 ;;;; multiple values optimization
2000 ;;; Do stuff to notice a change to a MV combination node. There are
2001 ;;; two main branches here:
2002 ;;; -- If the call is local, then it is already a MV let, or should
2003 ;;; become one. Note that although all :LOCAL MV calls must eventually
2004 ;;; be converted to :MV-LETs, there can be a window when the call
2005 ;;; is local, but has not been LET converted yet. This is because
2006 ;;; the entry-point lambdas may have stray references (in other
2007 ;;; entry points) that have not been deleted yet.
2008 ;;; -- The call is full. This case is somewhat similar to the non-MV
2009 ;;; combination optimization: we propagate return type information and
2010 ;;; notice non-returning calls. We also have an optimization
2011 ;;; which tries to convert MV-CALLs into MV-binds.
2012 (defun ir1-optimize-mv-combination (node)
2013 (let ((fun (basic-combination-fun node
)))
2014 (unless (and (node-p (lvar-uses fun
))
2015 (node-to-be-deleted-p (lvar-uses fun
)))
2016 (ecase (basic-combination-kind node
)
2018 (when (lvar-reoptimize fun
)
2019 (setf (lvar-reoptimize fun
) nil
)
2020 (maybe-let-convert (combination-lambda node
)))
2021 (loop for arg in
(basic-combination-args node
)
2023 (setf (lvar-reoptimize arg
) nil
))
2024 (when (eq (functional-kind (combination-lambda node
)) :mv-let
)
2025 (unless (convert-mv-bind-to-let node
)
2026 (ir1-optimize-mv-bind node
))))
2028 (let* ((fun-changed (lvar-reoptimize fun
))
2029 (args (basic-combination-args node
)))
2031 (setf (lvar-reoptimize fun
) nil
)
2032 (let ((type (lvar-type fun
)))
2033 (when (fun-type-p type
)
2034 (derive-node-type node
(fun-type-returns type
))))
2035 (maybe-terminate-block node nil
)
2036 (let ((use (lvar-uses fun
)))
2037 (when (and (ref-p use
) (functional-p (ref-leaf use
)))
2038 (convert-call-if-possible use node
)
2039 (when (eq (basic-combination-kind node
) :local
)
2040 (maybe-let-convert (ref-leaf use
))))))
2041 (unless (or (eq (basic-combination-kind node
) :local
)
2042 (eq (lvar-fun-name fun
) '%throw
))
2043 (ir1-optimize-mv-call node
))
2045 (setf (lvar-reoptimize arg
) nil
))))
2049 ;;; Propagate derived type info from the values lvar to the vars.
2050 (defun ir1-optimize-mv-bind (node)
2051 (declare (type mv-combination node
))
2052 (let* ((args (basic-combination-args node
))
2053 (vars (lambda-vars (combination-lambda node
)))
2054 (types (if (singleton-p args
)
2055 (values-type-in (lvar-derived-type (first args
))
2057 (loop for arg in args
2058 append
(values-types (lvar-derived-type arg
))))))
2059 (loop for var in vars
2061 do
(if (basic-var-sets var
)
2062 (propagate-from-sets var type
)
2063 (propagate-to-refs var type
)))
2064 (loop for arg in args
2066 (setf (lvar-reoptimize arg
) nil
)))
2069 (defun ir1-optimize-mv-call (node)
2070 (let ((fun (basic-combination-fun node
))
2071 (*compiler-error-context
* node
)
2072 (ref (lvar-uses (basic-combination-fun node
)))
2073 (args (basic-combination-args node
)))
2075 (multiple-value-bind (min max
) (fun-type-nargs (lvar-type fun
))
2077 (loop for arg in args
2078 for nvals
= (nth-value 1 (values-types (lvar-derived-type arg
)))
2079 when
(eq nvals
:unknown
) return nil
2082 (when (and min
(< total-nvals min
))
2084 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2087 (setf (basic-combination-kind node
) :error
)
2088 (return-from ir1-optimize-mv-call
))
2089 (when (and max
(> total-nvals max
))
2091 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2094 (setf (basic-combination-kind node
) :error
)
2095 (return-from ir1-optimize-mv-call
)))
2096 (let ((count (cond (total-nvals)
2097 ((and (policy node
(zerop verify-arg-count
))
2102 (with-ir1-environment-from-node node
2103 (let* ((dums (make-gensym-list count
))
2105 (leaf (ref-leaf ref
))
2106 (fun (ir1-convert-lambda
2107 `(lambda (&optional
,@dums
&rest
,ignore
)
2108 (declare (ignore ,ignore
))
2109 (%funcall
,leaf
,@dums
))
2110 :debug-name
(leaf-%debug-name leaf
))))
2111 (change-ref-leaf ref fun
)
2112 (aver (eq (basic-combination-kind node
) :full
))
2113 (locall-analyze-component *current-component
*)
2114 (aver (eq (basic-combination-kind node
) :local
))))))))))
2118 ;;; (multiple-value-bind
2127 ;;; What we actually do is convert the VALUES combination into a
2128 ;;; normal LET combination calling the original :MV-LET lambda. If
2129 ;;; there are extra args to VALUES, discard the corresponding
2130 ;;; lvars. If there are insufficient args, insert references to NIL.
2131 (defun convert-mv-bind-to-let (call)
2132 (declare (type mv-combination call
))
2133 (let* ((args (basic-combination-args call
))
2134 (use (lvar-uses (first args
))))
2135 (when (and (singleton-p args
)
2137 (eq (lvar-fun-name (combination-fun use
))
2139 (let* ((fun (combination-lambda call
))
2140 (vars (lambda-vars fun
))
2141 (vals (combination-args use
))
2142 (nvars (length vars
))
2143 (nvals (length vals
)))
2144 (cond ((> nvals nvars
)
2145 (mapc #'flush-dest
(subseq vals nvars
))
2146 (setq vals
(subseq vals
0 nvars
)))
2148 (with-ir1-environment-from-node use
2149 (let ((node-prev (node-prev use
)))
2150 (setf (node-prev use
) nil
)
2151 (setf (ctran-next node-prev
) nil
)
2152 (collect ((res vals
))
2153 (loop for count below
(- nvars nvals
)
2154 for prev
= node-prev then ctran
2155 for ctran
= (make-ctran)
2156 and lvar
= (make-lvar use
)
2157 do
(reference-constant prev ctran lvar nil
)
2159 finally
(link-node-to-previous-ctran
2161 (setq vals
(res)))))))
2162 (setf (combination-args use
) vals
)
2163 (flush-dest (combination-fun use
))
2164 (let ((fun-lvar (basic-combination-fun call
)))
2165 (setf (lvar-dest fun-lvar
) use
)
2166 (setf (combination-fun use
) fun-lvar
)
2167 (flush-lvar-externally-checkable-type fun-lvar
))
2168 (setf (combination-kind use
) :local
)
2169 (setf (functional-kind fun
) :let
)
2170 (flush-dest (first (basic-combination-args call
)))
2173 (reoptimize-lvar (first vals
)))
2174 ;; Propagate derived types from the VALUES call to its args:
2175 ;; transforms can leave the VALUES call with a better type
2176 ;; than its args have, so make sure not to throw that away.
2177 (let ((types (values-type-types (node-derived-type use
))))
2180 (let ((type (pop types
)))
2181 (assert-lvar-type val type
**zero-typecheck-policy
**)))))
2182 ;; Propagate declared types of MV-BIND variables.
2183 (propagate-to-args use fun
)
2184 (reoptimize-call use
))
2188 ;;; (values-list (list x y z))
2193 ;;; In implementation, this is somewhat similar to
2194 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2195 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2196 ;;; (allowing the LIST to be flushed.)
2198 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2199 (defoptimizer (values-list optimizer
) ((list) node
)
2200 (let ((use (lvar-uses list
)))
2201 (when (and (combination-p use
)
2202 (eq (lvar-fun-name (combination-fun use
))
2205 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2206 (change-ref-leaf (lvar-uses (combination-fun node
))
2207 (find-free-fun 'values
"in a strange place"))
2208 (setf (combination-kind node
) :full
)
2209 (let ((args (combination-args use
)))
2211 (setf (lvar-dest arg
) node
)
2212 (flush-lvar-externally-checkable-type arg
))
2213 (setf (combination-args use
) nil
)
2215 (flush-combination use
)
2216 (setf (combination-args node
) args
))
2219 ;;; If VALUES appears in a non-MV context, then effectively convert it
2220 ;;; to a PROG1. This allows the computation of the additional values
2221 ;;; to become dead code.
2222 (deftransform values
((&rest vals
) * * :node node
)
2223 (unless (lvar-single-value-p (node-lvar node
))
2224 (give-up-ir1-transform))
2225 (setf (node-derived-type node
)
2226 (make-short-values-type (list (single-value-type
2227 (node-derived-type node
)))))
2228 (principal-lvar-single-valuify (node-lvar node
))
2230 (let ((dummies (make-gensym-list (length (cdr vals
)))))
2231 `(lambda (val ,@dummies
)
2232 (declare (ignore ,@dummies
))
2238 (defun delete-cast (cast)
2239 (declare (type cast cast
))
2240 (let ((value (cast-value cast
))
2241 (lvar (cast-lvar cast
)))
2242 (when (and (bound-cast-p cast
)
2243 (bound-cast-check cast
))
2244 (flush-combination (bound-cast-check cast
))
2245 (setf (bound-cast-check cast
) nil
))
2246 (delete-filter cast lvar value
)
2248 (reoptimize-lvar lvar
)
2249 (when (lvar-single-value-p lvar
)
2250 (note-single-valuified-lvar lvar
)))
2253 (defun may-delete-vestigial-exit (cast)
2254 (let ((exit-lexenv (cast-vestigial-exit-lexenv cast
)))
2256 ;; Vestigial exits are only introduced when eliminating a local
2257 ;; RETURN-FROM. We may delete them only when we can show that
2258 ;; there are no other code paths that use the entry LVAR that
2259 ;; are live from within the block that contained the deleted
2260 ;; EXIT and that all uses of the entry LVAR have the same
2261 ;; dynamic-extent environment. The conservative version of this
2262 ;; is that there are no EXITs for any ENTRY introduced between
2263 ;; the LEXENV of the deleted EXIT and the LEXENV of the target
2264 ;; ENTRY and that there is no :DYNAMIC-EXTENT cleanup between
2265 ;; the deleted EXIT and the target ENTRY (which the CAST shares
2266 ;; a lexenv with). See ONLY-HARMLESS-CLEANUPS in locall.lisp
2267 ;; for a similar analysis.
2268 (let ((entry-cleanup (block-start-cleanup (node-block cast
))))
2269 (do-nested-cleanups (cleanup exit-lexenv
)
2270 (when (eq cleanup entry-cleanup
)
2272 (case (cleanup-kind cleanup
)
2274 (when (entry-exits (cleanup-mess-up cleanup
))
2275 (return-from may-delete-vestigial-exit nil
)))
2277 (return-from may-delete-vestigial-exit nil
)))))))
2280 (defun compile-time-type-error-context (context)
2281 #+sb-xc-host context
2282 #-sb-xc-host
(source-to-string context
))
2284 (defun ir1-optimize-cast (cast &optional do-not-optimize
)
2285 (declare (type cast cast
))
2286 (let ((value (cast-value cast
))
2287 (atype (cast-asserted-type cast
)))
2288 (unless (or do-not-optimize
2289 (not (may-delete-vestigial-exit cast
)))
2290 (when (and (bound-cast-p cast
)
2291 (bound-cast-check cast
)
2292 (constant-lvar-p (bound-cast-bound cast
)))
2294 (specifier-type `(integer 0 (,(lvar-value (bound-cast-bound cast
)))))
2295 (cast-asserted-type cast
) atype
2296 (bound-cast-derived cast
) t
))
2297 (let ((lvar (node-lvar cast
)))
2298 (when (and (or (not (bound-cast-p cast
))
2299 (bound-cast-derived cast
))
2300 (values-subtypep (lvar-derived-type value
)
2301 (cast-asserted-type cast
)))
2302 (when (and (function-designator-cast-p cast
)
2304 (let ((*valid-fun-use-name
* (function-designator-cast-caller cast
))
2305 (*lossage-fun
* #'compiler-warn
)
2306 (*compiler-error-context
* cast
))
2307 (valid-callable-argument lvar
2309 (function-designator-cast-arg-count cast
))))
2312 (return-from ir1-optimize-cast t
))
2314 (when (and (listp (lvar-uses value
))
2316 ;; Pathwise removing of CAST
2317 (let ((ctran (node-next cast
))
2318 (dest (lvar-dest lvar
))
2321 (do-uses (use value
)
2322 (when (and (values-subtypep (node-derived-type use
) atype
)
2323 (immediately-used-p value use
))
2325 (when ctran
(ensure-block-start ctran
))
2326 (setq next-block
(first (block-succ (node-block cast
))))
2327 (ensure-block-start (node-prev cast
))
2328 (reoptimize-lvar lvar
)
2329 (setf (lvar-%derived-type value
) nil
))
2330 (%delete-lvar-use use
)
2331 (add-lvar-use use lvar
)
2332 (unlink-blocks (node-block use
) (node-block cast
))
2333 (link-blocks (node-block use
) next-block
)
2334 (when (and (return-p dest
)
2335 (basic-combination-p use
)
2336 (eq (basic-combination-kind use
) :local
))
2338 (dolist (use (merges))
2339 (merge-tail-sets use
))))))
2341 (when (and (bound-cast-p cast
)
2342 (bound-cast-check cast
)
2343 (policy cast
(= insert-array-bounds-checks
0)))
2344 (flush-combination (bound-cast-check cast
))
2345 (setf (bound-cast-check cast
) nil
)))
2347 (let* ((value-type (lvar-derived-type value
))
2348 (int (values-type-intersection value-type atype
)))
2349 (derive-node-type cast int
)
2351 (neq int
*empty-type
*)
2352 (eq value-type
*empty-type
*)))
2353 ;; No need to transform into an analog of
2354 ;; %COMPILE-TIME-TYPE-ERROR, %CHECK-BOUND will signal at
2355 ;; run-time and %CHECK-BOUND ir2-converter will signal at
2356 ;; compile-time if it survives further stages of ir1
2358 ((bound-cast-p cast
))
2360 ;; FIXME: Do it in one step.
2361 (let ((context (node-source-form cast
))
2362 (detail (lvar-all-sources (cast-value cast
))))
2363 (unless (cast-silent-conflict cast
)
2366 (if (cast-single-value-p cast
)
2368 `(multiple-value-call #'list
'dummy
))))
2371 ;; FIXME: Derived type.
2372 (if (cast-silent-conflict cast
)
2373 (let ((dummy-sym (gensym)))
2374 `(let ((,dummy-sym
'dummy
))
2375 ,(internal-type-error-call dummy-sym atype
2376 (cast-context cast
))
2378 `(%compile-time-type-error
'dummy
2379 ',(type-specifier atype
)
2380 ',(type-specifier value-type
)
2382 ',(compile-time-type-error-context context
)
2383 ',(cast-context cast
)))))
2384 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2385 ;; functions, so we declare the return type of
2386 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2388 (setq value
(cast-value cast
))
2389 (derive-node-type (lvar-uses value
) *empty-type
*)
2390 (maybe-terminate-block (lvar-uses value
) nil
)
2391 ;; FIXME: Is it necessary?
2392 (aver (null (block-pred (node-block cast
))))
2393 (delete-block-lazily (node-block cast
))
2394 (return-from ir1-optimize-cast
)))
2395 (when (eq (node-derived-type cast
) *empty-type
*)
2396 (maybe-terminate-block cast nil
))
2398 (when (and (cast-%type-check cast
)
2399 (values-subtypep value-type
2400 (cast-type-to-check cast
)))
2401 (setf (cast-%type-check cast
) nil
))))
2403 (unless do-not-optimize
2404 (setf (node-reoptimize cast
) nil
)))