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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34.1.1 - (hide annotations) (vendor branch)
Wed Dec 9 00:49:18 1992 UTC (21 years, 4 months ago) by ram
Branch: patch_16
Changes since 1.34: +3 -2 lines
Get fix to not call LET-CONVERT in MAYBE-LET-CONVERT if alreay an assignment.
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 ram 1.34.1.1 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/locall.lisp,v 1.34.1.1 1992/12/09 00:49:18 ram Exp $")
11 ram 1.15 ;;;
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 ram 1.31 (in-package :c)
28 wlott 1.1
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 ram 1.31 ;;; Merge-Tail-Sets -- Interface
60     ;;;
61     ;;; This function handles merging the tail sets if Call is potentially
62     ;;; tail-recursive, and is a call to a function with a different TAIL-SET than
63     ;;; Call's Fun. This must be called whenever we alter IR1 so as to place a
64     ;;; local call in what might be a TR context. Note that any call which returns
65     ;;; its value to a RETURN is considered potentially TR, since any implicit
66     ;;; MV-PROG1 might be optimized away.
67     ;;;
68     ;;; We destructively modify the set for the calling function to represent both,
69     ;;; and then change all the functions in callee's set to reference the first.
70     ;;; If we do merge, we reoptimize the RETURN-RESULT continuation to cause
71     ;;; IR1-OPTIMIZE-RETURN to recompute the tail set type.
72     ;;;
73 ram 1.33 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
74     (declare (type basic-combination call) (type clambda new-fun))
75 ram 1.31 (let ((return (continuation-dest (node-cont call))))
76     (when (return-p return)
77     (let ((call-set (lambda-tail-set (node-home-lambda call)))
78 ram 1.33 (fun-set (lambda-tail-set new-fun)))
79 ram 1.31 (unless (eq call-set fun-set)
80     (let ((funs (tail-set-functions fun-set)))
81     (dolist (fun funs)
82     (setf (lambda-tail-set fun) call-set))
83     (setf (tail-set-functions call-set)
84     (nconc (tail-set-functions call-set) funs)))
85     (reoptimize-continuation (return-result return))
86     t)))))
87    
88    
89 wlott 1.1 ;;; Convert-Call -- Internal
90     ;;;
91 ram 1.31 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set the
92 wlott 1.1 ;;; combination kind to :Local, add Fun to the Calls of the function that the
93 ram 1.33 ;;; call is in, call MERGE-TAIL-SETS, then replace the function in the Ref node
94     ;;; with the new function.
95 wlott 1.1 ;;;
96 ram 1.33 ;;; We change the Ref last, since changing the reference can trigger let
97 wlott 1.1 ;;; conversion of the new function, but will only do so if the call is local.
98 ram 1.33 ;;; Note that the replacement may trigger let conversion or other changes in
99     ;;; IR1. We must call MERGE-TAIL-SETS with NEW-FUN before the substitution,
100     ;;; since after the substitution (and let conversion), the call may no longer
101     ;;; be recognizable as tail-recursive.
102 wlott 1.1 ;;;
103     (defun convert-call (ref call fun)
104     (declare (type ref ref) (type combination call) (type clambda fun))
105     (propagate-to-args call fun)
106     (setf (basic-combination-kind call) :local)
107 ram 1.6 (pushnew fun (lambda-calls (node-home-lambda call)))
108 ram 1.33 (merge-tail-sets call fun)
109 wlott 1.1 (change-ref-leaf ref fun)
110     (undefined-value))
111    
112    
113     ;;;; External entry point creation:
114    
115     ;;; Make-XEP-Lambda -- Internal
116     ;;;
117     ;;; Return a Lambda form that can be used as the definition of the XEP for
118     ;;; Fun.
119     ;;;
120     ;;; If Fun is a lambda, then we check the number of arguments (conditional
121     ;;; on policy) and call Fun with all the arguments.
122     ;;;
123     ;;; If Fun is an Optional-Dispatch, then we dispatch off of the number of
124     ;;; supplied arguments by doing do an = test for each entry-point, calling the
125     ;;; entry with the appropriate prefix of the passed arguments.
126     ;;;
127     ;;; If there is a more arg, then there are a couple of optimizations that we
128     ;;; make (more for space than anything else):
129     ;;; -- If Min-Args is 0, then we make the more entry a T clause, since no
130     ;;; argument count error is possible.
131     ;;; -- We can omit the = clause for the last entry-point, allowing the case of
132     ;;; 0 more args to fall through to the more entry.
133     ;;;
134     ;;; We don't bother to policy conditionalize wrong arg errors in optional
135     ;;; dispatches, since the additional overhead is negligible compared to the
136     ;;; other hair going down.
137     ;;;
138     ;;; Note that if policy indicates it, argument type declarations in Fun will
139     ;;; be verified. Since nothing is known about the type of the XEP arg vars,
140     ;;; type checks will be emitted when the XEP's arg vars are passed to the
141     ;;; actual function.
142     ;;;
143     (defun make-xep-lambda (fun)
144     (declare (type functional fun))
145     (etypecase fun
146     (clambda
147     (let ((nargs (length (lambda-vars fun)))
148     (n-supplied (gensym)))
149     (collect ((temps))
150     (dotimes (i nargs)
151     (temps (gensym)))
152     `(lambda (,n-supplied ,@(temps))
153     (declare (fixnum ,n-supplied))
154 ram 1.4 ,(if (policy (lambda-bind fun) (zerop safety))
155 wlott 1.1 `(declare (ignore ,n-supplied))
156     `(%verify-argument-count ,n-supplied ,nargs))
157     (%funcall ,fun ,@(temps))))))
158     (optional-dispatch
159     (let* ((min (optional-dispatch-min-args fun))
160     (max (optional-dispatch-max-args fun))
161     (more (optional-dispatch-more-entry fun))
162     (n-supplied (gensym)))
163     (collect ((temps)
164     (entries))
165     (dotimes (i max)
166     (temps (gensym)))
167    
168     (do ((eps (optional-dispatch-entry-points fun) (rest eps))
169     (n min (1+ n)))
170     ((null eps))
171     (entries `((= ,n-supplied ,n)
172     (%funcall ,(first eps) ,@(subseq (temps) 0 n)))))
173    
174     `(lambda (,n-supplied ,@(temps))
175     (declare (fixnum ,n-supplied))
176     (cond
177     ,@(if more (butlast (entries)) (entries))
178     ,@(when more
179     `((,(if (zerop min) 't `(>= ,n-supplied ,max))
180     ,(let ((n-context (gensym))
181     (n-count (gensym)))
182     `(multiple-value-bind
183     (,n-context ,n-count)
184     (%more-arg-context ,n-supplied ,max)
185     (%funcall ,more ,@(temps) ,n-context ,n-count))))))
186     (t
187     (%argument-count-error ,n-supplied)))))))))
188    
189    
190     ;;; Make-External-Entry-Point -- Internal
191     ;;;
192     ;;; Make an external entry point (XEP) for Fun and return it. We convert
193     ;;; the result of Make-XEP-Lambda in the correct environment, then associate
194     ;;; this lambda with Fun as its XEP. After the conversion, we iterate over the
195     ;;; function's associated lambdas, redoing local call analysis so that the XEP
196 ram 1.18 ;;; calls will get converted. We also bind *lexical-environment* to change the
197     ;;; compilation policy over to the interface policy.
198 wlott 1.1 ;;;
199     ;;; We set Reanalyze and Reoptimize in the component, just in case we
200     ;;; discover an XEP after the initial local call analyze pass.
201     ;;;
202     (defun make-external-entry-point (fun)
203     (declare (type functional fun))
204     (assert (not (functional-entry-function fun)))
205     (with-ir1-environment (lambda-bind (main-entry fun))
206 ram 1.18 (let* ((*lexical-environment*
207     (make-lexenv :cookie
208     (make-interface-cookie *lexical-environment*)))
209     (res (ir1-convert-lambda (make-xep-lambda fun))))
210 wlott 1.1 (setf (functional-kind res) :external)
211 ram 1.12 (setf (leaf-ever-used res) t)
212 wlott 1.1 (setf (functional-entry-function res) fun)
213     (setf (functional-entry-function fun) res)
214     (setf (component-reanalyze *current-component*) t)
215     (setf (component-reoptimize *current-component*) t)
216     (etypecase fun
217     (clambda (local-call-analyze-1 fun))
218     (optional-dispatch
219     (dolist (ep (optional-dispatch-entry-points fun))
220     (local-call-analyze-1 ep))
221     (when (optional-dispatch-more-entry fun)
222     (local-call-analyze-1 (optional-dispatch-more-entry fun)))))
223     res)))
224    
225    
226     ;;; Reference-Entry-Point -- Internal
227     ;;;
228     ;;; Notice a Ref that is not in a local-call context. If the Ref is already
229     ;;; to an XEP, then do nothing, otherwise change it to the XEP, making an XEP
230     ;;; if necessary.
231     ;;;
232     ;;; If Ref is to a special :Cleanup or :Escape function, then we treat it as
233     ;;; though it was not an XEP reference (i.e. leave it alone.)
234     ;;;
235     (defun reference-entry-point (ref)
236     (declare (type ref ref))
237     (let ((fun (ref-leaf ref)))
238     (unless (or (external-entry-point-p fun)
239     (member (functional-kind fun) '(:escape :cleanup)))
240     (change-ref-leaf ref (or (functional-entry-function fun)
241     (make-external-entry-point fun))))))
242    
243    
244     ;;; Local-Call-Analyze-1 -- Interface
245     ;;;
246     ;;; Attempt to convert all references to Fun to local calls. The reference
247     ;;; cannot be :Notinline, and must be the function for a call. The function
248     ;;; continuation must be used only once, since otherwise we cannot be sure what
249     ;;; function is to be called. The call continuation would be multiply used if
250     ;;; there is hairy stuff such as conditionals in the expression that computes
251     ;;; the function.
252     ;;;
253 ram 1.5 ;;; Except in the interpreter, we don't attempt to convert calls that appear
254 ram 1.10 ;;; in a top-level lambda unless there is only one reference or the function is
255     ;;; a unwind-protect cleanup. This allows top-level components to contain only
256     ;;; load-time code: any references to run-time functions will be as closures.
257 ram 1.3 ;;;
258 wlott 1.1 ;;; If we cannot convert a reference, then we mark the referenced function
259     ;;; as an entry-point, creating a new XEP if necessary.
260     ;;;
261     ;;; This is broken off from Local-Call-Analyze so that people can force
262     ;;; analysis of newly introduced calls. Note that we don't do let conversion
263     ;;; here.
264     ;;;
265     (defun local-call-analyze-1 (fun)
266     (declare (type functional fun))
267 ram 1.3 (let ((refs (leaf-refs fun)))
268     (dolist (ref refs)
269     (let* ((cont (node-cont ref))
270     (dest (continuation-dest cont)))
271     (cond ((and (basic-combination-p dest)
272     (eq (basic-combination-fun dest) cont)
273     (eq (continuation-use cont) ref)
274     (or (null (rest refs))
275 ram 1.5 *converting-for-interpreter*
276 ram 1.10 (eq (functional-kind fun) :cleanup)
277 ram 1.6 (not (eq (functional-kind (node-home-lambda ref))
278 ram 1.3 :top-level))))
279     (ecase (ref-inlinep ref)
280 ram 1.21 ((nil :inline :maybe-inline)
281 ram 1.3 (convert-call-if-possible ref dest))
282     ((:notinline)))
283    
284     (unless (eq (basic-combination-kind dest) :local)
285     (reference-entry-point ref)))
286     (t
287     (reference-entry-point ref))))))
288 wlott 1.1
289     (undefined-value))
290    
291    
292     ;;; Local-Call-Analyze -- Interface
293     ;;;
294     ;;; We examine all New-Functions in component, attempting to convert calls
295     ;;; into local calls when it is legal. We also attempt to convert each lambda
296     ;;; to a let. Let conversion is also triggered by deletion of a function
297     ;;; reference, but functions that start out eligible for conversion must be
298     ;;; noticed sometime.
299     ;;;
300     ;;; Note that there is a lot of action going on behind the scenes here,
301     ;;; triggered by reference deletion. In particular, the Component-Lambdas are
302     ;;; being hacked to remove newly deleted and let converted lambdas, so it is
303     ;;; important that the lambda is added to the Component-Lambdas when it is.
304     ;;;
305     (defun local-call-analyze (component)
306     (declare (type component component))
307     (loop
308     (unless (component-new-functions component) (return))
309 ram 1.19 (let* ((fun (pop (component-new-functions component)))
310     (kind (functional-kind fun)))
311     (cond ((eq kind :deleted))
312     ((and (null (leaf-refs fun)) (eq kind nil)
313     (not (functional-entry-function fun)))
314     (delete-functional fun))
315     (t
316     (when (lambda-p fun)
317     (push fun (component-lambdas component)))
318     (local-call-analyze-1 fun)
319     (when (lambda-p fun)
320     (maybe-let-convert fun))))))
321 ram 1.17
322 wlott 1.1 (undefined-value))
323    
324    
325     ;;; Convert-Call-If-Possible -- Interface
326     ;;;
327     ;;; Dispatch to the appropriate function to attempt to convert a call. This
328     ;;; is called in IR1 optimize as well as in local call analysis. If the call
329     ;;; is already :Local, we do nothing. If the call is in the top-level
330     ;;; component, also do nothing, since we don't want to join top-level code into
331     ;;; normal components.
332     ;;;
333     ;;; We bind *Compiler-Error-Context* to the node for the call so that
334     ;;; warnings will get the right context.
335     ;;;
336     (defun convert-call-if-possible (ref call)
337     (declare (type ref ref) (type basic-combination call))
338 ram 1.32 (unless (or (eq (basic-combination-kind call) :local)
339     (let ((block (node-block call)))
340     (or (block-delete-p block)
341     (eq (functional-kind (block-home-lambda block))
342     :deleted))))
343 ram 1.16 (let ((fun (let ((fun (ref-leaf ref)))
344     (if (external-entry-point-p fun)
345     (functional-entry-function fun)
346     fun)))
347     (*compiler-error-context* call))
348 ram 1.27 (let ((c1 (block-component (node-block call)))
349 ram 1.29 (c2 (block-component (node-block (lambda-bind (main-entry fun))))))
350 ram 1.27 (assert (or (eq c1 c2)
351     (and (eq (component-kind c1) :initial)
352     (eq (component-kind c2) :initial)))))
353 ram 1.16 (assert (member (functional-kind fun) '(nil :escape :cleanup :optional)))
354     (cond ((mv-combination-p call)
355 wlott 1.1 (convert-mv-call ref call fun))
356     ((lambda-p fun)
357     (convert-lambda-call ref call fun))
358     (t
359 ram 1.16 (convert-hairy-call ref call fun)))))
360 wlott 1.1 (undefined-value))
361    
362    
363     ;;; Convert-MV-Call -- Internal
364     ;;;
365     ;;; Attempt to convert a multiple-value call. The only interesting case is
366     ;;; a call to a function that Looks-Like-An-MV-Bind, has exactly one reference
367     ;;; and no XEP, and is called with one values continuation.
368     ;;;
369     ;;; We change the call to be to the last optional entry point and change the
370     ;;; call to be local. Due to our preconditions, the call should eventually be
371     ;;; converted to a let, but we can't do that now, since there may be stray
372     ;;; references to the e-p lambda due to optional defaulting code.
373     ;;;
374     ;;; We also use variable types for the called function to construct an
375     ;;; assertion for the values continuation.
376     ;;;
377 ram 1.34 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
378     ;;;
379 wlott 1.1 (defun convert-mv-call (ref call fun)
380     (declare (type ref ref) (type mv-combination call) (type functional fun))
381     (when (and (looks-like-an-mv-bind fun)
382     (not (functional-entry-function fun))
383     (= (length (leaf-refs fun)) 1)
384     (= (length (basic-combination-args call)) 1))
385     (let ((ep (car (last (optional-dispatch-entry-points fun)))))
386     (setf (basic-combination-kind call) :local)
387 ram 1.6 (pushnew ep (lambda-calls (node-home-lambda call)))
388 ram 1.34 (merge-tail-sets call ep)
389 ram 1.22 (change-ref-leaf ref ep)
390 ram 1.31
391 wlott 1.1 (assert-continuation-type
392     (first (basic-combination-args call))
393     (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
394     :rest *universal-type*))))
395     (undefined-value))
396    
397    
398     ;;; Convert-Lambda-Call -- Internal
399     ;;;
400     ;;; Attempt to convert a call to a lambda. If the number of args is wrong,
401     ;;; we give a warning and mark the Ref as :Notinline to remove it from future
402     ;;; consideration. If the argcount is O.K. then we just convert it.
403     ;;;
404     (defun convert-lambda-call (ref call fun)
405 ram 1.9 (declare (type ref ref) (type combination call) (type clambda fun))
406 wlott 1.1 (let ((nargs (length (lambda-vars fun)))
407     (call-args (length (combination-args call))))
408     (cond ((= call-args nargs)
409     (convert-call ref call fun))
410     (t
411     (compiler-warning
412     "Function called with ~R argument~:P, but wants exactly ~R."
413     call-args nargs)
414     (setf (ref-inlinep ref) :notinline)))))
415    
416    
417    
418     ;;;; Optional, more and keyword calls:
419    
420     ;;; Convert-Hairy-Call -- Internal
421     ;;;
422     ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
423     ;;; only fixed args are supplied, then convert a call to the correct entry
424     ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
425     ;;; don't convert calls to functions that have a more (or rest) arg.
426     ;;;
427     (defun convert-hairy-call (ref call fun)
428     (declare (type ref ref) (type combination call)
429     (type optional-dispatch fun))
430     (let ((min-args (optional-dispatch-min-args fun))
431     (max-args (optional-dispatch-max-args fun))
432     (call-args (length (combination-args call))))
433     (cond ((< call-args min-args)
434     (compiler-warning "Function called with ~R argument~:P, but wants at least ~R."
435     call-args min-args)
436     (setf (ref-inlinep ref) :notinline))
437     ((<= call-args max-args)
438     (convert-call ref call
439     (elt (optional-dispatch-entry-points fun)
440     (- call-args min-args))))
441 ram 1.13 ((optional-dispatch-more-entry fun)
442     (convert-more-call ref call fun))
443     (t
444 wlott 1.1 (compiler-warning "Function called with ~R argument~:P, but wants at most ~R."
445     call-args max-args)
446 ram 1.13 (setf (ref-inlinep ref) :notinline))))
447 wlott 1.1
448     (undefined-value))
449    
450    
451     ;;; Convert-Hairy-Fun-Entry -- Internal
452     ;;;
453     ;;; This function is used to convert a call to an entry point when complex
454     ;;; transformations need to be done on the original arguments. Entry is the
455     ;;; entry point function that we are calling. Vars is a list of variable names
456     ;;; which are bound to the oringinal call arguments. Ignores is the subset of
457     ;;; Vars which are ignored. Args is the list of arguments to the entry point
458     ;;; function.
459     ;;;
460     ;;; In order to avoid gruesome graph grovelling, we introduce a new function
461     ;;; that rearranges the arguments and calls the entry point. We analyze the
462     ;;; new function and the entry point immediately so that everything gets
463     ;;; converted during the single pass.
464     ;;;
465     (defun convert-hairy-fun-entry (ref call entry vars ignores args)
466 ram 1.9 (declare (list vars ignores args) (type ref ref) (type combination call)
467     (type clambda entry))
468 wlott 1.1 (let ((new-fun
469     (with-ir1-environment call
470     (ir1-convert-lambda
471     `(lambda ,vars
472 ram 1.13 (declare (ignorable . ,ignores))
473 ram 1.6 (%funcall ,entry . ,args))))))
474 wlott 1.1 (convert-call ref call new-fun)
475     (dolist (ref (leaf-refs entry))
476     (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
477    
478    
479 ram 1.13 ;;; Convert-More-Call -- Internal
480 wlott 1.1 ;;;
481 ram 1.13 ;;; Use Convert-Hairy-Fun-Entry to convert a more-arg call to a known
482     ;;; function into a local call to the Main-Entry.
483 wlott 1.1 ;;;
484     ;;; First we verify that all keywords are constant and legal. If there
485     ;;; aren't, then we warn the user and don't attempt to convert the call.
486     ;;;
487     ;;; We massage the supplied keyword arguments into the order expected by the
488     ;;; main entry. This is done by binding all the arguments to the keyword call
489     ;;; to variables in the introduced lambda, then passing these values variables
490     ;;; in the correct order when calling the main entry. Unused arguments
491     ;;; (such as the keywords themselves) are discarded simply by not passing them
492     ;;; along.
493     ;;;
494 ram 1.13 ;;; If there is a rest arg, then we bundle up the args and pass them to
495     ;;; LIST.
496     ;;;
497     (defun convert-more-call (ref call fun)
498 wlott 1.1 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
499     (let* ((max (optional-dispatch-max-args fun))
500     (arglist (optional-dispatch-arglist fun))
501     (args (combination-args call))
502 ram 1.13 (more (nthcdr max args))
503 ram 1.11 (flame (policy call (or (> speed brevity) (> space brevity))))
504 wlott 1.1 (loser nil))
505     (collect ((temps)
506 ram 1.13 (more-temps)
507 wlott 1.1 (ignores)
508     (supplied)
509     (key-vars))
510    
511     (dolist (var arglist)
512     (let ((info (lambda-var-arg-info var)))
513     (when info
514     (ecase (arg-info-kind info)
515     (:keyword
516     (key-vars var))
517 ram 1.16 ((:rest :optional))))))
518 wlott 1.1
519     (dotimes (i max)
520 ram 1.13 (temps (gensym "FIXED-ARG-TEMP-")))
521 wlott 1.1
522 ram 1.13 (dotimes (i (length more))
523     (more-temps (gensym "MORE-ARG-TEMP-")))
524 wlott 1.1
525 ram 1.13 (when (optional-dispatch-keyp fun)
526     (when (oddp (length more))
527     (compiler-warning "Function called with odd number of ~
528     arguments in keyword portion.")
529     (setf (ref-inlinep ref) :notinline)
530     (return-from convert-more-call))
531    
532     (do ((key more (cddr key))
533     (temp (more-temps) (cddr temp)))
534     ((null key))
535     (let ((cont (first key)))
536     (unless (constant-continuation-p cont)
537     (when flame
538     (compiler-note "Non-constant keyword in keyword call."))
539     (setf (ref-inlinep ref) :notinline)
540     (return-from convert-more-call))
541    
542     (let ((name (continuation-value cont))
543     (dummy (first temp))
544     (val (second temp)))
545     (dolist (var (key-vars)
546     (progn
547     (ignores dummy val)
548     (setq loser name)))
549     (let ((info (lambda-var-arg-info var)))
550     (when (eq (arg-info-keyword info) name)
551 wlott 1.1 (ignores dummy)
552 ram 1.14 (supplied (cons var val))
553     (return)))))))
554 ram 1.13
555     (when (and loser (not (optional-dispatch-allowp fun)))
556     (compiler-warning "Function called with unknown argument keyword ~S."
557     loser)
558     (setf (ref-inlinep ref) :notinline)
559     (return-from convert-more-call)))
560 wlott 1.1
561     (collect ((call-args))
562     (do ((var arglist (cdr var))
563     (temp (temps) (cdr temp)))
564     (())
565     (let ((info (lambda-var-arg-info (car var))))
566     (if info
567 ram 1.13 (ecase (arg-info-kind info)
568 wlott 1.1 (:optional
569     (call-args (car temp))
570     (when (arg-info-supplied-p info)
571     (call-args t)))
572 ram 1.13 (:rest
573     (call-args `(list ,@(more-temps)))
574     (return))
575     (:keyword
576 wlott 1.1 (return)))
577     (call-args (car temp)))))
578    
579     (dolist (var (key-vars))
580     (let ((info (lambda-var-arg-info var))
581     (temp (cdr (assoc var (supplied)))))
582     (if temp
583     (call-args temp)
584     (call-args (arg-info-default info)))
585     (when (arg-info-supplied-p info)
586     (call-args (not (null temp))))))
587    
588     (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
589 ram 1.13 (append (temps) (more-temps))
590     (ignores) (call-args)))))
591 wlott 1.1
592     (undefined-value))
593    
594    
595     ;;;; Let conversion:
596     ;;;
597     ;;; Converting to a let has differing significance to various parts of the
598     ;;; compiler:
599     ;;; -- The body of a Let is spliced in immediately after the the corresponding
600     ;;; combination node, making the control transfer explicit and allowing lets
601     ;;; to mashed together into a single block. The value of the let is
602     ;;; delivered directly to the original continuation for the call,
603     ;;; eliminating the need to propagate information from the dummy result
604     ;;; continuation.
605     ;;; -- As far as IR1 optimization is concerned, it is interesting in that there
606     ;;; is only one expression that the variable can be bound to, and this is
607     ;;; easily substitited for.
608     ;;; -- Lets are interesting to environment analysis and the back end because in
609     ;;; most ways a let can be considered to be "the same function" as its home
610     ;;; function.
611     ;;; -- Let conversion has dynamic scope implications, since control transfers
612     ;;; within the same environment are local. In a local control transfer,
613     ;;; cleanup code must be emitted to remove dynamic bindings that are no
614     ;;; longer in effect.
615    
616    
617 ram 1.23 ;;; Insert-Let-Body -- Internal
618     ;;;
619     ;;; Set up the control transfer to the called lambda. We split the call
620     ;;; block immediately after the call, and link the head of Fun to the call
621     ;;; block. The successor block after splitting (where we return to) is
622     ;;; returned.
623     ;;;
624     ;;; If the lambda is is a different component than the call, then we call
625     ;;; JOIN-COMPONENTS. This only happens in block compilation before
626     ;;; FIND-INITIAL-DFO.
627     ;;;
628     (defun insert-let-body (fun call)
629     (declare (type clambda fun) (type basic-combination call))
630     (let* ((call-block (node-block call))
631     (bind-block (node-block (lambda-bind fun)))
632     (component (block-component call-block)))
633     (let ((fun-component (block-component bind-block)))
634     (unless (eq fun-component component)
635     (assert (eq (component-kind component) :initial))
636     (join-components component fun-component)))
637    
638     (let ((*current-component* component))
639     (node-ends-block call))
640     (assert (= (length (block-succ call-block)) 1))
641     (let ((next-block (first (block-succ call-block))))
642     (unlink-blocks call-block next-block)
643     (link-blocks call-block bind-block)
644     next-block)))
645    
646    
647 ram 1.6 ;;; Merge-Lets -- Internal
648 wlott 1.1 ;;;
649     ;;; Handle the environment semantics of let conversion. We add the lambda
650 ram 1.23 ;;; and its lets to lets for the Call's home function. We merge the calls for
651 ram 1.6 ;;; Fun with the calls for the home function, removing Fun in the process. We
652     ;;; also merge the Entries.
653 wlott 1.1 ;;;
654 ram 1.23 ;;; We also unlink the function head from the component head and set
655     ;;; Component-Reanalyze to true to indicate that the DFO should be recomputed.
656     ;;;
657 ram 1.6 (defun merge-lets (fun call)
658 wlott 1.1 (declare (type clambda fun) (type basic-combination call))
659 ram 1.23 (let ((component (block-component (node-block call))))
660     (unlink-blocks (component-head component) (node-block (lambda-bind fun)))
661     (setf (component-lambdas component)
662     (delete fun (component-lambdas component)))
663     (setf (component-reanalyze component) t))
664     (setf (lambda-call-lexenv fun) (node-lexenv call))
665     (let ((tails (lambda-tail-set fun)))
666     (setf (tail-set-functions tails)
667     (delete fun (tail-set-functions tails))))
668     (setf (lambda-tail-set fun) nil)
669     (let* ((home (node-home-lambda call))
670 ram 1.3 (home-env (lambda-environment home)))
671 wlott 1.1 (push fun (lambda-lets home))
672     (setf (lambda-home fun) home)
673 ram 1.3 (setf (lambda-environment fun) home-env)
674 wlott 1.1
675 ram 1.6 (let ((lets (lambda-lets fun)))
676 wlott 1.1 (dolist (let lets)
677 ram 1.3 (setf (lambda-home let) home)
678     (setf (lambda-environment let) home-env))
679 wlott 1.1
680     (setf (lambda-lets home) (nconc lets (lambda-lets home)))
681     (setf (lambda-lets fun) ()))
682    
683     (setf (lambda-calls home)
684     (nunion (lambda-calls fun)
685     (delete fun (lambda-calls home))))
686     (setf (lambda-calls fun) ())
687    
688     (setf (lambda-entries home)
689     (nconc (lambda-entries fun) (lambda-entries home)))
690     (setf (lambda-entries fun) ()))
691     (undefined-value))
692    
693    
694     ;;; Move-Return-Uses -- Internal
695     ;;;
696 ram 1.23 ;;; Handle the value semantics of let conversion. Delete Fun's return node,
697     ;;; and change the control flow to transfer to Next-Block instead. Move all
698     ;;; the uses of the result continuation to Call's Cont.
699 wlott 1.1 ;;;
700     ;;; If the actual continuation is only used by the let call, then we
701     ;;; intersect the type assertion on the dummy continuation with the assertion
702     ;;; for the actual continuation; in all other cases assertions on the dummy
703     ;;; continuation are lost.
704     ;;;
705 ram 1.7 ;;; We also intersect the derived type of the call with the derived type of
706     ;;; all the dummy continuation's uses. This serves mainly to propagate
707     ;;; TRULY-THE through lets.
708     ;;;
709 ram 1.23 (defun move-return-uses (fun call next-block)
710     (declare (type clambda fun) (type basic-combination call)
711     (type cblock next-block))
712     (let* ((return (lambda-return fun))
713     (return-block (node-block return)))
714     (unlink-blocks return-block
715     (component-tail (block-component return-block)))
716     (link-blocks return-block next-block)
717     (unlink-node return)
718     (delete-return return)
719     (let ((result (return-result return))
720     (cont (node-cont call))
721     (call-type (node-derived-type call)))
722     (when (eq (continuation-use cont) call)
723     (assert-continuation-type cont (continuation-asserted-type result)))
724     (unless (eq call-type *wild-type*)
725     (do-uses (use result)
726     (derive-node-type use call-type)))
727     (substitute-continuation-uses cont result)))
728     (undefined-value))
729 wlott 1.1
730    
731 ram 1.23
732     ;;; MOVE-LET-CALL-CONT -- Internal
733     ;;;
734     ;;; Change all Cont for all the calls to Fun to be the start continuation
735     ;;; for the bind node. This allows the blocks to be joined if the caller count
736     ;;; ever goes to one.
737     ;;;
738     (defun move-let-call-cont (fun)
739     (declare (type clambda fun))
740     (let ((new-cont (node-prev (lambda-bind fun))))
741     (dolist (ref (leaf-refs fun))
742     (let ((dest (continuation-dest (node-cont ref))))
743     (delete-continuation-use dest)
744     (add-continuation-use dest new-cont))))
745 wlott 1.1 (undefined-value))
746    
747    
748 ram 1.25 ;;; Unconvert-Tail-Calls -- Internal
749     ;;;
750     ;;; We are converting Fun to be a let when the call is in a non-tail
751     ;;; position. Any previously tail calls in Fun are no longer tail calls, and
752     ;;; must be restored to normal calls which transfer to Next-Block (Fun's
753 ram 1.31 ;;; return point.) We can't do this by DO-USES on the RETURN-RESULT, because
754     ;;; the return might have been deleted (if all calls were TR.)
755 ram 1.25 ;;;
756 ram 1.30 ;;; The called function might be an assignment in the case where we are
757     ;;; currently converting that function. In steady-state, assignments never
758     ;;; appear in the lambda-calls.
759     ;;;
760 ram 1.25 (defun unconvert-tail-calls (fun call next-block)
761     (dolist (called (lambda-calls fun))
762     (dolist (ref (leaf-refs called))
763     (let ((this-call (continuation-dest (node-cont ref))))
764     (when (and (node-tail-p this-call)
765     (eq (node-home-lambda this-call) fun))
766     (setf (node-tail-p this-call) nil)
767 ram 1.30 (ecase (functional-kind called)
768     ((nil :cleanup :optional)
769     (let ((block (node-block this-call)))
770     (unlink-blocks block (first (block-succ block)))
771     (link-blocks block next-block)
772     (delete-continuation-use this-call)
773     (add-continuation-use this-call (node-cont call))))
774     (:assignment
775     (assert (eq called fun))))))))
776 ram 1.25 (undefined-value))
777    
778    
779 ram 1.23 ;;; MOVE-RETURN-STUFF -- Internal
780     ;;;
781     ;;; Deal with returning from a let or assignment that we are converting.
782     ;;; FUN is the function we are calling, CALL is a call to FUN, and NEXT-BLOCK
783     ;;; is the return point for a non-tail call, or NULL if call is a tail call.
784     ;;;
785 ram 1.26 ;;; If the call is not a tail call, then we must do UNCONVERT-TAIL-CALLS, since
786     ;;; a tail call is a call which returns its value out of the enclosing non-let
787     ;;; function. When call is non-TR, we must convert it back to an ordinary
788     ;;; local call, since the value must be delivered to the receiver of CALL's
789     ;;; value.
790     ;;;
791 ram 1.23 ;;; We do different things depending on whether the caller and callee have
792     ;;; returns left:
793 ram 1.26 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT. Either the
794     ;;; function doesn't return, or all returns are via tail-recursive local
795     ;;; calls.
796 ram 1.23 ;;; -- If CALL is a non-tail call, or if both have returns, then we
797     ;;; delete the callee's return, move its uses to the call's result
798     ;;; continuation, and transfer control to the appropriate return point.
799     ;;; -- If the callee has a return, but the caller doesn't, then we move the
800 ram 1.25 ;;; return to the caller.
801 ram 1.23 ;;;
802     (defun move-return-stuff (fun call next-block)
803     (declare (type clambda fun) (type basic-combination call)
804     (type (or cblock null) next-block))
805 ram 1.25 (when next-block
806     (unconvert-tail-calls fun call next-block))
807 ram 1.23 (let* ((return (lambda-return fun))
808     (call-fun (node-home-lambda call))
809     (call-return (lambda-return call-fun)))
810 ram 1.25 (cond ((not return))
811     ((or next-block call-return)
812     (unless (block-delete-p (node-block return))
813     (move-return-uses fun call
814     (or next-block (node-block call-return)))))
815     (t
816     (assert (node-tail-p call))
817     (setf (lambda-return call-fun) return)
818     (setf (return-lambda return) call-fun))))
819     (move-let-call-cont fun)
820 ram 1.23 (undefined-value))
821    
822    
823 wlott 1.1 ;;; Let-Convert -- Internal
824     ;;;
825     ;;; Actually do let conversion. We call subfunctions to do most of the
826     ;;; work. We change the Call's cont to be the continuation heading the bind
827     ;;; block, and also do Reoptimize-Continuation on the args and Cont so that
828 ram 1.5 ;;; let-specific IR1 optimizations get a chance. We blow away any entry for
829     ;;; the function in *free-functions* so that nobody will create new reference
830     ;;; to it.
831 wlott 1.1 ;;;
832     (defun let-convert (fun call)
833     (declare (type clambda fun) (type basic-combination call))
834 ram 1.23 (let ((next-block (if (node-tail-p call)
835     nil
836     (insert-let-body fun call))))
837 ram 1.25 (move-return-stuff fun call next-block)
838     (merge-lets fun call))
839 ram 1.23
840 ram 1.16 (maybe-remove-free-function fun)
841 wlott 1.1 (dolist (arg (basic-combination-args call))
842     (when arg
843     (reoptimize-continuation arg)))
844     (reoptimize-continuation (node-cont call))
845     (undefined-value))
846    
847    
848     ;;; Maybe-Let-Convert -- Interface
849     ;;;
850     ;;; This function is called when there is some reason to believe that
851     ;;; the lambda Fun might be converted into a let. This is done after local
852     ;;; call analysis, and also when a reference is deleted. We only convert to a
853     ;;; let when the function is a normal local function, has no XEP, and is
854     ;;; referenced in exactly one local call. Conversion is also inhibited if the
855 ram 1.23 ;;; only reference is in a block about to be deleted. We return true if we
856     ;;; converted.
857 wlott 1.1 ;;;
858     ;;; These rules may seem unnecessarily restrictive, since there are some
859     ;;; cases where we could do the return with a jump that don't satisfy these
860     ;;; requirements. The reason for doing things this way is that it makes the
861 ram 1.23 ;;; concept of a let much more useful at the level of IR1 semantics. The
862     ;;; :ASSIGNMENT function kind provides another way to optimize calls to
863     ;;; single-return/multiple call functions.
864 wlott 1.1 ;;;
865     ;;; We don't attempt to convert calls to functions that have an XEP, since
866     ;;; we might be embarrassed later when we want to convert a newly discovered
867     ;;; local call.
868     ;;;
869     (defun maybe-let-convert (fun)
870     (declare (type clambda fun))
871     (let ((refs (leaf-refs fun)))
872     (when (and refs (null (rest refs))
873 ram 1.23 (member (functional-kind fun) '(nil :assignment))
874 wlott 1.1 (not (functional-entry-function fun)))
875     (let* ((ref-cont (node-cont (first refs)))
876     (dest (continuation-dest ref-cont)))
877     (when (and (basic-combination-p dest)
878     (eq (basic-combination-fun dest) ref-cont)
879 ram 1.24 (eq (basic-combination-kind dest) :local)
880     (not (block-delete-p (node-block dest))))
881 ram 1.34.1.1 (unless (eq (functional-kind fun) :assignment)
882     (let-convert fun dest))
883 wlott 1.1 (setf (functional-kind fun)
884 ram 1.23 (if (mv-combination-p dest) :mv-let :let))))
885     t)))
886    
887    
888     ;;;; Tail local calls and assignments:
889    
890     ;;; ONLY-HARMLESS-CLEANUPS -- Internal
891     ;;;
892     ;;; Return T if there are no cleanups between Block1 and Block2, or if they
893     ;;; definitely won't generate any cleanup code. Currently we recognize lexical
894     ;;; entry points that are only used locally (if at all).
895     ;;;
896     (defun only-harmless-cleanups (block1 block2)
897     (declare (type cblock block1 block2))
898     (or (eq block1 block2)
899     (let ((cleanup2 (block-start-cleanup block2)))
900     (do ((cleanup (block-end-cleanup block1)
901     (node-enclosing-cleanup (cleanup-mess-up cleanup))))
902     ((eq cleanup cleanup2) t)
903     (case (cleanup-kind cleanup)
904     ((:block :tagbody)
905     (unless (null (entry-exits (cleanup-mess-up cleanup)))
906     (return nil)))
907     (t (return nil)))))))
908    
909    
910     ;;; MAYBE-CONVERT-TAIL-LOCAL-CALL -- Interface
911     ;;;
912 ram 1.31 ;;; If a potentially TR local call really is TR, then convert it to jump
913     ;;; directly to the called function. We also call MAYBE-CONVERT-TO-ASSIGNMENT.
914     ;;; We can switch the succesor (potentially deleting the RETURN node) unless:
915     ;;; -- The call has already been converted.
916     ;;; -- The call isn't TR (random implicit MV PROG1.)
917 ram 1.23 ;;; -- The call is in an XEP (thus we might decide to make it non-tail so that
918     ;;; we can use known return inside the component.)
919     ;;; -- There is a change in the cleanup between the call in the return, so we
920     ;;; might need to introduce cleanup code.
921     ;;;
922     (defun maybe-convert-tail-local-call (call)
923     (declare (type combination call))
924     (let ((return (continuation-dest (node-cont call))))
925     (assert (return-p return))
926     (when (and (not (node-tail-p call))
927 ram 1.31 (immediately-used-p (return-result return) call)
928 ram 1.23 (not (eq (functional-kind (node-home-lambda call))
929     :external))
930     (only-harmless-cleanups (node-block call)
931     (node-block return)))
932     (node-ends-block call)
933     (let ((block (node-block call))
934     (fun (combination-lambda call)))
935     (setf (node-tail-p call) t)
936     (unlink-blocks block (first (block-succ block)))
937     (link-blocks block (node-block (lambda-bind fun)))
938     (values t (maybe-convert-to-assignment fun))))))
939    
940    
941     ;;; MAYBE-CONVERT-TO-ASSIGNMENT -- Interface
942     ;;;
943     ;;; Called when we believe it might make sense to convert Fun to an
944 ram 1.25 ;;; assignment. All this function really does is determine when a function
945     ;;; with more than one call can still be combined with the calling function's
946     ;;; environment. We can convert when:
947 ram 1.23 ;;; -- The function is a normal, non-entry function, and
948 ram 1.25 ;;; -- Except for one call, all calls must be tail recursive calls in the
949     ;;; called function (i.e. are self-recursive tail calls)
950 ram 1.23 ;;;
951 ram 1.25 ;;; There may be one outside call, and it need not be tail-recursive. Since
952     ;;; all tail local calls have already been converted to direct transfers, the
953     ;;; only control semantics needed are to splice in the body at the non-tail
954     ;;; call. If there is no non-tail call, then we need only merge the
955     ;;; environments. Both cases are handled by LET-CONVERT.
956     ;;;
957     ;;; ### It would actually be possible to allow any number of outside calls as
958     ;;; long as they all return to the same place (i.e. have the same conceptual
959     ;;; continuation.) A special case of this would be when all of the outside
960     ;;; calls are tail recursive.
961 ram 1.23 ;;;
962     (defun maybe-convert-to-assignment (fun)
963     (declare (type clambda fun))
964     (when (and (not (functional-kind fun))
965     (not (functional-entry-function fun)))
966     (let ((non-tail nil)
967     (call-fun nil))
968     (when (dolist (ref (leaf-refs fun) t)
969     (let ((dest (continuation-dest (node-cont ref))))
970     (when (block-delete-p (node-block dest)) (return nil))
971     (let ((home (node-home-lambda ref)))
972     (unless (eq home fun)
973     (when call-fun (return nil))
974     (setq call-fun home))
975     (unless (node-tail-p dest)
976     (when (or non-tail (eq home fun)) (return nil))
977     (setq non-tail dest)))))
978 ram 1.30 (setf (functional-kind fun) :assignment)
979 ram 1.23 (let-convert fun (or non-tail
980     (continuation-dest
981     (node-cont (first (leaf-refs fun))))))
982     t))))

  ViewVC Help
Powered by ViewVC 1.1.5