Fix VOPs on EQL-typed arguments and closures.
[sbcl.git] / src / compiler / ir1opt.lisp
blobf9e5c08eabb21ac268f91a47159d4b337442bc45
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
6 ;;;; well.
8 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; more information.
10 ;;;;
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.
17 (in-package "SB!C")
19 ;;;; interface for obtaining results of constant folding
21 ;;; Return true for an LVAR whose sole use is a reference to a
22 ;;; constant leaf.
23 (defun constant-lvar-p (thing)
24 (declare (type (or lvar null) thing))
25 (and (lvar-p 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 for EQL types
32 (defun strictly-constant-lvar-p (thing)
33 (declare (type (or lvar null) thing))
34 (and (lvar-p 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
39 ;;; node.
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))
44 leaf)
45 (if (and (ref-p use)
46 (constant-p (setf leaf (ref-leaf use))))
47 (constant-value leaf)
48 (multiple-value-bind (constantp value) (type-singleton-p type)
49 (unless constantp
50 (error "~S used on non-constant LVAR ~S" 'lvar-value lvar))
51 value))))
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.
58 ;;;
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*)
68 ((listp uses)
69 (do ((res (,accessor (first uses))
70 (values-type-union (,accessor (first current))
71 res))
72 (current (rest uses) (rest current)))
73 ((or (null current) (eq res *wild-type*))
74 res)))
76 (,accessor uses))))))
78 #!-sb-fluid (declaim (inline lvar-derived-type))
79 (defun lvar-derived-type (lvar)
80 (declare (type lvar lvar))
81 (or (lvar-%derived-type lvar)
82 (setf (lvar-%derived-type lvar)
83 (%lvar-derived-type lvar))))
84 (defun %lvar-derived-type (lvar)
85 (lvar-type-using lvar node-derived-type))
87 ;;; Return the derived type for LVAR's first value. This is guaranteed
88 ;;; not to be a VALUES or FUNCTION type.
89 (declaim (ftype (sfunction (lvar) ctype) lvar-type))
90 (defun lvar-type (lvar)
91 (single-value-type (lvar-derived-type lvar)))
93 ;;; LVAR-CONSERVATIVE-TYPE
94 ;;;
95 ;;; Certain types refer to the contents of an object, which can
96 ;;; change without type derivation noticing: CONS types and ARRAY
97 ;;; types suffer from this:
98 ;;;
99 ;;; (let ((x (the (cons fixnum fixnum) (cons a b))))
100 ;;; (setf (car x) c)
101 ;;; (+ (car x) (cdr x)))
103 ;;; Python doesn't realize that the SETF CAR can change the type of X -- so we
104 ;;; cannot use LVAR-TYPE which gets the derived results. Worse, still, instead
105 ;;; of (SETF CAR) we might have a call to a user-defined function FOO which
106 ;;; does the same -- so there is no way to use the derived information in
107 ;;; general.
109 ;;; So, the conservative option is to use the derived type if the leaf has
110 ;;; only a single ref -- in which case there cannot be a prior call that
111 ;;; mutates it. Otherwise we use the declared type or punt to the most general
112 ;;; type we know to be correct for sure.
113 (defun lvar-conservative-type (lvar)
114 (let ((derived-type (lvar-type lvar))
115 (t-type *universal-type*))
116 ;; Recompute using NODE-CONSERVATIVE-TYPE instead of derived type if
117 ;; necessary -- picking off some easy cases up front.
118 (cond ((or (eq derived-type t-type)
119 ;; Can't use CSUBTYPEP!
120 (type= derived-type (specifier-type 'list))
121 (type= derived-type (specifier-type 'null)))
122 derived-type)
123 ((and (cons-type-p derived-type)
124 (eq t-type (cons-type-car-type derived-type))
125 (eq t-type (cons-type-cdr-type derived-type)))
126 derived-type)
127 ((and (array-type-p derived-type)
128 (or (not (array-type-complexp derived-type))
129 (let ((dimensions (array-type-dimensions derived-type)))
130 (or (eq '* dimensions)
131 (every (lambda (dim) (eq '* dim)) dimensions)))))
132 derived-type)
133 ((type-needs-conservation-p derived-type)
134 (single-value-type (lvar-type-using lvar node-conservative-type)))
136 derived-type))))
138 (defun node-conservative-type (node)
139 (let* ((derived-values-type (node-derived-type node))
140 (derived-type (single-value-type derived-values-type)))
141 (if (ref-p node)
142 (let ((leaf (ref-leaf node)))
143 (if (and (basic-var-p leaf)
144 (cdr (leaf-refs leaf)))
145 (coerce-to-values
146 (if (eq :declared (leaf-where-from leaf))
147 (leaf-type leaf)
148 (conservative-type derived-type)))
149 derived-values-type))
150 derived-values-type)))
152 (defun conservative-type (type)
153 (cond ((or (eq type *universal-type*)
154 (eq type (specifier-type 'list))
155 (eq type (specifier-type 'null)))
156 type)
157 ((cons-type-p type)
158 (specifier-type 'cons))
159 ((array-type-p type)
160 (if (array-type-complexp type)
161 (make-array-type
162 ;; ADJUST-ARRAY may change dimensions, but rank stays same.
163 (let ((old (array-type-dimensions type)))
164 (if (eq '* old)
166 (mapcar (constantly '*) old)))
167 ;; Complexity cannot change.
168 :complexp (array-type-complexp type)
169 ;; Element type cannot change.
170 :element-type (array-type-element-type type)
171 :specialized-element-type (array-type-specialized-element-type type))
172 ;; Simple arrays cannot change at all.
173 type))
174 ((union-type-p type)
175 ;; Conservative union type is an union of conservative types.
176 (let ((res *empty-type*))
177 (dolist (part (union-type-types type) res)
178 (setf res (type-union res (conservative-type part))))))
180 ;; Catch-all.
182 ;; If the type contains some CONS types, the conservative type contains all
183 ;; of them.
184 (when (types-equal-or-intersect type (specifier-type 'cons))
185 (setf type (type-union type (specifier-type 'cons))))
186 ;; Similarly for non-simple arrays -- it should be possible to preserve
187 ;; more information here, but really...
188 (let ((non-simple-arrays (specifier-type '(and array (not simple-array)))))
189 (when (types-equal-or-intersect type non-simple-arrays)
190 (setf type (type-union type non-simple-arrays))))
191 type)))
193 (defun type-needs-conservation-p (type)
194 (cond ((eq type *universal-type*)
195 ;; Excluding T is necessary, because we do want type derivation to
196 ;; be able to narrow it down in case someone (most like a macro-expansion...)
197 ;; actually declares something as having type T.
198 nil)
199 ((or (cons-type-p type) (and (array-type-p type) (array-type-complexp type)))
200 ;; Covered by the next case as well, but this is a quick test.
202 ((types-equal-or-intersect type (specifier-type '(or cons (and array (not simple-array)))))
203 t)))
205 ;;; If LVAR is an argument of a function, return a type which the
206 ;;; function checks LVAR for.
207 #!-sb-fluid (declaim (inline lvar-externally-checkable-type))
208 (defun lvar-externally-checkable-type (lvar)
209 (or (lvar-%externally-checkable-type lvar)
210 (%lvar-%externally-checkable-type lvar)))
211 (defun %lvar-%externally-checkable-type (lvar)
212 (declare (type lvar lvar))
213 (let ((dest (lvar-dest lvar)))
214 (if (not (and dest (combination-p dest)))
215 ;; TODO: MV-COMBINATION
216 (setf (lvar-%externally-checkable-type lvar) *wild-type*)
217 (let* ((fun (combination-fun dest))
218 (args (combination-args dest))
219 (fun-type (lvar-type fun)))
220 (setf (lvar-%externally-checkable-type fun) *wild-type*)
221 (if (or (not (call-full-like-p dest))
222 (not (fun-type-p fun-type))
223 ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
224 (fun-type-wild-args fun-type))
225 (dolist (arg args)
226 (when arg
227 (setf (lvar-%externally-checkable-type arg)
228 *wild-type*)))
229 (map-combination-args-and-types
230 (lambda (arg type)
231 (setf (lvar-%externally-checkable-type arg)
232 (acond ((lvar-%externally-checkable-type arg)
233 (values-type-intersection
234 it (coerce-to-values type)))
235 (t (coerce-to-values type)))))
236 dest)))))
237 (or (lvar-%externally-checkable-type lvar) *wild-type*))
238 #!-sb-fluid(declaim (inline flush-lvar-externally-checkable-type))
239 (defun flush-lvar-externally-checkable-type (lvar)
240 (declare (type lvar lvar))
241 (setf (lvar-%externally-checkable-type lvar) nil))
243 ;;;; interface routines used by optimizers
245 (declaim (inline reoptimize-component))
246 (defun reoptimize-component (component kind)
247 (declare (type component component)
248 (type (member nil :maybe t) kind))
249 (aver kind)
250 (unless (eq (component-reoptimize component) t)
251 (setf (component-reoptimize component) kind)))
253 ;;; This function is called by optimizers to indicate that something
254 ;;; interesting has happened to the value of LVAR. Optimizers must
255 ;;; make sure that they don't call for reoptimization when nothing has
256 ;;; happened, since optimization will fail to terminate.
258 ;;; We clear any cached type for the lvar and set the reoptimize flags
259 ;;; on everything in sight.
260 (defun reoptimize-lvar (lvar)
261 (declare (type (or lvar null) lvar))
262 (when lvar
263 (setf (lvar-%derived-type lvar) nil)
264 (let ((dest (lvar-dest lvar)))
265 (when dest
266 (setf (lvar-reoptimize lvar) t)
267 (setf (node-reoptimize dest) t)
268 (binding* (;; Since this may be called during IR1 conversion,
269 ;; PREV may be missing.
270 (prev (node-prev dest) :exit-if-null)
271 (block (ctran-block prev))
272 (component (block-component block)))
273 (when (typep dest 'cif)
274 (setf (block-test-modified block) t))
275 (setf (block-reoptimize block) t)
276 (reoptimize-component component :maybe))))
277 (do-uses (node lvar)
278 (setf (block-type-check (node-block node)) t)))
279 (values))
281 (defun reoptimize-lvar-uses (lvar)
282 (declare (type lvar lvar))
283 (do-uses (use lvar)
284 (setf (node-reoptimize use) t)
285 (setf (block-reoptimize (node-block use)) t)
286 (reoptimize-component (node-component use) :maybe)))
288 ;;; Annotate NODE to indicate that its result has been proven to be
289 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
290 ;;; only correct way to supply information discovered about a node's
291 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
292 ;;; information may be lost and reoptimization may not happen.
294 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
295 ;;; intersection is different from the old type, then we do a
296 ;;; REOPTIMIZE-LVAR on the NODE-LVAR.
297 (defun derive-node-type (node rtype &key from-scratch)
298 (declare (type valued-node node) (type ctype rtype))
299 (let* ((initial-type (node-derived-type node))
300 (node-type (if from-scratch
301 *wild-type*
302 initial-type)))
303 (unless (eq initial-type rtype)
304 (let ((int (values-type-intersection node-type rtype))
305 (lvar (node-lvar node)))
306 (when (type/= initial-type int)
307 (when (and *check-consistency*
308 (eq int *empty-type*)
309 (not (eq rtype *empty-type*)))
310 (aver (not from-scratch))
311 (let ((*compiler-error-context* node))
312 (compiler-warn
313 "New inferred type ~S conflicts with old type:~
314 ~% ~S~%*** possible internal error? Please report this."
315 (type-specifier rtype) (type-specifier node-type))))
316 (setf (node-derived-type node) int)
317 ;; If the new type consists of only one object, replace the
318 ;; node with a constant reference.
319 (when (and (ref-p node)
320 (lambda-var-p (ref-leaf node)))
321 (let ((type (single-value-type int)))
322 (when (and (member-type-p type)
323 (eql 1 (member-type-size type)))
324 (change-ref-leaf node (find-constant
325 (first (member-type-members type)))))))
326 (reoptimize-lvar lvar)))))
327 (values))
329 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
330 ;;; error for LVAR's value not to be TYPEP to TYPE. We implement it
331 ;;; splitting off DEST a new CAST node; old LVAR will deliver values
332 ;;; to CAST. If we improve the assertion, we set TYPE-CHECK and
333 ;;; TYPE-ASSERTED to guarantee that the new assertion will be checked.
334 (defun assert-lvar-type (lvar type policy)
335 (declare (type lvar lvar) (type ctype type))
336 (unless (values-subtypep (lvar-derived-type lvar) type)
337 (let ((internal-lvar (make-lvar))
338 (dest (lvar-dest lvar)))
339 (substitute-lvar internal-lvar lvar)
340 (let ((cast (insert-cast-before dest lvar type policy)))
341 (use-lvar cast internal-lvar)
342 t))))
345 ;;;; IR1-OPTIMIZE
347 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
348 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
349 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
350 ;;; we are done, then another iteration would be beneficial.
351 (defun ir1-optimize (component fastp)
352 (declare (type component component))
353 (setf (component-reoptimize component) nil)
354 (loop with block = (block-next (component-head component))
355 with tail = (component-tail component)
356 for last-block = block
357 until (eq block tail)
358 do (cond
359 ;; We delete blocks when there is either no predecessor or the
360 ;; block is in a lambda that has been deleted. These blocks
361 ;; would eventually be deleted by DFO recomputation, but doing
362 ;; it here immediately makes the effect available to IR1
363 ;; optimization.
364 ((or (block-delete-p block)
365 (null (block-pred block)))
366 (delete-block-lazily block)
367 (setq block (clean-component component block)))
368 ((eq (functional-kind (block-home-lambda block)) :deleted)
369 ;; Preserve the BLOCK-SUCC invariant that almost every block has
370 ;; one successor (and a block with DELETE-P set is an acceptable
371 ;; exception).
372 (mark-for-deletion block)
373 (setq block (clean-component component block)))
375 (loop
376 (let ((succ (block-succ block)))
377 (unless (singleton-p succ)
378 (return)))
380 (let ((last (block-last block)))
381 (typecase last
382 (cif
383 (flush-dest (if-test last))
384 (when (unlink-node last)
385 (return)))
386 (exit
387 (when (maybe-delete-exit last)
388 (return)))))
390 (unless (join-successor-if-possible block)
391 (return)))
393 (when (and (not fastp) (block-reoptimize block) (block-component block))
394 (aver (not (block-delete-p block)))
395 (ir1-optimize-block block))
397 (cond ((and (block-delete-p block) (block-component block))
398 (setq block (clean-component component block)))
399 ((and (block-flush-p block) (block-component block))
400 (flush-dead-code block)))))
401 do (when (eq block last-block)
402 (setq block (block-next block))))
404 (values))
406 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
407 ;;; flags.
409 ;;; Note that although they are cleared here, REOPTIMIZE flags might
410 ;;; still be set upon return from this function, meaning that further
411 ;;; optimization is wanted (as a consequence of optimizations we did).
412 (defun ir1-optimize-block (block)
413 (declare (type cblock block))
414 ;; We clear the node and block REOPTIMIZE flags before doing the
415 ;; optimization, not after. This ensures that the node or block will
416 ;; be reoptimized if necessary.
417 (setf (block-reoptimize block) nil)
418 (do-nodes (node nil block :restart-p t)
419 (when (node-reoptimize node)
420 ;; As above, we clear the node REOPTIMIZE flag before optimizing.
421 (setf (node-reoptimize node) nil)
422 (typecase node
423 (ref)
424 (combination
425 ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
426 ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
427 ;; any argument changes.
428 (ir1-optimize-combination node))
429 (cif
430 (ir1-optimize-if node))
431 (creturn
432 ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
433 ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
434 ;; clear the flag itself. -- WHN 2002-02-02, quoting original
435 ;; CMU CL comments
436 (setf (node-reoptimize node) t)
437 (ir1-optimize-return node))
438 (mv-combination
439 (ir1-optimize-mv-combination node))
440 (exit
441 ;; With an EXIT, we derive the node's type from the VALUE's
442 ;; type.
443 (let ((value (exit-value node)))
444 (when value
445 (derive-node-type node (lvar-derived-type value)))))
446 (cset
447 ;; PROPAGATE-FROM-SETS can do a better job if NODE-REOPTIMIZE
448 ;; is accurate till the node actually has been reoptimized.
449 (setf (node-reoptimize node) t)
450 (ir1-optimize-set node))
451 (cast
452 (ir1-optimize-cast node)))))
454 (values))
456 ;;; Try to join with a successor block. If we succeed, we return true,
457 ;;; otherwise false.
458 (defun join-successor-if-possible (block)
459 (declare (type cblock block))
460 (let ((next (first (block-succ block))))
461 (when (block-start next) ; NEXT is not an END-OF-COMPONENT marker
462 (cond ( ;; We cannot combine with a successor block if:
464 ;; the successor has more than one predecessor;
465 (rest (block-pred next))
466 ;; the successor is the current block (infinite loop);
467 (eq next block)
468 ;; the next block has a different cleanup, and thus
469 ;; we may want to insert cleanup code between the
470 ;; two blocks at some point;
471 (not (eq (block-end-cleanup block)
472 (block-start-cleanup next)))
473 ;; the next block has a different home lambda, and
474 ;; thus the control transfer is a non-local exit.
475 (not (eq (block-home-lambda block)
476 (block-home-lambda next)))
477 ;; Stack analysis phase wants ENTRY to start a block...
478 (entry-p (block-start-node next))
479 (let ((last (block-last block)))
480 (and (valued-node-p last)
481 (awhen (node-lvar last)
483 ;; ... and a DX-allocator to end a block.
484 (lvar-dynamic-extent it)
485 ;; FIXME: This is a partial workaround for bug 303.
486 (consp (lvar-uses it)))))))
487 nil)
489 (join-blocks block next)
490 t)))))
492 ;;; Join together two blocks. The code in BLOCK2 is moved into BLOCK1
493 ;;; and BLOCK2 is deleted from the DFO. We combine the optimize flags
494 ;;; for the two blocks so that any indicated optimization gets done.
495 (defun join-blocks (block1 block2)
496 (declare (type cblock block1 block2))
497 (let* ((last1 (block-last block1))
498 (last2 (block-last block2))
499 (succ (block-succ block2))
500 (start2 (block-start block2)))
501 (do ((ctran start2 (node-next (ctran-next ctran))))
502 ((not ctran))
503 (setf (ctran-block ctran) block1))
505 (unlink-blocks block1 block2)
506 (dolist (block succ)
507 (unlink-blocks block2 block)
508 (link-blocks block1 block))
510 (setf (ctran-kind start2) :inside-block)
511 (setf (node-next last1) start2)
512 (setf (ctran-use start2) last1)
513 (setf (block-last block1) last2))
515 (setf (block-flags block1)
516 (attributes-union (block-flags block1)
517 (block-flags block2)
518 (block-attributes type-asserted test-modified)))
520 (let ((next (block-next block2))
521 (prev (block-prev block2)))
522 (setf (block-next prev) next)
523 (setf (block-prev next) prev))
525 (values))
527 ;;; Delete any nodes in BLOCK whose value is unused and which have no
528 ;;; side effects. We can delete sets of lexical variables when the set
529 ;;; variable has no references.
530 (defun flush-dead-code (block &aux victim)
531 (declare (type cblock block))
532 (setf (block-flush-p block) nil)
533 (do-nodes-backwards (node lvar block :restart-p t)
534 (unless lvar
535 (typecase node
536 (ref
537 (setf victim node)
538 (delete-ref node)
539 (unlink-node node))
540 (combination
541 (when (flushable-combination-p node)
542 (setf victim node)
543 (flush-combination node)))
544 (mv-combination
545 (when (eq (basic-combination-kind node) :local)
546 (let ((fun (combination-lambda node)))
547 (when (dolist (var (lambda-vars fun) t)
548 (when (or (leaf-refs var)
549 (lambda-var-sets var))
550 (return nil)))
551 (setf victim node)
552 (flush-dest (first (basic-combination-args node)))
553 (delete-let fun)))))
554 (exit
555 (let ((value (exit-value node)))
556 (when value
557 (setf victim node)
558 (flush-dest value)
559 (setf (exit-value node) nil))))
560 (cset
561 (let ((var (set-var node)))
562 (when (and (lambda-var-p var)
563 (null (leaf-refs var)))
564 (setf victim node)
565 (flush-dest (set-value node))
566 (setf (basic-var-sets var)
567 (delq node (basic-var-sets var)))
568 (unlink-node node))))
569 (cast
570 (unless (cast-type-check node)
571 (setf victim node)
572 (flush-dest (cast-value node))
573 (unlink-node node))))))
575 victim)
577 ;;;; local call return type propagation
579 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
580 ;;; flag set. It iterates over the uses of the RESULT, looking for
581 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
582 ;;; call, then we union its type together with the types of other such
583 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
584 ;;; type with the RESULT's asserted type. We can make this
585 ;;; intersection now (potentially before type checking) because this
586 ;;; assertion on the result will eventually be checked (if
587 ;;; appropriate.)
589 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
590 ;;; combination, which may change the successor of the call to be the
591 ;;; called function, and if so, checks if the call can become an
592 ;;; assignment. If we convert to an assignment, we abort, since the
593 ;;; RETURN has been deleted.
594 (defun find-result-type (node)
595 (declare (type creturn node))
596 (let ((result (return-result node)))
597 (collect ((use-union *empty-type* values-type-union))
598 (do-uses (use result)
599 (let ((use-home (node-home-lambda use)))
600 (cond ((or (eq (functional-kind use-home) :deleted)
601 (block-delete-p (node-block use))))
602 ((and (basic-combination-p use)
603 (eq (basic-combination-kind use) :local))
604 (aver (eq (lambda-tail-set use-home)
605 (lambda-tail-set (combination-lambda use))))
606 (when (combination-p use)
607 (when (nth-value 1 (maybe-convert-tail-local-call use))
608 (return-from find-result-type t))))
610 (use-union (node-derived-type use))))))
611 (let ((int
612 ;; (values-type-intersection
613 ;; (continuation-asserted-type result) ; FIXME -- APD, 2002-01-26
614 (use-union)
615 ;; )
617 (setf (return-result-type node) int))))
618 nil)
620 ;;; Do stuff to realize that something has changed about the value
621 ;;; delivered to a return node. Since we consider the return values of
622 ;;; all functions in the tail set to be equivalent, this amounts to
623 ;;; bringing the entire tail set up to date. We iterate over the
624 ;;; returns for all the functions in the tail set, reanalyzing them
625 ;;; all (not treating NODE specially.)
627 ;;; When we are done, we check whether the new type is different from
628 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
629 ;;; all the lvars for references to functions in the tail set. This
630 ;;; will cause IR1-OPTIMIZE-COMBINATION to derive the new type as the
631 ;;; results of the calls.
632 (defun ir1-optimize-return (node)
633 (declare (type creturn node))
634 (tagbody
635 :restart
636 (let* ((tails (lambda-tail-set (return-lambda node)))
637 (funs (tail-set-funs tails)))
638 (collect ((res *empty-type* values-type-union))
639 (dolist (fun funs)
640 (let ((return (lambda-return fun)))
641 (when return
642 (when (node-reoptimize return)
643 (setf (node-reoptimize return) nil)
644 (when (find-result-type return)
645 (go :restart)))
646 (res (return-result-type return)))))
648 (when (type/= (res) (tail-set-type tails))
649 (setf (tail-set-type tails) (res))
650 (dolist (fun (tail-set-funs tails))
651 (dolist (ref (leaf-refs fun))
652 (reoptimize-lvar (node-lvar ref))))))))
654 (values))
656 ;;;; IF optimization
658 ;;; Utility: return T if both argument cblocks are equivalent. For now,
659 ;;; detect only blocks that read the same leaf into the same lvar, and
660 ;;; continue to the same block.
661 (defun cblocks-equivalent-p (x y)
662 (declare (type cblock x y))
663 (and (ref-p (block-start-node x))
664 (eq (block-last x) (block-start-node x))
666 (ref-p (block-start-node y))
667 (eq (block-last y) (block-start-node y))
669 (equal (block-succ x) (block-succ y))
670 (eql (ref-lvar (block-start-node x)) (ref-lvar (block-start-node y)))
671 (eql (ref-leaf (block-start-node x)) (ref-leaf (block-start-node y)))))
673 ;;; Check whether the predicate is known to be true or false,
674 ;;; deleting the IF node in favor of the appropriate branch when this
675 ;;; is the case.
676 ;;; Similarly, when both branches are equivalent, branch directly to either
677 ;;; of them.
678 ;;; Also, if the test has multiple uses, replicate the node when possible...
679 ;;; in fact, splice in direct jumps to the right branch if possible.
680 (defun ir1-optimize-if (node)
681 (declare (type cif node))
682 (let ((test (if-test node))
683 (block (node-block node)))
684 (let* ((type (lvar-type test))
685 (consequent (if-consequent node))
686 (alternative (if-alternative node))
687 (victim
688 (cond ((constant-lvar-p test)
689 (if (lvar-value test) alternative consequent))
690 ((not (types-equal-or-intersect type (specifier-type 'null)))
691 alternative)
692 ((type= type (specifier-type 'null))
693 consequent)
694 ((or (eq consequent alternative) ; Can this happen?
695 (cblocks-equivalent-p alternative consequent))
696 alternative))))
697 (when victim
698 (kill-if-branch-1 node test block victim)
699 (return-from ir1-optimize-if (values))))
700 (tension-if-if-1 node test block)
701 (duplicate-if-if-1 node test block)
702 (values)))
704 ;; When we know that we only have a single successor, kill the victim
705 ;; ... unless the victim and the remaining successor are the same.
706 (defun kill-if-branch-1 (node test block victim)
707 (declare (type cif node))
708 (flush-dest test)
709 (when (rest (block-succ block))
710 (unlink-blocks block victim))
711 (setf (component-reanalyze (node-component node)) t)
712 (unlink-node node))
714 ;; When if/if conversion would leave (if ... (if nil ...)) or
715 ;; (if ... (if not-nil ...)), splice the correct successor right
716 ;; in.
717 (defun tension-if-if-1 (node test block)
718 (when (and (eq (block-start-node block) node)
719 (listp (lvar-uses test)))
720 (do-uses (use test)
721 (when (immediately-used-p test use)
722 (let* ((type (single-value-type (node-derived-type use)))
723 (target (if (type= type (specifier-type 'null))
724 (if-alternative node)
725 (multiple-value-bind (typep surep)
726 (ctypep nil type)
727 (and (not typep) surep
728 (if-consequent node))))))
729 (when target
730 (let ((pred (node-block use)))
731 (cond ((listp (lvar-uses test))
732 (change-block-successor pred block target)
733 (delete-lvar-use use))
735 ;; only one use left. Just kill the now-useless
736 ;; branch to avoid spurious code deletion notes.
737 (aver (rest (block-succ block)))
738 (kill-if-branch-1
739 node test block
740 (if (eql target (if-alternative node))
741 (if-consequent node)
742 (if-alternative node)))
743 (return-from tension-if-if-1))))))))))
745 ;; Finally, duplicate EQ-nil tests
746 (defun duplicate-if-if-1 (node test block)
747 (when (and (eq (block-start-node block) node)
748 (listp (lvar-uses test)))
749 (do-uses (use test)
750 (when (immediately-used-p test use)
751 (convert-if-if use node)
752 ;; leave the last use as is, instead of replacing
753 ;; the (singly-referenced) CIF node with a duplicate.
754 (when (not (listp (lvar-uses test))) (return))))))
756 ;;; Create a new copy of an IF node that tests the value of the node
757 ;;; USE. The test must have >1 use, and must be immediately used by
758 ;;; USE. NODE must be the only node in its block (implying that
759 ;;; block-start = if-test).
761 ;;; This optimization has an effect semantically similar to the
762 ;;; source-to-source transformation:
763 ;;; (IF (IF A B C) D E) ==>
764 ;;; (IF A (IF B D E) (IF C D E))
766 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
767 ;;; node so that dead code deletion notes will definitely not consider
768 ;;; either node to be part of the original source. One node might
769 ;;; become unreachable, resulting in a spurious note.
770 (defun convert-if-if (use node)
771 (declare (type node use) (type cif node))
772 (with-ir1-environment-from-node node
773 (let* ((block (node-block node))
774 (test (if-test node))
775 (cblock (if-consequent node))
776 (ablock (if-alternative node))
777 (use-block (node-block use))
778 (new-ctran (make-ctran))
779 (new-lvar (make-lvar))
780 (new-node (make-if :test new-lvar
781 :consequent cblock
782 :alternative ablock))
783 (new-block (ctran-starts-block new-ctran)))
784 (link-node-to-previous-ctran new-node new-ctran)
785 (setf (lvar-dest new-lvar) new-node)
786 (setf (block-last new-block) new-node)
788 (unlink-blocks use-block block)
789 (%delete-lvar-use use)
790 (add-lvar-use use new-lvar)
791 (link-blocks use-block new-block)
793 (link-blocks new-block cblock)
794 (link-blocks new-block ablock)
796 (push "<IF Duplication>" (node-source-path node))
797 (push "<IF Duplication>" (node-source-path new-node))
799 (reoptimize-lvar test)
800 (reoptimize-lvar new-lvar)
801 (setf (component-reanalyze *current-component*) t)))
802 (values))
804 ;;;; exit IR1 optimization
806 ;;; This function attempts to delete an exit node, returning true if
807 ;;; it deletes the block as a consequence:
808 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
809 ;;; anything, since there is nothing to be done.
810 ;;; -- If the exit node and its ENTRY have the same home lambda then
811 ;;; we know the exit is local, and can delete the exit. We change
812 ;;; uses of the Exit-Value to be uses of the original lvar,
813 ;;; then unlink the node. If the exit is to a TR context, then we
814 ;;; must do MERGE-TAIL-SETS on any local calls which delivered
815 ;;; their value to this exit.
816 ;;; -- If there is no value (as in a GO), then we skip the value
817 ;;; semantics.
819 ;;; This function is also called by environment analysis, since it
820 ;;; wants all exits to be optimized even if normal optimization was
821 ;;; omitted.
822 (defun maybe-delete-exit (node)
823 (declare (type exit node))
824 (let ((value (exit-value node))
825 (entry (exit-entry node)))
826 (when (and entry
827 (eq (node-home-lambda node) (node-home-lambda entry)))
828 (setf (entry-exits entry) (delq node (entry-exits entry)))
829 (if value
830 (with-ir1-environment-from-node entry
831 ;; We can't simply use DELETE-FILTER to unlink the node
832 ;; and substitute some LVAR magic, as this can confuse the
833 ;; stack analysis if there's another EXIT to the same
834 ;; continuation. Instead, we fabricate a new block (in
835 ;; the same lexenv as the ENTRY, so it can't be merged
836 ;; backwards), insert a gimmicked CAST node to link up the
837 ;; LVAR holding the value being returned to the LVAR which
838 ;; is expecting to accept the value, thus placing the
839 ;; return value where it needs to be while still providing
840 ;; the hook required for stack analysis.
841 ;; -- AJB, 2014-Mar-03
842 (let* ((exit-block (node-block node))
843 (new-ctran (make-ctran))
844 (new-block (ctran-starts-block new-ctran))
845 (cast-node
846 (%make-cast
847 :asserted-type *wild-type*
848 :type-to-check *wild-type*
849 :value value
850 :vestigial-exit-lexenv (node-lexenv node)
851 :vestigial-exit-entry-lexenv (node-lexenv entry)
852 :%type-check nil)))
853 ;; We only expect a single successor to EXIT-BLOCK,
854 ;; because it contains an EXIT node (which must end its
855 ;; block) and the only blocks that have more than once
856 ;; successor are those with IF nodes (which also must
857 ;; end their blocks). Still, just to be sure, we use a
858 ;; construct that guarantees an error if this
859 ;; expectation is violated.
860 (destructuring-bind
861 (entry-block)
862 (block-succ exit-block)
864 ;; Finish creating the new block.
865 (link-node-to-previous-ctran cast-node new-ctran)
866 (setf (block-last new-block) cast-node)
868 ;; Link the new block into the control sequence.
869 (unlink-blocks exit-block entry-block)
870 (link-blocks exit-block new-block)
871 (link-blocks new-block entry-block)
873 ;; Finish re-pointing the value-holding LVAR to the
874 ;; CAST node.
875 (setf (lvar-dest value) cast-node)
876 (setf (exit-value node) nil)
877 (reoptimize-lvar value)
879 ;; Register the CAST node as providing a value to the
880 ;; LVAR for the continuation.
881 (add-lvar-use cast-node (node-lvar node))
882 (reoptimize-lvar (node-lvar node))
884 ;; Remove the EXIT node.
885 (unlink-node node)
887 ;; And, because we created a new block, we need to
888 ;; force component reanalysis (to assign a DFO number
889 ;; to the block if nothing else).
890 (setf (component-reanalyze *current-component*) t))))
891 (unlink-node node)))))
894 ;;;; combination IR1 optimization
896 ;;; Report as we try each transform?
897 #!+sb-show
898 (defvar *show-transforms-p* nil)
900 (defun check-important-result (node info)
901 (when (and (null (node-lvar node))
902 (ir1-attributep (fun-info-attributes info) important-result))
903 (let ((*compiler-error-context* node))
904 (compiler-style-warn
905 "The return value of ~A should not be discarded."
906 (lvar-fun-name (basic-combination-fun node))))))
908 ;;; Do IR1 optimizations on a COMBINATION node.
909 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
910 (defun ir1-optimize-combination (node)
911 (when (lvar-reoptimize (basic-combination-fun node))
912 (propagate-fun-change node)
913 (maybe-terminate-block node nil))
914 (let ((args (basic-combination-args node))
915 (kind (basic-combination-kind node))
916 (info (basic-combination-fun-info node)))
917 (ecase kind
918 (:local
919 (let ((fun (combination-lambda node)))
920 (if (eq (functional-kind fun) :let)
921 (propagate-let-args node fun)
922 (propagate-local-call-args node fun))))
923 (:error
924 (dolist (arg args)
925 (when arg
926 (setf (lvar-reoptimize arg) nil))))
927 (:full
928 (dolist (arg args)
929 (when arg
930 (setf (lvar-reoptimize arg) nil)))
931 (cond (info
932 (check-important-result node info)
933 (let ((fun (fun-info-destroyed-constant-args info)))
934 (when fun
935 (let ((destroyed-constant-args (funcall fun args)))
936 (when destroyed-constant-args
937 (let ((*compiler-error-context* node))
938 (warn 'constant-modified
939 :fun-name (lvar-fun-name
940 (basic-combination-fun node)))
941 (setf (basic-combination-kind node) :error)
942 (return-from ir1-optimize-combination))))))
943 (let ((fun (fun-info-derive-type info)))
944 (when fun
945 (let ((res (funcall fun node)))
946 (when res
947 (derive-node-type node (coerce-to-values res))
948 (maybe-terminate-block node nil))))))
950 ;; Check against the DEFINED-TYPE unless TYPE is already good.
951 (let* ((fun (basic-combination-fun node))
952 (uses (lvar-uses fun))
953 (leaf (when (ref-p uses) (ref-leaf uses))))
954 (multiple-value-bind (type defined-type)
955 (if (global-var-p leaf)
956 (values (leaf-type leaf) (leaf-defined-type leaf))
957 (values nil nil))
958 (when (and (not (fun-type-p type)) (fun-type-p defined-type))
959 (validate-call-type node type leaf)))))))
960 (:known
961 (aver info)
962 (dolist (arg args)
963 (when arg
964 (setf (lvar-reoptimize arg) nil)))
965 (check-important-result node info)
966 (let ((fun (fun-info-destroyed-constant-args info)))
967 (when (and fun
968 ;; If somebody is really sure that they want to modify
969 ;; constants, let them.
970 (policy node (> check-constant-modification 0)))
971 (let ((destroyed-constant-args (funcall fun args)))
972 (when destroyed-constant-args
973 (let ((*compiler-error-context* node))
974 (warn 'constant-modified
975 :fun-name (lvar-fun-name
976 (basic-combination-fun node)))
977 (setf (basic-combination-kind node) :error)
978 (return-from ir1-optimize-combination))))))
980 (let ((attr (fun-info-attributes info)))
981 (when (and (ir1-attributep attr foldable)
982 ;; KLUDGE: The next test could be made more sensitive,
983 ;; only suppressing constant-folding of functions with
984 ;; CALL attributes when they're actually passed
985 ;; function arguments. -- WHN 19990918
986 (not (ir1-attributep attr call))
987 (every #'constant-lvar-p args)
988 (node-lvar node))
989 (constant-fold-call node)
990 (return-from ir1-optimize-combination))
991 (when (and (ir1-attributep attr commutative)
992 (= (length args) 2)
993 (constant-lvar-p (first args))
994 (not (constant-lvar-p (second args))))
995 (setf (basic-combination-args node) (nreverse args))))
996 (let ((fun (fun-info-derive-type info)))
997 (when fun
998 (let ((res (funcall fun node)))
999 (when res
1000 (derive-node-type node (coerce-to-values res))
1001 (maybe-terminate-block node nil)))))
1003 (let ((fun (fun-info-optimizer info)))
1004 (unless (and fun (funcall fun node))
1005 ;; First give the VM a peek at the call
1006 (multiple-value-bind (style transform)
1007 (combination-implementation-style node)
1008 (ecase style
1009 (:direct
1010 ;; The VM knows how to handle this.
1012 (:transform
1013 ;; The VM mostly knows how to handle this. We need
1014 ;; to massage the call slightly, though.
1015 (transform-call node transform (combination-fun-source-name node)))
1016 ((:default :maybe)
1017 ;; Let transforms have a crack at it.
1018 (dolist (x (fun-info-transforms info))
1019 #!+sb-show
1020 (when *show-transforms-p*
1021 (let* ((lvar (basic-combination-fun node))
1022 (fname (lvar-fun-name lvar t)))
1023 (/show "trying transform" x (transform-function x) "for" fname)))
1024 (unless (ir1-transform node x)
1025 #!+sb-show
1026 (when *show-transforms-p*
1027 (/show "quitting because IR1-TRANSFORM result was NIL"))
1028 (return)))))))))))
1030 (values))
1032 (defun xep-tail-combination-p (node)
1033 (and (combination-p node)
1034 (let* ((lvar (combination-lvar node))
1035 (dest (when (lvar-p lvar) (lvar-dest lvar)))
1036 (lambda (when (return-p dest) (return-lambda dest))))
1037 (and (lambda-p lambda)
1038 (eq :external (lambda-kind lambda))))))
1040 ;;; If NODE doesn't return (i.e. return type is NIL), then terminate
1041 ;;; the block there, and link it to the component tail.
1043 ;;; Except when called during IR1 convertion, we delete the
1044 ;;; continuation if it has no other uses. (If it does have other uses,
1045 ;;; we reoptimize.)
1047 ;;; Termination on the basis of a continuation type is
1048 ;;; inhibited when:
1049 ;;; -- The continuation is deleted (hence the assertion is spurious), or
1050 ;;; -- We are in IR1 conversion (where THE assertions are subject to
1051 ;;; weakening.) FIXME: Now THE assertions are not weakened, but new
1052 ;;; uses can(?) be added later. -- APD, 2003-07-17
1054 ;;; Why do we need to consider LVAR type? -- APD, 2003-07-30
1055 (defun maybe-terminate-block (node ir1-converting-not-optimizing-p)
1056 (declare (type (or basic-combination cast ref) node))
1057 (let* ((block (node-block node))
1058 (lvar (node-lvar node))
1059 (ctran (node-next node))
1060 (tail (component-tail (block-component block)))
1061 (succ (first (block-succ block))))
1062 (declare (ignore lvar))
1063 (unless (or (and (eq node (block-last block)) (eq succ tail))
1064 (block-delete-p block))
1065 ;; Even if the combination will never return, don't terminate if this
1066 ;; is the tail call of a XEP: doing that would inhibit TCO.
1067 (when (and (eq (node-derived-type node) *empty-type*)
1068 (not (xep-tail-combination-p node)))
1069 (cond (ir1-converting-not-optimizing-p
1070 (cond
1071 ((block-last block)
1072 (aver (eq (block-last block) node)))
1074 (setf (block-last block) node)
1075 (setf (ctran-use ctran) nil)
1076 (setf (ctran-kind ctran) :unused)
1077 (setf (ctran-block ctran) nil)
1078 (setf (node-next node) nil)
1079 (link-blocks block (ctran-starts-block ctran)))))
1081 (node-ends-block node)))
1083 (let ((succ (first (block-succ block))))
1084 (unlink-blocks block succ)
1085 (setf (component-reanalyze (block-component block)) t)
1086 (aver (not (block-succ block)))
1087 (link-blocks block tail)
1088 (cond (ir1-converting-not-optimizing-p
1089 (%delete-lvar-use node))
1090 (t (delete-lvar-use node)
1091 (when (null (block-pred succ))
1092 (mark-for-deletion succ)))))
1093 t))))
1095 ;;; This is called both by IR1 conversion and IR1 optimization when
1096 ;;; they have verified the type signature for the call, and are
1097 ;;; wondering if something should be done to special-case the call. If
1098 ;;; CALL is a call to a global function, then see whether it defined
1099 ;;; or known:
1100 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
1101 ;;; the expansion and change the call to call it. Expansion is
1102 ;;; enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
1103 ;;; true, we never expand, since this function has already been
1104 ;;; converted. Local call analysis will duplicate the definition
1105 ;;; if necessary. We claim that the parent form is LABELS for
1106 ;;; context declarations, since we don't want it to be considered
1107 ;;; a real global function.
1108 ;;; -- If it is a known function, mark it as such by setting the KIND.
1110 ;;; We return the leaf referenced (NIL if not a leaf) and the
1111 ;;; FUN-INFO assigned.
1112 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
1113 (declare (type combination call))
1114 (let* ((ref (lvar-uses (basic-combination-fun call)))
1115 (leaf (when (ref-p ref) (ref-leaf ref)))
1116 (inlinep (if (defined-fun-p leaf)
1117 (defined-fun-inlinep leaf)
1118 :no-chance)))
1119 (cond
1120 ((eq inlinep :notinline)
1121 (let ((info (info :function :info (leaf-source-name leaf))))
1122 (when info
1123 (setf (basic-combination-fun-info call) info))
1124 (values nil nil)))
1125 ((not (and (global-var-p leaf)
1126 (eq (global-var-kind leaf) :global-function)))
1127 (values leaf nil))
1128 ((and (ecase inlinep
1129 (:inline t)
1130 (:no-chance nil)
1131 ((nil :maybe-inline) (policy call (zerop space))))
1132 (defined-fun-p leaf)
1133 (defined-fun-inline-expansion leaf)
1134 (inline-expansion-ok call))
1135 ;; Inline: if the function has already been converted at another call
1136 ;; site in this component, we point this REF to the functional. If not,
1137 ;; we convert the expansion.
1139 ;; For :INLINE case local call analysis will copy the expansion later,
1140 ;; but for :MAYBE-INLINE and NIL cases we only get one copy of the
1141 ;; expansion per component.
1143 ;; FIXME: We also convert in :INLINE & FUNCTIONAL-KIND case below. What
1144 ;; is it for?
1145 (flet ((frob ()
1146 (let* ((name (leaf-source-name leaf))
1147 (res (ir1-convert-inline-expansion
1148 name
1149 (defined-fun-inline-expansion leaf)
1150 leaf
1151 inlinep
1152 (info :function :info name))))
1153 ;; Allow backward references to this function from following
1154 ;; forms. (Reused only if policy matches.)
1155 (push res (defined-fun-functionals leaf))
1156 (change-ref-leaf ref res))))
1157 (let ((fun (defined-fun-functional leaf)))
1158 (if (or (not fun)
1159 (and (eq inlinep :inline) (functional-kind fun)))
1160 ;; Convert.
1161 (if ir1-converting-not-optimizing-p
1162 (frob)
1163 (with-ir1-environment-from-node call
1164 (frob)
1165 (locall-analyze-component *current-component*)))
1166 ;; If we've already converted, change ref to the converted
1167 ;; functional.
1168 (change-ref-leaf ref fun))))
1169 (values (ref-leaf ref) nil))
1171 (let ((info (info :function :info (leaf-source-name leaf))))
1172 (if info
1173 (values leaf
1174 (progn
1175 (setf (basic-combination-kind call) :known)
1176 (setf (basic-combination-fun-info call) info)))
1177 (values leaf nil)))))))
1179 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
1180 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
1181 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
1182 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
1183 ;;; syntax check, arg/result type processing, but still call
1184 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
1185 ;;; and that checking is done by local call analysis.
1186 (defun validate-call-type (call type fun &optional ir1-converting-not-optimizing-p)
1187 (declare (type combination call) (type ctype type))
1188 (let* ((where (when fun (leaf-where-from fun)))
1189 (same-file-p (eq :defined-here where)))
1190 (cond ((not (fun-type-p type))
1191 (aver (multiple-value-bind (val win)
1192 (csubtypep type (specifier-type 'function))
1193 (or val (not win))))
1194 ;; Using the defined-type too early is a bit of a waste: during
1195 ;; conversion we cannot use the untrusted ASSERT-CALL-TYPE, etc.
1196 (when (and fun (not ir1-converting-not-optimizing-p))
1197 (let ((defined-type (leaf-defined-type fun)))
1198 (when (and (fun-type-p defined-type)
1199 (neq fun (combination-type-validated-for-leaf call)))
1200 ;; Don't validate multiple times against the same leaf --
1201 ;; it doesn't add any information, but may generate the same warning
1202 ;; multiple times.
1203 (setf (combination-type-validated-for-leaf call) fun)
1204 (when (and (valid-fun-use call defined-type
1205 :argument-test #'always-subtypep
1206 :result-test nil
1207 :lossage-fun (if same-file-p
1208 #'compiler-warn
1209 #'compiler-style-warn)
1210 :unwinnage-fun #'compiler-notify)
1211 same-file-p)
1212 (assert-call-type call defined-type nil)
1213 (maybe-terminate-block call ir1-converting-not-optimizing-p)))))
1214 (recognize-known-call call ir1-converting-not-optimizing-p))
1215 ((valid-fun-use call type
1216 :argument-test #'always-subtypep
1217 :result-test nil
1218 :lossage-fun #'compiler-warn
1219 :unwinnage-fun #'compiler-notify)
1220 (assert-call-type call type)
1221 (maybe-terminate-block call ir1-converting-not-optimizing-p)
1222 (recognize-known-call call ir1-converting-not-optimizing-p))
1224 (setf (combination-kind call) :error)
1225 (values nil nil)))))
1227 ;;; This is called by IR1-OPTIMIZE when the function for a call has
1228 ;;; changed. If the call is local, we try to LET-convert it, and
1229 ;;; derive the result type. If it is a :FULL call, we validate it
1230 ;;; against the type, which recognizes known calls, does inline
1231 ;;; expansion, etc. If a call to a predicate in a non-conditional
1232 ;;; position or to a function with a source transform, then we
1233 ;;; reconvert the form to give IR1 another chance.
1234 (defun propagate-fun-change (call)
1235 (declare (type combination call))
1236 (let ((*compiler-error-context* call)
1237 (fun-lvar (basic-combination-fun call)))
1238 (setf (lvar-reoptimize fun-lvar) nil)
1239 (case (combination-kind call)
1240 (:local
1241 (let ((fun (combination-lambda call)))
1242 (maybe-let-convert fun)
1243 (unless (member (functional-kind fun) '(:let :assignment :deleted))
1244 (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
1245 (:full
1246 (multiple-value-bind (leaf info)
1247 (let* ((uses (lvar-uses fun-lvar))
1248 (leaf (when (ref-p uses) (ref-leaf uses))))
1249 (validate-call-type call (lvar-type fun-lvar) leaf))
1250 (cond ((functional-p leaf)
1251 (convert-call-if-possible
1252 (lvar-uses (basic-combination-fun call))
1253 call))
1254 ((not leaf))
1255 ((and (global-var-p leaf)
1256 (eq (global-var-kind leaf) :global-function)
1257 (leaf-has-source-name-p leaf)
1258 (or (info :function :source-transform (leaf-source-name leaf))
1259 (and info
1260 (ir1-attributep (fun-info-attributes info)
1261 predicate)
1262 (let ((lvar (node-lvar call)))
1263 (and lvar (not (if-p (lvar-dest lvar))))))))
1264 (let ((name (leaf-source-name leaf))
1265 (dummies (make-gensym-list
1266 (length (combination-args call)))))
1267 (transform-call call
1268 `(lambda ,dummies
1269 (,@(if (symbolp name)
1270 `(,name)
1271 `(funcall #',name))
1272 ,@dummies))
1273 (leaf-source-name leaf)))))))))
1274 (values))
1276 ;;;; known function optimization
1278 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1279 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1280 ;;; replace it, otherwise add a new one.
1281 (defun record-optimization-failure (node transform args)
1282 (declare (type combination node) (type transform transform)
1283 (type (or fun-type list) args))
1284 (let* ((table (component-failed-optimizations *component-being-compiled*))
1285 (found (assoc transform (gethash node table))))
1286 (if found
1287 (setf (cdr found) args)
1288 (push (cons transform args) (gethash node table))))
1289 (values))
1291 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1292 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1293 ;;; doing the transform for some reason and FLAME is true, then we
1294 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1295 ;;; finalize to pick up. We return true if the transform failed, and
1296 ;;; thus further transformation should be attempted. We return false
1297 ;;; if either the transform succeeded or was aborted.
1298 (defun ir1-transform (node transform)
1299 (declare (type combination node) (type transform transform))
1300 (let* ((type (transform-type transform))
1301 (fun (transform-function transform))
1302 (constrained (fun-type-p type))
1303 (table (component-failed-optimizations *component-being-compiled*))
1304 (flame (case (transform-important transform)
1305 ((t) (policy node (>= speed inhibit-warnings)))
1306 (:slightly (policy node (> speed inhibit-warnings)))))
1307 (*compiler-error-context* node))
1308 (cond ((or (not constrained)
1309 (valid-fun-use node type))
1310 (multiple-value-bind (severity args)
1311 (catch 'give-up-ir1-transform
1312 (transform-call node
1313 (funcall fun node)
1314 (combination-fun-source-name node))
1315 (values :none nil))
1316 (ecase severity
1317 (:none
1318 (remhash node table)
1319 nil)
1320 (:aborted
1321 (setf (combination-kind node) :error)
1322 (when args
1323 (apply #'warn args))
1324 (remhash node table)
1325 nil)
1326 (:failure
1327 (if args
1328 (when flame
1329 (record-optimization-failure node transform args))
1330 (setf (gethash node table)
1331 (remove transform (gethash node table) :key #'car)))
1333 (:delayed
1334 (remhash node table)
1335 nil))))
1336 ((and flame
1337 (valid-fun-use node
1338 type
1339 :argument-test #'types-equal-or-intersect
1340 :result-test #'values-types-equal-or-intersect))
1341 (record-optimization-failure node transform type)
1344 t))))
1346 ;;; When we don't like an IR1 transform, we throw the severity/reason
1347 ;;; and args.
1349 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1350 ;;; aborting this attempt to transform the call, but admitting the
1351 ;;; possibility that this or some other transform will later succeed.
1352 ;;; If arguments are supplied, they are format arguments for an
1353 ;;; efficiency note.
1355 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1356 ;;; force a normal call to the function at run time. No further
1357 ;;; optimizations will be attempted.
1359 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1360 ;;; delay the transform on the node until later. REASONS specifies
1361 ;;; when the transform will be later retried. The :OPTIMIZE reason
1362 ;;; causes the transform to be delayed until after the current IR1
1363 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1364 ;;; be delayed until after constraint propagation.
1366 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1367 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1368 ;;; do CASE operations on the various REASON values, it might be a
1369 ;;; good idea to go OO, representing the reasons by objects, using
1370 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1371 ;;; SIGNAL instead of THROW.
1372 (declaim (ftype (function (&rest t) nil) give-up-ir1-transform))
1373 (defun give-up-ir1-transform (&rest args)
1374 (throw 'give-up-ir1-transform (values :failure args)))
1375 (defun abort-ir1-transform (&rest args)
1376 (throw 'give-up-ir1-transform (values :aborted args)))
1377 (defun delay-ir1-transform (node &rest reasons)
1378 (let ((assoc (assoc node *delayed-ir1-transforms*)))
1379 (cond ((not assoc)
1380 (setf *delayed-ir1-transforms*
1381 (acons node reasons *delayed-ir1-transforms*))
1382 (throw 'give-up-ir1-transform :delayed))
1383 ((cdr assoc)
1384 (dolist (reason reasons)
1385 (pushnew reason (cdr assoc)))
1386 (throw 'give-up-ir1-transform :delayed)))))
1388 ;;; Poor man's catching and resignalling
1389 ;;; Implicit %GIVE-UP macrolet will resignal the give-up "condition"
1390 (defmacro catch-give-up-ir1-transform ((form &optional args) &body gave-up-body)
1391 (let ((block (gensym "BLOCK"))
1392 (kind (gensym "KIND"))
1393 (args (or args (gensym "ARGS"))))
1394 `(block ,block
1395 (multiple-value-bind (,kind ,args)
1396 (catch 'give-up-ir1-transform
1397 (return-from ,block ,form))
1398 (ecase ,kind
1399 (:delayed
1400 (throw 'give-up-ir1-transform :delayed))
1401 ((:failure :aborted)
1402 (macrolet ((%give-up ()
1403 `(throw 'give-up-ir1-transform (values ,',kind
1404 ,',args))))
1405 ,@gave-up-body)))))))
1407 ;;; Clear any delayed transform with no reasons - these should have
1408 ;;; been tried in the last pass. Then remove the reason from the
1409 ;;; delayed transform reasons, and if any become empty then set
1410 ;;; reoptimize flags for the node. Return true if any transforms are
1411 ;;; to be retried.
1412 (defun retry-delayed-ir1-transforms (reason)
1413 (setf *delayed-ir1-transforms*
1414 (remove-if-not #'cdr *delayed-ir1-transforms*))
1415 (let ((reoptimize nil))
1416 (dolist (assoc *delayed-ir1-transforms*)
1417 (let ((reasons (remove reason (cdr assoc))))
1418 (setf (cdr assoc) reasons)
1419 (unless reasons
1420 (let ((node (car assoc)))
1421 (unless (node-deleted node)
1422 (setf reoptimize t)
1423 (setf (node-reoptimize node) t)
1424 (let ((block (node-block node)))
1425 (setf (block-reoptimize block) t)
1426 (reoptimize-component (block-component block) :maybe)))))))
1427 reoptimize))
1429 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1430 ;;; environment, and then install it as the function for the call
1431 ;;; NODE. We do local call analysis so that the new function is
1432 ;;; integrated into the control flow.
1434 ;;; We require the original function source name in order to generate
1435 ;;; a meaningful debug name for the lambda we set up. (It'd be
1436 ;;; possible to do this starting from debug names as well as source
1437 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1438 ;;; generality, since source names are always known to our callers.)
1439 (defun transform-call (call res source-name)
1440 (declare (type combination call) (list res))
1441 (aver (and (legal-fun-name-p source-name)
1442 (not (eql source-name '.anonymous.))))
1443 (node-ends-block call)
1444 ;; The internal variables of a transform are not going to be
1445 ;; interesting to the debugger, so there's no sense in
1446 ;; suppressing the substitution of variables with only one use
1447 ;; (the extra variables can slow down constraint propagation).
1449 ;; This needs to be done before the WITH-IR1-ENVIRONMENT-FROM-NODE,
1450 ;; so that it will bind *LEXENV* to the right environment.
1451 (setf (combination-lexenv call)
1452 (make-lexenv :default (combination-lexenv call)
1453 :policy (process-optimize-decl
1454 '(optimize
1455 (preserve-single-use-debug-variables 0))
1456 (lexenv-policy
1457 (combination-lexenv call)))))
1458 (with-ir1-environment-from-node call
1459 (with-component-last-block (*current-component*
1460 (block-next (node-block call)))
1462 (let ((new-fun (ir1-convert-inline-lambda
1464 :debug-name (debug-name 'lambda-inlined source-name)
1465 :system-lambda t))
1466 (ref (lvar-use (combination-fun call))))
1467 (change-ref-leaf ref new-fun)
1468 (setf (combination-kind call) :full)
1469 (locall-analyze-component *current-component*))))
1470 (values))
1472 ;;; Replace a call to a foldable function of constant arguments with
1473 ;;; the result of evaluating the form. If there is an error during the
1474 ;;; evaluation, we give a warning and leave the call alone, making the
1475 ;;; call a :ERROR call.
1477 ;;; If there is more than one value, then we transform the call into a
1478 ;;; VALUES form.
1479 (defun constant-fold-call (call)
1480 (let ((args (mapcar #'lvar-value (combination-args call)))
1481 (fun-name (combination-fun-source-name call)))
1482 (multiple-value-bind (values win)
1483 (careful-call fun-name
1484 args
1485 call
1486 ;; Note: CMU CL had COMPILER-WARN here, and that
1487 ;; seems more natural, but it's probably not.
1489 ;; It's especially not while bug 173 exists:
1490 ;; Expressions like
1491 ;; (COND (END
1492 ;; (UNLESS (OR UNSAFE? (<= END SIZE)))
1493 ;; ...))
1494 ;; can cause constant-folding TYPE-ERRORs (in
1495 ;; #'<=) when END can be proved to be NIL, even
1496 ;; though the code is perfectly legal and safe
1497 ;; because a NIL value of END means that the
1498 ;; #'<= will never be executed.
1500 ;; Moreover, even without bug 173,
1501 ;; quite-possibly-valid code like
1502 ;; (COND ((NONINLINED-PREDICATE END)
1503 ;; (UNLESS (<= END SIZE))
1504 ;; ...))
1505 ;; (where NONINLINED-PREDICATE is something the
1506 ;; compiler can't do at compile time, but which
1507 ;; turns out to make the #'<= expression
1508 ;; unreachable when END=NIL) could cause errors
1509 ;; when the compiler tries to constant-fold (<=
1510 ;; END SIZE).
1512 ;; So, with or without bug 173, it'd be
1513 ;; unnecessarily evil to do a full
1514 ;; COMPILER-WARNING (and thus return FAILURE-P=T
1515 ;; from COMPILE-FILE) for legal code, so we we
1516 ;; use a wimpier COMPILE-STYLE-WARNING instead.
1517 #-sb-xc-host #'compiler-style-warn
1518 ;; On the other hand, for code we control, we
1519 ;; should be able to work around any bug
1520 ;; 173-related problems, and in particular we
1521 ;; want to be alerted to calls to our own
1522 ;; functions which aren't being folded away; a
1523 ;; COMPILER-WARNING is butch enough to stop the
1524 ;; SBCL build itself in its tracks.
1525 #+sb-xc-host #'compiler-warn
1526 "constant folding")
1527 (cond ((not win)
1528 (setf (combination-kind call) :error))
1529 ((and (proper-list-of-length-p values 1))
1530 (with-ir1-environment-from-node call
1531 (let* ((lvar (node-lvar call))
1532 (prev (node-prev call))
1533 (intermediate-ctran (make-ctran)))
1534 (%delete-lvar-use call)
1535 (setf (ctran-next prev) nil)
1536 (setf (node-prev call) nil)
1537 (reference-constant prev intermediate-ctran lvar
1538 (first values))
1539 (link-node-to-previous-ctran call intermediate-ctran)
1540 (reoptimize-lvar lvar)
1541 (flush-combination call))))
1542 (t (let ((dummies (make-gensym-list (length args))))
1543 (transform-call
1544 call
1545 `(lambda ,dummies
1546 (declare (ignore ,@dummies))
1547 (values ,@(mapcar (lambda (x) `',x) values)))
1548 fun-name))))))
1549 (values))
1551 ;;;; local call optimization
1553 ;;; Propagate TYPE to LEAF and its REFS, marking things changed.
1555 ;;; If the leaf type is a function type, then just leave it alone, since TYPE
1556 ;;; is never going to be more specific than that (and TYPE-INTERSECTION would
1557 ;;; choke.)
1559 ;;; Also, if the type is one requiring special care don't touch it if the leaf
1560 ;;; has multiple references -- otherwise LVAR-CONSERVATIVE-TYPE is screwed.
1561 (defun propagate-to-refs (leaf type)
1562 (declare (type leaf leaf) (type ctype type))
1563 (let ((var-type (leaf-type leaf))
1564 (refs (leaf-refs leaf)))
1565 (unless (or (fun-type-p var-type)
1566 (and (cdr refs)
1567 (eq :declared (leaf-where-from leaf))
1568 (type-needs-conservation-p var-type)))
1569 (let ((int (type-approx-intersection2 var-type type)))
1570 (when (type/= int var-type)
1571 (setf (leaf-type leaf) int)
1572 (let ((s-int (make-single-value-type int)))
1573 (dolist (ref refs)
1574 (derive-node-type ref s-int)
1575 ;; KLUDGE: LET var substitution
1576 (let* ((lvar (node-lvar ref)))
1577 (when (and lvar (combination-p (lvar-dest lvar)))
1578 (reoptimize-lvar lvar)))))))
1579 (values))))
1581 ;;; Iteration variable: exactly one SETQ of the form:
1583 ;;; (let ((var initial))
1584 ;;; ...
1585 ;;; (setq var (+ var step))
1586 ;;; ...)
1587 (defun maybe-infer-iteration-var-type (var initial-type)
1588 (binding* ((sets (lambda-var-sets var) :exit-if-null)
1589 (set (first sets))
1590 (() (null (rest sets)) :exit-if-null)
1591 (set-use (principal-lvar-use (set-value set)))
1592 (() (and (combination-p set-use)
1593 (eq (combination-kind set-use) :known)
1594 (fun-info-p (combination-fun-info set-use))
1595 (not (node-to-be-deleted-p set-use))
1596 (or (eq (combination-fun-source-name set-use) '+)
1597 (eq (combination-fun-source-name set-use) '-)))
1598 :exit-if-null)
1599 (minusp (eq (combination-fun-source-name set-use) '-))
1600 (+-args (basic-combination-args set-use))
1601 (() (and (proper-list-of-length-p +-args 2 2)
1602 (let ((first (principal-lvar-use
1603 (first +-args))))
1604 (and (ref-p first)
1605 (eq (ref-leaf first) var))))
1606 :exit-if-null)
1607 (step-type (lvar-type (second +-args)))
1608 (set-type (lvar-type (set-value set))))
1609 (when (and (numeric-type-p initial-type)
1610 (numeric-type-p step-type)
1611 (or (numeric-type-equal initial-type step-type)
1612 ;; Detect cases like (LOOP FOR 1.0 to 5.0 ...), where
1613 ;; the initial and the step are of different types,
1614 ;; and the step is less contagious.
1615 (numeric-type-equal initial-type
1616 (numeric-contagion initial-type
1617 step-type))))
1618 (labels ((leftmost (x y cmp cmp=)
1619 (cond ((eq x nil) nil)
1620 ((eq y nil) nil)
1621 ((listp x)
1622 (let ((x1 (first x)))
1623 (cond ((listp y)
1624 (let ((y1 (first y)))
1625 (if (funcall cmp x1 y1) x y)))
1627 (if (funcall cmp x1 y) x y)))))
1628 ((listp y)
1629 (let ((y1 (first y)))
1630 (if (funcall cmp= x y1) x y)))
1631 (t (if (funcall cmp x y) x y))))
1632 (max* (x y) (leftmost x y #'> #'>=))
1633 (min* (x y) (leftmost x y #'< #'<=)))
1634 (multiple-value-bind (low high)
1635 (let ((step-type-non-negative (csubtypep step-type (specifier-type
1636 '(real 0 *))))
1637 (step-type-non-positive (csubtypep step-type (specifier-type
1638 '(real * 0)))))
1639 (cond ((or (and step-type-non-negative (not minusp))
1640 (and step-type-non-positive minusp))
1641 (values (numeric-type-low initial-type)
1642 (when (and (numeric-type-p set-type)
1643 (numeric-type-equal set-type initial-type))
1644 (max* (numeric-type-high initial-type)
1645 (numeric-type-high set-type)))))
1646 ((or (and step-type-non-positive (not minusp))
1647 (and step-type-non-negative minusp))
1648 (values (when (and (numeric-type-p set-type)
1649 (numeric-type-equal set-type initial-type))
1650 (min* (numeric-type-low initial-type)
1651 (numeric-type-low set-type)))
1652 (numeric-type-high initial-type)))
1654 (values nil nil))))
1655 (modified-numeric-type initial-type
1656 :low low
1657 :high high
1658 :enumerable nil))))))
1659 (deftransform + ((x y) * * :result result)
1660 "check for iteration variable reoptimization"
1661 (let ((dest (principal-lvar-end result))
1662 (use (principal-lvar-use x)))
1663 (when (and (ref-p use)
1664 (set-p dest)
1665 (eq (ref-leaf use)
1666 (set-var dest)))
1667 (reoptimize-lvar (set-value dest))))
1668 (give-up-ir1-transform))
1670 ;;; Figure out the type of a LET variable that has sets. We compute
1671 ;;; the union of the INITIAL-TYPE and the types of all the set
1672 ;;; values and to a PROPAGATE-TO-REFS with this type.
1673 (defun propagate-from-sets (var initial-type)
1674 (let ((changes (not (csubtypep (lambda-var-last-initial-type var) initial-type)))
1675 (types nil))
1676 (dolist (set (lambda-var-sets var))
1677 (let ((type (lvar-type (set-value set))))
1678 (push type types)
1679 (when (node-reoptimize set)
1680 (let ((old-type (node-derived-type set)))
1681 (unless (values-subtypep old-type type)
1682 (derive-node-type set (make-single-value-type type))
1683 (setf changes t)))
1684 (setf (node-reoptimize set) nil))))
1685 (when changes
1686 (setf (lambda-var-last-initial-type var) initial-type)
1687 (let ((res-type (or (maybe-infer-iteration-var-type var initial-type)
1688 (apply #'type-union initial-type types))))
1689 (propagate-to-refs var res-type))))
1690 (values))
1692 ;;; If a LET variable, find the initial value's type and do
1693 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1694 ;;; type.
1695 (defun ir1-optimize-set (node)
1696 (declare (type cset node))
1697 (let ((var (set-var node)))
1698 (when (and (lambda-var-p var) (leaf-refs var))
1699 (let ((home (lambda-var-home var)))
1700 (when (eq (functional-kind home) :let)
1701 (let* ((initial-value (let-var-initial-value var))
1702 (initial-type (lvar-type initial-value)))
1703 (setf (lvar-reoptimize initial-value) nil)
1704 (propagate-from-sets var initial-type))))))
1705 (derive-node-type node (make-single-value-type
1706 (lvar-type (set-value node))))
1707 (setf (node-reoptimize node) nil)
1708 (values))
1710 ;;; Return true if the value of REF will always be the same (and is
1711 ;;; thus legal to substitute.)
1712 (defun constant-reference-p (ref)
1713 (declare (type ref ref))
1714 (let ((leaf (ref-leaf ref)))
1715 (typecase leaf
1716 ((or constant functional) t)
1717 (lambda-var
1718 (null (lambda-var-sets leaf)))
1719 (defined-fun
1720 (not (eq (defined-fun-inlinep leaf) :notinline)))
1721 (global-var
1722 (case (global-var-kind leaf)
1723 (:global-function
1724 (let ((name (leaf-source-name leaf)))
1725 (or #-sb-xc-host
1726 (eq (symbol-package (fun-name-block-name name))
1727 *cl-package*)
1728 (info :function :info name)))))))))
1730 ;;; If we have a non-set LET var with a single use, then (if possible)
1731 ;;; replace the variable reference's LVAR with the arg lvar.
1733 ;;; We change the REF to be a reference to NIL with unused value, and
1734 ;;; let it be flushed as dead code. A side effect of this substitution
1735 ;;; is to delete the variable.
1736 (defun substitute-single-use-lvar (arg var)
1737 (declare (type lvar arg) (type lambda-var var))
1738 (binding* ((ref (first (leaf-refs var)))
1739 (lvar (node-lvar ref) :exit-if-null)
1740 (dest (lvar-dest lvar))
1741 (dest-lvar (when (valued-node-p dest) (node-lvar dest))))
1742 (when (and
1743 ;; Think about (LET ((A ...)) (IF ... A ...)): two
1744 ;; LVAR-USEs should not be met on one path. Another problem
1745 ;; is with dynamic-extent.
1746 (eq (lvar-uses lvar) ref)
1747 (not (block-delete-p (node-block ref)))
1748 ;; If the destinatation is dynamic extent, don't substitute unless
1749 ;; the source is as well.
1750 (or (not dest-lvar)
1751 (not (lvar-dynamic-extent dest-lvar))
1752 (lvar-dynamic-extent lvar))
1753 (typecase dest
1754 ;; we should not change lifetime of unknown values lvars
1755 (cast
1756 (and (type-single-value-p (lvar-derived-type arg))
1757 (multiple-value-bind (pdest pprev)
1758 (principal-lvar-end lvar)
1759 (declare (ignore pdest))
1760 (lvar-single-value-p pprev))
1761 ;; CASTs can disappear, don't substitute if
1762 ;; DEST-LVAR has other uses (this will be
1763 ;; insufficient if we have a CAST-CAST chain, but
1764 ;; works well for a single CAST)
1765 (or (null dest-lvar)
1766 (atom (lvar-uses dest-lvar)))))
1767 (mv-combination
1768 (or (eq (basic-combination-fun dest) lvar)
1769 (and (eq (basic-combination-kind dest) :local)
1770 (type-single-value-p (lvar-derived-type arg)))))
1771 ((or creturn exit)
1772 ;; While CRETURN and EXIT nodes may be known-values,
1773 ;; they have their own complications, such as
1774 ;; substitution into CRETURN may create new tail calls.
1775 nil)
1777 (aver (lvar-single-value-p lvar))
1779 (eq (node-home-lambda ref)
1780 (lambda-home (lambda-var-home var))))
1781 (let ((ref-type (single-value-type (node-derived-type ref))))
1782 (cond ((csubtypep (single-value-type (lvar-type arg)) ref-type)
1783 (substitute-lvar-uses lvar arg
1784 ;; Really it is (EQ (LVAR-USES LVAR) REF):
1786 (delete-lvar-use ref))
1788 (let* ((value (make-lvar))
1789 (cast (insert-cast-before ref value ref-type
1790 ;; KLUDGE: it should be (TYPE-CHECK 0)
1791 *policy*)))
1792 (setf (cast-type-to-check cast) *wild-type*)
1793 (substitute-lvar-uses value arg
1794 ;; FIXME
1796 (%delete-lvar-use ref)
1797 (add-lvar-use cast lvar)))))
1798 (setf (node-derived-type ref) *wild-type*)
1799 (change-ref-leaf ref (find-constant nil))
1800 (delete-ref ref)
1801 (unlink-node ref)
1802 (reoptimize-lvar lvar)
1803 t)))
1805 ;;; Delete a LET, removing the call and bind nodes, and warning about
1806 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1807 ;;; along right away and delete the REF and then the lambda, since we
1808 ;;; flush the FUN lvar.
1809 (defun delete-let (clambda)
1810 (declare (type clambda clambda))
1811 (aver (functional-letlike-p clambda))
1812 (note-unreferenced-fun-vars clambda)
1813 (let ((call (let-combination clambda)))
1814 (flush-dest (basic-combination-fun call))
1815 (unlink-node call)
1816 (unlink-node (lambda-bind clambda))
1817 (setf (lambda-bind clambda) nil))
1818 (setf (functional-kind clambda) :zombie)
1819 (let ((home (lambda-home clambda)))
1820 (setf (lambda-lets home) (delete clambda (lambda-lets home))))
1821 (values))
1823 ;;; This function is called when one of the arguments to a LET
1824 ;;; changes. We look at each changed argument. If the corresponding
1825 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1826 ;;; consider substituting for the variable, and also propagate
1827 ;;; derived-type information for the arg to all the VAR's refs.
1829 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1830 ;;; subtype of the argument's leaf type. This prevents type checking
1831 ;;; from being defeated, and also ensures that the best representation
1832 ;;; for the variable can be used.
1834 ;;; Substitution of individual references is inhibited if the
1835 ;;; reference is in a different component from the home. This can only
1836 ;;; happen with closures over top level lambda vars. In such cases,
1837 ;;; the references may have already been compiled, and thus can't be
1838 ;;; retroactively modified.
1840 ;;; If all of the variables are deleted (have no references) when we
1841 ;;; are done, then we delete the LET.
1843 ;;; Note that we are responsible for clearing the LVAR-REOPTIMIZE
1844 ;;; flags.
1845 (defun propagate-let-args (call fun)
1846 (declare (type combination call) (type clambda fun))
1847 (loop for arg in (combination-args call)
1848 and var in (lambda-vars fun) do
1849 (when (and arg (lvar-reoptimize arg))
1850 (setf (lvar-reoptimize arg) nil)
1851 (cond
1852 ((lambda-var-sets var)
1853 (propagate-from-sets var (lvar-type arg)))
1854 ((let ((use (lvar-uses arg)))
1855 (when (ref-p use)
1856 (let ((leaf (ref-leaf use)))
1857 (when (and (constant-reference-p use)
1858 (csubtypep (leaf-type leaf)
1859 ;; (NODE-DERIVED-TYPE USE) would
1860 ;; be better -- APD, 2003-05-15
1861 (leaf-type var)))
1862 (propagate-to-refs var (lvar-type arg))
1863 (let ((use-component (node-component use)))
1864 (prog1 (substitute-leaf-if
1865 (lambda (ref)
1866 (cond ((eq (node-component ref) use-component)
1869 (aver (lambda-toplevelish-p (lambda-home fun)))
1870 nil)))
1871 leaf var)))
1872 t)))))
1873 ((and (null (rest (leaf-refs var)))
1874 (not (preserve-single-use-debug-var-p call var))
1875 (substitute-single-use-lvar arg var)))
1877 (propagate-to-refs var (lvar-type arg))))))
1879 (when (every #'not (combination-args call))
1880 (delete-let fun))
1882 (values))
1884 ;;; This function is called when one of the args to a non-LET local
1885 ;;; call changes. For each changed argument corresponding to an unset
1886 ;;; variable, we compute the union of the types across all calls and
1887 ;;; propagate this type information to the var's refs.
1889 ;;; If the function has an entry-fun, then we don't do anything: since
1890 ;;; it has a XEP we would not discover anything.
1892 ;;; If the function is an optional-entry-point, we will just make sure
1893 ;;; &REST lists are known to be lists. Doing the regular rigamarole
1894 ;;; can erronously propagate too strict types into refs: see
1895 ;;; BUG-655203-REGRESSION in tests/compiler.pure.lisp.
1897 ;;; We can clear the LVAR-REOPTIMIZE flags for arguments in all calls
1898 ;;; corresponding to changed arguments in CALL, since the only use in
1899 ;;; IR1 optimization of the REOPTIMIZE flag for local call args is
1900 ;;; right here.
1901 (defun propagate-local-call-args (call fun)
1902 (declare (type combination call) (type clambda fun))
1903 (unless (functional-entry-fun fun)
1904 (if (lambda-optional-dispatch fun)
1905 ;; We can still make sure &REST is known to be a list.
1906 (loop for var in (lambda-vars fun)
1907 do (let ((info (lambda-var-arg-info var)))
1908 (when (and info (eq :rest (arg-info-kind info)))
1909 (propagate-from-sets var (specifier-type 'list)))))
1910 ;; The normal case.
1911 (let* ((vars (lambda-vars fun))
1912 (union (mapcar (lambda (arg var)
1913 (when (and arg
1914 (lvar-reoptimize arg)
1915 (null (basic-var-sets var)))
1916 (lvar-type arg)))
1917 (basic-combination-args call)
1918 vars))
1919 (this-ref (lvar-use (basic-combination-fun call))))
1921 (dolist (arg (basic-combination-args call))
1922 (when arg
1923 (setf (lvar-reoptimize arg) nil)))
1925 (dolist (ref (leaf-refs fun))
1926 (let ((dest (node-dest ref)))
1927 (unless (or (eq ref this-ref) (not dest))
1928 (setq union
1929 (mapcar (lambda (this-arg old)
1930 (when old
1931 (setf (lvar-reoptimize this-arg) nil)
1932 (type-union (lvar-type this-arg) old)))
1933 (basic-combination-args dest)
1934 union)))))
1936 (loop for var in vars
1937 and type in union
1938 when type do (propagate-to-refs var type)))))
1940 (values))
1942 ;;;; multiple values optimization
1944 ;;; Do stuff to notice a change to a MV combination node. There are
1945 ;;; two main branches here:
1946 ;;; -- If the call is local, then it is already a MV let, or should
1947 ;;; become one. Note that although all :LOCAL MV calls must eventually
1948 ;;; be converted to :MV-LETs, there can be a window when the call
1949 ;;; is local, but has not been LET converted yet. This is because
1950 ;;; the entry-point lambdas may have stray references (in other
1951 ;;; entry points) that have not been deleted yet.
1952 ;;; -- The call is full. This case is somewhat similar to the non-MV
1953 ;;; combination optimization: we propagate return type information and
1954 ;;; notice non-returning calls. We also have an optimization
1955 ;;; which tries to convert MV-CALLs into MV-binds.
1956 (defun ir1-optimize-mv-combination (node)
1957 (let ((fun (basic-combination-fun node)))
1958 (unless (and (node-p (lvar-uses fun))
1959 (node-to-be-deleted-p (lvar-uses fun)))
1960 (ecase (basic-combination-kind node)
1961 (:local
1962 (when (lvar-reoptimize fun)
1963 (setf (lvar-reoptimize fun) nil)
1964 (maybe-let-convert (combination-lambda node)))
1965 (setf (lvar-reoptimize (first (basic-combination-args node))) nil)
1966 (when (eq (functional-kind (combination-lambda node)) :mv-let)
1967 (unless (convert-mv-bind-to-let node)
1968 (ir1-optimize-mv-bind node))))
1969 (:full
1970 (let* ((fun-changed (lvar-reoptimize fun))
1971 (args (basic-combination-args node)))
1972 (when fun-changed
1973 (setf (lvar-reoptimize fun) nil)
1974 (let ((type (lvar-type fun)))
1975 (when (fun-type-p type)
1976 (derive-node-type node (fun-type-returns type))))
1977 (maybe-terminate-block node nil)
1978 (let ((use (lvar-uses fun)))
1979 (when (and (ref-p use) (functional-p (ref-leaf use)))
1980 (convert-call-if-possible use node)
1981 (when (eq (basic-combination-kind node) :local)
1982 (maybe-let-convert (ref-leaf use))))))
1983 (unless (or (eq (basic-combination-kind node) :local)
1984 (eq (lvar-fun-name fun) '%throw))
1985 (ir1-optimize-mv-call node))
1986 (dolist (arg args)
1987 (setf (lvar-reoptimize arg) nil))))
1988 (:error))))
1989 (values))
1991 ;;; Propagate derived type info from the values lvar to the vars.
1992 (defun ir1-optimize-mv-bind (node)
1993 (declare (type mv-combination node))
1994 (let* ((arg (first (basic-combination-args node)))
1995 (vars (lambda-vars (combination-lambda node)))
1996 (n-vars (length vars))
1997 (types (values-type-in (lvar-derived-type arg)
1998 n-vars)))
1999 (loop for var in vars
2000 and type in types
2001 do (if (basic-var-sets var)
2002 (propagate-from-sets var type)
2003 (propagate-to-refs var type)))
2004 (setf (lvar-reoptimize arg) nil))
2005 (values))
2007 ;;; If possible, convert a general MV call to an MV-BIND. We can do
2008 ;;; this if:
2009 ;;; -- The call has only one argument, and
2010 ;;; -- The function has a known fixed number of arguments, or
2011 ;;; -- The argument yields a known fixed number of values.
2013 ;;; What we do is change the function in the MV-CALL to be a lambda
2014 ;;; that "looks like an MV bind", which allows
2015 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
2016 ;;; converted (the next time around.) This new lambda just calls the
2017 ;;; actual function with the MV-BIND variables as arguments. Note that
2018 ;;; this new MV bind is not let-converted immediately, as there are
2019 ;;; going to be stray references from the entry-point functions until
2020 ;;; they get deleted.
2022 ;;; In order to avoid loss of argument count checking, we only do the
2023 ;;; transformation according to a known number of expected argument if
2024 ;;; safety is unimportant. We can always convert if we know the number
2025 ;;; of actual values, since the normal call that we build will still
2026 ;;; do any appropriate argument count checking.
2028 ;;; We only attempt the transformation if the called function is a
2029 ;;; constant reference. This allows us to just splice the leaf into
2030 ;;; the new function, instead of trying to somehow bind the function
2031 ;;; expression. The leaf must be constant because we are evaluating it
2032 ;;; again in a different place. This also has the effect of squelching
2033 ;;; multiple warnings when there is an argument count error.
2034 (defun ir1-optimize-mv-call (node)
2035 (let ((fun (basic-combination-fun node))
2036 (*compiler-error-context* node)
2037 (ref (lvar-uses (basic-combination-fun node)))
2038 (args (basic-combination-args node)))
2040 (unless (and (ref-p ref) (constant-reference-p ref)
2041 (singleton-p args))
2042 (return-from ir1-optimize-mv-call))
2044 (multiple-value-bind (min max)
2045 (fun-type-nargs (lvar-type fun))
2046 (let ((total-nvals
2047 (multiple-value-bind (types nvals)
2048 (values-types (lvar-derived-type (first args)))
2049 (declare (ignore types))
2050 (if (eq nvals :unknown) nil nvals))))
2052 (when total-nvals
2053 (when (and min (< total-nvals min))
2054 (compiler-warn
2055 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2056 at least ~R."
2057 total-nvals min)
2058 (setf (basic-combination-kind node) :error)
2059 (return-from ir1-optimize-mv-call))
2060 (when (and max (> total-nvals max))
2061 (compiler-warn
2062 "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
2063 at most ~R."
2064 total-nvals max)
2065 (setf (basic-combination-kind node) :error)
2066 (return-from ir1-optimize-mv-call)))
2068 (let ((count (cond (total-nvals)
2069 ((and (policy node (zerop verify-arg-count))
2070 (eql min max))
2071 min)
2072 (t nil))))
2073 (when count
2074 (with-ir1-environment-from-node node
2075 (let* ((dums (make-gensym-list count))
2076 (ignore (gensym))
2077 (leaf (ref-leaf ref))
2078 (fun (ir1-convert-lambda
2079 `(lambda (&optional ,@dums &rest ,ignore)
2080 (declare (ignore ,ignore))
2081 (%funcall ,leaf ,@dums))
2082 :source-name (leaf-%source-name leaf)
2083 :debug-name (leaf-%debug-name leaf))))
2084 (change-ref-leaf ref fun)
2085 (aver (eq (basic-combination-kind node) :full))
2086 (locall-analyze-component *current-component*)
2087 (aver (eq (basic-combination-kind node) :local)))))))))
2088 (values))
2090 ;;; If we see:
2091 ;;; (multiple-value-bind
2092 ;;; (x y)
2093 ;;; (values xx yy)
2094 ;;; ...)
2095 ;;; Convert to:
2096 ;;; (let ((x xx)
2097 ;;; (y yy))
2098 ;;; ...)
2100 ;;; What we actually do is convert the VALUES combination into a
2101 ;;; normal LET combination calling the original :MV-LET lambda. If
2102 ;;; there are extra args to VALUES, discard the corresponding
2103 ;;; lvars. If there are insufficient args, insert references to NIL.
2104 (defun convert-mv-bind-to-let (call)
2105 (declare (type mv-combination call))
2106 (let* ((arg (first (basic-combination-args call)))
2107 (use (lvar-uses arg)))
2108 (when (and (combination-p use)
2109 (eq (lvar-fun-name (combination-fun use))
2110 'values))
2111 (let* ((fun (combination-lambda call))
2112 (vars (lambda-vars fun))
2113 (vals (combination-args use))
2114 (nvars (length vars))
2115 (nvals (length vals)))
2116 (cond ((> nvals nvars)
2117 (mapc #'flush-dest (subseq vals nvars))
2118 (setq vals (subseq vals 0 nvars)))
2119 ((< nvals nvars)
2120 (with-ir1-environment-from-node use
2121 (let ((node-prev (node-prev use)))
2122 (setf (node-prev use) nil)
2123 (setf (ctran-next node-prev) nil)
2124 (collect ((res vals))
2125 (loop for count below (- nvars nvals)
2126 for prev = node-prev then ctran
2127 for ctran = (make-ctran)
2128 and lvar = (make-lvar use)
2129 do (reference-constant prev ctran lvar nil)
2130 (res lvar)
2131 finally (link-node-to-previous-ctran
2132 use ctran))
2133 (setq vals (res)))))))
2134 (setf (combination-args use) vals)
2135 (flush-dest (combination-fun use))
2136 (let ((fun-lvar (basic-combination-fun call)))
2137 (setf (lvar-dest fun-lvar) use)
2138 (setf (combination-fun use) fun-lvar)
2139 (flush-lvar-externally-checkable-type fun-lvar))
2140 (setf (combination-kind use) :local)
2141 (setf (functional-kind fun) :let)
2142 (flush-dest (first (basic-combination-args call)))
2143 (unlink-node call)
2144 (when vals
2145 (reoptimize-lvar (first vals)))
2146 ;; Propagate derived types from the VALUES call to its args:
2147 ;; transforms can leave the VALUES call with a better type
2148 ;; than its args have, so make sure not to throw that away.
2149 (let ((types (values-type-types (node-derived-type use))))
2150 (dolist (val vals)
2151 (when types
2152 (let ((type (pop types)))
2153 (assert-lvar-type val type **zero-typecheck-policy**)))))
2154 ;; Propagate declared types of MV-BIND variables.
2155 (propagate-to-args use fun)
2156 (reoptimize-call use))
2157 t)))
2159 ;;; If we see:
2160 ;;; (values-list (list x y z))
2162 ;;; Convert to:
2163 ;;; (values x y z)
2165 ;;; In implementation, this is somewhat similar to
2166 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
2167 ;;; args of the VALUES-LIST call, flushing the old argument lvar
2168 ;;; (allowing the LIST to be flushed.)
2170 ;;; FIXME: Thus we lose possible type assertions on (LIST ...).
2171 (defoptimizer (values-list optimizer) ((list) node)
2172 (let ((use (lvar-uses list)))
2173 (when (and (combination-p use)
2174 (eq (lvar-fun-name (combination-fun use))
2175 'list))
2177 ;; FIXME: VALUES might not satisfy an assertion on NODE-LVAR.
2178 (change-ref-leaf (lvar-uses (combination-fun node))
2179 (find-free-fun 'values "in a strange place"))
2180 (setf (combination-kind node) :full)
2181 (let ((args (combination-args use)))
2182 (dolist (arg args)
2183 (setf (lvar-dest arg) node)
2184 (flush-lvar-externally-checkable-type arg))
2185 (setf (combination-args use) nil)
2186 (flush-dest list)
2187 (flush-combination use)
2188 (setf (combination-args node) args))
2189 t)))
2191 ;;; If VALUES appears in a non-MV context, then effectively convert it
2192 ;;; to a PROG1. This allows the computation of the additional values
2193 ;;; to become dead code.
2194 (deftransform values ((&rest vals) * * :node node)
2195 (unless (lvar-single-value-p (node-lvar node))
2196 (give-up-ir1-transform))
2197 (setf (node-derived-type node)
2198 (make-short-values-type (list (single-value-type
2199 (node-derived-type node)))))
2200 (principal-lvar-single-valuify (node-lvar node))
2201 (if vals
2202 (let ((dummies (make-gensym-list (length (cdr vals)))))
2203 `(lambda (val ,@dummies)
2204 (declare (ignore ,@dummies))
2205 val))
2206 nil))
2208 ;;; TODO:
2209 ;;; - CAST chains;
2210 (defun delete-cast (cast)
2211 (declare (type cast cast))
2212 (let ((value (cast-value cast))
2213 (lvar (node-lvar cast)))
2214 (delete-filter cast lvar value)
2215 (when lvar
2216 (reoptimize-lvar lvar)
2217 (when (lvar-single-value-p lvar)
2218 (note-single-valuified-lvar lvar)))
2219 (values)))
2221 (defun may-delete-vestigial-exit (cast)
2222 (let ((exit-lexenv (cast-vestigial-exit-lexenv cast)))
2223 (when exit-lexenv
2224 ;; Vestigial exits are only introduced when eliminating a local
2225 ;; RETURN-FROM. We may delete them only when we can show that
2226 ;; there are no other code paths that use the entry LVAR that
2227 ;; are live from within the block that contained the deleted
2228 ;; EXIT (our predecessor block). The conservative version of
2229 ;; this is that there are no EXITs for any ENTRY introduced
2230 ;; between the LEXENV of the deleted EXIT and the LEXENV of the
2231 ;; target ENTRY.
2232 (let* ((entry-lexenv (cast-vestigial-exit-entry-lexenv cast))
2233 (entry-blocks (lexenv-blocks entry-lexenv))
2234 (entry-tags (lexenv-tags entry-lexenv)))
2235 (do ((current-block (lexenv-blocks exit-lexenv) (cdr current-block)))
2236 ((eq current-block entry-blocks))
2237 (when (entry-exits (cadar current-block))
2238 (return-from may-delete-vestigial-exit nil)))
2239 (do ((current-tag (lexenv-tags exit-lexenv) (cdr current-tag)))
2240 ((eq current-tag entry-tags))
2241 (when (entry-exits (cadar current-tag))
2242 (return-from may-delete-vestigial-exit nil))))))
2243 (values t))
2245 (defun compile-time-type-error-context (context)
2246 #+sb-xc-host context
2247 #-sb-xc-host (source-to-string context))
2249 (defun ir1-optimize-cast (cast &optional do-not-optimize)
2250 (declare (type cast cast))
2251 (let ((value (cast-value cast))
2252 (atype (cast-asserted-type cast)))
2253 (unless (or do-not-optimize
2254 (not (may-delete-vestigial-exit cast)))
2255 (let ((lvar (node-lvar cast)))
2256 (when (values-subtypep (lvar-derived-type value)
2257 (cast-asserted-type cast))
2258 (delete-cast cast)
2259 (return-from ir1-optimize-cast t))
2261 (when (and (listp (lvar-uses value))
2262 lvar)
2263 ;; Pathwise removing of CAST
2264 (let ((ctran (node-next cast))
2265 (dest (lvar-dest lvar))
2266 next-block)
2267 (collect ((merges))
2268 (do-uses (use value)
2269 (when (and (values-subtypep (node-derived-type use) atype)
2270 (immediately-used-p value use))
2271 (unless next-block
2272 (when ctran (ensure-block-start ctran))
2273 (setq next-block (first (block-succ (node-block cast))))
2274 (ensure-block-start (node-prev cast))
2275 (reoptimize-lvar lvar)
2276 (setf (lvar-%derived-type value) nil))
2277 (%delete-lvar-use use)
2278 (add-lvar-use use lvar)
2279 (unlink-blocks (node-block use) (node-block cast))
2280 (link-blocks (node-block use) next-block)
2281 (when (and (return-p dest)
2282 (basic-combination-p use)
2283 (eq (basic-combination-kind use) :local))
2284 (merges use))))
2285 (dolist (use (merges))
2286 (merge-tail-sets use)))))))
2288 (let* ((value-type (lvar-derived-type value))
2289 (int (values-type-intersection value-type atype)))
2290 (derive-node-type cast int)
2291 (when (eq int *empty-type*)
2292 (unless (eq value-type *empty-type*)
2294 ;; FIXME: Do it in one step.
2295 (let ((context (node-source-form cast))
2296 (detail (lvar-all-sources (cast-value cast))))
2297 (filter-lvar
2298 value
2299 (if (cast-single-value-p cast)
2300 `(list 'dummy)
2301 `(multiple-value-call #'list 'dummy)))
2302 (filter-lvar
2303 (cast-value cast)
2304 ;; FIXME: Derived type.
2305 `(%compile-time-type-error 'dummy
2306 ',(type-specifier atype)
2307 ',(type-specifier value-type)
2308 ',detail
2309 ',(compile-time-type-error-context context))))
2310 ;; KLUDGE: FILTER-LVAR does not work for non-returning
2311 ;; functions, so we declare the return type of
2312 ;; %COMPILE-TIME-TYPE-ERROR to be * and derive the real type
2313 ;; here.
2314 (setq value (cast-value cast))
2315 (derive-node-type (lvar-uses value) *empty-type*)
2316 (maybe-terminate-block (lvar-uses value) nil)
2317 ;; FIXME: Is it necessary?
2318 (aver (null (block-pred (node-block cast))))
2319 (delete-block-lazily (node-block cast))
2320 (return-from ir1-optimize-cast)))
2321 (when (eq (node-derived-type cast) *empty-type*)
2322 (maybe-terminate-block cast nil))
2324 (when (and (cast-%type-check cast)
2325 (values-subtypep value-type
2326 (cast-type-to-check cast)))
2327 (setf (cast-%type-check cast) nil))))
2329 (unless do-not-optimize
2330 (setf (node-reoptimize cast) nil)))
2332 (deftransform make-symbol ((string) (simple-string))
2333 `(%make-symbol string))