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