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

Contents of /src/compiler/ir1opt.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5