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 (or (let ((use (principal-lvar-use thing
)))
27 (and (ref-p use
) (constant-p (ref-leaf use
))))
28 ;; check for EQL types and singleton numeric types
29 (values (type-singleton-p (lvar-type thing
))))))
31 ;;; Same as above except it doesn't consider EQL types
32 (defun strictly-constant-lvar-p (thing)
33 (declare (type (or lvar null
) thing
))
35 (let ((use (principal-lvar-use thing
)))
36 (and (ref-p use
) (constant-p (ref-leaf use
))))))
38 ;;; Return the constant value for an LVAR whose only use is a constant
40 (declaim (ftype (function (lvar) t
) lvar-value
))
41 (defun lvar-value (lvar)
42 (let ((use (principal-lvar-use lvar
))
43 (type (lvar-type lvar
))
46 (constant-p (setf leaf
(ref-leaf use
))))
48 (multiple-value-bind (constantp value
) (type-singleton-p type
)
50 (error "~S used on non-constant LVAR ~S" 'lvar-value lvar
))
53 ;;;; interface for obtaining results of type inference
55 ;;; Our best guess for the type of this lvar's value. Note that this
56 ;;; may be VALUES or FUNCTION type, which cannot be passed as an
57 ;;; argument to the normal type operations. See LVAR-TYPE.
59 ;;; The result value is cached in the LVAR-%DERIVED-TYPE slot. If the
60 ;;; slot is true, just return that value, otherwise recompute and
61 ;;; stash the value there.
62 (eval-when (:compile-toplevel
:execute
)
63 (#+sb-xc-host cl
:defmacro
64 #-sb-xc-host sb
!xc
:defmacro
65 lvar-type-using
(lvar accessor
)
66 `(let ((uses (lvar-uses ,lvar
)))
67 (cond ((null uses
) *empty-type
*)
69 (do ((res (,accessor
(first uses
))
70 (values-type-union (,accessor
(first current
))
72 (current (rest uses
) (rest current
)))
73 ((or (null current
) (eq res
*wild-type
*))
78 (defun %lvar-derived-type
(lvar)
79 (lvar-type-using lvar node-derived-type
))
81 ;;; Return the derived type for LVAR's first value. This is guaranteed
82 ;;; not to be a VALUES or FUNCTION type.
83 (declaim (ftype (sfunction (lvar) ctype
) lvar-type
))
84 (defun lvar-type (lvar)
85 (single-value-type (lvar-derived-type lvar
)))
87 ;;; LVAR-CONSERVATIVE-TYPE
89 ;;; Certain types refer to the contents of an object, which can
90 ;;; change without type derivation noticing: CONS types and ARRAY
91 ;;; types suffer from this:
93 ;;; (let ((x (the (cons fixnum fixnum) (cons a b))))
95 ;;; (+ (car x) (cdr x)))
97 ;;; Python doesn't realize that the SETF CAR can change the type of X -- so we
98 ;;; cannot use LVAR-TYPE which gets the derived results. Worse, still, instead
99 ;;; of (SETF CAR) we might have a call to a user-defined function FOO which
100 ;;; does the same -- so there is no way to use the derived information in
103 ;;; So, the conservative option is to use the derived type if the leaf has
104 ;;; only a single ref -- in which case there cannot be a prior call that
105 ;;; mutates it. Otherwise we use the declared type or punt to the most general
106 ;;; type we know to be correct for sure.
107 (defun lvar-conservative-type (lvar)
108 (let ((derived-type (lvar-type lvar
))
109 (t-type *universal-type
*))
110 ;; Recompute using NODE-CONSERVATIVE-TYPE instead of derived type if
111 ;; necessary -- picking off some easy cases up front.
112 (cond ((or (eq derived-type t-type
)
113 ;; Can't use CSUBTYPEP!
114 (type= derived-type
(specifier-type 'list
))
115 (type= derived-type
(specifier-type 'null
)))
117 ((and (cons-type-p derived-type
)
118 (eq t-type
(cons-type-car-type derived-type
))
119 (eq t-type
(cons-type-cdr-type derived-type
)))
121 ((and (array-type-p derived-type
)
122 (or (not (array-type-complexp derived-type
))
123 (let ((dimensions (array-type-dimensions derived-type
)))
124 (or (eq '* dimensions
)
125 (every (lambda (dim) (eq '* dim
)) dimensions
)))))
127 ((type-needs-conservation-p derived-type
)
128 (single-value-type (lvar-type-using lvar node-conservative-type
)))
132 (defun node-conservative-type (node)
133 (let* ((derived-values-type (node-derived-type node
))
134 (derived-type (single-value-type derived-values-type
)))
136 (let ((leaf (ref-leaf node
)))
137 (if (and (basic-var-p leaf
)
138 (cdr (leaf-refs leaf
)))
140 (if (eq :declared
(leaf-where-from leaf
))
142 (conservative-type derived-type
)))
143 derived-values-type
))
144 derived-values-type
)))
146 (defun conservative-type (type)
147 (cond ((or (eq type
*universal-type
*)
148 (eq type
(specifier-type 'list
))
149 (eq type
(specifier-type 'null
)))
152 (specifier-type 'cons
))
154 (if (array-type-complexp type
)
156 ;; ADJUST-ARRAY may change dimensions, but rank stays same.
157 (let ((old (array-type-dimensions type
)))
160 (mapcar (constantly '*) old
)))
161 ;; Complexity cannot change.
162 :complexp
(array-type-complexp type
)
163 ;; Element type cannot change.
164 :element-type
(array-type-element-type type
)
165 :specialized-element-type
(array-type-specialized-element-type type
))
166 ;; Simple arrays cannot change at all.
169 ;; Conservative union type is an union of conservative types.
170 (let ((res *empty-type
*))
171 (dolist (part (union-type-types type
) res
)
172 (setf res
(type-union res
(conservative-type part
))))))
176 ;; If the type contains some CONS types, the conservative type contains all
178 (when (types-equal-or-intersect type
(specifier-type 'cons
))
179 (setf type
(type-union type
(specifier-type 'cons
))))
180 ;; Similarly for non-simple arrays -- it should be possible to preserve
181 ;; more information here, but really...
182 (let ((non-simple-arrays (specifier-type '(and array
(not simple-array
)))))
183 (when (types-equal-or-intersect type non-simple-arrays
)
184 (setf type
(type-union type non-simple-arrays
))))
187 (defun type-needs-conservation-p (type)
188 (cond ((eq type
*universal-type
*)
189 ;; Excluding T is necessary, because we do want type derivation to
190 ;; be able to narrow it down in case someone (most like a macro-expansion...)
191 ;; actually declares something as having type T.
193 ((or (cons-type-p type
) (and (array-type-p type
) (array-type-complexp type
)))
194 ;; Covered by the next case as well, but this is a quick test.
196 ((types-equal-or-intersect type
(specifier-type '(or cons
(and array
(not simple-array
)))))
199 ;;; If LVAR is an argument of a function, return a type which the
200 ;;; function checks LVAR for.
201 #!-sb-fluid
(declaim (inline lvar-externally-checkable-type
))
202 (defun lvar-externally-checkable-type (lvar)
203 (or (lvar-%externally-checkable-type lvar
)
204 (%lvar-%externally-checkable-type lvar
)))
205 (defun %lvar-%externally-checkable-type
(lvar)
206 (declare (type lvar lvar
))
207 (let ((dest (lvar-dest lvar
)))
208 (if (not (and dest
(combination-p dest
)))
209 ;; TODO: MV-COMBINATION
210 (setf (lvar-%externally-checkable-type lvar
) *wild-type
*)
211 (let* ((fun (combination-fun dest
))
212 (args (combination-args dest
))
213 (fun-type (lvar-type fun
)))
214 (setf (lvar-%externally-checkable-type fun
) *wild-type
*)
215 (if (or (not (call-full-like-p dest
))
216 (not (fun-type-p fun-type
))
217 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
218 (fun-type-wild-args fun-type
))
221 (setf (lvar-%externally-checkable-type arg
)
223 (map-combination-args-and-types
225 (setf (lvar-%externally-checkable-type arg
)
226 (acond ((lvar-%externally-checkable-type arg
)
227 (values-type-intersection
228 it
(coerce-to-values type
)))
229 (t (coerce-to-values type
)))))
231 (or (lvar-%externally-checkable-type lvar
) *wild-type
*))
233 ;;;; interface routines used by optimizers
235 ;;; This function is called by optimizers to indicate that something
236 ;;; interesting has happened to the value of LVAR. Optimizers must
237 ;;; make sure that they don't call for reoptimization when nothing has
238 ;;; happened, since optimization will fail to terminate.
240 ;;; We clear any cached type for the lvar and set the reoptimize flags
241 ;;; on everything in sight.
242 (defun reoptimize-lvar (lvar)
243 (declare (type (or lvar null
) lvar
))
245 (setf (lvar-%derived-type lvar
) nil
)
246 (let ((dest (lvar-dest lvar
)))
248 (setf (lvar-reoptimize lvar
) t
)
249 (setf (node-reoptimize dest
) t
)
250 (binding* (;; Since this may be called during IR1 conversion,
251 ;; PREV may be missing.
252 (prev (node-prev dest
) :exit-if-null
)
253 (block (ctran-block prev
))
254 (component (block-component block
)))
255 (when (typep dest
'cif
)
256 (setf (block-test-modified block
) t
))
257 (setf (block-reoptimize block
) t
)
258 (reoptimize-component component
:maybe
))))
260 (setf (block-type-check (node-block node
)) t
)))
263 (defun reoptimize-lvar-uses (lvar)
264 (declare (type lvar lvar
))
266 (setf (node-reoptimize use
) t
)
267 (setf (block-reoptimize (node-block use
)) t
)
268 (reoptimize-component (node-component use
) :maybe
)))
270 ;;; Annotate NODE to indicate that its result has been proven to be
271 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
272 ;;; only correct way to supply information discovered about a node's
273 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
274 ;;; information may be lost and reoptimization may not happen.
276 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
277 ;;; intersection is different from the old type, then we do a
278 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
279 (defun derive-node-type (node rtype
&key from-scratch
)
280 (declare (type valued-node node
) (type ctype rtype
))
281 (let* ((initial-type (node-derived-type node
))
282 (node-type (if from-scratch
285 (unless (eq initial-type rtype
)
286 (let ((int (values-type-intersection node-type rtype
))
287 (lvar (node-lvar node
)))
288 (when (type/= initial-type int
)
289 (when (and *check-consistency
*
290 (eq int
*empty-type
*)
291 (not (eq rtype
*empty-type
*)))
292 (aver (not from-scratch
))
293 (let ((*compiler-error-context
* node
))
295 "New inferred type ~S conflicts with old type:~
296 ~% ~S~%*** possible internal error? Please report this."
297 (type-specifier rtype
) (type-specifier node-type
))))
298 (setf (node-derived-type node
) int
)
299 ;; If the new type consists of only one object, replace the
300 ;; node with a constant reference.
301 (when (and (ref-p node
)
302 (lambda-var-p (ref-leaf node
)))
303 (let ((type (single-value-type int
)))
304 (when (and (member-type-p type
)
305 (eql 1 (member-type-size type
)))
306 (change-ref-leaf node
(find-constant
307 (first (member-type-members type
)))))))
308 (reoptimize-lvar lvar
)))))
311 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
312 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
313 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
314 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
315 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
316 (defun assert-lvar-type (lvar type policy
)
317 (declare (type lvar lvar
) (type ctype type
))
318 (unless (values-subtypep (lvar-derived-type lvar
) type
)
319 (let ((internal-lvar (make-lvar))
320 (dest (lvar-dest lvar
)))
321 (substitute-lvar internal-lvar lvar
)
322 (let ((cast (insert-cast-before dest lvar type policy
)))
323 (use-lvar cast internal-lvar
)
329 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
330 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
331 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
332 ;;; we are done, then another iteration would be beneficial.
333 (defun ir1-optimize (component fastp
)
334 (declare (type component component
))
335 (setf (component-reoptimize component
) nil
)
336 (loop with block
= (block-next (component-head component
))
337 with tail
= (component-tail component
)
338 for last-block
= block
339 until
(eq block tail
)
341 ;; We delete blocks when there is either no predecessor or the
342 ;; block is in a lambda that has been deleted. These blocks
343 ;; would eventually be deleted by DFO recomputation, but doing
344 ;; it here immediately makes the effect available to IR1
346 ((or (block-delete-p block
)
347 (null (block-pred block
)))
348 (delete-block-lazily block
)
349 (setq block
(clean-component component block
)))
350 ((eq (functional-kind (block-home-lambda block
)) :deleted
)
351 ;; Preserve the BLOCK-SUCC invariant that almost every block has
352 ;; one successor (and a block with DELETE-P set is an acceptable
354 (mark-for-deletion block
)
355 (setq block
(clean-component component block
)))
358 (let ((succ (block-succ block
)))
359 (unless (singleton-p succ
)
362 (let ((last (block-last block
)))
365 (flush-dest (if-test last
))
366 (when (unlink-node last
)
369 (when (maybe-delete-exit last
)
372 (unless (join-successor-if-possible block
)
375 (when (and (not fastp
) (block-reoptimize block
) (block-component block
))
376 (aver (not (block-delete-p block
)))
377 (ir1-optimize-block block
))
379 (cond ((and (block-delete-p block
) (block-component block
))
380 (setq block
(clean-component component block
)))
381 ((and (block-flush-p block
) (block-component block
))
382 (flush-dead-code block
)))))
383 do
(when (eq block last-block
)
384 (setq block
(block-next block
))))
388 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
391 ;;; Note that although they are cleared here, REOPTIMIZE flags might
392 ;;; still be set upon return from this function, meaning that further
393 ;;; optimization is wanted (as a consequence of optimizations we did).
394 (defun ir1-optimize-block (block)
395 (declare (type cblock block
))
396 ;; We clear the node and block REOPTIMIZE flags before doing the
397 ;; optimization, not after. This ensures that the node or block will
398 ;; be reoptimized if necessary.
399 (setf (block-reoptimize block
) nil
)
400 (do-nodes (node nil block
:restart-p t
)
401 (when (node-reoptimize node
)
402 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
403 (setf (node-reoptimize node
) nil
)
407 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
408 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
409 ;; any argument changes.
410 (ir1-optimize-combination node
))
412 (ir1-optimize-if node
))
414 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
415 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
416 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
418 (setf (node-reoptimize node
) t
)
419 (ir1-optimize-return node
))
421 (ir1-optimize-mv-combination node
))
423 ;; With an EXIT, we derive the node's type from the VALUE's
425 (let ((value (exit-value node
)))
427 (derive-node-type node
(lvar-derived-type value
)))))
429 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
430 ;; is accurate till the node actually has been reoptimized.
431 (setf (node-reoptimize node
) t
)
432 (ir1-optimize-set node
))
434 (ir1-optimize-cast node
)))))
438 ;;; Try to join with a successor block. If we succeed, we return true,
440 (defun join-successor-if-possible (block)
441 (declare (type cblock block
))
442 (let ((next (first (block-succ block
))))
443 (when (block-start next
) ; NEXT is not an END-OF-COMPONENT marker
444 (cond ( ;; We cannot combine with a successor block if:
446 ;; the successor has more than one predecessor;
447 (rest (block-pred next
))
448 ;; the successor is the current block (infinite loop);
450 ;; the next block has a different cleanup, and thus
451 ;; we may want to insert cleanup code between the
452 ;; two blocks at some point;
453 (not (eq (block-end-cleanup block
)
454 (block-start-cleanup next
)))
455 ;; the next block has a different home lambda, and
456 ;; thus the control transfer is a non-local exit.
457 (not (eq (block-home-lambda block
)
458 (block-home-lambda next
)))
459 ;; Stack analysis phase wants ENTRY to start a block...
460 (entry-p (block-start-node next
))
461 (let ((last (block-last block
)))
462 (and (valued-node-p last
)
463 (awhen (node-lvar last
)
465 ;; ... and a DX-allocator to end a block.
466 (lvar-dynamic-extent it
)
467 ;; FIXME: This is a partial workaround for bug 303.
468 (consp (lvar-uses it
)))))))
471 (join-blocks block next
)
474 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
475 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
476 ;;; for the two blocks so that any indicated optimization gets done.
477 (defun join-blocks (block1 block2
)
478 (declare (type cblock block1 block2
))
479 (let* ((last1 (block-last block1
))
480 (last2 (block-last block2
))
481 (succ (block-succ block2
))
482 (start2 (block-start block2
)))
483 (do ((ctran start2
(node-next (ctran-next ctran
))))
485 (setf (ctran-block ctran
) block1
))
487 (unlink-blocks block1 block2
)
489 (unlink-blocks block2 block
)
490 (link-blocks block1 block
))
492 (setf (ctran-kind start2
) :inside-block
)
493 (setf (node-next last1
) start2
)
494 (setf (ctran-use start2
) last1
)
495 (setf (block-last block1
) last2
))
497 (setf (block-flags block1
)
498 (attributes-union (block-flags block1
)
500 (block-attributes type-asserted test-modified
)))
502 (let ((next (block-next block2
))
503 (prev (block-prev block2
)))
504 (setf (block-next prev
) next
)
505 (setf (block-prev next
) prev
))
509 ;;; Delete any nodes in BLOCK whose value is unused and which have no
510 ;;; side effects. We can delete sets of lexical variables when the set
511 ;;; variable has no references.
512 (defun flush-dead-code (block &aux victim
)
513 (declare (type cblock block
))
514 (setf (block-flush-p block
) nil
)
515 (do-nodes-backwards (node lvar block
:restart-p t
)
523 (when (flushable-combination-p node
)
525 (flush-combination node
)))
527 (when (eq (basic-combination-kind node
) :local
)
528 (let ((fun (combination-lambda node
)))
529 (when (dolist (var (lambda-vars fun
) t
)
530 (when (or (leaf-refs var
)
531 (lambda-var-sets var
))
534 (flush-dest (first (basic-combination-args node
)))
537 (let ((value (exit-value node
)))
541 (setf (exit-value node
) nil
))))
543 (let ((var (set-var node
)))
544 (when (and (lambda-var-p var
)
545 (null (leaf-refs var
)))
547 (flush-dest (set-value node
))
548 (setf (basic-var-sets var
)
549 (delq node
(basic-var-sets var
)))
550 (unlink-node node
))))
552 (unless (cast-type-check node
)
554 (flush-dest (cast-value node
))
555 (unlink-node node
))))))
559 ;;;; local call return type propagation
561 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
562 ;;; flag set. It iterates over the uses of the RESULT, looking for
563 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
564 ;;; call, then we union its type together with the types of other such
565 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
566 ;;; type with the RESULT's asserted type. We can make this
567 ;;; intersection now (potentially before type checking) because this
568 ;;; assertion on the result will eventually be checked (if
571 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
572 ;;; combination, which may change the successor of the call to be the
573 ;;; called function, and if so, checks if the call can become an
574 ;;; assignment. If we convert to an assignment, we abort, since the
575 ;;; RETURN has been deleted.
576 (defun find-result-type (node)
577 (declare (type creturn node
))
578 (let ((result (return-result node
)))
579 (collect ((use-union *empty-type
* values-type-union
))
580 (do-uses (use result
)
581 (let ((use-home (node-home-lambda use
)))
582 (cond ((or (eq (functional-kind use-home
) :deleted
)
583 (block-delete-p (node-block use
))))
584 ((and (basic-combination-p use
)
585 (eq (basic-combination-kind use
) :local
))
586 (aver (eq (lambda-tail-set use-home
)
587 (lambda-tail-set (combination-lambda use
))))
588 (when (combination-p use
)
589 (when (nth-value 1 (maybe-convert-tail-local-call use
))
590 (return-from find-result-type t
))))
592 (use-union (node-derived-type use
))))))
594 ;; (values-type-intersection
595 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
599 (setf (return-result-type node
) int
))))
602 ;;; Do stuff to realize that something has changed about the value
603 ;;; delivered to a return node. Since we consider the return values of
604 ;;; all functions in the tail set to be equivalent, this amounts to
605 ;;; bringing the entire tail set up to date. We iterate over the
606 ;;; returns for all the functions in the tail set, reanalyzing them
607 ;;; all (not treating NODE specially.)
609 ;;; When we are done, we check whether the new type is different from
610 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
611 ;;; all the lvars for references to functions in the tail set. This
612 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
613 ;;; results of the calls.
614 (defun ir1-optimize-return (node)
615 (declare (type creturn node
))
618 (let* ((tails (lambda-tail-set (return-lambda node
)))
619 (funs (tail-set-funs tails
)))
620 (collect ((res *empty-type
* values-type-union
))
622 (let ((return (lambda-return fun
)))
624 (when (node-reoptimize return
)
625 (setf (node-reoptimize return
) nil
)
626 (when (find-result-type return
)
628 (res (return-result-type return
)))))
630 (when (type/= (res) (tail-set-type tails
))
631 (setf (tail-set-type tails
) (res))
632 (dolist (fun (tail-set-funs tails
))
633 (dolist (ref (leaf-refs fun
))
634 (reoptimize-lvar (node-lvar ref
))))))))
640 ;;; Utility: return T if both argument cblocks are equivalent. For now,
641 ;;; detect only blocks that read the same leaf into the same lvar, and
642 ;;; continue to the same block.
643 (defun cblocks-equivalent-p (x y
)
644 (declare (type cblock x y
))
645 (and (ref-p (block-start-node x
))
646 (eq (block-last x
) (block-start-node x
))
648 (ref-p (block-start-node y
))
649 (eq (block-last y
) (block-start-node y
))
651 (equal (block-succ x
) (block-succ y
))
652 (eql (ref-lvar (block-start-node x
)) (ref-lvar (block-start-node y
)))
653 (eql (ref-leaf (block-start-node x
)) (ref-leaf (block-start-node y
)))))
655 ;;; Check whether the predicate is known to be true or false,
656 ;;; deleting the IF node in favor of the appropriate branch when this
658 ;;; Similarly, when both branches are equivalent, branch directly to either
660 ;;; Also, if the test has multiple uses, replicate the node when possible...
661 ;;; in fact, splice in direct jumps to the right branch if possible.
662 (defun ir1-optimize-if (node)
663 (declare (type cif node
))
664 (let ((test (if-test node
))
665 (block (node-block node
)))
666 (let* ((type (lvar-type test
))
667 (consequent (if-consequent node
))
668 (alternative (if-alternative node
))
670 (cond ((constant-lvar-p test
)
671 (if (lvar-value test
) alternative consequent
))
672 ((not (types-equal-or-intersect type
(specifier-type 'null
)))
674 ((type= type
(specifier-type 'null
))
676 ((or (eq consequent alternative
) ; Can this happen?
677 (cblocks-equivalent-p alternative consequent
))
680 (kill-if-branch-1 node test block victim
)
681 (return-from ir1-optimize-if
(values))))
682 (tension-if-if-1 node test block
)
683 (duplicate-if-if-1 node test block
)
686 ;; When we know that we only have a single successor, kill the victim
687 ;; ... unless the victim and the remaining successor are the same.
688 (defun kill-if-branch-1 (node test block victim
)
689 (declare (type cif node
))
691 (when (rest (block-succ block
))
692 (unlink-blocks block victim
))
693 (setf (component-reanalyze (node-component node
)) t
)
696 ;; When if/if conversion would leave (if ... (if nil ...)) or
697 ;; (if ... (if not-nil ...)), splice the correct successor right
699 (defun tension-if-if-1 (node test block
)
700 (when (and (eq (block-start-node block
) node
)
701 (listp (lvar-uses test
)))
703 (when (immediately-used-p test use
)
704 (let* ((type (single-value-type (node-derived-type use
)))
705 (target (if (type= type
(specifier-type 'null
))
706 (if-alternative node
)
707 (multiple-value-bind (typep surep
)
709 (and (not typep
) surep
710 (if-consequent node
))))))
712 (let ((pred (node-block use
)))
713 (cond ((listp (lvar-uses test
))
714 (change-block-successor pred block target
)
715 (delete-lvar-use use
))
717 ;; only one use left. Just kill the now-useless
718 ;; branch to avoid spurious code deletion notes.
719 (aver (rest (block-succ block
)))
722 (if (eql target
(if-alternative node
))
724 (if-alternative node
)))
725 (return-from tension-if-if-1
))))))))))
727 ;; Finally, duplicate EQ-nil tests
728 (defun duplicate-if-if-1 (node test block
)
729 (when (and (eq (block-start-node block
) node
)
730 (listp (lvar-uses test
)))
732 (when (immediately-used-p test use
)
733 (convert-if-if use node
)
734 ;; leave the last use as is, instead of replacing
735 ;; the (singly-referenced) CIF node with a duplicate.
736 (when (not (listp (lvar-uses test
))) (return))))))
738 ;;; Create a new copy of an IF node that tests the value of the node
739 ;;; USE. The test must have >1 use, and must be immediately used by
740 ;;; USE. NODE must be the only node in its block (implying that
741 ;;; block-start = if-test).
743 ;;; This optimization has an effect semantically similar to the
744 ;;; source-to-source transformation:
745 ;;; (IF (IF A B C) D E) ==>
746 ;;; (IF A (IF B D E) (IF C D E))
748 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
749 ;;; node so that dead code deletion notes will definitely not consider
750 ;;; either node to be part of the original source. One node might
751 ;;; become unreachable, resulting in a spurious note.
752 (defun convert-if-if (use node
)
753 (declare (type node use
) (type cif node
))
754 (with-ir1-environment-from-node node
755 (let* ((block (node-block node
))
756 (test (if-test node
))
757 (cblock (if-consequent node
))
758 (ablock (if-alternative node
))
759 (use-block (node-block use
))
760 (new-ctran (make-ctran))
761 (new-lvar (make-lvar))
762 (new-node (make-if :test new-lvar
764 :alternative ablock
))
765 (new-block (ctran-starts-block new-ctran
)))
766 (link-node-to-previous-ctran new-node new-ctran
)
767 (setf (lvar-dest new-lvar
) new-node
)
768 (setf (block-last new-block
) new-node
)
770 (unlink-blocks use-block block
)
771 (%delete-lvar-use use
)
772 (add-lvar-use use new-lvar
)
773 (link-blocks use-block new-block
)
775 (link-blocks new-block cblock
)
776 (link-blocks new-block ablock
)
778 (push "<IF Duplication>" (node-source-path node
))
779 (push "<IF Duplication>" (node-source-path new-node
))
781 (reoptimize-lvar test
)
782 (reoptimize-lvar new-lvar
)
783 (setf (component-reanalyze *current-component
*) t
)))
786 ;;;; exit IR1 optimization
788 ;;; This function attempts to delete an exit node, returning true if
789 ;;; it deletes the block as a consequence:
790 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
791 ;;; anything, since there is nothing to be done.
792 ;;; -- If the exit node and its ENTRY have the same home lambda then
793 ;;; we know the exit is local, and can delete the exit. We change
794 ;;; uses of the Exit-Value to be uses of the original lvar,
795 ;;; then unlink the node. If the exit is to a TR context, then we
796 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
797 ;;; their value to this exit.
798 ;;; -- If there is no value (as in a GO), then we skip the value
801 ;;; This function is also called by environment analysis, since it
802 ;;; wants all exits to be optimized even if normal optimization was
804 (defun maybe-delete-exit (node)
805 (declare (type exit node
))
806 (let ((value (exit-value node
))
807 (entry (exit-entry node
)))
809 (eq (node-home-lambda node
) (node-home-lambda entry
)))
810 (setf (entry-exits entry
) (delq node
(entry-exits entry
)))
812 (with-ir1-environment-from-node entry
813 ;; We can't simply use DELETE-FILTER to unlink the node
814 ;; and substitute some LVAR magic, as this can confuse the
815 ;; stack analysis if there's another EXIT to the same
816 ;; continuation. Instead, we fabricate a new block (in
817 ;; the same lexenv as the ENTRY, so it can't be merged
818 ;; backwards), insert a gimmicked CAST node to link up the
819 ;; LVAR holding the value being returned to the LVAR which
820 ;; is expecting to accept the value, thus placing the
821 ;; return value where it needs to be while still providing
822 ;; the hook required for stack analysis.
823 ;; -- AJB, 2014-Mar-03
824 (let* ((exit-block (node-block node
))
825 (new-ctran (make-ctran))
826 (new-block (ctran-starts-block new-ctran
))
829 :asserted-type
*wild-type
*
830 :type-to-check
*wild-type
*
832 :vestigial-exit-lexenv
(node-lexenv node
)
833 :vestigial-exit-entry-lexenv
(node-lexenv entry
)
835 ;; We only expect a single successor to EXIT-BLOCK,
836 ;; because it contains an EXIT node (which must end its
837 ;; block) and the only blocks that have more than once
838 ;; successor are those with IF nodes (which also must
839 ;; end their blocks). Still, just to be sure, we use a
840 ;; construct that guarantees an error if this
841 ;; expectation is violated.
844 (block-succ exit-block
)
846 ;; Finish creating the new block.
847 (link-node-to-previous-ctran cast-node new-ctran
)
848 (setf (block-last new-block
) cast-node
)
850 ;; Link the new block into the control sequence.
851 (unlink-blocks exit-block entry-block
)
852 (link-blocks exit-block new-block
)
853 (link-blocks new-block entry-block
)
855 ;; Finish re-pointing the value-holding LVAR to the
857 (setf (lvar-dest value
) cast-node
)
858 (setf (exit-value node
) nil
)
859 (reoptimize-lvar value
)
861 ;; Register the CAST node as providing a value to the
862 ;; LVAR for the continuation.
863 (add-lvar-use cast-node
(node-lvar node
))
864 (reoptimize-lvar (node-lvar node
))
866 ;; Remove the EXIT node.
869 ;; And, because we created a new block, we need to
870 ;; force component reanalysis (to assign a DFO number
871 ;; to the block if nothing else).
872 (setf (component-reanalyze *current-component
*) t
))))
873 (unlink-node node
)))))
876 ;;;; combination IR1 optimization
878 ;;; Report as we try each transform?
880 (defvar *show-transforms-p
* nil
)
882 (defun check-important-result (node info
)
883 (when (and (null (node-lvar node
))
884 (ir1-attributep (fun-info-attributes info
) important-result
))
885 (let ((*compiler-error-context
* node
))
887 "The return value of ~A should not be discarded."
888 (lvar-fun-name (basic-combination-fun node
) t
)))))
890 ;;; Do IR1 optimizations on a COMBINATION node.
891 (declaim (ftype (function (combination) (values)) ir1-optimize-combination
))
892 (defun ir1-optimize-combination (node)
893 (when (lvar-reoptimize (basic-combination-fun node
))
894 (propagate-fun-change node
)
895 (maybe-terminate-block node nil
))
896 (let ((args (basic-combination-args node
))
897 (kind (basic-combination-kind node
))
898 (info (basic-combination-fun-info node
)))
901 (let ((fun (combination-lambda node
)))
902 (if (eq (functional-kind fun
) :let
)
903 (propagate-let-args node fun
)
904 (propagate-local-call-args node fun
))))
908 (setf (lvar-reoptimize arg
) nil
))))
912 (setf (lvar-reoptimize arg
) nil
)))
914 (check-important-result node info
)
915 (let ((fun (fun-info-destroyed-constant-args info
)))
916 (when (and fun
(funcall fun args
))
917 (let ((*compiler-error-context
* node
))
918 (warn 'constant-modified
919 :fun-name
(lvar-fun-name
920 (basic-combination-fun node
) t
))
921 (setf (basic-combination-kind node
) :error
)
922 (return-from ir1-optimize-combination
))))
923 (let ((fun (fun-info-derive-type info
)))
925 (let ((res (funcall fun node
)))
927 (derive-node-type node
(coerce-to-values res
))
928 (maybe-terminate-block node nil
))))))
930 ;; Check against the DEFINED-TYPE unless TYPE is already good.
931 (let* ((fun (basic-combination-fun node
))
932 (uses (lvar-uses fun
))
933 (leaf (when (ref-p uses
) (ref-leaf uses
))))
934 (multiple-value-bind (type defined-type
)
935 (if (global-var-p leaf
)
936 (values (leaf-type leaf
) (leaf-defined-type leaf
))
938 (when (and (not (fun-type-p type
)) (fun-type-p defined-type
))
939 (validate-call-type node type leaf
)))))))
944 (setf (lvar-reoptimize arg
) nil
)))
945 (check-important-result node info
)
946 (let ((fun (fun-info-destroyed-constant-args info
)))
948 ;; If somebody is really sure that they want to modify
949 ;; constants, let them.
950 (policy node
(> check-constant-modification
0)))
951 (let ((destroyed-constant-args (funcall fun args
)))
952 (when destroyed-constant-args
953 (let ((*compiler-error-context
* node
))
954 (warn 'constant-modified
955 :fun-name
(lvar-fun-name
956 (basic-combination-fun node
)))
957 (setf (basic-combination-kind node
) :error
)
958 (return-from ir1-optimize-combination
))))))
960 (let ((attr (fun-info-attributes info
)))
961 (when (constant-fold-call-p node
)
962 (constant-fold-call node
)
963 (return-from ir1-optimize-combination
))
964 (when (and (ir1-attributep attr commutative
)
966 (constant-lvar-p (first args
))
967 (not (constant-lvar-p (second args
))))
968 (setf (basic-combination-args node
) (nreverse args
))))
969 (let ((fun (fun-info-derive-type info
)))
971 (let ((res (funcall fun node
)))
973 (derive-node-type node
(coerce-to-values res
))
974 (maybe-terminate-block node nil
)))))
976 (let ((fun (fun-info-optimizer info
)))
977 (unless (and fun
(funcall fun node
))
978 ;; First give the VM a peek at the call
979 (multiple-value-bind (style transform
)
980 (combination-implementation-style node
)
983 ;; The VM knows how to handle this.
986 ;; The VM mostly knows how to handle this. We need
987 ;; to massage the call slightly, though.
988 (transform-call node transform
(combination-fun-source-name node
)))
990 ;; Let transforms have a crack at it.
991 (dolist (x (fun-info-transforms info
))
993 (when *show-transforms-p
*
994 (let* ((lvar (basic-combination-fun node
))
995 (fname (lvar-fun-name lvar t
)))
996 (/show
"trying transform" x
(transform-function x
) "for" fname
)))
997 (unless (ir1-transform node x
)
999 (when *show-transforms-p
*
1000 (/show
"quitting because IR1-TRANSFORM result was NIL"))
1005 (defun xep-tail-combination-p (node)
1006 (and (combination-p node
)
1007 (let* ((lvar (combination-lvar node
))
1008 (dest (when (lvar-p lvar
) (lvar-dest lvar
)))
1009 (lambda (when (return-p dest
) (return-lambda dest
))))
1010 (and (lambda-p lambda
)
1011 (eq :external
(lambda-kind lambda
))))))
1013 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
1014 ;;; the block there, and link it to the component tail.
1016 ;;; Except when called during IR1 convertion, we delete the
1017 ;;; continuation if it has no other uses. (If it does have other uses,
1020 ;;; Termination on the basis of a continuation type is
1022 ;;; -- The continuation is deleted (hence the assertion is spurious), or
1023 ;;; -- We are in IR1 conversion (where THE assertions are subject to
1024 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
1025 ;;; uses can(?) be added later. -- APD, 2003-07-17
1027 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
1028 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p
)
1029 (declare (type (or basic-combination cast ref
) node
))
1030 (let* ((block (node-block node
))
1031 (lvar (node-lvar node
))
1032 (ctran (node-next node
))
1033 (tail (component-tail (block-component block
)))
1034 (succ (first (block-succ block
))))
1035 (declare (ignore lvar
))
1036 (unless (or (and (eq node
(block-last block
)) (eq succ tail
))
1037 (block-delete-p block
))
1038 ;; Even if the combination will never return, don't terminate if this
1039 ;; is the tail call of a XEP: doing that would inhibit TCO.
1040 (when (and (eq (node-derived-type node
) *empty-type
*)
1041 (not (xep-tail-combination-p node
)))
1042 (cond (ir1-converting-not-optimizing-p
1045 (aver (eq (block-last block
) node
)))
1047 (setf (block-last block
) node
)
1048 (setf (ctran-use ctran
) nil
)
1049 (setf (ctran-kind ctran
) :unused
)
1050 (setf (ctran-block ctran
) nil
)
1051 (setf (node-next node
) nil
)
1052 (link-blocks block
(ctran-starts-block ctran
)))))
1054 (node-ends-block node
)))
1056 (let ((succ (first (block-succ block
))))
1057 (unlink-blocks block succ
)
1058 (setf (component-reanalyze (block-component block
)) t
)
1059 (aver (not (block-succ block
)))
1060 (link-blocks block tail
)
1061 (cond (ir1-converting-not-optimizing-p
1062 (%delete-lvar-use node
))
1063 (t (delete-lvar-use node
)
1064 (when (null (block-pred succ
))
1065 (mark-for-deletion succ
)))))
1068 ;;; This is called both by IR1 conversion and IR1 optimization when
1069 ;;; they have verified the type signature for the call, and are
1070 ;;; wondering if something should be done to special-case the call. If
1071 ;;; CALL is a call to a global function, then see whether it defined
1073 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
1074 ;;; the expansion and change the call to call it. Expansion is
1075 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
1076 ;;; true, we never expand, since this function has already been
1077 ;;; converted. Local call analysis will duplicate the definition
1078 ;;; if necessary. We claim that the parent form is LABELS for
1079 ;;; context declarations, since we don't want it to be considered
1080 ;;; a real global function.
1081 ;;; -- If it is a known function, mark it as such by setting the KIND.
1083 ;;; We return the leaf referenced (NIL if not a leaf) and the
1084 ;;; FUN-INFO assigned.
1085 (defun recognize-known-call (call ir1-converting-not-optimizing-p
)
1086 (declare (type combination call
))
1087 (let* ((ref (lvar-uses (basic-combination-fun call
)))
1088 (leaf (when (ref-p ref
) (ref-leaf ref
)))
1089 (inlinep (if (defined-fun-p leaf
)
1090 (defined-fun-inlinep leaf
)
1093 ((eq inlinep
:notinline
)
1094 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1096 (setf (basic-combination-fun-info call
) info
))
1098 ((not (and (global-var-p leaf
)
1099 (eq (global-var-kind leaf
) :global-function
)))
1101 ((and (ecase inlinep
1104 ((nil :maybe-inline
) (policy call
(zerop space
))))
1105 (defined-fun-p leaf
)
1106 (defined-fun-inline-expansion leaf
)
1107 (inline-expansion-ok call
))
1108 ;; Inline: if the function has already been converted at another call
1109 ;; site in this component, we point this REF to the functional. If not,
1110 ;; we convert the expansion.
1112 ;; For :INLINE case local call analysis will copy the expansion later,
1113 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1114 ;; expansion per component.
1116 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1119 (let* ((name (leaf-source-name leaf
))
1120 (res (ir1-convert-inline-expansion
1122 (defined-fun-inline-expansion leaf
)
1125 (info :function
:info name
))))
1126 ;; Allow backward references to this function from following
1127 ;; forms. (Reused only if policy matches.)
1128 (push res
(defined-fun-functionals leaf
))
1129 (change-ref-leaf ref res
))))
1130 (let ((fun (defined-fun-functional leaf
)))
1132 (and (eq inlinep
:inline
) (functional-kind fun
)))
1134 (if ir1-converting-not-optimizing-p
1136 (with-ir1-environment-from-node call
1138 (locall-analyze-component *current-component
*)))
1139 ;; If we've already converted, change ref to the converted
1141 (change-ref-leaf ref fun
))))
1142 (values (ref-leaf ref
) nil
))
1144 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1148 (setf (basic-combination-kind call
) :known
)
1149 (setf (basic-combination-fun-info call
) info
)))
1150 (values leaf nil
)))))))
1152 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1153 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1154 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1155 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1156 ;;; syntax check, arg/result type processing, but still call
1157 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1158 ;;; and that checking is done by local call analysis.
1159 (defun validate-call-type (call type fun
&optional ir1-converting-not-optimizing-p
)
1160 (declare (type combination call
) (type ctype type
))
1161 (let* ((where (when fun
(leaf-where-from fun
)))
1162 (same-file-p (eq :defined-here where
)))
1163 (cond ((not (fun-type-p type
))
1164 (aver (multiple-value-bind (val win
)
1165 (csubtypep type
(specifier-type 'function
))
1166 (or val
(not win
))))
1167 ;; Using the defined-type too early is a bit of a waste: during
1168 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1169 (when (and fun
(not ir1-converting-not-optimizing-p
))
1170 (let ((defined-type (leaf-defined-type fun
)))
1171 (when (and (fun-type-p defined-type
)
1172 (neq fun
(combination-type-validated-for-leaf call
)))
1173 ;; Don't validate multiple times against the same leaf --
1174 ;; it doesn't add any information, but may generate the same warning
1176 (setf (combination-type-validated-for-leaf call
) fun
)
1177 (when (and (valid-fun-use call defined-type
1178 :argument-test
#'always-subtypep
1180 :lossage-fun
(if same-file-p
1182 #'compiler-style-warn
)
1183 :unwinnage-fun
#'compiler-notify
)
1185 (assert-call-type call defined-type nil
)
1186 (maybe-terminate-block call ir1-converting-not-optimizing-p
)))))
1187 (recognize-known-call call ir1-converting-not-optimizing-p
))
1188 ((valid-fun-use call type
1189 :argument-test
#'always-subtypep
1191 :lossage-fun
#'compiler-warn
1192 :unwinnage-fun
#'compiler-notify
)
1193 (assert-call-type call type
)
1194 (maybe-terminate-block call ir1-converting-not-optimizing-p
)
1195 (recognize-known-call call ir1-converting-not-optimizing-p
))
1197 (setf (combination-kind call
) :error
)
1198 (values nil nil
)))))
1200 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1201 ;;; changed. If the call is local, we try to LET-convert it, and
1202 ;;; derive the result type. If it is a :FULL call, we validate it
1203 ;;; against the type, which recognizes known calls, does inline
1204 ;;; expansion, etc. If a call to a predicate in a non-conditional
1205 ;;; position or to a function with a source transform, then we
1206 ;;; reconvert the form to give IR1 another chance.
1207 (defun propagate-fun-change (call)
1208 (declare (type combination call
))
1209 (let ((*compiler-error-context
* call
)
1210 (fun-lvar (basic-combination-fun call
)))
1211 (setf (lvar-reoptimize fun-lvar
) nil
)
1212 (case (combination-kind call
)
1214 (let ((fun (combination-lambda call
)))
1215 (maybe-let-convert fun
)
1216 (unless (member (functional-kind fun
) '(:let
:assignment
:deleted
))
1217 (derive-node-type call
(tail-set-type (lambda-tail-set fun
))))))
1219 (multiple-value-bind (leaf info
)
1220 (let* ((uses (lvar-uses fun-lvar
))
1221 (leaf (when (ref-p uses
) (ref-leaf uses
))))
1222 (validate-call-type call
(lvar-type fun-lvar
) leaf
))
1223 (cond ((functional-p leaf
)
1224 (convert-call-if-possible
1225 (lvar-uses (basic-combination-fun call
))
1228 ((and (global-var-p leaf
)
1229 (eq (global-var-kind leaf
) :global-function
)
1230 (leaf-has-source-name-p leaf
)
1231 (or (info :function
:source-transform
(leaf-source-name leaf
))
1233 (ir1-attributep (fun-info-attributes info
)
1235 (let ((lvar (node-lvar call
)))
1236 (and lvar
(not (if-p (lvar-dest lvar
))))))))
1237 (let ((name (leaf-source-name leaf
))
1238 (dummies (make-gensym-list
1239 (length (combination-args call
)))))
1240 (transform-call call
1242 (,@(if (symbolp name
)
1246 (leaf-source-name leaf
)))))))))
1249 ;;;; known function optimization
1251 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1252 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1253 ;;; replace it, otherwise add a new one.
1254 (defun record-optimization-failure (node transform args
)
1255 (declare (type combination node
) (type transform transform
)
1256 (type (or fun-type list
) args
))
1257 (let* ((table (component-failed-optimizations *component-being-compiled
*))
1258 (found (assoc transform
(gethash node table
))))
1260 (setf (cdr found
) args
)
1261 (push (cons transform args
) (gethash node table
))))
1264 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1265 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1266 ;;; doing the transform for some reason and FLAME is true, then we
1267 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1268 ;;; finalize to pick up. We return true if the transform failed, and
1269 ;;; thus further transformation should be attempted. We return false
1270 ;;; if either the transform succeeded or was aborted.
1271 (defun ir1-transform (node transform
)
1272 (declare (type combination node
) (type transform transform
))
1273 (let* ((type (transform-type transform
))
1274 (fun (transform-function transform
))
1275 (constrained (fun-type-p type
))
1276 (table (component-failed-optimizations *component-being-compiled
*))
1277 (flame (case (transform-important transform
)
1278 ((t) (policy node
(>= speed inhibit-warnings
)))
1279 (:slightly
(policy node
(> speed inhibit-warnings
)))))
1280 (*compiler-error-context
* node
))
1281 (cond ((or (not constrained
)
1282 (valid-fun-use node type
))
1283 (multiple-value-bind (severity args
)
1284 (catch 'give-up-ir1-transform
1285 (transform-call node
1287 (combination-fun-source-name node
))
1291 (remhash node table
)
1294 (setf (combination-kind node
) :error
)
1296 (apply #'warn args
))
1297 (remhash node table
)
1302 (record-optimization-failure node transform args
))
1303 (setf (gethash node table
)
1304 (remove transform
(gethash node table
) :key
#'car
)))
1307 (remhash node table
)
1312 :argument-test
#'types-equal-or-intersect
1313 :result-test
#'values-types-equal-or-intersect
))
1314 (record-optimization-failure node transform type
)
1319 ;;; When we don't like an IR1 transform, we throw the severity/reason
1322 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1323 ;;; aborting this attempt to transform the call, but admitting the
1324 ;;; possibility that this or some other transform will later succeed.
1325 ;;; If arguments are supplied, they are format arguments for an
1326 ;;; efficiency note.
1328 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1329 ;;; force a normal call to the function at run time. No further
1330 ;;; optimizations will be attempted.
1332 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1333 ;;; delay the transform on the node until later. REASONS specifies
1334 ;;; when the transform will be later retried. The :OPTIMIZE reason
1335 ;;; causes the transform to be delayed until after the current IR1
1336 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1337 ;;; be delayed until after constraint propagation.
1339 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1340 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1341 ;;; do CASE operations on the various REASON values, it might be a
1342 ;;; good idea to go OO, representing the reasons by objects, using
1343 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1344 ;;; SIGNAL instead of THROW.
1345 (declaim (ftype (function (&rest t
) #+(and sb-xc-host ccl
) *
1346 #-
(and sb-xc-host ccl
) nil
) give-up-ir1-transform
))
1347 (defun give-up-ir1-transform (&rest args
)
1348 (throw 'give-up-ir1-transform
(values :failure args
)))
1349 (defun abort-ir1-transform (&rest args
)
1350 (throw 'give-up-ir1-transform
(values :aborted args
)))
1351 (defun delay-ir1-transform (node &rest reasons
)
1352 (let ((assoc (assoc node
*delayed-ir1-transforms
*)))
1354 (setf *delayed-ir1-transforms
*
1355 (acons node reasons
*delayed-ir1-transforms
*))
1356 (throw 'give-up-ir1-transform
:delayed
))
1358 (dolist (reason reasons
)
1359 (pushnew reason
(cdr assoc
)))
1360 (throw 'give-up-ir1-transform
:delayed
)))))
1362 ;;; Poor man's catching and resignalling
1363 ;;; Implicit %GIVE-UP macrolet will resignal the give-up "condition"
1364 (defmacro catch-give-up-ir1-transform
((form &optional args
) &body gave-up-body
)
1365 (let ((block (gensym "BLOCK"))
1366 (kind (gensym "KIND"))
1367 (args (or args
(gensym "ARGS"))))
1369 (multiple-value-bind (,kind
,args
)
1370 (catch 'give-up-ir1-transform
1371 (return-from ,block
,form
))
1374 (throw 'give-up-ir1-transform
:delayed
))
1375 ((:failure
:aborted
)
1376 (macrolet ((%give-up
()
1377 `(throw 'give-up-ir1-transform
(values ,',kind
1379 ,@gave-up-body
)))))))
1381 ;;; Clear any delayed transform with no reasons - these should have
1382 ;;; been tried in the last pass. Then remove the reason from the
1383 ;;; delayed transform reasons, and if any become empty then set
1384 ;;; reoptimize flags for the node. Return true if any transforms are
1386 (defun retry-delayed-ir1-transforms (reason)
1387 (setf *delayed-ir1-transforms
*
1388 (remove-if-not #'cdr
*delayed-ir1-transforms
*))
1389 (let ((reoptimize nil
))
1390 (dolist (assoc *delayed-ir1-transforms
*)
1391 (let ((reasons (remove reason
(cdr assoc
))))
1392 (setf (cdr assoc
) reasons
)
1394 (let ((node (car assoc
)))
1395 (unless (node-deleted node
)
1397 (setf (node-reoptimize node
) t
)
1398 (let ((block (node-block node
)))
1399 (setf (block-reoptimize block
) t
)
1400 (reoptimize-component (block-component block
) :maybe
)))))))
1403 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1404 ;;; environment, and then install it as the function for the call
1405 ;;; NODE. We do local call analysis so that the new function is
1406 ;;; integrated into the control flow.
1408 ;;; We require the original function source name in order to generate
1409 ;;; a meaningful debug name for the lambda we set up. (It'd be
1410 ;;; possible to do this starting from debug names as well as source
1411 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1412 ;;; generality, since source names are always known to our callers.)
1413 (defun transform-call (call res source-name
)
1414 (declare (type combination call
) (list res
))
1415 (aver (and (legal-fun-name-p source-name
)
1416 (not (eql source-name
'.anonymous.
))))
1417 (node-ends-block call
)
1418 ;; The internal variables of a transform are not going to be
1419 ;; interesting to the debugger, so there's no sense in
1420 ;; suppressing the substitution of variables with only one use
1421 ;; (the extra variables can slow down constraint propagation).
1423 ;; This needs to be done before the WITH-IR1-ENVIRONMENT-FROM-NODE,
1424 ;; so that it will bind *LEXENV* to the right environment.
1425 (setf (combination-lexenv call
)
1426 (make-lexenv :default
(combination-lexenv call
)
1427 :policy
(process-optimize-decl
1429 (preserve-single-use-debug-variables 0))
1431 (combination-lexenv call
)))))
1432 (with-ir1-environment-from-node call
1433 (with-component-last-block (*current-component
*
1434 (block-next (node-block call
)))
1436 (let ((new-fun (ir1-convert-inline-lambda
1438 :debug-name
(debug-name 'lambda-inlined source-name
)
1440 (ref (lvar-use (combination-fun call
))))
1441 (change-ref-leaf ref new-fun
)
1442 (setf (combination-kind call
) :full
)
1443 (locall-analyze-component *current-component
*))))
1446 (defun constant-fold-arg-p (name)
1451 (let* ((info (info :function
:info name
))
1452 (attributes (and info
1453 (fun-info-attributes info
))))
1455 (ir1-attributep attributes foldable
)
1456 (not (ir1-attributep attributes call
)))))))
1458 ;;; Return T if the function is foldable and if it's marked as CALL
1459 ;;; all function arguments are FOLDABLE too.
1460 (defun constant-fold-call-p (combination)
1461 (let* ((info (basic-combination-fun-info combination
))
1462 (attr (fun-info-attributes info
))
1463 (args (basic-combination-args combination
)))
1464 (cond ((not (ir1-attributep attr foldable
))
1466 ((ir1-attributep attr call
)
1467 (apply (fun-info-foldable-call-check info
)
1468 (mapcar (lambda (lvar)
1469 (or (lvar-fun-name lvar t
)
1470 (if (constant-lvar-p lvar
)
1472 (return-from constant-fold-call-p nil
))))
1475 (every #'constant-lvar-p args
)))))
1477 ;;; Replace a call to a foldable function of constant arguments with
1478 ;;; the result of evaluating the form. If there is an error during the
1479 ;;; evaluation, we give a warning and leave the call alone, making the
1480 ;;; call a :ERROR call.
1482 ;;; If there is more than one value, then we transform the call into a
1484 (defun constant-fold-call (call)
1485 (let ((args (mapcar (lambda (lvar)
1486 (let ((name (lvar-fun-name lvar t
)))
1489 (lvar-value lvar
))))
1490 (combination-args call
)))
1491 (fun-name (combination-fun-source-name call
)))
1492 (multiple-value-bind (values win
)
1493 (careful-call fun-name
1496 ;; Note: CMU CL had COMPILER-WARN here, and that
1497 ;; seems more natural, but it's probably not.
1499 ;; It's especially not while bug 173 exists:
1502 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1504 ;; can cause constant-folding TYPE-ERRORs (in
1505 ;; #'<=) when END can be proved to be NIL, even
1506 ;; though the code is perfectly legal and safe
1507 ;; because a NIL value of END means that the
1508 ;; #'<= will never be executed.
1510 ;; Moreover, even without bug 173,
1511 ;; quite-possibly-valid code like
1512 ;; (COND ((NONINLINED-PREDICATE END)
1513 ;; (UNLESS (<= END SIZE))
1515 ;; (where NONINLINED-PREDICATE is something the
1516 ;; compiler can't do at compile time, but which
1517 ;; turns out to make the #'<= expression
1518 ;; unreachable when END=NIL) could cause errors
1519 ;; when the compiler tries to constant-fold (<=
1522 ;; So, with or without bug 173, it'd be
1523 ;; unnecessarily evil to do a full
1524 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1525 ;; from COMPILE-FILE) for legal code, so we we
1526 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1527 #-sb-xc-host
#'compiler-style-warn
1528 ;; On the other hand, for code we control, we
1529 ;; should be able to work around any bug
1530 ;; 173-related problems, and in particular we
1531 ;; want to be alerted to calls to our own
1532 ;; functions which aren't being folded away; a
1533 ;; COMPILER-WARNING is butch enough to stop the
1534 ;; SBCL build itself in its tracks.
1535 #+sb-xc-host
#'compiler-warn
1538 (setf (combination-kind call
) :error
))
1539 ((and (proper-list-of-length-p values
1))
1540 (with-ir1-environment-from-node call
1541 (let* ((lvar (node-lvar call
))
1542 (prev (node-prev call
))
1543 (intermediate-ctran (make-ctran)))
1544 (%delete-lvar-use call
)
1545 (setf (ctran-next prev
) nil
)
1546 (setf (node-prev call
) nil
)
1547 (reference-constant prev intermediate-ctran lvar
1549 (link-node-to-previous-ctran call intermediate-ctran
)
1550 (reoptimize-lvar lvar
)
1551 (flush-combination call
))))
1552 (t (let ((dummies (make-gensym-list (length args
))))
1556 (declare (ignore ,@dummies
))
1557 (values ,@(mapcar (lambda (x) `',x
) values
)))
1561 ;;;; local call optimization
1563 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1565 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1566 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1569 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1570 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1571 (defun propagate-to-refs (leaf type
)
1572 (declare (type leaf leaf
) (type ctype type
))
1573 (let ((var-type (leaf-type leaf
))
1574 (refs (leaf-refs leaf
)))
1575 (unless (or (fun-type-p var-type
)
1577 (eq :declared
(leaf-where-from leaf
))
1578 (type-needs-conservation-p var-type
)))
1579 (let ((int (type-approx-intersection2 var-type type
)))
1580 (when (type/= int var-type
)
1581 (setf (leaf-type leaf
) int
)
1582 (let ((s-int (make-single-value-type int
)))
1584 (derive-node-type ref s-int
)
1585 ;; KLUDGE: LET var substitution
1586 (let* ((lvar (node-lvar ref
)))
1587 (when (and lvar
(combination-p (lvar-dest lvar
)))
1588 (reoptimize-lvar lvar
)))))))
1591 ;;; Iteration variable: exactly one SETQ of the form:
1593 ;;; (let ((var initial))
1595 ;;; (setq var (+ var step))
1597 (defun maybe-infer-iteration-var-type (var initial-type
)
1598 (binding* ((sets (lambda-var-sets var
) :exit-if-null
)
1600 (() (null (rest sets
)) :exit-if-null
)
1601 (set-use (principal-lvar-use (set-value set
)))
1602 (() (and (combination-p set-use
)
1603 (eq (combination-kind set-use
) :known
)
1604 (fun-info-p (combination-fun-info set-use
))
1605 (not (node-to-be-deleted-p set-use
))
1606 (or (eq (combination-fun-source-name set-use
) '+)
1607 (eq (combination-fun-source-name set-use
) '-
)))
1609 (minusp (eq (combination-fun-source-name set-use
) '-
))
1610 (+-args
(basic-combination-args set-use
))
1611 (() (and (proper-list-of-length-p +-args
2 2)
1612 (let ((first (principal-lvar-use
1615 (eq (ref-leaf first
) var
))))
1617 (step-type (lvar-type (second +-args
)))
1618 (set-type (lvar-type (set-value set
))))
1619 (when (and (numeric-type-p initial-type
)
1620 (numeric-type-p step-type
)
1621 (or (numeric-type-equal initial-type step-type
)
1622 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1623 ;; the initial and the step are of different types,
1624 ;; and the step is less contagious.
1625 (numeric-type-equal initial-type
1626 (numeric-contagion initial-type
1628 (labels ((leftmost (x y cmp cmp
=)
1629 (cond ((eq x nil
) nil
)
1632 (let ((x1 (first x
)))
1634 (let ((y1 (first y
)))
1635 (if (funcall cmp x1 y1
) x y
)))
1637 (if (funcall cmp x1 y
) x y
)))))
1639 (let ((y1 (first y
)))
1640 (if (funcall cmp
= x y1
) x y
)))
1641 (t (if (funcall cmp x y
) x y
))))
1642 (max* (x y
) (leftmost x y
#'> #'>=))
1643 (min* (x y
) (leftmost x y
#'< #'<=)))
1644 (multiple-value-bind (low high
)
1645 (let ((step-type-non-negative (csubtypep step-type
(specifier-type
1647 (step-type-non-positive (csubtypep step-type
(specifier-type
1649 (cond ((or (and step-type-non-negative
(not minusp
))
1650 (and step-type-non-positive minusp
))
1651 (values (numeric-type-low initial-type
)
1652 (when (and (numeric-type-p set-type
)
1653 (numeric-type-equal set-type initial-type
))
1654 (max* (numeric-type-high initial-type
)
1655 (numeric-type-high set-type
)))))
1656 ((or (and step-type-non-positive
(not minusp
))
1657 (and step-type-non-negative minusp
))
1658 (values (when (and (numeric-type-p set-type
)
1659 (numeric-type-equal set-type initial-type
))
1660 (min* (numeric-type-low initial-type
)
1661 (numeric-type-low set-type
)))
1662 (numeric-type-high initial-type
)))
1665 (modified-numeric-type initial-type
1668 :enumerable nil
))))))
1669 (deftransform + ((x y
) * * :result result
)
1670 "check for iteration variable reoptimization"
1671 (let ((dest (principal-lvar-end result
))
1672 (use (principal-lvar-use x
)))
1673 (when (and (ref-p use
)
1677 (reoptimize-lvar (set-value dest
))))
1678 (give-up-ir1-transform))
1680 ;;; Figure out the type of a LET variable that has sets. We compute
1681 ;;; the union of the INITIAL-TYPE and the types of all the set
1682 ;;; values and to a PROPAGATE-TO-REFS with this type.
1683 (defun propagate-from-sets (var initial-type
)
1684 (let ((changes (not (csubtypep (lambda-var-last-initial-type var
) initial-type
)))
1686 (dolist (set (lambda-var-sets var
))
1687 (let ((type (lvar-type (set-value set
))))
1689 (when (node-reoptimize set
)
1690 (let ((old-type (node-derived-type set
)))
1691 (unless (values-subtypep old-type type
)
1692 (derive-node-type set
(make-single-value-type type
))
1694 (setf (node-reoptimize set
) nil
))))
1696 (setf (lambda-var-last-initial-type var
) initial-type
)
1697 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type
)
1698 (apply #'type-union initial-type types
))))
1699 (propagate-to-refs var res-type
))))
1702 ;;; If a LET variable, find the initial value's type and do
1703 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1705 (defun ir1-optimize-set (node)
1706 (declare (type cset node
))
1707 (let ((var (set-var node
)))
1708 (when (and (lambda-var-p var
) (leaf-refs var
))
1709 (let ((home (lambda-var-home var
)))
1710 (when (eq (functional-kind home
) :let
)
1711 (let* ((initial-value (let-var-initial-value var
))
1712 (initial-type (lvar-type initial-value
)))
1713 (setf (lvar-reoptimize initial-value
) nil
)
1714 (propagate-from-sets var initial-type
))))))
1715 (derive-node-type node
(make-single-value-type
1716 (lvar-type (set-value node
))))
1717 (setf (node-reoptimize node
) nil
)
1720 ;;; Return true if the value of REF will always be the same (and is
1721 ;;; thus legal to substitute.)
1722 (defun constant-reference-p (ref)
1723 (declare (type ref ref
))
1724 (let ((leaf (ref-leaf ref
)))
1726 ((or constant functional
) t
)
1728 (null (lambda-var-sets leaf
)))
1730 (not (eq (defined-fun-inlinep leaf
) :notinline
)))
1732 (case (global-var-kind leaf
)
1734 (let ((name (leaf-source-name leaf
)))
1736 (eq (symbol-package (fun-name-block-name name
))
1738 (info :function
:info name
)))))))))
1740 ;;; If we have a non-set LET var with a single use, then (if possible)
1741 ;;; replace the variable reference's LVAR with the arg lvar.
1743 ;;; We change the REF to be a reference to NIL with unused value, and
1744 ;;; let it be flushed as dead code. A side effect of this substitution
1745 ;;; is to delete the variable.
1746 (defun substitute-single-use-lvar (arg var
)
1747 (declare (type lvar arg
) (type lambda-var var
))
1748 (binding* ((ref (first (leaf-refs var
)))
1749 (lvar (node-lvar ref
) :exit-if-null
)
1750 (dest (lvar-dest lvar
))
1751 (dest-lvar (when (valued-node-p dest
) (node-lvar dest
))))
1753 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1754 ;; LVAR-USEs should not be met on one path. Another problem
1755 ;; is with dynamic-extent.
1756 (eq (lvar-uses lvar
) ref
)
1757 (not (block-delete-p (node-block ref
)))
1758 ;; If the destinatation is dynamic extent, don't substitute unless
1759 ;; the source is as well.
1761 (not (lvar-dynamic-extent dest-lvar
))
1762 (lvar-dynamic-extent lvar
))
1764 ;; we should not change lifetime of unknown values lvars
1766 (and (type-single-value-p (lvar-derived-type arg
))
1767 (multiple-value-bind (pdest pprev
)
1768 (principal-lvar-end lvar
)
1769 (declare (ignore pdest
))
1770 (lvar-single-value-p pprev
))
1771 ;; CASTs can disappear, don't substitute if
1772 ;; DEST-LVAR has other uses (this will be
1773 ;; insufficient if we have a CAST-CAST chain, but
1774 ;; works well for a single CAST)
1775 (or (null dest-lvar
)
1776 (atom (lvar-uses dest-lvar
)))))
1778 (or (eq (basic-combination-fun dest
) lvar
)
1779 (and (eq (basic-combination-kind dest
) :local
)
1780 (type-single-value-p (lvar-derived-type arg
)))))
1782 ;; While CRETURN and EXIT nodes may be known-values,
1783 ;; they have their own complications, such as
1784 ;; substitution into CRETURN may create new tail calls.
1787 (aver (lvar-single-value-p lvar
))
1789 (eq (node-home-lambda ref
)
1790 (lambda-home (lambda-var-home var
))))
1791 (let ((ref-type (single-value-type (node-derived-type ref
))))
1792 (cond ((csubtypep (single-value-type (lvar-type arg
)) ref-type
)
1793 (substitute-lvar-uses lvar arg
1794 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1796 (delete-lvar-use ref
))
1798 (let* ((value (make-lvar))
1799 (cast (insert-cast-before ref value ref-type
1800 ;; KLUDGE: it should be (TYPE-CHECK 0)
1802 (setf (cast-type-to-check cast
) *wild-type
*)
1803 (substitute-lvar-uses value arg
1806 (%delete-lvar-use ref
)
1807 (add-lvar-use cast lvar
)))))
1808 (setf (node-derived-type ref
) *wild-type
*)
1809 (change-ref-leaf ref
(find-constant nil
))
1812 (reoptimize-lvar lvar
)
1815 ;;; Delete a LET, removing the call and bind nodes, and warning about
1816 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1817 ;;; along right away and delete the REF and then the lambda, since we
1818 ;;; flush the FUN lvar.
1819 (defun delete-let (clambda)
1820 (declare (type clambda clambda
))
1821 (aver (functional-letlike-p clambda
))
1822 (note-unreferenced-fun-vars clambda
)
1823 (let ((call (let-combination clambda
)))
1824 (flush-dest (basic-combination-fun call
))
1826 (unlink-node (lambda-bind clambda
))
1827 (setf (lambda-bind clambda
) nil
))
1828 (setf (functional-kind clambda
) :zombie
)
1829 (let ((home (lambda-home clambda
)))
1830 (setf (lambda-lets home
) (delete clambda
(lambda-lets home
))))
1833 ;;; This function is called when one of the arguments to a LET
1834 ;;; changes. We look at each changed argument. If the corresponding
1835 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1836 ;;; consider substituting for the variable, and also propagate
1837 ;;; derived-type information for the arg to all the VAR's refs.
1839 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1840 ;;; subtype of the argument's leaf type. This prevents type checking
1841 ;;; from being defeated, and also ensures that the best representation
1842 ;;; for the variable can be used.
1844 ;;; Substitution of individual references is inhibited if the
1845 ;;; reference is in a different component from the home. This can only
1846 ;;; happen with closures over top level lambda vars. In such cases,
1847 ;;; the references may have already been compiled, and thus can't be
1848 ;;; retroactively modified.
1850 ;;; If all of the variables are deleted (have no references) when we
1851 ;;; are done, then we delete the LET.
1853 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1855 (defun propagate-let-args (call fun
)
1856 (declare (type combination call
) (type clambda fun
))
1857 (loop for arg in
(combination-args call
)
1858 and var in
(lambda-vars fun
) do
1859 (when (and arg
(lvar-reoptimize arg
))
1860 (setf (lvar-reoptimize arg
) nil
)
1862 ((lambda-var-sets var
)
1863 (propagate-from-sets var
(lvar-type arg
)))
1864 ((let ((use (lvar-uses arg
)))
1866 (let ((leaf (ref-leaf use
)))
1867 (when (and (constant-reference-p use
)
1868 (csubtypep (leaf-type leaf
)
1869 ;; (NODE-DERIVED-TYPE USE) would
1870 ;; be better -- APD, 2003-05-15
1872 (propagate-to-refs var
(lvar-type arg
))
1873 (let ((use-component (node-component use
)))
1874 (prog1 (substitute-leaf-if
1876 (cond ((eq (node-component ref
) use-component
)
1879 (aver (lambda-toplevelish-p (lambda-home fun
)))
1883 ((and (null (rest (leaf-refs var
)))
1884 (not (preserve-single-use-debug-var-p call var
))
1885 (substitute-single-use-lvar arg var
)))
1887 (propagate-to-refs var
(lvar-type arg
))))))
1889 (when (every #'not
(combination-args call
))
1894 ;;; This function is called when one of the args to a non-LET local
1895 ;;; call changes. For each changed argument corresponding to an unset
1896 ;;; variable, we compute the union of the types across all calls and
1897 ;;; propagate this type information to the var's refs.
1899 ;;; If the function has an entry-fun, then we don't do anything: since
1900 ;;; it has a XEP we would not discover anything.
1902 ;;; If the function is an optional-entry-point, we will just make sure
1903 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1904 ;;; can erronously propagate too strict types into refs: see
1905 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1907 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1908 ;;; corresponding to changed arguments in CALL, since the only use in
1909 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1911 (defun propagate-local-call-args (call fun
)
1912 (declare (type combination call
) (type clambda fun
))
1913 (unless (functional-entry-fun fun
)
1914 (if (lambda-optional-dispatch fun
)
1915 ;; We can still make sure &REST is known to be a list.
1916 (loop for var in
(lambda-vars fun
)
1917 do
(let ((info (lambda-var-arg-info var
)))
1918 (when (and info
(eq :rest
(arg-info-kind info
)))
1919 (propagate-from-sets var
(specifier-type 'list
)))))
1921 (let* ((vars (lambda-vars fun
))
1922 (union (mapcar (lambda (arg var
)
1924 (lvar-reoptimize arg
)
1925 (null (basic-var-sets var
)))
1927 (basic-combination-args call
)
1929 (this-ref (lvar-use (basic-combination-fun call
))))
1931 (dolist (arg (basic-combination-args call
))
1933 (setf (lvar-reoptimize arg
) nil
)))
1935 (dolist (ref (leaf-refs fun
))
1936 (let ((dest (node-dest ref
)))
1937 (unless (or (eq ref this-ref
) (not dest
))
1939 (mapcar (lambda (this-arg old
)
1941 (setf (lvar-reoptimize this-arg
) nil
)
1942 (type-union (lvar-type this-arg
) old
)))
1943 (basic-combination-args dest
)
1946 (loop for var in vars
1948 when type do
(propagate-to-refs var type
)))))
1952 ;;;; multiple values optimization
1954 ;;; Do stuff to notice a change to a MV combination node. There are
1955 ;;; two main branches here:
1956 ;;; -- If the call is local, then it is already a MV let, or should
1957 ;;; become one. Note that although all :LOCAL MV calls must eventually
1958 ;;; be converted to :MV-LETs, there can be a window when the call
1959 ;;; is local, but has not been LET converted yet. This is because
1960 ;;; the entry-point lambdas may have stray references (in other
1961 ;;; entry points) that have not been deleted yet.
1962 ;;; -- The call is full. This case is somewhat similar to the non-MV
1963 ;;; combination optimization: we propagate return type information and
1964 ;;; notice non-returning calls. We also have an optimization
1965 ;;; which tries to convert MV-CALLs into MV-binds.
1966 (defun ir1-optimize-mv-combination (node)
1967 (let ((fun (basic-combination-fun node
)))
1968 (unless (and (node-p (lvar-uses fun
))
1969 (node-to-be-deleted-p (lvar-uses fun
)))
1970 (ecase (basic-combination-kind node
)
1972 (when (lvar-reoptimize fun
)
1973 (setf (lvar-reoptimize fun
) nil
)
1974 (maybe-let-convert (combination-lambda node
)))
1975 (setf (lvar-reoptimize (first (basic-combination-args node
))) nil
)
1976 (when (eq (functional-kind (combination-lambda node
)) :mv-let
)
1977 (unless (convert-mv-bind-to-let node
)
1978 (ir1-optimize-mv-bind node
))))
1980 (let* ((fun-changed (lvar-reoptimize fun
))
1981 (args (basic-combination-args node
)))
1983 (setf (lvar-reoptimize fun
) nil
)
1984 (let ((type (lvar-type fun
)))
1985 (when (fun-type-p type
)
1986 (derive-node-type node
(fun-type-returns type
))))
1987 (maybe-terminate-block node nil
)
1988 (let ((use (lvar-uses fun
)))
1989 (when (and (ref-p use
) (functional-p (ref-leaf use
)))
1990 (convert-call-if-possible use node
)
1991 (when (eq (basic-combination-kind node
) :local
)
1992 (maybe-let-convert (ref-leaf use
))))))
1993 (unless (or (eq (basic-combination-kind node
) :local
)
1994 (eq (lvar-fun-name fun
) '%throw
))
1995 (ir1-optimize-mv-call node
))
1997 (setf (lvar-reoptimize arg
) nil
))))
2001 ;;; Propagate derived type info from the values lvar to the vars.
2002 (defun ir1-optimize-mv-bind (node)
2003 (declare (type mv-combination node
))
2004 (let* ((arg (first (basic-combination-args node
)))
2005 (vars (lambda-vars (combination-lambda node
)))
2006 (n-vars (length vars
))
2007 (types (values-type-in (lvar-derived-type arg
)
2009 (loop for var in vars
2011 do
(if (basic-var-sets var
)
2012 (propagate-from-sets var type
)
2013 (propagate-to-refs var type
)))
2014 (setf (lvar-reoptimize arg
) nil
))
2017 ;;; If possible, convert a general MV call to an MV-BIND. We can do
2019 ;;; -- The call has only one argument, and
2020 ;;; -- The function has a known fixed number of arguments, or
2021 ;;; -- The argument yields a known fixed number of values.
2023 ;;; What we do is change the function in the MV-CALL to be a lambda
2024 ;;; that "looks like an MV bind", which allows
2025 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
2026 ;;; converted (the next time around.) This new lambda just calls the
2027 ;;; actual function with the MV-BIND variables as arguments. Note that
2028 ;;; this new MV bind is not let-converted immediately, as there are
2029 ;;; going to be stray references from the entry-point functions until
2030 ;;; they get deleted.
2032 ;;; In order to avoid loss of argument count checking, we only do the
2033 ;;; transformation according to a known number of expected argument if
2034 ;;; safety is unimportant. We can always convert if we know the number
2035 ;;; of actual values, since the normal call that we build will still
2036 ;;; do any appropriate argument count checking.
2038 ;;; We only attempt the transformation if the called function is a
2039 ;;; constant reference. This allows us to just splice the leaf into
2040 ;;; the new function, instead of trying to somehow bind the function
2041 ;;; expression. The leaf must be constant because we are evaluating it
2042 ;;; again in a different place. This also has the effect of squelching
2043 ;;; multiple warnings when there is an argument count error.
2044 (defun ir1-optimize-mv-call (node)
2045 (let ((fun (basic-combination-fun node
))
2046 (*compiler-error-context
* node
)
2047 (ref (lvar-uses (basic-combination-fun node
)))
2048 (args (basic-combination-args node
)))
2050 (unless (and (ref-p ref
) (constant-reference-p ref
)
2052 (return-from ir1-optimize-mv-call
))
2054 (multiple-value-bind (min max
)
2055 (fun-type-nargs (lvar-type fun
))
2057 (multiple-value-bind (types nvals
)
2058 (values-types (lvar-derived-type (first args
)))
2059 (declare (ignore types
))
2060 (if (eq nvals
:unknown
) nil nvals
))))
2063 (when (and min
(< total-nvals min
))
2065 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2068 (setf (basic-combination-kind node
) :error
)
2069 (return-from ir1-optimize-mv-call
))
2070 (when (and max
(> total-nvals max
))
2072 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2075 (setf (basic-combination-kind node
) :error
)
2076 (return-from ir1-optimize-mv-call
)))
2078 (let ((count (cond (total-nvals)
2079 ((and (policy node
(zerop verify-arg-count
))
2084 (with-ir1-environment-from-node node
2085 (let* ((dums (make-gensym-list count
))
2087 (leaf (ref-leaf ref
))
2088 (fun (ir1-convert-lambda
2089 `(lambda (&optional
,@dums
&rest
,ignore
)
2090 (declare (ignore ,ignore
))
2091 (%funcall
,leaf
,@dums
))
2092 :source-name
(leaf-%source-name leaf
)
2093 :debug-name
(leaf-%debug-name leaf
))))
2094 (change-ref-leaf ref fun
)
2095 (aver (eq (basic-combination-kind node
) :full
))
2096 (locall-analyze-component *current-component
*)
2097 (aver (eq (basic-combination-kind node
) :local
)))))))))
2101 ;;; (multiple-value-bind
2110 ;;; What we actually do is convert the VALUES combination into a
2111 ;;; normal LET combination calling the original :MV-LET lambda. If
2112 ;;; there are extra args to VALUES, discard the corresponding
2113 ;;; lvars. If there are insufficient args, insert references to NIL.
2114 (defun convert-mv-bind-to-let (call)
2115 (declare (type mv-combination call
))
2116 (let* ((arg (first (basic-combination-args call
)))
2117 (use (lvar-uses arg
)))
2118 (when (and (combination-p use
)
2119 (eq (lvar-fun-name (combination-fun use
))
2121 (let* ((fun (combination-lambda call
))
2122 (vars (lambda-vars fun
))
2123 (vals (combination-args use
))
2124 (nvars (length vars
))
2125 (nvals (length vals
)))
2126 (cond ((> nvals nvars
)
2127 (mapc #'flush-dest
(subseq vals nvars
))
2128 (setq vals
(subseq vals
0 nvars
)))
2130 (with-ir1-environment-from-node use
2131 (let ((node-prev (node-prev use
)))
2132 (setf (node-prev use
) nil
)
2133 (setf (ctran-next node-prev
) nil
)
2134 (collect ((res vals
))
2135 (loop for count below
(- nvars nvals
)
2136 for prev
= node-prev then ctran
2137 for ctran
= (make-ctran)
2138 and lvar
= (make-lvar use
)
2139 do
(reference-constant prev ctran lvar nil
)
2141 finally
(link-node-to-previous-ctran
2143 (setq vals
(res)))))))
2144 (setf (combination-args use
) vals
)
2145 (flush-dest (combination-fun use
))
2146 (let ((fun-lvar (basic-combination-fun call
)))
2147 (setf (lvar-dest fun-lvar
) use
)
2148 (setf (combination-fun use
) fun-lvar
)
2149 (flush-lvar-externally-checkable-type fun-lvar
))
2150 (setf (combination-kind use
) :local
)
2151 (setf (functional-kind fun
) :let
)
2152 (flush-dest (first (basic-combination-args call
)))
2155 (reoptimize-lvar (first vals
)))
2156 ;; Propagate derived types from the VALUES call to its args:
2157 ;; transforms can leave the VALUES call with a better type
2158 ;; than its args have, so make sure not to throw that away.
2159 (let ((types (values-type-types (node-derived-type use
))))
2162 (let ((type (pop types
)))
2163 (assert-lvar-type val type
**zero-typecheck-policy
**)))))
2164 ;; Propagate declared types of MV-BIND variables.
2165 (propagate-to-args use fun
)
2166 (reoptimize-call use
))
2170 ;;; (values-list (list x y z))
2175 ;;; In implementation, this is somewhat similar to
2176 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2177 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2178 ;;; (allowing the LIST to be flushed.)
2180 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2181 (defoptimizer (values-list optimizer
) ((list) node
)
2182 (let ((use (lvar-uses list
)))
2183 (when (and (combination-p use
)
2184 (eq (lvar-fun-name (combination-fun use
))
2187 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2188 (change-ref-leaf (lvar-uses (combination-fun node
))
2189 (find-free-fun 'values
"in a strange place"))
2190 (setf (combination-kind node
) :full
)
2191 (let ((args (combination-args use
)))
2193 (setf (lvar-dest arg
) node
)
2194 (flush-lvar-externally-checkable-type arg
))
2195 (setf (combination-args use
) nil
)
2197 (flush-combination use
)
2198 (setf (combination-args node
) args
))
2201 ;;; If VALUES appears in a non-MV context, then effectively convert it
2202 ;;; to a PROG1. This allows the computation of the additional values
2203 ;;; to become dead code.
2204 (deftransform values
((&rest vals
) * * :node node
)
2205 (unless (lvar-single-value-p (node-lvar node
))
2206 (give-up-ir1-transform))
2207 (setf (node-derived-type node
)
2208 (make-short-values-type (list (single-value-type
2209 (node-derived-type node
)))))
2210 (principal-lvar-single-valuify (node-lvar node
))
2212 (let ((dummies (make-gensym-list (length (cdr vals
)))))
2213 `(lambda (val ,@dummies
)
2214 (declare (ignore ,@dummies
))
2220 (defun delete-cast (cast)
2221 (declare (type cast cast
))
2222 (let ((value (cast-value cast
))
2223 (lvar (cast-lvar cast
)))
2224 (when (and (bound-cast-p cast
)
2225 (bound-cast-check cast
))
2226 (flush-combination (bound-cast-check cast
))
2227 (setf (bound-cast-check cast
) nil
))
2228 (delete-filter cast lvar value
)
2230 (reoptimize-lvar lvar
)
2231 (when (lvar-single-value-p lvar
)
2232 (note-single-valuified-lvar lvar
)))
2235 (defun may-delete-vestigial-exit (cast)
2236 (let ((exit-lexenv (cast-vestigial-exit-lexenv cast
)))
2238 ;; Vestigial exits are only introduced when eliminating a local
2239 ;; RETURN-FROM. We may delete them only when we can show that
2240 ;; there are no other code paths that use the entry LVAR that
2241 ;; are live from within the block that contained the deleted
2242 ;; EXIT (our predecessor block). The conservative version of
2243 ;; this is that there are no EXITs for any ENTRY introduced
2244 ;; between the LEXENV of the deleted EXIT and the LEXENV of the
2246 (let* ((entry-lexenv (cast-vestigial-exit-entry-lexenv cast
))
2247 (entry-blocks (lexenv-blocks entry-lexenv
))
2248 (entry-tags (lexenv-tags entry-lexenv
)))
2249 (do ((current-block (lexenv-blocks exit-lexenv
) (cdr current-block
)))
2250 ((eq current-block entry-blocks
))
2251 (when (entry-exits (cadar current-block
))
2252 (return-from may-delete-vestigial-exit nil
)))
2253 (do ((current-tag (lexenv-tags exit-lexenv
) (cdr current-tag
)))
2254 ((eq current-tag entry-tags
))
2255 (when (entry-exits (cadar current-tag
))
2256 (return-from may-delete-vestigial-exit nil
))))))
2259 (defun compile-time-type-error-context (context)
2260 #+sb-xc-host context
2261 #-sb-xc-host
(source-to-string context
))
2263 (defun ir1-optimize-cast (cast &optional do-not-optimize
)
2264 (declare (type cast cast
))
2265 (let ((value (cast-value cast
))
2266 (atype (cast-asserted-type cast
)))
2267 (unless (or do-not-optimize
2268 (not (may-delete-vestigial-exit cast
)))
2269 (when (and (bound-cast-p cast
)
2270 (bound-cast-check cast
)
2271 (constant-lvar-p (bound-cast-bound cast
)))
2273 (specifier-type `(integer 0 (,(lvar-value (bound-cast-bound cast
)))))
2274 (cast-asserted-type cast
) atype
2275 (bound-cast-derived cast
) t
))
2276 (let ((lvar (node-lvar cast
)))
2277 (when (and (or (not (bound-cast-p cast
))
2278 (bound-cast-derived cast
))
2279 (values-subtypep (lvar-derived-type value
)
2280 (cast-asserted-type cast
)))
2282 (return-from ir1-optimize-cast t
))
2284 (when (and (listp (lvar-uses value
))
2286 ;; Pathwise removing of CAST
2287 (let ((ctran (node-next cast
))
2288 (dest (lvar-dest lvar
))
2291 (do-uses (use value
)
2292 (when (and (values-subtypep (node-derived-type use
) atype
)
2293 (immediately-used-p value use
))
2295 (when ctran
(ensure-block-start ctran
))
2296 (setq next-block
(first (block-succ (node-block cast
))))
2297 (ensure-block-start (node-prev cast
))
2298 (reoptimize-lvar lvar
)
2299 (setf (lvar-%derived-type value
) nil
))
2300 (%delete-lvar-use use
)
2301 (add-lvar-use use lvar
)
2302 (unlink-blocks (node-block use
) (node-block cast
))
2303 (link-blocks (node-block use
) next-block
)
2304 (when (and (return-p dest
)
2305 (basic-combination-p use
)
2306 (eq (basic-combination-kind use
) :local
))
2308 (dolist (use (merges))
2309 (merge-tail-sets use
))))))
2311 (when (and (bound-cast-p cast
)
2312 (bound-cast-check cast
)
2313 (policy cast
(= insert-array-bounds-checks
0)))
2314 (flush-combination (bound-cast-check cast
))
2315 (setf (bound-cast-check cast
) nil
)))
2317 (let* ((value-type (lvar-derived-type value
))
2318 (int (values-type-intersection value-type atype
)))
2319 (derive-node-type cast int
)
2321 (neq int
*empty-type
*)
2322 (eq value-type
*empty-type
*)))
2323 ;; No need to transform into an analog of
2324 ;; %COMPILE-TIME-TYPE-ERROR, %CHECK-BOUND will signal at
2325 ;; run-time and %CHECK-BOUND ir2-converter will signal at
2326 ;; compile-time if it survives further stages of ir1
2328 ((bound-cast-p cast
))
2330 ;; FIXME: Do it in one step.
2331 (let ((context (node-source-form cast
))
2332 (detail (lvar-all-sources (cast-value cast
))))
2335 (if (cast-single-value-p cast
)
2337 `(multiple-value-call #'list
'dummy
)))
2340 ;; FIXME: Derived type.
2341 `(%compile-time-type-error
'dummy
2342 ',(type-specifier atype
)
2343 ',(type-specifier value-type
)
2345 ',(compile-time-type-error-context context
))))
2346 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2347 ;; functions, so we declare the return type of
2348 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2350 (setq value
(cast-value cast
))
2351 (derive-node-type (lvar-uses value
) *empty-type
*)
2352 (maybe-terminate-block (lvar-uses value
) nil
)
2353 ;; FIXME: Is it necessary?
2354 (aver (null (block-pred (node-block cast
))))
2355 (delete-block-lazily (node-block cast
))
2356 (return-from ir1-optimize-cast
)))
2357 (when (eq (node-derived-type cast
) *empty-type
*)
2358 (maybe-terminate-block cast nil
))
2360 (when (and (cast-%type-check cast
)
2361 (values-subtypep value-type
2362 (cast-type-to-check cast
)))
2363 (setf (cast-%type-check cast
) nil
))))
2365 (unless do-not-optimize
2366 (setf (node-reoptimize cast
) nil
)))
2368 (deftransform make-symbol
((string) (simple-string))
2369 `(%make-symbol string
))