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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations)
Sat Apr 20 14:12:00 1991 UTC (23 years ago) by ram
Branch: MAIN
Changes since 1.17: +7 -3 lines
Use the interface cookie when converting an XEP.
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.18 1991/04/20 14:12:00 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)
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 (unless (eq (functional-kind fun) :deleted)
276 (when (lambda-p fun)
277 (push fun (component-lambdas component)))
278 (local-call-analyze-1 fun)
279 (when (lambda-p fun)
280 (maybe-let-convert fun)))))
281
282 (undefined-value))
283
284
285 ;;; Convert-Call-If-Possible -- Interface
286 ;;;
287 ;;; Dispatch to the appropriate function to attempt to convert a call. This
288 ;;; is called in IR1 optimize as well as in local call analysis. If the call
289 ;;; is already :Local, we do nothing. If the call is in the top-level
290 ;;; component, also do nothing, since we don't want to join top-level code into
291 ;;; normal components.
292 ;;;
293 ;;; We bind *Compiler-Error-Context* to the node for the call so that
294 ;;; warnings will get the right context.
295 ;;;
296 (defun convert-call-if-possible (ref call)
297 (declare (type ref ref) (type basic-combination call))
298 (unless (eq (basic-combination-kind call) :local)
299 (let ((fun (let ((fun (ref-leaf ref)))
300 (if (external-entry-point-p fun)
301 (functional-entry-function fun)
302 fun)))
303 (*compiler-error-context* call))
304 (assert (member (functional-kind fun) '(nil :escape :cleanup :optional)))
305 (cond ((mv-combination-p call)
306 (convert-mv-call ref call fun))
307 ((lambda-p fun)
308 (convert-lambda-call ref call fun))
309 (t
310 (convert-hairy-call ref call fun)))))
311 (undefined-value))
312
313
314 ;;; Convert-MV-Call -- Internal
315 ;;;
316 ;;; Attempt to convert a multiple-value call. The only interesting case is
317 ;;; a call to a function that Looks-Like-An-MV-Bind, has exactly one reference
318 ;;; and no XEP, and is called with one values continuation.
319 ;;;
320 ;;; We change the call to be to the last optional entry point and change the
321 ;;; call to be local. Due to our preconditions, the call should eventually be
322 ;;; converted to a let, but we can't do that now, since there may be stray
323 ;;; references to the e-p lambda due to optional defaulting code.
324 ;;;
325 ;;; We also use variable types for the called function to construct an
326 ;;; assertion for the values continuation.
327 ;;;
328 (defun convert-mv-call (ref call fun)
329 (declare (type ref ref) (type mv-combination call) (type functional fun))
330 (when (and (looks-like-an-mv-bind fun)
331 (not (functional-entry-function fun))
332 (= (length (leaf-refs fun)) 1)
333 (= (length (basic-combination-args call)) 1))
334 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
335 (change-ref-leaf ref ep)
336 (setf (basic-combination-kind call) :local)
337 (pushnew ep (lambda-calls (node-home-lambda call)))
338
339 (assert-continuation-type
340 (first (basic-combination-args call))
341 (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
342 :rest *universal-type*))))
343 (undefined-value))
344
345
346 ;;; Convert-Lambda-Call -- Internal
347 ;;;
348 ;;; Attempt to convert a call to a lambda. If the number of args is wrong,
349 ;;; we give a warning and mark the Ref as :Notinline to remove it from future
350 ;;; consideration. If the argcount is O.K. then we just convert it.
351 ;;;
352 (defun convert-lambda-call (ref call fun)
353 (declare (type ref ref) (type combination call) (type clambda fun))
354 (let ((nargs (length (lambda-vars fun)))
355 (call-args (length (combination-args call))))
356 (cond ((= call-args nargs)
357 (convert-call ref call fun))
358 (t
359 (compiler-warning
360 "Function called with ~R argument~:P, but wants exactly ~R."
361 call-args nargs)
362 (setf (ref-inlinep ref) :notinline)))))
363
364
365
366 ;;;; Optional, more and keyword calls:
367
368 ;;; Convert-Hairy-Call -- Internal
369 ;;;
370 ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
371 ;;; only fixed args are supplied, then convert a call to the correct entry
372 ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
373 ;;; don't convert calls to functions that have a more (or rest) arg.
374 ;;;
375 (defun convert-hairy-call (ref call fun)
376 (declare (type ref ref) (type combination call)
377 (type optional-dispatch fun))
378 (let ((min-args (optional-dispatch-min-args fun))
379 (max-args (optional-dispatch-max-args fun))
380 (call-args (length (combination-args call))))
381 (cond ((< call-args min-args)
382 (compiler-warning "Function called with ~R argument~:P, but wants at least ~R."
383 call-args min-args)
384 (setf (ref-inlinep ref) :notinline))
385 ((<= call-args max-args)
386 (convert-call ref call
387 (elt (optional-dispatch-entry-points fun)
388 (- call-args min-args))))
389 ((optional-dispatch-more-entry fun)
390 (convert-more-call ref call fun))
391 (t
392 (compiler-warning "Function called with ~R argument~:P, but wants at most ~R."
393 call-args max-args)
394 (setf (ref-inlinep ref) :notinline))))
395
396 (undefined-value))
397
398
399 ;;; Convert-Hairy-Fun-Entry -- Internal
400 ;;;
401 ;;; This function is used to convert a call to an entry point when complex
402 ;;; transformations need to be done on the original arguments. Entry is the
403 ;;; entry point function that we are calling. Vars is a list of variable names
404 ;;; which are bound to the oringinal call arguments. Ignores is the subset of
405 ;;; Vars which are ignored. Args is the list of arguments to the entry point
406 ;;; function.
407 ;;;
408 ;;; In order to avoid gruesome graph grovelling, we introduce a new function
409 ;;; that rearranges the arguments and calls the entry point. We analyze the
410 ;;; new function and the entry point immediately so that everything gets
411 ;;; converted during the single pass.
412 ;;;
413 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
414 (declare (list vars ignores args) (type ref ref) (type combination call)
415 (type clambda entry))
416 (let ((new-fun
417 (with-ir1-environment call
418 (ir1-convert-lambda
419 `(lambda ,vars
420 (declare (ignorable . ,ignores))
421 (%funcall ,entry . ,args))))))
422 (convert-call ref call new-fun)
423 (dolist (ref (leaf-refs entry))
424 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
425
426
427 ;;; Convert-More-Call -- Internal
428 ;;;
429 ;;; Use Convert-Hairy-Fun-Entry to convert a more-arg call to a known
430 ;;; function into a local call to the Main-Entry.
431 ;;;
432 ;;; First we verify that all keywords are constant and legal. If there
433 ;;; aren't, then we warn the user and don't attempt to convert the call.
434 ;;;
435 ;;; We massage the supplied keyword arguments into the order expected by the
436 ;;; main entry. This is done by binding all the arguments to the keyword call
437 ;;; to variables in the introduced lambda, then passing these values variables
438 ;;; in the correct order when calling the main entry. Unused arguments
439 ;;; (such as the keywords themselves) are discarded simply by not passing them
440 ;;; along.
441 ;;;
442 ;;; If there is a rest arg, then we bundle up the args and pass them to
443 ;;; LIST.
444 ;;;
445 (defun convert-more-call (ref call fun)
446 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
447 (let* ((max (optional-dispatch-max-args fun))
448 (arglist (optional-dispatch-arglist fun))
449 (args (combination-args call))
450 (more (nthcdr max args))
451 (flame (policy call (or (> speed brevity) (> space brevity))))
452 (loser nil))
453 (collect ((temps)
454 (more-temps)
455 (ignores)
456 (supplied)
457 (key-vars))
458
459 (dolist (var arglist)
460 (let ((info (lambda-var-arg-info var)))
461 (when info
462 (ecase (arg-info-kind info)
463 (:keyword
464 (key-vars var))
465 ((:rest :optional))))))
466
467 (dotimes (i max)
468 (temps (gensym "FIXED-ARG-TEMP-")))
469
470 (dotimes (i (length more))
471 (more-temps (gensym "MORE-ARG-TEMP-")))
472
473 (when (optional-dispatch-keyp fun)
474 (when (oddp (length more))
475 (compiler-warning "Function called with odd number of ~
476 arguments in keyword portion.")
477 (setf (ref-inlinep ref) :notinline)
478 (return-from convert-more-call))
479
480 (do ((key more (cddr key))
481 (temp (more-temps) (cddr temp)))
482 ((null key))
483 (let ((cont (first key)))
484 (unless (constant-continuation-p cont)
485 (when flame
486 (compiler-note "Non-constant keyword in keyword call."))
487 (setf (ref-inlinep ref) :notinline)
488 (return-from convert-more-call))
489
490 (let ((name (continuation-value cont))
491 (dummy (first temp))
492 (val (second temp)))
493 (dolist (var (key-vars)
494 (progn
495 (ignores dummy val)
496 (setq loser name)))
497 (let ((info (lambda-var-arg-info var)))
498 (when (eq (arg-info-keyword info) name)
499 (ignores dummy)
500 (supplied (cons var val))
501 (return)))))))
502
503 (when (and loser (not (optional-dispatch-allowp fun)))
504 (compiler-warning "Function called with unknown argument keyword ~S."
505 loser)
506 (setf (ref-inlinep ref) :notinline)
507 (return-from convert-more-call)))
508
509 (collect ((call-args))
510 (do ((var arglist (cdr var))
511 (temp (temps) (cdr temp)))
512 (())
513 (let ((info (lambda-var-arg-info (car var))))
514 (if info
515 (ecase (arg-info-kind info)
516 (:optional
517 (call-args (car temp))
518 (when (arg-info-supplied-p info)
519 (call-args t)))
520 (:rest
521 (call-args `(list ,@(more-temps)))
522 (return))
523 (:keyword
524 (return)))
525 (call-args (car temp)))))
526
527 (dolist (var (key-vars))
528 (let ((info (lambda-var-arg-info var))
529 (temp (cdr (assoc var (supplied)))))
530 (if temp
531 (call-args temp)
532 (call-args (arg-info-default info)))
533 (when (arg-info-supplied-p info)
534 (call-args (not (null temp))))))
535
536 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
537 (append (temps) (more-temps))
538 (ignores) (call-args)))))
539
540 (undefined-value))
541
542
543 ;;;; Let conversion:
544 ;;;
545 ;;; Converting to a let has differing significance to various parts of the
546 ;;; compiler:
547 ;;; -- The body of a Let is spliced in immediately after the the corresponding
548 ;;; combination node, making the control transfer explicit and allowing lets
549 ;;; to mashed together into a single block. The value of the let is
550 ;;; delivered directly to the original continuation for the call,
551 ;;; eliminating the need to propagate information from the dummy result
552 ;;; continuation.
553 ;;; -- As far as IR1 optimization is concerned, it is interesting in that there
554 ;;; is only one expression that the variable can be bound to, and this is
555 ;;; easily substitited for.
556 ;;; -- Lets are interesting to environment analysis and the back end because in
557 ;;; most ways a let can be considered to be "the same function" as its home
558 ;;; function.
559 ;;; -- Let conversion has dynamic scope implications, since control transfers
560 ;;; within the same environment are local. In a local control transfer,
561 ;;; cleanup code must be emitted to remove dynamic bindings that are no
562 ;;; longer in effect.
563
564
565 ;;; Merge-Lets -- Internal
566 ;;;
567 ;;; Handle the environment semantics of let conversion. We add the lambda
568 ;;; and its lets to lets for the call's home function. We merge the calls for
569 ;;; Fun with the calls for the home function, removing Fun in the process. We
570 ;;; also merge the Entries.
571 ;;;
572 (defun merge-lets (fun call)
573 (declare (type clambda fun) (type basic-combination call))
574 (let* ((prev (node-prev call))
575 (home (block-home-lambda (continuation-block prev)))
576 (home-env (lambda-environment home)))
577 (push fun (lambda-lets home))
578 (setf (lambda-home fun) home)
579 (setf (lambda-environment fun) home-env)
580
581 (let ((lets (lambda-lets fun)))
582 (dolist (let lets)
583 (setf (lambda-home let) home)
584 (setf (lambda-environment let) home-env))
585
586 (setf (lambda-lets home) (nconc lets (lambda-lets home)))
587 (setf (lambda-lets fun) ()))
588
589 (setf (lambda-calls home)
590 (nunion (lambda-calls fun)
591 (delete fun (lambda-calls home))))
592 (setf (lambda-calls fun) ())
593
594 (setf (lambda-entries home)
595 (nconc (lambda-entries fun) (lambda-entries home)))
596 (setf (lambda-entries fun) ()))
597 (undefined-value))
598
599
600 ;;; Insert-Let-Body -- Internal
601 ;;;
602 ;;; Handle the control semantics of let conversion. We split the call block
603 ;;; immediately after the call, and link the head and tail of Fun to the call
604 ;;; block and the following block. We also unlink the function head and tail
605 ;;; from the component head and tail and flush the function from the
606 ;;; Component-Lambdas. We set Component-Reanalyze to true to indicate that the
607 ;;; DFO should be recomputed.
608 ;;;
609 ;;; If the lambda is is a different component than the call, then we call
610 ;;; JOIN-COMPONENTS. This only happens before the FIND-INITIAL-DFO in block
611 ;;; compilation.
612 ;;;
613 (defun insert-let-body (fun call)
614 (declare (type clambda fun) (type basic-combination call))
615 (setf (lambda-call-lexenv fun) (node-lexenv call))
616 (let* ((call-block (node-block call))
617 (bind-block (node-block (lambda-bind fun)))
618 (component (block-component call-block)))
619 (let ((fun-component (block-component bind-block)))
620 (unless (eq fun-component component)
621 (assert (eq (component-kind component) :initial))
622 (join-components component fun-component)))
623
624 (let ((*current-component* component))
625 (node-ends-block call))
626 (setf (component-lambdas component)
627 (delete fun (component-lambdas component)))
628 (assert (= (length (block-succ call-block)) 1))
629 (let ((next-block (first (block-succ call-block))))
630 (unlink-blocks call-block next-block)
631 (unlink-blocks (component-head component) bind-block)
632 (link-blocks call-block bind-block)
633 (let ((return (lambda-return fun)))
634 (when return
635 (let ((return-block (node-block return)))
636 (unlink-blocks return-block (component-tail component))
637 (link-blocks return-block next-block)))))
638 (setf (component-reanalyze component) t))
639 (undefined-value))
640
641
642 ;;; Move-Return-Uses -- Internal
643 ;;;
644 ;;; Handle the value semantics of let conversion. When Fun has a return
645 ;;; node, we delete it and move all the uses of the result continuation to
646 ;;; Call's Cont.
647 ;;;
648 ;;; If the actual continuation is only used by the let call, then we
649 ;;; intersect the type assertion on the dummy continuation with the assertion
650 ;;; for the actual continuation; in all other cases assertions on the dummy
651 ;;; continuation are lost.
652 ;;;
653 ;;; We also intersect the derived type of the call with the derived type of
654 ;;; all the dummy continuation's uses. This serves mainly to propagate
655 ;;; TRULY-THE through lets.
656 ;;;
657 (defun move-return-uses (fun call)
658 (declare (type clambda fun) (type basic-combination call))
659 (let ((return (lambda-return fun)))
660 (when return
661 (unlink-node return)
662 (delete-return return)
663
664 (let ((result (return-result return))
665 (cont (node-cont call))
666 (call-type (node-derived-type call)))
667 (when (eq (continuation-use cont) call)
668 (assert-continuation-type cont (continuation-asserted-type result)))
669 (unless (eq call-type *wild-type*)
670 (do-uses (use result)
671 (derive-node-type use call-type)))
672
673 (delete-continuation-use call)
674 (add-continuation-use call (node-prev (lambda-bind fun)))
675 (substitute-continuation-uses cont result))))
676
677 (undefined-value))
678
679
680 ;;; Let-Convert -- Internal
681 ;;;
682 ;;; Actually do let conversion. We call subfunctions to do most of the
683 ;;; work. We change the Call's cont to be the continuation heading the bind
684 ;;; block, and also do Reoptimize-Continuation on the args and Cont so that
685 ;;; let-specific IR1 optimizations get a chance. We blow away any entry for
686 ;;; the function in *free-functions* so that nobody will create new reference
687 ;;; to it.
688 ;;;
689 (defun let-convert (fun call)
690 (declare (type clambda fun) (type basic-combination call))
691 (insert-let-body fun call)
692 (merge-lets fun call)
693 (move-return-uses fun call)
694 (maybe-remove-free-function fun)
695 (dolist (arg (basic-combination-args call))
696 (when arg
697 (reoptimize-continuation arg)))
698 (reoptimize-continuation (node-cont call))
699 (undefined-value))
700
701
702 ;;; Maybe-Let-Convert -- Interface
703 ;;;
704 ;;; This function is called when there is some reason to believe that
705 ;;; the lambda Fun might be converted into a let. This is done after local
706 ;;; call analysis, and also when a reference is deleted. We only convert to a
707 ;;; let when the function is a normal local function, has no XEP, and is
708 ;;; referenced in exactly one local call. Conversion is also inhibited if the
709 ;;; only reference is in a block about to be deleted.
710 ;;;
711 ;;; These rules may seem unnecessarily restrictive, since there are some
712 ;;; cases where we could do the return with a jump that don't satisfy these
713 ;;; requirements. The reason for doing things this way is that it makes the
714 ;;; concept of a let much more useful at the level of IR1 semantics. Low-level
715 ;;; control and environment optimizations can always be done later on.
716 ;;;
717 ;;; We don't attempt to convert calls to functions that have an XEP, since
718 ;;; we might be embarrassed later when we want to convert a newly discovered
719 ;;; local call.
720 ;;;
721 (defun maybe-let-convert (fun)
722 (declare (type clambda fun))
723 (let ((refs (leaf-refs fun)))
724 (when (and refs (null (rest refs))
725 (not (block-delete-p (node-block (first refs))))
726 (not (functional-kind fun))
727 (not (functional-entry-function fun)))
728 (let* ((ref-cont (node-cont (first refs)))
729 (dest (continuation-dest ref-cont)))
730 (when (and (basic-combination-p dest)
731 (eq (basic-combination-fun dest) ref-cont)
732 (eq (basic-combination-kind dest) :local))
733 (let-convert fun dest)
734 (setf (functional-kind fun)
735 (if (mv-combination-p dest) :mv-let :let))))))
736 (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5