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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5