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

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5