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

Contents of /src/compiler/ir1opt.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Mon May 7 11:30:17 1990 UTC (23 years, 11 months ago) by ram
Branch: MAIN
Changes since 1.4: +12 -15 lines
Rolled back to 1.2, since there seem to be serious problems with the
deleted-contiuation merging.
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     (exit
344     (let ((value (exit-value node)))
345     (when value
346     (derive-node-type node (continuation-derived-type value)))))
347     (cset
348     (ir1-optimize-set node)))))
349     (undefined-value))
350    
351    
352     ;;; Join-Successor-If-Possible -- Internal
353     ;;;
354     ;;; We cannot combine with a successor block if:
355     ;;; 1] The successor has more than one predecessor.
356     ;;; 2] The last node's Cont is also used somewhere else.
357     ;;; 3] The successor is the current block (infinite loop).
358     ;;; 4] The next block has a different cleanup, and thus we may want to insert
359     ;;; cleanup code between the two blocks at some point.
360     ;;; 5] The next block has a different home lambda, and thus the control
361     ;;; transfer is a non-local exit.
362     ;;;
363     ;;; If we succeed, we return true, otherwise false.
364     ;;;
365     ;;; Joining is easy when the successor's Start continuation is the same from
366     ;;; our Last's Cont. If they differ, then we can still join when the last
367     ;;; continuation has no next and the next continuation has no uses. In this
368 ram 1.5 ;;; case, we replace the next continuation with the last before joining the
369 wlott 1.1 ;;; blocks.
370     ;;;
371     (defun join-successor-if-possible (block)
372     (declare (type cblock block))
373     (let ((next (first (block-succ block))))
374     (when (block-lambda next)
375     (let* ((last (block-last block))
376     (last-cont (node-cont last))
377     (next-cont (block-start next))
378     (cleanup (block-end-cleanup block))
379     (next-cleanup (block-start-cleanup next))
380     (lambda (block-lambda block))
381     (next-lambda (block-lambda next)))
382     (cond ((or (rest (block-pred next))
383 ram 1.5 (not (eq (continuation-use last-cont) last))
384 wlott 1.1 (eq next block)
385     (not (eq (lambda-home lambda) (lambda-home next-lambda)))
386     (not (eq (find-enclosing-cleanup cleanup)
387     (find-enclosing-cleanup next-cleanup))))
388     nil)
389 ram 1.5 ((eq last-cont next-cont)
390 wlott 1.1 (join-blocks block next)
391     t)
392 ram 1.5 ((and (null (block-start-uses next))
393     (eq (continuation-kind last-cont) :inside-block))
394     (let ((next-node (continuation-next next-cont)))
395     (assert (not (continuation-dest next-cont)))
396     (delete-continuation next-cont)
397     (setf (node-prev next-node) last-cont)
398     (setf (continuation-next last-cont) next-node)
399     (setf (block-start next) last-cont)
400     (join-blocks block next))
401 wlott 1.1 t)
402     (t
403     nil))))))
404    
405    
406     ;;; Join-Blocks -- Internal
407     ;;;
408     ;;; Join together two blocks which have the same ending/starting
409     ;;; continuation. The code in Block2 is moved into Block1 and Block2 is
410     ;;; deleted from the DFO. The End-Cleanup for Block1 is set to that for
411     ;;; Block2 so that we don't lose cleanup info. We combine the optimize flags
412     ;;; for the two blocks so that any indicated optimization gets done.
413     ;;;
414     (defun join-blocks (block1 block2)
415     (declare (type cblock block1 block2))
416     (let* ((last (block-last block2))
417     (last-cont (node-cont last))
418     (succ (block-succ block2))
419     (start2 (block-start block2)))
420     (do ((cont start2 (node-cont (continuation-next cont))))
421     ((eq cont last-cont)
422     (when (eq (continuation-kind last-cont) :inside-block)
423     (setf (continuation-block last-cont) block1)))
424     (setf (continuation-block cont) block1))
425    
426     (unlink-blocks block1 block2)
427     (dolist (block succ)
428     (unlink-blocks block2 block)
429     (link-blocks block1 block))
430    
431     (setf (block-last block1) last)
432     (setf (continuation-kind start2) :inside-block))
433    
434     (setf (block-end-cleanup block1) (block-end-cleanup block2))
435    
436     (setf (block-reoptimize block1)
437     (or (block-reoptimize block1) (block-reoptimize block2)))
438     (setf (block-type-asserted block1) t)
439     (setf (block-test-modified block1) t)
440    
441     (let ((next (block-next block2))
442     (prev (block-prev block2)))
443     (setf (block-next prev) next)
444     (setf (block-prev next) prev))
445    
446     (undefined-value))
447    
448    
449     ;;;; Local call return type propagation:
450    
451     ;;; Find-Result-Type -- Internal
452     ;;;
453     ;;; This function is called on RETURN nodes that have their REOPTIMIZE flag
454     ;;; set. It iterates over the uses of the RESULT, looking for interesting
455     ;;; stuff to update the TAIL-SET:
456     ;;; -- If a use is a local call, then we check that the called function has
457     ;;; the tail set Tails. If we encounter any different tail set, we return
458     ;;; the second value true.
459     ;;; -- If a use isn't a local call, then we union its type together with the
460     ;;; types of other such uses. We assign to the RETURN-RESULT-TYPE the
461     ;;; intersection of this type with the RESULT's asserted type. We can make
462     ;;; this intersection now (potentially before type checking) because this
463     ;;; assertion on the result will eventually be checked (if appropriate.)
464     ;;;
465     (defun find-result-type (node tails)
466     (declare (type creturn node))
467     (let ((result (return-result node))
468     (retry nil))
469     (collect ((use-union *empty-type* values-type-union))
470     (do-uses (use result)
471     (if (and (basic-combination-p use)
472     (eq (basic-combination-kind use) :local))
473     (when (merge-tail-sets use tails)
474     (setq retry t))
475     (use-union (node-derived-type use))))
476     (let ((int (values-type-intersection
477     (continuation-asserted-type result)
478     (use-union))))
479     (setf (return-result-type node) int)))
480     retry))
481    
482    
483     ;;; Merge-Tail-Sets -- Internal
484     ;;;
485     ;;; This function handles merging the tail sets if Call is a call to a
486     ;;; function with a different TAIL-SET than Ret-Set. We return true if we do
487     ;;; anything.
488     ;;;
489     ;;; It is assumed that Call sends its value to a RETURN node. We
490     ;;; destructively modify the set for the returning function to represent both,
491     ;;; and then change all the functions in callee's set to reference the first.
492     ;;;
493     ;;; If the called function has no tail set, then do nothing; if it doesn't
494     ;;; return, then it can't affect the callers value.
495     ;;;
496     (defun merge-tail-sets (call ret-set)
497     (declare (type basic-combination call) (type tail-set ret-set))
498     (let ((fun-set (lambda-tail-set (combination-lambda call))))
499     (when (and fun-set (not (eq ret-set fun-set)))
500     (let ((funs (tail-set-functions fun-set)))
501     (dolist (fun funs)
502     (setf (lambda-tail-set fun) ret-set))
503     (setf (tail-set-functions ret-set)
504     (nconc (tail-set-functions ret-set) funs)))
505     t)))
506    
507    
508     ;;; IR1-Optimize-Return -- Internal
509     ;;;
510     ;;; Do stuff to realize that something has changed about the value delivered
511     ;;; to a return node. Since we consider the return values of all functions in
512     ;;; the tail set to be equivalent, this amounts to bringing the entire tail set
513     ;;; up to date. We iterate over the returns for all the functions in the tail
514     ;;; set, reanalyzing them all (not treating Node specially.)
515     ;;;
516     ;;; During this iteration, we may discover new functions that should be
517     ;;; added to the tail set. If this happens, we restart the iteration over the
518     ;;; TAIL-SET-FUNCTIONS. Note that this really doesn't duplicate much work, as
519     ;;; we clear the NODE-REOPTIMIZE flags in the return nodes as we go, thus we
520     ;;; don't call FIND-RESULT-TYPE on any given return more than once.
521     ;;;
522     ;;; Restarting the iteration doesn't disturb the computation of the result
523     ;;; type RES, since we will just be adding more types to the union. (or when
524     ;;; we iterate over a return multiple times, unioning in the same type more
525     ;;; than once.)
526     ;;;
527     ;;; When we are done, we check if the new type is different from the old
528     ;;; TAIL-SET-TYPE. If so, we set the type and also reoptimize all the
529     ;;; continuations for references to functions in the tail set. This will
530     ;;; cause IR1-OPTIMIZE-COMBINATION to derive the new type as the results of the
531     ;;; calls.
532     ;;;
533     (defun ir1-optimize-return (node)
534     (declare (type creturn node))
535     (let ((tails (lambda-tail-set (return-lambda node))))
536     (collect ((res *empty-type* values-type-union))
537     (loop
538     (block RETRY
539     (let ((funs (tail-set-functions tails)))
540     (dolist (fun funs)
541     (let ((return (lambda-return fun)))
542     (when (node-reoptimize return)
543     (setf (node-reoptimize node) nil)
544     (when (find-result-type return tails) (return-from RETRY)))
545     (res (return-result-type return)))))
546     (return)))
547    
548     (when (type/= (res) (tail-set-type tails))
549     (setf (tail-set-type tails) (res))
550     (dolist (fun (tail-set-functions tails))
551     (dolist (ref (leaf-refs fun))
552     (reoptimize-continuation (node-cont ref)))))))
553    
554     (undefined-value))
555    
556    
557     ;;; IR1-Optimize-If -- Internal
558     ;;;
559     ;;; If the test has multiple uses, replicate the node when possible. Also
560     ;;; check if the predicate is known to be true or false, deleting the IF node
561     ;;; in favor of the appropriate branch when this is the case.
562     ;;;
563     (defun ir1-optimize-if (node)
564     (declare (type cif node))
565     (let ((test (if-test node))
566     (block (node-block node)))
567    
568     (when (and (eq (block-start block) test)
569     (eq (continuation-next test) node)
570     (rest (block-start-uses block)))
571     (do-uses (use test)
572     (when (immediately-used-p test use)
573     (convert-if-if use node)
574     (when (continuation-use test) (return)))))
575    
576     (let* ((type (continuation-type test))
577     (victim
578     (cond ((constant-continuation-p test)
579     (if (continuation-value test)
580     (if-alternative node)
581     (if-consequent node)))
582     ((not (types-intersect type *null-type*))
583     (if-alternative node))
584     ((type= type *null-type*)
585     (if-consequent node)))))
586     (when victim
587     (flush-dest test)
588     (when (rest (block-succ block))
589     (unlink-blocks block victim))
590     (setf (component-reanalyze (block-component (node-block node))) t)
591     (unlink-node node))))
592     (undefined-value))
593    
594    
595     ;;; Convert-If-If -- Internal
596     ;;;
597     ;;; Create a new copy of an IF Node that tests the value of the node Use.
598     ;;; The test must have >1 use, and must be immediately used by Use. Node must
599     ;;; be the only node in its block (implying that block-start = if-test).
600     ;;;
601     ;;; This optimization has an effect semantically similar to the
602     ;;; source-to-source transformation:
603     ;;; (IF (IF A B C) D E) ==>
604     ;;; (IF A (IF B D E) (IF C D E))
605     ;;;
606     (defun convert-if-if (use node)
607     (declare (type node use) (type cif node))
608     (with-ir1-environment node
609     (let* ((block (node-block node))
610     (test (if-test node))
611     (cblock (if-consequent node))
612     (ablock (if-alternative node))
613     (use-block (node-block use))
614     (dummy-cont (make-continuation))
615     (new-cont (make-continuation))
616     (new-node (make-if :test new-cont :source (node-source node)
617     :consequent cblock :alternative ablock))
618     (new-block (continuation-starts-block new-cont)))
619     (prev-link new-node new-cont)
620     (setf (continuation-dest new-cont) new-node)
621     (add-continuation-use new-node dummy-cont)
622     (setf (block-last new-block) new-node)
623    
624     (unlink-blocks use-block block)
625     (delete-continuation-use use)
626     (add-continuation-use use new-cont)
627     (link-blocks use-block new-block)
628    
629     (link-blocks new-block cblock)
630     (link-blocks new-block ablock)
631    
632     (reoptimize-continuation test)
633     (reoptimize-continuation new-cont)
634     (setf (component-reanalyze *current-component*) t)))
635     (undefined-value))
636    
637    
638     ;;;; Exit IR1 optimization:
639    
640     ;;; Maybe-Delete-Exit -- Interface
641     ;;;
642     ;;; This function attempts to delete an exit node, returning true if it
643     ;;; deletes the block as a consequence:
644     ;;; -- If the exit is degenerate (has no Entry), then we don't do anything,
645     ;;; since there is nothing to be done.
646     ;;; -- If the exit node and its Entry have the same home lambda then we know
647     ;;; the exit is local, and can delete the exit. We change uses of the
648     ;;; Exit-Value to be uses of the original continuation, then unlink the
649     ;;; node.
650     ;;; -- If there is no value (as in a GO), then we skip the value semantics.
651     ;;;
652     ;;; This function is also called by environment analysis, since it wants all
653     ;;; exits to be optimized even if normal optimization was omitted.
654     ;;;
655     (defun maybe-delete-exit (node)
656     (declare (type exit node))
657     (let ((value (exit-value node))
658     (entry (exit-entry node))
659     (cont (node-cont node)))
660     (when (and entry
661     (eq (lambda-home (block-lambda (node-block node)))
662     (lambda-home (block-lambda (node-block entry)))))
663     (prog1
664     (unlink-node node)
665     (when value
666     (substitute-continuation-uses cont value))))))
667    
668    
669     ;;;; Combination IR1 optimization:
670    
671     ;;; Ir1-Optimize-Combination -- Internal
672     ;;;
673     ;;; Do IR1 optimizations on a Combination node.
674     ;;;
675     (proclaim '(function ir1-optimize-combination (combination) void))
676     (defun ir1-optimize-combination (node)
677     (let ((args (basic-combination-args node))
678     (kind (basic-combination-kind node)))
679     (case kind
680     (:local
681     (let ((fun (combination-lambda node)))
682     (if (eq (functional-kind fun) :let)
683     (propagate-let-args node fun)
684     (propagate-local-call-args node fun))))
685     (:full
686     (dolist (arg args)
687     (when arg
688     (setf (continuation-reoptimize arg) nil))))
689     (t
690     (dolist (arg args)
691     (when arg
692     (setf (continuation-reoptimize arg) nil)))
693    
694     (let ((attr (function-info-attributes kind)))
695     (when (and (ir1-attributep attr foldable)
696     (not (ir1-attributep attr call))
697     (every #'constant-continuation-p args)
698     (continuation-dest (node-cont node)))
699     (constant-fold-call node)
700     (return-from ir1-optimize-combination)))
701    
702     (let ((fun (function-info-derive-type kind)))
703     (when fun
704     (let ((res (funcall fun node)))
705     (when res
706     (derive-node-type node res)))))
707    
708     (let ((fun (function-info-optimizer kind)))
709     (unless (and fun (funcall fun node))
710     (dolist (x (function-info-transforms kind))
711 wlott 1.2 (unless (ir1-transform node (car x) (cdr x))
712     (return))))))))
713 wlott 1.1
714     (undefined-value))
715    
716    
717     ;;; Recognize-Known-Call -- Interface
718     ;;;
719     ;;; If Call is a call to a known function, mark it as such by setting the
720     ;;; Kind. In addition to a direct check for the function name in the table, we
721     ;;; also must check for slot accessors. If the function is a slot accessor,
722     ;;; then we set the combination kind to the function info of %Slot-Setter or
723     ;;; %Slot-Accessor, as appropriate.
724     ;;;
725     (defun recognize-known-call (call)
726     (declare (type combination call))
727     (let* ((fun (basic-combination-fun call))
728     (name (continuation-function-name fun)))
729     (when name
730     (let ((info (info function info name)))
731     (cond (info
732     (setf (basic-combination-kind call) info))
733     ((slot-accessor-p (ref-leaf (continuation-use fun)))
734     (setf (basic-combination-kind call)
735     (info function info
736     (if (consp name)
737     '%slot-setter
738     '%slot-accessor))))))))
739     (undefined-value))
740    
741    
742     ;;; Propagate-Function-Change -- Internal
743     ;;;
744     ;;; Called by Ir1-Optimize when the function for a call has changed.
745     ;;; If the call is to a functional, then we attempt to convert it to a local
746     ;;; call, otherwise we check the call for legality with respect to the new
747     ;;; type; if it is illegal, we mark the Ref as :Notline and punt.
748     ;;;
749     ;;; If we do have a good type for the call, we propagate type information from
750     ;;; the type to the arg and result continuations. If we discover that the call
751     ;;; is to a known global function, then we mark the combination as known.
752     ;;;
753     (defun propagate-function-change (call)
754     (declare (type combination call))
755     (let* ((fun (combination-fun call))
756     (use (continuation-use fun))
757     (type (continuation-derived-type fun))
758     (*compiler-error-context* call))
759     (setf (continuation-reoptimize fun) nil)
760     (cond ((or (not (ref-p use))
761     (eq (ref-inlinep use) :notinline)))
762     ((functional-p (ref-leaf use))
763     (let ((leaf (ref-leaf use)))
764     (cond ((eq (combination-kind call) :local)
765     (let ((tail-set (lambda-tail-set leaf)))
766     (when tail-set
767     (derive-node-type
768     call (tail-set-type tail-set)))))
769     ((not (eq (ref-inlinep use) :notinline))
770     (convert-call-if-possible use call)
771     (maybe-let-convert leaf)))))
772     ((not (function-type-p type)))
773     ((valid-function-use call type
774     :argument-test #'always-subtypep
775     :result-test #'always-subtypep
776     :error-function #'compiler-warning
777     :warning-function #'compiler-note)
778     (assert-call-type call type)
779     (recognize-known-call call))
780     (t
781     (setf (ref-inlinep use) :notinline))))
782    
783     (undefined-value))
784    
785    
786     ;;;; Known function optimization:
787    
788     ;;; IR1-Transform -- Internal
789     ;;;
790     ;;; Attempt to transform Node using Function, subject to the call type
791     ;;; constraint Type. If we are inhibited from doing the transform for some
792     ;;; reason and Flame is true, then we make a note of the message in
793 wlott 1.2 ;;; *failed-optimizations* for IR1 finalize to pick up. We return true if
794     ;;; the transform failed, and thus further transformation should be
795     ;;; attempted. We return false if either the transform suceeded or was
796     ;;; aborted.
797 wlott 1.1 ;;;
798     (defun ir1-transform (node type fun)
799 wlott 1.2 (declare (type combination node) (type ctype type) (type function fun))
800 wlott 1.1 (let ((constrained (function-type-p type))
801     (flame (policy node (> speed brevity)))
802     (*compiler-error-context* node))
803     (cond ((or (not constrained)
804     (valid-function-use node type))
805     (multiple-value-bind
806     (severity args)
807     (catch 'give-up
808     (transform-call node (funcall fun node))
809     (remhash node *failed-optimizations*)
810     (values :none nil))
811     (ecase severity
812 wlott 1.2 (:none nil)
813 wlott 1.1 (:aborted
814     (setf (combination-kind node) :full)
815     (setf (ref-inlinep (continuation-use (combination-fun node)))
816     :notinline)
817     (when args
818 wlott 1.2 (apply #'compiler-warning args))
819     nil)
820 wlott 1.1 (:failure
821     (when (and flame args)
822 wlott 1.2 (setf (gethash node *failed-optimizations*) args))
823     t))))
824 wlott 1.1 ((and flame
825     (valid-function-use node type
826     :argument-test #'types-intersect
827     :result-test #'values-types-intersect))
828 wlott 1.2 (setf (gethash node *failed-optimizations*) type)
829     t))))
830 wlott 1.1
831    
832     ;;; GIVE-UP, ABORT-TRANSFORM -- Interface
833     ;;;
834     ;;; Just throw the severity and args...
835     ;;;
836     (proclaim '(function give-up (&rest t) nil))
837     (defun give-up (&rest args)
838     "This function is used to throw out of an IR1 transform, aborting this
839     attempt to transform the call, but admitting the possibility that this or
840     some other transform will later suceed. If arguments are supplied, they are
841     format arguments for an efficiency note."
842     (throw 'give-up (values :failure args)))
843     ;;;
844     (defun abort-transform (&rest args)
845     "This function is used to throw out of an IR1 transform and force a normal
846     call to the function at run time. No further optimizations will be
847     attempted."
848     (throw 'give-up (values :aborted args)))
849    
850    
851     ;;; Transform-Call -- Internal
852     ;;;
853     ;;; Take the lambda-expression Res, IR1 convert it in the proper
854     ;;; environment, and then install it as the function for the call Node. We do
855     ;;; local call analysis so that the new function is integrated into the control
856     ;;; flow. We set the Reanalyze flag in the component to cause the DFO to be
857     ;;; recomputed at soonest convenience.
858     ;;;
859     (defun transform-call (node res)
860     (declare (type combination node) (list res))
861     (with-ir1-environment node
862     (let ((new-fun (ir1-convert-lambda res (node-source node)))
863     (ref (continuation-use (combination-fun node))))
864     (change-ref-leaf ref new-fun)
865     (setf (combination-kind node) :full)
866     (local-call-analyze *current-component*)))
867     (undefined-value))
868    
869    
870     ;;; Constant-Fold-Call -- Internal
871     ;;;
872     ;;; Replace a call to a foldable function of constant arguments with the
873     ;;; result of evaluating the form. We insert the resulting constant node after
874     ;;; the call, stealing the call's continuation. We give the call a
875     ;;; continuation with no Dest, which should cause it and its arguments to go
876     ;;; away. If there is an error during the evaluation, we give a warning and
877     ;;; leave the call alone, making the call a full call and marking it as
878     ;;; :notinline to make sure that it stays that way.
879     ;;;
880     ;;; For now, if the result is other than one value, we don't fold it.
881     ;;;
882     (defun constant-fold-call (call)
883     (declare (type combination call))
884     (let* ((args (mapcar #'continuation-value (combination-args call)))
885     (ref (continuation-use (combination-fun call)))
886     (fun (leaf-name (ref-leaf ref))))
887    
888     (multiple-value-bind (values win)
889     (careful-call fun args call "constant folding")
890     (cond
891     ((not win)
892     (setf (ref-inlinep ref) :notinline)
893     (setf (combination-kind call) :full))
894     ((= (length values) 1)
895     (with-ir1-environment call
896     (let* ((leaf (find-constant (first values)))
897     (node (make-ref (leaf-type leaf)
898     (node-source call)
899     leaf
900     nil))
901     (dummy (make-continuation))
902     (cont (node-cont call))
903     (block (node-block call))
904     (next (continuation-next cont)))
905     (push node (leaf-refs leaf))
906     (setf (leaf-ever-used leaf) t)
907    
908     (delete-continuation-use call)
909     (add-continuation-use call dummy)
910     (prev-link node dummy)
911     (add-continuation-use node cont)
912     (setf (continuation-next cont) next)
913     (when (eq call (block-last block))
914     (setf (block-last block) node))
915     (reoptimize-continuation cont)))))))
916    
917     (undefined-value))
918    
919    
920     ;;;; Local call optimization:
921    
922     ;;; Propagate-To-Refs -- Internal
923     ;;;
924     ;;; Propagate Type to Leaf and its Refs, marking things changed. If the
925     ;;; leaf type is a function type, then just leave it alone, since TYPE is never
926     ;;; going to be more specific than that (and TYPE-INTERSECTION would choke.)
927     ;;;
928     (defun propagate-to-refs (leaf type)
929     (declare (type leaf leaf) (type ctype type))
930     (let ((var-type (leaf-type leaf)))
931     (unless (function-type-p var-type)
932     (let ((int (type-intersection var-type type)))
933     (when (type/= int var-type)
934     (setf (leaf-type leaf) int)
935     (dolist (ref (leaf-refs leaf))
936     (derive-node-type ref int))))
937     (undefined-value))))
938    
939    
940     ;;; PROPAGATE-FROM-SETS -- Internal
941     ;;;
942     ;;; Figure out the type of a LET variable that has sets. We compute the
943     ;;; union of the initial value Type and the types of all the set values and to
944     ;;; a PROPAGATE-TO-REFS with this type.
945     ;;;
946     (defun propagate-from-sets (var type)
947     (collect ((res *empty-type* type-union))
948     (res type)
949     (dolist (set (basic-var-sets var))
950     (res (continuation-type (set-value set)))
951     (setf (node-reoptimize set) nil))
952     (propagate-to-refs var (res)))
953     (undefined-value))
954    
955    
956     ;;; IR1-OPTIMIZE-SET -- Internal
957     ;;;
958     ;;; If a let variable, find the initial value's type and do
959     ;;; PROPAGATE-FROM-SETS. We also derive the VALUE's type as the node's type.
960     ;;;
961     (defun ir1-optimize-set (node)
962     (declare (type cset node))
963     (let ((var (set-var node)))
964     (when (and (lambda-var-p var) (leaf-refs var))
965     (let ((home (lambda-var-home var)))
966     (when (eq (functional-kind home) :let)
967     (let ((iv (let-var-initial-value var)))
968     (setf (continuation-reoptimize iv) nil)
969     (propagate-from-sets var (continuation-type iv)))))))
970    
971     (derive-node-type node (continuation-type (set-value node)))
972     (undefined-value))
973    
974    
975     ;;; Propagate-Let-Args -- Internal
976     ;;;
977     ;;; This function is called when one of the arguments to a LET changes. We
978     ;;; look at each changed argument. If the corresponding variable is set, then
979     ;;; we call PROPAGATE-FROM-SETS. Otherwise, we consider substituting for the
980     ;;; variable, and also propagate derived-type information for the arg to all
981     ;;; the Var's refs.
982     ;;;
983     ;;; Substitution is inhibited when the Ref's derived type isn't a subtype of
984     ;;; the argument's asserted type. This prevents type checking from being
985     ;;; defeated, and also ensures that the best representation for the variable
986     ;;; can be used.
987     ;;;
988     ;;; Note that we are responsible for clearing the Continuation-Reoptimize
989     ;;; flags.
990     ;;;
991     (defun propagate-let-args (call fun)
992     (declare (type combination call) (type clambda fun))
993     (mapc #'(lambda (arg var)
994     (when (and arg
995     (continuation-reoptimize arg))
996     (setf (continuation-reoptimize arg) nil)
997     (cond
998     ((lambda-var-sets var)
999     (propagate-from-sets var (continuation-type arg)))
1000     (t
1001     (let ((use (continuation-use arg)))
1002     (when (ref-p use)
1003     (let ((leaf (ref-leaf use)))
1004     (when (and (or (constant-p leaf)
1005     (functional-p leaf)
1006     (and (lambda-var-p leaf)
1007     (null (lambda-var-sets leaf))))
1008     (values-subtypep
1009     (node-derived-type use)
1010     (continuation-asserted-type arg)))
1011     (substitute-leaf leaf var)))))
1012     (propagate-to-refs var (continuation-type arg))))))
1013     (basic-combination-args call)
1014     (lambda-vars fun))
1015     (undefined-value))
1016    
1017    
1018     ;;; Propagate-Local-Call-Args -- Internal
1019     ;;;
1020     ;;; This function is called when one of the args to a non-let local call
1021     ;;; changes. For each changed argument corresponding to an unset variable, we
1022     ;;; compute the union of the types across all calls and propagate this type
1023     ;;; information to the var's refs.
1024     ;;;
1025     ;;; If the function has an XEP, then we don't do anything, since we won't
1026     ;;; discover anything.
1027     ;;;
1028     ;;; We can clear the Continuation-Reoptimize flags for arguments in all calls
1029     ;;; corresponding to changed arguments in Call, since the only use in IR1
1030     ;;; optimization of the Reoptimize flag for local call args is right here.
1031     ;;;
1032     (defun propagate-local-call-args (call fun)
1033     (declare (type combination call) (type clambda fun))
1034    
1035     (unless (functional-entry-function fun)
1036     (let* ((vars (lambda-vars fun))
1037     (union (mapcar #'(lambda (arg var)
1038     (when (and arg
1039     (continuation-reoptimize arg)
1040     (null (basic-var-sets var)))
1041     (continuation-type arg)))
1042     (basic-combination-args call)
1043     vars))
1044     (this-ref (continuation-use (basic-combination-fun call))))
1045    
1046     (dolist (arg (basic-combination-args call))
1047     (when arg
1048     (setf (continuation-reoptimize arg) nil)))
1049    
1050     (dolist (ref (leaf-refs fun))
1051     (unless (eq ref this-ref)
1052     (setq union
1053     (mapcar #'(lambda (this-arg old)
1054     (when old
1055     (setf (continuation-reoptimize this-arg) nil)
1056     (type-union (continuation-type this-arg) old)))
1057     (basic-combination-args
1058     (continuation-dest (node-cont ref)))
1059     union))))
1060    
1061     (mapc #'(lambda (var type)
1062     (when type
1063     (propagate-to-refs var type)))
1064     vars union)))
1065    
1066     (undefined-value))
1067    
1068    
1069     ;;; Flush-Dead-Code -- Internal
1070     ;;;
1071     ;;; Delete any nodes in Block whose value is unused and have no
1072     ;;; side-effects. We can delete sets of lexical variables when the set
1073     ;;; variable has no references.
1074     ;;;
1075     ;;; [### For now, don't delete potentially flushable calls when they have the
1076     ;;; Call attribute. Someday we should look at the funcitonal args to determine
1077     ;;; if they have any side-effects.]
1078     ;;;
1079     (defun flush-dead-code (block)
1080     (declare (type cblock block))
1081     (do-nodes-backwards (node cont block)
1082     (unless (continuation-dest cont)
1083     (typecase node
1084     (ref
1085     (delete-ref node)
1086     (unlink-node node))
1087     (combination
1088     (let ((info (combination-kind node)))
1089     (when (function-info-p info)
1090     (let ((attr (function-info-attributes info)))
1091     (when (and (ir1-attributep attr flushable)
1092     (not (ir1-attributep attr call)))
1093     (flush-dest (combination-fun node))
1094     (dolist (arg (combination-args node))
1095     (flush-dest arg))
1096     (unlink-node node))))))
1097     (exit
1098     (let ((value (exit-value node)))
1099     (when value
1100     (flush-dest value)
1101     (setf (exit-value node) nil))))
1102     (cset
1103     (let ((var (set-var node)))
1104     (when (and (lambda-var-p var)
1105     (null (leaf-refs var)))
1106     (flush-dest (set-value node))
1107     (setf (basic-var-sets var)
1108     (delete node (basic-var-sets var)))
1109     (unlink-node node)))))))
1110    
1111     (setf (block-flush-p block) nil)
1112     (undefined-value))
1113    

  ViewVC Help
Powered by ViewVC 1.1.5