1 ;;;; This file implements local call analysis. A local call is a
2 ;;;; function call between functions being compiled at the same time.
3 ;;;; If we can tell at compile time that such a call is legal, then we
4 ;;;; change the combination to call the correct lambda, mark it as
5 ;;;; local, and add this link to our call graph. Once a call is local,
6 ;;;; it is then eligible for let conversion, which places the body of
7 ;;;; the function inline.
9 ;;;; We cannot always do a local call even when we do have the
10 ;;;; function being called. Calls that cannot be shown to have legal
11 ;;;; arg counts are not converted.
13 ;;;; This software is part of the SBCL system. See the README file for
14 ;;;; more information.
16 ;;;; This software is derived from the CMU CL system, which was
17 ;;;; written at Carnegie Mellon University and released into the
18 ;;;; public domain. The software is in the public domain and is
19 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
20 ;;;; files for more information.
24 ;;; This function propagates information from the variables in the function
25 ;;; Fun to the actual arguments in Call. This is also called by the VALUES IR1
26 ;;; optimizer when it sleazily converts MV-BINDs to LETs.
28 ;;; We flush all arguments to Call that correspond to unreferenced variables
29 ;;; in Fun. We leave NILs in the Combination-Args so that the remaining args
30 ;;; still match up with their vars.
32 ;;; We also apply the declared variable type assertion to the argument
34 (defun propagate-to-args (call fun)
35 (declare (type combination call) (type clambda fun))
36 (do ((args (basic-combination-args call) (cdr args))
37 (vars (lambda-vars fun) (cdr vars)))
39 (let ((arg (car args))
41 (cond ((leaf-refs var)
42 (assert-continuation-type arg (leaf-type var)))
45 (setf (car args) nil)))))
49 ;;; This function handles merging the tail sets if Call is potentially
50 ;;; tail-recursive, and is a call to a function with a different TAIL-SET than
51 ;;; Call's Fun. This must be called whenever we alter IR1 so as to place a
52 ;;; local call in what might be a TR context. Note that any call which returns
53 ;;; its value to a RETURN is considered potentially TR, since any implicit
54 ;;; MV-PROG1 might be optimized away.
56 ;;; We destructively modify the set for the calling function to represent both,
57 ;;; and then change all the functions in callee's set to reference the first.
58 ;;; If we do merge, we reoptimize the RETURN-RESULT continuation to cause
59 ;;; IR1-OPTIMIZE-RETURN to recompute the tail set type.
60 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
61 (declare (type basic-combination call) (type clambda new-fun))
62 (let ((return (continuation-dest (node-cont call))))
63 (when (return-p return)
64 (let ((call-set (lambda-tail-set (node-home-lambda call)))
65 (fun-set (lambda-tail-set new-fun)))
66 (unless (eq call-set fun-set)
67 (let ((funs (tail-set-functions fun-set)))
69 (setf (lambda-tail-set fun) call-set))
70 (setf (tail-set-functions call-set)
71 (nconc (tail-set-functions call-set) funs)))
72 (reoptimize-continuation (return-result return))
75 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
76 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
77 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
78 ;;; the function in the REF node with the new function.
80 ;;; We change the REF last, since changing the reference can trigger
81 ;;; LET conversion of the new function, but will only do so if the
82 ;;; call is local. Note that the replacement may trigger LET
83 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
84 ;;; with NEW-FUN before the substitution, since after the substitution
85 ;;; (and LET conversion), the call may no longer be recognizable as
87 (defun convert-call (ref call fun)
88 (declare (type ref ref) (type combination call) (type clambda fun))
89 (propagate-to-args call fun)
90 (setf (basic-combination-kind call) :local)
91 (pushnew fun (lambda-calls (node-home-lambda call)))
92 (merge-tail-sets call fun)
93 (change-ref-leaf ref fun)
96 ;;;; external entry point creation
98 ;;; Return a Lambda form that can be used as the definition of the XEP
101 ;;; If FUN is a lambda, then we check the number of arguments
102 ;;; (conditional on policy) and call FUN with all the arguments.
104 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
105 ;;; of supplied arguments by doing do an = test for each entry-point,
106 ;;; calling the entry with the appropriate prefix of the passed
109 ;;; If there is a more arg, then there are a couple of optimizations
110 ;;; that we make (more for space than anything else):
111 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
112 ;;; no argument count error is possible.
113 ;;; -- We can omit the = clause for the last entry-point, allowing the
114 ;;; case of 0 more args to fall through to the more entry.
116 ;;; We don't bother to policy conditionalize wrong arg errors in
117 ;;; optional dispatches, since the additional overhead is negligible
118 ;;; compared to the cost of everything else going on.
120 ;;; Note that if policy indicates it, argument type declarations in
121 ;;; Fun will be verified. Since nothing is known about the type of the
122 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
123 ;;; are passed to the actual function.
124 (defun make-xep-lambda (fun)
125 (declare (type functional fun))
128 (let ((nargs (length (lambda-vars fun)))
129 (n-supplied (gensym))
130 (temps (make-gensym-list (length (lambda-vars fun)))))
131 `(lambda (,n-supplied ,@temps)
132 (declare (type index ,n-supplied))
133 ,(if (policy *lexenv* (zerop safety))
134 `(declare (ignore ,n-supplied))
135 `(%verify-argument-count ,n-supplied ,nargs))
136 (%funcall ,fun ,@temps))))
138 (let* ((min (optional-dispatch-min-args fun))
139 (max (optional-dispatch-max-args fun))
140 (more (optional-dispatch-more-entry fun))
141 (n-supplied (gensym))
142 (temps (make-gensym-list max)))
144 (do ((eps (optional-dispatch-entry-points fun) (rest eps))
147 (entries `((= ,n-supplied ,n)
148 (%funcall ,(first eps) ,@(subseq temps 0 n)))))
149 `(lambda (,n-supplied ,@temps)
150 ;; FIXME: Make sure that INDEX type distinguishes between
151 ;; target and host. (Probably just make the SB!XC:DEFTYPE
152 ;; different from CL:DEFTYPE.)
153 (declare (type index ,n-supplied))
155 ,@(if more (butlast (entries)) (entries))
157 `((,(if (zerop min) t `(>= ,n-supplied ,max))
158 ,(let ((n-context (gensym))
160 `(multiple-value-bind (,n-context ,n-count)
161 (%more-arg-context ,n-supplied ,max)
162 (%funcall ,more ,@temps ,n-context ,n-count))))))
164 (%argument-count-error ,n-supplied)))))))))
166 ;;; Make an external entry point (XEP) for FUN and return it. We
167 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
168 ;;; then associate this lambda with FUN as its XEP. After the
169 ;;; conversion, we iterate over the function's associated lambdas,
170 ;;; redoing local call analysis so that the XEP calls will get
173 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
174 ;;; discover an XEP after the initial local call analyze pass.
175 (defun make-external-entry-point (fun)
176 (declare (type functional fun))
177 (aver (not (functional-entry-function fun)))
178 (with-ir1-environment (lambda-bind (main-entry fun))
179 (let ((res (ir1-convert-lambda (make-xep-lambda fun))))
180 (setf (functional-kind res) :external
181 (leaf-ever-used res) t
182 (functional-entry-function res) fun
183 (functional-entry-function fun) res
184 (component-reanalyze *current-component*) t
185 (component-reoptimize *current-component*) t)
187 (clambda (local-call-analyze-1 fun))
189 (dolist (ep (optional-dispatch-entry-points fun))
190 (local-call-analyze-1 ep))
191 (when (optional-dispatch-more-entry fun)
192 (local-call-analyze-1 (optional-dispatch-more-entry fun)))))
195 ;;; Notice a Ref that is not in a local-call context. If the Ref is
196 ;;; already to an XEP, then do nothing, otherwise change it to the
197 ;;; XEP, making an XEP if necessary.
199 ;;; If Ref is to a special :Cleanup or :Escape function, then we treat
200 ;;; it as though it was not an XEP reference (i.e. leave it alone.)
201 (defun reference-entry-point (ref)
202 (declare (type ref ref))
203 (let ((fun (ref-leaf ref)))
204 (unless (or (external-entry-point-p fun)
205 (member (functional-kind fun) '(:escape :cleanup)))
206 (change-ref-leaf ref (or (functional-entry-function fun)
207 (make-external-entry-point fun))))))
209 ;;; Attempt to convert all references to Fun to local calls. The
210 ;;; reference must be the function for a call, and the function
211 ;;; continuation must be used only once, since otherwise we cannot be
212 ;;; sure what function is to be called. The call continuation would be
213 ;;; multiply used if there is hairy stuff such as conditionals in the
214 ;;; expression that computes the function.
216 ;;; If we cannot convert a reference, then we mark the referenced
217 ;;; function as an entry-point, creating a new XEP if necessary. We
218 ;;; don't try to convert calls that are in error (:ERROR kind.)
220 ;;; This is broken off from Local-Call-Analyze so that people can
221 ;;; force analysis of newly introduced calls. Note that we don't do
222 ;;; LET conversion here.
223 (defun local-call-analyze-1 (fun)
224 (declare (type functional fun))
225 (let ((refs (leaf-refs fun))
228 (let* ((cont (node-cont ref))
229 (dest (continuation-dest cont)))
230 (cond ((and (basic-combination-p dest)
231 (eq (basic-combination-fun dest) cont)
232 (eq (continuation-use cont) ref))
234 (convert-call-if-possible ref dest)
236 (unless (eq (basic-combination-kind dest) :local)
237 (reference-entry-point ref)))
239 (reference-entry-point ref))))
240 (setq first-time nil)))
244 ;;; We examine all New-Functions in component, attempting to convert
245 ;;; calls into local calls when it is legal. We also attempt to
246 ;;; convert each lambda to a LET. LET conversion is also triggered by
247 ;;; deletion of a function reference, but functions that start out
248 ;;; eligible for conversion must be noticed sometime.
250 ;;; Note that there is a lot of action going on behind the scenes
251 ;;; here, triggered by reference deletion. In particular, the
252 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and let
253 ;;; converted lambdas, so it is important that the lambda is added to
254 ;;; the COMPONENT-LAMBDAS when it is. Also, the
255 ;;; COMPONENT-NEW-FUNCTIONS may contain all sorts of drivel, since it
256 ;;; is not updated when we delete functions, etc. Only
257 ;;; COMPONENT-LAMBDAS is updated.
259 ;;; COMPONENT-REANALYZE-FUNCTIONS is treated similarly to
260 ;;; NEW-FUNCTIONS, but we don't add lambdas to the LAMBDAS.
261 (defun local-call-analyze (component)
262 (declare (type component component))
264 (let* ((new (pop (component-new-functions component)))
265 (fun (or new (pop (component-reanalyze-functions component)))))
266 (unless fun (return))
267 (let ((kind (functional-kind fun)))
268 (cond ((member kind '(:deleted :let :mv-let :assignment)))
269 ((and (null (leaf-refs fun)) (eq kind nil)
270 (not (functional-entry-function fun)))
271 (delete-functional fun))
273 (when (and new (lambda-p fun))
274 (push fun (component-lambdas component)))
275 (local-call-analyze-1 fun)
277 (maybe-let-convert fun)))))))
281 ;;; If policy is auspicious, CALL is not in an XEP, and we don't seem
282 ;;; to be in an infinite recursive loop, then change the reference to
283 ;;; reference a fresh copy. We return whichever function we decide to
285 (defun maybe-expand-local-inline (fun ref call)
286 (if (and (policy call
287 (and (>= speed space) (>= speed compilation-speed)))
288 (not (eq (functional-kind (node-home-lambda call)) :external))
289 (not *converting-for-interpreter*)
290 (inline-expansion-ok call))
291 (with-ir1-environment call
292 (let* ((*lexenv* (functional-lexenv fun))
294 (res (catch 'local-call-lossage
296 (ir1-convert-lambda (functional-inline-expansion
300 (change-ref-leaf ref res)
303 (let ((*compiler-error-context* call))
304 (compiler-note "couldn't inline expand because expansion ~
305 calls this let-converted local function:~
311 ;;; Dispatch to the appropriate function to attempt to convert a call. Ref
312 ;;; most be a reference to a FUNCTIONAL. This is called in IR1 optimize as
313 ;;; well as in local call analysis. If the call is is already :Local, we do
314 ;;; nothing. If the call is already scheduled for deletion, also do nothing
315 ;;; (in addition to saving time, this also avoids some problems with optimizing
316 ;;; collections of functions that are partially deleted.)
318 ;;; This is called both before and after FIND-INITIAL-DFO runs. When called
319 ;;; on a :INITIAL component, we don't care whether the caller and callee are in
320 ;;; the same component. Afterward, we must stick with whatever component
321 ;;; division we have chosen.
323 ;;; Before attempting to convert a call, we see whether the function is
324 ;;; supposed to be inline expanded. Call conversion proceeds as before
325 ;;; after any expansion.
327 ;;; We bind *Compiler-Error-Context* to the node for the call so that
328 ;;; warnings will get the right context.
329 (defun convert-call-if-possible (ref call)
330 (declare (type ref ref) (type basic-combination call))
331 (let* ((block (node-block call))
332 (component (block-component block))
333 (original-fun (ref-leaf ref)))
334 (aver (functional-p original-fun))
335 (unless (or (member (basic-combination-kind call) '(:local :error))
336 (block-delete-p block)
337 (eq (functional-kind (block-home-lambda block)) :deleted)
338 (member (functional-kind original-fun)
339 '(:top-level-xep :deleted))
340 (not (or (eq (component-kind component) :initial)
343 (lambda-bind (main-entry original-fun))))
345 (let ((fun (if (external-entry-point-p original-fun)
346 (functional-entry-function original-fun)
348 (*compiler-error-context* call))
350 (when (and (eq (functional-inlinep fun) :inline)
351 (rest (leaf-refs original-fun)))
352 (setq fun (maybe-expand-local-inline fun ref call)))
354 (aver (member (functional-kind fun)
355 '(nil :escape :cleanup :optional)))
356 (cond ((mv-combination-p call)
357 (convert-mv-call ref call fun))
359 (convert-lambda-call ref call fun))
361 (convert-hairy-call ref call fun))))))
365 ;;; Attempt to convert a multiple-value call. The only interesting
366 ;;; case is a call to a function that Looks-Like-An-MV-Bind, has
367 ;;; exactly one reference and no XEP, and is called with one values
370 ;;; We change the call to be to the last optional entry point and
371 ;;; change the call to be local. Due to our preconditions, the call
372 ;;; should eventually be converted to a let, but we can't do that now,
373 ;;; since there may be stray references to the e-p lambda due to
374 ;;; optional defaulting code.
376 ;;; We also use variable types for the called function to construct an
377 ;;; assertion for the values continuation.
379 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
380 (defun convert-mv-call (ref call fun)
381 (declare (type ref ref) (type mv-combination call) (type functional fun))
382 (when (and (looks-like-an-mv-bind fun)
383 (not (functional-entry-function fun))
384 (= (length (leaf-refs fun)) 1)
385 (= (length (basic-combination-args call)) 1))
386 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
387 (setf (basic-combination-kind call) :local)
388 (pushnew ep (lambda-calls (node-home-lambda call)))
389 (merge-tail-sets call ep)
390 (change-ref-leaf ref ep)
392 (assert-continuation-type
393 (first (basic-combination-args call))
394 (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
395 :rest *universal-type*))))
398 ;;; Attempt to convert a call to a lambda. If the number of args is
399 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
400 ;;; from future consideration. If the argcount is O.K. then we just
402 (defun convert-lambda-call (ref call fun)
403 (declare (type ref ref) (type combination call) (type clambda fun))
404 (let ((nargs (length (lambda-vars fun)))
405 (call-args (length (combination-args call))))
406 (cond ((= call-args nargs)
407 (convert-call ref call fun))
409 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
410 ;; Compiler" that calling a function with "the wrong number of
411 ;; arguments" be only a STYLE-ERROR. I think, though, that this
412 ;; should only apply when the number of arguments is inferred
413 ;; from a previous definition. If the number of arguments
414 ;; is DECLAIMed, surely calling with the wrong number is a
415 ;; real WARNING. As long as SBCL continues to use CMU CL's
416 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
417 ;; but as long as we continue to use that policy, that's the
418 ;; not our biggest problem.:-| When we fix that policy, this
419 ;; should come back into compliance. (So fix that policy!)
421 ;; FIXME, continued: Except that section "3.2.2.3 Semantic
422 ;; Constraints" says that if it's within the same file, it's
423 ;; wrong. And we're in locall.lisp here, so it's probably
424 ;; (haven't checked this..) a call to something in the same
425 ;; file. So maybe it deserves a full warning anyway.
427 "function called with ~R argument~:P, but wants exactly ~R"
429 (setf (basic-combination-kind call) :error)))))
431 ;;;; optional, more and keyword calls
433 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
434 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
435 ;;; a call to the correct entry point. If &KEY args are supplied, then
436 ;;; dispatch to a subfunction. We don't convert calls to functions
437 ;;; that have a &MORE (or &REST) arg.
438 (defun convert-hairy-call (ref call fun)
439 (declare (type ref ref) (type combination call)
440 (type optional-dispatch fun))
441 (let ((min-args (optional-dispatch-min-args fun))
442 (max-args (optional-dispatch-max-args fun))
443 (call-args (length (combination-args call))))
444 (cond ((< call-args min-args)
445 ;; FIXME: See FIXME note at the previous
446 ;; wrong-number-of-arguments warnings in this file.
448 "function called with ~R argument~:P, but wants at least ~R"
450 (setf (basic-combination-kind call) :error))
451 ((<= call-args max-args)
452 (convert-call ref call
453 (elt (optional-dispatch-entry-points fun)
454 (- call-args min-args))))
455 ((optional-dispatch-more-entry fun)
456 (convert-more-call ref call fun))
458 ;; FIXME: See FIXME note at the previous
459 ;; wrong-number-of-arguments warnings in this file.
461 "function called with ~R argument~:P, but wants at most ~R"
463 (setf (basic-combination-kind call) :error))))
466 ;;; This function is used to convert a call to an entry point when complex
467 ;;; transformations need to be done on the original arguments. Entry is the
468 ;;; entry point function that we are calling. Vars is a list of variable names
469 ;;; which are bound to the original call arguments. Ignores is the subset of
470 ;;; Vars which are ignored. Args is the list of arguments to the entry point
473 ;;; In order to avoid gruesome graph grovelling, we introduce a new function
474 ;;; that rearranges the arguments and calls the entry point. We analyze the
475 ;;; new function and the entry point immediately so that everything gets
476 ;;; converted during the single pass.
477 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
478 (declare (list vars ignores args) (type ref ref) (type combination call)
479 (type clambda entry))
481 (with-ir1-environment call
484 (declare (ignorable . ,ignores))
485 (%funcall ,entry . ,args))))))
486 (convert-call ref call new-fun)
487 (dolist (ref (leaf-refs entry))
488 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
490 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
491 ;;; function into a local call to the MAIN-ENTRY.
493 ;;; First we verify that all keywords are constant and legal. If there
494 ;;; aren't, then we warn the user and don't attempt to convert the call.
496 ;;; We massage the supplied &KEY arguments into the order expected
497 ;;; by the main entry. This is done by binding all the arguments to
498 ;;; the keyword call to variables in the introduced lambda, then
499 ;;; passing these values variables in the correct order when calling
500 ;;; the main entry. Unused arguments (such as the keywords themselves)
501 ;;; are discarded simply by not passing them along.
503 ;;; If there is a &REST arg, then we bundle up the args and pass them
505 (defun convert-more-call (ref call fun)
506 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
507 (let* ((max (optional-dispatch-max-args fun))
508 (arglist (optional-dispatch-arglist fun))
509 (args (combination-args call))
510 (more (nthcdr max args))
511 (flame (policy call (or (> speed inhibit-warnings)
512 (> space inhibit-warnings))))
514 (temps (make-gensym-list max))
515 (more-temps (make-gensym-list (length more))))
520 (dolist (var arglist)
521 (let ((info (lambda-var-arg-info var)))
523 (ecase (arg-info-kind info)
527 ((:more-context :more-count)
528 (compiler-warning "can't local-call functions with &MORE args")
529 (setf (basic-combination-kind call) :error)
530 (return-from convert-more-call))))))
532 (when (optional-dispatch-keyp fun)
533 (when (oddp (length more))
534 (compiler-warning "function called with odd number of ~
535 arguments in keyword portion")
537 (setf (basic-combination-kind call) :error)
538 (return-from convert-more-call))
540 (do ((key more (cddr key))
541 (temp more-temps (cddr temp)))
543 (let ((cont (first key)))
544 (unless (constant-continuation-p cont)
546 (compiler-note "non-constant keyword in keyword call"))
547 (setf (basic-combination-kind call) :error)
548 (return-from convert-more-call))
550 (let ((name (continuation-value cont))
553 (dolist (var (key-vars)
557 (let ((info (lambda-var-arg-info var)))
558 (when (eq (arg-info-key info) name)
560 (supplied (cons var val))
563 (when (and loser (not (optional-dispatch-allowp fun)))
564 (compiler-warning "function called with unknown argument keyword ~S"
566 (setf (basic-combination-kind call) :error)
567 (return-from convert-more-call)))
569 (collect ((call-args))
570 (do ((var arglist (cdr var))
571 (temp temps (cdr temp)))
573 (let ((info (lambda-var-arg-info (car var))))
575 (ecase (arg-info-kind info)
577 (call-args (car temp))
578 (when (arg-info-supplied-p info)
581 (call-args `(list ,@more-temps))
585 (call-args (car temp)))))
587 (dolist (var (key-vars))
588 (let ((info (lambda-var-arg-info var))
589 (temp (cdr (assoc var (supplied)))))
592 (call-args (arg-info-default info)))
593 (when (arg-info-supplied-p info)
594 (call-args (not (null temp))))))
596 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
597 (append temps more-temps)
598 (ignores) (call-args)))))
604 ;;;; Converting to a LET has differing significance to various parts
605 ;;;; of the compiler:
606 ;;;; -- The body of a LET is spliced in immediately after the
607 ;;;; corresponding combination node, making the control transfer
608 ;;;; explicit and allowing LETs to be mashed together into a single
609 ;;;; block. The value of the LET is delivered directly to the
610 ;;;; original continuation for the call,eliminating the need to
611 ;;;; propagate information from the dummy result continuation.
612 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
613 ;;;; that there is only one expression that the variable can be bound
614 ;;;; to, and this is easily substitited for.
615 ;;;; -- LETs are interesting to environment analysis and to the back
616 ;;;; end because in most ways a LET can be considered to be "the
617 ;;;; same function" as its home function.
618 ;;;; -- LET conversion has dynamic scope implications, since control
619 ;;;; transfers within the same environment are local. In a local
620 ;;;; control transfer, cleanup code must be emitted to remove
621 ;;;; dynamic bindings that are no longer in effect.
623 ;;; Set up the control transfer to the called lambda. We split the
624 ;;; call block immediately after the call, and link the head of FUN to
625 ;;; the call block. The successor block after splitting (where we
626 ;;; return to) is returned.
628 ;;; If the lambda is is a different component than the call, then we
629 ;;; call JOIN-COMPONENTS. This only happens in block compilation
630 ;;; before FIND-INITIAL-DFO.
631 (defun insert-let-body (fun call)
632 (declare (type clambda fun) (type basic-combination call))
633 (let* ((call-block (node-block call))
634 (bind-block (node-block (lambda-bind fun)))
635 (component (block-component call-block)))
636 (let ((fun-component (block-component bind-block)))
637 (unless (eq fun-component component)
638 (aver (eq (component-kind component) :initial))
639 (join-components component fun-component)))
641 (let ((*current-component* component))
642 (node-ends-block call))
643 ;; FIXME: Use PROPER-LIST-OF-LENGTH-P here, and look for other
644 ;; uses of '=.*length' which could also be converted to use
645 ;; PROPER-LIST-OF-LENGTH-P.
646 (aver (= (length (block-succ call-block)) 1))
647 (let ((next-block (first (block-succ call-block))))
648 (unlink-blocks call-block next-block)
649 (link-blocks call-block bind-block)
652 ;;; Handle the environment semantics of LET conversion. We add the
653 ;;; lambda and its LETs to lets for the CALL's home function. We merge
654 ;;; the calls for FUN with the calls for the home function, removing
655 ;;; FUN in the process. We also merge the Entries.
657 ;;; We also unlink the function head from the component head and set
658 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
660 (defun merge-lets (fun call)
661 (declare (type clambda fun) (type basic-combination call))
662 (let ((component (block-component (node-block call))))
663 (unlink-blocks (component-head component) (node-block (lambda-bind fun)))
664 (setf (component-lambdas component)
665 (delete fun (component-lambdas component)))
666 (setf (component-reanalyze component) t))
667 (setf (lambda-call-lexenv fun) (node-lexenv call))
668 (let ((tails (lambda-tail-set fun)))
669 (setf (tail-set-functions tails)
670 (delete fun (tail-set-functions tails))))
671 (setf (lambda-tail-set fun) nil)
672 (let* ((home (node-home-lambda call))
673 (home-env (lambda-environment home)))
674 (push fun (lambda-lets home))
675 (setf (lambda-home fun) home)
676 (setf (lambda-environment fun) home-env)
678 (let ((lets (lambda-lets fun)))
680 (setf (lambda-home let) home)
681 (setf (lambda-environment let) home-env))
683 (setf (lambda-lets home) (nconc lets (lambda-lets home)))
684 (setf (lambda-lets fun) ()))
686 (setf (lambda-calls home)
687 (delete fun (nunion (lambda-calls fun) (lambda-calls home))))
688 (setf (lambda-calls fun) ())
690 (setf (lambda-entries home)
691 (nconc (lambda-entries fun) (lambda-entries home)))
692 (setf (lambda-entries fun) ()))
695 ;;; Handle the value semantics of LET conversion. Delete FUN's return
696 ;;; node, and change the control flow to transfer to NEXT-BLOCK
697 ;;; instead. Move all the uses of the result continuation to CALL's
700 ;;; If the actual continuation is only used by the LET call, then we
701 ;;; intersect the type assertion on the dummy continuation with the
702 ;;; assertion for the actual continuation; in all other cases
703 ;;; assertions on the dummy continuation are lost.
705 ;;; We also intersect the derived type of the CALL with the derived
706 ;;; type of all the dummy continuation's uses. This serves mainly to
707 ;;; propagate TRULY-THE through LETs.
708 (defun move-return-uses (fun call next-block)
709 (declare (type clambda fun) (type basic-combination call)
710 (type cblock next-block))
711 (let* ((return (lambda-return fun))
712 (return-block (node-block return)))
713 (unlink-blocks return-block
714 (component-tail (block-component return-block)))
715 (link-blocks return-block next-block)
717 (delete-return return)
718 (let ((result (return-result return))
719 (cont (node-cont call))
720 (call-type (node-derived-type call)))
721 (when (eq (continuation-use cont) call)
722 (assert-continuation-type cont (continuation-asserted-type result)))
723 (unless (eq call-type *wild-type*)
724 (do-uses (use result)
725 (derive-node-type use call-type)))
726 (substitute-continuation-uses cont result)))
729 ;;; Change all CONT for all the calls to FUN to be the start
730 ;;; continuation for the bind node. This allows the blocks to be
731 ;;; joined if the caller count ever goes to one.
732 (defun move-let-call-cont (fun)
733 (declare (type clambda fun))
734 (let ((new-cont (node-prev (lambda-bind fun))))
735 (dolist (ref (leaf-refs fun))
736 (let ((dest (continuation-dest (node-cont ref))))
737 (delete-continuation-use dest)
738 (add-continuation-use dest new-cont))))
741 ;;; We are converting FUN to be a LET when the call is in a non-tail
742 ;;; position. Any previously tail calls in FUN are no longer tail
743 ;;; calls, and must be restored to normal calls which transfer to
744 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
745 ;;; the RETURN-RESULT, because the return might have been deleted (if
746 ;;; all calls were TR.)
748 ;;; The called function might be an assignment in the case where we
749 ;;; are currently converting that function. In steady-state,
750 ;;; assignments never appear in the lambda-calls.
751 (defun unconvert-tail-calls (fun call next-block)
752 (dolist (called (lambda-calls fun))
753 (dolist (ref (leaf-refs called))
754 (let ((this-call (continuation-dest (node-cont ref))))
755 (when (and (node-tail-p this-call)
756 (eq (node-home-lambda this-call) fun))
757 (setf (node-tail-p this-call) nil)
758 (ecase (functional-kind called)
759 ((nil :cleanup :optional)
760 (let ((block (node-block this-call))
761 (cont (node-cont call)))
762 (ensure-block-start cont)
763 (unlink-blocks block (first (block-succ block)))
764 (link-blocks block next-block)
765 (delete-continuation-use this-call)
766 (add-continuation-use this-call cont)))
769 (aver (eq called fun))))))))
772 ;;; Deal with returning from a LET or assignment that we are
773 ;;; converting. FUN is the function we are calling, CALL is a call to
774 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
775 ;;; NULL if call is a tail call.
777 ;;; If the call is not a tail call, then we must do
778 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
779 ;;; its value out of the enclosing non-let function. When call is
780 ;;; non-TR, we must convert it back to an ordinary local call, since
781 ;;; the value must be delivered to the receiver of CALL's value.
783 ;;; We do different things depending on whether the caller and callee
784 ;;; have returns left:
786 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT. Either
787 ;;; the function doesn't return, or all returns are via tail-recursive
789 ;;; -- If CALL is a non-tail call, or if both have returns, then we
790 ;;; delete the callee's return, move its uses to the call's result
791 ;;; continuation, and transfer control to the appropriate return point.
792 ;;; -- If the callee has a return, but the caller doesn't, then we move the
793 ;;; return to the caller.
794 (defun move-return-stuff (fun call next-block)
795 (declare (type clambda fun) (type basic-combination call)
796 (type (or cblock null) next-block))
798 (unconvert-tail-calls fun call next-block))
799 (let* ((return (lambda-return fun))
800 (call-fun (node-home-lambda call))
801 (call-return (lambda-return call-fun)))
803 ((or next-block call-return)
804 (unless (block-delete-p (node-block return))
805 (move-return-uses fun call
806 (or next-block (node-block call-return)))))
808 (aver (node-tail-p call))
809 (setf (lambda-return call-fun) return)
810 (setf (return-lambda return) call-fun))))
811 (move-let-call-cont fun)
814 ;;; Actually do LET conversion. We call subfunctions to do most of the
815 ;;; work. We change the CALL's cont to be the continuation heading the
816 ;;; bind block, and also do REOPTIMIZE-CONTINUATION on the args and
817 ;;; Cont so that let-specific IR1 optimizations get a chance. We blow
818 ;;; away any entry for the function in *FREE-FUNCTIONS* so that nobody
819 ;;; will create new reference to it.
820 (defun let-convert (fun call)
821 (declare (type clambda fun) (type basic-combination call))
822 (let ((next-block (if (node-tail-p call)
824 (insert-let-body fun call))))
825 (move-return-stuff fun call next-block)
826 (merge-lets fun call)))
828 ;;; Reoptimize all of Call's args and its result.
829 (defun reoptimize-call (call)
830 (declare (type basic-combination call))
831 (dolist (arg (basic-combination-args call))
833 (reoptimize-continuation arg)))
834 (reoptimize-continuation (node-cont call))
837 ;;; We also don't convert calls to named functions which appear in the
838 ;;; initial component, delaying this until optimization. This
839 ;;; minimizes the likelyhood that we well let-convert a function which
840 ;;; may have references added due to later local inline expansion
841 (defun ok-initial-convert-p (fun)
842 (not (and (leaf-name fun)
845 (node-block (lambda-bind fun))))
848 ;;; This function is called when there is some reason to believe that
849 ;;; the lambda Fun might be converted into a let. This is done after
850 ;;; local call analysis, and also when a reference is deleted. We only
851 ;;; convert to a let when the function is a normal local function, has
852 ;;; no XEP, and is referenced in exactly one local call. Conversion is
853 ;;; also inhibited if the only reference is in a block about to be
854 ;;; deleted. We return true if we converted.
856 ;;; These rules may seem unnecessarily restrictive, since there are
857 ;;; some cases where we could do the return with a jump that don't
858 ;;; satisfy these requirements. The reason for doing things this way
859 ;;; is that it makes the concept of a LET much more useful at the
860 ;;; level of IR1 semantics. The :ASSIGNMENT function kind provides
861 ;;; another way to optimize calls to single-return/multiple call
864 ;;; We don't attempt to convert calls to functions that have an XEP,
865 ;;; since we might be embarrassed later when we want to convert a
866 ;;; newly discovered local call. Also, see OK-INITIAL-CONVERT-P.
867 (defun maybe-let-convert (fun)
868 (declare (type clambda fun))
869 (let ((refs (leaf-refs fun)))
872 (member (functional-kind fun) '(nil :assignment))
873 (not (functional-entry-function fun)))
874 (let* ((ref-cont (node-cont (first refs)))
875 (dest (continuation-dest ref-cont)))
877 (basic-combination-p dest)
878 (eq (basic-combination-fun dest) ref-cont)
879 (eq (basic-combination-kind dest) :local)
880 (not (block-delete-p (node-block dest)))
881 (cond ((ok-initial-convert-p fun) t)
883 (reoptimize-continuation ref-cont)
885 (unless (eq (functional-kind fun) :assignment)
886 (let-convert fun dest))
887 (reoptimize-call dest)
888 (setf (functional-kind fun)
889 (if (mv-combination-p dest) :mv-let :let))))
892 ;;;; tail local calls and assignments
894 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
895 ;;; they definitely won't generate any cleanup code. Currently we
896 ;;; recognize lexical entry points that are only used locally (if at
898 (defun only-harmless-cleanups (block1 block2)
899 (declare (type cblock block1 block2))
900 (or (eq block1 block2)
901 (let ((cleanup2 (block-start-cleanup block2)))
902 (do ((cleanup (block-end-cleanup block1)
903 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
904 ((eq cleanup cleanup2) t)
905 (case (cleanup-kind cleanup)
907 (unless (null (entry-exits (cleanup-mess-up cleanup)))
909 (t (return nil)))))))
911 ;;; If a potentially TR local call really is TR, then convert it to
912 ;;; jump directly to the called function. We also call
913 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
914 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
915 ;;; the succesor (potentially deleting the RETURN node) unless:
916 ;;; -- The call has already been converted.
917 ;;; -- The call isn't TR (some implicit MV PROG1.)
918 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
919 ;;; so that we can use known return inside the component.)
920 ;;; -- There is a change in the cleanup between the call in the return,
921 ;;; so we might need to introduce cleanup code.
922 (defun maybe-convert-tail-local-call (call)
923 (declare (type combination call))
924 (let ((return (continuation-dest (node-cont call))))
925 (aver (return-p return))
926 (when (and (not (node-tail-p call))
927 (immediately-used-p (return-result return) call)
928 (not (eq (functional-kind (node-home-lambda call))
930 (only-harmless-cleanups (node-block call)
931 (node-block return)))
932 (node-ends-block call)
933 (let ((block (node-block call))
934 (fun (combination-lambda call)))
935 (setf (node-tail-p call) t)
936 (unlink-blocks block (first (block-succ block)))
937 (link-blocks block (node-block (lambda-bind fun)))
938 (values t (maybe-convert-to-assignment fun))))))
940 ;;; This is called when we believe it might make sense to convert Fun
941 ;;; to an assignment. All this function really does is determine when
942 ;;; a function with more than one call can still be combined with the
943 ;;; calling function's environment. We can convert when:
944 ;;; -- The function is a normal, non-entry function, and
945 ;;; -- Except for one call, all calls must be tail recursive calls
946 ;;; in the called function (i.e. are self-recursive tail calls)
947 ;;; -- OK-INITIAL-CONVERT-P is true.
949 ;;; There may be one outside call, and it need not be tail-recursive.
950 ;;; Since all tail local calls have already been converted to direct
951 ;;; transfers, the only control semantics needed are to splice in the
952 ;;; body at the non-tail call. If there is no non-tail call, then we
953 ;;; need only merge the environments. Both cases are handled by
956 ;;; ### It would actually be possible to allow any number of outside
957 ;;; calls as long as they all return to the same place (i.e. have the
958 ;;; same conceptual continuation.) A special case of this would be
959 ;;; when all of the outside calls are tail recursive.
960 (defun maybe-convert-to-assignment (fun)
961 (declare (type clambda fun))
962 (when (and (not (functional-kind fun))
963 (not (functional-entry-function fun)))
966 (when (and (dolist (ref (leaf-refs fun) t)
967 (let ((dest (continuation-dest (node-cont ref))))
969 (block-delete-p (node-block dest)))
971 (let ((home (node-home-lambda ref)))
972 (unless (eq home fun)
973 (when call-fun (return nil))
974 (setq call-fun home))
975 (unless (node-tail-p dest)
976 (when (or non-tail (eq home fun)) (return nil))
977 (setq non-tail dest)))))
978 (ok-initial-convert-p fun))
979 (setf (functional-kind fun) :assignment)
980 (let-convert fun (or non-tail
982 (node-cont (first (leaf-refs fun))))))
983 (when non-tail (reoptimize-call non-tail))