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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5