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