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