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 (setf (block-reoptimize block
) t
)
259 (reoptimize-component component
:maybe
))))
261 (setf (block-type-check (node-block node
)) t
)))
264 (defun reoptimize-lvar-uses (lvar)
265 (declare (type lvar lvar
))
267 (setf (node-reoptimize use
) t
)
268 (setf (block-reoptimize (node-block use
)) t
)
269 (reoptimize-component (node-component use
) :maybe
)))
271 ;;; Annotate NODE to indicate that its result has been proven to be
272 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
273 ;;; only correct way to supply information discovered about a node's
274 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
275 ;;; information may be lost and reoptimization may not happen.
277 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
278 ;;; intersection is different from the old type, then we do a
279 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
280 (defun derive-node-type (node rtype
&key from-scratch
)
281 (declare (type valued-node node
) (type ctype rtype
))
282 (let* ((initial-type (node-derived-type node
))
283 (node-type (if from-scratch
286 (unless (eq initial-type rtype
)
287 (let ((int (values-type-intersection node-type rtype
))
288 (lvar (node-lvar node
)))
289 (when (type/= initial-type int
)
290 (when (and *check-consistency
*
291 (eq int
*empty-type
*)
292 (not (eq rtype
*empty-type
*)))
293 (aver (not from-scratch
))
294 (let ((*compiler-error-context
* node
))
296 "New inferred type ~S conflicts with old type:~
297 ~% ~S~%*** possible internal error? Please report this."
298 (type-specifier rtype
) (type-specifier node-type
))))
299 (setf (node-derived-type node
) int
)
300 ;; If the new type consists of only one object, replace the
301 ;; node with a constant reference.
302 (when (and (ref-p node
)
303 (lambda-var-p (ref-leaf node
)))
304 (let ((type (single-value-type int
)))
305 (when (and (member-type-p type
)
306 (eql 1 (member-type-size type
)))
307 (change-ref-leaf node
(find-constant
308 (first (member-type-members type
)))))))
309 (reoptimize-lvar lvar
)))))
312 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
313 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
314 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
315 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK to
316 ;;; guarantee that the new assertion will be checked.
317 (defun assert-lvar-type (lvar type policy
&optional context
)
318 (declare (type lvar lvar
) (type ctype type
))
319 (unless (values-subtypep (lvar-derived-type lvar
) type
)
320 (let ((internal-lvar (make-lvar))
321 (dest (lvar-dest lvar
)))
322 (substitute-lvar internal-lvar lvar
)
323 (let ((cast (insert-cast-before dest lvar type policy
325 (use-lvar cast internal-lvar
)
328 (defun insert-function-designator-cast-before (next lvar type arg-count caller policy
)
329 (declare (type node next
) (type lvar lvar
) (type ctype type
))
330 (with-ir1-environment-from-node next
331 (let* ((ctran (node-prev next
))
332 (cast (make-function-designator-cast :asserted-type type
333 :type-to-check
(maybe-weaken-check type policy
)
335 :derived-type
(coerce-to-values type
)
338 (internal-ctran (make-ctran)))
339 (setf (ctran-next ctran
) cast
340 (node-prev cast
) ctran
)
341 (use-ctran cast internal-ctran
)
342 (link-node-to-previous-ctran next internal-ctran
)
343 (setf (lvar-dest lvar
) cast
)
344 (reoptimize-lvar lvar
)
345 (when (return-p next
)
346 (node-ends-block cast
))
347 (setf (block-type-check (node-block cast
)) t
)
350 (defun assert-function-designator-lvar-type (lvar type arg-count caller
)
351 (declare (type lvar lvar
) (type ctype type
))
352 (let ((internal-lvar (make-lvar))
353 (dest (lvar-dest lvar
)))
354 (substitute-lvar internal-lvar lvar
)
355 (let ((cast (insert-function-designator-cast-before dest lvar type arg-count
357 (use-lvar cast internal-lvar
))))
362 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
363 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
364 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
365 ;;; we are done, then another iteration would be beneficial.
366 (defun ir1-optimize (component fastp
)
367 (declare (type component component
))
368 (setf (component-reoptimize component
) nil
)
369 (loop with block
= (block-next (component-head component
))
370 with tail
= (component-tail component
)
371 for last-block
= block
372 until
(eq block tail
)
374 ;; We delete blocks when there is either no predecessor or the
375 ;; block is in a lambda that has been deleted. These blocks
376 ;; would eventually be deleted by DFO recomputation, but doing
377 ;; it here immediately makes the effect available to IR1
379 ((or (block-delete-p block
)
380 (null (block-pred block
)))
381 (delete-block-lazily block
)
382 (setq block
(clean-component component block
)))
383 ((eq (functional-kind (block-home-lambda block
)) :deleted
)
384 ;; Preserve the BLOCK-SUCC invariant that almost every block has
385 ;; one successor (and a block with DELETE-P set is an acceptable
387 (mark-for-deletion block
)
388 (setq block
(clean-component component block
)))
391 (let ((succ (block-succ block
)))
392 (unless (singleton-p succ
)
395 (let ((last (block-last block
)))
398 (flush-dest (if-test last
))
399 (when (unlink-node last
)
402 (when (maybe-delete-exit last
)
405 (unless (join-successor-if-possible block
)
408 (when (and (not fastp
) (block-reoptimize block
) (block-component block
))
409 (aver (not (block-delete-p block
)))
410 (ir1-optimize-block block
))
412 (cond ((and (block-delete-p block
) (block-component block
))
413 (setq block
(clean-component component block
)))
414 ((and (block-flush-p block
) (block-component block
))
415 (flush-dead-code block
)))))
416 do
(when (eq block last-block
)
417 (setq block
(block-next block
))))
421 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
424 ;;; Note that although they are cleared here, REOPTIMIZE flags might
425 ;;; still be set upon return from this function, meaning that further
426 ;;; optimization is wanted (as a consequence of optimizations we did).
427 (defun ir1-optimize-block (block)
428 (declare (type cblock block
))
429 ;; We clear the node and block REOPTIMIZE flags before doing the
430 ;; optimization, not after. This ensures that the node or block will
431 ;; be reoptimized if necessary.
432 (setf (block-reoptimize block
) nil
)
433 (do-nodes (node nil block
:restart-p t
)
434 (when (node-reoptimize node
)
435 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
436 (setf (node-reoptimize node
) nil
)
440 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
441 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
442 ;; any argument changes.
443 (ir1-optimize-combination node
))
445 (ir1-optimize-if node
))
447 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
448 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
449 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
451 (setf (node-reoptimize node
) t
)
452 (ir1-optimize-return node
))
454 (ir1-optimize-mv-combination node
))
456 ;; With an EXIT, we derive the node's type from the VALUE's
458 (let ((value (exit-value node
)))
460 (derive-node-type node
(lvar-derived-type value
)))))
462 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
463 ;; is accurate till the node actually has been reoptimized.
464 (setf (node-reoptimize node
) t
)
465 (ir1-optimize-set node
))
467 (ir1-optimize-cast node
)))))
471 ;;; Try to join with a successor block. If we succeed, we return true,
473 (defun join-successor-if-possible (block)
474 (declare (type cblock block
))
475 (let ((next (first (block-succ block
))))
476 (when (block-start next
) ; NEXT is not an END-OF-COMPONENT marker
477 (cond ( ;; We cannot combine with a successor block if:
479 ;; the successor has more than one predecessor;
480 (rest (block-pred next
))
481 ;; the successor is the current block (infinite loop);
483 ;; the next block has a different cleanup, and thus
484 ;; we may want to insert cleanup code between the
485 ;; two blocks at some point;
486 (not (eq (block-end-cleanup block
)
487 (block-start-cleanup next
)))
488 ;; the next block has a different home lambda, and
489 ;; thus the control transfer is a non-local exit.
490 (not (eq (block-home-lambda block
)
491 (block-home-lambda next
)))
492 ;; Stack analysis phase wants ENTRY to start a block...
493 (entry-p (block-start-node next
))
494 (let ((last (block-last block
)))
495 (and (valued-node-p last
)
496 (awhen (node-lvar last
)
498 ;; ... and a DX-allocator to end a block.
499 (lvar-dynamic-extent it
)
500 ;; ... and for there to be no chance of there
501 ;; being two successive USEs of the same
502 ;; multi-valued LVAR in the same block (since
503 ;; we can only insert cleanup code at block
504 ;; boundaries, but need to discard
505 ;; multi-valued LVAR contents before they are
507 (and (consp (lvar-uses it
))
508 (not (lvar-single-value-p it
))))))))
511 (join-blocks block next
)
514 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
515 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
516 ;;; for the two blocks so that any indicated optimization gets done.
517 (defun join-blocks (block1 block2
)
518 (declare (type cblock block1 block2
))
519 (let* ((last1 (block-last block1
))
520 (last2 (block-last block2
))
521 (succ (block-succ block2
))
522 (start2 (block-start block2
)))
523 (do ((ctran start2
(node-next (ctran-next ctran
))))
525 (setf (ctran-block ctran
) block1
))
527 (unlink-blocks block1 block2
)
529 (unlink-blocks block2 block
)
530 (link-blocks block1 block
))
532 (setf (ctran-kind start2
) :inside-block
)
533 (setf (node-next last1
) start2
)
534 (setf (ctran-use start2
) last1
)
535 (setf (block-last block1
) last2
))
537 (setf (block-flags block1
)
538 (attributes-union (block-flags block1
)
539 (block-flags block2
)))
541 (let ((next (block-next block2
))
542 (prev (block-prev block2
)))
543 (setf (block-next prev
) next
)
544 (setf (block-prev next
) prev
))
548 ;;; Delete any nodes in BLOCK whose value is unused and which have no
549 ;;; side effects. We can delete sets of lexical variables when the set
550 ;;; variable has no references.
551 (defun flush-dead-code (block &aux victim
)
552 (declare (type cblock block
))
553 (setf (block-flush-p block
) nil
)
554 (do-nodes-backwards (node lvar block
:restart-p t
)
556 (do-uses (other-node lvar
)
557 (when (and (neq node other-node
)
558 (eq block
(node-block other-node
)))
559 ;; This must be a preceding node and the current node will
560 ;; overwrite the value, unlink the lvar and the node will
561 ;; get a chance to be deleted on one of the next iterations
562 (delete-lvar-use other-node
)))
569 (when (flushable-combination-p node
)
571 (flush-combination node
)))
573 (when (eq (basic-combination-kind node
) :local
)
574 (let ((fun (combination-lambda node
)))
575 (when (dolist (var (lambda-vars fun
) t
)
576 (when (or (leaf-refs var
)
577 (lambda-var-sets var
))
580 (mapc #'flush-dest
(basic-combination-args node
))
583 (let ((value (exit-value node
)))
587 (setf (exit-value node
) nil
))))
589 (let ((var (set-var node
)))
590 (when (and (lambda-var-p var
)
591 (null (leaf-refs var
)))
593 (flush-dest (set-value node
))
594 (setf (basic-var-sets var
)
595 (delq node
(basic-var-sets var
)))
596 (unlink-node node
))))
598 (unless (cast-type-check node
)
600 (flush-dest (cast-value node
))
601 (unlink-node node
))))))
605 ;;;; local call return type propagation
607 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
608 ;;; flag set. It iterates over the uses of the RESULT, looking for
609 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
610 ;;; call, then we union its type together with the types of other such
611 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
612 ;;; type with the RESULT's asserted type. We can make this
613 ;;; intersection now (potentially before type checking) because this
614 ;;; assertion on the result will eventually be checked (if
617 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
618 ;;; combination, which may change the successor of the call to be the
619 ;;; called function, and if so, checks if the call can become an
620 ;;; assignment. If we convert to an assignment, we abort, since the
621 ;;; RETURN has been deleted.
622 (defun find-result-type (node)
623 (declare (type creturn node
))
624 (let ((result (return-result node
)))
625 (collect ((use-union *empty-type
* values-type-union
))
626 (do-uses (use result
)
627 (let ((use-home (node-home-lambda use
)))
628 (cond ((or (eq (functional-kind use-home
) :deleted
)
629 (block-delete-p (node-block use
))))
630 ((not (and (basic-combination-p use
)
631 (eq (basic-combination-kind use
) :local
)))
632 (use-union (node-derived-type use
)))
633 ((or (eq (functional-kind (combination-lambda use
)) :deleted
)
634 (block-delete-p (lambda-block (combination-lambda use
)))))
636 (aver (eq (lambda-tail-set use-home
)
637 (lambda-tail-set (combination-lambda use
))))
638 (when (combination-p use
)
639 (when (nth-value 1 (maybe-convert-tail-local-call use
))
640 (return-from find-result-type t
)))))))
642 ;; (values-type-intersection
643 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
647 (setf (return-result-type node
) int
))))
650 ;;; Do stuff to realize that something has changed about the value
651 ;;; delivered to a return node. Since we consider the return values of
652 ;;; all functions in the tail set to be equivalent, this amounts to
653 ;;; bringing the entire tail set up to date. We iterate over the
654 ;;; returns for all the functions in the tail set, reanalyzing them
655 ;;; all (not treating NODE specially.)
657 ;;; When we are done, we check whether the new type is different from
658 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
659 ;;; all the lvars for references to functions in the tail set. This
660 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
661 ;;; results of the calls.
662 (defun ir1-optimize-return (node)
663 (declare (type creturn node
))
664 (let ((lambda (return-lambda node
)))
667 (let* ((tails (lambda-tail-set lambda
))
668 (funs (tail-set-funs tails
)))
669 (collect ((res *empty-type
* values-type-union
))
671 (let ((return (lambda-return fun
)))
673 (when (node-reoptimize return
)
674 (setf (node-reoptimize return
) nil
)
675 (when (find-result-type return
)
677 (res (return-result-type return
)))))
679 (when (type/= (res) (tail-set-type tails
))
680 (setf (tail-set-type tails
) (res))
681 (dolist (fun (tail-set-funs tails
))
682 (dolist (ref (leaf-refs fun
))
683 (reoptimize-lvar (node-lvar ref
)))))))))
689 ;;; Utility: return T if both argument cblocks are equivalent. For now,
690 ;;; detect only blocks that read the same leaf into the same lvar, and
691 ;;; continue to the same block.
692 (defun cblocks-equivalent-p (x y
)
693 (declare (type cblock x y
))
694 (and (ref-p (block-start-node x
))
695 (eq (block-last x
) (block-start-node x
))
697 (ref-p (block-start-node y
))
698 (eq (block-last y
) (block-start-node y
))
700 (equal (block-succ x
) (block-succ y
))
701 (eql (ref-lvar (block-start-node x
)) (ref-lvar (block-start-node y
)))
702 (eql (ref-leaf (block-start-node x
)) (ref-leaf (block-start-node y
)))))
704 ;;; Check whether the predicate is known to be true or false,
705 ;;; deleting the IF node in favor of the appropriate branch when this
707 ;;; Similarly, when both branches are equivalent, branch directly to either
709 ;;; Also, if the test has multiple uses, replicate the node when possible...
710 ;;; in fact, splice in direct jumps to the right branch if possible.
711 (defun ir1-optimize-if (node)
712 (declare (type cif node
))
713 (let ((test (if-test node
))
714 (block (node-block node
)))
715 (let* ((type (lvar-type test
))
716 (consequent (if-consequent node
))
717 (alternative (if-alternative node
))
719 (cond ((constant-lvar-p test
)
720 (if (lvar-value test
) alternative consequent
))
721 ((not (types-equal-or-intersect type
(specifier-type 'null
)))
723 ((type= type
(specifier-type 'null
))
725 ((or (eq consequent alternative
) ; Can this happen?
726 (cblocks-equivalent-p alternative consequent
))
727 ;; Even if the references are the same they can have
728 ;; different derived types based on the TEST
729 ;; Don't lose the second type when killing it.
730 (let ((consequent-ref (block-start-node consequent
)))
731 (derive-node-type consequent-ref
733 (node-derived-type consequent-ref
)
734 (node-derived-type (block-start-node alternative
)))
738 (kill-if-branch-1 node test block victim
)
739 (return-from ir1-optimize-if
(values))))
740 (tension-if-if-1 node test block
)
741 (duplicate-if-if-1 node test block
)
744 ;; When we know that we only have a single successor, kill the victim
745 ;; ... unless the victim and the remaining successor are the same.
746 (defun kill-if-branch-1 (node test block victim
)
747 (declare (type cif node
))
749 (when (rest (block-succ block
))
750 (unlink-blocks block victim
))
751 (setf (component-reanalyze (node-component node
)) t
)
754 ;; When if/if conversion would leave (if ... (if nil ...)) or
755 ;; (if ... (if not-nil ...)), splice the correct successor right
757 (defun tension-if-if-1 (node test block
)
758 (when (and (eq (block-start-node block
) node
)
759 (listp (lvar-uses test
)))
761 (when (immediately-used-p test use
)
762 (let* ((type (single-value-type (node-derived-type use
)))
763 (target (if (type= type
(specifier-type 'null
))
764 (if-alternative node
)
765 (multiple-value-bind (typep surep
)
767 (and (not typep
) surep
768 (if-consequent node
))))))
770 (let ((pred (node-block use
)))
771 (cond ((listp (lvar-uses test
))
772 (change-block-successor pred block target
)
773 (delete-lvar-use use
))
775 ;; only one use left. Just kill the now-useless
776 ;; branch to avoid spurious code deletion notes.
777 (aver (rest (block-succ block
)))
780 (if (eql target
(if-alternative node
))
782 (if-alternative node
)))
783 (return-from tension-if-if-1
))))))))))
785 ;; Finally, duplicate EQ-nil tests
786 (defun duplicate-if-if-1 (node test block
)
787 (when (and (eq (block-start-node block
) node
)
788 (listp (lvar-uses test
)))
790 (when (immediately-used-p test use
)
791 (convert-if-if use node
)
792 ;; leave the last use as is, instead of replacing
793 ;; the (singly-referenced) CIF node with a duplicate.
794 (when (not (listp (lvar-uses test
))) (return))))))
796 ;;; Create a new copy of an IF node that tests the value of the node
797 ;;; USE. The test must have >1 use, and must be immediately used by
798 ;;; USE. NODE must be the only node in its block (implying that
799 ;;; block-start = if-test).
801 ;;; This optimization has an effect semantically similar to the
802 ;;; source-to-source transformation:
803 ;;; (IF (IF A B C) D E) ==>
804 ;;; (IF A (IF B D E) (IF C D E))
806 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
807 ;;; node so that dead code deletion notes will definitely not consider
808 ;;; either node to be part of the original source. One node might
809 ;;; become unreachable, resulting in a spurious note.
810 (defun convert-if-if (use node
)
811 (declare (type node use
) (type cif node
))
812 (with-ir1-environment-from-node node
813 (let* ((block (node-block node
))
814 (test (if-test node
))
815 (cblock (if-consequent node
))
816 (ablock (if-alternative node
))
817 (use-block (node-block use
))
818 (new-ctran (make-ctran))
819 (new-lvar (make-lvar))
820 (new-node (make-if :test new-lvar
822 :alternative ablock
))
823 (new-block (ctran-starts-block new-ctran
)))
824 (link-node-to-previous-ctran new-node new-ctran
)
825 (setf (lvar-dest new-lvar
) new-node
)
826 (setf (block-last new-block
) new-node
)
828 (unlink-blocks use-block block
)
829 (%delete-lvar-use use
)
830 (add-lvar-use use new-lvar
)
831 (link-blocks use-block new-block
)
833 (link-blocks new-block cblock
)
834 (link-blocks new-block ablock
)
836 (push "<IF Duplication>" (node-source-path node
))
837 (push "<IF Duplication>" (node-source-path new-node
))
839 (reoptimize-lvar test
)
840 (reoptimize-lvar new-lvar
)
841 (setf (component-reanalyze *current-component
*) t
)))
844 ;;;; exit IR1 optimization
846 ;;; This function attempts to delete an exit node, returning true if
847 ;;; it deletes the block as a consequence:
848 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
849 ;;; anything, since there is nothing to be done.
850 ;;; -- If the exit node and its ENTRY have the same home lambda then
851 ;;; we know the exit is local, and can delete the exit. We change
852 ;;; uses of the Exit-Value to be uses of the original lvar,
853 ;;; then unlink the node. If the exit is to a TR context, then we
854 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
855 ;;; their value to this exit.
856 ;;; -- If there is no value (as in a GO), then we skip the value
859 ;;; This function is also called by environment analysis, since it
860 ;;; wants all exits to be optimized even if normal optimization was
862 (defun maybe-delete-exit (node)
863 (declare (type exit node
))
864 (let ((value (exit-value node
))
865 (entry (exit-entry node
)))
867 (eq (node-home-lambda node
) (node-home-lambda entry
)))
868 (setf (entry-exits entry
) (delq node
(entry-exits entry
)))
870 (delete-filter node
(node-lvar node
) value
)
871 (unlink-node node
)))))
874 ;;;; combination IR1 optimization
876 ;;; Report as we try each transform?
878 (defvar *show-transforms-p
* nil
)
880 (defun check-important-result (node info
)
881 (when (and (null (node-lvar node
))
882 (ir1-attributep (fun-info-attributes info
) important-result
))
883 (let ((*compiler-error-context
* node
))
885 "The return value of ~A should not be discarded."
886 (lvar-fun-name (basic-combination-fun node
) t
)))))
888 ;;; Do IR1 optimizations on a COMBINATION node.
889 (declaim (ftype (function (combination) (values)) ir1-optimize-combination
))
890 (defun ir1-optimize-combination (node)
891 (when (lvar-reoptimize (basic-combination-fun node
))
892 (propagate-fun-change node
)
893 (maybe-terminate-block node nil
))
894 (let ((args (basic-combination-args node
))
895 (info (basic-combination-fun-info node
)))
896 (flet ((clear-reoptimize-args ()
899 (setf (lvar-reoptimize arg
) nil
))))
901 (check-important-result node info
)
902 (let ((fun (fun-info-destroyed-constant-args info
)))
904 ;; If somebody is really sure that they want to modify
905 ;; constants, let them.
906 (policy node
(> check-constant-modification
0))
908 (let ((*compiler-error-context
* node
))
909 (warn 'constant-modified
910 :fun-name
(lvar-fun-name
911 (basic-combination-fun node
) t
))
912 (setf (basic-combination-kind node
) :error
)
913 (return-from ir1-optimize-combination
))))
914 (let ((fun (fun-info-derive-type info
)))
916 (let ((res (funcall fun node
)))
918 (derive-node-type node
(coerce-to-values res
))
919 (maybe-terminate-block node nil
)))))))
920 (ecase (basic-combination-kind node
)
922 (let ((fun (combination-lambda node
)))
923 (if (eq (functional-kind fun
) :let
)
924 (propagate-let-args node fun
)
925 (propagate-local-call-args node fun
))))
927 (clear-reoptimize-args))
929 (clear-reoptimize-args)
931 ;; This is a known function marked NOTINLINE
934 ;; Check against the DEFINED-TYPE unless TYPE is already good.
935 (let* ((fun (basic-combination-fun node
))
936 (uses (lvar-uses fun
))
937 (leaf (when (ref-p uses
) (ref-leaf uses
))))
938 (multiple-value-bind (type defined-type
)
939 (if (global-var-p leaf
)
940 (values (leaf-type leaf
) (leaf-defined-type leaf
))
942 (when (and (not (fun-type-p type
)) (fun-type-p defined-type
))
943 (validate-call-type node type leaf
)))))))
946 (clear-reoptimize-args)
948 (let ((attr (fun-info-attributes info
)))
949 (when (constant-fold-call-p node
)
950 (constant-fold-call node
)
951 (return-from ir1-optimize-combination
))
952 (when (and (ir1-attributep attr commutative
)
954 (constant-lvar-p (first args
))
955 (not (constant-lvar-p (second args
))))
956 (setf (basic-combination-args node
) (nreverse args
))))
958 (let ((fun (fun-info-optimizer info
)))
959 (unless (and fun
(funcall fun node
))
960 ;; First give the VM a peek at the call
961 (multiple-value-bind (style transform
)
962 (combination-implementation-style node
)
965 ;; The VM knows how to handle this.
968 ;; The VM mostly knows how to handle this. We need
969 ;; to massage the call slightly, though.
970 (transform-call node transform
(combination-fun-source-name node
)))
972 ;; Let transforms have a crack at it.
973 (dolist (x (fun-info-transforms info
))
975 (when *show-transforms-p
*
976 (let* ((lvar (basic-combination-fun node
))
977 (fname (lvar-fun-name lvar t
)))
978 (/show
"trying transform" x
(transform-function x
) "for" fname
)))
979 (unless (ir1-transform node x
)
981 (when *show-transforms-p
*
982 (/show
"quitting because IR1-TRANSFORM result was NIL"))
986 (defun xep-tail-combination-p (node)
987 (and (combination-p node
)
988 (let* ((lvar (combination-lvar node
))
989 (dest (when (lvar-p lvar
) (lvar-dest lvar
)))
990 (lambda (when (return-p dest
) (return-lambda dest
))))
991 (and (lambda-p lambda
)
992 (eq :external
(lambda-kind lambda
))))))
994 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
995 ;;; the block there, and link it to the component tail.
997 ;;; Except when called during IR1 convertion, we delete the
998 ;;; continuation if it has no other uses. (If it does have other uses,
1001 ;;; Termination on the basis of a continuation type is
1003 ;;; -- The continuation is deleted (hence the assertion is spurious), or
1004 ;;; -- We are in IR1 conversion (where THE assertions are subject to
1005 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
1006 ;;; uses can(?) be added later. -- APD, 2003-07-17
1008 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
1009 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p
)
1010 (declare (type (or basic-combination cast ref
) node
))
1011 (let* ((block (node-block node
))
1012 (lvar (node-lvar node
))
1013 (ctran (node-next node
))
1014 (tail (component-tail (block-component block
)))
1015 (succ (first (block-succ block
))))
1016 (declare (ignore lvar
))
1017 (unless (or (and (eq node
(block-last block
)) (eq succ tail
))
1018 (block-delete-p block
))
1019 ;; Even if the combination will never return, don't terminate if this
1020 ;; is the tail call of a XEP: doing that would inhibit TCO.
1021 (when (and (eq (node-derived-type node
) *empty-type
*)
1022 (not (xep-tail-combination-p node
)))
1023 (cond (ir1-converting-not-optimizing-p
1026 (aver (eq (block-last block
) node
)))
1028 (setf (block-last block
) node
)
1029 (setf (ctran-use ctran
) nil
)
1030 (setf (ctran-kind ctran
) :unused
)
1031 (setf (ctran-block ctran
) nil
)
1032 (setf (node-next node
) nil
)
1033 (link-blocks block
(ctran-starts-block ctran
)))))
1035 (node-ends-block node
)))
1037 (let ((succ (first (block-succ block
))))
1038 (unlink-blocks block succ
)
1039 (setf (component-reanalyze (block-component block
)) t
)
1040 (aver (not (block-succ block
)))
1041 (link-blocks block tail
)
1042 (cond (ir1-converting-not-optimizing-p
1043 (%delete-lvar-use node
))
1044 (t (delete-lvar-use node
)
1045 (when (null (block-pred succ
))
1046 (mark-for-deletion succ
)))))
1049 ;;; This is called both by IR1 conversion and IR1 optimization when
1050 ;;; they have verified the type signature for the call, and are
1051 ;;; wondering if something should be done to special-case the call. If
1052 ;;; CALL is a call to a global function, then see whether it defined
1054 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
1055 ;;; the expansion and change the call to call it. Expansion is
1056 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
1057 ;;; true, we never expand, since this function has already been
1058 ;;; converted. Local call analysis will duplicate the definition
1059 ;;; if necessary. We claim that the parent form is LABELS for
1060 ;;; context declarations, since we don't want it to be considered
1061 ;;; a real global function.
1062 ;;; -- If it is a known function, mark it as such by setting the KIND.
1064 ;;; We return the leaf referenced (NIL if not a leaf) and the
1065 ;;; FUN-INFO assigned.
1066 (defun recognize-known-call (call ir1-converting-not-optimizing-p
)
1067 (declare (type combination call
))
1068 (let* ((ref (lvar-uses (basic-combination-fun call
)))
1069 (leaf (when (ref-p ref
) (ref-leaf ref
)))
1070 (inlinep (if (defined-fun-p leaf
)
1071 (defined-fun-inlinep leaf
)
1074 ((eq inlinep
:notinline
)
1075 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1077 (setf (basic-combination-fun-info call
) info
))
1079 ((not (and (global-var-p leaf
)
1080 (eq (global-var-kind leaf
) :global-function
)))
1082 ((and (ecase inlinep
1085 ((nil :maybe-inline
) (policy call
(zerop space
))))
1086 (defined-fun-p leaf
)
1087 (defined-fun-inline-expansion leaf
)
1088 (inline-expansion-ok call
))
1089 ;; Inline: if the function has already been converted at another call
1090 ;; site in this component, we point this REF to the functional. If not,
1091 ;; we convert the expansion.
1093 ;; For :INLINE case local call analysis will copy the expansion later,
1094 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1095 ;; expansion per component.
1097 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1099 (with-ir1-environment-from-node call
1100 (let ((fun (defined-fun-functional leaf
)))
1102 (and (eq inlinep
:inline
) (functional-kind fun
)))
1104 (let* ((name (leaf-source-name leaf
))
1105 (res (ir1-convert-inline-expansion
1107 (defined-fun-inline-expansion leaf
)
1110 (info :function
:info name
))))
1111 ;; Allow backward references to this function from following
1112 ;; forms. (Reused only if policy matches.)
1113 (push res
(defined-fun-functionals leaf
))
1114 (change-ref-leaf ref res
)
1115 (unless ir1-converting-not-optimizing-p
1116 (locall-analyze-component *current-component
*)))
1117 ;; If we've already converted, change ref to the converted
1119 (change-ref-leaf ref fun
))))
1120 (values (ref-leaf ref
) nil
))
1122 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1126 (setf (basic-combination-kind call
) :known
)
1127 (setf (basic-combination-fun-info call
) info
)))
1128 (values leaf nil
)))))))
1130 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1131 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1132 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1133 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1134 ;;; syntax check, arg/result type processing, but still call
1135 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1136 ;;; and that checking is done by local call analysis.
1137 (defun validate-call-type (call type fun
&optional ir1-converting-not-optimizing-p
)
1138 (declare (type combination call
) (type ctype type
))
1139 (let* ((where (when fun
(leaf-where-from fun
)))
1140 (same-file-p (eq :defined-here where
)))
1141 (cond ((not (fun-type-p type
))
1142 (aver (multiple-value-bind (val win
)
1143 (csubtypep type
(specifier-type 'function
))
1144 (or val
(not win
))))
1145 ;; Using the defined-type too early is a bit of a waste: during
1146 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1147 (when (and fun
(not ir1-converting-not-optimizing-p
))
1148 (let ((defined-type (leaf-defined-type fun
)))
1149 (when (and (fun-type-p defined-type
)
1150 (neq fun
(combination-type-validated-for-leaf call
)))
1151 ;; Don't validate multiple times against the same leaf --
1152 ;; it doesn't add any information, but may generate the same warning
1154 (setf (combination-type-validated-for-leaf call
) fun
)
1155 (when (and (valid-fun-use call defined-type
1156 :argument-test
#'always-subtypep
1158 :lossage-fun
(if same-file-p
1160 #'compiler-style-warn
)
1161 :unwinnage-fun
#'compiler-notify
)
1163 (assert-call-type call defined-type nil
)
1164 (maybe-terminate-block call ir1-converting-not-optimizing-p
)))))
1165 (recognize-known-call call ir1-converting-not-optimizing-p
))
1166 ((let ((*valid-callable-argument-assert-unknown-lvars
* t
))
1167 (valid-fun-use call type
1168 :argument-test
#'always-subtypep
1170 :lossage-fun
#'compiler-warn
1171 :unwinnage-fun
#'compiler-notify
))
1172 (assert-call-type call type
)
1173 (maybe-terminate-block call ir1-converting-not-optimizing-p
)
1174 (recognize-known-call call ir1-converting-not-optimizing-p
))
1176 (setf (combination-kind call
) :error
)
1177 (values nil nil
)))))
1179 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1180 ;;; changed. If the call is local, we try to LET-convert it, and
1181 ;;; derive the result type. If it is a :FULL call, we validate it
1182 ;;; against the type, which recognizes known calls, does inline
1183 ;;; expansion, etc. If a call to a predicate in a non-conditional
1184 ;;; position or to a function with a source transform, then we
1185 ;;; reconvert the form to give IR1 another chance.
1186 (defun propagate-fun-change (call)
1187 (declare (type combination call
))
1188 (let ((*compiler-error-context
* call
)
1189 (fun-lvar (basic-combination-fun call
)))
1190 (setf (lvar-reoptimize fun-lvar
) nil
)
1191 (case (combination-kind call
)
1193 (let ((fun (combination-lambda call
)))
1194 (maybe-let-convert fun
)
1195 (unless (member (functional-kind fun
) '(:let
:assignment
:deleted
))
1196 (derive-node-type call
(tail-set-type (lambda-tail-set fun
))))))
1198 (multiple-value-bind (leaf info
)
1199 (let* ((uses (lvar-uses fun-lvar
))
1200 (leaf (when (ref-p uses
) (ref-leaf uses
))))
1201 (validate-call-type call
(or (lvar-fun-type fun-lvar
)
1202 (lvar-type fun-lvar
)) leaf
))
1203 (cond ((functional-p leaf
)
1204 (convert-call-if-possible
1205 (lvar-uses (basic-combination-fun call
))
1208 ((and (global-var-p leaf
)
1209 (eq (global-var-kind leaf
) :global-function
)
1210 (leaf-has-source-name-p leaf
)
1211 (or (info :function
:source-transform
(leaf-source-name leaf
))
1213 (ir1-attributep (fun-info-attributes info
)
1215 (let ((lvar (node-lvar call
)))
1216 (and lvar
(not (if-p (lvar-dest lvar
))))))))
1217 (let ((name (leaf-source-name leaf
))
1218 (dummies (make-gensym-list
1219 (length (combination-args call
)))))
1220 (transform-call call
1222 (,@(if (symbolp name
)
1226 (leaf-source-name leaf
)
1230 ;;;; known function optimization
1232 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1233 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1234 ;;; replace it, otherwise add a new one.
1235 (defun record-optimization-failure (node transform args
)
1236 (declare (type combination node
) (type transform transform
)
1237 (type (or fun-type list
) args
))
1238 (let* ((table (component-failed-optimizations *component-being-compiled
*))
1239 (found (assoc transform
(gethash node table
))))
1241 (setf (cdr found
) args
)
1242 (push (cons transform args
) (gethash node table
))))
1245 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1246 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1247 ;;; doing the transform for some reason and FLAME is true, then we
1248 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1249 ;;; finalize to pick up. We return true if the transform failed, and
1250 ;;; thus further transformation should be attempted. We return false
1251 ;;; if either the transform succeeded or was aborted.
1252 (defun ir1-transform (node transform
)
1253 (declare (type combination node
) (type transform transform
))
1254 (declare (notinline warn
)) ; See COMPILER-WARN for rationale
1255 (let* ((type (transform-type transform
))
1256 (fun (transform-function transform
))
1257 (constrained (fun-type-p type
))
1258 (table (component-failed-optimizations *component-being-compiled
*))
1259 (flame (case (transform-important transform
)
1260 ((t) (policy node
(>= speed inhibit-warnings
)))
1261 (:slightly
(policy node
(> speed inhibit-warnings
)))))
1262 (*compiler-error-context
* node
)
1263 (policy-test (transform-policy transform
)))
1264 (cond ((and policy-test
1265 (not (funcall policy-test node
))))
1266 ((or (not constrained
)
1267 (valid-fun-use node type
))
1268 (multiple-value-bind (severity args
)
1269 (catch 'give-up-ir1-transform
1270 (transform-call node
1272 (combination-fun-source-name node
))
1276 (remhash node table
)
1279 (setf (combination-kind node
) :error
)
1281 (apply #'warn args
))
1282 (remhash node table
)
1287 (record-optimization-failure node transform args
))
1288 (setf (gethash node table
)
1289 (remove transform
(gethash node table
) :key
#'car
)))
1292 (remhash node table
)
1297 :argument-test
#'types-equal-or-intersect
1298 :result-test
#'values-types-equal-or-intersect
))
1299 (record-optimization-failure node transform type
)
1304 ;;; When we don't like an IR1 transform, we throw the severity/reason
1307 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1308 ;;; aborting this attempt to transform the call, but admitting the
1309 ;;; possibility that this or some other transform will later succeed.
1310 ;;; If arguments are supplied, they are format arguments for an
1311 ;;; efficiency note.
1313 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1314 ;;; force a normal call to the function at run time. No further
1315 ;;; optimizations will be attempted.
1317 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1318 ;;; delay the transform on the node until later. REASONS specifies
1319 ;;; when the transform will be later retried. The :OPTIMIZE reason
1320 ;;; causes the transform to be delayed until after the current IR1
1321 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1322 ;;; be delayed until after constraint propagation.
1324 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1325 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1326 ;;; do CASE operations on the various REASON values, it might be a
1327 ;;; good idea to go OO, representing the reasons by objects, using
1328 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1329 ;;; SIGNAL instead of THROW.
1330 (declaim (ftype (function (&rest t
) #+(and sb-xc-host ccl
) *
1331 #-
(and sb-xc-host ccl
) nil
) give-up-ir1-transform
))
1332 (defun give-up-ir1-transform (&rest args
)
1333 (throw 'give-up-ir1-transform
(values :failure args
)))
1334 (defun abort-ir1-transform (&rest args
)
1335 (throw 'give-up-ir1-transform
(values :aborted args
)))
1336 (defun delay-ir1-transform (node &rest reasons
)
1337 (let ((assoc (assoc node
*delayed-ir1-transforms
*)))
1339 (setf *delayed-ir1-transforms
*
1340 (acons node reasons
*delayed-ir1-transforms
*))
1341 (throw 'give-up-ir1-transform
:delayed
))
1343 (dolist (reason reasons
)
1344 (pushnew reason
(cdr assoc
)))
1345 (throw 'give-up-ir1-transform
:delayed
)))))
1347 ;;; Poor man's catching and resignalling
1348 ;;; Implicit %GIVE-UP macrolet will resignal the give-up "condition"
1349 (defmacro catch-give-up-ir1-transform
((form &optional args
) &body gave-up-body
)
1350 (let ((block (gensym "BLOCK"))
1351 (kind (gensym "KIND"))
1352 (args (or args
(gensym "ARGS"))))
1354 (multiple-value-bind (,kind
,args
)
1355 (catch 'give-up-ir1-transform
1356 (return-from ,block
,form
))
1359 (throw 'give-up-ir1-transform
:delayed
))
1360 ((:failure
:aborted
)
1361 (macrolet ((%give-up
()
1362 `(throw 'give-up-ir1-transform
(values ,',kind
1364 ,@gave-up-body
)))))))
1366 ;;; Clear any delayed transform with no reasons - these should have
1367 ;;; been tried in the last pass. Then remove the reason from the
1368 ;;; delayed transform reasons, and if any become empty then set
1369 ;;; reoptimize flags for the node. Return true if any transforms are
1371 (defun retry-delayed-ir1-transforms (reason)
1372 (setf *delayed-ir1-transforms
*
1373 (remove-if-not #'cdr
*delayed-ir1-transforms
*))
1374 (let ((reoptimize nil
))
1375 (dolist (assoc *delayed-ir1-transforms
*)
1376 (let ((reasons (remove reason
(cdr assoc
))))
1377 (setf (cdr assoc
) reasons
)
1379 (let ((node (car assoc
)))
1380 (unless (node-deleted node
)
1382 (setf (node-reoptimize node
) t
)
1383 (let ((block (node-block node
)))
1384 (setf (block-reoptimize block
) t
)
1385 (reoptimize-component (block-component block
) :maybe
)))))))
1388 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1389 ;;; environment, and then install it as the function for the call
1390 ;;; NODE. We do local call analysis so that the new function is
1391 ;;; integrated into the control flow.
1393 ;;; We require the original function source name in order to generate
1394 ;;; a meaningful debug name for the lambda we set up. (It'd be
1395 ;;; possible to do this starting from debug names as well as source
1396 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1397 ;;; generality, since source names are always known to our callers.)
1398 (defun transform-call (call res source-name
&optional
(reoptimize-combination t
))
1399 (declare (type combination call
) (list res
))
1400 (aver (and (legal-fun-name-p source-name
)
1401 (not (eql source-name
'.anonymous.
))))
1402 (node-ends-block call
)
1403 (setf (combination-lexenv call
)
1404 (make-lexenv :default
(combination-lexenv call
)
1406 ;; The internal variables of a transform are not going to be
1407 ;; interesting to the debugger, so there's no sense in
1408 ;; suppressing the substitution of variables with only one use
1409 ;; (the extra variables can slow down constraint propagation).
1411 preserve-single-use-debug-variables
1414 (combination-lexenv call
)))))
1415 (with-ir1-environment-from-node call
1416 (with-component-last-block (*current-component
*
1417 (block-next (node-block call
)))
1418 (let ((new-fun (ir1-convert-inline-lambda
1420 :debug-name
(debug-name 'lambda-inlined source-name
)
1422 (type (node-derived-type call
))
1423 (ref (lvar-use (combination-fun call
))))
1424 (change-ref-leaf ref new-fun
)
1425 (setf (combination-kind call
) :full
)
1426 ;; Don't lose the original derived type
1427 (let ((return (lambda-return (main-entry new-fun
))))
1429 (do-uses (node (return-result
1430 (lambda-return (main-entry new-fun
))))
1431 (derive-node-type node type
))))
1433 (locall-analyze-component *current-component
*)
1434 (when reoptimize-combination
1435 ;; This is mainly to call PROPAGATE-LET-ARGS so that the
1436 ;; newly converted code gets to better types sooner.
1437 (setf (node-reoptimize call
) nil
)
1438 (ir1-optimize-combination call
)))))
1441 (defun constant-fold-arg-p (name)
1446 (let* ((info (info :function
:info name
))
1447 (attributes (and info
1448 (fun-info-attributes info
))))
1450 (ir1-attributep attributes foldable
)
1451 (not (ir1-attributep attributes call
)))))))
1453 ;;; Return T if the function is foldable and if it's marked as CALL
1454 ;;; all function arguments are FOLDABLE too.
1455 (defun constant-fold-call-p (combination)
1456 (let* ((info (basic-combination-fun-info combination
))
1457 (attr (fun-info-attributes info
))
1458 (args (basic-combination-args combination
)))
1459 (cond ((not (ir1-attributep attr foldable
))
1461 ((ir1-attributep attr call
)
1462 (and (every (lambda (lvar)
1463 (or (lvar-fun-name lvar t
)
1464 (constant-lvar-p lvar
)))
1466 (map-callable-arguments
1467 (lambda (lvar &key
&allow-other-keys
)
1468 (constant-fold-arg-p (or (lvar-fun-name lvar t
)
1469 (lvar-value lvar
))))
1472 (every #'constant-lvar-p args
)))))
1474 ;;; Replace a call to a foldable function of constant arguments with
1475 ;;; the result of evaluating the form. If there is an error during the
1476 ;;; evaluation, we give a warning and leave the call alone, making the
1477 ;;; call a :ERROR call.
1479 ;;; If there is more than one value, then we transform the call into a
1481 (defun constant-fold-call (call)
1482 (let ((args (mapcar (lambda (lvar)
1483 (let ((name (lvar-fun-name lvar t
)))
1486 (lvar-value lvar
))))
1487 (combination-args call
)))
1488 (fun-name (combination-fun-source-name call
)))
1489 (multiple-value-bind (values win
)
1490 (careful-call fun-name
1493 ;; Note: CMU CL had COMPILER-WARN here, and that
1494 ;; seems more natural, but it's probably not.
1496 ;; It's especially not while bug 173 exists:
1499 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1501 ;; can cause constant-folding TYPE-ERRORs (in
1502 ;; #'<=) when END can be proved to be NIL, even
1503 ;; though the code is perfectly legal and safe
1504 ;; because a NIL value of END means that the
1505 ;; #'<= will never be executed.
1507 ;; Moreover, even without bug 173,
1508 ;; quite-possibly-valid code like
1509 ;; (COND ((NONINLINED-PREDICATE END)
1510 ;; (UNLESS (<= END SIZE))
1512 ;; (where NONINLINED-PREDICATE is something the
1513 ;; compiler can't do at compile time, but which
1514 ;; turns out to make the #'<= expression
1515 ;; unreachable when END=NIL) could cause errors
1516 ;; when the compiler tries to constant-fold (<=
1519 ;; So, with or without bug 173, it'd be
1520 ;; unnecessarily evil to do a full
1521 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1522 ;; from COMPILE-FILE) for legal code, so we we
1523 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1524 #-sb-xc-host
#'compiler-style-warn
1525 ;; On the other hand, for code we control, we
1526 ;; should be able to work around any bug
1527 ;; 173-related problems, and in particular we
1528 ;; want to be alerted to calls to our own
1529 ;; functions which aren't being folded away; a
1530 ;; COMPILER-WARNING is butch enough to stop the
1531 ;; SBCL build itself in its tracks.
1532 #+sb-xc-host
#'compiler-warn
1535 (setf (combination-kind call
) :error
))
1536 ((and (proper-list-of-length-p values
1))
1537 (with-ir1-environment-from-node call
1538 (let* ((lvar (node-lvar call
))
1539 (prev (node-prev call
))
1540 (intermediate-ctran (make-ctran)))
1541 (%delete-lvar-use call
)
1542 (setf (ctran-next prev
) nil
)
1543 (setf (node-prev call
) nil
)
1544 (reference-constant prev intermediate-ctran lvar
1546 (link-node-to-previous-ctran call intermediate-ctran
)
1547 (reoptimize-lvar lvar
)
1548 (flush-combination call
))))
1549 (t (let ((dummies (make-gensym-list (length args
))))
1553 (declare (ignore ,@dummies
))
1554 (values ,@(mapcar (lambda (x) `',x
) values
)))
1558 ;;;; local call optimization
1560 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1562 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1563 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1566 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1567 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1568 (defun propagate-to-refs (leaf type
)
1569 (declare (type leaf leaf
) (type ctype type
))
1570 (let ((var-type (leaf-type leaf
))
1571 (refs (leaf-refs leaf
)))
1572 (unless (or (fun-type-p var-type
)
1574 (eq :declared
(leaf-where-from leaf
))
1575 (type-needs-conservation-p var-type
)))
1576 (let ((int (type-approx-intersection2 var-type type
)))
1577 (when (type/= int var-type
)
1578 (setf (leaf-type leaf
) int
)
1579 (let ((s-int (make-single-value-type int
)))
1581 (derive-node-type ref s-int
)
1582 ;; KLUDGE: LET var substitution
1583 (let* ((lvar (node-lvar ref
)))
1584 (when (and lvar
(combination-p (lvar-dest lvar
)))
1585 (reoptimize-lvar lvar
)))))))
1588 ;;; Iteration variable: exactly one SETQ of the form:
1590 ;;; (let ((var initial))
1592 ;;; (setq var (+ var step))
1594 (defun maybe-infer-iteration-var-type (var initial-type
)
1595 (binding* ((sets (lambda-var-sets var
) :exit-if-null
)
1597 (() (null (rest sets
)) :exit-if-null
)
1598 (set-use (principal-lvar-use (set-value set
)))
1599 (() (and (combination-p set-use
)
1600 (eq (combination-kind set-use
) :known
)
1601 (fun-info-p (combination-fun-info set-use
))
1602 (not (node-to-be-deleted-p set-use
))
1603 (or (eq (combination-fun-source-name set-use
) '+)
1604 (eq (combination-fun-source-name set-use
) '-
)))
1606 (minusp (eq (combination-fun-source-name set-use
) '-
))
1607 (+-args
(basic-combination-args set-use
))
1608 (() (and (proper-list-of-length-p +-args
2 2)
1609 (let ((first (principal-lvar-use
1612 (eq (ref-leaf first
) var
))))
1614 (step-type (lvar-type (second +-args
)))
1615 (set-type (lvar-type (set-value set
))))
1616 (when (and (numeric-type-p initial-type
)
1617 (numeric-type-p step-type
)
1618 (or (numeric-type-equal initial-type step-type
)
1619 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1620 ;; the initial and the step are of different types,
1621 ;; and the step is less contagious.
1622 (numeric-type-equal initial-type
1623 (numeric-contagion initial-type
1625 (labels ((leftmost (x y cmp cmp
=)
1626 (cond ((eq x nil
) nil
)
1629 (let ((x1 (first x
)))
1631 (let ((y1 (first y
)))
1632 (if (funcall cmp x1 y1
) x y
)))
1634 (if (funcall cmp x1 y
) x y
)))))
1636 (let ((y1 (first y
)))
1637 (if (funcall cmp
= x y1
) x y
)))
1638 (t (if (funcall cmp x y
) x y
))))
1639 (max* (x y
) (leftmost x y
#'> #'>=))
1640 (min* (x y
) (leftmost x y
#'< #'<=)))
1641 (multiple-value-bind (low high
)
1642 (let ((step-type-non-negative (csubtypep step-type
(specifier-type
1644 (step-type-non-positive (csubtypep step-type
(specifier-type
1646 (cond ((or (and step-type-non-negative
(not minusp
))
1647 (and step-type-non-positive minusp
))
1648 (values (numeric-type-low initial-type
)
1649 (when (and (numeric-type-p set-type
)
1650 (numeric-type-equal set-type initial-type
))
1651 (max* (numeric-type-high initial-type
)
1652 (numeric-type-high set-type
)))))
1653 ((or (and step-type-non-positive
(not minusp
))
1654 (and step-type-non-negative minusp
))
1655 (values (when (and (numeric-type-p set-type
)
1656 (numeric-type-equal set-type initial-type
))
1657 (min* (numeric-type-low initial-type
)
1658 (numeric-type-low set-type
)))
1659 (numeric-type-high initial-type
)))
1662 (modified-numeric-type initial-type
1665 :enumerable nil
))))))
1666 (deftransform + ((x y
) * * :result result
)
1667 "check for iteration variable reoptimization"
1668 (let ((dest (principal-lvar-end result
))
1669 (use (principal-lvar-use x
)))
1670 (when (and (ref-p use
)
1674 (reoptimize-lvar (set-value dest
))))
1675 (give-up-ir1-transform))
1677 ;;; Figure out the type of a LET variable that has sets. We compute
1678 ;;; the union of the INITIAL-TYPE and the types of all the set
1679 ;;; values and to a PROPAGATE-TO-REFS with this type.
1680 (defun propagate-from-sets (var initial-type
)
1681 (let ((changes (not (csubtypep (lambda-var-last-initial-type var
) initial-type
)))
1683 (dolist (set (lambda-var-sets var
))
1684 (let ((type (lvar-type (set-value set
))))
1686 (when (node-reoptimize set
)
1687 (let ((old-type (node-derived-type set
)))
1688 (unless (values-subtypep old-type type
)
1689 (derive-node-type set
(make-single-value-type type
))
1691 (setf (node-reoptimize set
) nil
))))
1693 (setf (lambda-var-last-initial-type var
) initial-type
)
1694 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type
)
1695 (apply #'type-union initial-type types
))))
1696 (propagate-to-refs var res-type
))))
1699 ;;; If a LET variable, find the initial value's type and do
1700 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1702 (defun ir1-optimize-set (node)
1703 (declare (type cset node
))
1704 (let ((var (set-var node
)))
1705 (when (and (lambda-var-p var
) (leaf-refs var
))
1706 (let ((home (lambda-var-home var
)))
1707 (when (eq (functional-kind home
) :let
)
1708 (let* ((initial-value (let-var-initial-value var
))
1709 (initial-type (lvar-type initial-value
)))
1710 (setf (lvar-reoptimize initial-value
) nil
)
1711 (propagate-from-sets var initial-type
))))))
1712 (derive-node-type node
(make-single-value-type
1713 (lvar-type (set-value node
))))
1714 (setf (node-reoptimize node
) nil
)
1717 ;;; Return true if the value of REF will always be the same (and is
1718 ;;; thus legal to substitute.)
1719 (defun constant-reference-p (ref)
1720 (declare (type ref ref
))
1721 (let ((leaf (ref-leaf ref
)))
1723 ((or constant functional
) t
)
1725 (null (lambda-var-sets leaf
)))
1727 (not (eq (defined-fun-inlinep leaf
) :notinline
)))
1729 (case (global-var-kind leaf
)
1731 (let ((name (leaf-source-name leaf
)))
1733 (eq (symbol-package (fun-name-block-name name
))
1735 (info :function
:info name
)))))))))
1737 ;;; If we have a non-set LET var with a single use, then (if possible)
1738 ;;; replace the variable reference's LVAR with the arg lvar.
1740 ;;; We change the REF to be a reference to NIL with unused value, and
1741 ;;; let it be flushed as dead code. A side effect of this substitution
1742 ;;; is to delete the variable.
1743 (defun substitute-single-use-lvar (arg var
)
1744 (declare (type lvar arg
) (type lambda-var var
))
1745 (binding* ((ref (first (leaf-refs var
)))
1746 (lvar (node-lvar ref
) :exit-if-null
)
1747 (dest (lvar-dest lvar
))
1748 (dest-lvar (when (valued-node-p dest
) (node-lvar dest
))))
1750 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1751 ;; LVAR-USEs should not be met on one path. Another problem
1752 ;; is with dynamic-extent.
1753 (eq (lvar-uses lvar
) ref
)
1754 (not (block-delete-p (node-block ref
)))
1755 ;; If the destinatation is dynamic extent, don't substitute unless
1756 ;; the source is as well.
1758 (not (lvar-dynamic-extent dest-lvar
))
1759 (lvar-dynamic-extent lvar
))
1761 ;; we should not change lifetime of unknown values lvars
1763 (and (type-single-value-p (lvar-derived-type arg
))
1764 (multiple-value-bind (pdest pprev
)
1765 (principal-lvar-end lvar
)
1766 (declare (ignore pdest
))
1767 (lvar-single-value-p pprev
))
1768 ;; CASTs can disappear, don't substitute if
1769 ;; DEST-LVAR has other uses (this will be
1770 ;; insufficient if we have a CAST-CAST chain, but
1771 ;; works well for a single CAST)
1772 (or (null dest-lvar
)
1773 (atom (lvar-uses dest-lvar
)))))
1775 (or (eq (basic-combination-fun dest
) lvar
)
1776 (and (eq (basic-combination-kind dest
) :local
)
1777 (type-single-value-p (lvar-derived-type arg
)))))
1779 ;; While CRETURN and EXIT nodes may be known-values,
1780 ;; they have their own complications, such as
1781 ;; substitution into CRETURN may create new tail calls.
1784 (aver (lvar-single-value-p lvar
))
1786 (eq (node-home-lambda ref
)
1787 (lambda-home (lambda-var-home var
))))
1788 (let ((ref-type (single-value-type (node-derived-type ref
))))
1789 (cond ((csubtypep (single-value-type (lvar-type arg
)) ref-type
)
1790 (substitute-lvar-uses lvar arg
1791 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1793 (delete-lvar-use ref
))
1795 (let* ((value (make-lvar))
1796 (cast (insert-cast-before ref value ref-type
1797 ;; KLUDGE: it should be (TYPE-CHECK 0)
1799 (setf (cast-type-to-check cast
) *wild-type
*)
1800 (substitute-lvar-uses value arg
1803 (%delete-lvar-use ref
)
1804 (add-lvar-use cast lvar
)))))
1805 (setf (node-derived-type ref
) *wild-type
*)
1806 (change-ref-leaf ref
(find-constant nil
))
1809 (reoptimize-lvar lvar
)
1812 ;;; Delete a LET, removing the call and bind nodes, and warning about
1813 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1814 ;;; along right away and delete the REF and then the lambda, since we
1815 ;;; flush the FUN lvar.
1816 (defun delete-let (clambda)
1817 (declare (type clambda clambda
))
1818 (aver (functional-letlike-p clambda
))
1819 (note-unreferenced-fun-vars clambda
)
1820 (let ((call (let-combination clambda
)))
1821 (flush-dest (basic-combination-fun call
))
1823 (unlink-node (lambda-bind clambda
))
1824 (setf (lambda-bind clambda
) nil
))
1825 (setf (functional-kind clambda
) :zombie
)
1826 (let ((home (lambda-home clambda
)))
1827 (setf (lambda-lets home
) (delete clambda
(lambda-lets home
))))
1830 ;;; This function is called when one of the arguments to a LET
1831 ;;; changes. We look at each changed argument. If the corresponding
1832 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1833 ;;; consider substituting for the variable, and also propagate
1834 ;;; derived-type information for the arg to all the VAR's refs.
1836 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1837 ;;; subtype of the argument's leaf type. This prevents type checking
1838 ;;; from being defeated, and also ensures that the best representation
1839 ;;; for the variable can be used.
1841 ;;; Substitution of individual references is inhibited if the
1842 ;;; reference is in a different component from the home. This can only
1843 ;;; happen with closures over top level lambda vars. In such cases,
1844 ;;; the references may have already been compiled, and thus can't be
1845 ;;; retroactively modified.
1847 ;;; If all of the variables are deleted (have no references) when we
1848 ;;; are done, then we delete the LET.
1850 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1852 (defun propagate-let-args (call fun
)
1853 (declare (type basic-combination call
) (type clambda fun
))
1854 (map-combination-arg-var
1855 (lambda (arg var type
)
1857 ((lambda-var-deleted var
))
1858 ((lambda-var-sets var
)
1859 (propagate-from-sets var type
))
1861 (let ((use (lvar-uses arg
)))
1863 (let ((leaf (ref-leaf use
)))
1864 (when (and (constant-reference-p use
)
1865 (csubtypep (leaf-type leaf
)
1866 ;; (NODE-DERIVED-TYPE USE) would
1867 ;; be better -- APD, 2003-05-15
1869 (propagate-to-refs var type
)
1870 (unless (preserve-single-use-debug-var-p call var
)
1871 (let ((use-component (node-component use
)))
1874 (cond ((eq (node-component ref
) use-component
)
1877 (aver (lambda-toplevelish-p (lambda-home fun
)))
1882 (null (rest (leaf-refs var
)))
1883 (not (preserve-single-use-debug-var-p call var
))
1884 (substitute-single-use-lvar arg var
)))
1886 (propagate-to-refs var type
))))
1890 (when (every #'not
(basic-combination-args call
))
1895 ;;; This function is called when one of the args to a non-LET local
1896 ;;; call changes. For each changed argument corresponding to an unset
1897 ;;; variable, we compute the union of the types across all calls and
1898 ;;; propagate this type information to the var's refs.
1900 ;;; If the function has an entry-fun, then we don't do anything: since
1901 ;;; it has a XEP we would not discover anything.
1903 ;;; If the function is an optional-entry-point, we will just make sure
1904 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1905 ;;; can erronously propagate too strict types into refs: see
1906 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1908 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1909 ;;; corresponding to changed arguments in CALL, since the only use in
1910 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1912 (defun propagate-local-call-args (call fun
)
1913 (declare (type combination call
) (type clambda fun
))
1914 (unless (functional-entry-fun fun
)
1915 (if (lambda-optional-dispatch fun
)
1916 ;; We can still make sure &REST is known to be a list.
1917 (loop for var in
(lambda-vars fun
)
1918 do
(let ((info (lambda-var-arg-info var
)))
1919 (when (and info
(eq :rest
(arg-info-kind info
)))
1920 (propagate-from-sets var
(specifier-type 'list
)))))
1922 (let* ((vars (lambda-vars fun
))
1923 (union (mapcar (lambda (arg var
)
1925 (lvar-reoptimize arg
)
1926 (null (basic-var-sets var
)))
1928 (basic-combination-args call
)
1930 (this-ref (lvar-use (basic-combination-fun call
))))
1932 (dolist (arg (basic-combination-args call
))
1934 (setf (lvar-reoptimize arg
) nil
)))
1936 (dolist (ref (leaf-refs fun
))
1937 (let ((dest (node-dest ref
)))
1938 (unless (or (eq ref this-ref
) (not dest
))
1940 (mapcar (lambda (this-arg old
)
1942 (setf (lvar-reoptimize this-arg
) nil
)
1943 (type-union (lvar-type this-arg
) old
)))
1944 (basic-combination-args dest
)
1947 (loop for var in vars
1949 when type do
(propagate-to-refs var type
)))))
1953 ;;;; multiple values optimization
1955 ;;; Do stuff to notice a change to a MV combination node. There are
1956 ;;; two main branches here:
1957 ;;; -- If the call is local, then it is already a MV let, or should
1958 ;;; become one. Note that although all :LOCAL MV calls must eventually
1959 ;;; be converted to :MV-LETs, there can be a window when the call
1960 ;;; is local, but has not been LET converted yet. This is because
1961 ;;; the entry-point lambdas may have stray references (in other
1962 ;;; entry points) that have not been deleted yet.
1963 ;;; -- The call is full. This case is somewhat similar to the non-MV
1964 ;;; combination optimization: we propagate return type information and
1965 ;;; notice non-returning calls. We also have an optimization
1966 ;;; which tries to convert MV-CALLs into MV-binds.
1967 (defun ir1-optimize-mv-combination (node)
1968 (let ((fun (basic-combination-fun node
)))
1969 (unless (and (node-p (lvar-uses fun
))
1970 (node-to-be-deleted-p (lvar-uses fun
)))
1971 (ecase (basic-combination-kind node
)
1973 (let ((lambda (combination-lambda node
)))
1974 (when (lvar-reoptimize fun
)
1975 (setf (lvar-reoptimize fun
) nil
)
1976 (maybe-let-convert lambda
))
1977 (cond ((neq (functional-kind lambda
) :mv-let
)
1978 (loop for arg in
(basic-combination-args node
)
1980 (setf (lvar-reoptimize arg
) nil
)))
1981 ((convert-mv-bind-to-let node
))
1983 (propagate-let-args node lambda
)))))
1985 (let* ((fun-changed (lvar-reoptimize fun
)))
1986 (loop for arg in
(basic-combination-args node
)
1988 (setf (lvar-reoptimize arg
) nil
))
1990 (setf (lvar-reoptimize fun
) nil
)
1991 (let ((type (lvar-type fun
)))
1992 (when (fun-type-p type
)
1993 (derive-node-type node
(fun-type-returns type
))))
1994 (maybe-terminate-block node nil
)
1995 (let ((use (lvar-uses fun
)))
1996 (when (and (ref-p use
) (functional-p (ref-leaf use
)))
1997 (convert-call-if-possible use node
)
1998 (when (eq (basic-combination-kind node
) :local
)
1999 (maybe-let-convert (ref-leaf use
))))))
2000 (unless (or (eq (basic-combination-kind node
) :local
)
2001 (eq (lvar-fun-name fun
) '%throw
))
2002 (ir1-optimize-mv-call node
))))
2007 (defun ir1-optimize-mv-call (node)
2008 (let ((fun (basic-combination-fun node
))
2009 (*compiler-error-context
* node
)
2010 (ref (lvar-uses (basic-combination-fun node
)))
2011 (args (basic-combination-args node
)))
2013 (multiple-value-bind (min max
) (fun-type-nargs (lvar-type fun
))
2015 (loop for arg in args
2016 for nvals
= (nth-value 1 (values-types (lvar-derived-type arg
)))
2017 when
(eq nvals
:unknown
) return nil
2020 (when (and min
(< total-nvals min
))
2022 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2025 (setf (basic-combination-kind node
) :error
)
2026 (return-from ir1-optimize-mv-call
))
2027 (when (and max
(> total-nvals max
))
2029 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2032 (setf (basic-combination-kind node
) :error
)
2033 (return-from ir1-optimize-mv-call
)))
2034 (let ((count (cond (total-nvals)
2035 ((and (policy node
(zerop verify-arg-count
))
2040 (with-ir1-environment-from-node node
2041 (let* ((dums (make-gensym-list count
))
2043 (leaf (ref-leaf ref
))
2044 (fun (ir1-convert-lambda
2045 `(lambda (&optional
,@dums
&rest
,ignore
)
2046 (declare (ignore ,ignore
))
2047 (%funcall
,leaf
,@dums
))
2048 :debug-name
(leaf-%debug-name leaf
))))
2049 (change-ref-leaf ref fun
)
2050 (aver (eq (basic-combination-kind node
) :full
))
2051 (locall-analyze-component *current-component
*)
2052 (aver (eq (basic-combination-kind node
) :local
))))))))))
2056 ;;; (multiple-value-bind
2065 ;;; What we actually do is convert the VALUES combination into a
2066 ;;; normal LET combination calling the original :MV-LET lambda. If
2067 ;;; there are extra args to VALUES, discard the corresponding
2068 ;;; lvars. If there are insufficient args, insert references to NIL.
2069 (defun convert-mv-bind-to-let (call)
2070 (declare (type mv-combination call
))
2071 (let* ((args (basic-combination-args call
))
2072 (use (lvar-uses (first args
))))
2073 (when (and (singleton-p args
)
2075 (eq (lvar-fun-name (combination-fun use
))
2077 (setf (lvar-reoptimize (car args
)) nil
)
2078 (let* ((fun (combination-lambda call
))
2079 (vars (lambda-vars fun
))
2080 (vals (combination-args use
))
2081 (nvars (length vars
))
2082 (nvals (length vals
)))
2083 (cond ((> nvals nvars
)
2084 (mapc #'flush-dest
(subseq vals nvars
))
2085 (setq vals
(subseq vals
0 nvars
)))
2087 (with-ir1-environment-from-node use
2088 (let ((node-prev (node-prev use
)))
2089 (setf (node-prev use
) nil
)
2090 (setf (ctran-next node-prev
) nil
)
2091 (collect ((res vals
))
2092 (loop for count below
(- nvars nvals
)
2093 for prev
= node-prev then ctran
2094 for ctran
= (make-ctran)
2095 and lvar
= (make-lvar use
)
2096 do
(reference-constant prev ctran lvar nil
)
2098 finally
(link-node-to-previous-ctran
2100 (setq vals
(res)))))))
2101 (setf (combination-args use
) vals
)
2102 (flush-dest (combination-fun use
))
2103 (let ((fun-lvar (basic-combination-fun call
)))
2104 (setf (lvar-dest fun-lvar
) use
)
2105 (setf (combination-fun use
) fun-lvar
)
2106 (flush-lvar-externally-checkable-type fun-lvar
))
2107 (setf (combination-kind use
) :local
)
2108 (setf (functional-kind fun
) :let
)
2109 (flush-dest (first (basic-combination-args call
)))
2112 (reoptimize-lvar (first vals
)))
2113 ;; Propagate derived types from the VALUES call to its args:
2114 ;; transforms can leave the VALUES call with a better type
2115 ;; than its args have, so make sure not to throw that away.
2116 (let ((types (values-type-types (node-derived-type use
))))
2119 (let ((type (pop types
)))
2120 (assert-lvar-type val type
**zero-typecheck-policy
**)))))
2121 ;; Propagate declared types of MV-BIND variables.
2122 (propagate-to-args use fun
)
2123 (reoptimize-call use
))
2127 ;;; (values-list (list x y z))
2132 ;;; In implementation, this is somewhat similar to
2133 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2134 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2135 ;;; (allowing the LIST to be flushed.)
2137 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2138 (defoptimizer (values-list optimizer
) ((list) node
)
2139 (let ((use (lvar-uses list
)))
2140 (when (and (combination-p use
)
2141 (eq (lvar-fun-name (combination-fun use
))
2144 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2145 (change-ref-leaf (lvar-uses (combination-fun node
))
2146 (find-free-fun 'values
"in a strange place"))
2147 (setf (combination-kind node
) :full
)
2148 (let ((args (combination-args use
)))
2150 (setf (lvar-dest arg
) node
)
2151 (flush-lvar-externally-checkable-type arg
))
2152 (setf (combination-args use
) nil
)
2154 (flush-combination use
)
2155 (setf (combination-args node
) args
))
2158 ;;; If VALUES appears in a non-MV context, then effectively convert it
2159 ;;; to a PROG1. This allows the computation of the additional values
2160 ;;; to become dead code.
2161 (deftransform values
((&rest vals
) * * :node node
)
2162 (unless (lvar-single-value-p (node-lvar node
))
2163 (give-up-ir1-transform))
2164 (setf (node-derived-type node
)
2165 (make-short-values-type (list (single-value-type
2166 (node-derived-type node
)))))
2167 (principal-lvar-single-valuify (node-lvar node
))
2169 (let ((dummies (make-gensym-list (length (cdr vals
)))))
2170 `(lambda (val ,@dummies
)
2171 (declare (ignore ,@dummies
))
2177 (defun delete-cast (cast)
2178 (declare (type cast cast
))
2179 (let ((value (cast-value cast
))
2180 (lvar (cast-lvar cast
)))
2181 (when (and (bound-cast-p cast
)
2182 (bound-cast-check cast
))
2183 (flush-combination (bound-cast-check cast
))
2184 (setf (bound-cast-check cast
) nil
))
2185 (delete-filter cast lvar value
)
2187 (reoptimize-lvar lvar
)
2188 (when (lvar-single-value-p lvar
)
2189 (note-single-valuified-lvar lvar
)))
2192 (defun may-delete-vestigial-exit (cast)
2193 ;; VESTIGIAL-EXIT-CASTs come from MULTIPLE-VALUES-PROG1 to avoid
2194 ;; overwriting their lvars
2195 (not (vestigial-exit-cast-p cast
)))
2197 (defun compile-time-type-error-context (context)
2198 #+sb-xc-host context
2199 #-sb-xc-host
(source-to-string context
))
2201 (defun ir1-optimize-cast (cast &optional do-not-optimize
)
2202 (declare (type cast cast
))
2203 (let ((value (cast-value cast
))
2204 (atype (cast-asserted-type cast
)))
2205 (unless (or do-not-optimize
2206 (not (may-delete-vestigial-exit cast
)))
2207 (when (and (bound-cast-p cast
)
2208 (bound-cast-check cast
)
2209 (constant-lvar-p (bound-cast-bound cast
)))
2211 (specifier-type `(integer 0 (,(lvar-value (bound-cast-bound cast
)))))
2212 (cast-asserted-type cast
) atype
2213 (bound-cast-derived cast
) t
))
2214 (let ((lvar (node-lvar cast
)))
2215 (when (and (or (not (bound-cast-p cast
))
2216 (bound-cast-derived cast
))
2217 (not (function-designator-cast-p cast
))
2218 (values-subtypep (lvar-derived-type value
)
2219 (cast-asserted-type cast
)))
2221 (return-from ir1-optimize-cast t
))
2223 (when (and (listp (lvar-uses value
))
2225 ;; Pathwise removing of CAST
2226 (let ((ctran (node-next cast
))
2227 (dest (lvar-dest lvar
))
2230 (do-uses (use value
)
2231 (when (and (values-subtypep (node-derived-type use
) atype
)
2232 (immediately-used-p value use
))
2234 (when ctran
(ensure-block-start ctran
))
2235 (setq next-block
(first (block-succ (node-block cast
))))
2236 (ensure-block-start (node-prev cast
))
2237 (reoptimize-lvar lvar
)
2238 (setf (lvar-%derived-type value
) nil
))
2239 (%delete-lvar-use use
)
2240 (add-lvar-use use lvar
)
2241 (unlink-blocks (node-block use
) (node-block cast
))
2242 (link-blocks (node-block use
) next-block
)
2243 (when (and (return-p dest
)
2244 (basic-combination-p use
)
2245 (eq (basic-combination-kind use
) :local
))
2247 (dolist (use (merges))
2248 (merge-tail-sets use
))))))
2250 (when (and (bound-cast-p cast
)
2251 (bound-cast-check cast
)
2252 (policy cast
(= insert-array-bounds-checks
0)))
2253 (flush-combination (bound-cast-check cast
))
2254 (setf (bound-cast-check cast
) nil
)))
2256 (let* ((value-type (lvar-derived-type value
))
2257 (int (values-type-intersection value-type atype
)))
2258 (derive-node-type cast int
)
2260 (neq int
*empty-type
*)
2261 (eq value-type
*empty-type
*)))
2262 ;; No need to transform into an analog of
2263 ;; %COMPILE-TIME-TYPE-ERROR, %CHECK-BOUND will signal at
2264 ;; run-time and %CHECK-BOUND ir2-converter will signal at
2265 ;; compile-time if it survives further stages of ir1
2267 ((bound-cast-p cast
))
2269 ;; FIXME: Do it in one step.
2270 (let ((context (node-source-form cast
))
2271 (detail (lvar-all-sources (cast-value cast
))))
2272 (unless (cast-silent-conflict cast
)
2275 (if (cast-single-value-p cast
)
2277 `(multiple-value-call #'list
'dummy
))))
2280 ;; FIXME: Derived type.
2281 (if (cast-silent-conflict cast
)
2282 (let ((dummy-sym (gensym)))
2283 `(let ((,dummy-sym
'dummy
))
2284 ,(internal-type-error-call dummy-sym atype
2285 (cast-context cast
))
2287 `(%compile-time-type-error
'dummy
2288 ',(type-specifier atype
)
2289 ',(type-specifier value-type
)
2291 ',(compile-time-type-error-context context
)
2292 ',(cast-context cast
)))))
2293 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2294 ;; functions, so we declare the return type of
2295 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2297 (setq value
(cast-value cast
))
2298 (derive-node-type (lvar-uses value
) *empty-type
*)
2299 (maybe-terminate-block (lvar-uses value
) nil
)
2300 ;; FIXME: Is it necessary?
2301 (aver (null (block-pred (node-block cast
))))
2302 (delete-block-lazily (node-block cast
))
2303 (return-from ir1-optimize-cast
)))
2304 (when (eq (node-derived-type cast
) *empty-type
*)
2305 (maybe-terminate-block cast nil
))
2307 (when (and (cast-%type-check cast
)
2308 (values-subtypep value-type
2309 (cast-type-to-check cast
)))
2310 (setf (cast-%type-check cast
) nil
))))
2312 (unless do-not-optimize
2313 (setf (node-reoptimize cast
) nil
)))