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
227 (lambda (arg type
&rest args
)
228 (declare (ignore args
))
229 (setf (lvar-%externally-checkable-type arg
)
230 (acond ((lvar-%externally-checkable-type arg
)
231 (values-type-intersection
232 it
(coerce-to-values type
)))
233 (t (coerce-to-values type
)))))
235 (or (lvar-%externally-checkable-type lvar
) *wild-type
*))
237 ;;;; interface routines used by optimizers
239 ;;; This function is called by optimizers to indicate that something
240 ;;; interesting has happened to the value of LVAR. Optimizers must
241 ;;; make sure that they don't call for reoptimization when nothing has
242 ;;; happened, since optimization will fail to terminate.
244 ;;; We clear any cached type for the lvar and set the reoptimize flags
245 ;;; on everything in sight.
246 (defun reoptimize-lvar (lvar)
247 (declare (type (or lvar null
) lvar
))
249 (setf (lvar-%derived-type lvar
) nil
)
250 (let ((dest (lvar-dest lvar
)))
252 (setf (lvar-reoptimize lvar
) t
)
253 (setf (node-reoptimize dest
) t
)
254 (binding* (;; Since this may be called during IR1 conversion,
255 ;; PREV may be missing.
256 (prev (node-prev dest
) :exit-if-null
)
257 (block (ctran-block prev
))
258 (component (block-component block
)))
259 (setf (block-reoptimize block
) t
)
260 (reoptimize-component component
:maybe
))
261 (loop for cast in
(lvar-dependent-casts lvar
)
262 do
(setf (node-reoptimize cast
) t
))))
264 (setf (block-type-check (node-block node
)) t
)))
267 (defun reoptimize-lvar-uses (lvar)
268 (declare (type lvar lvar
))
270 (setf (node-reoptimize use
) t
)
271 (setf (block-reoptimize (node-block use
)) t
)
272 (reoptimize-component (node-component use
) :maybe
)))
274 ;;; Annotate NODE to indicate that its result has been proven to be
275 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
276 ;;; only correct way to supply information discovered about a node's
277 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
278 ;;; information may be lost and reoptimization may not happen.
280 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
281 ;;; intersection is different from the old type, then we do a
282 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
283 (defun derive-node-type (node rtype
&key from-scratch
)
284 (declare (type valued-node node
) (type ctype rtype
))
285 (let* ((initial-type (node-derived-type node
))
286 (node-type (if from-scratch
289 (unless (eq initial-type rtype
)
290 (let ((int (values-type-intersection node-type rtype
))
291 (lvar (node-lvar node
)))
292 ;; Don't use type/=, it will return NIL on unknown types.
293 ;; Instead of checking the second value just negate TYPE=
294 (unless (type= initial-type int
)
295 (when (and *check-consistency
*
296 (eq int
*empty-type
*)
297 (not (eq rtype
*empty-type
*)))
298 (aver (not from-scratch
))
299 (let ((*compiler-error-context
* node
))
301 "New inferred type ~S conflicts with old type:~
302 ~% ~S~%*** possible internal error? Please report this."
303 (type-specifier rtype
) (type-specifier node-type
))))
304 (setf (node-derived-type node
) int
)
305 ;; If the new type consists of only one object, replace the
306 ;; node with a constant reference.
307 (when (and (ref-p node
)
308 (lambda-var-p (ref-leaf node
)))
309 (let ((type (single-value-type int
)))
310 (when (and (member-type-p type
)
311 (eql 1 (member-type-size type
)))
312 (change-ref-leaf node
(find-constant
313 (first (member-type-members type
)))))))
314 (reoptimize-lvar lvar
)))))
317 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
318 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
319 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
320 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK to
321 ;;; guarantee that the new assertion will be checked.
322 (defun assert-lvar-type (lvar type policy
&optional context
)
323 (declare (type lvar lvar
) (type ctype type
))
324 (unless (values-subtypep (lvar-derived-type lvar
) type
)
325 (let ((internal-lvar (make-lvar))
326 (dest (lvar-dest lvar
)))
327 (substitute-lvar internal-lvar lvar
)
328 (let ((cast (insert-cast-before dest lvar type policy
330 (use-lvar cast internal-lvar
)
336 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
337 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
338 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
339 ;;; we are done, then another iteration would be beneficial.
340 (defun ir1-optimize (component fastp
)
341 (declare (type component component
))
342 (setf (component-reoptimize component
) nil
)
343 (loop with block
= (block-next (component-head component
))
344 with tail
= (component-tail component
)
345 for last-block
= block
346 until
(eq block tail
)
348 ;; We delete blocks when there is either no predecessor or the
349 ;; block is in a lambda that has been deleted. These blocks
350 ;; would eventually be deleted by DFO recomputation, but doing
351 ;; it here immediately makes the effect available to IR1
353 ((or (block-delete-p block
)
354 (null (block-pred block
)))
355 (delete-block-lazily block
)
356 (setq block
(clean-component component block
)))
357 ((eq (functional-kind (block-home-lambda block
)) :deleted
)
358 ;; Preserve the BLOCK-SUCC invariant that almost every block has
359 ;; one successor (and a block with DELETE-P set is an acceptable
361 (mark-for-deletion block
)
362 (setq block
(clean-component component block
)))
365 (let ((succ (block-succ block
)))
366 (unless (singleton-p succ
)
369 (let ((last (block-last block
)))
372 (flush-dest (if-test last
))
373 (when (unlink-node last
)
376 (when (maybe-delete-exit last
)
379 (unless (join-successor-if-possible block
)
382 (when (and (not fastp
) (block-reoptimize block
) (block-component block
))
383 (aver (not (block-delete-p block
)))
384 (ir1-optimize-block block
))
386 (cond ((and (block-delete-p block
) (block-component block
))
387 (setq block
(clean-component component block
)))
388 ((and (block-flush-p block
) (block-component block
))
389 (flush-dead-code block
)))))
390 do
(when (eq block last-block
)
391 (setq block
(block-next block
))))
395 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
398 ;;; Note that although they are cleared here, REOPTIMIZE flags might
399 ;;; still be set upon return from this function, meaning that further
400 ;;; optimization is wanted (as a consequence of optimizations we did).
401 (defun ir1-optimize-block (block)
402 (declare (type cblock block
))
403 ;; We clear the node and block REOPTIMIZE flags before doing the
404 ;; optimization, not after. This ensures that the node or block will
405 ;; be reoptimized if necessary.
406 (setf (block-reoptimize block
) nil
)
407 (do-nodes (node nil block
:restart-p t
)
408 (when (node-reoptimize node
)
409 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
410 (setf (node-reoptimize node
) nil
)
414 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
415 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
416 ;; any argument changes.
417 (ir1-optimize-combination node
))
419 (ir1-optimize-if node
))
421 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
422 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
423 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
425 (setf (node-reoptimize node
) t
)
426 (ir1-optimize-return node
))
428 (ir1-optimize-mv-combination node
))
430 ;; With an EXIT, we derive the node's type from the VALUE's
432 (let ((value (exit-value node
)))
434 (derive-node-type node
(lvar-derived-type value
)))))
436 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
437 ;; is accurate till the node actually has been reoptimized.
438 (setf (node-reoptimize node
) t
)
439 (ir1-optimize-set node
))
441 (ir1-optimize-cast node
)))))
445 ;;; Try to join with a successor block. If we succeed, we return true,
447 (defun join-successor-if-possible (block)
448 (declare (type cblock block
))
449 (let ((next (first (block-succ block
))))
450 (when (block-start next
) ; NEXT is not an END-OF-COMPONENT marker
451 (cond ( ;; We cannot combine with a successor block if:
453 ;; the successor has more than one predecessor;
454 (rest (block-pred next
))
455 ;; the successor is the current block (infinite loop);
457 ;; the next block has a different cleanup, and thus
458 ;; we may want to insert cleanup code between the
459 ;; two blocks at some point;
460 (not (eq (block-end-cleanup block
)
461 (block-start-cleanup next
)))
462 ;; the next block has a different home lambda, and
463 ;; thus the control transfer is a non-local exit.
464 (not (eq (block-home-lambda block
)
465 (block-home-lambda next
)))
466 ;; Stack analysis phase wants ENTRY to start a block...
467 (entry-p (block-start-node next
))
468 (let ((last (block-last block
)))
469 (and (valued-node-p last
)
470 (awhen (node-lvar last
)
472 ;; ... and a DX-allocator to end a block.
473 (lvar-dynamic-extent it
)
474 ;; ... and for there to be no chance of there
475 ;; being two successive USEs of the same
476 ;; multi-valued LVAR in the same block (since
477 ;; we can only insert cleanup code at block
478 ;; boundaries, but need to discard
479 ;; multi-valued LVAR contents before they are
481 (and (consp (lvar-uses it
))
482 (not (lvar-single-value-p it
))))))))
485 (join-blocks block next
)
488 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
489 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
490 ;;; for the two blocks so that any indicated optimization gets done.
491 (defun join-blocks (block1 block2
)
492 (declare (type cblock block1 block2
))
493 (let* ((last1 (block-last block1
))
494 (last2 (block-last block2
))
495 (succ (block-succ block2
))
496 (start2 (block-start block2
)))
497 (do ((ctran start2
(node-next (ctran-next ctran
))))
499 (setf (ctran-block ctran
) block1
))
501 (unlink-blocks block1 block2
)
503 (unlink-blocks block2 block
)
504 (link-blocks block1 block
))
506 (setf (ctran-kind start2
) :inside-block
)
507 (setf (node-next last1
) start2
)
508 (setf (ctran-use start2
) last1
)
509 (setf (block-last block1
) last2
))
511 (setf (block-flags block1
)
512 (attributes-union (block-flags block1
)
513 (block-flags block2
)))
515 (let ((next (block-next block2
))
516 (prev (block-prev block2
)))
517 (setf (block-next prev
) next
)
518 (setf (block-prev next
) prev
))
522 ;;; Delete any nodes in BLOCK whose value is unused and which have no
523 ;;; side effects. We can delete sets of lexical variables when the set
524 ;;; variable has no references.
525 (defun flush-dead-code (block)
526 (declare (type cblock block
))
527 (setf (block-flush-p block
) nil
)
528 (do-nodes-backwards (node lvar block
:restart-p t
)
530 (do-uses (other-node lvar
)
531 (when (and (neq node other-node
)
532 (eq block
(node-block other-node
)))
533 ;; This must be a preceding node and the current node will
534 ;; overwrite the value, unlink the lvar and the node will
535 ;; get a chance to be deleted on one of the next iterations
536 (delete-lvar-use other-node
)))
542 (when (and (not (node-tail-p node
))
543 (flushable-combination-p node
))
544 (flush-combination node
)))
546 (when (eq (basic-combination-kind node
) :local
)
547 (let ((fun (combination-lambda node
)))
548 (when (dolist (var (lambda-vars fun
) t
)
549 (when (or (leaf-refs var
)
550 (lambda-var-sets var
))
552 (mapc #'flush-dest
(basic-combination-args node
))
555 (let ((value (exit-value node
)))
558 (setf (exit-value node
) nil
))))
560 (let ((var (set-var node
)))
561 (when (and (lambda-var-p var
)
562 (null (leaf-refs var
)))
563 (flush-dest (set-value node
))
564 (setf (basic-var-sets var
)
565 (delq node
(basic-var-sets var
)))
566 (unlink-node node
))))
568 (unless (cast-type-check node
)
569 (flush-dest (cast-value node
))
570 (unlink-node node
)))))))
572 ;;;; local call return type propagation
574 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
575 ;;; flag set. It iterates over the uses of the RESULT, looking for
576 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
577 ;;; call, then we union its type together with the types of other such
578 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
579 ;;; type with the RESULT's asserted type. We can make this
580 ;;; intersection now (potentially before type checking) because this
581 ;;; assertion on the result will eventually be checked (if
584 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
585 ;;; combination, which may change the successor of the call to be the
586 ;;; called function, and if so, checks if the call can become an
587 ;;; assignment. If we convert to an assignment, we abort, since the
588 ;;; RETURN has been deleted.
589 (defun find-result-type (node)
590 (declare (type creturn node
))
591 (let ((result (return-result node
)))
592 (collect ((use-union *empty-type
* values-type-union
))
593 (do-uses (use result
)
594 (let ((use-home (node-home-lambda use
)))
595 (cond ((or (eq (functional-kind use-home
) :deleted
)
596 (block-delete-p (node-block use
))))
597 ((not (and (basic-combination-p use
)
598 (eq (basic-combination-kind use
) :local
)))
599 (use-union (node-derived-type use
)))
600 ((or (eq (functional-kind (combination-lambda use
)) :deleted
)
601 (block-delete-p (lambda-block (combination-lambda use
)))))
603 (aver (eq (lambda-tail-set use-home
)
604 (lambda-tail-set (combination-lambda use
))))
605 (when (combination-p use
)
606 (when (nth-value 1 (maybe-convert-tail-local-call use
))
607 (return-from find-result-type t
)))))))
609 ;; (values-type-intersection
610 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
614 (setf (return-result-type node
) int
))))
617 ;;; Do stuff to realize that something has changed about the value
618 ;;; delivered to a return node. Since we consider the return values of
619 ;;; all functions in the tail set to be equivalent, this amounts to
620 ;;; bringing the entire tail set up to date. We iterate over the
621 ;;; returns for all the functions in the tail set, reanalyzing them
622 ;;; all (not treating NODE specially.)
624 ;;; When we are done, we check whether the new type is different from
625 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
626 ;;; all the lvars for references to functions in the tail set. This
627 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
628 ;;; results of the calls.
629 (defun ir1-optimize-return (node)
630 (declare (type creturn node
))
631 (let ((lambda (return-lambda node
)))
634 (let* ((tails (lambda-tail-set lambda
))
635 (funs (tail-set-funs tails
)))
636 (collect ((res *empty-type
* values-type-union
))
638 (let ((return (lambda-return fun
)))
640 (when (node-reoptimize return
)
641 (setf (node-reoptimize return
) nil
)
642 (when (find-result-type return
)
644 (res (return-result-type return
)))))
646 (when (type/= (res) (tail-set-type tails
))
647 (setf (tail-set-type tails
) (res))
648 (dolist (fun (tail-set-funs tails
))
649 (dolist (ref (leaf-refs fun
))
650 (reoptimize-lvar (node-lvar ref
)))))))))
656 ;;; Utility: return T if both argument cblocks are equivalent. For now,
657 ;;; detect only blocks that read the same leaf into the same lvar, and
658 ;;; continue to the same block.
659 (defun cblocks-equivalent-p (x y
)
660 (declare (type cblock x y
))
661 (and (ref-p (block-start-node x
))
662 (eq (block-last x
) (block-start-node x
))
664 (ref-p (block-start-node y
))
665 (eq (block-last y
) (block-start-node y
))
667 (equal (block-succ x
) (block-succ y
))
668 (eql (ref-lvar (block-start-node x
)) (ref-lvar (block-start-node y
)))
669 (eql (ref-leaf (block-start-node x
)) (ref-leaf (block-start-node y
)))))
671 ;;; Check whether the predicate is known to be true or false,
672 ;;; deleting the IF node in favor of the appropriate branch when this
674 ;;; Similarly, when both branches are equivalent, branch directly to either
676 ;;; Also, if the test has multiple uses, replicate the node when possible...
677 ;;; in fact, splice in direct jumps to the right branch if possible.
678 (defun ir1-optimize-if (node)
679 (declare (type cif node
))
680 (let ((test (if-test node
))
681 (block (node-block node
)))
682 (let* ((type (lvar-type test
))
683 (consequent (if-consequent node
))
684 (alternative (if-alternative node
))
686 (cond ((constant-lvar-p test
)
687 (if (lvar-value test
) alternative consequent
))
688 ((not (types-equal-or-intersect type
(specifier-type 'null
)))
690 ((type= type
(specifier-type 'null
))
692 ((or (eq consequent alternative
) ; Can this happen?
693 (cblocks-equivalent-p alternative consequent
))
694 ;; Even if the references are the same they can have
695 ;; different derived types based on the TEST
696 ;; Don't lose the second type when killing it.
697 (let ((consequent-ref (block-start-node consequent
)))
698 (derive-node-type consequent-ref
700 (node-derived-type consequent-ref
)
701 (node-derived-type (block-start-node alternative
)))
705 (kill-if-branch-1 node test block victim
)
706 (return-from ir1-optimize-if
(values))))
707 (tension-if-if-1 node test block
)
708 (duplicate-if-if-1 node test block
)
711 ;; When we know that we only have a single successor, kill the victim
712 ;; ... unless the victim and the remaining successor are the same.
713 (defun kill-if-branch-1 (node test block victim
)
714 (declare (type cif node
))
716 (when (rest (block-succ block
))
717 (unlink-blocks block victim
))
718 (setf (component-reanalyze (node-component node
)) t
)
721 ;; When if/if conversion would leave (if ... (if nil ...)) or
722 ;; (if ... (if not-nil ...)), splice the correct successor right
724 (defun tension-if-if-1 (node test block
)
725 (when (and (eq (block-start-node block
) node
)
726 (listp (lvar-uses test
)))
728 (when (immediately-used-p test use
)
729 (let* ((type (single-value-type (node-derived-type use
)))
730 (target (if (type= type
(specifier-type 'null
))
731 (if-alternative node
)
732 (multiple-value-bind (typep surep
)
734 (and (not typep
) surep
735 (if-consequent node
))))))
737 (let ((pred (node-block use
)))
738 (cond ((listp (lvar-uses test
))
739 (change-block-successor pred block target
)
740 (delete-lvar-use use
))
742 ;; only one use left. Just kill the now-useless
743 ;; branch to avoid spurious code deletion notes.
744 (aver (rest (block-succ block
)))
747 (if (eql target
(if-alternative node
))
749 (if-alternative node
)))
750 (return-from tension-if-if-1
))))))))))
752 ;; Finally, duplicate EQ-nil tests
753 (defun duplicate-if-if-1 (node test block
)
754 (when (and (eq (block-start-node block
) node
)
755 (listp (lvar-uses test
)))
757 (when (immediately-used-p test use
)
758 (convert-if-if use node
)
759 ;; leave the last use as is, instead of replacing
760 ;; the (singly-referenced) CIF node with a duplicate.
761 (when (not (listp (lvar-uses test
))) (return))))))
763 ;;; Create a new copy of an IF node that tests the value of the node
764 ;;; USE. The test must have >1 use, and must be immediately used by
765 ;;; USE. NODE must be the only node in its block (implying that
766 ;;; block-start = if-test).
768 ;;; This optimization has an effect semantically similar to the
769 ;;; source-to-source transformation:
770 ;;; (IF (IF A B C) D E) ==>
771 ;;; (IF A (IF B D E) (IF C D E))
773 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
774 ;;; node so that dead code deletion notes will definitely not consider
775 ;;; either node to be part of the original source. One node might
776 ;;; become unreachable, resulting in a spurious note.
777 (defun convert-if-if (use node
)
778 (declare (type node use
) (type cif node
))
779 (with-ir1-environment-from-node node
780 (let* ((block (node-block node
))
781 (test (if-test node
))
782 (cblock (if-consequent node
))
783 (ablock (if-alternative node
))
784 (use-block (node-block use
))
785 (new-ctran (make-ctran))
786 (new-lvar (make-lvar))
787 (new-node (make-if :test new-lvar
789 :alternative ablock
))
790 (new-block (ctran-starts-block new-ctran
)))
791 (link-node-to-previous-ctran new-node new-ctran
)
792 (setf (lvar-dest new-lvar
) new-node
)
793 (setf (block-last new-block
) new-node
)
795 (unlink-blocks use-block block
)
796 (%delete-lvar-use use
)
797 (add-lvar-use use new-lvar
)
798 (link-blocks use-block new-block
)
800 (link-blocks new-block cblock
)
801 (link-blocks new-block ablock
)
803 (push "<IF Duplication>" (node-source-path node
))
804 (push "<IF Duplication>" (node-source-path new-node
))
806 (reoptimize-lvar test
)
807 (reoptimize-lvar new-lvar
)
808 (setf (component-reanalyze *current-component
*) t
)))
811 ;;;; exit IR1 optimization
813 ;;; This function attempts to delete an exit node, returning true if
814 ;;; it deletes the block as a consequence:
815 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
816 ;;; anything, since there is nothing to be done.
817 ;;; -- If the exit node and its ENTRY have the same home lambda then
818 ;;; we know the exit is local, and can delete the exit. We change
819 ;;; uses of the Exit-Value to be uses of the original lvar,
820 ;;; then unlink the node. If the exit is to a TR context, then we
821 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
822 ;;; their value to this exit.
823 ;;; -- If there is no value (as in a GO), then we skip the value
826 ;;; This function is also called by environment analysis, since it
827 ;;; wants all exits to be optimized even if normal optimization was
829 (defun maybe-delete-exit (node)
830 (declare (type exit node
))
831 (let ((value (exit-value node
))
832 (entry (exit-entry node
)))
834 (eq (node-home-lambda node
) (node-home-lambda entry
)))
835 (setf (entry-exits entry
) (delq node
(entry-exits entry
)))
837 (delete-filter node
(node-lvar node
) value
)
838 (unlink-node node
)))))
841 ;;;; combination IR1 optimization
843 ;;; Report as we try each transform?
845 (defvar *show-transforms-p
* nil
)
847 (defun check-important-result (node info
)
848 (when (and (null (node-lvar node
))
849 (ir1-attributep (fun-info-attributes info
) important-result
))
850 (let ((*compiler-error-context
* node
))
852 "The return value of ~A should not be discarded."
853 (lvar-fun-name (basic-combination-fun node
) t
)))))
855 ;;; Do IR1 optimizations on a COMBINATION node.
856 (declaim (ftype (function (combination) (values)) ir1-optimize-combination
))
857 (defun ir1-optimize-combination (node)
858 (when (lvar-reoptimize (basic-combination-fun node
))
859 (propagate-fun-change node
)
860 (when (node-deleted node
)
861 (return-from ir1-optimize-combination
))
862 (maybe-terminate-block node nil
))
863 (let ((args (basic-combination-args node
))
864 (info (basic-combination-fun-info node
)))
865 (flet ((clear-reoptimize-args ()
868 (setf (lvar-reoptimize arg
) nil
))))
870 (check-important-result node info
)
871 (let ((fun (fun-info-derive-type info
)))
873 (let ((res (funcall fun node
)))
875 (derive-node-type node
(coerce-to-values res
))
876 (maybe-terminate-block node nil
)))))))
877 (ecase (basic-combination-kind node
)
879 (let ((fun (combination-lambda node
)))
880 (if (eq (functional-kind fun
) :let
)
881 (propagate-let-args node fun
)
882 (propagate-local-call-args node fun
))))
884 (clear-reoptimize-args))
886 (clear-reoptimize-args)
888 ;; This is a known function marked NOTINLINE
891 ;; Check against the DEFINED-TYPE unless TYPE is already good.
892 (let* ((fun (basic-combination-fun node
))
893 (uses (lvar-uses fun
))
894 (leaf (when (ref-p uses
) (ref-leaf uses
))))
895 (multiple-value-bind (type defined-type
)
896 (if (global-var-p leaf
)
897 (values (leaf-type leaf
) (leaf-defined-type leaf
))
899 (when (and (not (fun-type-p type
)) (fun-type-p defined-type
))
900 (validate-call-type node type leaf
)))))))
903 (clear-reoptimize-args)
905 (let ((attr (fun-info-attributes info
)))
906 (when (constant-fold-call-p node
)
907 (constant-fold-call node
)
908 (return-from ir1-optimize-combination
))
909 (when (and (ir1-attributep attr commutative
)
911 (constant-lvar-p (first args
))
912 (not (constant-lvar-p (second args
))))
913 (setf (basic-combination-args node
) (nreverse args
))))
915 (let ((fun (fun-info-optimizer info
)))
916 (unless (and fun
(funcall fun node
))
917 ;; First give the VM a peek at the call
918 (multiple-value-bind (style transform
)
919 (combination-implementation-style node
)
922 ;; The VM knows how to handle this.
925 ;; The VM mostly knows how to handle this. We need
926 ;; to massage the call slightly, though.
927 (transform-call node transform
(combination-fun-source-name node
)))
929 ;; Let transforms have a crack at it.
930 (dolist (x (fun-info-transforms info
))
932 (when *show-transforms-p
*
933 (let* ((lvar (basic-combination-fun node
))
934 (fname (lvar-fun-name lvar t
)))
935 (/show
"trying transform" x
(transform-function x
) "for" fname
)))
936 (unless (ir1-transform node x
)
938 (when *show-transforms-p
*
939 (/show
"quitting because IR1-TRANSFORM result was NIL"))
943 (defun xep-tail-combination-p (node)
944 (and (combination-p node
)
945 (let* ((lvar (combination-lvar node
))
946 (dest (when (lvar-p lvar
) (lvar-dest lvar
)))
947 (lambda (when (return-p dest
) (return-lambda dest
))))
948 (and (lambda-p lambda
)
949 (eq :external
(lambda-kind lambda
))))))
951 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
952 ;;; the block there, and link it to the component tail.
954 ;;; Except when called during IR1 convertion, we delete the
955 ;;; continuation if it has no other uses. (If it does have other uses,
958 ;;; Termination on the basis of a continuation type is
960 ;;; -- The continuation is deleted (hence the assertion is spurious), or
961 ;;; -- We are in IR1 conversion (where THE assertions are subject to
962 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
963 ;;; uses can(?) be added later. -- APD, 2003-07-17
965 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
966 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p
)
967 (declare (type (or basic-combination cast cset ref
) node
))
968 (let* ((block (node-block node
))
969 (lvar (node-lvar node
))
970 (ctran (node-next node
))
971 (tail (component-tail (block-component block
)))
972 (succ (first (block-succ block
))))
973 (declare (ignore lvar
))
974 (unless (or (and (eq node
(block-last block
)) (eq succ tail
))
975 (block-delete-p block
))
976 ;; Even if the combination will never return, don't terminate if this
977 ;; is the tail call of a XEP: doing that would inhibit TCO.
978 (when (and (eq (node-derived-type node
) *empty-type
*)
979 (not (xep-tail-combination-p node
)))
980 (cond (ir1-converting-not-optimizing-p
983 (aver (eq (block-last block
) node
)))
985 (setf (block-last block
) node
)
986 (setf (ctran-use ctran
) nil
)
987 (setf (ctran-kind ctran
) :unused
)
988 (setf (ctran-block ctran
) nil
)
989 (setf (node-next node
) nil
)
990 (link-blocks block
(ctran-starts-block ctran
)))))
992 (node-ends-block node
)))
994 (let ((succ (first (block-succ block
))))
995 (unlink-blocks block succ
)
996 (setf (component-reanalyze (block-component block
)) t
)
997 (aver (not (block-succ block
)))
998 (link-blocks block tail
)
999 (cond (ir1-converting-not-optimizing-p
1000 (%delete-lvar-use node
))
1001 (t (delete-lvar-use node
)
1002 (when (null (block-pred succ
))
1003 (mark-for-deletion succ
)))))
1006 ;;; This is called both by IR1 conversion and IR1 optimization when
1007 ;;; they have verified the type signature for the call, and are
1008 ;;; wondering if something should be done to special-case the call. If
1009 ;;; CALL is a call to a global function, then see whether it defined
1011 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
1012 ;;; the expansion and change the call to call it. Expansion is
1013 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
1014 ;;; true, we never expand, since this function has already been
1015 ;;; converted. Local call analysis will duplicate the definition
1016 ;;; if necessary. We claim that the parent form is LABELS for
1017 ;;; context declarations, since we don't want it to be considered
1018 ;;; a real global function.
1019 ;;; -- If it is a known function, mark it as such by setting the KIND.
1021 ;;; We return the leaf referenced (NIL if not a leaf) and the
1022 ;;; FUN-INFO assigned.
1023 (defun recognize-known-call (call ir1-converting-not-optimizing-p
)
1024 (declare (type combination call
))
1025 (let* ((ref (lvar-uses (basic-combination-fun call
)))
1026 (leaf (when (ref-p ref
) (ref-leaf ref
)))
1027 (inlinep (if (defined-fun-p leaf
)
1028 (defined-fun-inlinep leaf
)
1031 ((eq inlinep
:notinline
)
1032 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1034 (setf (basic-combination-fun-info call
) info
))
1036 ((not (and (global-var-p leaf
)
1037 (eq (global-var-kind leaf
) :global-function
)))
1039 ((and (ecase inlinep
1042 ((nil :maybe-inline
) (policy call
(zerop space
))))
1043 (defined-fun-p leaf
)
1044 (defined-fun-inline-expansion leaf
)
1045 (inline-expansion-ok call
))
1046 ;; Inline: if the function has already been converted at another call
1047 ;; site in this component, we point this REF to the functional. If not,
1048 ;; we convert the expansion.
1050 ;; For :INLINE case local call analysis will copy the expansion later,
1051 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1052 ;; expansion per component.
1054 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1056 (with-ir1-environment-from-node call
1057 (let ((fun (defined-fun-functional leaf
)))
1059 (and (eq inlinep
:inline
) (functional-kind fun
)))
1061 (let* ((name (leaf-source-name leaf
))
1062 (res (ir1-convert-inline-expansion
1064 (defined-fun-inline-expansion leaf
)
1067 (info :function
:info name
))))
1068 ;; Allow backward references to this function from following
1069 ;; forms. (Reused only if policy matches.)
1070 (push res
(defined-fun-functionals leaf
))
1071 (change-ref-leaf ref res
)
1072 (unless ir1-converting-not-optimizing-p
1073 (locall-analyze-component *current-component
*)))
1074 ;; If we've already converted, change ref to the converted
1076 (change-ref-leaf ref fun
))))
1077 (values (ref-leaf ref
) nil
))
1079 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1083 (setf (basic-combination-kind call
) :known
)
1084 (setf (basic-combination-fun-info call
) info
)))
1085 (values leaf nil
)))))))
1087 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1088 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1089 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1090 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1091 ;;; syntax check, arg/result type processing, but still call
1092 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1093 ;;; and that checking is done by local call analysis.
1094 (defun validate-call-type (call type fun
&optional ir1-converting-not-optimizing-p
)
1095 (declare (type combination call
) (type ctype type
))
1096 (let* ((where (when fun
(leaf-where-from fun
)))
1097 (same-file-p (eq :defined-here where
)))
1098 (cond ((not (fun-type-p type
))
1099 ;; Using the defined-type too early is a bit of a waste: during
1100 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1101 (when (and fun
(not ir1-converting-not-optimizing-p
))
1102 (let ((defined-type (leaf-defined-type fun
)))
1103 (when (and (fun-type-p defined-type
)
1104 (neq fun
(combination-type-validated-for-leaf call
)))
1105 ;; Don't validate multiple times against the same leaf --
1106 ;; it doesn't add any information, but may generate the same warning
1108 (setf (combination-type-validated-for-leaf call
) fun
)
1109 (when (and (valid-fun-use call defined-type
1110 :argument-test
#'always-subtypep
1112 :lossage-fun
(if same-file-p
1114 #'compiler-style-warn
)
1115 :unwinnage-fun
#'compiler-notify
)
1117 (assert-call-type call defined-type nil
)
1118 (maybe-terminate-block call ir1-converting-not-optimizing-p
)))))
1119 (recognize-known-call call ir1-converting-not-optimizing-p
))
1120 ((valid-fun-use call type
1121 :argument-test
#'always-subtypep
1123 :lossage-fun
#'compiler-warn
1124 :unwinnage-fun
#'compiler-notify
)
1125 (assert-call-type call type
)
1126 (maybe-terminate-block call ir1-converting-not-optimizing-p
)
1127 (recognize-known-call call ir1-converting-not-optimizing-p
))
1129 (setf (combination-kind call
) :error
)
1130 (values nil nil
)))))
1132 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1133 ;;; changed. If the call is local, we try to LET-convert it, and
1134 ;;; derive the result type. If it is a :FULL call, we validate it
1135 ;;; against the type, which recognizes known calls, does inline
1136 ;;; expansion, etc. If a call to a predicate in a non-conditional
1137 ;;; position or to a function with a source transform, then we
1138 ;;; reconvert the form to give IR1 another chance.
1139 (defun propagate-fun-change (call)
1140 (declare (type combination call
))
1141 (let ((*compiler-error-context
* call
)
1142 (fun-lvar (basic-combination-fun call
)))
1143 (setf (lvar-reoptimize fun-lvar
) nil
)
1144 (case (combination-kind call
)
1146 (let ((fun (combination-lambda call
)))
1147 (maybe-let-convert fun
)
1148 (unless (member (functional-kind fun
) '(:let
:assignment
:deleted
))
1149 (derive-node-type call
(tail-set-type (lambda-tail-set fun
))))))
1151 (multiple-value-bind (leaf info
)
1152 (let* ((uses (lvar-uses fun-lvar
))
1153 (leaf (when (ref-p uses
) (ref-leaf uses
))))
1154 (validate-call-type call
(lvar-fun-type fun-lvar
) leaf
))
1155 (cond ((functional-p leaf
)
1156 (convert-call-if-possible
1157 (lvar-uses (basic-combination-fun call
))
1160 ((and (global-var-p leaf
)
1161 (eq (global-var-kind leaf
) :global-function
)
1162 (leaf-has-source-name-p leaf
)
1163 (or (info :function
:source-transform
(leaf-source-name leaf
))
1165 (ir1-attributep (fun-info-attributes info
)
1167 (let ((lvar (node-lvar call
)))
1168 (and lvar
(not (if-p (lvar-dest lvar
))))))))
1169 (let ((name (leaf-source-name leaf
))
1170 (dummies (make-gensym-list
1171 (length (combination-args call
)))))
1172 (transform-call call
1174 (,@(if (symbolp name
)
1178 (leaf-source-name leaf
)
1182 ;;;; known function optimization
1184 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1185 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1186 ;;; replace it, otherwise add a new one.
1187 (defun record-optimization-failure (node transform args
)
1188 (declare (type combination node
) (type transform transform
)
1189 (type (or fun-type list
) args
))
1190 (let* ((table (component-failed-optimizations *component-being-compiled
*))
1191 (found (assoc transform
(gethash node table
))))
1193 (setf (cdr found
) args
)
1194 (push (cons transform args
) (gethash node table
))))
1197 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1198 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1199 ;;; doing the transform for some reason and FLAME is true, then we
1200 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1201 ;;; finalize to pick up. We return true if the transform failed, and
1202 ;;; thus further transformation should be attempted. We return false
1203 ;;; if either the transform succeeded or was aborted.
1204 (defun ir1-transform (node transform
)
1205 (declare (type combination node
) (type transform transform
))
1206 (declare (notinline warn
)) ; See COMPILER-WARN for rationale
1207 (let* ((type (transform-type transform
))
1208 (fun (transform-function transform
))
1209 (constrained (fun-type-p type
))
1210 (table (component-failed-optimizations *component-being-compiled
*))
1211 (flame (case (transform-important transform
)
1212 ((t) (policy node
(>= speed inhibit-warnings
)))
1213 (:slightly
(policy node
(> speed inhibit-warnings
)))))
1214 (*compiler-error-context
* node
)
1215 (policy-test (transform-policy transform
)))
1216 (cond ((and policy-test
1217 (not (funcall policy-test node
))))
1218 ((or (not constrained
)
1219 (valid-fun-use node type
))
1220 (multiple-value-bind (severity args
)
1221 (catch 'give-up-ir1-transform
1222 (transform-call node
1224 (combination-fun-source-name node
))
1228 (remhash node table
)
1231 (setf (combination-kind node
) :error
)
1233 (apply #'warn args
))
1234 (remhash node table
)
1239 (record-optimization-failure node transform args
))
1240 (setf (gethash node table
)
1241 (remove transform
(gethash node table
) :key
#'car
)))
1244 (remhash node table
)
1249 :argument-test
#'types-equal-or-intersect
1250 :result-test
#'values-types-equal-or-intersect
))
1251 (record-optimization-failure node transform type
)
1256 ;;; When we don't like an IR1 transform, we throw the severity/reason
1259 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1260 ;;; aborting this attempt to transform the call, but admitting the
1261 ;;; possibility that this or some other transform will later succeed.
1262 ;;; If arguments are supplied, they are format arguments for an
1263 ;;; efficiency note.
1265 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1266 ;;; force a normal call to the function at run time. No further
1267 ;;; optimizations will be attempted.
1269 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1270 ;;; delay the transform on the node until later. REASONS specifies
1271 ;;; when the transform will be later retried. The :OPTIMIZE reason
1272 ;;; causes the transform to be delayed until after the current IR1
1273 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1274 ;;; be delayed until after constraint propagation.
1276 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1277 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1278 ;;; do CASE operations on the various REASON values, it might be a
1279 ;;; good idea to go OO, representing the reasons by objects, using
1280 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1281 ;;; SIGNAL instead of THROW.
1282 (declaim (ftype (function (&rest t
) #+(and sb-xc-host ccl
) *
1283 #-
(and sb-xc-host ccl
) nil
) give-up-ir1-transform
))
1284 (defun give-up-ir1-transform (&rest args
)
1285 (throw 'give-up-ir1-transform
(values :failure args
)))
1286 (defun abort-ir1-transform (&rest args
)
1287 (throw 'give-up-ir1-transform
(values :aborted args
)))
1288 (defun delay-ir1-transform (node &rest reasons
)
1289 (let ((assoc (assoc node
*delayed-ir1-transforms
*)))
1291 (setf *delayed-ir1-transforms
*
1292 (acons node reasons
*delayed-ir1-transforms
*))
1293 (throw 'give-up-ir1-transform
:delayed
))
1295 (dolist (reason reasons
)
1296 (pushnew reason
(cdr assoc
)))
1297 (throw 'give-up-ir1-transform
:delayed
)))))
1299 ;;; Poor man's catching and resignalling
1300 ;;; Implicit %GIVE-UP macrolet will resignal the give-up "condition"
1301 (defmacro catch-give-up-ir1-transform
((form &optional args
) &body gave-up-body
)
1302 (let ((block (gensym "BLOCK"))
1303 (kind (gensym "KIND"))
1304 (args (or args
(gensym "ARGS"))))
1306 (multiple-value-bind (,kind
,args
)
1307 (catch 'give-up-ir1-transform
1308 (return-from ,block
,form
))
1311 (throw 'give-up-ir1-transform
:delayed
))
1312 ((:failure
:aborted
)
1313 (macrolet ((%give-up
()
1314 `(throw 'give-up-ir1-transform
(values ,',kind
1316 ,@gave-up-body
)))))))
1318 ;;; Clear any delayed transform with no reasons - these should have
1319 ;;; been tried in the last pass. Then remove the reason from the
1320 ;;; delayed transform reasons, and if any become empty then set
1321 ;;; reoptimize flags for the node. Return true if any transforms are
1323 (defun retry-delayed-ir1-transforms (reason)
1324 (setf *delayed-ir1-transforms
*
1325 (remove-if-not #'cdr
*delayed-ir1-transforms
*))
1326 (let ((reoptimize nil
))
1327 (dolist (assoc *delayed-ir1-transforms
*)
1328 (let ((reasons (remove reason
(cdr assoc
))))
1329 (setf (cdr assoc
) reasons
)
1331 (let ((node (car assoc
)))
1332 (unless (node-deleted node
)
1334 (setf (node-reoptimize node
) t
)
1335 (let ((block (node-block node
)))
1336 (setf (block-reoptimize block
) t
)
1337 (reoptimize-component (block-component block
) :maybe
)))))))
1340 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1341 ;;; environment, and then install it as the function for the call
1342 ;;; NODE. We do local call analysis so that the new function is
1343 ;;; integrated into the control flow.
1345 ;;; We require the original function source name in order to generate
1346 ;;; a meaningful debug name for the lambda we set up. (It'd be
1347 ;;; possible to do this starting from debug names as well as source
1348 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1349 ;;; generality, since source names are always known to our callers.)
1350 (defun transform-call (call res source-name
&optional
(reoptimize-combination t
))
1351 (declare (type combination call
) (list res
))
1352 (aver (and (legal-fun-name-p source-name
)
1353 (not (eql source-name
'.anonymous.
))))
1354 (node-ends-block call
)
1355 (setf (combination-lexenv call
)
1356 (make-lexenv :default
(combination-lexenv call
)
1358 ;; The internal variables of a transform are not going to be
1359 ;; interesting to the debugger, so there's no sense in
1360 ;; suppressing the substitution of variables with only one use
1361 ;; (the extra variables can slow down constraint propagation).
1363 preserve-single-use-debug-variables
1366 (combination-lexenv call
)))))
1367 (with-ir1-environment-from-node call
1368 (with-component-last-block (*current-component
*
1369 (block-next (node-block call
)))
1370 (let ((new-fun (ir1-convert-inline-lambda
1372 :debug-name
(debug-name 'lambda-inlined source-name
)
1374 (type (node-derived-type call
))
1375 (ref (lvar-use (combination-fun call
))))
1376 (change-ref-leaf ref new-fun
)
1377 (setf (combination-kind call
) :full
)
1378 ;; Don't lose the original derived type
1379 (let ((return (lambda-return (main-entry new-fun
))))
1381 (do-uses (node (return-result
1382 (lambda-return (main-entry new-fun
))))
1383 (derive-node-type node type
))))))
1384 ;; Must be done outside of WITH-COMPONENT-LAST-BLOCK
1385 ;; otherwise REMOVE-FROM-DFO might remove that block
1386 ;; but new code still will get attached to it.
1387 (locall-analyze-component *current-component
*))
1388 (when reoptimize-combination
1389 ;; This is mainly to call PROPAGATE-LET-ARGS so that the
1390 ;; newly converted code gets to better types sooner.
1391 (setf (node-reoptimize call
) nil
)
1392 (ir1-optimize-combination call
))
1395 (defun constant-fold-arg-p (name)
1400 (let* ((info (info :function
:info name
))
1401 (attributes (and info
1402 (fun-info-attributes info
))))
1404 (ir1-attributep attributes foldable
)
1405 (not (ir1-attributep attributes call
)))))))
1407 ;;; Return T if the function is foldable and if it's marked as CALL
1408 ;;; all function arguments are FOLDABLE too.
1409 (defun constant-fold-call-p (combination)
1410 (let* ((info (basic-combination-fun-info combination
))
1411 (attr (fun-info-attributes info
))
1412 (args (basic-combination-args combination
)))
1413 (cond ((not (ir1-attributep attr foldable
))
1415 ((ir1-attributep attr call
)
1416 (map-combination-args-and-types
1417 (lambda (arg type lvars
&optional annotation
)
1418 (declare (ignore type lvars
))
1419 (unless (if (eql (car annotation
) 'function-designator
)
1420 (let ((fun (or (lvar-fun-name arg t
)
1421 (and (constant-lvar-p arg
)
1422 (lvar-value arg
)))))
1424 (constant-fold-arg-p fun
)))
1425 (constant-lvar-p arg
))
1426 (return-from constant-fold-call-p
)))
1430 (return-from constant-fold-call-p
)))
1433 (every #'constant-lvar-p args
)))))
1435 ;;; Replace a call to a foldable function of constant arguments with
1436 ;;; the result of evaluating the form. If there is an error during the
1437 ;;; evaluation, we give a warning and leave the call alone, making the
1438 ;;; call a :ERROR call.
1440 ;;; If there is more than one value, then we transform the call into a
1442 (defun constant-fold-call (call)
1443 (let* ((fun-name (lvar-fun-name (combination-fun call
) t
))
1444 (type (info :function
:type fun-name
))
1445 (args (mapcar (lambda (lvar)
1447 (let ((name (lvar-fun-name lvar t
)))
1452 (let ((args (combination-args call
)))
1453 (if (fun-type-p type
)
1454 (resolve-key-args args type
)
1456 (multiple-value-bind (values win
)
1457 (careful-call fun-name
1460 ;; Note: CMU CL had COMPILER-WARN here, and that
1461 ;; seems more natural, but it's probably not.
1463 ;; It's especially not while bug 173 exists:
1466 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1468 ;; can cause constant-folding TYPE-ERRORs (in
1469 ;; #'<=) when END can be proved to be NIL, even
1470 ;; though the code is perfectly legal and safe
1471 ;; because a NIL value of END means that the
1472 ;; #'<= will never be executed.
1474 ;; Moreover, even without bug 173,
1475 ;; quite-possibly-valid code like
1476 ;; (COND ((NONINLINED-PREDICATE END)
1477 ;; (UNLESS (<= END SIZE))
1479 ;; (where NONINLINED-PREDICATE is something the
1480 ;; compiler can't do at compile time, but which
1481 ;; turns out to make the #'<= expression
1482 ;; unreachable when END=NIL) could cause errors
1483 ;; when the compiler tries to constant-fold (<=
1486 ;; So, with or without bug 173, it'd be
1487 ;; unnecessarily evil to do a full
1488 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1489 ;; from COMPILE-FILE) for legal code, so we we
1490 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1491 #-sb-xc-host
#'compiler-style-warn
1492 ;; On the other hand, for code we control, we
1493 ;; should be able to work around any bug
1494 ;; 173-related problems, and in particular we
1495 ;; want to be alerted to calls to our own
1496 ;; functions which aren't being folded away; a
1497 ;; COMPILER-WARNING is butch enough to stop the
1498 ;; SBCL build itself in its tracks.
1499 #+sb-xc-host
#'compiler-warn
1502 (setf (combination-kind call
) :error
))
1503 ((and (proper-list-of-length-p values
1))
1504 (with-ir1-environment-from-node call
1505 (let* ((lvar (node-lvar call
))
1506 (prev (node-prev call
))
1507 (intermediate-ctran (make-ctran)))
1508 (%delete-lvar-use call
)
1509 (setf (ctran-next prev
) nil
)
1510 (setf (node-prev call
) nil
)
1511 (reference-constant prev intermediate-ctran lvar
1513 (link-node-to-previous-ctran call intermediate-ctran
)
1514 (reoptimize-lvar lvar
)
1515 (flush-combination call
))))
1516 (t (let ((dummies (make-gensym-list (length args
))))
1520 (declare (ignore ,@dummies
))
1521 (values ,@(mapcar (lambda (x) `',x
) values
)))
1525 ;;;; local call optimization
1527 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1529 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1530 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1533 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1534 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1535 (defun propagate-to-refs (leaf type
)
1536 (declare (type leaf leaf
) (type ctype type
))
1537 (let ((var-type (leaf-type leaf
))
1538 (refs (leaf-refs leaf
)))
1539 (unless (or (fun-type-p var-type
)
1541 (eq :declared
(leaf-where-from leaf
))
1542 (type-needs-conservation-p var-type
)))
1543 (let ((int (type-approx-intersection2 var-type type
)))
1544 (when (type/= int var-type
)
1545 (setf (leaf-type leaf
) int
)
1546 (let ((s-int (make-single-value-type int
)))
1548 (derive-node-type ref s-int
)
1549 (maybe-terminate-block ref nil
)
1550 ;; KLUDGE: LET var substitution
1551 (let* ((lvar (node-lvar ref
)))
1552 (when (and lvar
(combination-p (lvar-dest lvar
)))
1553 (reoptimize-lvar lvar
)))))))
1556 ;;; Iteration variable: exactly one SETQ of the form:
1558 ;;; (let ((var initial))
1560 ;;; (setq var (+ var step))
1562 (defun maybe-infer-iteration-var-type (var initial-type
)
1563 (binding* ((sets (lambda-var-sets var
) :exit-if-null
)
1565 (() (null (rest sets
)) :exit-if-null
)
1566 (set-use (principal-lvar-use (set-value set
)))
1567 (() (and (combination-p set-use
)
1568 (eq (combination-kind set-use
) :known
)
1569 (fun-info-p (combination-fun-info set-use
))
1570 (not (node-to-be-deleted-p set-use
))
1571 (or (eq (combination-fun-source-name set-use
) '+)
1572 (eq (combination-fun-source-name set-use
) '-
)))
1574 (minusp (eq (combination-fun-source-name set-use
) '-
))
1575 (+-args
(basic-combination-args set-use
))
1576 (() (and (proper-list-of-length-p +-args
2 2)
1577 (let ((first (principal-lvar-use
1580 (eq (ref-leaf first
) var
))))
1582 (step-type (lvar-type (second +-args
)))
1583 (set-type (lvar-type (set-value set
))))
1584 (when (and (numeric-type-p initial-type
)
1585 (numeric-type-p step-type
)
1586 (or (numeric-type-equal initial-type step-type
)
1587 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1588 ;; the initial and the step are of different types,
1589 ;; and the step is less contagious.
1590 (numeric-type-equal initial-type
1591 (numeric-contagion initial-type
1593 (labels ((leftmost (x y cmp cmp
=)
1594 (cond ((eq x nil
) nil
)
1597 (let ((x1 (first x
)))
1599 (let ((y1 (first y
)))
1600 (if (funcall cmp x1 y1
) x y
)))
1602 (if (funcall cmp x1 y
) x y
)))))
1604 (let ((y1 (first y
)))
1605 (if (funcall cmp
= x y1
) x y
)))
1606 (t (if (funcall cmp x y
) x y
))))
1607 (max* (x y
) (leftmost x y
#'> #'>=))
1608 (min* (x y
) (leftmost x y
#'< #'<=)))
1609 (multiple-value-bind (low high
)
1610 (let ((step-type-non-negative (csubtypep step-type
(specifier-type
1612 (step-type-non-positive (csubtypep step-type
(specifier-type
1614 (cond ((or (and step-type-non-negative
(not minusp
))
1615 (and step-type-non-positive minusp
))
1616 (values (numeric-type-low initial-type
)
1617 (when (and (numeric-type-p set-type
)
1618 (numeric-type-equal set-type initial-type
))
1619 (max* (numeric-type-high initial-type
)
1620 (numeric-type-high set-type
)))))
1621 ((or (and step-type-non-positive
(not minusp
))
1622 (and step-type-non-negative minusp
))
1623 (values (when (and (numeric-type-p set-type
)
1624 (numeric-type-equal set-type initial-type
))
1625 (min* (numeric-type-low initial-type
)
1626 (numeric-type-low set-type
)))
1627 (numeric-type-high initial-type
)))
1630 (modified-numeric-type initial-type
1633 :enumerable nil
))))))
1634 (deftransform + ((x y
) * * :result result
)
1635 "check for iteration variable reoptimization"
1636 (let ((dest (principal-lvar-end result
))
1637 (use (principal-lvar-use x
)))
1638 (when (and (ref-p use
)
1642 (reoptimize-lvar (set-value dest
))))
1643 (give-up-ir1-transform))
1645 ;;; Figure out the type of a LET variable that has sets. We compute
1646 ;;; the union of the INITIAL-TYPE and the types of all the set
1647 ;;; values and to a PROPAGATE-TO-REFS with this type.
1648 (defun propagate-from-sets (var initial-type
)
1649 (let ((changes (not (csubtypep (lambda-var-last-initial-type var
) initial-type
)))
1651 (dolist (set (lambda-var-sets var
))
1652 (let ((type (lvar-type (set-value set
))))
1654 (when (node-reoptimize set
)
1655 (let ((old-type (node-derived-type set
)))
1656 (unless (values-subtypep old-type type
)
1657 (derive-node-type set
(make-single-value-type type
))
1658 (maybe-terminate-block set nil
)
1660 (setf (node-reoptimize set
) nil
))))
1662 (setf (lambda-var-last-initial-type var
) initial-type
)
1663 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type
)
1664 (apply #'type-union initial-type types
))))
1665 (propagate-to-refs var res-type
))))
1668 ;;; If a LET variable, find the initial value's type and do
1669 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1671 (defun ir1-optimize-set (node)
1672 (declare (type cset node
))
1673 (let ((var (set-var node
)))
1674 (when (and (lambda-var-p var
) (leaf-refs var
))
1675 (let ((home (lambda-var-home var
)))
1676 (when (eq (functional-kind home
) :let
)
1677 (let* ((initial-value (let-var-initial-value var
))
1678 (initial-type (lvar-type initial-value
)))
1679 (setf (lvar-reoptimize initial-value
) nil
)
1680 (propagate-from-sets var initial-type
))))))
1681 (derive-node-type node
(make-single-value-type
1682 (lvar-type (set-value node
))))
1683 (setf (node-reoptimize node
) nil
)
1686 ;;; Return true if the value of REF will always be the same (and is
1687 ;;; thus legal to substitute.)
1688 (defun constant-reference-p (ref)
1689 (declare (type ref ref
))
1690 (let ((leaf (ref-leaf ref
)))
1692 ((or constant functional
) t
)
1694 (null (lambda-var-sets leaf
)))
1696 (not (eq (defined-fun-inlinep leaf
) :notinline
)))
1698 (case (global-var-kind leaf
)
1700 (let ((name (leaf-source-name leaf
)))
1702 (eq (symbol-package (fun-name-block-name name
))
1704 (info :function
:info name
)))))))))
1706 ;;; If we have a non-set LET var with a single use, then (if possible)
1707 ;;; replace the variable reference's LVAR with the arg lvar.
1709 ;;; We change the REF to be a reference to NIL with unused value, and
1710 ;;; let it be flushed as dead code. A side effect of this substitution
1711 ;;; is to delete the variable.
1712 (defun substitute-single-use-lvar (arg var
)
1713 (declare (type lvar arg
) (type lambda-var var
))
1714 (binding* ((ref (first (leaf-refs var
)))
1715 (lvar (node-lvar ref
) :exit-if-null
)
1716 (dest (lvar-dest lvar
))
1717 (dest-lvar (when (valued-node-p dest
) (node-lvar dest
))))
1719 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1720 ;; LVAR-USEs should not be met on one path. Another problem
1721 ;; is with dynamic-extent.
1722 (eq (lvar-uses lvar
) ref
)
1723 (not (block-delete-p (node-block ref
)))
1724 ;; If the destinatation is dynamic extent, don't substitute unless
1725 ;; the source is as well.
1727 (not (lvar-dynamic-extent dest-lvar
))
1728 (lvar-dynamic-extent lvar
))
1730 ;; we should not change lifetime of unknown values lvars
1732 (and (type-single-value-p (lvar-derived-type arg
))
1733 (multiple-value-bind (pdest pprev
)
1734 (principal-lvar-end lvar
)
1735 (declare (ignore pdest
))
1736 (lvar-single-value-p pprev
))
1737 ;; CASTs can disappear, don't substitute if
1738 ;; DEST-LVAR has other uses (this will be
1739 ;; insufficient if we have a CAST-CAST chain, but
1740 ;; works well for a single CAST)
1741 (or (null dest-lvar
)
1742 (atom (lvar-uses dest-lvar
)))))
1744 (or (eq (basic-combination-fun dest
) lvar
)
1745 (and (eq (basic-combination-kind dest
) :local
)
1746 (type-single-value-p (lvar-derived-type arg
)))))
1748 ;; While CRETURN and EXIT nodes may be known-values,
1749 ;; they have their own complications, such as
1750 ;; substitution into CRETURN may create new tail calls.
1753 (aver (lvar-single-value-p lvar
))
1755 (eq (node-home-lambda ref
)
1756 (lambda-home (lambda-var-home var
))))
1757 (let ((ref-type (single-value-type (node-derived-type ref
))))
1758 (cond ((csubtypep (single-value-type (lvar-type arg
)) ref-type
)
1759 (substitute-lvar-uses lvar arg
1760 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1762 (delete-lvar-use ref
))
1764 (let* ((value (make-lvar))
1765 (cast (insert-cast-before ref value ref-type
1766 ;; KLUDGE: it should be (TYPE-CHECK 0)
1768 (setf (cast-type-to-check cast
) *wild-type
*)
1769 (substitute-lvar-uses value arg
1772 (%delete-lvar-use ref
)
1773 (add-lvar-use cast lvar
)))))
1774 (setf (node-derived-type ref
) *wild-type
*)
1775 (change-ref-leaf ref
(find-constant nil
))
1778 (reoptimize-lvar lvar
)
1781 ;;; Delete a LET, removing the call and bind nodes, and warning about
1782 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1783 ;;; along right away and delete the REF and then the lambda, since we
1784 ;;; flush the FUN lvar.
1785 (defun delete-let (clambda)
1786 (declare (type clambda clambda
))
1787 (aver (functional-letlike-p clambda
))
1788 (note-unreferenced-fun-vars clambda
)
1789 (let ((call (let-combination clambda
)))
1790 (flush-dest (basic-combination-fun call
))
1792 (unlink-node (lambda-bind clambda
))
1793 (setf (lambda-bind clambda
) nil
))
1794 (setf (functional-kind clambda
) :zombie
)
1795 (let ((home (lambda-home clambda
)))
1796 (setf (lambda-lets home
) (delete clambda
(lambda-lets home
))))
1799 ;;; This function is called when one of the arguments to a LET
1800 ;;; changes. We look at each changed argument. If the corresponding
1801 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1802 ;;; consider substituting for the variable, and also propagate
1803 ;;; derived-type information for the arg to all the VAR's refs.
1805 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1806 ;;; subtype of the argument's leaf type. This prevents type checking
1807 ;;; from being defeated, and also ensures that the best representation
1808 ;;; for the variable can be used.
1810 ;;; Substitution of individual references is inhibited if the
1811 ;;; reference is in a different component from the home. This can only
1812 ;;; happen with closures over top level lambda vars. In such cases,
1813 ;;; the references may have already been compiled, and thus can't be
1814 ;;; retroactively modified.
1816 ;;; If all of the variables are deleted (have no references) when we
1817 ;;; are done, then we delete the LET.
1819 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1821 (defun propagate-let-args (call fun
)
1822 (declare (type basic-combination call
) (type clambda fun
))
1823 (map-combination-arg-var
1824 (lambda (arg var type
)
1826 ((lambda-var-deleted var
))
1827 ((lambda-var-sets var
)
1828 (propagate-from-sets var type
))
1830 (let ((use (lvar-uses arg
)))
1832 (let ((leaf (ref-leaf use
)))
1833 (when (and (constant-reference-p use
)
1834 (csubtypep (leaf-type leaf
)
1835 ;; (NODE-DERIVED-TYPE USE) would
1836 ;; be better -- APD, 2003-05-15
1838 (propagate-to-refs var type
)
1839 (unless (preserve-single-use-debug-var-p call var
)
1840 (update-dependent-casts leaf arg
)
1841 (let ((use-component (node-component use
)))
1844 (cond ((eq (node-component ref
) use-component
)
1847 (aver (lambda-toplevelish-p (lambda-home fun
)))
1852 (null (rest (leaf-refs var
)))
1853 (not (preserve-single-use-debug-var-p call var
))
1854 (substitute-single-use-lvar arg var
)))
1856 (propagate-to-refs var type
))))
1860 (when (every #'not
(basic-combination-args call
))
1865 ;;; This function is called when one of the args to a non-LET local
1866 ;;; call changes. For each changed argument corresponding to an unset
1867 ;;; variable, we compute the union of the types across all calls and
1868 ;;; propagate this type information to the var's refs.
1870 ;;; If the function has an entry-fun, then we don't do anything: since
1871 ;;; it has a XEP we would not discover anything.
1873 ;;; If the function is an optional-entry-point, we will just make sure
1874 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1875 ;;; can erronously propagate too strict types into refs: see
1876 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1878 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1879 ;;; corresponding to changed arguments in CALL, since the only use in
1880 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1882 (defun propagate-local-call-args (call fun
)
1883 (declare (type combination call
) (type clambda fun
))
1884 (unless (functional-entry-fun fun
)
1885 (if (lambda-optional-dispatch fun
)
1886 ;; We can still make sure &REST is known to be a list.
1887 (loop for var in
(lambda-vars fun
)
1888 do
(let ((info (lambda-var-arg-info var
)))
1889 (when (and info
(eq :rest
(arg-info-kind info
)))
1890 (propagate-from-sets var
(specifier-type 'list
)))))
1892 (let* ((vars (lambda-vars fun
))
1893 (union (mapcar (lambda (arg var
)
1895 (lvar-reoptimize arg
)
1896 (null (basic-var-sets var
)))
1898 (basic-combination-args call
)
1900 (this-ref (lvar-use (basic-combination-fun call
))))
1902 (dolist (arg (basic-combination-args call
))
1904 (setf (lvar-reoptimize arg
) nil
)))
1906 (dolist (ref (leaf-refs fun
))
1907 (let ((dest (node-dest ref
)))
1908 (unless (or (eq ref this-ref
) (not dest
))
1910 (mapcar (lambda (this-arg old
)
1912 (setf (lvar-reoptimize this-arg
) nil
)
1913 (type-union (lvar-type this-arg
) old
)))
1914 (basic-combination-args dest
)
1917 (loop for var in vars
1919 when type do
(propagate-to-refs var type
)))))
1923 ;;;; multiple values optimization
1925 ;;; Do stuff to notice a change to a MV combination node. There are
1926 ;;; two main branches here:
1927 ;;; -- If the call is local, then it is already a MV let, or should
1928 ;;; become one. Note that although all :LOCAL MV calls must eventually
1929 ;;; be converted to :MV-LETs, there can be a window when the call
1930 ;;; is local, but has not been LET converted yet. This is because
1931 ;;; the entry-point lambdas may have stray references (in other
1932 ;;; entry points) that have not been deleted yet.
1933 ;;; -- The call is full. This case is somewhat similar to the non-MV
1934 ;;; combination optimization: we propagate return type information and
1935 ;;; notice non-returning calls. We also have an optimization
1936 ;;; which tries to convert MV-CALLs into MV-binds.
1937 (defun ir1-optimize-mv-combination (node)
1938 (let ((fun (basic-combination-fun node
)))
1939 (unless (and (node-p (lvar-uses fun
))
1940 (node-to-be-deleted-p (lvar-uses fun
)))
1941 (ecase (basic-combination-kind node
)
1943 (let ((lambda (combination-lambda node
)))
1944 (when (lvar-reoptimize fun
)
1945 (setf (lvar-reoptimize fun
) nil
)
1946 (maybe-let-convert lambda
))
1947 (cond ((neq (functional-kind lambda
) :mv-let
)
1948 (loop for arg in
(basic-combination-args node
)
1950 (setf (lvar-reoptimize arg
) nil
)))
1951 ((convert-mv-bind-to-let node
))
1953 (propagate-let-args node lambda
)))))
1955 (let* ((fun-changed (lvar-reoptimize fun
)))
1956 (loop for arg in
(basic-combination-args node
)
1958 (setf (lvar-reoptimize arg
) nil
))
1960 (setf (lvar-reoptimize fun
) nil
)
1961 (let ((type (lvar-type fun
)))
1962 (when (fun-type-p type
)
1963 (derive-node-type node
(fun-type-returns type
))))
1964 (maybe-terminate-block node nil
)
1965 (let ((use (lvar-uses fun
)))
1966 (when (and (ref-p use
) (functional-p (ref-leaf use
)))
1967 (convert-call-if-possible use node
)
1968 (when (eq (basic-combination-kind node
) :local
)
1969 (maybe-let-convert (ref-leaf use
))))))
1970 (unless (or (eq (basic-combination-kind node
) :local
)
1971 (eq (lvar-fun-name fun
) '%throw
))
1972 (ir1-optimize-mv-call node
))))
1977 (defun count-values (call &optional min
)
1978 (loop for arg in
(basic-combination-args call
)
1979 for nvals
= (nth-value 1 (values-types (lvar-derived-type arg
)))
1980 if
(eq nvals
:unknown
)
1986 (defun check-mv-call-arguments (call)
1987 (let ((*compiler-error-context
* call
)
1988 (max-accepted (nth-value 1 (fun-type-nargs
1990 (basic-combination-fun call
)))))
1991 (min-args (count-values call t
)))
1992 (cond ((not min-args
))
1993 ((and max-accepted
(> min-args max-accepted
))
1995 "MULTIPLE-VALUE-CALL with at least ~R values when the function expects ~
1997 min-args max-accepted
)
1998 (setf (basic-combination-kind call
) :error
)
2002 (defun ir1-optimize-mv-call (node)
2003 (let* ((fun (basic-combination-fun node
))
2004 (uses (lvar-uses (basic-combination-fun node
)))
2005 (count (count-values node
)))
2007 (with-ir1-environment-from-node node
2008 (let* ((dums (make-gensym-list count
))
2010 (ref-p (ref-p uses
))
2011 (new-fun (ir1-convert-lambda
2012 `(lambda (&optional
,@dums
&rest
,ignore
)
2013 (declare (ignore ,ignore
))
2014 ;; REFERENCE-LEAF does a better job referencing
2015 ;; DEFINED-FUNs than just using the LVAR.
2017 `(%funcall
,(ref-leaf uses
) ,@dums
))
2019 `(%funcall-lvar
,fun
,@dums
)))))))
2021 (change-ref-leaf uses new-fun
))
2023 (reoptimize-lvar fun
)
2024 (setf (basic-combination-fun node
)
2025 (insert-ref-before new-fun node
))))
2026 (aver (eq (basic-combination-kind node
) :full
))
2027 (locall-analyze-component *current-component
*)
2028 (aver (eq (basic-combination-kind node
) :local
))))
2029 ;; The total argument count is not known, but all the known
2030 ;; values can already cause a conflict.
2031 (check-mv-call-arguments node
)))
2035 ;;; (multiple-value-bind
2044 ;;; What we actually do is convert the VALUES combination into a
2045 ;;; normal LET combination calling the original :MV-LET lambda. If
2046 ;;; there are extra args to VALUES, discard the corresponding
2047 ;;; lvars. If there are insufficient args, insert references to NIL.
2048 (defun convert-mv-bind-to-let (call)
2049 (declare (type mv-combination call
))
2050 (let* ((args (basic-combination-args call
))
2051 (use (lvar-uses (first args
))))
2052 (when (and (singleton-p args
)
2054 (eq (lvar-fun-name (combination-fun use
))
2056 (setf (lvar-reoptimize (car args
)) nil
)
2057 (let* ((fun (combination-lambda call
))
2058 (vars (lambda-vars fun
))
2059 (vals (combination-args use
))
2060 (nvars (length vars
))
2061 (nvals (length vals
)))
2062 (cond ((> nvals nvars
)
2063 (mapc #'flush-dest
(subseq vals nvars
))
2064 (setq vals
(subseq vals
0 nvars
)))
2066 (with-ir1-environment-from-node use
2067 (let ((node-prev (node-prev use
)))
2068 (setf (node-prev use
) nil
)
2069 (setf (ctran-next node-prev
) nil
)
2070 (collect ((res vals
))
2071 (loop for count below
(- nvars nvals
)
2072 for prev
= node-prev then ctran
2073 for ctran
= (make-ctran)
2074 and lvar
= (make-lvar use
)
2075 do
(reference-constant prev ctran lvar nil
)
2077 finally
(link-node-to-previous-ctran
2079 (setq vals
(res)))))))
2080 (setf (combination-args use
) vals
)
2081 (flush-dest (combination-fun use
))
2082 (let ((fun-lvar (basic-combination-fun call
)))
2083 (setf (lvar-dest fun-lvar
) use
)
2084 (setf (combination-fun use
) fun-lvar
)
2085 (flush-lvar-externally-checkable-type fun-lvar
))
2086 (setf (combination-kind use
) :local
)
2087 (setf (functional-kind fun
) :let
)
2088 (flush-dest (first (basic-combination-args call
)))
2091 (reoptimize-lvar (first vals
)))
2092 ;; Propagate derived types from the VALUES call to its args:
2093 ;; transforms can leave the VALUES call with a better type
2094 ;; than its args have, so make sure not to throw that away.
2095 (let ((types (values-type-types (node-derived-type use
))))
2098 (let ((type (pop types
)))
2099 (assert-lvar-type val type
**zero-typecheck-policy
**)))))
2100 ;; Propagate declared types of MV-BIND variables.
2101 (propagate-to-args use fun
)
2102 (reoptimize-call use
))
2106 ;;; (values-list (list x y z))
2111 ;;; In implementation, this is somewhat similar to
2112 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2113 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2114 ;;; (allowing the LIST to be flushed.)
2116 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2117 (defoptimizer (values-list optimizer
) ((list) node
)
2118 (let ((use (lvar-uses list
)))
2119 (when (and (combination-p use
)
2120 (eq (lvar-fun-name (combination-fun use
))
2123 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2124 (change-ref-leaf (lvar-uses (combination-fun node
))
2125 (find-free-fun 'values
"in a strange place"))
2126 (setf (combination-kind node
) :full
)
2127 (let ((args (combination-args use
)))
2129 (setf (lvar-dest arg
) node
)
2130 (flush-lvar-externally-checkable-type arg
))
2131 (setf (combination-args use
) nil
)
2133 (flush-combination use
)
2134 (setf (combination-args node
) args
))
2137 ;;; If VALUES appears in a non-MV context, then effectively convert it
2138 ;;; to a PROG1. This allows the computation of the additional values
2139 ;;; to become dead code.
2140 (deftransform values
((&rest vals
) * * :node node
)
2141 (unless (lvar-single-value-p (node-lvar node
))
2142 (give-up-ir1-transform))
2143 (setf (node-derived-type node
)
2144 (make-short-values-type (list (single-value-type
2145 (node-derived-type node
)))))
2146 (principal-lvar-single-valuify (node-lvar node
))
2148 (let ((dummies (make-gensym-list (length (cdr vals
)))))
2149 `(lambda (val ,@dummies
)
2150 (declare (ignore ,@dummies
))
2156 (defun delete-cast (cast)
2157 (declare (type cast cast
))
2158 (let ((value (cast-value cast
))
2159 (lvar (cast-lvar cast
)))
2160 (when (and (bound-cast-p cast
)
2161 (bound-cast-check cast
))
2162 (flush-combination (bound-cast-check cast
))
2163 (setf (bound-cast-check cast
) nil
))
2164 (delete-filter cast lvar value
)
2166 (reoptimize-lvar lvar
)
2167 (when (lvar-single-value-p lvar
)
2168 (note-single-valuified-lvar lvar
)))
2171 (defun compile-time-type-error-context (context)
2172 #+sb-xc-host context
2173 #-sb-xc-host
(source-to-string context
))
2175 (defun may-delete-function-designator-cast (cast)
2176 ;; If the destination is a combination-fun that means the function
2177 ;; is called here and not passed somewhere else, there's no longer a
2178 ;; need to check the function type, the arguments to the call will
2180 (let* ((lvar (cast-lvar cast
))
2183 (and (basic-combination-p dest
)
2184 (eq (basic-combination-fun dest
) lvar
))))
2186 (defun may-delete-bound-cast (cast)
2187 (when (bound-cast-check cast
)
2188 (when (constant-lvar-p (bound-cast-bound cast
))
2189 (setf (cast-asserted-type cast
)
2190 (specifier-type `(integer 0 (,(lvar-value (bound-cast-bound cast
)))))
2191 (bound-cast-derived cast
) t
))
2192 (when (policy cast
(= insert-array-bounds-checks
0))
2193 (flush-combination (bound-cast-check cast
))
2194 (setf (bound-cast-check cast
) nil
)))
2195 (bound-cast-derived cast
))
2197 (defun may-delete-modifying-cast (cast)
2198 (or (not (modifying-cast-caller cast
)) ;; already warned
2199 (let* ((value (cast-value cast
))
2200 (uses (lvar-uses value
))
2201 (lvar (or (and (ref-p uses
)
2202 (lambda-var-ref-lvar uses
))
2204 (when (constant-lvar-p lvar
)
2205 (let ((*compiler-error-context
* cast
)
2206 (value (lvar-value lvar
)))
2209 (not (arrayp value
))
2210 (not (type= (cast-asserted-type cast
)
2211 (specifier-type 'hash-table
))))
2212 (typep value
'(vector * 0)))
2213 (warn 'constant-modified
2214 :fun-name
(shiftf (modifying-cast-caller cast
) nil
) ;; warn once
2218 (defun may-delete-cast-with-hook (cast)
2219 (or (not (cast-with-hook-hook cast
)) ;; run once
2220 (let ((value (cast-value cast
)))
2221 (when (constant-lvar-p value
)
2222 (let ((*compiler-error-context
* cast
))
2223 (funcall (cast-with-hook-hook cast
)
2226 (setf (cast-with-hook-hook cast
) nil
))
2229 (defun may-delete-cast (cast)
2231 (vestigial-exit-cast
2233 (function-designator-cast
2234 (may-delete-function-designator-cast cast
))
2236 (may-delete-bound-cast cast
))
2238 (may-delete-modifying-cast cast
))
2240 (may-delete-cast-with-hook cast
))
2243 ;;; Delete or move around casts when possible
2244 (defun maybe-delete-cast (cast)
2245 (let ((lvar (cast-lvar cast
))
2246 (value (cast-value cast
)))
2247 (cond ((not (may-delete-cast cast
))
2249 ((values-subtypep (lvar-derived-type value
)
2250 (cast-asserted-type cast
))
2253 ((and (listp (lvar-uses value
))
2255 ;; Turn (the vector (if x y #()) into
2256 ;; (if x (the vector y) #())
2257 (let ((atype (cast-asserted-type cast
))
2258 (ctran (node-next cast
))
2259 (dest (lvar-dest lvar
))
2262 (do-uses (use value
)
2263 (when (and (values-subtypep (node-derived-type use
) atype
)
2264 (immediately-used-p value use
))
2266 (when ctran
(ensure-block-start ctran
))
2267 (setq next-block
(first (block-succ (node-block cast
))))
2268 (ensure-block-start (node-prev cast
))
2269 (reoptimize-lvar lvar
)
2270 (setf (lvar-%derived-type value
) nil
))
2271 (%delete-lvar-use use
)
2272 (add-lvar-use use lvar
)
2273 (unlink-blocks (node-block use
) (node-block cast
))
2274 (link-blocks (node-block use
) next-block
)
2275 (when (and (return-p dest
)
2276 (basic-combination-p use
)
2277 (eq (basic-combination-kind use
) :local
))
2279 (dolist (use (merges))
2280 (merge-tail-sets use
))))))))
2282 (defun ir1-optimize-cast (cast)
2283 (declare (type cast cast
))
2284 (unless (maybe-delete-cast cast
)
2285 (let* ((value (cast-value cast
))
2286 (atype (cast-asserted-type cast
))
2287 (value-type (lvar-derived-type value
))
2288 (int (values-type-intersection value-type atype
)))
2289 (derive-node-type cast int
)
2290 (cond ((or (neq int
*empty-type
*)
2291 (eq value-type
*empty-type
*)))
2292 ;; No need to transform into an analog of
2293 ;; %COMPILE-TIME-TYPE-ERROR, %CHECK-BOUND will signal at
2294 ;; run-time and %CHECK-BOUND ir2-converter will signal at
2295 ;; compile-time if it survives further stages of ir1
2297 ((bound-cast-p cast
))
2299 (let ((context (node-source-form cast
))
2300 (detail (lvar-all-sources (cast-value cast
))))
2301 (unless (cast-silent-conflict cast
)
2304 (if (cast-single-value-p cast
)
2306 `(multiple-value-call #'list
'dummy
))))
2309 ;; FIXME: Derived type.
2310 (if (cast-silent-conflict cast
)
2311 (let ((dummy-sym (gensym)))
2312 `(let ((,dummy-sym
'dummy
))
2313 ,(internal-type-error-call dummy-sym atype
2314 (cast-context cast
))
2316 `(%compile-time-type-error
'dummy
2317 ',(type-specifier atype
)
2318 ',(type-specifier value-type
)
2320 ',(compile-time-type-error-context context
)
2321 ',(cast-context cast
)))))
2322 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2323 ;; functions, so we declare the return type of
2324 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2326 (setq value
(cast-value cast
))
2327 (derive-node-type (lvar-uses value
) *empty-type
*)
2328 (maybe-terminate-block (lvar-uses value
) nil
)
2329 ;; FIXME: Is it necessary?
2330 (aver (null (block-pred (node-block cast
))))
2331 (delete-block-lazily (node-block cast
))
2332 (return-from ir1-optimize-cast
)))
2333 (when (eq (node-derived-type cast
) *empty-type
*)
2334 (maybe-terminate-block cast nil
))
2336 (when (and (cast-%type-check cast
)
2337 (values-subtypep value-type
2338 (cast-type-to-check cast
)))
2339 (setf (cast-%type-check cast
) nil
))
2340 (setf (node-reoptimize cast
) nil
))))
2342 (defun cast-type-check (cast)
2343 (declare (type cast cast
))
2344 (let ((check (cast-%type-check cast
)))
2346 (cast-reoptimize cast
)
2347 (values-subtypep (lvar-derived-type (cast-value cast
))
2348 (cast-type-to-check cast
)))
2349 (setf (cast-%type-check cast
) nil
)