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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Tue May 29 16:16:29 1990 UTC (23 years, 10 months ago) by ram
Branch: MAIN
Branch point for: eval_debug
Changes since 1.4: +13 -5 lines
When *CONVERTING-FOR-INTERPRETER*, convert multi-ref calls even in top-level
code.  Possibly a hack, given that this is to make interpreted 
unwind-protect work, and top-level NLX doesn't work in general in compiled
code.
When let-converting, blow away any entry for this function in *FREE-FUNCTIONS(
so that we don't add references to functions after they are let converted.
[This was a problem with semi-inline functions...]
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 local call analysis. A local call is a function
11     ;;; call between functions being compiled at the same time. If we can tell at
12     ;;; compile time that such a call is legal, then we modify the flow graph to
13     ;;; represent the control transfers previously implicit in the call. This
14     ;;; change allows us to do inter-routine flow analysis.
15     ;;;
16     ;;; We cannot always do a local call even when we do have the function being
17     ;;; called. Local call can be explicitly disabled by a NOTINLINE declaration.
18     ;;; Calls that cannot be shown to have legal arg counts are also not converted.
19     ;;;
20     ;;; Written by Rob MacLachlan
21     ;;;
22     (in-package 'c)
23    
24    
25     ;;; Propagate-To-Args -- Internal
26     ;;;
27     ;;; This function propagates information from the variables in the function
28     ;;; Fun to the actual arguments in Call.
29     ;;;
30     ;;; We flush all arguments to Call that correspond to unreferenced variables
31     ;;; in Fun. We leave NILs in the Combination-Args so that the remaining args
32     ;;; still match up with their vars.
33     ;;;
34     ;;; We also apply the declared variable type assertion to the argument
35     ;;; continuations.
36     ;;;
37     (defun propagate-to-args (call fun)
38     (declare (type combination call) (type clambda fun))
39     (do ((args (basic-combination-args call) (cdr args))
40     (vars (lambda-vars fun) (cdr vars)))
41     ((null args))
42     (let ((arg (car args))
43     (var (car vars)))
44     (cond ((leaf-refs var)
45     (assert-continuation-type arg (leaf-type var)))
46     (t
47     (flush-dest arg)
48     (setf (car args) nil)))))
49    
50     (undefined-value))
51    
52    
53     ;;; Convert-Call -- Internal
54     ;;;
55     ;;; Convert a combination into a local call. We Propagate-To-Args, set the
56     ;;; combination kind to :Local, add Fun to the Calls of the function that the
57     ;;; call is in, then replace the function in the Ref node with the new
58     ;;; function.
59     ;;;
60     ;;; We change the Ref last, since changing the reference can trigger let
61     ;;; conversion of the new function, but will only do so if the call is local.
62     ;;;
63     (defun convert-call (ref call fun)
64     (declare (type ref ref) (type combination call) (type clambda fun))
65     (propagate-to-args call fun)
66     (setf (basic-combination-kind call) :local)
67     (pushnew fun (lambda-calls (lambda-home (block-lambda (node-block call)))))
68     (change-ref-leaf ref fun)
69     (undefined-value))
70    
71    
72     ;;;; External entry point creation:
73    
74     ;;; Make-XEP-Lambda -- Internal
75     ;;;
76     ;;; Return a Lambda form that can be used as the definition of the XEP for
77     ;;; Fun.
78     ;;;
79     ;;; If Fun is a lambda, then we check the number of arguments (conditional
80     ;;; on policy) and call Fun with all the arguments.
81     ;;;
82     ;;; If Fun is an Optional-Dispatch, then we dispatch off of the number of
83     ;;; supplied arguments by doing do an = test for each entry-point, calling the
84     ;;; entry with the appropriate prefix of the passed arguments.
85     ;;;
86     ;;; If there is a more arg, then there are a couple of optimizations that we
87     ;;; make (more for space than anything else):
88     ;;; -- If Min-Args is 0, then we make the more entry a T clause, since no
89     ;;; argument count error is possible.
90     ;;; -- We can omit the = clause for the last entry-point, allowing the case of
91     ;;; 0 more args to fall through to the more entry.
92     ;;;
93     ;;; We don't bother to policy conditionalize wrong arg errors in optional
94     ;;; dispatches, since the additional overhead is negligible compared to the
95     ;;; other hair going down.
96     ;;;
97     ;;; Note that if policy indicates it, argument type declarations in Fun will
98     ;;; be verified. Since nothing is known about the type of the XEP arg vars,
99     ;;; type checks will be emitted when the XEP's arg vars are passed to the
100     ;;; actual function.
101     ;;;
102     (defun make-xep-lambda (fun)
103     (declare (type functional fun))
104     (etypecase fun
105     (clambda
106     (let ((nargs (length (lambda-vars fun)))
107     (n-supplied (gensym)))
108     (collect ((temps))
109     (dotimes (i nargs)
110     (temps (gensym)))
111     `(lambda (,n-supplied ,@(temps))
112     (declare (fixnum ,n-supplied))
113 ram 1.4 ,(if (policy (lambda-bind fun) (zerop safety))
114 wlott 1.1 `(declare (ignore ,n-supplied))
115     `(%verify-argument-count ,n-supplied ,nargs))
116     (%funcall ,fun ,@(temps))))))
117     (optional-dispatch
118     (let* ((min (optional-dispatch-min-args fun))
119     (max (optional-dispatch-max-args fun))
120     (more (optional-dispatch-more-entry fun))
121     (n-supplied (gensym)))
122     (collect ((temps)
123     (entries))
124     (dotimes (i max)
125     (temps (gensym)))
126    
127     (do ((eps (optional-dispatch-entry-points fun) (rest eps))
128     (n min (1+ n)))
129     ((null eps))
130     (entries `((= ,n-supplied ,n)
131     (%funcall ,(first eps) ,@(subseq (temps) 0 n)))))
132    
133     `(lambda (,n-supplied ,@(temps))
134     (declare (fixnum ,n-supplied))
135     (cond
136     ,@(if more (butlast (entries)) (entries))
137     ,@(when more
138     `((,(if (zerop min) 't `(>= ,n-supplied ,max))
139     ,(let ((n-context (gensym))
140     (n-count (gensym)))
141     `(multiple-value-bind
142     (,n-context ,n-count)
143     (%more-arg-context ,n-supplied ,max)
144     (%funcall ,more ,@(temps) ,n-context ,n-count))))))
145     (t
146     (%argument-count-error ,n-supplied)))))))))
147    
148    
149     ;;; Make-External-Entry-Point -- Internal
150     ;;;
151     ;;; Make an external entry point (XEP) for Fun and return it. We convert
152     ;;; the result of Make-XEP-Lambda in the correct environment, then associate
153     ;;; this lambda with Fun as its XEP. After the conversion, we iterate over the
154     ;;; function's associated lambdas, redoing local call analysis so that the XEP
155     ;;; calls will get converted.
156     ;;;
157     ;;; We set Reanalyze and Reoptimize in the component, just in case we
158     ;;; discover an XEP after the initial local call analyze pass.
159     ;;;
160     (defun make-external-entry-point (fun)
161     (declare (type functional fun))
162     (assert (not (functional-entry-function fun)))
163     (with-ir1-environment (lambda-bind (main-entry fun))
164     (let ((res (ir1-convert-lambda (make-xep-lambda fun))))
165     (setf (functional-kind res) :external)
166     (setf (functional-entry-function res) fun)
167     (setf (functional-entry-function fun) res)
168     (setf (component-reanalyze *current-component*) t)
169     (setf (component-reoptimize *current-component*) t)
170     (etypecase fun
171     (clambda (local-call-analyze-1 fun))
172     (optional-dispatch
173     (dolist (ep (optional-dispatch-entry-points fun))
174     (local-call-analyze-1 ep))
175     (when (optional-dispatch-more-entry fun)
176     (local-call-analyze-1 (optional-dispatch-more-entry fun)))))
177     res)))
178    
179    
180     ;;; Reference-Entry-Point -- Internal
181     ;;;
182     ;;; Notice a Ref that is not in a local-call context. If the Ref is already
183     ;;; to an XEP, then do nothing, otherwise change it to the XEP, making an XEP
184     ;;; if necessary.
185     ;;;
186     ;;; If Ref is to a special :Cleanup or :Escape function, then we treat it as
187     ;;; though it was not an XEP reference (i.e. leave it alone.)
188     ;;;
189     (defun reference-entry-point (ref)
190     (declare (type ref ref))
191     (let ((fun (ref-leaf ref)))
192     (unless (or (external-entry-point-p fun)
193     (member (functional-kind fun) '(:escape :cleanup)))
194     (change-ref-leaf ref (or (functional-entry-function fun)
195     (make-external-entry-point fun))))))
196    
197    
198     ;;; Local-Call-Analyze-1 -- Interface
199     ;;;
200     ;;; Attempt to convert all references to Fun to local calls. The reference
201     ;;; cannot be :Notinline, and must be the function for a call. The function
202     ;;; continuation must be used only once, since otherwise we cannot be sure what
203     ;;; function is to be called. The call continuation would be multiply used if
204     ;;; there is hairy stuff such as conditionals in the expression that computes
205     ;;; the function.
206     ;;;
207 ram 1.5 ;;; Except in the interpreter, we don't attempt to convert calls that appear
208     ;;; in a top-level lambda unless there is only one reference. This ensures
209     ;;; that top-level components will contain only load-time code: any references
210     ;;; to run-time functions will be as closures.
211 ram 1.3 ;;;
212 wlott 1.1 ;;; If we cannot convert a reference, then we mark the referenced function
213     ;;; as an entry-point, creating a new XEP if necessary.
214     ;;;
215     ;;; This is broken off from Local-Call-Analyze so that people can force
216     ;;; analysis of newly introduced calls. Note that we don't do let conversion
217     ;;; here.
218     ;;;
219     (defun local-call-analyze-1 (fun)
220     (declare (type functional fun))
221 ram 1.3 (let ((refs (leaf-refs fun)))
222     (dolist (ref refs)
223     (let* ((cont (node-cont ref))
224     (dest (continuation-dest cont)))
225     (cond ((and (basic-combination-p dest)
226     (eq (basic-combination-fun dest) cont)
227     (eq (continuation-use cont) ref)
228     (or (null (rest refs))
229 ram 1.5 *converting-for-interpreter*
230 ram 1.3 (not (eq (functional-kind
231     (lambda-home
232     (block-lambda (node-block ref))))
233     :top-level))))
234     (ecase (ref-inlinep ref)
235     ((nil :inline)
236     (convert-call-if-possible ref dest))
237     ((:notinline)))
238    
239     (unless (eq (basic-combination-kind dest) :local)
240     (reference-entry-point ref)))
241     (t
242     (reference-entry-point ref))))))
243 wlott 1.1
244     (undefined-value))
245    
246    
247     ;;; Local-Call-Analyze -- Interface
248     ;;;
249     ;;; We examine all New-Functions in component, attempting to convert calls
250     ;;; into local calls when it is legal. We also attempt to convert each lambda
251     ;;; to a let. Let conversion is also triggered by deletion of a function
252     ;;; reference, but functions that start out eligible for conversion must be
253     ;;; noticed sometime.
254     ;;;
255     ;;; Note that there is a lot of action going on behind the scenes here,
256     ;;; triggered by reference deletion. In particular, the Component-Lambdas are
257     ;;; being hacked to remove newly deleted and let converted lambdas, so it is
258     ;;; important that the lambda is added to the Component-Lambdas when it is.
259     ;;;
260     (defun local-call-analyze (component)
261     (declare (type component component))
262     (loop
263     (unless (component-new-functions component) (return))
264     (let ((fun (pop (component-new-functions component))))
265     (unless (eq (functional-kind fun) :deleted)
266     (when (lambda-p fun)
267     (push fun (component-lambdas component)))
268     (local-call-analyze-1 fun)
269     (when (lambda-p fun)
270     (maybe-let-convert fun)))))
271    
272     (undefined-value))
273    
274    
275     ;;; Convert-Call-If-Possible -- Interface
276     ;;;
277     ;;; Dispatch to the appropriate function to attempt to convert a call. This
278     ;;; is called in IR1 optimize as well as in local call analysis. If the call
279     ;;; is already :Local, we do nothing. If the call is in the top-level
280     ;;; component, also do nothing, since we don't want to join top-level code into
281     ;;; normal components.
282     ;;;
283     ;;; We bind *Compiler-Error-Context* to the node for the call so that
284     ;;; warnings will get the right context.
285     ;;;
286     (defun convert-call-if-possible (ref call)
287     (declare (type ref ref) (type basic-combination call))
288     (let ((fun (let ((fun (ref-leaf ref)))
289     (if (external-entry-point-p fun)
290     (functional-entry-function fun)
291     fun)))
292     (*compiler-error-context* call))
293     (cond ((eq (basic-combination-kind call) :local))
294     ((mv-combination-p call)
295     (convert-mv-call ref call fun))
296     ((lambda-p fun)
297     (convert-lambda-call ref call fun))
298     (t
299     (convert-hairy-call ref call fun))))
300     (undefined-value))
301    
302    
303     ;;; Convert-MV-Call -- Internal
304     ;;;
305     ;;; Attempt to convert a multiple-value call. The only interesting case is
306     ;;; a call to a function that Looks-Like-An-MV-Bind, has exactly one reference
307     ;;; and no XEP, and is called with one values continuation.
308     ;;;
309     ;;; We change the call to be to the last optional entry point and change the
310     ;;; call to be local. Due to our preconditions, the call should eventually be
311     ;;; converted to a let, but we can't do that now, since there may be stray
312     ;;; references to the e-p lambda due to optional defaulting code.
313     ;;;
314     ;;; We also use variable types for the called function to construct an
315     ;;; assertion for the values continuation.
316     ;;;
317     (defun convert-mv-call (ref call fun)
318     (declare (type ref ref) (type mv-combination call) (type functional fun))
319     (when (and (looks-like-an-mv-bind fun)
320     (not (functional-entry-function fun))
321     (= (length (leaf-refs fun)) 1)
322     (= (length (basic-combination-args call)) 1))
323     (let ((ep (car (last (optional-dispatch-entry-points fun)))))
324     (change-ref-leaf ref ep)
325     (setf (basic-combination-kind call) :local)
326     (pushnew ep
327     (lambda-calls (lambda-home (block-lambda (node-block call)))))
328    
329     (assert-continuation-type
330     (first (basic-combination-args call))
331     (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
332     :rest *universal-type*))))
333     (undefined-value))
334    
335    
336     ;;; Convert-Lambda-Call -- Internal
337     ;;;
338     ;;; Attempt to convert a call to a lambda. If the number of args is wrong,
339     ;;; we give a warning and mark the Ref as :Notinline to remove it from future
340     ;;; consideration. If the argcount is O.K. then we just convert it.
341     ;;;
342     (proclaim '(function convert-lambda-call (ref combination lambda) void))
343     (defun convert-lambda-call (ref call fun)
344     (let ((nargs (length (lambda-vars fun)))
345     (call-args (length (combination-args call))))
346     (cond ((= call-args nargs)
347     (convert-call ref call fun))
348     (t
349     (compiler-warning
350     "Function called with ~R argument~:P, but wants exactly ~R."
351     call-args nargs)
352     (setf (ref-inlinep ref) :notinline)))))
353    
354    
355    
356     ;;;; Optional, more and keyword calls:
357    
358     ;;; Convert-Hairy-Call -- Internal
359     ;;;
360     ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
361     ;;; only fixed args are supplied, then convert a call to the correct entry
362     ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
363     ;;; don't convert calls to functions that have a more (or rest) arg.
364     ;;;
365     (defun convert-hairy-call (ref call fun)
366     (declare (type ref ref) (type combination call)
367     (type optional-dispatch fun))
368     (let ((min-args (optional-dispatch-min-args fun))
369     (max-args (optional-dispatch-max-args fun))
370     (call-args (length (combination-args call))))
371     (cond ((< call-args min-args)
372     (compiler-warning "Function called with ~R argument~:P, but wants at least ~R."
373     call-args min-args)
374     (setf (ref-inlinep ref) :notinline))
375     ((<= call-args max-args)
376     (convert-call ref call
377     (elt (optional-dispatch-entry-points fun)
378     (- call-args min-args))))
379     ((not (optional-dispatch-more-entry fun))
380     (compiler-warning "Function called with ~R argument~:P, but wants at most ~R."
381     call-args max-args)
382     (setf (ref-inlinep ref) :notinline))
383     ((optional-dispatch-keyp fun)
384     (cond ((oddp (- call-args max-args))
385     (compiler-warning "Function called with odd number of ~
386     arguments in keyword portion."))
387     (t
388     (convert-keyword-call ref call fun))))))
389    
390     (undefined-value))
391    
392    
393     ;;; Convert-Hairy-Fun-Entry -- Internal
394     ;;;
395     ;;; This function is used to convert a call to an entry point when complex
396     ;;; transformations need to be done on the original arguments. Entry is the
397     ;;; entry point function that we are calling. Vars is a list of variable names
398     ;;; which are bound to the oringinal call arguments. Ignores is the subset of
399     ;;; Vars which are ignored. Args is the list of arguments to the entry point
400     ;;; function.
401     ;;;
402     ;;; In order to avoid gruesome graph grovelling, we introduce a new function
403     ;;; that rearranges the arguments and calls the entry point. We analyze the
404     ;;; new function and the entry point immediately so that everything gets
405     ;;; converted during the single pass.
406     ;;;
407     (proclaim '(function convert-hairy-fun-entry
408     (ref combination lambda list list list)))
409     (defun convert-hairy-fun-entry (ref call entry vars ignores args)
410     (let ((new-fun
411     (with-ir1-environment call
412     (ir1-convert-lambda
413     `(lambda ,vars
414     (declare (ignore . ,ignores))
415     (%funcall ,entry . ,args))
416     (node-source call)))))
417     (convert-call ref call new-fun)
418     (dolist (ref (leaf-refs entry))
419     (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
420    
421    
422     ;;; Convert-Keyword-Call -- Internal
423     ;;;
424     ;;; Use Convert-Hairy-Fun-Entry to convert a keyword call to a known
425     ;;; functions into a local call to the Main-Entry.
426     ;;;
427     ;;; First we verify that all keywords are constant and legal. If there
428     ;;; aren't, then we warn the user and don't attempt to convert the call.
429     ;;;
430     ;;; We massage the supplied keyword arguments into the order expected by the
431     ;;; main entry. This is done by binding all the arguments to the keyword call
432     ;;; to variables in the introduced lambda, then passing these values variables
433     ;;; in the correct order when calling the main entry. Unused arguments
434     ;;; (such as the keywords themselves) are discarded simply by not passing them
435     ;;; along.
436     ;;;
437     (defun convert-keyword-call (ref call fun)
438     (declare (type ref ref) (type combination call) (type optional-dispatch fun))
439     (let* ((max (optional-dispatch-max-args fun))
440     (arglist (optional-dispatch-arglist fun))
441     (args (combination-args call))
442     (keys (nthcdr max args))
443     (loser nil))
444     (collect ((temps)
445     (ignores)
446     (supplied)
447     (key-vars))
448    
449     (dolist (var arglist)
450     (let ((info (lambda-var-arg-info var)))
451     (when info
452     (ecase (arg-info-kind info)
453     (:rest
454     (setf (ref-inlinep ref) :notinline)
455     (return-from convert-keyword-call))
456     (:keyword
457     (key-vars var))
458     (:optional)))))
459    
460     (dotimes (i max)
461     (temps (gensym)))
462    
463     (do ((key keys (cddr key)))
464     ((null key))
465     (let ((cont (first key)))
466     (unless (constant-continuation-p cont)
467     (when (policy call (or (> speed brevity) (> space brevity)))
468     (compiler-note "Non-constant keyword in keyword call."))
469     (setf (ref-inlinep ref) :notinline)
470     (return-from convert-keyword-call))
471    
472     (let ((name (continuation-value cont)))
473     (dolist (var (key-vars)
474     (let ((dummy1 (gensym))
475     (dummy2 (gensym)))
476     (temps dummy1 dummy2)
477     (ignores dummy1 dummy2)
478     (setq loser name)))
479     (let ((info (lambda-var-arg-info var)))
480     (when (eq (arg-info-keyword info) name)
481     (let ((dummy (gensym))
482     (temp (gensym)))
483     (temps dummy temp)
484     (ignores dummy)
485     (supplied (cons var temp)))
486     (return)))))))
487    
488     (when (and loser (not (optional-dispatch-allowp fun)))
489     (compiler-warning "Function called with unknown argument keyword ~S."
490     loser)
491     (setf (ref-inlinep ref) :notinline)
492     (return-from convert-keyword-call))
493    
494     (collect ((call-args))
495     (do ((var arglist (cdr var))
496     (temp (temps) (cdr temp)))
497     (())
498     (let ((info (lambda-var-arg-info (car var))))
499     (if info
500     (case (arg-info-kind info)
501     (:optional
502     (call-args (car temp))
503     (when (arg-info-supplied-p info)
504     (call-args t)))
505     (t
506     (return)))
507     (call-args (car temp)))))
508    
509     (dolist (var (key-vars))
510     (let ((info (lambda-var-arg-info var))
511     (temp (cdr (assoc var (supplied)))))
512     (if temp
513     (call-args temp)
514     (call-args (arg-info-default info)))
515     (when (arg-info-supplied-p info)
516     (call-args (not (null temp))))))
517    
518     (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
519     (temps) (ignores) (call-args)))))
520    
521     (undefined-value))
522    
523    
524     ;;;; Let conversion:
525     ;;;
526     ;;; Converting to a let has differing significance to various parts of the
527     ;;; compiler:
528     ;;; -- The body of a Let is spliced in immediately after the the corresponding
529     ;;; combination node, making the control transfer explicit and allowing lets
530     ;;; to mashed together into a single block. The value of the let is
531     ;;; delivered directly to the original continuation for the call,
532     ;;; eliminating the need to propagate information from the dummy result
533     ;;; continuation.
534     ;;; -- As far as IR1 optimization is concerned, it is interesting in that there
535     ;;; is only one expression that the variable can be bound to, and this is
536     ;;; easily substitited for.
537     ;;; -- Lets are interesting to environment analysis and the back end because in
538     ;;; most ways a let can be considered to be "the same function" as its home
539     ;;; function.
540     ;;; -- Let conversion has dynamic scope implications, since control transfers
541     ;;; within the same environment are local. In a local control transfer,
542     ;;; cleanup code must be emitted to remove dynamic bindings that are no
543     ;;; longer in effect.
544    
545    
546     ;;; Merge-Cleanups-And-Lets -- Internal
547     ;;;
548     ;;; Handle the environment semantics of let conversion. We add the lambda
549     ;;; and its lets to lets for the call's home function and move any cleanups and
550     ;;; calls to the home function. We merge the calls for Fun with the calls for
551     ;;; the home function, removing Fun in the process. We also merge the Entries.
552 ram 1.2 ;;; This must run after INSERT-LET-BODY, since the call to NODE-ENDS-BLOCK
553     ;;; figures out the actual cleanup current at the let call (and sets the
554     ;;; start/end cleanups accordingly.)
555 wlott 1.1 ;;;
556     (defun merge-cleanups-and-lets (fun call)
557     (declare (type clambda fun) (type basic-combination call))
558     (let* ((prev (node-prev call))
559 ram 1.3 (home (lambda-home (block-lambda (continuation-block prev))))
560     (home-env (lambda-environment home)))
561 wlott 1.1 (push fun (lambda-lets home))
562     (setf (lambda-home fun) home)
563 ram 1.3 (setf (lambda-environment fun) home-env)
564 wlott 1.1
565     (let ((cleanup (find-enclosing-cleanup
566     (block-end-cleanup (continuation-block prev))))
567     (lets (lambda-lets fun)))
568     (dolist (let lets)
569 ram 1.3 (setf (lambda-home let) home)
570     (setf (lambda-environment let) home-env))
571 wlott 1.1 (when cleanup
572     (dolist (let lets)
573     (unless (lambda-cleanup let)
574     (setf (lambda-cleanup let) cleanup)))
575     (setf (lambda-cleanup fun) cleanup))
576    
577     (setf (lambda-lets home) (nconc lets (lambda-lets home)))
578     (setf (lambda-lets fun) ()))
579    
580     (setf (lambda-calls home)
581     (nunion (lambda-calls fun)
582     (delete fun (lambda-calls home))))
583     (setf (lambda-calls fun) ())
584    
585     (setf (lambda-entries home)
586     (nconc (lambda-entries fun) (lambda-entries home)))
587     (setf (lambda-entries fun) ()))
588     (undefined-value))
589    
590    
591     ;;; Insert-Let-Body -- Internal
592     ;;;
593     ;;; Handle the control semantics of let conversion. We split the call block
594     ;;; immediately after the call, and link the head and tail of Fun to the call
595     ;;; block and the following block. We also unlink the function head and tail
596     ;;; from the component head and tail and flush the function from the
597     ;;; Component-Lambdas. We set Component-Reanalyze to true to indicate that the
598     ;;; DFO should be recomputed.
599     ;;;
600     (defun insert-let-body (fun call)
601     (declare (type clambda fun) (type basic-combination call))
602     (let* ((call-block (node-block call))
603     (bind-block (node-block (lambda-bind fun)))
604     (component (block-component call-block)))
605     (let ((*current-component* component))
606     (node-ends-block call))
607     (setf (component-lambdas component)
608     (delete fun (component-lambdas component)))
609     (assert (= (length (block-succ call-block)) 1))
610     (let ((next-block (first (block-succ call-block))))
611     (unlink-blocks call-block next-block)
612     (unlink-blocks (component-head component) bind-block)
613     (link-blocks call-block bind-block)
614     (let ((return (lambda-return fun)))
615     (when return
616     (let ((return-block (node-block return)))
617     (unlink-blocks return-block (component-tail component))
618     (link-blocks return-block next-block)))))
619     (setf (component-reanalyze component) t))
620     (undefined-value))
621    
622    
623     ;;; Move-Return-Uses -- Internal
624     ;;;
625     ;;; Handle the value semantics of let conversion. When Fun has a return
626     ;;; node, we delete it and move all the uses of the result continuation to
627     ;;; Call's Cont.
628     ;;;
629     ;;; If the actual continuation is only used by the let call, then we
630     ;;; intersect the type assertion on the dummy continuation with the assertion
631     ;;; for the actual continuation; in all other cases assertions on the dummy
632     ;;; continuation are lost.
633     ;;;
634     (defun move-return-uses (fun call)
635     (declare (type clambda fun) (type basic-combination call))
636     (let ((return (lambda-return fun)))
637     (when return
638     (unlink-node return)
639     (delete-return return)
640    
641     (let ((result (return-result return))
642     (cont (node-cont call)))
643     (when (eq (continuation-use cont) call)
644     (assert-continuation-type cont (continuation-asserted-type result)))
645     (delete-continuation-use call)
646     (add-continuation-use call (node-prev (lambda-bind fun)))
647     (substitute-continuation-uses cont result))))
648    
649     (undefined-value))
650    
651    
652     ;;; Let-Convert -- Internal
653     ;;;
654     ;;; Actually do let conversion. We call subfunctions to do most of the
655     ;;; work. We change the Call's cont to be the continuation heading the bind
656     ;;; block, and also do Reoptimize-Continuation on the args and Cont so that
657 ram 1.5 ;;; let-specific IR1 optimizations get a chance. We blow away any entry for
658     ;;; the function in *free-functions* so that nobody will create new reference
659     ;;; to it.
660 wlott 1.1 ;;;
661     (defun let-convert (fun call)
662     (declare (type clambda fun) (type basic-combination call))
663 ram 1.2 (insert-let-body fun call)
664 wlott 1.1 (merge-cleanups-and-lets fun call)
665     (move-return-uses fun call)
666 ram 1.5
667     (let* ((fun (or (lambda-optional-dispatch fun) fun))
668     (entry (gethash (leaf-name fun) *free-functions*)))
669     (when (eq entry fun)
670     (remhash (leaf-name fun) *free-functions*)))
671 wlott 1.1
672     (dolist (arg (basic-combination-args call))
673     (when arg
674     (reoptimize-continuation arg)))
675     (reoptimize-continuation (node-cont call))
676     (undefined-value))
677    
678    
679     ;;; Maybe-Let-Convert -- Interface
680     ;;;
681     ;;; This function is called when there is some reason to believe that
682     ;;; the lambda Fun might be converted into a let. This is done after local
683     ;;; call analysis, and also when a reference is deleted. We only convert to a
684     ;;; let when the function is a normal local function, has no XEP, and is
685     ;;; referenced in exactly one local call. Conversion is also inhibited if the
686     ;;; only reference is in a block about to be deleted.
687     ;;;
688     ;;; These rules may seem unnecessarily restrictive, since there are some
689     ;;; cases where we could do the return with a jump that don't satisfy these
690     ;;; requirements. The reason for doing things this way is that it makes the
691     ;;; concept of a let much more useful at the level of IR1 semantics. Low-level
692     ;;; control and environment optimizations can always be done later on.
693     ;;;
694     ;;; We don't attempt to convert calls to functions that have an XEP, since
695     ;;; we might be embarrassed later when we want to convert a newly discovered
696     ;;; local call.
697     ;;;
698     (defun maybe-let-convert (fun)
699     (declare (type clambda fun))
700     (let ((refs (leaf-refs fun)))
701     (when (and refs (null (rest refs))
702     (not (block-delete-p (node-block (first refs))))
703     (not (functional-kind fun))
704     (not (functional-entry-function fun)))
705     (let* ((ref-cont (node-cont (first refs)))
706     (dest (continuation-dest ref-cont)))
707     (when (and (basic-combination-p dest)
708     (eq (basic-combination-fun dest) ref-cont)
709     (eq (basic-combination-kind dest) :local))
710     (let-convert fun dest)
711     (setf (functional-kind fun)
712     (if (mv-combination-p dest) :mv-let :let))))))
713     (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5