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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (show annotations)
Mon Sep 21 15:37:46 1992 UTC (21 years, 7 months ago) by ram
Branch: MAIN
Changes since 1.35: +29 -6 lines
Catch LOCAL-CALL-LOSSAGE around the call to ir1-convert-lambda when inline
expanding local functions.  
In MAYBE-LET-CONVERT, don't convert calls to named functions when the call is
in an initial component (delay until ir1 optimization.)
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; 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.36 1992/09/21 15:37:46 ram Exp $")
11 ;;;
12 ;;; **********************************************************************
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 ;;; 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 ;;;
21 ;;; We cannot always do a local call even when we do have the function being
22 ;;; called. Calls that cannot be shown to have legal arg counts are not
23 ;;; converted.
24 ;;;
25 ;;; Written by Rob MacLachlan
26 ;;;
27 (in-package :c)
28
29
30 ;;; Propagate-To-Args -- Interface
31 ;;;
32 ;;; This function propagates information from the variables in the function
33 ;;; 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 ;;;
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 ;;; 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 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
74 (declare (type basic-combination call) (type clambda new-fun))
75 (let ((return (continuation-dest (node-cont call))))
76 (when (return-p return)
77 (let ((call-set (lambda-tail-set (node-home-lambda call)))
78 (fun-set (lambda-tail-set new-fun)))
79 (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 ;;; Convert-Call -- Internal
90 ;;;
91 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set the
92 ;;; combination kind to :Local, add Fun to the Calls of the function that the
93 ;;; call is in, call MERGE-TAIL-SETS, then replace the function in the Ref node
94 ;;; with the new function.
95 ;;;
96 ;;; We change the Ref last, since changing the reference can trigger let
97 ;;; conversion of the new function, but will only do so if the call is local.
98 ;;; 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 ;;;
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 (pushnew fun (lambda-calls (node-home-lambda call)))
108 (merge-tail-sets call fun)
109 (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 ,(if (policy nil (zerop safety))
155 `(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 ;;; calls will get converted. We also bind *lexical-environment* to change the
197 ;;; compilation policy over to the interface policy.
198 ;;;
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 (let* ((*lexical-environment*
207 (make-lexenv :cookie
208 (make-interface-cookie *lexical-environment*)))
209 (res (ir1-convert-lambda (make-xep-lambda fun))))
210 (setf (functional-kind res) :external)
211 (setf (leaf-ever-used res) t)
212 (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
245 ;;; Local-Call-Analyze-1 -- Interface
246 ;;;
247 ;;; Attempt to convert all references to Fun to local calls. The reference
248 ;;; must be the function for a call, and the function continuation must be used
249 ;;; only once, since otherwise we cannot be sure what function is to be called.
250 ;;; The call continuation would be multiply used if there is hairy stuff such
251 ;;; as conditionals in the expression that computes the function.
252 ;;;
253 ;;; If we cannot convert a reference, then we mark the referenced function
254 ;;; as an entry-point, creating a new XEP if necessary. We don't try to
255 ;;; convert calls that are in error (:ERROR kind.)
256 ;;;
257 ;;; This is broken off from Local-Call-Analyze so that people can force
258 ;;; analysis of newly introduced calls. Note that we don't do let conversion
259 ;;; here.
260 ;;;
261 (defun local-call-analyze-1 (fun)
262 (declare (type functional fun))
263 (let ((refs (leaf-refs fun))
264 (first-time t))
265 (dolist (ref refs)
266 (let* ((cont (node-cont ref))
267 (dest (continuation-dest cont)))
268 (cond ((and (basic-combination-p dest)
269 (eq (basic-combination-fun dest) cont)
270 (eq (continuation-use cont) ref))
271
272 (convert-call-if-possible ref dest)
273
274 (unless (eq (basic-combination-kind dest) :local)
275 (reference-entry-point ref)))
276 (t
277 (reference-entry-point ref))))
278 (setq first-time nil)))
279
280 (undefined-value))
281
282
283 ;;; Local-Call-Analyze -- Interface
284 ;;;
285 ;;; We examine all New-Functions in component, attempting to convert calls
286 ;;; into local calls when it is legal. We also attempt to convert each lambda
287 ;;; to a let. Let conversion is also triggered by deletion of a function
288 ;;; reference, but functions that start out eligible for conversion must be
289 ;;; noticed sometime.
290 ;;;
291 ;;; Note that there is a lot of action going on behind the scenes here,
292 ;;; triggered by reference deletion. In particular, the Component-Lambdas are
293 ;;; being hacked to remove newly deleted and let converted lambdas, so it is
294 ;;; important that the lambda is added to the Component-Lambdas when it is.
295 ;;; Also, the COMPOENT-NEW-FUNCTIONS may contain all sorts of drivel, since it
296 ;;; is not updated when we delete functions, etc. Only COMPONENT-LAMBDAS is
297 ;;; updated.
298 ;;;
299 (defun local-call-analyze (component)
300 (declare (type component component))
301 (loop
302 (unless (component-new-functions component) (return))
303 (let* ((fun (pop (component-new-functions component)))
304 (kind (functional-kind fun)))
305 (cond ((member kind '(:deleted :let :mv-let :assignment)))
306 ((and (null (leaf-refs fun)) (eq kind nil)
307 (not (functional-entry-function fun)))
308 (delete-functional fun))
309 (t
310 (when (lambda-p fun)
311 (push fun (component-lambdas component)))
312 (local-call-analyze-1 fun)
313 (when (lambda-p fun)
314 (maybe-let-convert fun))))))
315
316 (undefined-value))
317
318
319 ;;; MAYBE-EXPAND-LOCAL-INLINE -- Internal
320 ;;;
321 ;;; If policy is auspicious, Call is not in an XEP, and we don't seem to be
322 ;;; in an infinite recursive loop, then change the reference to reference a
323 ;;; fresh copy. We return whichever function we decide to reference.
324 ;;;
325 (defun maybe-expand-local-inline (fun ref call)
326 (if (and (policy call (>= speed space) (>= speed cspeed))
327 (not (eq (functional-kind (node-home-lambda call)) :external))
328 (inline-expansion-ok call))
329 (with-ir1-environment call
330 (let* ((*lexical-environment* (functional-lexenv fun))
331 (won nil)
332 (res (catch 'local-call-lossage
333 (prog1
334 (ir1-convert-lambda (functional-inline-expansion fun))
335 (setq won t)))))
336 (cond (won
337 (change-ref-leaf ref res)
338 res)
339 (t
340 (let ((*compiler-error-context* call))
341 (compiler-note "Couldn't inline expand because expansion ~
342 calls this let-converted local function:~
343 ~% ~S"
344 (leaf-name res)))
345 fun))))
346 fun))
347
348
349 ;;; Convert-Call-If-Possible -- Interface
350 ;;;
351 ;;; Dispatch to the appropriate function to attempt to convert a call. This
352 ;;; is called in IR1 optimize as well as in local call analysis. If the call
353 ;;; is is already :Local, we do nothing. If the call is already scheduled for
354 ;;; deletion, also do nothing (in addition to saving time, this also avoids
355 ;;; some problems with optimizing collections of functions that are partially
356 ;;; deleted.)
357 ;;;
358 ;;; This is called both before and after FIND-INITIAL-DFO runs. When called
359 ;;; on a :INITIAL component, we don't care whether the caller and callee are in
360 ;;; the same component. Afterward, we must stick with whatever component
361 ;;; division we have chosen.
362 ;;;
363 ;;; Before attempting to convert a call, we see if the function is supposed
364 ;;; to be inline expanded. Call conversion proceeds as before after any
365 ;;; expansion.
366 ;;;
367 ;;; We bind *Compiler-Error-Context* to the node for the call so that
368 ;;; warnings will get the right context.
369 ;;;
370 ;;;
371 (defun convert-call-if-possible (ref call)
372 (declare (type ref ref) (type basic-combination call))
373 (let* ((block (node-block call))
374 (component (block-component block))
375 (original-fun (ref-leaf ref)))
376 (unless (or (member (basic-combination-kind call) '(:local :error))
377 (block-delete-p block)
378 (eq (functional-kind (block-home-lambda block)) :deleted)
379 (not (or (eq (component-kind component) :initial)
380 (eq (block-component
381 (node-block
382 (lambda-bind (main-entry original-fun))))
383 component))))
384 (let ((fun (if (external-entry-point-p original-fun)
385 (functional-entry-function original-fun)
386 original-fun))
387 (*compiler-error-context* call))
388
389 (when (and (eq (functional-inlinep fun) :inline)
390 (rest (leaf-refs original-fun)))
391 (setq fun (maybe-expand-local-inline fun ref call)))
392
393 (assert (member (functional-kind fun)
394 '(nil :escape :cleanup :optional)))
395 (cond ((mv-combination-p call)
396 (convert-mv-call ref call fun))
397 ((lambda-p fun)
398 (convert-lambda-call ref call fun))
399 (t
400 (convert-hairy-call ref call fun))))))
401
402 (undefined-value))
403
404
405 ;;; Convert-MV-Call -- Internal
406 ;;;
407 ;;; Attempt to convert a multiple-value call. The only interesting case is
408 ;;; a call to a function that Looks-Like-An-MV-Bind, has exactly one reference
409 ;;; and no XEP, and is called with one values continuation.
410 ;;;
411 ;;; We change the call to be to the last optional entry point and change the
412 ;;; call to be local. Due to our preconditions, the call should eventually be
413 ;;; converted to a let, but we can't do that now, since there may be stray
414 ;;; references to the e-p lambda due to optional defaulting code.
415 ;;;
416 ;;; We also use variable types for the called function to construct an
417 ;;; assertion for the values continuation.
418 ;;;
419 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
420 ;;;
421 (defun convert-mv-call (ref call fun)
422 (declare (type ref ref) (type mv-combination call) (type functional fun))
423 (when (and (looks-like-an-mv-bind fun)
424 (not (functional-entry-function fun))
425 (= (length (leaf-refs fun)) 1)
426 (= (length (basic-combination-args call)) 1))
427 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
428 (setf (basic-combination-kind call) :local)
429 (pushnew ep (lambda-calls (node-home-lambda call)))
430 (merge-tail-sets call ep)
431 (change-ref-leaf ref ep)
432
433 (assert-continuation-type
434 (first (basic-combination-args call))
435 (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
436 :rest *universal-type*))))
437 (undefined-value))
438
439
440 ;;; Convert-Lambda-Call -- Internal
441 ;;;
442 ;;; Attempt to convert a call to a lambda. If the number of args is wrong,
443 ;;; we give a warning and mark the call as :ERROR to remove it from future
444 ;;; consideration. If the argcount is O.K. then we just convert it.
445 ;;;
446 (defun convert-lambda-call (ref call fun)
447 (declare (type ref ref) (type combination call) (type clambda fun))
448 (let ((nargs (length (lambda-vars fun)))
449 (call-args (length (combination-args call))))
450 (cond ((= call-args nargs)
451 (convert-call ref call fun))
452 (t
453 (compiler-warning
454 "Function called with ~R argument~:P, but wants exactly ~R."
455 call-args nargs)
456 (setf (basic-combination-kind call) :error)))))
457
458
459
460 ;;;; Optional, more and keyword calls:
461
462 ;;; Convert-Hairy-Call -- Internal
463 ;;;
464 ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
465 ;;; only fixed args are supplied, then convert a call to the correct entry
466 ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
467 ;;; don't convert calls to functions that have a more (or rest) arg.
468 ;;;
469 (defun convert-hairy-call (ref call fun)
470 (declare (type ref ref) (type combination call)
471 (type optional-dispatch fun))
472 (let ((min-args (optional-dispatch-min-args fun))
473 (max-args (optional-dispatch-max-args fun))
474 (call-args (length (combination-args call))))
475 (cond ((< call-args min-args)
476 (compiler-warning "Function called with ~R argument~:P, but wants at least ~R."
477 call-args min-args)
478 (setf (basic-combination-kind call) :error))
479 ((<= call-args max-args)
480 (convert-call ref call
481 (elt (optional-dispatch-entry-points fun)
482 (- call-args min-args))))
483 ((optional-dispatch-more-entry fun)
484 (convert-more-call ref call fun))
485 (t
486 (compiler-warning "Function called with ~R argument~:P, but wants at most ~R."
487 call-args max-args)
488 (setf (basic-combination-kind call) :error))))
489 (undefined-value))
490
491
492 ;;; Convert-Hairy-Fun-Entry -- Internal
493 ;;;
494 ;;; This function is used to convert a call to an entry point when complex
495 ;;; transformations need to be done on the original arguments. Entry is the
496 ;;; entry point function that we are calling. Vars is a list of variable names
497 ;;; which are bound to the oringinal call arguments. Ignores is the subset of
498 ;;; Vars which are ignored. Args is the list of arguments to the entry point
499 ;;; function.
500 ;;;
501 ;;; In order to avoid gruesome graph grovelling, we introduce a new function
502 ;;; that rearranges the arguments and calls the entry point. We analyze the
503 ;;; new function and the entry point immediately so that everything gets
504 ;;; converted during the single pass.
505 ;;;
506 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
507 (declare (list vars ignores args) (type ref ref) (type combination call)
508 (type clambda entry))
509 (let ((new-fun
510 (with-ir1-environment call
511 (ir1-convert-lambda
512 `(lambda ,vars
513 (declare (ignorable . ,ignores))
514 (%funcall ,entry . ,args))))))
515 (convert-call ref call new-fun)
516 (dolist (ref (leaf-refs entry))
517 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
518
519
520 ;;; Convert-More-Call -- Internal
521 ;;;
522 ;;; Use Convert-Hairy-Fun-Entry to convert a more-arg call to a known
523 ;;; function into a local call to the Main-Entry.
524 ;;;
525 ;;; First we verify that all keywords are constant and legal. If there
526 ;;; aren't, then we warn the user and don't attempt to convert the call.
527 ;;;
528 ;;; We massage the supplied keyword arguments into the order expected by the
529 ;;; main entry. This is done by binding all the arguments to the keyword call
530 ;;; to variables in the introduced lambda, then passing these values variables
531 ;;; in the correct order when calling the main entry. Unused arguments
532 ;;; (such as the keywords themselves) are discarded simply by not passing them
533 ;;; along.
534 ;;;
535 ;;; If there is a rest arg, then we bundle up the args and pass them to
536 ;;; LIST.
537 ;;;
538 (defun convert-more-call (ref call fun)
539 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
540 (let* ((max (optional-dispatch-max-args fun))
541 (arglist (optional-dispatch-arglist fun))
542 (args (combination-args call))
543 (more (nthcdr max args))
544 (flame (policy call (or (> speed brevity) (> space brevity))))
545 (loser nil))
546 (collect ((temps)
547 (more-temps)
548 (ignores)
549 (supplied)
550 (key-vars))
551
552 (dolist (var arglist)
553 (let ((info (lambda-var-arg-info var)))
554 (when info
555 (ecase (arg-info-kind info)
556 (:keyword
557 (key-vars var))
558 ((:rest :optional))))))
559
560 (dotimes (i max)
561 (temps (gensym "FIXED-ARG-TEMP-")))
562
563 (dotimes (i (length more))
564 (more-temps (gensym "MORE-ARG-TEMP-")))
565
566 (when (optional-dispatch-keyp fun)
567 (when (oddp (length more))
568 (compiler-warning "Function called with odd number of ~
569 arguments in keyword portion.")
570
571 (setf (basic-combination-kind call) :error)
572 (return-from convert-more-call))
573
574 (do ((key more (cddr key))
575 (temp (more-temps) (cddr temp)))
576 ((null key))
577 (let ((cont (first key)))
578 (unless (constant-continuation-p cont)
579 (when flame
580 (compiler-note "Non-constant keyword in keyword call."))
581 (setf (basic-combination-kind call) :error)
582 (return-from convert-more-call))
583
584 (let ((name (continuation-value cont))
585 (dummy (first temp))
586 (val (second temp)))
587 (dolist (var (key-vars)
588 (progn
589 (ignores dummy val)
590 (setq loser name)))
591 (let ((info (lambda-var-arg-info var)))
592 (when (eq (arg-info-keyword info) name)
593 (ignores dummy)
594 (supplied (cons var val))
595 (return)))))))
596
597 (when (and loser (not (optional-dispatch-allowp fun)))
598 (compiler-warning "Function called with unknown argument keyword ~S."
599 loser)
600 (setf (basic-combination-kind call) :error)
601 (return-from convert-more-call)))
602
603 (collect ((call-args))
604 (do ((var arglist (cdr var))
605 (temp (temps) (cdr temp)))
606 (())
607 (let ((info (lambda-var-arg-info (car var))))
608 (if info
609 (ecase (arg-info-kind info)
610 (:optional
611 (call-args (car temp))
612 (when (arg-info-supplied-p info)
613 (call-args t)))
614 (:rest
615 (call-args `(list ,@(more-temps)))
616 (return))
617 (:keyword
618 (return)))
619 (call-args (car temp)))))
620
621 (dolist (var (key-vars))
622 (let ((info (lambda-var-arg-info var))
623 (temp (cdr (assoc var (supplied)))))
624 (if temp
625 (call-args temp)
626 (call-args (arg-info-default info)))
627 (when (arg-info-supplied-p info)
628 (call-args (not (null temp))))))
629
630 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
631 (append (temps) (more-temps))
632 (ignores) (call-args)))))
633
634 (undefined-value))
635
636
637 ;;;; Let conversion:
638 ;;;
639 ;;; Converting to a let has differing significance to various parts of the
640 ;;; compiler:
641 ;;; -- The body of a Let is spliced in immediately after the the corresponding
642 ;;; combination node, making the control transfer explicit and allowing lets
643 ;;; to mashed together into a single block. The value of the let is
644 ;;; delivered directly to the original continuation for the call,
645 ;;; eliminating the need to propagate information from the dummy result
646 ;;; continuation.
647 ;;; -- As far as IR1 optimization is concerned, it is interesting in that there
648 ;;; is only one expression that the variable can be bound to, and this is
649 ;;; easily substitited for.
650 ;;; -- Lets are interesting to environment analysis and the back end because in
651 ;;; most ways a let can be considered to be "the same function" as its home
652 ;;; function.
653 ;;; -- Let conversion has dynamic scope implications, since control transfers
654 ;;; within the same environment are local. In a local control transfer,
655 ;;; cleanup code must be emitted to remove dynamic bindings that are no
656 ;;; longer in effect.
657
658
659 ;;; Insert-Let-Body -- Internal
660 ;;;
661 ;;; Set up the control transfer to the called lambda. We split the call
662 ;;; block immediately after the call, and link the head of Fun to the call
663 ;;; block. The successor block after splitting (where we return to) is
664 ;;; returned.
665 ;;;
666 ;;; If the lambda is is a different component than the call, then we call
667 ;;; JOIN-COMPONENTS. This only happens in block compilation before
668 ;;; FIND-INITIAL-DFO.
669 ;;;
670 (defun insert-let-body (fun call)
671 (declare (type clambda fun) (type basic-combination call))
672 (let* ((call-block (node-block call))
673 (bind-block (node-block (lambda-bind fun)))
674 (component (block-component call-block)))
675 (let ((fun-component (block-component bind-block)))
676 (unless (eq fun-component component)
677 (assert (eq (component-kind component) :initial))
678 (join-components component fun-component)))
679
680 (let ((*current-component* component))
681 (node-ends-block call))
682 (assert (= (length (block-succ call-block)) 1))
683 (let ((next-block (first (block-succ call-block))))
684 (unlink-blocks call-block next-block)
685 (link-blocks call-block bind-block)
686 next-block)))
687
688
689 ;;; Merge-Lets -- Internal
690 ;;;
691 ;;; Handle the environment semantics of let conversion. We add the lambda
692 ;;; and its lets to lets for the Call's home function. We merge the calls for
693 ;;; Fun with the calls for the home function, removing Fun in the process. We
694 ;;; also merge the Entries.
695 ;;;
696 ;;; We also unlink the function head from the component head and set
697 ;;; Component-Reanalyze to true to indicate that the DFO should be recomputed.
698 ;;;
699 (defun merge-lets (fun call)
700 (declare (type clambda fun) (type basic-combination call))
701 (let ((component (block-component (node-block call))))
702 (unlink-blocks (component-head component) (node-block (lambda-bind fun)))
703 (setf (component-lambdas component)
704 (delete fun (component-lambdas component)))
705 (setf (component-reanalyze component) t))
706 (setf (lambda-call-lexenv fun) (node-lexenv call))
707 (let ((tails (lambda-tail-set fun)))
708 (setf (tail-set-functions tails)
709 (delete fun (tail-set-functions tails))))
710 (setf (lambda-tail-set fun) nil)
711 (let* ((home (node-home-lambda call))
712 (home-env (lambda-environment home)))
713 (push fun (lambda-lets home))
714 (setf (lambda-home fun) home)
715 (setf (lambda-environment fun) home-env)
716
717 (let ((lets (lambda-lets fun)))
718 (dolist (let lets)
719 (setf (lambda-home let) home)
720 (setf (lambda-environment let) home-env))
721
722 (setf (lambda-lets home) (nconc lets (lambda-lets home)))
723 (setf (lambda-lets fun) ()))
724
725 (setf (lambda-calls home)
726 (nunion (lambda-calls fun)
727 (delete fun (lambda-calls home))))
728 (setf (lambda-calls fun) ())
729
730 (setf (lambda-entries home)
731 (nconc (lambda-entries fun) (lambda-entries home)))
732 (setf (lambda-entries fun) ()))
733 (undefined-value))
734
735
736 ;;; Move-Return-Uses -- Internal
737 ;;;
738 ;;; Handle the value semantics of let conversion. Delete Fun's return node,
739 ;;; and change the control flow to transfer to Next-Block instead. Move all
740 ;;; the uses of the result continuation to Call's Cont.
741 ;;;
742 ;;; If the actual continuation is only used by the let call, then we
743 ;;; intersect the type assertion on the dummy continuation with the assertion
744 ;;; for the actual continuation; in all other cases assertions on the dummy
745 ;;; continuation are lost.
746 ;;;
747 ;;; We also intersect the derived type of the call with the derived type of
748 ;;; all the dummy continuation's uses. This serves mainly to propagate
749 ;;; TRULY-THE through lets.
750 ;;;
751 (defun move-return-uses (fun call next-block)
752 (declare (type clambda fun) (type basic-combination call)
753 (type cblock next-block))
754 (let* ((return (lambda-return fun))
755 (return-block (node-block return)))
756 (unlink-blocks return-block
757 (component-tail (block-component return-block)))
758 (link-blocks return-block next-block)
759 (unlink-node return)
760 (delete-return return)
761 (let ((result (return-result return))
762 (cont (node-cont call))
763 (call-type (node-derived-type call)))
764 (when (eq (continuation-use cont) call)
765 (assert-continuation-type cont (continuation-asserted-type result)))
766 (unless (eq call-type *wild-type*)
767 (do-uses (use result)
768 (derive-node-type use call-type)))
769 (substitute-continuation-uses cont result)))
770 (undefined-value))
771
772
773
774 ;;; MOVE-LET-CALL-CONT -- Internal
775 ;;;
776 ;;; Change all Cont for all the calls to Fun to be the start continuation
777 ;;; for the bind node. This allows the blocks to be joined if the caller count
778 ;;; ever goes to one.
779 ;;;
780 (defun move-let-call-cont (fun)
781 (declare (type clambda fun))
782 (let ((new-cont (node-prev (lambda-bind fun))))
783 (dolist (ref (leaf-refs fun))
784 (let ((dest (continuation-dest (node-cont ref))))
785 (delete-continuation-use dest)
786 (add-continuation-use dest new-cont))))
787 (undefined-value))
788
789
790 ;;; Unconvert-Tail-Calls -- Internal
791 ;;;
792 ;;; We are converting Fun to be a let when the call is in a non-tail
793 ;;; position. Any previously tail calls in Fun are no longer tail calls, and
794 ;;; must be restored to normal calls which transfer to Next-Block (Fun's
795 ;;; return point.) We can't do this by DO-USES on the RETURN-RESULT, because
796 ;;; the return might have been deleted (if all calls were TR.)
797 ;;;
798 ;;; The called function might be an assignment in the case where we are
799 ;;; currently converting that function. In steady-state, assignments never
800 ;;; appear in the lambda-calls.
801 ;;;
802 (defun unconvert-tail-calls (fun call next-block)
803 (dolist (called (lambda-calls fun))
804 (dolist (ref (leaf-refs called))
805 (let ((this-call (continuation-dest (node-cont ref))))
806 (when (and (node-tail-p this-call)
807 (eq (node-home-lambda this-call) fun))
808 (setf (node-tail-p this-call) nil)
809 (ecase (functional-kind called)
810 ((nil :cleanup :optional)
811 (let ((block (node-block this-call)))
812 (unlink-blocks block (first (block-succ block)))
813 (link-blocks block next-block)
814 (delete-continuation-use this-call)
815 (add-continuation-use this-call (node-cont call))))
816 (:assignment
817 (assert (eq called fun))))))))
818 (undefined-value))
819
820
821 ;;; MOVE-RETURN-STUFF -- Internal
822 ;;;
823 ;;; Deal with returning from a let or assignment that we are converting.
824 ;;; FUN is the function we are calling, CALL is a call to FUN, and NEXT-BLOCK
825 ;;; is the return point for a non-tail call, or NULL if call is a tail call.
826 ;;;
827 ;;; If the call is not a tail call, then we must do UNCONVERT-TAIL-CALLS, since
828 ;;; a tail call is a call which returns its value out of the enclosing non-let
829 ;;; function. When call is non-TR, we must convert it back to an ordinary
830 ;;; local call, since the value must be delivered to the receiver of CALL's
831 ;;; value.
832 ;;;
833 ;;; We do different things depending on whether the caller and callee have
834 ;;; returns left:
835 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT. Either the
836 ;;; function doesn't return, or all returns are via tail-recursive local
837 ;;; calls.
838 ;;; -- If CALL is a non-tail call, or if both have returns, then we
839 ;;; delete the callee's return, move its uses to the call's result
840 ;;; continuation, and transfer control to the appropriate return point.
841 ;;; -- If the callee has a return, but the caller doesn't, then we move the
842 ;;; return to the caller.
843 ;;;
844 (defun move-return-stuff (fun call next-block)
845 (declare (type clambda fun) (type basic-combination call)
846 (type (or cblock null) next-block))
847 (when next-block
848 (unconvert-tail-calls fun call next-block))
849 (let* ((return (lambda-return fun))
850 (call-fun (node-home-lambda call))
851 (call-return (lambda-return call-fun)))
852 (cond ((not return))
853 ((or next-block call-return)
854 (unless (block-delete-p (node-block return))
855 (move-return-uses fun call
856 (or next-block (node-block call-return)))))
857 (t
858 (assert (node-tail-p call))
859 (setf (lambda-return call-fun) return)
860 (setf (return-lambda return) call-fun))))
861 (move-let-call-cont fun)
862 (undefined-value))
863
864
865 ;;; Let-Convert -- Internal
866 ;;;
867 ;;; Actually do let conversion. We call subfunctions to do most of the
868 ;;; work. We change the Call's cont to be the continuation heading the bind
869 ;;; block, and also do Reoptimize-Continuation on the args and Cont so that
870 ;;; let-specific IR1 optimizations get a chance. We blow away any entry for
871 ;;; the function in *free-functions* so that nobody will create new reference
872 ;;; to it.
873 ;;;
874 (defun let-convert (fun call)
875 (declare (type clambda fun) (type basic-combination call))
876 (let ((next-block (if (node-tail-p call)
877 nil
878 (insert-let-body fun call))))
879 (move-return-stuff fun call next-block)
880 (merge-lets fun call))
881
882 (dolist (arg (basic-combination-args call))
883 (when arg
884 (reoptimize-continuation arg)))
885 (reoptimize-continuation (node-cont call))
886 (undefined-value))
887
888
889 ;;; Maybe-Let-Convert -- Interface
890 ;;;
891 ;;; This function is called when there is some reason to believe that
892 ;;; the lambda Fun might be converted into a let. This is done after local
893 ;;; call analysis, and also when a reference is deleted. We only convert to a
894 ;;; let when the function is a normal local function, has no XEP, and is
895 ;;; referenced in exactly one local call. Conversion is also inhibited if the
896 ;;; only reference is in a block about to be deleted. We return true if we
897 ;;; converted.
898 ;;;
899 ;;; These rules may seem unnecessarily restrictive, since there are some
900 ;;; cases where we could do the return with a jump that don't satisfy these
901 ;;; requirements. The reason for doing things this way is that it makes the
902 ;;; concept of a let much more useful at the level of IR1 semantics. The
903 ;;; :ASSIGNMENT function kind provides another way to optimize calls to
904 ;;; single-return/multiple call functions.
905 ;;;
906 ;;; We don't attempt to convert calls to functions that have an XEP, since
907 ;;; we might be embarrassed later when we want to convert a newly discovered
908 ;;; local call. We also don't convert calls to named functions which appear in
909 ;;; the initial component, delaying this until optimization. This minimizes
910 ;;; the likelyhood that we well let-convert a function which may have
911 ;;; references added due to later local inline expansion.
912 ;;;
913 (defun maybe-let-convert (fun)
914 (declare (type clambda fun))
915 (let ((refs (leaf-refs fun)))
916 (when (and refs (null (rest refs))
917 (member (functional-kind fun) '(nil :assignment))
918 (not (functional-entry-function fun)))
919 (let* ((ref-cont (node-cont (first refs)))
920 (dest (continuation-dest ref-cont)))
921 (when (and (basic-combination-p dest)
922 (eq (basic-combination-fun dest) ref-cont)
923 (eq (basic-combination-kind dest) :local)
924 (not (block-delete-p (node-block dest)))
925 (cond ((and (leaf-name fun)
926 (eq (component-kind
927 (block-component
928 (continuation-block ref-cont)))
929 :initial))
930 (reoptimize-continuation ref-cont)
931 nil)
932 (t t)))
933 (let-convert fun dest)
934 (setf (functional-kind fun)
935 (if (mv-combination-p dest) :mv-let :let))))
936 t)))
937
938
939 ;;;; Tail local calls and assignments:
940
941 ;;; ONLY-HARMLESS-CLEANUPS -- Internal
942 ;;;
943 ;;; Return T if there are no cleanups between Block1 and Block2, or if they
944 ;;; definitely won't generate any cleanup code. Currently we recognize lexical
945 ;;; entry points that are only used locally (if at all).
946 ;;;
947 (defun only-harmless-cleanups (block1 block2)
948 (declare (type cblock block1 block2))
949 (or (eq block1 block2)
950 (let ((cleanup2 (block-start-cleanup block2)))
951 (do ((cleanup (block-end-cleanup block1)
952 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
953 ((eq cleanup cleanup2) t)
954 (case (cleanup-kind cleanup)
955 ((:block :tagbody)
956 (unless (null (entry-exits (cleanup-mess-up cleanup)))
957 (return nil)))
958 (t (return nil)))))))
959
960
961 ;;; MAYBE-CONVERT-TAIL-LOCAL-CALL -- Interface
962 ;;;
963 ;;; If a potentially TR local call really is TR, then convert it to jump
964 ;;; directly to the called function. We also call MAYBE-CONVERT-TO-ASSIGNMENT.
965 ;;; We can switch the succesor (potentially deleting the RETURN node) unless:
966 ;;; -- The call has already been converted.
967 ;;; -- The call isn't TR (random implicit MV PROG1.)
968 ;;; -- The call is in an XEP (thus we might decide to make it non-tail so that
969 ;;; we can use known return inside the component.)
970 ;;; -- There is a change in the cleanup between the call in the return, so we
971 ;;; might need to introduce cleanup code.
972 ;;;
973 (defun maybe-convert-tail-local-call (call)
974 (declare (type combination call))
975 (let ((return (continuation-dest (node-cont call))))
976 (assert (return-p return))
977 (when (and (not (node-tail-p call))
978 (immediately-used-p (return-result return) call)
979 (not (eq (functional-kind (node-home-lambda call))
980 :external))
981 (only-harmless-cleanups (node-block call)
982 (node-block return)))
983 (node-ends-block call)
984 (let ((block (node-block call))
985 (fun (combination-lambda call)))
986 (setf (node-tail-p call) t)
987 (unlink-blocks block (first (block-succ block)))
988 (link-blocks block (node-block (lambda-bind fun)))
989 (values t (maybe-convert-to-assignment fun))))))
990
991
992 ;;; MAYBE-CONVERT-TO-ASSIGNMENT -- Interface
993 ;;;
994 ;;; Called when we believe it might make sense to convert Fun to an
995 ;;; assignment. All this function really does is determine when a function
996 ;;; with more than one call can still be combined with the calling function's
997 ;;; environment. We can convert when:
998 ;;; -- The function is a normal, non-entry function, and
999 ;;; -- Except for one call, all calls must be tail recursive calls in the
1000 ;;; called function (i.e. are self-recursive tail calls)
1001 ;;;
1002 ;;; There may be one outside call, and it need not be tail-recursive. Since
1003 ;;; all tail local calls have already been converted to direct transfers, the
1004 ;;; only control semantics needed are to splice in the body at the non-tail
1005 ;;; call. If there is no non-tail call, then we need only merge the
1006 ;;; environments. Both cases are handled by LET-CONVERT.
1007 ;;;
1008 ;;; ### It would actually be possible to allow any number of outside calls as
1009 ;;; long as they all return to the same place (i.e. have the same conceptual
1010 ;;; continuation.) A special case of this would be when all of the outside
1011 ;;; calls are tail recursive.
1012 ;;;
1013 (defun maybe-convert-to-assignment (fun)
1014 (declare (type clambda fun))
1015 (when (and (not (functional-kind fun))
1016 (not (functional-entry-function fun)))
1017 (let ((non-tail nil)
1018 (call-fun nil))
1019 (when (dolist (ref (leaf-refs fun) t)
1020 (let ((dest (continuation-dest (node-cont ref))))
1021 (when (block-delete-p (node-block dest)) (return nil))
1022 (let ((home (node-home-lambda ref)))
1023 (unless (eq home fun)
1024 (when call-fun (return nil))
1025 (setq call-fun home))
1026 (unless (node-tail-p dest)
1027 (when (or non-tail (eq home fun)) (return nil))
1028 (setq non-tail dest)))))
1029 (setf (functional-kind fun) :assignment)
1030 (let-convert fun (or non-tail
1031 (continuation-dest
1032 (node-cont (first (leaf-refs fun))))))
1033 t))))

  ViewVC Help
Powered by ViewVC 1.1.5