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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5