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 (but not singleton numeric types)
29 (let ((type (lvar-type thing
)))
30 (values (type-singleton-p type
))))))
32 ;;; Return the constant value for an LVAR whose only use is a constant
34 (declaim (ftype (function (lvar) t
) lvar-value
))
35 (defun lvar-value (lvar)
36 (let ((use (principal-lvar-use lvar
))
37 (type (lvar-type lvar
))
40 (constant-p (setf leaf
(ref-leaf use
))))
42 (multiple-value-bind (constantp value
) (type-singleton-p type
)
44 (error "~S used on non-constant LVAR ~S" 'lvar-value lvar
))
47 ;;;; interface for obtaining results of type inference
49 ;;; Our best guess for the type of this lvar's value. Note that this
50 ;;; may be VALUES or FUNCTION type, which cannot be passed as an
51 ;;; argument to the normal type operations. See LVAR-TYPE.
53 ;;; The result value is cached in the LVAR-%DERIVED-TYPE slot. If the
54 ;;; slot is true, just return that value, otherwise recompute and
55 ;;; stash the value there.
56 (eval-when (:compile-toplevel
:execute
)
57 (#+sb-xc-host cl
:defmacro
58 #-sb-xc-host sb
!xc
:defmacro
59 lvar-type-using
(lvar accessor
)
60 `(let ((uses (lvar-uses ,lvar
)))
61 (cond ((null uses
) *empty-type
*)
63 (do ((res (,accessor
(first uses
))
64 (values-type-union (,accessor
(first current
))
66 (current (rest uses
) (rest current
)))
67 ((or (null current
) (eq res
*wild-type
*))
72 #!-sb-fluid
(declaim (inline lvar-derived-type
))
73 (defun lvar-derived-type (lvar)
74 (declare (type lvar lvar
))
75 (or (lvar-%derived-type lvar
)
76 (setf (lvar-%derived-type lvar
)
77 (%lvar-derived-type lvar
))))
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
*))
232 #!-sb-fluid
(declaim (inline flush-lvar-externally-checkable-type
))
233 (defun flush-lvar-externally-checkable-type (lvar)
234 (declare (type lvar lvar
))
235 (setf (lvar-%externally-checkable-type lvar
) nil
))
237 ;;;; interface routines used by optimizers
239 (declaim (inline reoptimize-component
))
240 (defun reoptimize-component (component kind
)
241 (declare (type component component
)
242 (type (member nil
:maybe t
) kind
))
244 (unless (eq (component-reoptimize component
) t
)
245 (setf (component-reoptimize component
) kind
)))
247 ;;; This function is called by optimizers to indicate that something
248 ;;; interesting has happened to the value of LVAR. Optimizers must
249 ;;; make sure that they don't call for reoptimization when nothing has
250 ;;; happened, since optimization will fail to terminate.
252 ;;; We clear any cached type for the lvar and set the reoptimize flags
253 ;;; on everything in sight.
254 (defun reoptimize-lvar (lvar)
255 (declare (type (or lvar null
) lvar
))
257 (setf (lvar-%derived-type lvar
) nil
)
258 (let ((dest (lvar-dest lvar
)))
260 (setf (lvar-reoptimize lvar
) t
)
261 (setf (node-reoptimize dest
) t
)
262 (binding* (;; Since this may be called during IR1 conversion,
263 ;; PREV may be missing.
264 (prev (node-prev dest
) :exit-if-null
)
265 (block (ctran-block prev
))
266 (component (block-component block
)))
267 (when (typep dest
'cif
)
268 (setf (block-test-modified block
) t
))
269 (setf (block-reoptimize block
) t
)
270 (reoptimize-component component
:maybe
))))
272 (setf (block-type-check (node-block node
)) t
)))
275 (defun reoptimize-lvar-uses (lvar)
276 (declare (type lvar lvar
))
278 (setf (node-reoptimize use
) t
)
279 (setf (block-reoptimize (node-block use
)) t
)
280 (reoptimize-component (node-component use
) :maybe
)))
282 ;;; Annotate NODE to indicate that its result has been proven to be
283 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
284 ;;; only correct way to supply information discovered about a node's
285 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
286 ;;; information may be lost and reoptimization may not happen.
288 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
289 ;;; intersection is different from the old type, then we do a
290 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
291 (defun derive-node-type (node rtype
&key from-scratch
)
292 (declare (type valued-node node
) (type ctype rtype
))
293 (let* ((initial-type (node-derived-type node
))
294 (node-type (if from-scratch
297 (unless (eq initial-type rtype
)
298 (let ((int (values-type-intersection node-type rtype
))
299 (lvar (node-lvar node
)))
300 (when (type/= initial-type int
)
301 (when (and *check-consistency
*
302 (eq int
*empty-type
*)
303 (not (eq rtype
*empty-type
*)))
304 (aver (not from-scratch
))
305 (let ((*compiler-error-context
* node
))
307 "New inferred type ~S conflicts with old type:~
308 ~% ~S~%*** possible internal error? Please report this."
309 (type-specifier rtype
) (type-specifier node-type
))))
310 (setf (node-derived-type node
) int
)
311 ;; If the new type consists of only one object, replace the
312 ;; node with a constant reference.
313 (when (and (ref-p node
)
314 (lambda-var-p (ref-leaf node
)))
315 (let ((type (single-value-type int
)))
316 (when (and (member-type-p type
)
317 (eql 1 (member-type-size type
)))
318 (change-ref-leaf node
(find-constant
319 (first (member-type-members type
)))))))
320 (reoptimize-lvar lvar
)))))
323 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
324 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
325 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
326 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
327 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
328 (defun assert-lvar-type (lvar type policy
)
329 (declare (type lvar lvar
) (type ctype type
))
330 (unless (values-subtypep (lvar-derived-type lvar
) type
)
331 (let ((internal-lvar (make-lvar))
332 (dest (lvar-dest lvar
)))
333 (substitute-lvar internal-lvar lvar
)
334 (let ((cast (insert-cast-before dest lvar type policy
)))
335 (use-lvar cast internal-lvar
)
341 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
342 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
343 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
344 ;;; we are done, then another iteration would be beneficial.
345 (defun ir1-optimize (component fastp
)
346 (declare (type component component
))
347 (setf (component-reoptimize component
) nil
)
348 (loop with block
= (block-next (component-head component
))
349 with tail
= (component-tail component
)
350 for last-block
= block
351 until
(eq block tail
)
353 ;; We delete blocks when there is either no predecessor or the
354 ;; block is in a lambda that has been deleted. These blocks
355 ;; would eventually be deleted by DFO recomputation, but doing
356 ;; it here immediately makes the effect available to IR1
358 ((or (block-delete-p block
)
359 (null (block-pred block
)))
360 (delete-block-lazily block
)
361 (setq block
(clean-component component block
)))
362 ((eq (functional-kind (block-home-lambda block
)) :deleted
)
363 ;; Preserve the BLOCK-SUCC invariant that almost every block has
364 ;; one successor (and a block with DELETE-P set is an acceptable
366 (mark-for-deletion block
)
367 (setq block
(clean-component component block
)))
370 (let ((succ (block-succ block
)))
371 (unless (singleton-p succ
)
374 (let ((last (block-last block
)))
377 (flush-dest (if-test last
))
378 (when (unlink-node last
)
381 (when (maybe-delete-exit last
)
384 (unless (join-successor-if-possible block
)
387 (when (and (not fastp
) (block-reoptimize block
) (block-component block
))
388 (aver (not (block-delete-p block
)))
389 (ir1-optimize-block block
))
391 (cond ((and (block-delete-p block
) (block-component block
))
392 (setq block
(clean-component component block
)))
393 ((and (block-flush-p block
) (block-component block
))
394 (flush-dead-code block
)))))
395 do
(when (eq block last-block
)
396 (setq block
(block-next block
))))
400 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
403 ;;; Note that although they are cleared here, REOPTIMIZE flags might
404 ;;; still be set upon return from this function, meaning that further
405 ;;; optimization is wanted (as a consequence of optimizations we did).
406 (defun ir1-optimize-block (block)
407 (declare (type cblock block
))
408 ;; We clear the node and block REOPTIMIZE flags before doing the
409 ;; optimization, not after. This ensures that the node or block will
410 ;; be reoptimized if necessary.
411 (setf (block-reoptimize block
) nil
)
412 (do-nodes (node nil block
:restart-p t
)
413 (when (node-reoptimize node
)
414 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
415 (setf (node-reoptimize node
) nil
)
419 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
420 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
421 ;; any argument changes.
422 (ir1-optimize-combination node
))
424 (ir1-optimize-if node
))
426 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
427 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
428 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
430 (setf (node-reoptimize node
) t
)
431 (ir1-optimize-return node
))
433 (ir1-optimize-mv-combination node
))
435 ;; With an EXIT, we derive the node's type from the VALUE's
437 (let ((value (exit-value node
)))
439 (derive-node-type node
(lvar-derived-type value
)))))
441 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
442 ;; is accurate till the node actually has been reoptimized.
443 (setf (node-reoptimize node
) t
)
444 (ir1-optimize-set node
))
446 (ir1-optimize-cast node
)))))
450 ;;; Try to join with a successor block. If we succeed, we return true,
452 (defun join-successor-if-possible (block)
453 (declare (type cblock block
))
454 (let ((next (first (block-succ block
))))
455 (when (block-start next
) ; NEXT is not an END-OF-COMPONENT marker
456 (cond ( ;; We cannot combine with a successor block if:
458 ;; the successor has more than one predecessor;
459 (rest (block-pred next
))
460 ;; the successor is the current block (infinite loop);
462 ;; the next block has a different cleanup, and thus
463 ;; we may want to insert cleanup code between the
464 ;; two blocks at some point;
465 (not (eq (block-end-cleanup block
)
466 (block-start-cleanup next
)))
467 ;; the next block has a different home lambda, and
468 ;; thus the control transfer is a non-local exit.
469 (not (eq (block-home-lambda block
)
470 (block-home-lambda next
)))
471 ;; Stack analysis phase wants ENTRY to start a block...
472 (entry-p (block-start-node next
))
473 (let ((last (block-last block
)))
474 (and (valued-node-p last
)
475 (awhen (node-lvar last
)
477 ;; ... and a DX-allocator to end a block.
478 (lvar-dynamic-extent it
)
479 ;; FIXME: This is a partial workaround for bug 303.
480 (consp (lvar-uses it
)))))))
483 (join-blocks block next
)
486 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
487 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
488 ;;; for the two blocks so that any indicated optimization gets done.
489 (defun join-blocks (block1 block2
)
490 (declare (type cblock block1 block2
))
491 (let* ((last1 (block-last block1
))
492 (last2 (block-last block2
))
493 (succ (block-succ block2
))
494 (start2 (block-start block2
)))
495 (do ((ctran start2
(node-next (ctran-next ctran
))))
497 (setf (ctran-block ctran
) block1
))
499 (unlink-blocks block1 block2
)
501 (unlink-blocks block2 block
)
502 (link-blocks block1 block
))
504 (setf (ctran-kind start2
) :inside-block
)
505 (setf (node-next last1
) start2
)
506 (setf (ctran-use start2
) last1
)
507 (setf (block-last block1
) last2
))
509 (setf (block-flags block1
)
510 (attributes-union (block-flags block1
)
512 (block-attributes type-asserted test-modified
)))
514 (let ((next (block-next block2
))
515 (prev (block-prev block2
)))
516 (setf (block-next prev
) next
)
517 (setf (block-prev next
) prev
))
521 ;;; Delete any nodes in BLOCK whose value is unused and which have no
522 ;;; side effects. We can delete sets of lexical variables when the set
523 ;;; variable has no references.
524 (defun flush-dead-code (block &aux victim
)
525 (declare (type cblock block
))
526 (setf (block-flush-p block
) nil
)
527 (do-nodes-backwards (node lvar block
:restart-p t
)
535 (when (flushable-combination-p node
)
537 (flush-combination node
)))
539 (when (eq (basic-combination-kind node
) :local
)
540 (let ((fun (combination-lambda node
)))
541 (when (dolist (var (lambda-vars fun
) t
)
542 (when (or (leaf-refs var
)
543 (lambda-var-sets var
))
546 (flush-dest (first (basic-combination-args node
)))
549 (let ((value (exit-value node
)))
553 (setf (exit-value node
) nil
))))
555 (let ((var (set-var node
)))
556 (when (and (lambda-var-p var
)
557 (null (leaf-refs var
)))
559 (flush-dest (set-value node
))
560 (setf (basic-var-sets var
)
561 (delq node
(basic-var-sets var
)))
562 (unlink-node node
))))
564 (unless (cast-type-check node
)
566 (flush-dest (cast-value node
))
567 (unlink-node node
))))))
571 ;;;; local call return type propagation
573 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
574 ;;; flag set. It iterates over the uses of the RESULT, looking for
575 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
576 ;;; call, then we union its type together with the types of other such
577 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
578 ;;; type with the RESULT's asserted type. We can make this
579 ;;; intersection now (potentially before type checking) because this
580 ;;; assertion on the result will eventually be checked (if
583 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
584 ;;; combination, which may change the successor of the call to be the
585 ;;; called function, and if so, checks if the call can become an
586 ;;; assignment. If we convert to an assignment, we abort, since the
587 ;;; RETURN has been deleted.
588 (defun find-result-type (node)
589 (declare (type creturn node
))
590 (let ((result (return-result node
)))
591 (collect ((use-union *empty-type
* values-type-union
))
592 (do-uses (use result
)
593 (let ((use-home (node-home-lambda use
)))
594 (cond ((or (eq (functional-kind use-home
) :deleted
)
595 (block-delete-p (node-block use
))))
596 ((and (basic-combination-p use
)
597 (eq (basic-combination-kind use
) :local
))
598 (aver (eq (lambda-tail-set use-home
)
599 (lambda-tail-set (combination-lambda use
))))
600 (when (combination-p use
)
601 (when (nth-value 1 (maybe-convert-tail-local-call use
))
602 (return-from find-result-type t
))))
604 (use-union (node-derived-type use
))))))
606 ;; (values-type-intersection
607 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
611 (setf (return-result-type node
) int
))))
614 ;;; Do stuff to realize that something has changed about the value
615 ;;; delivered to a return node. Since we consider the return values of
616 ;;; all functions in the tail set to be equivalent, this amounts to
617 ;;; bringing the entire tail set up to date. We iterate over the
618 ;;; returns for all the functions in the tail set, reanalyzing them
619 ;;; all (not treating NODE specially.)
621 ;;; When we are done, we check whether the new type is different from
622 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
623 ;;; all the lvars for references to functions in the tail set. This
624 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
625 ;;; results of the calls.
626 (defun ir1-optimize-return (node)
627 (declare (type creturn node
))
630 (let* ((tails (lambda-tail-set (return-lambda node
)))
631 (funs (tail-set-funs tails
)))
632 (collect ((res *empty-type
* values-type-union
))
634 (let ((return (lambda-return fun
)))
636 (when (node-reoptimize return
)
637 (setf (node-reoptimize return
) nil
)
638 (when (find-result-type return
)
640 (res (return-result-type return
)))))
642 (when (type/= (res) (tail-set-type tails
))
643 (setf (tail-set-type tails
) (res))
644 (dolist (fun (tail-set-funs tails
))
645 (dolist (ref (leaf-refs fun
))
646 (reoptimize-lvar (node-lvar ref
))))))))
652 ;;; Utility: return T if both argument cblocks are equivalent. For now,
653 ;;; detect only blocks that read the same leaf into the same lvar, and
654 ;;; continue to the same block.
655 (defun cblocks-equivalent-p (x y
)
656 (declare (type cblock x y
))
657 (and (ref-p (block-start-node x
))
658 (eq (block-last x
) (block-start-node x
))
660 (ref-p (block-start-node y
))
661 (eq (block-last y
) (block-start-node y
))
663 (equal (block-succ x
) (block-succ y
))
664 (eql (ref-lvar (block-start-node x
)) (ref-lvar (block-start-node y
)))
665 (eql (ref-leaf (block-start-node x
)) (ref-leaf (block-start-node y
)))))
667 ;;; Check whether the predicate is known to be true or false,
668 ;;; deleting the IF node in favor of the appropriate branch when this
670 ;;; Similarly, when both branches are equivalent, branch directly to either
672 ;;; Also, if the test has multiple uses, replicate the node when possible...
673 ;;; in fact, splice in direct jumps to the right branch if possible.
674 (defun ir1-optimize-if (node)
675 (declare (type cif node
))
676 (let ((test (if-test node
))
677 (block (node-block node
)))
678 (let* ((type (lvar-type test
))
679 (consequent (if-consequent node
))
680 (alternative (if-alternative node
))
682 (cond ((constant-lvar-p test
)
683 (if (lvar-value test
) alternative consequent
))
684 ((not (types-equal-or-intersect type
(specifier-type 'null
)))
686 ((type= type
(specifier-type 'null
))
688 ((or (eq consequent alternative
) ; Can this happen?
689 (cblocks-equivalent-p alternative consequent
))
692 (kill-if-branch-1 node test block victim
)
693 (return-from ir1-optimize-if
(values))))
694 (tension-if-if-1 node test block
)
695 (duplicate-if-if-1 node test block
)
698 ;; When we know that we only have a single successor, kill the victim
699 ;; ... unless the victim and the remaining successor are the same.
700 (defun kill-if-branch-1 (node test block victim
)
701 (declare (type cif node
))
703 (when (rest (block-succ block
))
704 (unlink-blocks block victim
))
705 (setf (component-reanalyze (node-component node
)) t
)
708 ;; When if/if conversion would leave (if ... (if nil ...)) or
709 ;; (if ... (if not-nil ...)), splice the correct successor right
711 (defun tension-if-if-1 (node test block
)
712 (when (and (eq (block-start-node block
) node
)
713 (listp (lvar-uses test
)))
715 (when (immediately-used-p test use
)
716 (let* ((type (single-value-type (node-derived-type use
)))
717 (target (if (type= type
(specifier-type 'null
))
718 (if-alternative node
)
719 (multiple-value-bind (typep surep
)
721 (and (not typep
) surep
722 (if-consequent node
))))))
724 (let ((pred (node-block use
)))
725 (cond ((listp (lvar-uses test
))
726 (change-block-successor pred block target
)
727 (delete-lvar-use use
))
729 ;; only one use left. Just kill the now-useless
730 ;; branch to avoid spurious code deletion notes.
731 (aver (rest (block-succ block
)))
734 (if (eql target
(if-alternative node
))
736 (if-alternative node
)))
737 (return-from tension-if-if-1
))))))))))
739 ;; Finally, duplicate EQ-nil tests
740 (defun duplicate-if-if-1 (node test block
)
741 (when (and (eq (block-start-node block
) node
)
742 (listp (lvar-uses test
)))
744 (when (immediately-used-p test use
)
745 (convert-if-if use node
)
746 ;; leave the last use as is, instead of replacing
747 ;; the (singly-referenced) CIF node with a duplicate.
748 (when (not (listp (lvar-uses test
))) (return))))))
750 ;;; Create a new copy of an IF node that tests the value of the node
751 ;;; USE. The test must have >1 use, and must be immediately used by
752 ;;; USE. NODE must be the only node in its block (implying that
753 ;;; block-start = if-test).
755 ;;; This optimization has an effect semantically similar to the
756 ;;; source-to-source transformation:
757 ;;; (IF (IF A B C) D E) ==>
758 ;;; (IF A (IF B D E) (IF C D E))
760 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
761 ;;; node so that dead code deletion notes will definitely not consider
762 ;;; either node to be part of the original source. One node might
763 ;;; become unreachable, resulting in a spurious note.
764 (defun convert-if-if (use node
)
765 (declare (type node use
) (type cif node
))
766 (with-ir1-environment-from-node node
767 (let* ((block (node-block node
))
768 (test (if-test node
))
769 (cblock (if-consequent node
))
770 (ablock (if-alternative node
))
771 (use-block (node-block use
))
772 (new-ctran (make-ctran))
773 (new-lvar (make-lvar))
774 (new-node (make-if :test new-lvar
776 :alternative ablock
))
777 (new-block (ctran-starts-block new-ctran
)))
778 (link-node-to-previous-ctran new-node new-ctran
)
779 (setf (lvar-dest new-lvar
) new-node
)
780 (setf (block-last new-block
) new-node
)
782 (unlink-blocks use-block block
)
783 (%delete-lvar-use use
)
784 (add-lvar-use use new-lvar
)
785 (link-blocks use-block new-block
)
787 (link-blocks new-block cblock
)
788 (link-blocks new-block ablock
)
790 (push "<IF Duplication>" (node-source-path node
))
791 (push "<IF Duplication>" (node-source-path new-node
))
793 (reoptimize-lvar test
)
794 (reoptimize-lvar new-lvar
)
795 (setf (component-reanalyze *current-component
*) t
)))
798 ;;;; exit IR1 optimization
800 ;;; This function attempts to delete an exit node, returning true if
801 ;;; it deletes the block as a consequence:
802 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
803 ;;; anything, since there is nothing to be done.
804 ;;; -- If the exit node and its ENTRY have the same home lambda then
805 ;;; we know the exit is local, and can delete the exit. We change
806 ;;; uses of the Exit-Value to be uses of the original lvar,
807 ;;; then unlink the node. If the exit is to a TR context, then we
808 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
809 ;;; their value to this exit.
810 ;;; -- If there is no value (as in a GO), then we skip the value
813 ;;; This function is also called by environment analysis, since it
814 ;;; wants all exits to be optimized even if normal optimization was
816 (defun maybe-delete-exit (node)
817 (declare (type exit node
))
818 (let ((value (exit-value node
))
819 (entry (exit-entry node
)))
821 (eq (node-home-lambda node
) (node-home-lambda entry
)))
822 (setf (entry-exits entry
) (delq node
(entry-exits entry
)))
824 (with-ir1-environment-from-node entry
825 ;; We can't simply use DELETE-FILTER to unlink the node
826 ;; and substitute some LVAR magic, as this can confuse the
827 ;; stack analysis if there's another EXIT to the same
828 ;; continuation. Instead, we fabricate a new block (in
829 ;; the same lexenv as the ENTRY, so it can't be merged
830 ;; backwards), insert a gimmicked CAST node to link up the
831 ;; LVAR holding the value being returned to the LVAR which
832 ;; is expecting to accept the value, thus placing the
833 ;; return value where it needs to be while still providing
834 ;; the hook required for stack analysis.
835 ;; -- AJB, 2014-Mar-03
836 (let* ((exit-block (node-block node
))
837 (new-ctran (make-ctran))
838 (new-block (ctran-starts-block new-ctran
))
841 :asserted-type
*wild-type
*
842 :type-to-check
*wild-type
*
844 :vestigial-exit-lexenv
(node-lexenv node
)
845 :vestigial-exit-entry-lexenv
(node-lexenv entry
)
847 ;; We only expect a single successor to EXIT-BLOCK,
848 ;; because it contains an EXIT node (which must end its
849 ;; block) and the only blocks that have more than once
850 ;; successor are those with IF nodes (which also must
851 ;; end their blocks). Still, just to be sure, we use a
852 ;; construct that guarantees an error if this
853 ;; expectation is violated.
856 (block-succ exit-block
)
858 ;; Finish creating the new block.
859 (link-node-to-previous-ctran cast-node new-ctran
)
860 (setf (block-last new-block
) cast-node
)
862 ;; Link the new block into the control sequence.
863 (unlink-blocks exit-block entry-block
)
864 (link-blocks exit-block new-block
)
865 (link-blocks new-block entry-block
)
867 ;; Finish re-pointing the value-holding LVAR to the
869 (setf (lvar-dest value
) cast-node
)
870 (setf (exit-value node
) nil
)
871 (reoptimize-lvar value
)
873 ;; Register the CAST node as providing a value to the
874 ;; LVAR for the continuation.
875 (add-lvar-use cast-node
(node-lvar node
))
876 (reoptimize-lvar (node-lvar node
))
878 ;; Remove the EXIT node.
881 ;; And, because we created a new block, we need to
882 ;; force component reanalysis (to assign a DFO number
883 ;; to the block if nothing else).
884 (setf (component-reanalyze *current-component
*) t
))))
885 (unlink-node node
)))))
888 ;;;; combination IR1 optimization
890 ;;; Report as we try each transform?
892 (defvar *show-transforms-p
* nil
)
894 (defun check-important-result (node info
)
895 (when (and (null (node-lvar node
))
896 (ir1-attributep (fun-info-attributes info
) important-result
))
897 (let ((*compiler-error-context
* node
))
899 "The return value of ~A should not be discarded."
900 (lvar-fun-name (basic-combination-fun node
))))))
902 ;;; Do IR1 optimizations on a COMBINATION node.
903 (declaim (ftype (function (combination) (values)) ir1-optimize-combination
))
904 (defun ir1-optimize-combination (node)
905 (when (lvar-reoptimize (basic-combination-fun node
))
906 (propagate-fun-change node
)
907 (maybe-terminate-block node nil
))
908 (let ((args (basic-combination-args node
))
909 (kind (basic-combination-kind node
))
910 (info (basic-combination-fun-info node
)))
913 (let ((fun (combination-lambda node
)))
914 (if (eq (functional-kind fun
) :let
)
915 (propagate-let-args node fun
)
916 (propagate-local-call-args node fun
))))
920 (setf (lvar-reoptimize arg
) nil
))))
924 (setf (lvar-reoptimize arg
) nil
)))
926 (check-important-result node info
)
927 (let ((fun (fun-info-destroyed-constant-args info
)))
929 (let ((destroyed-constant-args (funcall fun args
)))
930 (when destroyed-constant-args
931 (let ((*compiler-error-context
* node
))
932 (warn 'constant-modified
933 :fun-name
(lvar-fun-name
934 (basic-combination-fun node
)))
935 (setf (basic-combination-kind node
) :error
)
936 (return-from ir1-optimize-combination
))))))
937 (let ((fun (fun-info-derive-type info
)))
939 (let ((res (funcall fun node
)))
941 (derive-node-type node
(coerce-to-values res
))
942 (maybe-terminate-block node nil
))))))
944 ;; Check against the DEFINED-TYPE unless TYPE is already good.
945 (let* ((fun (basic-combination-fun node
))
946 (uses (lvar-uses fun
))
947 (leaf (when (ref-p uses
) (ref-leaf uses
))))
948 (multiple-value-bind (type defined-type
)
949 (if (global-var-p leaf
)
950 (values (leaf-type leaf
) (leaf-defined-type leaf
))
952 (when (and (not (fun-type-p type
)) (fun-type-p defined-type
))
953 (validate-call-type node type leaf
)))))))
958 (setf (lvar-reoptimize arg
) nil
)))
959 (check-important-result node info
)
960 (let ((fun (fun-info-destroyed-constant-args info
)))
962 ;; If somebody is really sure that they want to modify
963 ;; constants, let them.
964 (policy node
(> check-constant-modification
0)))
965 (let ((destroyed-constant-args (funcall fun args
)))
966 (when destroyed-constant-args
967 (let ((*compiler-error-context
* node
))
968 (warn 'constant-modified
969 :fun-name
(lvar-fun-name
970 (basic-combination-fun node
)))
971 (setf (basic-combination-kind node
) :error
)
972 (return-from ir1-optimize-combination
))))))
974 (let ((attr (fun-info-attributes info
)))
975 (when (and (ir1-attributep attr foldable
)
976 ;; KLUDGE: The next test could be made more sensitive,
977 ;; only suppressing constant-folding of functions with
978 ;; CALL attributes when they're actually passed
979 ;; function arguments. -- WHN 19990918
980 (not (ir1-attributep attr call
))
981 (every #'constant-lvar-p args
)
983 (constant-fold-call node
)
984 (return-from ir1-optimize-combination
))
985 (when (and (ir1-attributep attr commutative
)
987 (constant-lvar-p (first args
))
988 (not (constant-lvar-p (second args
))))
989 (setf (basic-combination-args node
) (nreverse args
))))
990 (let ((fun (fun-info-derive-type info
)))
992 (let ((res (funcall fun node
)))
994 (derive-node-type node
(coerce-to-values res
))
995 (maybe-terminate-block node nil
)))))
997 (let ((fun (fun-info-optimizer info
)))
998 (unless (and fun
(funcall fun node
))
999 ;; First give the VM a peek at the call
1000 (multiple-value-bind (style transform
)
1001 (combination-implementation-style node
)
1004 ;; The VM knows how to handle this.
1007 ;; The VM mostly knows how to handle this. We need
1008 ;; to massage the call slightly, though.
1009 (transform-call node transform
(combination-fun-source-name node
)))
1011 ;; Let transforms have a crack at it.
1012 (dolist (x (fun-info-transforms info
))
1014 (when *show-transforms-p
*
1015 (let* ((lvar (basic-combination-fun node
))
1016 (fname (lvar-fun-name lvar t
)))
1017 (/show
"trying transform" x
(transform-function x
) "for" fname
)))
1018 (unless (ir1-transform node x
)
1020 (when *show-transforms-p
*
1021 (/show
"quitting because IR1-TRANSFORM result was NIL"))
1026 (defun xep-tail-combination-p (node)
1027 (and (combination-p node
)
1028 (let* ((lvar (combination-lvar node
))
1029 (dest (when (lvar-p lvar
) (lvar-dest lvar
)))
1030 (lambda (when (return-p dest
) (return-lambda dest
))))
1031 (and (lambda-p lambda
)
1032 (eq :external
(lambda-kind lambda
))))))
1034 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
1035 ;;; the block there, and link it to the component tail.
1037 ;;; Except when called during IR1 convertion, we delete the
1038 ;;; continuation if it has no other uses. (If it does have other uses,
1041 ;;; Termination on the basis of a continuation type is
1043 ;;; -- The continuation is deleted (hence the assertion is spurious), or
1044 ;;; -- We are in IR1 conversion (where THE assertions are subject to
1045 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
1046 ;;; uses can(?) be added later. -- APD, 2003-07-17
1048 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
1049 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p
)
1050 (declare (type (or basic-combination cast ref
) node
))
1051 (let* ((block (node-block node
))
1052 (lvar (node-lvar node
))
1053 (ctran (node-next node
))
1054 (tail (component-tail (block-component block
)))
1055 (succ (first (block-succ block
))))
1056 (declare (ignore lvar
))
1057 (unless (or (and (eq node
(block-last block
)) (eq succ tail
))
1058 (block-delete-p block
))
1059 ;; Even if the combination will never return, don't terminate if this
1060 ;; is the tail call of a XEP: doing that would inhibit TCO.
1061 (when (and (eq (node-derived-type node
) *empty-type
*)
1062 (not (xep-tail-combination-p node
)))
1063 (cond (ir1-converting-not-optimizing-p
1066 (aver (eq (block-last block
) node
)))
1068 (setf (block-last block
) node
)
1069 (setf (ctran-use ctran
) nil
)
1070 (setf (ctran-kind ctran
) :unused
)
1071 (setf (ctran-block ctran
) nil
)
1072 (setf (node-next node
) nil
)
1073 (link-blocks block
(ctran-starts-block ctran
)))))
1075 (node-ends-block node
)))
1077 (let ((succ (first (block-succ block
))))
1078 (unlink-blocks block succ
)
1079 (setf (component-reanalyze (block-component block
)) t
)
1080 (aver (not (block-succ block
)))
1081 (link-blocks block tail
)
1082 (cond (ir1-converting-not-optimizing-p
1083 (%delete-lvar-use node
))
1084 (t (delete-lvar-use node
)
1085 (when (null (block-pred succ
))
1086 (mark-for-deletion succ
)))))
1089 ;;; This is called both by IR1 conversion and IR1 optimization when
1090 ;;; they have verified the type signature for the call, and are
1091 ;;; wondering if something should be done to special-case the call. If
1092 ;;; CALL is a call to a global function, then see whether it defined
1094 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
1095 ;;; the expansion and change the call to call it. Expansion is
1096 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
1097 ;;; true, we never expand, since this function has already been
1098 ;;; converted. Local call analysis will duplicate the definition
1099 ;;; if necessary. We claim that the parent form is LABELS for
1100 ;;; context declarations, since we don't want it to be considered
1101 ;;; a real global function.
1102 ;;; -- If it is a known function, mark it as such by setting the KIND.
1104 ;;; We return the leaf referenced (NIL if not a leaf) and the
1105 ;;; FUN-INFO assigned.
1106 (defun recognize-known-call (call ir1-converting-not-optimizing-p
)
1107 (declare (type combination call
))
1108 (let* ((ref (lvar-uses (basic-combination-fun call
)))
1109 (leaf (when (ref-p ref
) (ref-leaf ref
)))
1110 (inlinep (if (defined-fun-p leaf
)
1111 (defined-fun-inlinep leaf
)
1114 ((eq inlinep
:notinline
)
1115 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1117 (setf (basic-combination-fun-info call
) info
))
1119 ((not (and (global-var-p leaf
)
1120 (eq (global-var-kind leaf
) :global-function
)))
1122 ((and (ecase inlinep
1125 ((nil :maybe-inline
) (policy call
(zerop space
))))
1126 (defined-fun-p leaf
)
1127 (defined-fun-inline-expansion leaf
)
1128 (inline-expansion-ok call
))
1129 ;; Inline: if the function has already been converted at another call
1130 ;; site in this component, we point this REF to the functional. If not,
1131 ;; we convert the expansion.
1133 ;; For :INLINE case local call analysis will copy the expansion later,
1134 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1135 ;; expansion per component.
1137 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1140 (let* ((name (leaf-source-name leaf
))
1141 (res (ir1-convert-inline-expansion
1143 (defined-fun-inline-expansion leaf
)
1146 (info :function
:info name
))))
1147 ;; Allow backward references to this function from following
1148 ;; forms. (Reused only if policy matches.)
1149 (push res
(defined-fun-functionals leaf
))
1150 (change-ref-leaf ref res
))))
1151 (let ((fun (defined-fun-functional leaf
)))
1153 (and (eq inlinep
:inline
) (functional-kind fun
)))
1155 (if ir1-converting-not-optimizing-p
1157 (with-ir1-environment-from-node call
1159 (locall-analyze-component *current-component
*)))
1160 ;; If we've already converted, change ref to the converted
1162 (change-ref-leaf ref fun
))))
1163 (values (ref-leaf ref
) nil
))
1165 (let ((info (info :function
:info
(leaf-source-name leaf
))))
1169 (setf (basic-combination-kind call
) :known
)
1170 (setf (basic-combination-fun-info call
) info
)))
1171 (values leaf nil
)))))))
1173 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1174 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1175 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1176 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1177 ;;; syntax check, arg/result type processing, but still call
1178 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1179 ;;; and that checking is done by local call analysis.
1180 (defun validate-call-type (call type fun
&optional ir1-converting-not-optimizing-p
)
1181 (declare (type combination call
) (type ctype type
))
1182 (let* ((where (when fun
(leaf-where-from fun
)))
1183 (same-file-p (eq :defined-here where
)))
1184 (cond ((not (fun-type-p type
))
1185 (aver (multiple-value-bind (val win
)
1186 (csubtypep type
(specifier-type 'function
))
1187 (or val
(not win
))))
1188 ;; Using the defined-type too early is a bit of a waste: during
1189 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1190 (when (and fun
(not ir1-converting-not-optimizing-p
))
1191 (let ((defined-type (leaf-defined-type fun
)))
1192 (when (and (fun-type-p defined-type
)
1193 (neq fun
(combination-type-validated-for-leaf call
)))
1194 ;; Don't validate multiple times against the same leaf --
1195 ;; it doesn't add any information, but may generate the same warning
1197 (setf (combination-type-validated-for-leaf call
) fun
)
1198 (when (and (valid-fun-use call defined-type
1199 :argument-test
#'always-subtypep
1201 :lossage-fun
(if same-file-p
1203 #'compiler-style-warn
)
1204 :unwinnage-fun
#'compiler-notify
)
1206 (assert-call-type call defined-type nil
)
1207 (maybe-terminate-block call ir1-converting-not-optimizing-p
)))))
1208 (recognize-known-call call ir1-converting-not-optimizing-p
))
1209 ((valid-fun-use call type
1210 :argument-test
#'always-subtypep
1212 :lossage-fun
#'compiler-warn
1213 :unwinnage-fun
#'compiler-notify
)
1214 (assert-call-type call type
)
1215 (maybe-terminate-block call ir1-converting-not-optimizing-p
)
1216 (recognize-known-call call ir1-converting-not-optimizing-p
))
1218 (setf (combination-kind call
) :error
)
1219 (values nil nil
)))))
1221 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1222 ;;; changed. If the call is local, we try to LET-convert it, and
1223 ;;; derive the result type. If it is a :FULL call, we validate it
1224 ;;; against the type, which recognizes known calls, does inline
1225 ;;; expansion, etc. If a call to a predicate in a non-conditional
1226 ;;; position or to a function with a source transform, then we
1227 ;;; reconvert the form to give IR1 another chance.
1228 (defun propagate-fun-change (call)
1229 (declare (type combination call
))
1230 (let ((*compiler-error-context
* call
)
1231 (fun-lvar (basic-combination-fun call
)))
1232 (setf (lvar-reoptimize fun-lvar
) nil
)
1233 (case (combination-kind call
)
1235 (let ((fun (combination-lambda call
)))
1236 (maybe-let-convert fun
)
1237 (unless (member (functional-kind fun
) '(:let
:assignment
:deleted
))
1238 (derive-node-type call
(tail-set-type (lambda-tail-set fun
))))))
1240 (multiple-value-bind (leaf info
)
1241 (let* ((uses (lvar-uses fun-lvar
))
1242 (leaf (when (ref-p uses
) (ref-leaf uses
))))
1243 (validate-call-type call
(lvar-type fun-lvar
) leaf
))
1244 (cond ((functional-p leaf
)
1245 (convert-call-if-possible
1246 (lvar-uses (basic-combination-fun call
))
1249 ((and (global-var-p leaf
)
1250 (eq (global-var-kind leaf
) :global-function
)
1251 (leaf-has-source-name-p leaf
)
1252 (or (info :function
:source-transform
(leaf-source-name leaf
))
1254 (ir1-attributep (fun-info-attributes info
)
1256 (let ((lvar (node-lvar call
)))
1257 (and lvar
(not (if-p (lvar-dest lvar
))))))))
1258 (let ((name (leaf-source-name leaf
))
1259 (dummies (make-gensym-list
1260 (length (combination-args call
)))))
1261 (transform-call call
1263 (,@(if (symbolp name
)
1267 (leaf-source-name leaf
)))))))))
1270 ;;;; known function optimization
1272 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1273 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1274 ;;; replace it, otherwise add a new one.
1275 (defun record-optimization-failure (node transform args
)
1276 (declare (type combination node
) (type transform transform
)
1277 (type (or fun-type list
) args
))
1278 (let* ((table (component-failed-optimizations *component-being-compiled
*))
1279 (found (assoc transform
(gethash node table
))))
1281 (setf (cdr found
) args
)
1282 (push (cons transform args
) (gethash node table
))))
1285 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1286 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1287 ;;; doing the transform for some reason and FLAME is true, then we
1288 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1289 ;;; finalize to pick up. We return true if the transform failed, and
1290 ;;; thus further transformation should be attempted. We return false
1291 ;;; if either the transform succeeded or was aborted.
1292 (defun ir1-transform (node transform
)
1293 (declare (type combination node
) (type transform transform
))
1294 (let* ((type (transform-type transform
))
1295 (fun (transform-function transform
))
1296 (constrained (fun-type-p type
))
1297 (table (component-failed-optimizations *component-being-compiled
*))
1298 (flame (case (transform-important transform
)
1299 ((t) (policy node
(>= speed inhibit-warnings
)))
1300 (:slightly
(policy node
(> speed inhibit-warnings
)))))
1301 (*compiler-error-context
* node
))
1302 (cond ((or (not constrained
)
1303 (valid-fun-use node type
))
1304 (multiple-value-bind (severity args
)
1305 (catch 'give-up-ir1-transform
1306 (transform-call node
1308 (combination-fun-source-name node
))
1312 (remhash node table
)
1315 (setf (combination-kind node
) :error
)
1317 (apply #'warn args
))
1318 (remhash node table
)
1323 (record-optimization-failure node transform args
))
1324 (setf (gethash node table
)
1325 (remove transform
(gethash node table
) :key
#'car
)))
1328 (remhash node table
)
1333 :argument-test
#'types-equal-or-intersect
1334 :result-test
#'values-types-equal-or-intersect
))
1335 (record-optimization-failure node transform type
)
1340 ;;; When we don't like an IR1 transform, we throw the severity/reason
1343 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1344 ;;; aborting this attempt to transform the call, but admitting the
1345 ;;; possibility that this or some other transform will later succeed.
1346 ;;; If arguments are supplied, they are format arguments for an
1347 ;;; efficiency note.
1349 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1350 ;;; force a normal call to the function at run time. No further
1351 ;;; optimizations will be attempted.
1353 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1354 ;;; delay the transform on the node until later. REASONS specifies
1355 ;;; when the transform will be later retried. The :OPTIMIZE reason
1356 ;;; causes the transform to be delayed until after the current IR1
1357 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1358 ;;; be delayed until after constraint propagation.
1360 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1361 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1362 ;;; do CASE operations on the various REASON values, it might be a
1363 ;;; good idea to go OO, representing the reasons by objects, using
1364 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1365 ;;; SIGNAL instead of THROW.
1366 (declaim (ftype (function (&rest t
) nil
) give-up-ir1-transform
))
1367 (defun give-up-ir1-transform (&rest args
)
1368 (throw 'give-up-ir1-transform
(values :failure args
)))
1369 (defun abort-ir1-transform (&rest args
)
1370 (throw 'give-up-ir1-transform
(values :aborted args
)))
1371 (defun delay-ir1-transform (node &rest reasons
)
1372 (let ((assoc (assoc node
*delayed-ir1-transforms
*)))
1374 (setf *delayed-ir1-transforms
*
1375 (acons node reasons
*delayed-ir1-transforms
*))
1376 (throw 'give-up-ir1-transform
:delayed
))
1378 (dolist (reason reasons
)
1379 (pushnew reason
(cdr assoc
)))
1380 (throw 'give-up-ir1-transform
:delayed
)))))
1382 ;;; Poor man's catching and resignalling
1383 ;;; Implicit GIVE-UP macrolet will resignal the give-up "condition"
1384 (defmacro catch-give-up-ir1-transform
(form &body gave-up-body
)
1385 (let ((block (gensym "BLOCK"))
1386 (kind (gensym "KIND"))
1387 (args (gensym "ARGS")))
1389 (multiple-value-bind (,kind
,args
)
1390 (catch 'give-up-ir1-transform
1391 (return-from ,block
,form
))
1394 (throw 'give-up-ir1-transform
:delayed
))
1395 ((:failure
:aborted
)
1396 (macrolet ((give-up ()
1397 `(throw 'give-up-ir1-transform
(values ,',kind
1399 ,@gave-up-body
)))))))
1401 ;;; Clear any delayed transform with no reasons - these should have
1402 ;;; been tried in the last pass. Then remove the reason from the
1403 ;;; delayed transform reasons, and if any become empty then set
1404 ;;; reoptimize flags for the node. Return true if any transforms are
1406 (defun retry-delayed-ir1-transforms (reason)
1407 (setf *delayed-ir1-transforms
*
1408 (remove-if-not #'cdr
*delayed-ir1-transforms
*))
1409 (let ((reoptimize nil
))
1410 (dolist (assoc *delayed-ir1-transforms
*)
1411 (let ((reasons (remove reason
(cdr assoc
))))
1412 (setf (cdr assoc
) reasons
)
1414 (let ((node (car assoc
)))
1415 (unless (node-deleted node
)
1417 (setf (node-reoptimize node
) t
)
1418 (let ((block (node-block node
)))
1419 (setf (block-reoptimize block
) t
)
1420 (reoptimize-component (block-component block
) :maybe
)))))))
1423 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1424 ;;; environment, and then install it as the function for the call
1425 ;;; NODE. We do local call analysis so that the new function is
1426 ;;; integrated into the control flow.
1428 ;;; We require the original function source name in order to generate
1429 ;;; a meaningful debug name for the lambda we set up. (It'd be
1430 ;;; possible to do this starting from debug names as well as source
1431 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1432 ;;; generality, since source names are always known to our callers.)
1433 (defun transform-call (call res source-name
)
1434 (declare (type combination call
) (list res
))
1435 (aver (and (legal-fun-name-p source-name
)
1436 (not (eql source-name
'.anonymous.
))))
1437 (node-ends-block call
)
1438 ;; The internal variables of a transform are not going to be
1439 ;; interesting to the debugger, so there's no sense in
1440 ;; suppressing the substitution of variables with only one use
1441 ;; (the extra variables can slow down constraint propagation).
1443 ;; This needs to be done before the WITH-IR1-ENVIRONMENT-FROM-NODE,
1444 ;; so that it will bind *LEXENV* to the right environment.
1445 (setf (combination-lexenv call
)
1446 (make-lexenv :default
(combination-lexenv call
)
1447 :policy
(process-optimize-decl
1449 (preserve-single-use-debug-variables 0))
1451 (combination-lexenv call
)))))
1452 (with-ir1-environment-from-node call
1453 (with-component-last-block (*current-component
*
1454 (block-next (node-block call
)))
1456 (let ((new-fun (ir1-convert-inline-lambda
1458 :debug-name
(debug-name 'lambda-inlined source-name
)
1460 (ref (lvar-use (combination-fun call
))))
1461 (change-ref-leaf ref new-fun
)
1462 (setf (combination-kind call
) :full
)
1463 (locall-analyze-component *current-component
*))))
1466 ;;; Replace a call to a foldable function of constant arguments with
1467 ;;; the result of evaluating the form. If there is an error during the
1468 ;;; evaluation, we give a warning and leave the call alone, making the
1469 ;;; call a :ERROR call.
1471 ;;; If there is more than one value, then we transform the call into a
1473 (defun constant-fold-call (call)
1474 (let ((args (mapcar #'lvar-value
(combination-args call
)))
1475 (fun-name (combination-fun-source-name call
)))
1476 (multiple-value-bind (values win
)
1477 (careful-call fun-name
1480 ;; Note: CMU CL had COMPILER-WARN here, and that
1481 ;; seems more natural, but it's probably not.
1483 ;; It's especially not while bug 173 exists:
1486 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1488 ;; can cause constant-folding TYPE-ERRORs (in
1489 ;; #'<=) when END can be proved to be NIL, even
1490 ;; though the code is perfectly legal and safe
1491 ;; because a NIL value of END means that the
1492 ;; #'<= will never be executed.
1494 ;; Moreover, even without bug 173,
1495 ;; quite-possibly-valid code like
1496 ;; (COND ((NONINLINED-PREDICATE END)
1497 ;; (UNLESS (<= END SIZE))
1499 ;; (where NONINLINED-PREDICATE is something the
1500 ;; compiler can't do at compile time, but which
1501 ;; turns out to make the #'<= expression
1502 ;; unreachable when END=NIL) could cause errors
1503 ;; when the compiler tries to constant-fold (<=
1506 ;; So, with or without bug 173, it'd be
1507 ;; unnecessarily evil to do a full
1508 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1509 ;; from COMPILE-FILE) for legal code, so we we
1510 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1511 #-sb-xc-host
#'compiler-style-warn
1512 ;; On the other hand, for code we control, we
1513 ;; should be able to work around any bug
1514 ;; 173-related problems, and in particular we
1515 ;; want to be alerted to calls to our own
1516 ;; functions which aren't being folded away; a
1517 ;; COMPILER-WARNING is butch enough to stop the
1518 ;; SBCL build itself in its tracks.
1519 #+sb-xc-host
#'compiler-warn
1522 (setf (combination-kind call
) :error
))
1523 ((and (proper-list-of-length-p values
1))
1524 (with-ir1-environment-from-node call
1525 (let* ((lvar (node-lvar call
))
1526 (prev (node-prev call
))
1527 (intermediate-ctran (make-ctran)))
1528 (%delete-lvar-use call
)
1529 (setf (ctran-next prev
) nil
)
1530 (setf (node-prev call
) nil
)
1531 (reference-constant prev intermediate-ctran lvar
1533 (link-node-to-previous-ctran call intermediate-ctran
)
1534 (reoptimize-lvar lvar
)
1535 (flush-combination call
))))
1536 (t (let ((dummies (make-gensym-list (length args
))))
1540 (declare (ignore ,@dummies
))
1541 (values ,@(mapcar (lambda (x) `',x
) values
)))
1545 ;;;; local call optimization
1547 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1549 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1550 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1553 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1554 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1555 (defun propagate-to-refs (leaf type
)
1556 (declare (type leaf leaf
) (type ctype type
))
1557 (let ((var-type (leaf-type leaf
))
1558 (refs (leaf-refs leaf
)))
1559 (unless (or (fun-type-p var-type
)
1561 (eq :declared
(leaf-where-from leaf
))
1562 (type-needs-conservation-p var-type
)))
1563 (let ((int (type-approx-intersection2 var-type type
)))
1564 (when (type/= int var-type
)
1565 (setf (leaf-type leaf
) int
)
1566 (let ((s-int (make-single-value-type int
)))
1568 (derive-node-type ref s-int
)
1569 ;; KLUDGE: LET var substitution
1570 (let* ((lvar (node-lvar ref
)))
1571 (when (and lvar
(combination-p (lvar-dest lvar
)))
1572 (reoptimize-lvar lvar
)))))))
1575 ;;; Iteration variable: exactly one SETQ of the form:
1577 ;;; (let ((var initial))
1579 ;;; (setq var (+ var step))
1581 (defun maybe-infer-iteration-var-type (var initial-type
)
1582 (binding* ((sets (lambda-var-sets var
) :exit-if-null
)
1584 (() (null (rest sets
)) :exit-if-null
)
1585 (set-use (principal-lvar-use (set-value set
)))
1586 (() (and (combination-p set-use
)
1587 (eq (combination-kind set-use
) :known
)
1588 (fun-info-p (combination-fun-info set-use
))
1589 (not (node-to-be-deleted-p set-use
))
1590 (or (eq (combination-fun-source-name set-use
) '+)
1591 (eq (combination-fun-source-name set-use
) '-
)))
1593 (minusp (eq (combination-fun-source-name set-use
) '-
))
1594 (+-args
(basic-combination-args set-use
))
1595 (() (and (proper-list-of-length-p +-args
2 2)
1596 (let ((first (principal-lvar-use
1599 (eq (ref-leaf first
) var
))))
1601 (step-type (lvar-type (second +-args
)))
1602 (set-type (lvar-type (set-value set
))))
1603 (when (and (numeric-type-p initial-type
)
1604 (numeric-type-p step-type
)
1605 (or (numeric-type-equal initial-type step-type
)
1606 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1607 ;; the initial and the step are of different types,
1608 ;; and the step is less contagious.
1609 (numeric-type-equal initial-type
1610 (numeric-contagion initial-type
1612 (labels ((leftmost (x y cmp cmp
=)
1613 (cond ((eq x nil
) nil
)
1616 (let ((x1 (first x
)))
1618 (let ((y1 (first y
)))
1619 (if (funcall cmp x1 y1
) x y
)))
1621 (if (funcall cmp x1 y
) x y
)))))
1623 (let ((y1 (first y
)))
1624 (if (funcall cmp
= x y1
) x y
)))
1625 (t (if (funcall cmp x y
) x y
))))
1626 (max* (x y
) (leftmost x y
#'> #'>=))
1627 (min* (x y
) (leftmost x y
#'< #'<=)))
1628 (multiple-value-bind (low high
)
1629 (let ((step-type-non-negative (csubtypep step-type
(specifier-type
1631 (step-type-non-positive (csubtypep step-type
(specifier-type
1633 (cond ((or (and step-type-non-negative
(not minusp
))
1634 (and step-type-non-positive minusp
))
1635 (values (numeric-type-low initial-type
)
1636 (when (and (numeric-type-p set-type
)
1637 (numeric-type-equal set-type initial-type
))
1638 (max* (numeric-type-high initial-type
)
1639 (numeric-type-high set-type
)))))
1640 ((or (and step-type-non-positive
(not minusp
))
1641 (and step-type-non-negative minusp
))
1642 (values (when (and (numeric-type-p set-type
)
1643 (numeric-type-equal set-type initial-type
))
1644 (min* (numeric-type-low initial-type
)
1645 (numeric-type-low set-type
)))
1646 (numeric-type-high initial-type
)))
1649 (modified-numeric-type initial-type
1652 :enumerable nil
))))))
1653 (deftransform + ((x y
) * * :result result
)
1654 "check for iteration variable reoptimization"
1655 (let ((dest (principal-lvar-end result
))
1656 (use (principal-lvar-use x
)))
1657 (when (and (ref-p use
)
1661 (reoptimize-lvar (set-value dest
))))
1662 (give-up-ir1-transform))
1664 ;;; Figure out the type of a LET variable that has sets. We compute
1665 ;;; the union of the INITIAL-TYPE and the types of all the set
1666 ;;; values and to a PROPAGATE-TO-REFS with this type.
1667 (defun propagate-from-sets (var initial-type
)
1668 (let ((changes (not (csubtypep (lambda-var-last-initial-type var
) initial-type
)))
1670 (dolist (set (lambda-var-sets var
))
1671 (let ((type (lvar-type (set-value set
))))
1673 (when (node-reoptimize set
)
1674 (let ((old-type (node-derived-type set
)))
1675 (unless (values-subtypep old-type type
)
1676 (derive-node-type set
(make-single-value-type type
))
1678 (setf (node-reoptimize set
) nil
))))
1680 (setf (lambda-var-last-initial-type var
) initial-type
)
1681 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type
)
1682 (apply #'type-union initial-type types
))))
1683 (propagate-to-refs var res-type
))))
1686 ;;; If a LET variable, find the initial value's type and do
1687 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1689 (defun ir1-optimize-set (node)
1690 (declare (type cset node
))
1691 (let ((var (set-var node
)))
1692 (when (and (lambda-var-p var
) (leaf-refs var
))
1693 (let ((home (lambda-var-home var
)))
1694 (when (eq (functional-kind home
) :let
)
1695 (let* ((initial-value (let-var-initial-value var
))
1696 (initial-type (lvar-type initial-value
)))
1697 (setf (lvar-reoptimize initial-value
) nil
)
1698 (propagate-from-sets var initial-type
))))))
1699 (derive-node-type node
(make-single-value-type
1700 (lvar-type (set-value node
))))
1701 (setf (node-reoptimize node
) nil
)
1704 ;;; Return true if the value of REF will always be the same (and is
1705 ;;; thus legal to substitute.)
1706 (defun constant-reference-p (ref)
1707 (declare (type ref ref
))
1708 (let ((leaf (ref-leaf ref
)))
1710 ((or constant functional
) t
)
1712 (null (lambda-var-sets leaf
)))
1714 (not (eq (defined-fun-inlinep leaf
) :notinline
)))
1716 (case (global-var-kind leaf
)
1718 (let ((name (leaf-source-name leaf
)))
1720 (eq (symbol-package (fun-name-block-name name
))
1722 (info :function
:info name
)))))))))
1724 ;;; If we have a non-set LET var with a single use, then (if possible)
1725 ;;; replace the variable reference's LVAR with the arg lvar.
1727 ;;; We change the REF to be a reference to NIL with unused value, and
1728 ;;; let it be flushed as dead code. A side effect of this substitution
1729 ;;; is to delete the variable.
1730 (defun substitute-single-use-lvar (arg var
)
1731 (declare (type lvar arg
) (type lambda-var var
))
1732 (binding* ((ref (first (leaf-refs var
)))
1733 (lvar (node-lvar ref
) :exit-if-null
)
1734 (dest (lvar-dest lvar
))
1735 (dest-lvar (when (valued-node-p dest
) (node-lvar dest
))))
1737 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1738 ;; LVAR-USEs should not be met on one path. Another problem
1739 ;; is with dynamic-extent.
1740 (eq (lvar-uses lvar
) ref
)
1741 (not (block-delete-p (node-block ref
)))
1742 ;; If the destinatation is dynamic extent, don't substitute unless
1743 ;; the source is as well.
1745 (not (lvar-dynamic-extent dest-lvar
))
1746 (lvar-dynamic-extent lvar
))
1748 ;; we should not change lifetime of unknown values lvars
1750 (and (type-single-value-p (lvar-derived-type arg
))
1751 (multiple-value-bind (pdest pprev
)
1752 (principal-lvar-end lvar
)
1753 (declare (ignore pdest
))
1754 (lvar-single-value-p pprev
))))
1756 (or (eq (basic-combination-fun dest
) lvar
)
1757 (and (eq (basic-combination-kind dest
) :local
)
1758 (type-single-value-p (lvar-derived-type arg
)))))
1760 ;; While CRETURN and EXIT nodes may be known-values,
1761 ;; they have their own complications, such as
1762 ;; substitution into CRETURN may create new tail calls.
1765 (aver (lvar-single-value-p lvar
))
1767 (eq (node-home-lambda ref
)
1768 (lambda-home (lambda-var-home var
))))
1769 (let ((ref-type (single-value-type (node-derived-type ref
))))
1770 (cond ((csubtypep (single-value-type (lvar-type arg
)) ref-type
)
1771 (substitute-lvar-uses lvar arg
1772 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1774 (delete-lvar-use ref
))
1776 (let* ((value (make-lvar))
1777 (cast (insert-cast-before ref value ref-type
1778 ;; KLUDGE: it should be (TYPE-CHECK 0)
1780 (setf (cast-type-to-check cast
) *wild-type
*)
1781 (substitute-lvar-uses value arg
1784 (%delete-lvar-use ref
)
1785 (add-lvar-use cast lvar
)))))
1786 (setf (node-derived-type ref
) *wild-type
*)
1787 (change-ref-leaf ref
(find-constant nil
))
1790 (reoptimize-lvar lvar
)
1793 ;;; Delete a LET, removing the call and bind nodes, and warning about
1794 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1795 ;;; along right away and delete the REF and then the lambda, since we
1796 ;;; flush the FUN lvar.
1797 (defun delete-let (clambda)
1798 (declare (type clambda clambda
))
1799 (aver (functional-letlike-p clambda
))
1800 (note-unreferenced-fun-vars clambda
)
1801 (let ((call (let-combination clambda
)))
1802 (flush-dest (basic-combination-fun call
))
1804 (unlink-node (lambda-bind clambda
))
1805 (setf (lambda-bind clambda
) nil
))
1806 (setf (functional-kind clambda
) :zombie
)
1807 (let ((home (lambda-home clambda
)))
1808 (setf (lambda-lets home
) (delete clambda
(lambda-lets home
))))
1811 ;;; This function is called when one of the arguments to a LET
1812 ;;; changes. We look at each changed argument. If the corresponding
1813 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1814 ;;; consider substituting for the variable, and also propagate
1815 ;;; derived-type information for the arg to all the VAR's refs.
1817 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1818 ;;; subtype of the argument's leaf type. This prevents type checking
1819 ;;; from being defeated, and also ensures that the best representation
1820 ;;; for the variable can be used.
1822 ;;; Substitution of individual references is inhibited if the
1823 ;;; reference is in a different component from the home. This can only
1824 ;;; happen with closures over top level lambda vars. In such cases,
1825 ;;; the references may have already been compiled, and thus can't be
1826 ;;; retroactively modified.
1828 ;;; If all of the variables are deleted (have no references) when we
1829 ;;; are done, then we delete the LET.
1831 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1833 (defun propagate-let-args (call fun
)
1834 (declare (type combination call
) (type clambda fun
))
1835 (loop for arg in
(combination-args call
)
1836 and var in
(lambda-vars fun
) do
1837 (when (and arg
(lvar-reoptimize arg
))
1838 (setf (lvar-reoptimize arg
) nil
)
1840 ((lambda-var-sets var
)
1841 (propagate-from-sets var
(lvar-type arg
)))
1842 ((let ((use (lvar-uses arg
)))
1844 (let ((leaf (ref-leaf use
)))
1845 (when (and (constant-reference-p use
)
1846 (csubtypep (leaf-type leaf
)
1847 ;; (NODE-DERIVED-TYPE USE) would
1848 ;; be better -- APD, 2003-05-15
1850 (propagate-to-refs var
(lvar-type arg
))
1851 (let ((use-component (node-component use
)))
1852 (prog1 (substitute-leaf-if
1854 (cond ((eq (node-component ref
) use-component
)
1857 (aver (lambda-toplevelish-p (lambda-home fun
)))
1861 ((and (null (rest (leaf-refs var
)))
1862 (not (preserve-single-use-debug-var-p call var
))
1863 (substitute-single-use-lvar arg var
)))
1865 (propagate-to-refs var
(lvar-type arg
))))))
1867 (when (every #'not
(combination-args call
))
1872 ;;; This function is called when one of the args to a non-LET local
1873 ;;; call changes. For each changed argument corresponding to an unset
1874 ;;; variable, we compute the union of the types across all calls and
1875 ;;; propagate this type information to the var's refs.
1877 ;;; If the function has an entry-fun, then we don't do anything: since
1878 ;;; it has a XEP we would not discover anything.
1880 ;;; If the function is an optional-entry-point, we will just make sure
1881 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1882 ;;; can erronously propagate too strict types into refs: see
1883 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1885 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1886 ;;; corresponding to changed arguments in CALL, since the only use in
1887 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1889 (defun propagate-local-call-args (call fun
)
1890 (declare (type combination call
) (type clambda fun
))
1891 (unless (functional-entry-fun fun
)
1892 (if (lambda-optional-dispatch fun
)
1893 ;; We can still make sure &REST is known to be a list.
1894 (loop for var in
(lambda-vars fun
)
1895 do
(let ((info (lambda-var-arg-info var
)))
1896 (when (and info
(eq :rest
(arg-info-kind info
)))
1897 (propagate-from-sets var
(specifier-type 'list
)))))
1899 (let* ((vars (lambda-vars fun
))
1900 (union (mapcar (lambda (arg var
)
1902 (lvar-reoptimize arg
)
1903 (null (basic-var-sets var
)))
1905 (basic-combination-args call
)
1907 (this-ref (lvar-use (basic-combination-fun call
))))
1909 (dolist (arg (basic-combination-args call
))
1911 (setf (lvar-reoptimize arg
) nil
)))
1913 (dolist (ref (leaf-refs fun
))
1914 (let ((dest (node-dest ref
)))
1915 (unless (or (eq ref this-ref
) (not dest
))
1917 (mapcar (lambda (this-arg old
)
1919 (setf (lvar-reoptimize this-arg
) nil
)
1920 (type-union (lvar-type this-arg
) old
)))
1921 (basic-combination-args dest
)
1924 (loop for var in vars
1926 when type do
(propagate-to-refs var type
)))))
1930 ;;;; multiple values optimization
1932 ;;; Do stuff to notice a change to a MV combination node. There are
1933 ;;; two main branches here:
1934 ;;; -- If the call is local, then it is already a MV let, or should
1935 ;;; become one. Note that although all :LOCAL MV calls must eventually
1936 ;;; be converted to :MV-LETs, there can be a window when the call
1937 ;;; is local, but has not been LET converted yet. This is because
1938 ;;; the entry-point lambdas may have stray references (in other
1939 ;;; entry points) that have not been deleted yet.
1940 ;;; -- The call is full. This case is somewhat similar to the non-MV
1941 ;;; combination optimization: we propagate return type information and
1942 ;;; notice non-returning calls. We also have an optimization
1943 ;;; which tries to convert MV-CALLs into MV-binds.
1944 (defun ir1-optimize-mv-combination (node)
1945 (let ((fun (basic-combination-fun node
)))
1946 (unless (and (node-p (lvar-uses fun
))
1947 (node-to-be-deleted-p (lvar-uses fun
)))
1948 (ecase (basic-combination-kind node
)
1950 (when (lvar-reoptimize fun
)
1951 (setf (lvar-reoptimize fun
) nil
)
1952 (maybe-let-convert (combination-lambda node
)))
1953 (setf (lvar-reoptimize (first (basic-combination-args node
))) nil
)
1954 (when (eq (functional-kind (combination-lambda node
)) :mv-let
)
1955 (unless (convert-mv-bind-to-let node
)
1956 (ir1-optimize-mv-bind node
))))
1958 (let* ((fun-changed (lvar-reoptimize fun
))
1959 (args (basic-combination-args node
)))
1961 (setf (lvar-reoptimize fun
) nil
)
1962 (let ((type (lvar-type fun
)))
1963 (when (fun-type-p type
)
1964 (derive-node-type node
(fun-type-returns type
))))
1965 (maybe-terminate-block node nil
)
1966 (let ((use (lvar-uses fun
)))
1967 (when (and (ref-p use
) (functional-p (ref-leaf use
)))
1968 (convert-call-if-possible use node
)
1969 (when (eq (basic-combination-kind node
) :local
)
1970 (maybe-let-convert (ref-leaf use
))))))
1971 (unless (or (eq (basic-combination-kind node
) :local
)
1972 (eq (lvar-fun-name fun
) '%throw
))
1973 (ir1-optimize-mv-call node
))
1975 (setf (lvar-reoptimize arg
) nil
))))
1979 ;;; Propagate derived type info from the values lvar to the vars.
1980 (defun ir1-optimize-mv-bind (node)
1981 (declare (type mv-combination node
))
1982 (let* ((arg (first (basic-combination-args node
)))
1983 (vars (lambda-vars (combination-lambda node
)))
1984 (n-vars (length vars
))
1985 (types (values-type-in (lvar-derived-type arg
)
1987 (loop for var in vars
1989 do
(if (basic-var-sets var
)
1990 (propagate-from-sets var type
)
1991 (propagate-to-refs var type
)))
1992 (setf (lvar-reoptimize arg
) nil
))
1995 ;;; If possible, convert a general MV call to an MV-BIND. We can do
1997 ;;; -- The call has only one argument, and
1998 ;;; -- The function has a known fixed number of arguments, or
1999 ;;; -- The argument yields a known fixed number of values.
2001 ;;; What we do is change the function in the MV-CALL to be a lambda
2002 ;;; that "looks like an MV bind", which allows
2003 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
2004 ;;; converted (the next time around.) This new lambda just calls the
2005 ;;; actual function with the MV-BIND variables as arguments. Note that
2006 ;;; this new MV bind is not let-converted immediately, as there are
2007 ;;; going to be stray references from the entry-point functions until
2008 ;;; they get deleted.
2010 ;;; In order to avoid loss of argument count checking, we only do the
2011 ;;; transformation according to a known number of expected argument if
2012 ;;; safety is unimportant. We can always convert if we know the number
2013 ;;; of actual values, since the normal call that we build will still
2014 ;;; do any appropriate argument count checking.
2016 ;;; We only attempt the transformation if the called function is a
2017 ;;; constant reference. This allows us to just splice the leaf into
2018 ;;; the new function, instead of trying to somehow bind the function
2019 ;;; expression. The leaf must be constant because we are evaluating it
2020 ;;; again in a different place. This also has the effect of squelching
2021 ;;; multiple warnings when there is an argument count error.
2022 (defun ir1-optimize-mv-call (node)
2023 (let ((fun (basic-combination-fun node
))
2024 (*compiler-error-context
* node
)
2025 (ref (lvar-uses (basic-combination-fun node
)))
2026 (args (basic-combination-args node
)))
2028 (unless (and (ref-p ref
) (constant-reference-p ref
)
2030 (return-from ir1-optimize-mv-call
))
2032 (multiple-value-bind (min max
)
2033 (fun-type-nargs (lvar-type fun
))
2035 (multiple-value-bind (types nvals
)
2036 (values-types (lvar-derived-type (first args
)))
2037 (declare (ignore types
))
2038 (if (eq nvals
:unknown
) nil nvals
))))
2041 (when (and min
(< total-nvals min
))
2043 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2046 (setf (basic-combination-kind node
) :error
)
2047 (return-from ir1-optimize-mv-call
))
2048 (when (and max
(> total-nvals max
))
2050 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2053 (setf (basic-combination-kind node
) :error
)
2054 (return-from ir1-optimize-mv-call
)))
2056 (let ((count (cond (total-nvals)
2057 ((and (policy node
(zerop verify-arg-count
))
2062 (with-ir1-environment-from-node node
2063 (let* ((dums (make-gensym-list count
))
2065 (leaf (ref-leaf ref
))
2066 (fun (ir1-convert-lambda
2067 `(lambda (&optional
,@dums
&rest
,ignore
)
2068 (declare (ignore ,ignore
))
2069 (%funcall
,leaf
,@dums
))
2070 :source-name
(leaf-%source-name leaf
)
2071 :debug-name
(leaf-%debug-name leaf
))))
2072 (change-ref-leaf ref fun
)
2073 (aver (eq (basic-combination-kind node
) :full
))
2074 (locall-analyze-component *current-component
*)
2075 (aver (eq (basic-combination-kind node
) :local
)))))))))
2079 ;;; (multiple-value-bind
2088 ;;; What we actually do is convert the VALUES combination into a
2089 ;;; normal LET combination calling the original :MV-LET lambda. If
2090 ;;; there are extra args to VALUES, discard the corresponding
2091 ;;; lvars. If there are insufficient args, insert references to NIL.
2092 (defun convert-mv-bind-to-let (call)
2093 (declare (type mv-combination call
))
2094 (let* ((arg (first (basic-combination-args call
)))
2095 (use (lvar-uses arg
)))
2096 (when (and (combination-p use
)
2097 (eq (lvar-fun-name (combination-fun use
))
2099 (let* ((fun (combination-lambda call
))
2100 (vars (lambda-vars fun
))
2101 (vals (combination-args use
))
2102 (nvars (length vars
))
2103 (nvals (length vals
)))
2104 (cond ((> nvals nvars
)
2105 (mapc #'flush-dest
(subseq vals nvars
))
2106 (setq vals
(subseq vals
0 nvars
)))
2108 (with-ir1-environment-from-node use
2109 (let ((node-prev (node-prev use
)))
2110 (setf (node-prev use
) nil
)
2111 (setf (ctran-next node-prev
) nil
)
2112 (collect ((res vals
))
2113 (loop for count below
(- nvars nvals
)
2114 for prev
= node-prev then ctran
2115 for ctran
= (make-ctran)
2116 and lvar
= (make-lvar use
)
2117 do
(reference-constant prev ctran lvar nil
)
2119 finally
(link-node-to-previous-ctran
2121 (setq vals
(res)))))))
2122 (setf (combination-args use
) vals
)
2123 (flush-dest (combination-fun use
))
2124 (let ((fun-lvar (basic-combination-fun call
)))
2125 (setf (lvar-dest fun-lvar
) use
)
2126 (setf (combination-fun use
) fun-lvar
)
2127 (flush-lvar-externally-checkable-type fun-lvar
))
2128 (setf (combination-kind use
) :local
)
2129 (setf (functional-kind fun
) :let
)
2130 (flush-dest (first (basic-combination-args call
)))
2133 (reoptimize-lvar (first vals
)))
2134 ;; Propagate derived types from the VALUES call to its args:
2135 ;; transforms can leave the VALUES call with a better type
2136 ;; than its args have, so make sure not to throw that away.
2137 (let ((types (values-type-types (node-derived-type use
))))
2140 (let ((type (pop types
)))
2141 (assert-lvar-type val type
'((type-check .
0)))))))
2142 ;; Propagate declared types of MV-BIND variables.
2143 (propagate-to-args use fun
)
2144 (reoptimize-call use
))
2148 ;;; (values-list (list x y z))
2153 ;;; In implementation, this is somewhat similar to
2154 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2155 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2156 ;;; (allowing the LIST to be flushed.)
2158 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2159 (defoptimizer (values-list optimizer
) ((list) node
)
2160 (let ((use (lvar-uses list
)))
2161 (when (and (combination-p use
)
2162 (eq (lvar-fun-name (combination-fun use
))
2165 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2166 (change-ref-leaf (lvar-uses (combination-fun node
))
2167 (find-free-fun 'values
"in a strange place"))
2168 (setf (combination-kind node
) :full
)
2169 (let ((args (combination-args use
)))
2171 (setf (lvar-dest arg
) node
)
2172 (flush-lvar-externally-checkable-type arg
))
2173 (setf (combination-args use
) nil
)
2175 (flush-combination use
)
2176 (setf (combination-args node
) args
))
2179 ;;; If VALUES appears in a non-MV context, then effectively convert it
2180 ;;; to a PROG1. This allows the computation of the additional values
2181 ;;; to become dead code.
2182 (deftransform values
((&rest vals
) * * :node node
)
2183 (unless (lvar-single-value-p (node-lvar node
))
2184 (give-up-ir1-transform))
2185 (setf (node-derived-type node
)
2186 (make-short-values-type (list (single-value-type
2187 (node-derived-type node
)))))
2188 (principal-lvar-single-valuify (node-lvar node
))
2190 (let ((dummies (make-gensym-list (length (cdr vals
)))))
2191 `(lambda (val ,@dummies
)
2192 (declare (ignore ,@dummies
))
2198 (defun delete-cast (cast)
2199 (declare (type cast cast
))
2200 (let ((value (cast-value cast
))
2201 (lvar (node-lvar cast
)))
2202 (delete-filter cast lvar value
)
2204 (reoptimize-lvar lvar
)
2205 (when (lvar-single-value-p lvar
)
2206 (note-single-valuified-lvar lvar
)))
2209 (defun may-delete-vestigial-exit (cast)
2210 (let ((exit-lexenv (cast-vestigial-exit-lexenv cast
)))
2212 ;; Vestigial exits are only introduced when eliminating a local
2213 ;; RETURN-FROM. We may delete them only when we can show that
2214 ;; there are no other code paths that use the entry LVAR that
2215 ;; are live from within the block that contained the deleted
2216 ;; EXIT (our predecessor block). The conservative version of
2217 ;; this is that there are no EXITs for any ENTRY introduced
2218 ;; between the LEXENV of the deleted EXIT and the LEXENV of the
2220 (let* ((entry-lexenv (cast-vestigial-exit-entry-lexenv cast
))
2221 (entry-blocks (lexenv-blocks entry-lexenv
))
2222 (entry-tags (lexenv-tags entry-lexenv
)))
2223 (do ((current-block (lexenv-blocks exit-lexenv
) (cdr current-block
)))
2224 ((eq current-block entry-blocks
))
2225 (when (entry-exits (cadar current-block
))
2226 (return-from may-delete-vestigial-exit nil
)))
2227 (do ((current-tag (lexenv-tags exit-lexenv
) (cdr current-tag
)))
2228 ((eq current-tag entry-tags
))
2229 (when (entry-exits (cadar current-tag
))
2230 (return-from may-delete-vestigial-exit nil
))))))
2233 (defun compile-time-type-error-context (context)
2234 #+sb-xc-host context
2235 #-sb-xc-host
(source-to-string context
))
2237 (defun ir1-optimize-cast (cast &optional do-not-optimize
)
2238 (declare (type cast cast
))
2239 (let ((value (cast-value cast
))
2240 (atype (cast-asserted-type cast
)))
2241 (unless (or do-not-optimize
2242 (not (may-delete-vestigial-exit cast
)))
2243 (let ((lvar (node-lvar cast
)))
2244 (when (values-subtypep (lvar-derived-type value
)
2245 (cast-asserted-type cast
))
2247 (return-from ir1-optimize-cast t
))
2249 (when (and (listp (lvar-uses value
))
2251 ;; Pathwise removing of CAST
2252 (let ((ctran (node-next cast
))
2253 (dest (lvar-dest lvar
))
2256 (do-uses (use value
)
2257 (when (and (values-subtypep (node-derived-type use
) atype
)
2258 (immediately-used-p value use
))
2260 (when ctran
(ensure-block-start ctran
))
2261 (setq next-block
(first (block-succ (node-block cast
))))
2262 (ensure-block-start (node-prev cast
))
2263 (reoptimize-lvar lvar
)
2264 (setf (lvar-%derived-type value
) nil
))
2265 (%delete-lvar-use use
)
2266 (add-lvar-use use lvar
)
2267 (unlink-blocks (node-block use
) (node-block cast
))
2268 (link-blocks (node-block use
) next-block
)
2269 (when (and (return-p dest
)
2270 (basic-combination-p use
)
2271 (eq (basic-combination-kind use
) :local
))
2273 (dolist (use (merges))
2274 (merge-tail-sets use
)))))))
2276 (let* ((value-type (lvar-derived-type value
))
2277 (int (values-type-intersection value-type atype
)))
2278 (derive-node-type cast int
)
2279 (when (eq int
*empty-type
*)
2280 (unless (eq value-type
*empty-type
*)
2282 ;; FIXME: Do it in one step.
2283 (let ((context (node-source-form cast
))
2284 (detail (lvar-all-sources (cast-value cast
))))
2287 (if (cast-single-value-p cast
)
2289 `(multiple-value-call #'list
'dummy
)))
2292 ;; FIXME: Derived type.
2293 `(%compile-time-type-error
'dummy
2294 ',(type-specifier atype
)
2295 ',(type-specifier value-type
)
2297 ',(compile-time-type-error-context context
))))
2298 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2299 ;; functions, so we declare the return type of
2300 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2302 (setq value
(cast-value cast
))
2303 (derive-node-type (lvar-uses value
) *empty-type
*)
2304 (maybe-terminate-block (lvar-uses value
) nil
)
2305 ;; FIXME: Is it necessary?
2306 (aver (null (block-pred (node-block cast
))))
2307 (delete-block-lazily (node-block cast
))
2308 (return-from ir1-optimize-cast
)))
2309 (when (eq (node-derived-type cast
) *empty-type
*)
2310 (maybe-terminate-block cast nil
))
2312 (when (and (cast-%type-check cast
)
2313 (values-subtypep value-type
2314 (cast-type-to-check cast
)))
2315 (setf (cast-%type-check cast
) nil
))))
2317 (unless do-not-optimize
2318 (setf (node-reoptimize cast
) nil
)))
2320 (deftransform make-symbol
((string) (simple-string))
2321 `(%make-symbol string
))