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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5