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

Contents of /src/compiler/ir1opt.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9.2.1 - (hide annotations)
Tue Jun 12 23:45:00 1990 UTC (23 years, 10 months ago) by wlott
Changes since 1.9: +2 -0 lines
Merged trunk changes into MIPS branch.
1 wlott 1.1 ;;; -*- Package: C; Log: C.Log -*-
2     ;;;
3     ;;; **********************************************************************
4     ;;; This code was written as part of the Spice 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 Spice Lisp, please contact
7     ;;; Scott Fahlman (FAHLMAN@CMUC).
8     ;;; **********************************************************************
9     ;;;
10     ;;; This file implements the IR1 optimization phase of the compiler. IR1
11     ;;; optimization is a grab-bag of optimizations that don't make major changes
12     ;;; to the block-level control flow and don't use flow analysis. These
13     ;;; optimizations can mostly be classified as "meta-evaluation", but there is a
14     ;;; sizable top-down component as well.
15     ;;;
16     ;;; Written by Rob MacLachlan
17     ;;;
18     (in-package 'c)
19    
20     ;;;
21     ;;; A hashtable from combination nodes to things describing how an
22     ;;; optimization of the node failed. If the thing is a list, then it is format
23     ;;; arguments. If it is a type, then the type is a type that the call failed
24     ;;; to match.
25     ;;;
26     (defvar *failed-optimizations* (make-hash-table :test #'eq))
27    
28    
29     ;;;; Interface for obtaining results of constant folding:
30    
31     ;;; Constant-Continuation-P -- Interface
32     ;;;
33     ;;; Return true if the sole use of Cont is a reference to a constant leaf.
34     ;;;
35     (proclaim '(function constant-continuation-p (continuation) boolean))
36     (defun constant-continuation-p (cont)
37     (let ((use (continuation-use cont)))
38     (and (ref-p use)
39     (constant-p (ref-leaf use)))))
40    
41    
42     ;;; Continuation-Value -- Interface
43     ;;;
44     ;;; Return the constant value for a continuation whose only use is a
45     ;;; constant node.
46     ;;;
47     (proclaim '(function continuation-value (continuation) t))
48     (defun continuation-value (cont)
49     (constant-value (ref-leaf (continuation-use cont))))
50    
51    
52     ;;;; Interface for obtaining results of type inference:
53    
54     ;;; CONTINUATION-PROVEN-TYPE -- Interface
55     ;;;
56     ;;; Return a (possibly values) type that describes what we have proven about
57     ;;; the type of Cont without taking any type assertions into consideration.
58     ;;; This is just the union of the NODE-DERIVED-TYPE of all the uses. Most
59     ;;; often people use CONTINUATION-DERIVED-TYPE or CONTINUATION-TYPE instead of
60     ;;; using this function directly.
61     ;;;
62     (defun continuation-proven-type (cont)
63     (declare (type continuation cont))
64     (ecase (continuation-kind cont)
65     ((:block-start :deleted-block-start)
66     (let ((uses (block-start-uses (continuation-block cont))))
67     (if uses
68     (do ((res (node-derived-type (first uses))
69     (values-type-union (node-derived-type (first current))
70     res))
71     (current (rest uses) (rest current)))
72     ((null current) res))
73     *empty-type*)))
74     (:inside-block
75     (node-derived-type (continuation-use cont)))))
76    
77    
78     ;;; Continuation-Derived-Type -- Interface
79     ;;;
80     ;;; Our best guess for the type of this continuation's value. Note that
81     ;;; this may be Values or Function type, which cannot be passed as an argument
82     ;;; to the normal type operations. See Continuation-Type. This may be called
83     ;;; on deleted continuations, always returning *.
84     ;;;
85     ;;; What we do is call CONTINUATION-PROVEN-TYPE and check whether the result
86     ;;; is a subtype of the assertion. If so, return the proven type and set
87     ;;; TYPE-CHECK to nil. Otherwise, return the intersection of the asserted and
88     ;;; proven types, and set TYPE-CHECK T. If TYPE-CHECK already has a non-null
89     ;;; value, then preserve it. Only in the somewhat unusual circumstance of
90     ;;; a newly discovered assertion will we change TYPE-CHECK from NIL to T.
91     ;;;
92     ;;; The result value is cached in the Continuation-%Derived-Type. If the
93     ;;; slot is true, just return that value, otherwise recompute and stash the
94     ;;; value there.
95     ;;;
96     (proclaim '(inline continuation-derived-type))
97     (defun continuation-derived-type (cont)
98     (declare (type continuation cont))
99     (or (continuation-%derived-type cont)
100     (%continuation-derived-type cont)))
101     ;;;
102     (defun %continuation-derived-type (cont)
103     (declare (type continuation cont))
104     (let ((proven (continuation-proven-type cont))
105     (asserted (continuation-asserted-type cont)))
106     (cond ((values-subtypep proven asserted)
107     (setf (continuation-%type-check cont) nil)
108     (setf (continuation-%derived-type cont) proven))
109     (t
110     (unless (or (continuation-%type-check cont)
111     (not (continuation-dest cont))
112     (eq asserted *universal-type*))
113     (setf (continuation-%type-check cont) t))
114    
115     (setf (continuation-%derived-type cont)
116     (values-type-intersection asserted proven))))))
117    
118    
119     ;;; CONTINUATION-TYPE-CHECK -- Interface
120     ;;;
121     ;;; Call CONTINUATION-DERIVED-TYPE to make sure the slot is up to date, then
122     ;;; return it.
123     ;;;
124     (proclaim '(inline continuation-type-check))
125     (defun continuation-type-check (cont)
126     (declare (type continuation cont))
127     (continuation-derived-type cont)
128     (continuation-%type-check cont))
129    
130    
131     ;;; Continuation-Type -- Interface
132     ;;;
133     ;;; Return the derived type for Cont's first value. This is guaranteed not
134     ;;; to be a Values or Function type.
135     ;;;
136     (proclaim '(function continuation-type (continuation) type))
137     (defun continuation-type (cont)
138     (single-value-type (continuation-derived-type cont)))
139    
140    
141     ;;;; Interface routines used by optimizers:
142    
143     ;;; Reoptimize-Continuation -- Interface
144     ;;;
145     ;;; This function is called by optimizers to indicate that something
146     ;;; interesting has happened to the value of Cont. Optimizers must make sure
147     ;;; that they don't call for reoptimization when nothing has happened, since
148     ;;; optimization will fail to terminate.
149     ;;;
150     ;;; We clear any cached type for the continuation and set the reoptimize
151     ;;; flags on everything in sight, unless the continuation is deleted (in which
152     ;;; case we do nothing.)
153     ;;;
154     ;;; Since this can get called curing IR1 conversion, we have to be careful
155     ;;; not to fly into space when the Dest's Prev is missing.
156     ;;;
157     (defun reoptimize-continuation (cont)
158     (declare (type continuation cont))
159     (unless (eq (continuation-kind cont) :deleted)
160     (setf (continuation-%derived-type cont) nil)
161     (let ((dest (continuation-dest cont)))
162     (when dest
163     (setf (continuation-reoptimize cont) t)
164     (setf (node-reoptimize dest) t)
165     (let ((prev (node-prev dest)))
166     (when prev
167     (let* ((block (continuation-block prev))
168     (component (block-component block)))
169     (setf (block-reoptimize block) t)
170     (setf (component-reoptimize component) t))))))
171     (do-uses (node cont)
172     (setf (block-type-check (node-block node)) t)))
173     (undefined-value))
174    
175    
176     ;;; Derive-Node-Type -- Interface
177     ;;;
178     ;;; Annotate Node to indicate that its result has been proven to be typep to
179     ;;; RType. After IR1 conversion has happened, this is the only correct way to
180     ;;; supply information discovered about a node's type. If you fuck with the
181     ;;; Node-Derived-Type directly, then information may be lost and reoptimization
182     ;;; may not happen.
183     ;;;
184     ;;; What we do is intersect Rtype with Node's Derived-Type. If the
185     ;;; intersection is different from the old type, then we do a
186     ;;; Reoptimize-Continuation on the Node-Cont.
187     ;;;
188     (defun derive-node-type (node rtype)
189     (declare (type node node) (type ctype rtype))
190     (let ((node-type (node-derived-type node)))
191     (unless (eq node-type rtype)
192     (let ((int (values-type-intersection node-type rtype)))
193     (when (type/= node-type int)
194     (setf (node-derived-type node) int)
195     (reoptimize-continuation (node-cont node))))))
196     (undefined-value))
197    
198    
199     ;;; Assert-Continuation-Type -- Interface
200     ;;;
201     ;;; Similar to Derive-Node-Type, but asserts that it is an error for Cont's
202     ;;; value not to be typep to Type. If we improve the assertion, we set
203     ;;; BLOCK-TYPE-CHECK to guarantee that the new assertion will be checked.
204     ;;;
205     (defun assert-continuation-type (cont type)
206     (declare (type continuation cont) (type ctype type))
207     (let ((cont-type (continuation-asserted-type cont)))
208     (unless (eq cont-type type)
209     (let ((int (values-type-intersection cont-type type)))
210     (when (type/= cont-type int)
211     (setf (continuation-asserted-type cont) int)
212     (do-uses (node cont)
213     (let ((block (node-block node)))
214     (setf (block-type-check block) t)
215     (setf (block-type-asserted block) t)))
216     (reoptimize-continuation cont)))))
217     (undefined-value))
218    
219    
220     ;;; Assert-Call-Type -- Interface
221     ;;;
222     ;;; Assert that Call is to a function of the specified Type. It is assumed
223     ;;; that the call is legal and has only constants in the keyword positions.
224     ;;;
225     (defun assert-call-type (call type)
226     (declare (type combination call) (type function-type type))
227     (derive-node-type call (function-type-returns type))
228     (let ((args (combination-args call)))
229     (dolist (req (function-type-required type))
230     (when (null args) (return-from assert-call-type))
231     (let ((arg (pop args)))
232     (assert-continuation-type arg req)))
233     (dolist (opt (function-type-optional type))
234     (when (null args) (return-from assert-call-type))
235     (let ((arg (pop args)))
236     (assert-continuation-type arg opt)))
237    
238     (let ((rest (function-type-rest type)))
239     (when rest
240     (dolist (arg args)
241     (assert-continuation-type arg rest))))
242    
243     (dolist (key (function-type-keywords type))
244     (let ((name (key-info-name key)))
245     (do ((arg args (cddr arg)))
246     ((null arg))
247     (when (eq (continuation-value (first arg)) name)
248     (assert-continuation-type
249     (second arg) (key-info-type key)))))))
250     (undefined-value))
251    
252    
253     ;;; IR1-Optimize -- Interface
254     ;;;
255     ;;; Do one forward pass over Component, deleting unreachable blocks and
256     ;;; doing IR1 optimizations. We can ignore all blocks that don't have
257     ;;; Block-Reoptimize set. If Component-Reoptimize is true when we are done,
258     ;;; then another iteration would be beneficial.
259     ;;;
260     ;;; We delete blocks when there is either no predecessor or the block is in
261     ;;; a lambda that has been deleted. These blocks would eventually be deleted
262     ;;; by DFO recomputation, but doing it here immediately makes the effect
263     ;;; avaliable to IR1 optimization.
264     ;;;
265     (defun ir1-optimize (component)
266     (declare (type component component))
267     (setf (component-reoptimize component) nil)
268     (do-blocks (block component)
269     (cond
270     ((or (block-delete-p block)
271     (null (block-pred block))
272     (eq (functional-kind (block-lambda block)) :deleted))
273     (delete-block block))
274     (t
275     (loop
276     (let ((succ (block-succ block)))
277     (unless (and succ (null (rest succ)))
278     (return)))
279    
280     (let ((last (block-last block)))
281     (typecase last
282     (cif
283     (flush-dest (if-test last))
284     (when (unlink-node last) (return)))
285     (exit
286     (when (maybe-delete-exit last) (return)))))
287    
288     (unless (join-successor-if-possible block)
289     (return)))
290    
291     (when (and (block-reoptimize block)
292     (block-component block))
293     (assert (not (block-delete-p block)))
294     (ir1-optimize-block block))
295    
296     (when (and (block-flush-p block)
297     (block-component block))
298     (assert (not (block-delete-p block)))
299     (flush-dead-code block)))))
300    
301     (undefined-value))
302    
303    
304     ;;; IR1-Optimize-Block -- Internal
305     ;;;
306     ;;; Loop over the nodes in Block, looking for stuff that needs to be
307     ;;; optimized. We dispatch off of the type of each node with its reoptimize
308     ;;; flag set:
309     ;;; -- With a combination, we call Propagate-Function-Change whenever the
310     ;;; function changes, and call IR1-Optimize-Combination if any argument
311     ;;; changes.
312     ;;; -- With an Exit, we derive the node's type from the Value's type. We don't
313     ;;; propagate Cont's assertion to the Value, since if we did, this would
314     ;;; move the checking of Cont's assertion to the exit. This wouldn't work
315     ;;; with Catch and UWP, where the Exit node is just a placeholder for the
316     ;;; actual unknown exit.
317     ;;;
318     ;;; Note that we clear the node & block reoptimize flags *before* doing the
319     ;;; optimization. This ensures that the node or block will be reoptimized if
320     ;;; necessary. We leave the NODE-OPTIMIZE flag set doing into
321     ;;; IR1-OPTIMIZE-RETURN, since it wants to clear the flag itself.
322     ;;;
323     (defun ir1-optimize-block (block)
324     (declare (type cblock block))
325     (setf (block-reoptimize block) nil)
326     (do-nodes (node cont block)
327     (when (node-reoptimize node)
328     (setf (node-reoptimize node) nil)
329     (typecase node
330     (ref)
331     (combination
332     (when (continuation-reoptimize (basic-combination-fun node))
333     (propagate-function-change node))
334     (when (dolist (arg (basic-combination-args node) nil)
335     (when (and arg (continuation-reoptimize arg))
336     (return t)))
337     (ir1-optimize-combination node)))
338     (cif
339     (ir1-optimize-if node))
340     (creturn
341     (setf (node-reoptimize node) t)
342     (ir1-optimize-return node))
343 ram 1.6 (mv-combination
344     (when (and (eq (basic-combination-kind node) :local)
345     (continuation-reoptimize
346     (first (basic-combination-args node))))
347     (ir1-optimize-mv-bind node)))
348 wlott 1.1 (exit
349     (let ((value (exit-value node)))
350     (when value
351     (derive-node-type node (continuation-derived-type value)))))
352     (cset
353     (ir1-optimize-set node)))))
354     (undefined-value))
355    
356    
357     ;;; Join-Successor-If-Possible -- Internal
358     ;;;
359     ;;; We cannot combine with a successor block if:
360     ;;; 1] The successor has more than one predecessor.
361     ;;; 2] The last node's Cont is also used somewhere else.
362     ;;; 3] The successor is the current block (infinite loop).
363     ;;; 4] The next block has a different cleanup, and thus we may want to insert
364     ;;; cleanup code between the two blocks at some point.
365     ;;; 5] The next block has a different home lambda, and thus the control
366     ;;; transfer is a non-local exit.
367     ;;;
368     ;;; If we succeed, we return true, otherwise false.
369     ;;;
370     ;;; Joining is easy when the successor's Start continuation is the same from
371     ;;; our Last's Cont. If they differ, then we can still join when the last
372     ;;; continuation has no next and the next continuation has no uses. In this
373 ram 1.5 ;;; case, we replace the next continuation with the last before joining the
374 wlott 1.1 ;;; blocks.
375     ;;;
376     (defun join-successor-if-possible (block)
377     (declare (type cblock block))
378     (let ((next (first (block-succ block))))
379     (when (block-lambda next)
380     (let* ((last (block-last block))
381     (last-cont (node-cont last))
382     (next-cont (block-start next))
383     (cleanup (block-end-cleanup block))
384     (next-cleanup (block-start-cleanup next))
385     (lambda (block-lambda block))
386     (next-lambda (block-lambda next)))
387     (cond ((or (rest (block-pred next))
388 ram 1.5 (not (eq (continuation-use last-cont) last))
389 wlott 1.1 (eq next block)
390     (not (eq (lambda-home lambda) (lambda-home next-lambda)))
391     (not (eq (find-enclosing-cleanup cleanup)
392     (find-enclosing-cleanup next-cleanup))))
393     nil)
394 ram 1.5 ((eq last-cont next-cont)
395 wlott 1.1 (join-blocks block next)
396     t)
397 ram 1.5 ((and (null (block-start-uses next))
398     (eq (continuation-kind last-cont) :inside-block))
399     (let ((next-node (continuation-next next-cont)))
400     (assert (not (continuation-dest next-cont)))
401     (delete-continuation next-cont)
402     (setf (node-prev next-node) last-cont)
403     (setf (continuation-next last-cont) next-node)
404     (setf (block-start next) last-cont)
405     (join-blocks block next))
406 wlott 1.1 t)
407     (t
408     nil))))))
409    
410    
411     ;;; Join-Blocks -- Internal
412     ;;;
413     ;;; Join together two blocks which have the same ending/starting
414     ;;; continuation. The code in Block2 is moved into Block1 and Block2 is
415     ;;; deleted from the DFO. The End-Cleanup for Block1 is set to that for
416     ;;; Block2 so that we don't lose cleanup info. We combine the optimize flags
417     ;;; for the two blocks so that any indicated optimization gets done.
418     ;;;
419     (defun join-blocks (block1 block2)
420     (declare (type cblock block1 block2))
421     (let* ((last (block-last block2))
422     (last-cont (node-cont last))
423     (succ (block-succ block2))
424     (start2 (block-start block2)))
425     (do ((cont start2 (node-cont (continuation-next cont))))
426     ((eq cont last-cont)
427     (when (eq (continuation-kind last-cont) :inside-block)
428     (setf (continuation-block last-cont) block1)))
429     (setf (continuation-block cont) block1))
430    
431     (unlink-blocks block1 block2)
432     (dolist (block succ)
433     (unlink-blocks block2 block)
434     (link-blocks block1 block))
435    
436     (setf (block-last block1) last)
437     (setf (continuation-kind start2) :inside-block))
438    
439     (setf (block-end-cleanup block1) (block-end-cleanup block2))
440    
441 ram 1.8 (when (block-reoptimize block2)
442     (setf (block-reoptimize block1) t))
443     (when (block-flush-p block2)
444     (setf (block-flush-p block1) t))
445     (when (block-type-check block2)
446     (setf (block-type-check block1) t))
447     (assert (not (block-delete-p block2)))
448    
449 wlott 1.1 (setf (block-type-asserted block1) t)
450     (setf (block-test-modified block1) t)
451    
452     (let ((next (block-next block2))
453     (prev (block-prev block2)))
454     (setf (block-next prev) next)
455     (setf (block-prev next) prev))
456    
457     (undefined-value))
458    
459    
460     ;;;; Local call return type propagation:
461    
462     ;;; Find-Result-Type -- Internal
463     ;;;
464     ;;; This function is called on RETURN nodes that have their REOPTIMIZE flag
465     ;;; set. It iterates over the uses of the RESULT, looking for interesting
466     ;;; stuff to update the TAIL-SET:
467     ;;; -- If a use is a local call, then we check that the called function has
468     ;;; the tail set Tails. If we encounter any different tail set, we return
469     ;;; the second value true.
470     ;;; -- If a use isn't a local call, then we union its type together with the
471     ;;; types of other such uses. We assign to the RETURN-RESULT-TYPE the
472     ;;; intersection of this type with the RESULT's asserted type. We can make
473     ;;; this intersection now (potentially before type checking) because this
474     ;;; assertion on the result will eventually be checked (if appropriate.)
475     ;;;
476     (defun find-result-type (node tails)
477     (declare (type creturn node))
478     (let ((result (return-result node))
479     (retry nil))
480     (collect ((use-union *empty-type* values-type-union))
481     (do-uses (use result)
482     (if (and (basic-combination-p use)
483     (eq (basic-combination-kind use) :local))
484     (when (merge-tail-sets use tails)
485     (setq retry t))
486     (use-union (node-derived-type use))))
487     (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     ;;; If the called function has no tail set, then do nothing; if it doesn't
505     ;;; return, then it can't affect the callers value.
506     ;;;
507     (defun merge-tail-sets (call ret-set)
508     (declare (type basic-combination call) (type tail-set ret-set))
509     (let ((fun-set (lambda-tail-set (combination-lambda call))))
510     (when (and fun-set (not (eq ret-set fun-set)))
511     (let ((funs (tail-set-functions fun-set)))
512     (dolist (fun funs)
513     (setf (lambda-tail-set fun) ret-set))
514     (setf (tail-set-functions ret-set)
515     (nconc (tail-set-functions ret-set) funs)))
516     t)))
517    
518    
519     ;;; IR1-Optimize-Return -- Internal
520     ;;;
521     ;;; Do stuff to realize that something has changed about the value delivered
522     ;;; to a return node. Since we consider the return values of all functions in
523     ;;; the tail set to be equivalent, this amounts to bringing the entire tail set
524     ;;; up to date. We iterate over the returns for all the functions in the tail
525     ;;; set, reanalyzing them all (not treating Node specially.)
526     ;;;
527     ;;; During this iteration, we may discover new functions that should be
528     ;;; added to the tail set. If this happens, we restart the iteration over the
529     ;;; TAIL-SET-FUNCTIONS. Note that this really doesn't duplicate much work, as
530     ;;; we clear the NODE-REOPTIMIZE flags in the return nodes as we go, thus we
531     ;;; don't call FIND-RESULT-TYPE on any given return more than once.
532     ;;;
533     ;;; Restarting the iteration doesn't disturb the computation of the result
534     ;;; type RES, since we will just be adding more types to the union. (or when
535     ;;; we iterate over a return multiple times, unioning in the same type more
536     ;;; than once.)
537     ;;;
538     ;;; When we are done, we check if the new type is different from the old
539     ;;; TAIL-SET-TYPE. If so, we set the type and also reoptimize all the
540     ;;; continuations for references to functions in the tail set. This will
541     ;;; cause IR1-OPTIMIZE-COMBINATION to derive the new type as the results of the
542     ;;; calls.
543     ;;;
544     (defun ir1-optimize-return (node)
545     (declare (type creturn node))
546     (let ((tails (lambda-tail-set (return-lambda node))))
547     (collect ((res *empty-type* values-type-union))
548     (loop
549     (block RETRY
550     (let ((funs (tail-set-functions tails)))
551     (dolist (fun funs)
552     (let ((return (lambda-return fun)))
553     (when (node-reoptimize return)
554     (setf (node-reoptimize node) nil)
555     (when (find-result-type return tails) (return-from RETRY)))
556     (res (return-result-type return)))))
557     (return)))
558    
559     (when (type/= (res) (tail-set-type tails))
560     (setf (tail-set-type tails) (res))
561     (dolist (fun (tail-set-functions tails))
562     (dolist (ref (leaf-refs fun))
563     (reoptimize-continuation (node-cont ref)))))))
564    
565     (undefined-value))
566    
567    
568     ;;; IR1-Optimize-If -- Internal
569     ;;;
570     ;;; If the test has multiple uses, replicate the node when possible. Also
571     ;;; check if the predicate is known to be true or false, deleting the IF node
572     ;;; in favor of the appropriate branch when this is the case.
573     ;;;
574     (defun ir1-optimize-if (node)
575     (declare (type cif node))
576     (let ((test (if-test node))
577     (block (node-block node)))
578    
579     (when (and (eq (block-start block) test)
580     (eq (continuation-next test) node)
581     (rest (block-start-uses block)))
582     (do-uses (use test)
583     (when (immediately-used-p test use)
584     (convert-if-if use node)
585     (when (continuation-use test) (return)))))
586    
587     (let* ((type (continuation-type test))
588     (victim
589     (cond ((constant-continuation-p test)
590     (if (continuation-value test)
591     (if-alternative node)
592     (if-consequent node)))
593     ((not (types-intersect type *null-type*))
594     (if-alternative node))
595     ((type= type *null-type*)
596     (if-consequent node)))))
597     (when victim
598     (flush-dest test)
599     (when (rest (block-succ block))
600     (unlink-blocks block victim))
601     (setf (component-reanalyze (block-component (node-block node))) t)
602     (unlink-node node))))
603     (undefined-value))
604    
605    
606     ;;; Convert-If-If -- Internal
607     ;;;
608     ;;; Create a new copy of an IF Node that tests the value of the node Use.
609     ;;; The test must have >1 use, and must be immediately used by Use. Node must
610     ;;; be the only node in its block (implying that block-start = if-test).
611     ;;;
612     ;;; This optimization has an effect semantically similar to the
613     ;;; source-to-source transformation:
614     ;;; (IF (IF A B C) D E) ==>
615     ;;; (IF A (IF B D E) (IF C D E))
616     ;;;
617     (defun convert-if-if (use node)
618     (declare (type node use) (type cif node))
619     (with-ir1-environment node
620     (let* ((block (node-block node))
621     (test (if-test node))
622     (cblock (if-consequent node))
623     (ablock (if-alternative node))
624     (use-block (node-block use))
625     (dummy-cont (make-continuation))
626     (new-cont (make-continuation))
627     (new-node (make-if :test new-cont :source (node-source node)
628     :consequent cblock :alternative ablock))
629     (new-block (continuation-starts-block new-cont)))
630     (prev-link new-node new-cont)
631     (setf (continuation-dest new-cont) new-node)
632     (add-continuation-use new-node dummy-cont)
633     (setf (block-last new-block) new-node)
634    
635     (unlink-blocks use-block block)
636     (delete-continuation-use use)
637     (add-continuation-use use new-cont)
638     (link-blocks use-block new-block)
639    
640     (link-blocks new-block cblock)
641     (link-blocks new-block ablock)
642    
643     (reoptimize-continuation test)
644     (reoptimize-continuation new-cont)
645     (setf (component-reanalyze *current-component*) t)))
646     (undefined-value))
647    
648    
649     ;;;; Exit IR1 optimization:
650    
651     ;;; Maybe-Delete-Exit -- Interface
652     ;;;
653     ;;; This function attempts to delete an exit node, returning true if it
654     ;;; deletes the block as a consequence:
655     ;;; -- If the exit is degenerate (has no Entry), then we don't do anything,
656     ;;; since there is nothing to be done.
657     ;;; -- If the exit node and its Entry have the same home lambda then we know
658     ;;; the exit is local, and can delete the exit. We change uses of the
659     ;;; Exit-Value to be uses of the original continuation, then unlink the
660     ;;; node.
661     ;;; -- If there is no value (as in a GO), then we skip the value semantics.
662     ;;;
663     ;;; This function is also called by environment analysis, since it wants all
664     ;;; exits to be optimized even if normal optimization was omitted.
665     ;;;
666     (defun maybe-delete-exit (node)
667     (declare (type exit node))
668     (let ((value (exit-value node))
669     (entry (exit-entry node))
670     (cont (node-cont node)))
671     (when (and entry
672     (eq (lambda-home (block-lambda (node-block node)))
673     (lambda-home (block-lambda (node-block entry)))))
674     (prog1
675     (unlink-node node)
676     (when value
677     (substitute-continuation-uses cont value))))))
678    
679    
680     ;;;; Combination IR1 optimization:
681    
682     ;;; Ir1-Optimize-Combination -- Internal
683     ;;;
684     ;;; Do IR1 optimizations on a Combination node.
685     ;;;
686     (proclaim '(function ir1-optimize-combination (combination) void))
687     (defun ir1-optimize-combination (node)
688     (let ((args (basic-combination-args node))
689     (kind (basic-combination-kind node)))
690     (case kind
691     (:local
692     (let ((fun (combination-lambda node)))
693     (if (eq (functional-kind fun) :let)
694     (propagate-let-args node fun)
695     (propagate-local-call-args node fun))))
696     (:full
697     (dolist (arg args)
698     (when arg
699     (setf (continuation-reoptimize arg) nil))))
700     (t
701     (dolist (arg args)
702     (when arg
703     (setf (continuation-reoptimize arg) nil)))
704    
705     (let ((attr (function-info-attributes kind)))
706     (when (and (ir1-attributep attr foldable)
707     (not (ir1-attributep attr call))
708     (every #'constant-continuation-p args)
709     (continuation-dest (node-cont node)))
710     (constant-fold-call node)
711     (return-from ir1-optimize-combination)))
712    
713     (let ((fun (function-info-derive-type kind)))
714     (when fun
715     (let ((res (funcall fun node)))
716     (when res
717     (derive-node-type node res)))))
718    
719     (let ((fun (function-info-optimizer kind)))
720     (unless (and fun (funcall fun node))
721     (dolist (x (function-info-transforms kind))
722 wlott 1.2 (unless (ir1-transform node (car x) (cdr x))
723     (return))))))))
724 wlott 1.1
725     (undefined-value))
726    
727    
728     ;;; Recognize-Known-Call -- Interface
729     ;;;
730     ;;; If Call is a call to a known function, mark it as such by setting the
731     ;;; Kind. In addition to a direct check for the function name in the table, we
732     ;;; also must check for slot accessors. If the function is a slot accessor,
733     ;;; then we set the combination kind to the function info of %Slot-Setter or
734     ;;; %Slot-Accessor, as appropriate.
735     ;;;
736     (defun recognize-known-call (call)
737     (declare (type combination call))
738     (let* ((fun (basic-combination-fun call))
739     (name (continuation-function-name fun)))
740     (when name
741     (let ((info (info function info name)))
742     (cond (info
743     (setf (basic-combination-kind call) info))
744     ((slot-accessor-p (ref-leaf (continuation-use fun)))
745     (setf (basic-combination-kind call)
746     (info function info
747     (if (consp name)
748     '%slot-setter
749     '%slot-accessor))))))))
750     (undefined-value))
751    
752    
753     ;;; Propagate-Function-Change -- Internal
754     ;;;
755     ;;; Called by Ir1-Optimize when the function for a call has changed.
756     ;;; If the call is to a functional, then we attempt to convert it to a local
757     ;;; call, otherwise we check the call for legality with respect to the new
758     ;;; type; if it is illegal, we mark the Ref as :Notline and punt.
759     ;;;
760     ;;; If we do have a good type for the call, we propagate type information from
761     ;;; the type to the arg and result continuations. If we discover that the call
762     ;;; is to a known global function, then we mark the combination as known.
763     ;;;
764     (defun propagate-function-change (call)
765     (declare (type combination call))
766     (let* ((fun (combination-fun call))
767     (use (continuation-use fun))
768     (type (continuation-derived-type fun))
769     (*compiler-error-context* call))
770     (setf (continuation-reoptimize fun) nil)
771     (cond ((or (not (ref-p use))
772     (eq (ref-inlinep use) :notinline)))
773     ((functional-p (ref-leaf use))
774     (let ((leaf (ref-leaf use)))
775     (cond ((eq (combination-kind call) :local)
776     (let ((tail-set (lambda-tail-set leaf)))
777     (when tail-set
778     (derive-node-type
779     call (tail-set-type tail-set)))))
780     ((not (eq (ref-inlinep use) :notinline))
781     (convert-call-if-possible use call)
782     (maybe-let-convert leaf)))))
783     ((not (function-type-p type)))
784     ((valid-function-use call type
785     :argument-test #'always-subtypep
786     :result-test #'always-subtypep
787     :error-function #'compiler-warning
788     :warning-function #'compiler-note)
789     (assert-call-type call type)
790     (recognize-known-call call))
791     (t
792     (setf (ref-inlinep use) :notinline))))
793    
794     (undefined-value))
795    
796    
797     ;;;; Known function optimization:
798    
799     ;;; IR1-Transform -- Internal
800     ;;;
801     ;;; Attempt to transform Node using Function, subject to the call type
802     ;;; constraint Type. If we are inhibited from doing the transform for some
803     ;;; reason and Flame is true, then we make a note of the message in
804 wlott 1.2 ;;; *failed-optimizations* for IR1 finalize to pick up. We return true if
805     ;;; the transform failed, and thus further transformation should be
806     ;;; attempted. We return false if either the transform suceeded or was
807     ;;; aborted.
808 wlott 1.1 ;;;
809     (defun ir1-transform (node type fun)
810 wlott 1.2 (declare (type combination node) (type ctype type) (type function fun))
811 wlott 1.1 (let ((constrained (function-type-p type))
812     (flame (policy node (> speed brevity)))
813     (*compiler-error-context* node))
814     (cond ((or (not constrained)
815     (valid-function-use node type))
816     (multiple-value-bind
817     (severity args)
818     (catch 'give-up
819     (transform-call node (funcall fun node))
820     (remhash node *failed-optimizations*)
821     (values :none nil))
822     (ecase severity
823 wlott 1.2 (:none nil)
824 wlott 1.1 (:aborted
825     (setf (combination-kind node) :full)
826     (setf (ref-inlinep (continuation-use (combination-fun node)))
827     :notinline)
828     (when args
829 wlott 1.2 (apply #'compiler-warning args))
830     nil)
831 wlott 1.1 (:failure
832     (when (and flame args)
833 wlott 1.2 (setf (gethash node *failed-optimizations*) args))
834     t))))
835 wlott 1.1 ((and flame
836     (valid-function-use node type
837     :argument-test #'types-intersect
838     :result-test #'values-types-intersect))
839 wlott 1.2 (setf (gethash node *failed-optimizations*) type)
840 wlott 1.9.2.1 t)
841     (t
842 wlott 1.2 t))))
843 wlott 1.1
844    
845     ;;; GIVE-UP, ABORT-TRANSFORM -- Interface
846     ;;;
847     ;;; Just throw the severity and args...
848     ;;;
849     (proclaim '(function give-up (&rest t) nil))
850     (defun give-up (&rest args)
851     "This function is used to throw out of an IR1 transform, aborting this
852     attempt to transform the call, but admitting the possibility that this or
853     some other transform will later suceed. If arguments are supplied, they are
854     format arguments for an efficiency note."
855     (throw 'give-up (values :failure args)))
856     ;;;
857     (defun abort-transform (&rest args)
858     "This function is used to throw out of an IR1 transform and force a normal
859     call to the function at run time. No further optimizations will be
860     attempted."
861     (throw 'give-up (values :aborted args)))
862    
863    
864     ;;; Transform-Call -- Internal
865     ;;;
866     ;;; Take the lambda-expression Res, IR1 convert it in the proper
867     ;;; environment, and then install it as the function for the call Node. We do
868     ;;; local call analysis so that the new function is integrated into the control
869     ;;; flow. We set the Reanalyze flag in the component to cause the DFO to be
870     ;;; recomputed at soonest convenience.
871     ;;;
872     (defun transform-call (node res)
873     (declare (type combination node) (list res))
874     (with-ir1-environment node
875     (let ((new-fun (ir1-convert-lambda res (node-source node)))
876     (ref (continuation-use (combination-fun node))))
877     (change-ref-leaf ref new-fun)
878     (setf (combination-kind node) :full)
879     (local-call-analyze *current-component*)))
880     (undefined-value))
881    
882    
883     ;;; Constant-Fold-Call -- Internal
884     ;;;
885     ;;; Replace a call to a foldable function of constant arguments with the
886     ;;; result of evaluating the form. We insert the resulting constant node after
887     ;;; the call, stealing the call's continuation. We give the call a
888     ;;; continuation with no Dest, which should cause it and its arguments to go
889     ;;; away. If there is an error during the evaluation, we give a warning and
890     ;;; leave the call alone, making the call a full call and marking it as
891     ;;; :notinline to make sure that it stays that way.
892     ;;;
893     ;;; For now, if the result is other than one value, we don't fold it.
894     ;;;
895     (defun constant-fold-call (call)
896     (declare (type combination call))
897     (let* ((args (mapcar #'continuation-value (combination-args call)))
898     (ref (continuation-use (combination-fun call)))
899     (fun (leaf-name (ref-leaf ref))))
900    
901     (multiple-value-bind (values win)
902     (careful-call fun args call "constant folding")
903     (cond
904     ((not win)
905     (setf (ref-inlinep ref) :notinline)
906     (setf (combination-kind call) :full))
907     ((= (length values) 1)
908     (with-ir1-environment call
909     (let* ((leaf (find-constant (first values)))
910     (node (make-ref (leaf-type leaf)
911     (node-source call)
912     leaf
913     nil))
914     (dummy (make-continuation))
915     (cont (node-cont call))
916     (block (node-block call))
917     (next (continuation-next cont)))
918     (push node (leaf-refs leaf))
919     (setf (leaf-ever-used leaf) t)
920    
921     (delete-continuation-use call)
922     (add-continuation-use call dummy)
923     (prev-link node dummy)
924     (add-continuation-use node cont)
925     (setf (continuation-next cont) next)
926     (when (eq call (block-last block))
927     (setf (block-last block) node))
928     (reoptimize-continuation cont)))))))
929    
930     (undefined-value))
931    
932    
933     ;;;; Local call optimization:
934    
935     ;;; Propagate-To-Refs -- Internal
936     ;;;
937     ;;; Propagate Type to Leaf and its Refs, marking things changed. If the
938     ;;; leaf type is a function type, then just leave it alone, since TYPE is never
939     ;;; going to be more specific than that (and TYPE-INTERSECTION would choke.)
940     ;;;
941     (defun propagate-to-refs (leaf type)
942     (declare (type leaf leaf) (type ctype type))
943     (let ((var-type (leaf-type leaf)))
944     (unless (function-type-p var-type)
945     (let ((int (type-intersection var-type type)))
946     (when (type/= int var-type)
947     (setf (leaf-type leaf) int)
948     (dolist (ref (leaf-refs leaf))
949     (derive-node-type ref int))))
950     (undefined-value))))
951    
952    
953     ;;; PROPAGATE-FROM-SETS -- Internal
954     ;;;
955     ;;; Figure out the type of a LET variable that has sets. We compute the
956     ;;; union of the initial value Type and the types of all the set values and to
957     ;;; a PROPAGATE-TO-REFS with this type.
958     ;;;
959     (defun propagate-from-sets (var type)
960 ram 1.6 (collect ((res type type-union))
961 wlott 1.1 (dolist (set (basic-var-sets var))
962     (res (continuation-type (set-value set)))
963     (setf (node-reoptimize set) nil))
964     (propagate-to-refs var (res)))
965     (undefined-value))
966    
967    
968     ;;; IR1-OPTIMIZE-SET -- Internal
969     ;;;
970     ;;; If a let variable, find the initial value's type and do
971     ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's type.
972     ;;;
973     (defun ir1-optimize-set (node)
974     (declare (type cset node))
975     (let ((var (set-var node)))
976     (when (and (lambda-var-p var) (leaf-refs var))
977     (let ((home (lambda-var-home var)))
978     (when (eq (functional-kind home) :let)
979     (let ((iv (let-var-initial-value var)))
980     (setf (continuation-reoptimize iv) nil)
981     (propagate-from-sets var (continuation-type iv)))))))
982    
983     (derive-node-type node (continuation-type (set-value node)))
984     (undefined-value))
985    
986    
987 ram 1.7 ;;; CONSTANT-REFERENCE-P -- Internal
988     ;;;
989     ;;; Return true if the value of Ref will always be the same (and is thus
990     ;;; legal to substitute.)
991     ;;;
992     (defun constant-reference-p (ref)
993     (declare (type ref ref))
994     (let ((leaf (ref-leaf ref)))
995     (typecase leaf
996     (constant t)
997     (functional t)
998     (lambda-var
999     (null (lambda-var-sets leaf)))
1000     (global-var
1001     (case (global-var-kind leaf)
1002     (:global-function
1003     (not (eq (ref-inlinep ref) :notinline)))
1004     (:constant t))))))
1005    
1006    
1007     ;;; SUBSTITUTE-SINGLE-USE-CONTINUATION -- Internal
1008     ;;;
1009     ;;; If we have a non-set let var with a single use, then (if possible)
1010     ;;; replace the variable reference's CONT with the arg continuation. This is
1011     ;;; inhibited when:
1012     ;;; -- CONT has other uses, or
1013     ;;; -- CONT receives multiple values, or
1014 ram 1.9 ;;; -- the reference is in a different environment from the variable, or
1015     ;;; -- either continuation has a funky TYPE-CHECK annotation.
1016 ram 1.7 ;;;
1017     ;;; We change the Ref to be a reference to NIL with unused value, and let it
1018     ;;; be flushed as dead code. A side-effect of this substitution is to delete
1019     ;;; the variable.
1020     ;;;
1021     (defun substitute-single-use-continuation (arg var)
1022     (declare (type continuation arg) (type lambda-var var))
1023     (let* ((ref (first (leaf-refs var)))
1024     (cont (node-cont ref))
1025     (dest (continuation-dest cont)))
1026     (when (and (eq (continuation-use cont) ref)
1027     dest
1028     (not (typep dest '(or creturn exit mv-combination)))
1029     (eq (lambda-home (block-lambda (node-block ref)))
1030 ram 1.9 (lambda-home (lambda-var-home var)))
1031     (member (continuation-type-check arg) '(t nil))
1032     (member (continuation-type-check cont) '(t nil)))
1033 ram 1.7 (assert-continuation-type arg (continuation-asserted-type cont))
1034     (change-ref-leaf ref (find-constant nil))
1035     (substitute-continuation arg cont)
1036     (reoptimize-continuation arg)
1037     t)))
1038    
1039    
1040 wlott 1.1 ;;; Propagate-Let-Args -- Internal
1041     ;;;
1042     ;;; This function is called when one of the arguments to a LET changes. We
1043     ;;; look at each changed argument. If the corresponding variable is set, then
1044     ;;; we call PROPAGATE-FROM-SETS. Otherwise, we consider substituting for the
1045     ;;; variable, and also propagate derived-type information for the arg to all
1046     ;;; the Var's refs.
1047     ;;;
1048     ;;; Substitution is inhibited when the Ref's derived type isn't a subtype of
1049     ;;; the argument's asserted type. This prevents type checking from being
1050     ;;; defeated, and also ensures that the best representation for the variable
1051     ;;; can be used.
1052     ;;;
1053     ;;; Note that we are responsible for clearing the Continuation-Reoptimize
1054     ;;; flags.
1055     ;;;
1056     (defun propagate-let-args (call fun)
1057     (declare (type combination call) (type clambda fun))
1058     (mapc #'(lambda (arg var)
1059     (when (and arg
1060     (continuation-reoptimize arg))
1061     (setf (continuation-reoptimize arg) nil)
1062     (cond
1063     ((lambda-var-sets var)
1064     (propagate-from-sets var (continuation-type arg)))
1065 ram 1.7 ((let ((use (continuation-use arg)))
1066 wlott 1.1 (when (ref-p use)
1067     (let ((leaf (ref-leaf use)))
1068 ram 1.7 (when (and (constant-reference-p use)
1069 wlott 1.1 (values-subtypep
1070     (node-derived-type use)
1071     (continuation-asserted-type arg)))
1072 ram 1.7 (substitute-leaf leaf var)
1073     (propagate-to-refs var (continuation-type arg))
1074     t)))))
1075     ((and (null (rest (leaf-refs var)))
1076     (substitute-single-use-continuation arg var)))
1077     (t
1078 wlott 1.1 (propagate-to-refs var (continuation-type arg))))))
1079     (basic-combination-args call)
1080     (lambda-vars fun))
1081     (undefined-value))
1082    
1083    
1084     ;;; Propagate-Local-Call-Args -- Internal
1085     ;;;
1086     ;;; This function is called when one of the args to a non-let local call
1087     ;;; changes. For each changed argument corresponding to an unset variable, we
1088     ;;; compute the union of the types across all calls and propagate this type
1089     ;;; information to the var's refs.
1090     ;;;
1091     ;;; If the function has an XEP, then we don't do anything, since we won't
1092     ;;; discover anything.
1093     ;;;
1094     ;;; We can clear the Continuation-Reoptimize flags for arguments in all calls
1095     ;;; corresponding to changed arguments in Call, since the only use in IR1
1096     ;;; optimization of the Reoptimize flag for local call args is right here.
1097     ;;;
1098     (defun propagate-local-call-args (call fun)
1099     (declare (type combination call) (type clambda fun))
1100    
1101     (unless (functional-entry-function fun)
1102     (let* ((vars (lambda-vars fun))
1103     (union (mapcar #'(lambda (arg var)
1104     (when (and arg
1105     (continuation-reoptimize arg)
1106     (null (basic-var-sets var)))
1107     (continuation-type arg)))
1108     (basic-combination-args call)
1109     vars))
1110     (this-ref (continuation-use (basic-combination-fun call))))
1111    
1112     (dolist (arg (basic-combination-args call))
1113     (when arg
1114     (setf (continuation-reoptimize arg) nil)))
1115    
1116     (dolist (ref (leaf-refs fun))
1117     (unless (eq ref this-ref)
1118     (setq union
1119     (mapcar #'(lambda (this-arg old)
1120     (when old
1121     (setf (continuation-reoptimize this-arg) nil)
1122     (type-union (continuation-type this-arg) old)))
1123     (basic-combination-args
1124     (continuation-dest (node-cont ref)))
1125     union))))
1126    
1127     (mapc #'(lambda (var type)
1128     (when type
1129     (propagate-to-refs var type)))
1130     vars union)))
1131    
1132 ram 1.6 (undefined-value))
1133    
1134    
1135     ;;; IR1-OPTIMIZE-MV-BIND -- Internal
1136     ;;;
1137     ;;; Propagate derived type info from the values continuation to the vars.
1138     ;;;
1139     (defun ir1-optimize-mv-bind (node)
1140     (declare (type mv-combination node))
1141     (let ((arg (first (basic-combination-args node)))
1142     (vars (lambda-vars (combination-lambda node))))
1143     (multiple-value-bind (types nvals)
1144     (values-types (continuation-derived-type arg))
1145     (unless (eq nvals :unknown)
1146     (mapc #'(lambda (var type)
1147     (if (basic-var-sets var)
1148     (propagate-from-sets var type)
1149     (propagate-to-refs var type)))
1150     vars
1151     (append types
1152     (make-list (max (- (length vars) nvals) 0)
1153     :initial-element *null-type*)))))
1154    
1155     (setf (continuation-reoptimize arg) nil))
1156 wlott 1.1 (undefined-value))
1157    
1158    
1159     ;;; Flush-Dead-Code -- Internal
1160     ;;;
1161     ;;; Delete any nodes in Block whose value is unused and have no
1162     ;;; side-effects. We can delete sets of lexical variables when the set
1163     ;;; variable has no references.
1164     ;;;
1165     ;;; [### For now, don't delete potentially flushable calls when they have the
1166     ;;; Call attribute. Someday we should look at the funcitonal args to determine
1167     ;;; if they have any side-effects.]
1168     ;;;
1169     (defun flush-dead-code (block)
1170     (declare (type cblock block))
1171     (do-nodes-backwards (node cont block)
1172     (unless (continuation-dest cont)
1173     (typecase node
1174     (ref
1175     (delete-ref node)
1176     (unlink-node node))
1177     (combination
1178     (let ((info (combination-kind node)))
1179     (when (function-info-p info)
1180     (let ((attr (function-info-attributes info)))
1181     (when (and (ir1-attributep attr flushable)
1182     (not (ir1-attributep attr call)))
1183     (flush-dest (combination-fun node))
1184     (dolist (arg (combination-args node))
1185     (flush-dest arg))
1186     (unlink-node node))))))
1187     (exit
1188     (let ((value (exit-value node)))
1189     (when value
1190     (flush-dest value)
1191     (setf (exit-value node) nil))))
1192     (cset
1193     (let ((var (set-var node)))
1194     (when (and (lambda-var-p var)
1195     (null (leaf-refs var)))
1196     (flush-dest (set-value node))
1197     (setf (basic-var-sets var)
1198     (delete node (basic-var-sets var)))
1199     (unlink-node node)))))))
1200    
1201     (setf (block-flush-p block) nil)
1202     (undefined-value))
1203    

  ViewVC Help
Powered by ViewVC 1.1.5