/[cmucl]/src/compiler/ir1opt.lisp
ViewVC logotype

Contents of /src/compiler/ir1opt.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5