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