0.7.10.32:
[sbcl.git] / src / compiler / ir1opt.lisp
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.
7
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.
16
17 (in-package "SB!C")
18 \f
19 ;;;; interface for obtaining results of constant folding
20
21 ;;; Return true for a CONTINUATION whose sole use is a reference to a
22 ;;; constant leaf.
23 (defun constant-continuation-p (thing)
24   (and (continuation-p thing)
25        (let ((use (continuation-use thing)))
26          (and (ref-p use)
27               (constant-p (ref-leaf use))))))
28
29 ;;; Return the constant value for a continuation whose only use is a
30 ;;; constant node.
31 (declaim (ftype (function (continuation) t) continuation-value))
32 (defun continuation-value (cont)
33   (aver (constant-continuation-p cont))
34   (constant-value (ref-leaf (continuation-use cont))))
35 \f
36 ;;;; interface for obtaining results of type inference
37
38 ;;; Return a (possibly values) type that describes what we have proven
39 ;;; about the type of Cont without taking any type assertions into
40 ;;; consideration. This is just the union of the NODE-DERIVED-TYPE of
41 ;;; all the uses. Most often people use CONTINUATION-DERIVED-TYPE or
42 ;;; CONTINUATION-TYPE instead of using this function directly.
43 (defun continuation-proven-type (cont)
44   (declare (type continuation cont))
45   (ecase (continuation-kind cont)
46     ((:block-start :deleted-block-start)
47      (let ((uses (block-start-uses (continuation-block cont))))
48        (if uses
49            (do ((res (node-derived-type (first uses))
50                      (values-type-union (node-derived-type (first current))
51                                         res))
52                 (current (rest uses) (rest current)))
53                ((null current) res))
54            *empty-type*)))
55     (:inside-block
56      (node-derived-type (continuation-use cont)))))
57
58 ;;; Our best guess for the type of this continuation's value. Note
59 ;;; that this may be VALUES or FUNCTION type, which cannot be passed
60 ;;; as an argument to the normal type operations. See
61 ;;; CONTINUATION-TYPE. This may be called on deleted continuations,
62 ;;; always returning *.
63 ;;;
64 ;;; What we do is call CONTINUATION-PROVEN-TYPE and check whether the
65 ;;; result is a subtype of the assertion. If so, return the proven
66 ;;; type and set TYPE-CHECK to nil. Otherwise, return the intersection
67 ;;; of the asserted and proven types, and set TYPE-CHECK T. If
68 ;;; TYPE-CHECK already has a non-null value, then preserve it. Only in
69 ;;; the somewhat unusual circumstance of a newly discovered assertion
70 ;;; will we change TYPE-CHECK from NIL to T.
71 ;;;
72 ;;; The result value is cached in the CONTINUATION-%DERIVED-TYPE slot.
73 ;;; If the slot is true, just return that value, otherwise recompute
74 ;;; and stash the value there.
75 #!-sb-fluid (declaim (inline continuation-derived-type))
76 (defun continuation-derived-type (cont)
77   (declare (type continuation cont))
78   (or (continuation-%derived-type cont)
79       (%continuation-derived-type cont)))
80 (defun %continuation-derived-type (cont)
81   (declare (type continuation cont))
82   (let ((proven (continuation-proven-type cont))
83         (asserted (continuation-asserted-type cont)))
84     (cond ((values-subtypep proven asserted)
85            (setf (continuation-%type-check cont) nil)
86            (setf (continuation-%derived-type cont) proven))
87           ((and (values-subtypep proven (specifier-type 'function))
88                 (values-subtypep asserted (specifier-type 'function)))
89            ;; It's physically impossible for a runtime type check to
90            ;; distinguish between the various subtypes of FUNCTION, so
91            ;; it'd be pointless to do more type checks here.
92            (setf (continuation-%type-check cont) nil)
93            (setf (continuation-%derived-type cont)
94                  ;; FIXME: This should depend on optimization
95                  ;; policy. This is for SPEED > SAFETY:
96                  #+nil (values-type-intersection asserted proven)
97                  ;; and this is for SAFETY >= SPEED:
98                  #-nil proven))
99           (t
100            (unless (or (continuation-%type-check cont)
101                        (not (continuation-dest cont))
102                        (eq asserted *universal-type*))
103              (setf (continuation-%type-check cont) t))
104
105            (setf (continuation-%derived-type cont)
106                  (values-type-intersection asserted proven))))))
107
108 ;;; Call CONTINUATION-DERIVED-TYPE to make sure the slot is up to
109 ;;; date, then return it.
110 #!-sb-fluid (declaim (inline continuation-type-check))
111 (defun continuation-type-check (cont)
112   (declare (type continuation cont))
113   (continuation-derived-type cont)
114   (continuation-%type-check cont))
115
116 ;;; Return the derived type for CONT's first value. This is guaranteed
117 ;;; not to be a VALUES or FUNCTION type.
118 (declaim (ftype (function (continuation) ctype) continuation-type))
119 (defun continuation-type (cont)
120   (single-value-type (continuation-derived-type cont)))
121
122 ;;; If CONT is an argument of a function, return a type which the
123 ;;; function checks CONT for.
124 #!-sb-fluid (declaim (inline continuation-externally-checkable-type))
125 (defun continuation-externally-checkable-type (cont)
126   (or (continuation-%externally-checkable-type cont)
127       (%continuation-%externally-checkable-type cont)))
128 (defun %continuation-%externally-checkable-type (cont)
129   (declare (type continuation cont))
130   (let ((dest (continuation-dest cont)))
131       (if (not (and dest (combination-p dest)))
132           ;; TODO: MV-COMBINATION
133           (setf (continuation-%externally-checkable-type cont) *wild-type*)
134           (let* ((fun (combination-fun dest))
135                  (args (combination-args dest))
136                  (fun-type (continuation-type fun)))
137             (if (or (not (fun-type-p fun-type))
138                     ;; FUN-TYPE might be (AND FUNCTION (SATISFIES ...)).
139                     (fun-type-wild-args fun-type))
140                 (progn (dolist (arg args)
141                          (when arg
142                            (setf (continuation-%externally-checkable-type arg)
143                                  *wild-type*)))
144                        *wild-type*)
145                 (let* ((arg-types (append (fun-type-required fun-type)
146                                           (fun-type-optional fun-type)
147                                           (let ((rest (list (or (fun-type-rest fun-type)
148                                                                 *wild-type*))))
149                                             (setf (cdr rest) rest)))))
150                   ;; TODO: &KEY
151                   (loop
152                      for arg of-type continuation in args
153                      and type of-type ctype in arg-types
154                      do (when arg
155                           (setf (continuation-%externally-checkable-type arg)
156                                 type)))
157                   (continuation-%externally-checkable-type cont)))))))
158 \f
159 ;;;; interface routines used by optimizers
160
161 ;;; This function is called by optimizers to indicate that something
162 ;;; interesting has happened to the value of Cont. Optimizers must
163 ;;; make sure that they don't call for reoptimization when nothing has
164 ;;; happened, since optimization will fail to terminate.
165 ;;;
166 ;;; We clear any cached type for the continuation and set the
167 ;;; reoptimize flags on everything in sight, unless the continuation
168 ;;; is deleted (in which case we do nothing.)
169 ;;;
170 ;;; Since this can get called during IR1 conversion, we have to be
171 ;;; careful not to fly into space when the Dest's Prev is missing.
172 (defun reoptimize-continuation (cont)
173   (declare (type continuation cont))
174   (unless (member (continuation-kind cont) '(:deleted :unused))
175     (setf (continuation-%derived-type cont) nil)
176     (let ((dest (continuation-dest cont)))
177       (when dest
178         (setf (continuation-reoptimize cont) t)
179         (setf (node-reoptimize dest) t)
180         (let ((prev (node-prev dest)))
181           (when prev
182             (let* ((block (continuation-block prev))
183                    (component (block-component block)))
184               (when (typep dest 'cif)
185                 (setf (block-test-modified block) t))
186               (setf (block-reoptimize block) t)
187               (setf (component-reoptimize component) t))))))
188     (do-uses (node cont)
189       (setf (block-type-check (node-block node)) t)))
190   (values))
191
192 ;;; Annotate NODE to indicate that its result has been proven to be
193 ;;; TYPEP to RTYPE. After IR1 conversion has happened, this is the
194 ;;; only correct way to supply information discovered about a node's
195 ;;; type. If you screw with the NODE-DERIVED-TYPE directly, then
196 ;;; information may be lost and reoptimization may not happen.
197 ;;;
198 ;;; What we do is intersect RTYPE with NODE's DERIVED-TYPE. If the
199 ;;; intersection is different from the old type, then we do a
200 ;;; REOPTIMIZE-CONTINUATION on the NODE-CONT.
201 (defun derive-node-type (node rtype)
202   (declare (type node node) (type ctype rtype))
203   (let ((node-type (node-derived-type node)))
204     (unless (eq node-type rtype)
205       (let ((int (values-type-intersection node-type rtype)))
206         (when (type/= node-type int)
207           (when (and *check-consistency*
208                      (eq int *empty-type*)
209                      (not (eq rtype *empty-type*)))
210             (let ((*compiler-error-context* node))
211               (compiler-warn
212                "New inferred type ~S conflicts with old type:~
213                 ~%  ~S~%*** possible internal error? Please report this."
214                (type-specifier rtype) (type-specifier node-type))))
215           (setf (node-derived-type node) int)
216           (reoptimize-continuation (node-cont node))))))
217   (values))
218
219 (defun set-continuation-type-assertion (cont atype ctype)
220   (declare (type continuation cont) (type ctype atype ctype))
221   (when (eq atype *wild-type*)
222     (return-from set-continuation-type-assertion))
223   (let* ((old-atype (continuation-asserted-type cont))
224          (old-ctype (continuation-type-to-check cont))
225          (new-atype (values-type-intersection old-atype atype))
226          (new-ctype (values-type-intersection old-ctype ctype)))
227     (when (or (type/= old-atype new-atype)
228               (type/= old-ctype new-ctype))
229       (setf (continuation-asserted-type cont) new-atype)
230       (setf (continuation-type-to-check cont) new-ctype)
231       (do-uses (node cont)
232         (setf (block-attributep (block-flags (node-block node))
233                                 type-check type-asserted)
234               t))
235       (reoptimize-continuation cont)))
236   (values))
237
238 ;;; This is similar to DERIVE-NODE-TYPE, but asserts that it is an
239 ;;; error for CONT's value not to be TYPEP to TYPE. If we improve the
240 ;;; assertion, we set TYPE-CHECK and TYPE-ASSERTED to guarantee that
241 ;;; the new assertion will be checked.
242 (defun assert-continuation-type (cont type policy)
243   (declare (type continuation cont) (type ctype type))
244   (when (eq type *wild-type*)
245     (return-from assert-continuation-type))
246   (set-continuation-type-assertion cont type (maybe-weaken-check type policy)))
247
248 ;;; Assert that CALL is to a function of the specified TYPE. It is
249 ;;; assumed that the call is legal and has only constants in the
250 ;;; keyword positions.
251 (defun assert-call-type (call type)
252   (declare (type combination call) (type fun-type type))
253   (derive-node-type call (fun-type-returns type))
254   (let ((args (combination-args call))
255         (policy (lexenv-policy (node-lexenv call))))
256     (dolist (req (fun-type-required type))
257       (when (null args) (return-from assert-call-type))
258       (let ((arg (pop args)))
259         (assert-continuation-type arg req policy)))
260     (dolist (opt (fun-type-optional type))
261       (when (null args) (return-from assert-call-type))
262       (let ((arg (pop args)))
263         (assert-continuation-type arg opt policy)))
264
265     (let ((rest (fun-type-rest type)))
266       (when rest
267         (dolist (arg args)
268           (assert-continuation-type arg rest policy))))
269
270     (dolist (key (fun-type-keywords type))
271       (let ((name (key-info-name key)))
272         (do ((arg args (cddr arg)))
273             ((null arg))
274           (when (eq (continuation-value (first arg)) name)
275             (assert-continuation-type
276              (second arg) (key-info-type key)
277              policy))))))
278   (values))
279 \f
280 ;;;; IR1-OPTIMIZE
281
282 ;;; Do one forward pass over COMPONENT, deleting unreachable blocks
283 ;;; and doing IR1 optimizations. We can ignore all blocks that don't
284 ;;; have the REOPTIMIZE flag set. If COMPONENT-REOPTIMIZE is true when
285 ;;; we are done, then another iteration would be beneficial.
286 (defun ir1-optimize (component)
287   (declare (type component component))
288   (setf (component-reoptimize component) nil)
289   (do-blocks (block component)
290     (cond
291       ;; We delete blocks when there is either no predecessor or the
292       ;; block is in a lambda that has been deleted. These blocks
293       ;; would eventually be deleted by DFO recomputation, but doing
294       ;; it here immediately makes the effect available to IR1
295       ;; optimization.
296       ((or (block-delete-p block)
297            (null (block-pred block)))
298        (delete-block block))
299       ((eq (functional-kind (block-home-lambda block)) :deleted)
300        ;; Preserve the BLOCK-SUCC invariant that almost every block has
301        ;; one successor (and a block with DELETE-P set is an acceptable
302        ;; exception).
303        (mark-for-deletion block)
304        (delete-block block))
305       (t
306        (loop
307           (let ((succ (block-succ block)))
308             (unless (and succ (null (rest succ)))
309               (return)))
310
311           (let ((last (block-last block)))
312             (typecase last
313               (cif
314                (flush-dest (if-test last))
315                (when (unlink-node last)
316                  (return)))
317               (exit
318                (when (maybe-delete-exit last)
319                  (return)))))
320
321           (unless (join-successor-if-possible block)
322             (return)))
323
324        (when (and (block-reoptimize block) (block-component block))
325          (aver (not (block-delete-p block)))
326          (ir1-optimize-block block))
327
328        (cond ((block-delete-p block)
329               (delete-block block))
330              ((and (block-flush-p block) (block-component block))
331               (flush-dead-code block))))))
332
333   (values))
334
335 ;;; Loop over the nodes in BLOCK, acting on (and clearing) REOPTIMIZE
336 ;;; flags.
337 ;;;
338 ;;; Note that although they are cleared here, REOPTIMIZE flags might
339 ;;; still be set upon return from this function, meaning that further
340 ;;; optimization is wanted (as a consequence of optimizations we did).
341 (defun ir1-optimize-block (block)
342   (declare (type cblock block))
343   ;; We clear the node and block REOPTIMIZE flags before doing the
344   ;; optimization, not after. This ensures that the node or block will
345   ;; be reoptimized if necessary.
346   (setf (block-reoptimize block) nil)
347   (do-nodes (node cont block :restart-p t)
348     (when (node-reoptimize node)
349       ;; As above, we clear the node REOPTIMIZE flag before optimizing.
350       (setf (node-reoptimize node) nil)
351       (typecase node
352         (ref)
353         (combination
354          ;; With a COMBINATION, we call PROPAGATE-FUN-CHANGE whenever
355          ;; the function changes, and call IR1-OPTIMIZE-COMBINATION if
356          ;; any argument changes.
357          (ir1-optimize-combination node))
358         (cif
359          (ir1-optimize-if node))
360         (creturn
361          ;; KLUDGE: We leave the NODE-OPTIMIZE flag set going into
362          ;; IR1-OPTIMIZE-RETURN, since IR1-OPTIMIZE-RETURN wants to
363          ;; clear the flag itself. -- WHN 2002-02-02, quoting original
364          ;; CMU CL comments
365          (setf (node-reoptimize node) t)
366          (ir1-optimize-return node))
367         (mv-combination
368          (ir1-optimize-mv-combination node))
369         (exit
370          ;; With an EXIT, we derive the node's type from the VALUE's
371          ;; type. We don't propagate CONT's assertion to the VALUE,
372          ;; since if we did, this would move the checking of CONT's
373          ;; assertion to the exit. This wouldn't work with CATCH and
374          ;; UWP, where the EXIT node is just a placeholder for the
375          ;; actual unknown exit.
376          (let ((value (exit-value node)))
377            (when value
378              (derive-node-type node (continuation-derived-type value)))))
379         (cset
380          (ir1-optimize-set node)))))
381   (values))
382
383 ;;; Try to join with a successor block. If we succeed, we return true,
384 ;;; otherwise false.
385 (defun join-successor-if-possible (block)
386   (declare (type cblock block))
387   (let ((next (first (block-succ block))))
388     (when (block-start next)
389       (let* ((last (block-last block))
390              (last-cont (node-cont last))
391              (next-cont (block-start next)))
392         (cond (;; We cannot combine with a successor block if:
393                (or
394                 ;; The successor has more than one predecessor.
395                 (rest (block-pred next))
396                 ;; The last node's CONT is also used somewhere else.
397                 (not (eq (continuation-use last-cont) last))
398                 ;; The successor is the current block (infinite loop).
399                 (eq next block)
400                 ;; The next block has a different cleanup, and thus
401                 ;; we may want to insert cleanup code between the
402                 ;; two blocks at some point.
403                 (not (eq (block-end-cleanup block)
404                          (block-start-cleanup next)))
405                 ;; The next block has a different home lambda, and
406                 ;; thus the control transfer is a non-local exit.
407                 (not (eq (block-home-lambda block)
408                          (block-home-lambda next))))
409                nil)
410               ;; Joining is easy when the successor's START
411               ;; continuation is the same from our LAST's CONT. 
412               ((eq last-cont next-cont)
413                (join-blocks block next)
414                t)
415               ;; If they differ, then we can still join when the last
416               ;; continuation has no next and the next continuation
417               ;; has no uses. 
418               ((and (null (block-start-uses next))
419                     (eq (continuation-kind last-cont) :inside-block))
420                ;; In this case, we replace the next
421                ;; continuation with the last before joining the blocks.
422                (let ((next-node (continuation-next next-cont)))
423                  ;; If NEXT-CONT does have a dest, it must be
424                  ;; unreachable, since there are no USES.
425                  ;; DELETE-CONTINUATION will mark the dest block as
426                  ;; DELETE-P [and also this block, unless it is no
427                  ;; longer backward reachable from the dest block.]
428                  (delete-continuation next-cont)
429                  (setf (node-prev next-node) last-cont)
430                  (setf (continuation-next last-cont) next-node)
431                  (setf (block-start next) last-cont)
432                  (join-blocks block next))
433                t)
434               (t
435                nil))))))
436
437 ;;; Join together two blocks which have the same ending/starting
438 ;;; continuation. The code in BLOCK2 is moved into BLOCK1 and BLOCK2
439 ;;; is deleted from the DFO. We combine the optimize flags for the two
440 ;;; blocks so that any indicated optimization gets done.
441 (defun join-blocks (block1 block2)
442   (declare (type cblock block1 block2))
443   (let* ((last (block-last block2))
444          (last-cont (node-cont last))
445          (succ (block-succ block2))
446          (start2 (block-start block2)))
447     (do ((cont start2 (node-cont (continuation-next cont))))
448         ((eq cont last-cont)
449          (when (eq (continuation-kind last-cont) :inside-block)
450            (setf (continuation-block last-cont) block1)))
451       (setf (continuation-block cont) block1))
452
453     (unlink-blocks block1 block2)
454     (dolist (block succ)
455       (unlink-blocks block2 block)
456       (link-blocks block1 block))
457
458     (setf (block-last block1) last)
459     (setf (continuation-kind start2) :inside-block))
460
461   (setf (block-flags block1)
462         (attributes-union (block-flags block1)
463                           (block-flags block2)
464                           (block-attributes type-asserted test-modified)))
465
466   (let ((next (block-next block2))
467         (prev (block-prev block2)))
468     (setf (block-next prev) next)
469     (setf (block-prev next) prev))
470
471   (values))
472
473 ;;; Delete any nodes in BLOCK whose value is unused and which have no
474 ;;; side effects. We can delete sets of lexical variables when the set
475 ;;; variable has no references.
476 (defun flush-dead-code (block)
477   (declare (type cblock block))
478   (do-nodes-backwards (node cont block)
479     (unless (continuation-dest cont)
480       (typecase node
481         (ref
482          (delete-ref node)
483          (unlink-node node))
484         (combination
485          (let ((info (combination-kind node)))
486            (when (fun-info-p info)
487              (let ((attr (fun-info-attributes info)))
488                (when (and (not (ir1-attributep attr call))
489                           ;; ### For now, don't delete potentially
490                           ;; flushable calls when they have the CALL
491                           ;; attribute. Someday we should look at the
492                           ;; functional args to determine if they have
493                           ;; any side effects.
494                           (if (policy node (= safety 3))
495                               (and (ir1-attributep attr flushable)
496                                    (every (lambda (arg)
497                                             ;; FIXME: when bug 203
498                                             ;; will be fixed, remove
499                                             ;; this check
500                                             (member (continuation-type-check arg)
501                                                     '(nil :deleted)))
502                                           (basic-combination-args node))
503                                    (valid-fun-use node
504                                                   (info :function :type
505                                                         (leaf-source-name (ref-leaf (continuation-use (basic-combination-fun node)))))
506                                                   :result-test #'always-subtypep
507                                                   :lossage-fun nil
508                                                   :unwinnage-fun nil))
509                               (ir1-attributep attr unsafely-flushable)))
510                  (flush-dest (combination-fun node))
511                  (dolist (arg (combination-args node))
512                    (flush-dest arg))
513                  (unlink-node node))))))
514         (mv-combination
515          (when (eq (basic-combination-kind node) :local)
516            (let ((fun (combination-lambda node)))
517              (when (dolist (var (lambda-vars fun) t)
518                      (when (or (leaf-refs var)
519                                (lambda-var-sets var))
520                        (return nil)))
521                (flush-dest (first (basic-combination-args node)))
522                (delete-let fun)))))
523         (exit
524          (let ((value (exit-value node)))
525            (when value
526              (flush-dest value)
527              (setf (exit-value node) nil))))
528         (cset
529          (let ((var (set-var node)))
530            (when (and (lambda-var-p var)
531                       (null (leaf-refs var)))
532              (flush-dest (set-value node))
533              (setf (basic-var-sets var)
534                    (delete node (basic-var-sets var)))
535              (unlink-node node)))))))
536
537   (setf (block-flush-p block) nil)
538   (values))
539 \f
540 ;;;; local call return type propagation
541
542 ;;; This function is called on RETURN nodes that have their REOPTIMIZE
543 ;;; flag set. It iterates over the uses of the RESULT, looking for
544 ;;; interesting stuff to update the TAIL-SET. If a use isn't a local
545 ;;; call, then we union its type together with the types of other such
546 ;;; uses. We assign to the RETURN-RESULT-TYPE the intersection of this
547 ;;; type with the RESULT's asserted type. We can make this
548 ;;; intersection now (potentially before type checking) because this
549 ;;; assertion on the result will eventually be checked (if
550 ;;; appropriate.)
551 ;;;
552 ;;; We call MAYBE-CONVERT-TAIL-LOCAL-CALL on each local non-MV
553 ;;; combination, which may change the succesor of the call to be the
554 ;;; called function, and if so, checks if the call can become an
555 ;;; assignment. If we convert to an assignment, we abort, since the
556 ;;; RETURN has been deleted.
557 (defun find-result-type (node)
558   (declare (type creturn node))
559   (let ((result (return-result node)))
560     (collect ((use-union *empty-type* values-type-union))
561       (do-uses (use result)
562         (cond ((and (basic-combination-p use)
563                     (eq (basic-combination-kind use) :local))
564                (aver (eq (lambda-tail-set (node-home-lambda use))
565                          (lambda-tail-set (combination-lambda use))))
566                (when (combination-p use)
567                  (when (nth-value 1 (maybe-convert-tail-local-call use))
568                    (return-from find-result-type (values)))))
569               (t
570                (use-union (node-derived-type use)))))
571       (let ((int (values-type-intersection
572                   (continuation-asserted-type result)
573                   (use-union))))
574         (setf (return-result-type node) int))))
575   (values))
576
577 ;;; Do stuff to realize that something has changed about the value
578 ;;; delivered to a return node. Since we consider the return values of
579 ;;; all functions in the tail set to be equivalent, this amounts to
580 ;;; bringing the entire tail set up to date. We iterate over the
581 ;;; returns for all the functions in the tail set, reanalyzing them
582 ;;; all (not treating Node specially.)
583 ;;;
584 ;;; When we are done, we check whether the new type is different from
585 ;;; the old TAIL-SET-TYPE. If so, we set the type and also reoptimize
586 ;;; all the continuations for references to functions in the tail set.
587 ;;; This will cause IR1-OPTIMIZE-COMBINATION to derive the new type as
588 ;;; the results of the calls.
589 (defun ir1-optimize-return (node)
590   (declare (type creturn node))
591   (let* ((tails (lambda-tail-set (return-lambda node)))
592          (funs (tail-set-funs tails)))
593     (collect ((res *empty-type* values-type-union))
594       (dolist (fun funs)
595         (let ((return (lambda-return fun)))
596           (when return
597             (when (node-reoptimize return)
598               (setf (node-reoptimize return) nil)
599               (find-result-type return))
600             (res (return-result-type return)))))
601
602       (when (type/= (res) (tail-set-type tails))
603         (setf (tail-set-type tails) (res))
604         (dolist (fun (tail-set-funs tails))
605           (dolist (ref (leaf-refs fun))
606             (reoptimize-continuation (node-cont ref)))))))
607
608   (values))
609 \f
610 ;;;; IF optimization
611
612 ;;; If the test has multiple uses, replicate the node when possible.
613 ;;; Also check whether the predicate is known to be true or false,
614 ;;; deleting the IF node in favor of the appropriate branch when this
615 ;;; is the case.
616 (defun ir1-optimize-if (node)
617   (declare (type cif node))
618   (let ((test (if-test node))
619         (block (node-block node)))
620
621     (when (and (eq (block-start block) test)
622                (eq (continuation-next test) node)
623                (rest (block-start-uses block)))
624       (do-uses (use test)
625         (when (immediately-used-p test use)
626           (convert-if-if use node)
627           (when (continuation-use test) (return)))))
628
629     (let* ((type (continuation-type test))
630            (victim
631             (cond ((constant-continuation-p test)
632                    (if (continuation-value test)
633                        (if-alternative node)
634                        (if-consequent node)))
635                   ((not (types-equal-or-intersect type (specifier-type 'null)))
636                    (if-alternative node))
637                   ((type= type (specifier-type 'null))
638                    (if-consequent node)))))
639       (when victim
640         (flush-dest test)
641         (when (rest (block-succ block))
642           (unlink-blocks block victim))
643         (setf (component-reanalyze (node-component node)) t)
644         (unlink-node node))))
645   (values))
646
647 ;;; Create a new copy of an IF node that tests the value of the node
648 ;;; USE. The test must have >1 use, and must be immediately used by
649 ;;; USE. NODE must be the only node in its block (implying that
650 ;;; block-start = if-test).
651 ;;;
652 ;;; This optimization has an effect semantically similar to the
653 ;;; source-to-source transformation:
654 ;;;    (IF (IF A B C) D E) ==>
655 ;;;    (IF A (IF B D E) (IF C D E))
656 ;;;
657 ;;; We clobber the NODE-SOURCE-PATH of both the original and the new
658 ;;; node so that dead code deletion notes will definitely not consider
659 ;;; either node to be part of the original source. One node might
660 ;;; become unreachable, resulting in a spurious note.
661 (defun convert-if-if (use node)
662   (declare (type node use) (type cif node))
663   (with-ir1-environment-from-node node
664     (let* ((block (node-block node))
665            (test (if-test node))
666            (cblock (if-consequent node))
667            (ablock (if-alternative node))
668            (use-block (node-block use))
669            (dummy-cont (make-continuation))
670            (new-cont (make-continuation))
671            (new-node (make-if :test new-cont
672                               :consequent cblock
673                               :alternative ablock))
674            (new-block (continuation-starts-block new-cont)))
675       (link-node-to-previous-continuation new-node new-cont)
676       (setf (continuation-dest new-cont) new-node)
677       (setf (continuation-%externally-checkable-type new-cont) nil)
678       (add-continuation-use new-node dummy-cont)
679       (setf (block-last new-block) new-node)
680
681       (unlink-blocks use-block block)
682       (delete-continuation-use use)
683       (add-continuation-use use new-cont)
684       (link-blocks use-block new-block)
685
686       (link-blocks new-block cblock)
687       (link-blocks new-block ablock)
688
689       (push "<IF Duplication>" (node-source-path node))
690       (push "<IF Duplication>" (node-source-path new-node))
691
692       (reoptimize-continuation test)
693       (reoptimize-continuation new-cont)
694       (setf (component-reanalyze *current-component*) t)))
695   (values))
696 \f
697 ;;;; exit IR1 optimization
698
699 ;;; This function attempts to delete an exit node, returning true if
700 ;;; it deletes the block as a consequence:
701 ;;; -- If the exit is degenerate (has no ENTRY), then we don't do
702 ;;;    anything, since there is nothing to be done.
703 ;;; -- If the exit node and its ENTRY have the same home lambda then
704 ;;;    we know the exit is local, and can delete the exit. We change
705 ;;;    uses of the Exit-Value to be uses of the original continuation,
706 ;;;    then unlink the node. If the exit is to a TR context, then we
707 ;;;    must do MERGE-TAIL-SETS on any local calls which delivered
708 ;;;    their value to this exit.
709 ;;; -- If there is no value (as in a GO), then we skip the value
710 ;;;    semantics.
711 ;;;
712 ;;; This function is also called by environment analysis, since it
713 ;;; wants all exits to be optimized even if normal optimization was
714 ;;; omitted.
715 (defun maybe-delete-exit (node)
716   (declare (type exit node))
717   (let ((value (exit-value node))
718         (entry (exit-entry node))
719         (cont (node-cont node)))
720     (when (and entry
721                (eq (node-home-lambda node) (node-home-lambda entry)))
722       (setf (entry-exits entry) (delete node (entry-exits entry)))
723       (prog1
724           (unlink-node node)
725         (when value
726           (collect ((merges))
727             (when (return-p (continuation-dest cont))
728               (do-uses (use value)
729                 (when (and (basic-combination-p use)
730                            (eq (basic-combination-kind use) :local))
731                   (merges use))))
732             (substitute-continuation-uses cont value)
733             (dolist (merge (merges))
734               (merge-tail-sets merge))))))))
735 \f
736 ;;;; combination IR1 optimization
737
738 ;;; Report as we try each transform?
739 #!+sb-show
740 (defvar *show-transforms-p* nil)
741
742 ;;; Do IR1 optimizations on a COMBINATION node.
743 (declaim (ftype (function (combination) (values)) ir1-optimize-combination))
744 (defun ir1-optimize-combination (node)
745   (when (continuation-reoptimize (basic-combination-fun node))
746     (propagate-fun-change node))
747   (let ((args (basic-combination-args node))
748         (kind (basic-combination-kind node)))
749     (case kind
750       (:local
751        (let ((fun (combination-lambda node)))
752          (if (eq (functional-kind fun) :let)
753              (propagate-let-args node fun)
754              (propagate-local-call-args node fun))))
755       ((:full :error)
756        (dolist (arg args)
757          (when arg
758            (setf (continuation-reoptimize arg) nil))))
759       (t
760        (dolist (arg args)
761          (when arg
762            (setf (continuation-reoptimize arg) nil)))
763
764        (let ((attr (fun-info-attributes kind)))
765          (when (and (ir1-attributep attr foldable)
766                     ;; KLUDGE: The next test could be made more sensitive,
767                     ;; only suppressing constant-folding of functions with
768                     ;; CALL attributes when they're actually passed
769                     ;; function arguments. -- WHN 19990918
770                     (not (ir1-attributep attr call))
771                     (every #'constant-continuation-p args)
772                     (continuation-dest (node-cont node))
773                     ;; Even if the function is foldable in principle,
774                     ;; it might be one of our low-level
775                     ;; implementation-specific functions. Such
776                     ;; functions don't necessarily exist at runtime on
777                     ;; a plain vanilla ANSI Common Lisp
778                     ;; cross-compilation host, in which case the
779                     ;; cross-compiler can't fold it because the
780                     ;; cross-compiler doesn't know how to evaluate it.
781                     #+sb-xc-host
782                     (fboundp (combination-fun-source-name node)))
783            (constant-fold-call node)
784            (return-from ir1-optimize-combination)))
785
786        (let ((fun (fun-info-derive-type kind)))
787          (when fun
788            (let ((res (funcall fun node)))
789              (when res
790                (derive-node-type node res)
791                (maybe-terminate-block node nil)))))
792
793        (let ((fun (fun-info-optimizer kind)))
794          (unless (and fun (funcall fun node))
795            (dolist (x (fun-info-transforms kind))
796              #!+sb-show 
797              (when *show-transforms-p*
798                (let* ((cont (basic-combination-fun node))
799                       (fname (continuation-fun-name cont t)))
800                  (/show "trying transform" x (transform-function x) "for" fname)))
801              (unless (ir1-transform node x)
802                #!+sb-show
803                (when *show-transforms-p*
804                  (/show "quitting because IR1-TRANSFORM result was NIL"))
805                (return))))))))
806
807   (values))
808
809 ;;; If CALL is to a function that doesn't return (i.e. return type is
810 ;;; NIL), then terminate the block there, and link it to the component
811 ;;; tail. We also change the call's CONT to be a dummy continuation to
812 ;;; prevent the use from confusing things.
813 ;;;
814 ;;; Except when called during IR1 [FIXME: What does this mean? Except
815 ;;; during IR1 conversion? What about IR1 optimization?], we delete
816 ;;; the continuation if it has no other uses. (If it does have other
817 ;;; uses, we reoptimize.)
818 ;;;
819 ;;; Termination on the basis of a continuation type assertion is
820 ;;; inhibited when:
821 ;;; -- The continuation is deleted (hence the assertion is spurious), or
822 ;;; -- We are in IR1 conversion (where THE assertions are subject to
823 ;;;    weakening.)
824 (defun maybe-terminate-block (call ir1-converting-not-optimizing-p)
825   (declare (type basic-combination call))
826   (let* ((block (node-block call))
827          (cont (node-cont call))
828          (tail (component-tail (block-component block)))
829          (succ (first (block-succ block))))
830     (unless (or (and (eq call (block-last block)) (eq succ tail))
831                 (block-delete-p block))
832       (when (or (and (eq (continuation-asserted-type cont) *empty-type*)
833                      (not (or ir1-converting-not-optimizing-p
834                               (eq (continuation-kind cont) :deleted))))
835                 (eq (node-derived-type call) *empty-type*))
836         (cond (ir1-converting-not-optimizing-p
837                (delete-continuation-use call)
838                (cond
839                 ((block-last block)
840                  (aver (and (eq (block-last block) call)
841                             (eq (continuation-kind cont) :block-start))))
842                 (t
843                  (setf (block-last block) call)
844                  (link-blocks block (continuation-starts-block cont)))))
845               (t
846                (node-ends-block call)
847                (delete-continuation-use call)
848                (if (eq (continuation-kind cont) :unused)
849                    (delete-continuation cont)
850                    (reoptimize-continuation cont))))
851         
852         (unlink-blocks block (first (block-succ block)))
853         (setf (component-reanalyze (block-component block)) t)
854         (aver (not (block-succ block)))
855         (link-blocks block tail)
856         (add-continuation-use call (make-continuation))
857         t))))
858
859 ;;; This is called both by IR1 conversion and IR1 optimization when
860 ;;; they have verified the type signature for the call, and are
861 ;;; wondering if something should be done to special-case the call. If
862 ;;; CALL is a call to a global function, then see whether it defined
863 ;;; or known:
864 ;;; -- If a DEFINED-FUN should be inline expanded, then convert
865 ;;;    the expansion and change the call to call it. Expansion is
866 ;;;    enabled if :INLINE or if SPACE=0. If the FUNCTIONAL slot is
867 ;;;    true, we never expand, since this function has already been
868 ;;;    converted. Local call analysis will duplicate the definition
869 ;;;    if necessary. We claim that the parent form is LABELS for
870 ;;;    context declarations, since we don't want it to be considered
871 ;;;    a real global function.
872 ;;; -- If it is a known function, mark it as such by setting the KIND.
873 ;;;
874 ;;; We return the leaf referenced (NIL if not a leaf) and the
875 ;;; FUN-INFO assigned.
876 ;;;
877 ;;; FIXME: The IR1-CONVERTING-NOT-OPTIMIZING-P argument is what the
878 ;;; old CMU CL code called IR1-P, without explanation. My (WHN
879 ;;; 2002-01-09) tentative understanding of it is that we can call this
880 ;;; operation either in initial IR1 conversion or in later IR1
881 ;;; optimization, and it tells which is which. But it would be good
882 ;;; for someone who really understands it to check whether this is
883 ;;; really right.
884 (defun recognize-known-call (call ir1-converting-not-optimizing-p)
885   (declare (type combination call))
886   (let* ((ref (continuation-use (basic-combination-fun call)))
887          (leaf (when (ref-p ref) (ref-leaf ref)))
888          (inlinep (if (defined-fun-p leaf)
889                       (defined-fun-inlinep leaf)
890                       :no-chance)))
891     (cond
892      ((eq inlinep :notinline) (values nil nil))
893      ((not (and (global-var-p leaf)
894                 (eq (global-var-kind leaf) :global-function)))
895       (values leaf nil))
896      ((and (ecase inlinep
897              (:inline t)
898              (:no-chance nil)
899              ((nil :maybe-inline) (policy call (zerop space))))
900            (defined-fun-p leaf)
901            (defined-fun-inline-expansion leaf)
902            (let ((fun (defined-fun-functional leaf)))
903              (or (not fun)
904                  (and (eq inlinep :inline) (functional-kind fun))))
905            (inline-expansion-ok call))
906       (flet (;; FIXME: Is this what the old CMU CL internal documentation
907              ;; called semi-inlining? A more descriptive name would
908              ;; be nice. -- WHN 2002-01-07
909              (frob ()
910                (let ((res (ir1-convert-lambda-for-defun
911                            (defined-fun-inline-expansion leaf)
912                            leaf t
913                            #'ir1-convert-inline-lambda)))
914                  (setf (defined-fun-functional leaf) res)
915                  (change-ref-leaf ref res))))
916         (if ir1-converting-not-optimizing-p
917             (frob)
918             (with-ir1-environment-from-node call
919               (frob)
920               (locall-analyze-component *current-component*))))
921
922       (values (ref-leaf (continuation-use (basic-combination-fun call)))
923               nil))
924      (t
925       (let ((info (info :function :info (leaf-source-name leaf))))
926         (if info
927             (values leaf (setf (basic-combination-kind call) info))
928             (values leaf nil)))))))
929
930 ;;; Check whether CALL satisfies TYPE. If so, apply the type to the
931 ;;; call, and do MAYBE-TERMINATE-BLOCK and return the values of
932 ;;; RECOGNIZE-KNOWN-CALL. If an error, set the combination kind and
933 ;;; return NIL, NIL. If the type is just FUNCTION, then skip the
934 ;;; syntax check, arg/result type processing, but still call
935 ;;; RECOGNIZE-KNOWN-CALL, since the call might be to a known lambda,
936 ;;; and that checking is done by local call analysis.
937 (defun validate-call-type (call type ir1-converting-not-optimizing-p)
938   (declare (type combination call) (type ctype type))
939   (cond ((not (fun-type-p type))
940          (aver (multiple-value-bind (val win)
941                    (csubtypep type (specifier-type 'function))
942                  (or val (not win))))
943          (recognize-known-call call ir1-converting-not-optimizing-p))
944         ((valid-fun-use call type
945                         :argument-test #'always-subtypep
946                         :result-test #'always-subtypep
947                         ;; KLUDGE: Common Lisp is such a dynamic
948                         ;; language that all we can do here in
949                         ;; general is issue a STYLE-WARNING. It
950                         ;; would be nice to issue a full WARNING
951                         ;; in the special case of of type
952                         ;; mismatches within a compilation unit
953                         ;; (as in section 3.2.2.3 of the spec)
954                         ;; but at least as of sbcl-0.6.11, we
955                         ;; don't keep track of whether the
956                         ;; mismatched data came from the same
957                         ;; compilation unit, so we can't do that.
958                         ;; -- WHN 2001-02-11
959                         ;;
960                         ;; FIXME: Actually, I think we could
961                         ;; issue a full WARNING if the call
962                         ;; violates a DECLAIM FTYPE.
963                         :lossage-fun #'compiler-style-warn
964                         :unwinnage-fun #'compiler-note)
965          (assert-call-type call type)
966          (maybe-terminate-block call ir1-converting-not-optimizing-p)
967          (recognize-known-call call ir1-converting-not-optimizing-p))
968         (t
969          (setf (combination-kind call) :error)
970          (values nil nil))))
971
972 ;;; This is called by IR1-OPTIMIZE when the function for a call has
973 ;;; changed. If the call is local, we try to LET-convert it, and
974 ;;; derive the result type. If it is a :FULL call, we validate it
975 ;;; against the type, which recognizes known calls, does inline
976 ;;; expansion, etc. If a call to a predicate in a non-conditional
977 ;;; position or to a function with a source transform, then we
978 ;;; reconvert the form to give IR1 another chance.
979 (defun propagate-fun-change (call)
980   (declare (type combination call))
981   (let ((*compiler-error-context* call)
982         (fun-cont (basic-combination-fun call)))
983     (setf (continuation-reoptimize fun-cont) nil)
984     (case (combination-kind call)
985       (:local
986        (let ((fun (combination-lambda call)))
987          (maybe-let-convert fun)
988          (unless (member (functional-kind fun) '(:let :assignment :deleted))
989            (derive-node-type call (tail-set-type (lambda-tail-set fun))))))
990       (:full
991        (multiple-value-bind (leaf info)
992            (validate-call-type call (continuation-type fun-cont) nil)
993          (cond ((functional-p leaf)
994                 (convert-call-if-possible
995                  (continuation-use (basic-combination-fun call))
996                  call))
997                ((not leaf))
998                ((and (leaf-has-source-name-p leaf)
999                      (or (info :function :source-transform (leaf-source-name leaf))
1000                          (and info
1001                               (ir1-attributep (fun-info-attributes info)
1002                                               predicate)
1003                               (let ((dest (continuation-dest (node-cont call))))
1004                                 (and dest (not (if-p dest)))))))
1005                 ;; FIXME: This SYMBOLP is part of a literal
1006                 ;; translation of a test in the old CMU CL
1007                 ;; source, and it's not quite clear what
1008                 ;; the old source meant. Did it mean "has a
1009                 ;; valid name"? Or did it mean "is an
1010                 ;; ordinary function name, not a SETF
1011                 ;; function"? Either way, the old CMU CL
1012                 ;; code probably didn't deal with SETF
1013                 ;; functions correctly, and neither does
1014                 ;; this new SBCL code, and that should be fixed.
1015                 (when (symbolp (leaf-source-name leaf))
1016                   (let ((dummies (make-gensym-list
1017                                   (length (combination-args call)))))
1018                     (transform-call call
1019                                     `(lambda ,dummies
1020                                       (,(leaf-source-name leaf)
1021                                        ,@dummies))
1022                                     (leaf-source-name leaf))))))))))
1023   (values))
1024 \f
1025 ;;;; known function optimization
1026
1027 ;;; Add a failed optimization note to FAILED-OPTIMZATIONS for NODE,
1028 ;;; FUN and ARGS. If there is already a note for NODE and TRANSFORM,
1029 ;;; replace it, otherwise add a new one.
1030 (defun record-optimization-failure (node transform args)
1031   (declare (type combination node) (type transform transform)
1032            (type (or fun-type list) args))
1033   (let* ((table (component-failed-optimizations *component-being-compiled*))
1034          (found (assoc transform (gethash node table))))
1035     (if found
1036         (setf (cdr found) args)
1037         (push (cons transform args) (gethash node table))))
1038   (values))
1039
1040 ;;; Attempt to transform NODE using TRANSFORM-FUNCTION, subject to the
1041 ;;; call type constraint TRANSFORM-TYPE. If we are inhibited from
1042 ;;; doing the transform for some reason and FLAME is true, then we
1043 ;;; make a note of the message in FAILED-OPTIMIZATIONS for IR1
1044 ;;; finalize to pick up. We return true if the transform failed, and
1045 ;;; thus further transformation should be attempted. We return false
1046 ;;; if either the transform succeeded or was aborted.
1047 (defun ir1-transform (node transform)
1048   (declare (type combination node) (type transform transform))
1049   (let* ((type (transform-type transform))
1050          (fun (transform-function transform))
1051          (constrained (fun-type-p type))
1052          (table (component-failed-optimizations *component-being-compiled*))
1053          (flame (if (transform-important transform)
1054                     (policy node (>= speed inhibit-warnings))
1055                     (policy node (> speed inhibit-warnings))))
1056          (*compiler-error-context* node))
1057     (cond ((or (not constrained)
1058                (valid-fun-use node type :strict-result t))
1059            (multiple-value-bind (severity args)
1060                (catch 'give-up-ir1-transform
1061                  (transform-call node
1062                                  (funcall fun node)
1063                                  (combination-fun-source-name node))
1064                  (values :none nil))
1065              (ecase severity
1066                (:none
1067                 (remhash node table)
1068                 nil)
1069                (:aborted
1070                 (setf (combination-kind node) :error)
1071                 (when args
1072                   (apply #'compiler-warn args))
1073                 (remhash node table)
1074                 nil)
1075                (:failure
1076                 (if args
1077                     (when flame
1078                       (record-optimization-failure node transform args))
1079                     (setf (gethash node table)
1080                           (remove transform (gethash node table) :key #'car)))
1081                 t)
1082                (:delayed
1083                  (remhash node table)
1084                  nil))))
1085           ((and flame
1086                 (valid-fun-use node
1087                                type
1088                                :argument-test #'types-equal-or-intersect
1089                                :result-test #'values-types-equal-or-intersect))
1090            (record-optimization-failure node transform type)
1091            t)
1092           (t
1093            t))))
1094
1095 ;;; When we don't like an IR1 transform, we throw the severity/reason
1096 ;;; and args. 
1097 ;;;
1098 ;;; GIVE-UP-IR1-TRANSFORM is used to throw out of an IR1 transform,
1099 ;;; aborting this attempt to transform the call, but admitting the
1100 ;;; possibility that this or some other transform will later succeed.
1101 ;;; If arguments are supplied, they are format arguments for an
1102 ;;; efficiency note.
1103 ;;;
1104 ;;; ABORT-IR1-TRANSFORM is used to throw out of an IR1 transform and
1105 ;;; force a normal call to the function at run time. No further
1106 ;;; optimizations will be attempted.
1107 ;;;
1108 ;;; DELAY-IR1-TRANSFORM is used to throw out of an IR1 transform, and
1109 ;;; delay the transform on the node until later. REASONS specifies
1110 ;;; when the transform will be later retried. The :OPTIMIZE reason
1111 ;;; causes the transform to be delayed until after the current IR1
1112 ;;; optimization pass. The :CONSTRAINT reason causes the transform to
1113 ;;; be delayed until after constraint propagation.
1114 ;;;
1115 ;;; FIXME: Now (0.6.11.44) that there are 4 variants of this (GIVE-UP,
1116 ;;; ABORT, DELAY/:OPTIMIZE, DELAY/:CONSTRAINT) and we're starting to
1117 ;;; do CASE operations on the various REASON values, it might be a
1118 ;;; good idea to go OO, representing the reasons by objects, using
1119 ;;; CLOS methods on the objects instead of CASE, and (possibly) using
1120 ;;; SIGNAL instead of THROW.
1121 (declaim (ftype (function (&rest t) nil) give-up-ir1-transform))
1122 (defun give-up-ir1-transform (&rest args)
1123   (throw 'give-up-ir1-transform (values :failure args)))
1124 (defun abort-ir1-transform (&rest args)
1125   (throw 'give-up-ir1-transform (values :aborted args)))
1126 (defun delay-ir1-transform (node &rest reasons)
1127   (let ((assoc (assoc node *delayed-ir1-transforms*)))
1128     (cond ((not assoc)
1129             (setf *delayed-ir1-transforms*
1130                     (acons node reasons *delayed-ir1-transforms*))
1131             (throw 'give-up-ir1-transform :delayed))
1132           ((cdr assoc)
1133             (dolist (reason reasons)
1134               (pushnew reason (cdr assoc)))
1135             (throw 'give-up-ir1-transform :delayed)))))
1136
1137 ;;; Clear any delayed transform with no reasons - these should have
1138 ;;; been tried in the last pass. Then remove the reason from the
1139 ;;; delayed transform reasons, and if any become empty then set
1140 ;;; reoptimize flags for the node. Return true if any transforms are
1141 ;;; to be retried.
1142 (defun retry-delayed-ir1-transforms (reason)
1143   (setf *delayed-ir1-transforms*
1144         (remove-if-not #'cdr *delayed-ir1-transforms*))
1145   (let ((reoptimize nil))
1146     (dolist (assoc *delayed-ir1-transforms*)
1147       (let ((reasons (remove reason (cdr assoc))))
1148         (setf (cdr assoc) reasons)
1149         (unless reasons
1150           (let ((node (car assoc)))
1151             (unless (node-deleted node)
1152               (setf reoptimize t)
1153               (setf (node-reoptimize node) t)
1154               (let ((block (node-block node)))
1155                 (setf (block-reoptimize block) t)
1156                 (setf (component-reoptimize (block-component block)) t)))))))
1157     reoptimize))
1158
1159 ;;; Take the lambda-expression RES, IR1 convert it in the proper
1160 ;;; environment, and then install it as the function for the call
1161 ;;; NODE. We do local call analysis so that the new function is
1162 ;;; integrated into the control flow.
1163 ;;;
1164 ;;; We require the original function source name in order to generate
1165 ;;; a meaningful debug name for the lambda we set up. (It'd be
1166 ;;; possible to do this starting from debug names as well as source
1167 ;;; names, but as of sbcl-0.7.1.5, there was no need for this
1168 ;;; generality, since source names are always known to our callers.)
1169 (defun transform-call (node res source-name)
1170   (declare (type combination node) (list res))
1171   (aver (and (legal-fun-name-p source-name)
1172              (not (eql source-name '.anonymous.))))
1173   (with-ir1-environment-from-node node
1174       (let ((new-fun (ir1-convert-inline-lambda
1175                       res
1176                       :debug-name (debug-namify "LAMBDA-inlined ~A"
1177                                                 (as-debug-name
1178                                                  source-name
1179                                                  "<unknown function>"))))
1180             (ref (continuation-use (combination-fun node))))
1181         (change-ref-leaf ref new-fun)
1182         (setf (combination-kind node) :full)
1183         (locall-analyze-component *current-component*)))
1184   (values))
1185
1186 ;;; Replace a call to a foldable function of constant arguments with
1187 ;;; the result of evaluating the form. We insert the resulting
1188 ;;; constant node after the call, stealing the call's continuation. We
1189 ;;; give the call a continuation with no DEST, which should cause it
1190 ;;; and its arguments to go away. If there is an error during the
1191 ;;; evaluation, we give a warning and leave the call alone, making the
1192 ;;; call a :ERROR call.
1193 ;;;
1194 ;;; If there is more than one value, then we transform the call into a
1195 ;;; VALUES form.
1196 (defun constant-fold-call (call)
1197   (let ((args (mapcar #'continuation-value (combination-args call)))
1198         (fun-name (combination-fun-source-name call)))
1199     (multiple-value-bind (values win)
1200         (careful-call fun-name
1201                       args
1202                       call
1203                       ;; Note: CMU CL had COMPILER-WARN here, and that
1204                       ;; seems more natural, but it's probably not.
1205                       ;;
1206                       ;; It's especially not while bug 173 exists:
1207                       ;; Expressions like
1208                       ;;   (COND (END
1209                       ;;          (UNLESS (OR UNSAFE? (<= END SIZE)))
1210                       ;;            ...))
1211                       ;; can cause constant-folding TYPE-ERRORs (in
1212                       ;; #'<=) when END can be proved to be NIL, even
1213                       ;; though the code is perfectly legal and safe
1214                       ;; because a NIL value of END means that the
1215                       ;; #'<= will never be executed.
1216                       ;;
1217                       ;; Moreover, even without bug 173,
1218                       ;; quite-possibly-valid code like
1219                       ;;   (COND ((NONINLINED-PREDICATE END)
1220                       ;;          (UNLESS (<= END SIZE))
1221                       ;;            ...))
1222                       ;; (where NONINLINED-PREDICATE is something the
1223                       ;; compiler can't do at compile time, but which
1224                       ;; turns out to make the #'<= expression
1225                       ;; unreachable when END=NIL) could cause errors
1226                       ;; when the compiler tries to constant-fold (<=
1227                       ;; END SIZE).
1228                       ;;
1229                       ;; So, with or without bug 173, it'd be 
1230                       ;; unnecessarily evil to do a full
1231                       ;; COMPILER-WARNING (and thus return FAILURE-P=T
1232                       ;; from COMPILE-FILE) for legal code, so we we
1233                       ;; use a wimpier COMPILE-STYLE-WARNING instead.
1234                       #'compiler-style-warn
1235                       "constant folding")
1236       (if (not win)
1237           (setf (combination-kind call) :error)
1238           (let ((dummies (make-gensym-list (length args))))
1239             (transform-call
1240              call
1241              `(lambda ,dummies
1242                 (declare (ignore ,@dummies))
1243                 (values ,@(mapcar (lambda (x) `',x) values)))
1244              fun-name)))))
1245   (values))
1246 \f
1247 ;;;; local call optimization
1248
1249 ;;; Propagate TYPE to LEAF and its REFS, marking things changed. If
1250 ;;; the leaf type is a function type, then just leave it alone, since
1251 ;;; TYPE is never going to be more specific than that (and
1252 ;;; TYPE-INTERSECTION would choke.)
1253 (defun propagate-to-refs (leaf type)
1254   (declare (type leaf leaf) (type ctype type))
1255   (let ((var-type (leaf-type leaf)))
1256     (unless (fun-type-p var-type)
1257       (let ((int (type-approx-intersection2 var-type type)))
1258         (when (type/= int var-type)
1259           (setf (leaf-type leaf) int)
1260           (dolist (ref (leaf-refs leaf))
1261             (derive-node-type ref int))))
1262       (values))))
1263
1264 ;;; Figure out the type of a LET variable that has sets. We compute
1265 ;;; the union of the initial value Type and the types of all the set
1266 ;;; values and to a PROPAGATE-TO-REFS with this type.
1267 (defun propagate-from-sets (var type)
1268   (collect ((res type type-union))
1269     (dolist (set (basic-var-sets var))
1270       (res (continuation-type (set-value set)))
1271       (setf (node-reoptimize set) nil))
1272     (propagate-to-refs var (res)))
1273   (values))
1274
1275 ;;; If a LET variable, find the initial value's type and do
1276 ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's
1277 ;;; type.
1278 (defun ir1-optimize-set (node)
1279   (declare (type cset node))
1280   (let ((var (set-var node)))
1281     (when (and (lambda-var-p var) (leaf-refs var))
1282       (let ((home (lambda-var-home var)))
1283         (when (eq (functional-kind home) :let)
1284           (let ((iv (let-var-initial-value var)))
1285             (setf (continuation-reoptimize iv) nil)
1286             (propagate-from-sets var (continuation-type iv)))))))
1287
1288   (derive-node-type node (continuation-type (set-value node)))
1289   (values))
1290
1291 ;;; Return true if the value of REF will always be the same (and is
1292 ;;; thus legal to substitute.)
1293 (defun constant-reference-p (ref)
1294   (declare (type ref ref))
1295   (let ((leaf (ref-leaf ref)))
1296     (typecase leaf
1297       ((or constant functional) t)
1298       (lambda-var
1299        (null (lambda-var-sets leaf)))
1300       (defined-fun
1301        (not (eq (defined-fun-inlinep leaf) :notinline)))
1302       #!+(and (not sb-fluid) (not sb-xc-host))
1303       (global-var
1304        (case (global-var-kind leaf)
1305          (:global-function (let ((name (leaf-source-name leaf)))
1306                              (when (consp name)
1307                                (aver (eq (first name) 'setf))
1308                                (setq name (second name)))
1309                              (eq (symbol-package name)
1310                                  *cl-package*))))))))
1311
1312 ;;; If we have a non-set LET var with a single use, then (if possible)
1313 ;;; replace the variable reference's CONT with the arg continuation.
1314 ;;; This is inhibited when:
1315 ;;; -- CONT has other uses, or
1316 ;;; -- CONT receives multiple values, or
1317 ;;; -- the reference is in a different environment from the variable, or
1318 ;;; -- either continuation has a funky TYPE-CHECK annotation.
1319 ;;; -- the continuations have incompatible assertions, so the new asserted type
1320 ;;;    would be NIL.
1321 ;;; -- the var's DEST has a different policy than the ARG's (think safety).
1322 ;;;
1323 ;;; We change the REF to be a reference to NIL with unused value, and
1324 ;;; let it be flushed as dead code. A side effect of this substitution
1325 ;;; is to delete the variable.
1326 (defun substitute-single-use-continuation (arg var)
1327   (declare (type continuation arg) (type lambda-var var))
1328   (let* ((ref (first (leaf-refs var)))
1329          (cont (node-cont ref))
1330          (cont-atype (continuation-asserted-type cont))
1331          (cont-ctype (continuation-type-to-check cont))
1332          (dest (continuation-dest cont)))
1333     (when (and (eq (continuation-use cont) ref)
1334                dest
1335                (not (typep dest '(or creturn exit mv-combination)))
1336                (eq (node-home-lambda ref)
1337                    (lambda-home (lambda-var-home var)))
1338                (member (continuation-type-check arg) '(t nil))
1339                (member (continuation-type-check cont) '(t nil))
1340                (not (eq (values-type-intersection
1341                          cont-atype
1342                          (continuation-asserted-type arg))
1343                         *empty-type*))
1344                (eq (lexenv-policy (node-lexenv dest))
1345                    (lexenv-policy (node-lexenv (continuation-dest arg)))))
1346       (aver (member (continuation-kind arg)
1347                     '(:block-start :deleted-block-start :inside-block)))
1348       (set-continuation-type-assertion arg cont-atype cont-ctype)
1349       (setf (node-derived-type ref) *wild-type*)
1350       (change-ref-leaf ref (find-constant nil))
1351       (substitute-continuation arg cont)
1352       (reoptimize-continuation arg)
1353       t)))
1354
1355 ;;; Delete a LET, removing the call and bind nodes, and warning about
1356 ;;; any unreferenced variables. Note that FLUSH-DEAD-CODE will come
1357 ;;; along right away and delete the REF and then the lambda, since we
1358 ;;; flush the FUN continuation.
1359 (defun delete-let (clambda)
1360   (declare (type clambda clambda))
1361   (aver (functional-letlike-p clambda))
1362   (note-unreferenced-vars clambda)
1363   (let ((call (let-combination clambda)))
1364     (flush-dest (basic-combination-fun call))
1365     (unlink-node call)
1366     (unlink-node (lambda-bind clambda))
1367     (setf (lambda-bind clambda) nil))
1368   (values))
1369
1370 ;;; This function is called when one of the arguments to a LET
1371 ;;; changes. We look at each changed argument. If the corresponding
1372 ;;; variable is set, then we call PROPAGATE-FROM-SETS. Otherwise, we
1373 ;;; consider substituting for the variable, and also propagate
1374 ;;; derived-type information for the arg to all the VAR's refs.
1375 ;;;
1376 ;;; Substitution is inhibited when the arg leaf's derived type isn't a
1377 ;;; subtype of the argument's asserted type. This prevents type
1378 ;;; checking from being defeated, and also ensures that the best
1379 ;;; representation for the variable can be used.
1380 ;;;
1381 ;;; Substitution of individual references is inhibited if the
1382 ;;; reference is in a different component from the home. This can only
1383 ;;; happen with closures over top level lambda vars. In such cases,
1384 ;;; the references may have already been compiled, and thus can't be
1385 ;;; retroactively modified.
1386 ;;;
1387 ;;; If all of the variables are deleted (have no references) when we
1388 ;;; are done, then we delete the LET.
1389 ;;;
1390 ;;; Note that we are responsible for clearing the
1391 ;;; CONTINUATION-REOPTIMIZE flags.
1392 (defun propagate-let-args (call fun)
1393   (declare (type combination call) (type clambda fun))
1394   (loop for arg in (combination-args call)
1395         and var in (lambda-vars fun) do
1396     (when (and arg (continuation-reoptimize arg))
1397       (setf (continuation-reoptimize arg) nil)
1398       (cond
1399        ((lambda-var-sets var)
1400         (propagate-from-sets var (continuation-type arg)))
1401        ((let ((use (continuation-use arg)))
1402           (when (ref-p use)
1403             (let ((leaf (ref-leaf use)))
1404               (when (and (constant-reference-p use)
1405                          (values-subtypep (leaf-type leaf)
1406                                           (continuation-asserted-type arg)))
1407                 (propagate-to-refs var (continuation-type arg))
1408                 (let ((use-component (node-component use)))
1409                   (substitute-leaf-if
1410                    (lambda (ref)
1411                      (cond ((eq (node-component ref) use-component)
1412                             t)
1413                            (t
1414                             (aver (lambda-toplevelish-p (lambda-home fun)))
1415                             nil)))
1416                    leaf var))
1417                 t)))))
1418        ((and (null (rest (leaf-refs var)))
1419              (substitute-single-use-continuation arg var)))
1420        (t
1421         (propagate-to-refs var (continuation-type arg))))))
1422
1423   (when (every #'null (combination-args call))
1424     (delete-let fun))
1425
1426   (values))
1427
1428 ;;; This function is called when one of the args to a non-LET local
1429 ;;; call changes. For each changed argument corresponding to an unset
1430 ;;; variable, we compute the union of the types across all calls and
1431 ;;; propagate this type information to the var's refs.
1432 ;;;
1433 ;;; If the function has an XEP, then we don't do anything, since we
1434 ;;; won't discover anything.
1435 ;;;
1436 ;;; We can clear the Continuation-Reoptimize flags for arguments in
1437 ;;; all calls corresponding to changed arguments in Call, since the
1438 ;;; only use in IR1 optimization of the Reoptimize flag for local call
1439 ;;; args is right here.
1440 (defun propagate-local-call-args (call fun)
1441   (declare (type combination call) (type clambda fun))
1442
1443   (unless (or (functional-entry-fun fun)
1444               (lambda-optional-dispatch fun))
1445     (let* ((vars (lambda-vars fun))
1446            (union (mapcar (lambda (arg var)
1447                             (when (and arg
1448                                        (continuation-reoptimize arg)
1449                                        (null (basic-var-sets var)))
1450                               (continuation-type arg)))
1451                           (basic-combination-args call)
1452                           vars))
1453            (this-ref (continuation-use (basic-combination-fun call))))
1454
1455       (dolist (arg (basic-combination-args call))
1456         (when arg
1457           (setf (continuation-reoptimize arg) nil)))
1458
1459       (dolist (ref (leaf-refs fun))
1460         (let ((dest (continuation-dest (node-cont ref))))
1461           (unless (or (eq ref this-ref) (not dest))
1462             (setq union
1463                   (mapcar (lambda (this-arg old)
1464                             (when old
1465                               (setf (continuation-reoptimize this-arg) nil)
1466                               (type-union (continuation-type this-arg) old)))
1467                           (basic-combination-args dest)
1468                           union)))))
1469
1470       (mapc (lambda (var type)
1471               (when type
1472                 (propagate-to-refs var type)))
1473             vars union)))
1474
1475   (values))
1476 \f
1477 ;;;; multiple values optimization
1478
1479 ;;; Do stuff to notice a change to a MV combination node. There are
1480 ;;; two main branches here:
1481 ;;;  -- If the call is local, then it is already a MV let, or should
1482 ;;;     become one. Note that although all :LOCAL MV calls must eventually
1483 ;;;     be converted to :MV-LETs, there can be a window when the call
1484 ;;;     is local, but has not been LET converted yet. This is because
1485 ;;;     the entry-point lambdas may have stray references (in other
1486 ;;;     entry points) that have not been deleted yet.
1487 ;;;  -- The call is full. This case is somewhat similar to the non-MV
1488 ;;;     combination optimization: we propagate return type information and
1489 ;;;     notice non-returning calls. We also have an optimization
1490 ;;;     which tries to convert MV-CALLs into MV-binds.
1491 (defun ir1-optimize-mv-combination (node)
1492   (ecase (basic-combination-kind node)
1493     (:local
1494      (let ((fun-cont (basic-combination-fun node)))
1495        (when (continuation-reoptimize fun-cont)
1496          (setf (continuation-reoptimize fun-cont) nil)
1497          (maybe-let-convert (combination-lambda node))))
1498      (setf (continuation-reoptimize (first (basic-combination-args node))) nil)
1499      (when (eq (functional-kind (combination-lambda node)) :mv-let)
1500        (unless (convert-mv-bind-to-let node)
1501          (ir1-optimize-mv-bind node))))
1502     (:full
1503      (let* ((fun (basic-combination-fun node))
1504             (fun-changed (continuation-reoptimize fun))
1505             (args (basic-combination-args node)))
1506        (when fun-changed
1507          (setf (continuation-reoptimize fun) nil)
1508          (let ((type (continuation-type fun)))
1509            (when (fun-type-p type)
1510              (derive-node-type node (fun-type-returns type))))
1511          (maybe-terminate-block node nil)
1512          (let ((use (continuation-use fun)))
1513            (when (and (ref-p use) (functional-p (ref-leaf use)))
1514              (convert-call-if-possible use node)
1515              (when (eq (basic-combination-kind node) :local)
1516                (maybe-let-convert (ref-leaf use))))))
1517        (unless (or (eq (basic-combination-kind node) :local)
1518                    (eq (continuation-fun-name fun) '%throw))
1519          (ir1-optimize-mv-call node))
1520        (dolist (arg args)
1521          (setf (continuation-reoptimize arg) nil))))
1522     (:error))
1523   (values))
1524
1525 ;;; Propagate derived type info from the values continuation to the
1526 ;;; vars.
1527 (defun ir1-optimize-mv-bind (node)
1528   (declare (type mv-combination node))
1529   (let ((arg (first (basic-combination-args node)))
1530         (vars (lambda-vars (combination-lambda node))))
1531     (multiple-value-bind (types nvals)
1532         (values-types (continuation-derived-type arg))
1533       (unless (eq nvals :unknown)
1534         (mapc (lambda (var type)
1535                 (if (basic-var-sets var)
1536                     (propagate-from-sets var type)
1537                     (propagate-to-refs var type)))
1538               vars
1539                 (append types
1540                         (make-list (max (- (length vars) nvals) 0)
1541                                    :initial-element (specifier-type 'null))))))
1542     (setf (continuation-reoptimize arg) nil))
1543   (values))
1544
1545 ;;; If possible, convert a general MV call to an MV-BIND. We can do
1546 ;;; this if:
1547 ;;; -- The call has only one argument, and
1548 ;;; -- The function has a known fixed number of arguments, or
1549 ;;; -- The argument yields a known fixed number of values.
1550 ;;;
1551 ;;; What we do is change the function in the MV-CALL to be a lambda
1552 ;;; that "looks like an MV bind", which allows
1553 ;;; IR1-OPTIMIZE-MV-COMBINATION to notice that this call can be
1554 ;;; converted (the next time around.) This new lambda just calls the
1555 ;;; actual function with the MV-BIND variables as arguments. Note that
1556 ;;; this new MV bind is not let-converted immediately, as there are
1557 ;;; going to be stray references from the entry-point functions until
1558 ;;; they get deleted.
1559 ;;;
1560 ;;; In order to avoid loss of argument count checking, we only do the
1561 ;;; transformation according to a known number of expected argument if
1562 ;;; safety is unimportant. We can always convert if we know the number
1563 ;;; of actual values, since the normal call that we build will still
1564 ;;; do any appropriate argument count checking.
1565 ;;;
1566 ;;; We only attempt the transformation if the called function is a
1567 ;;; constant reference. This allows us to just splice the leaf into
1568 ;;; the new function, instead of trying to somehow bind the function
1569 ;;; expression. The leaf must be constant because we are evaluating it
1570 ;;; again in a different place. This also has the effect of squelching
1571 ;;; multiple warnings when there is an argument count error.
1572 (defun ir1-optimize-mv-call (node)
1573   (let ((fun (basic-combination-fun node))
1574         (*compiler-error-context* node)
1575         (ref (continuation-use (basic-combination-fun node)))
1576         (args (basic-combination-args node)))
1577
1578     (unless (and (ref-p ref) (constant-reference-p ref)
1579                  args (null (rest args)))
1580       (return-from ir1-optimize-mv-call))
1581
1582     (multiple-value-bind (min max)
1583         (fun-type-nargs (continuation-type fun))
1584       (let ((total-nvals
1585              (multiple-value-bind (types nvals)
1586                  (values-types (continuation-derived-type (first args)))
1587                (declare (ignore types))
1588                (if (eq nvals :unknown) nil nvals))))
1589
1590         (when total-nvals
1591           (when (and min (< total-nvals min))
1592             (compiler-warn
1593              "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1594              at least ~R."
1595              total-nvals min)
1596             (setf (basic-combination-kind node) :error)
1597             (return-from ir1-optimize-mv-call))
1598           (when (and max (> total-nvals max))
1599             (compiler-warn
1600              "MULTIPLE-VALUE-CALL with ~R values when the function expects ~
1601              at most ~R."
1602              total-nvals max)
1603             (setf (basic-combination-kind node) :error)
1604             (return-from ir1-optimize-mv-call)))
1605
1606         (let ((count (cond (total-nvals)
1607                            ((and (policy node (zerop safety))
1608                                  (eql min max))
1609                             min)
1610                            (t nil))))
1611           (when count
1612             (with-ir1-environment-from-node node
1613               (let* ((dums (make-gensym-list count))
1614                      (ignore (gensym))
1615                      (fun (ir1-convert-lambda
1616                            `(lambda (&optional ,@dums &rest ,ignore)
1617                               (declare (ignore ,ignore))
1618                               (funcall ,(ref-leaf ref) ,@dums)))))
1619                 (change-ref-leaf ref fun)
1620                 (aver (eq (basic-combination-kind node) :full))
1621                 (locall-analyze-component *current-component*)
1622                 (aver (eq (basic-combination-kind node) :local)))))))))
1623   (values))
1624
1625 ;;; If we see:
1626 ;;;    (multiple-value-bind
1627 ;;;     (x y)
1628 ;;;     (values xx yy)
1629 ;;;      ...)
1630 ;;; Convert to:
1631 ;;;    (let ((x xx)
1632 ;;;       (y yy))
1633 ;;;      ...)
1634 ;;;
1635 ;;; What we actually do is convert the VALUES combination into a
1636 ;;; normal LET combination calling the original :MV-LET lambda. If
1637 ;;; there are extra args to VALUES, discard the corresponding
1638 ;;; continuations. If there are insufficient args, insert references
1639 ;;; to NIL.
1640 (defun convert-mv-bind-to-let (call)
1641   (declare (type mv-combination call))
1642   (let* ((arg (first (basic-combination-args call)))
1643          (use (continuation-use arg)))
1644     (when (and (combination-p use)
1645                (eq (continuation-fun-name (combination-fun use))
1646                    'values))
1647       (let* ((fun (combination-lambda call))
1648              (vars (lambda-vars fun))
1649              (vals (combination-args use))
1650              (nvars (length vars))
1651              (nvals (length vals)))
1652         (cond ((> nvals nvars)
1653                (mapc #'flush-dest (subseq vals nvars))
1654                (setq vals (subseq vals 0 nvars)))
1655               ((< nvals nvars)
1656                (with-ir1-environment-from-node use
1657                  (let ((node-prev (node-prev use)))
1658                    (setf (node-prev use) nil)
1659                    (setf (continuation-next node-prev) nil)
1660                    (collect ((res vals))
1661                      (loop as cont = (make-continuation use)
1662                            and prev = node-prev then cont
1663                            repeat (- nvars nvals)
1664                            do (reference-constant prev cont nil)
1665                               (res cont))
1666                      (setq vals (res)))
1667                    (link-node-to-previous-continuation use
1668                                                        (car (last vals)))))))
1669         (setf (combination-args use) vals)
1670         (flush-dest (combination-fun use))
1671         (let ((fun-cont (basic-combination-fun call)))
1672           (setf (continuation-dest fun-cont) use)
1673           (setf (combination-fun use) fun-cont)
1674           (setf (continuation-%externally-checkable-type fun-cont) nil))
1675         (setf (combination-kind use) :local)
1676         (setf (functional-kind fun) :let)
1677         (flush-dest (first (basic-combination-args call)))
1678         (unlink-node call)
1679         (when vals
1680           (reoptimize-continuation (first vals)))
1681         (propagate-to-args use fun))
1682       t)))
1683
1684 ;;; If we see:
1685 ;;;    (values-list (list x y z))
1686 ;;;
1687 ;;; Convert to:
1688 ;;;    (values x y z)
1689 ;;;
1690 ;;; In implementation, this is somewhat similar to
1691 ;;; CONVERT-MV-BIND-TO-LET. We grab the args of LIST and make them
1692 ;;; args of the VALUES-LIST call, flushing the old argument
1693 ;;; continuation (allowing the LIST to be flushed.)
1694 (defoptimizer (values-list optimizer) ((list) node)
1695   (let ((use (continuation-use list)))
1696     (when (and (combination-p use)
1697                (eq (continuation-fun-name (combination-fun use))
1698                    'list))
1699       (change-ref-leaf (continuation-use (combination-fun node))
1700                        (find-free-fun 'values "in a strange place"))
1701       (setf (combination-kind node) :full)
1702       (let ((args (combination-args use)))
1703         (dolist (arg args)
1704           (setf (continuation-dest arg) node)
1705           (setf (continuation-%externally-checkable-type arg) nil))
1706         (setf (combination-args use) nil)
1707         (flush-dest list)
1708         (setf (combination-args node) args))
1709       t)))
1710
1711 ;;; If VALUES appears in a non-MV context, then effectively convert it
1712 ;;; to a PROG1. This allows the computation of the additional values
1713 ;;; to become dead code.
1714 (deftransform values ((&rest vals) * * :node node)
1715   (when (typep (continuation-dest (node-cont node))
1716                '(or creturn exit mv-combination))
1717     (give-up-ir1-transform))
1718   (setf (node-derived-type node) *wild-type*)
1719   (if vals
1720       (let ((dummies (make-gensym-list (length (cdr vals)))))
1721         `(lambda (val ,@dummies)
1722            (declare (ignore ,@dummies))
1723            val))
1724       nil))