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.
27 ;;; This function propagates information from the variables in the function
28 ;;; Fun to the actual arguments in Call. This is also called by the VALUES IR1
29 ;;; optimizer when it sleazily converts MV-BINDs to LETs.
31 ;;; We flush all arguments to Call that correspond to unreferenced variables
32 ;;; in Fun. We leave NILs in the Combination-Args so that the remaining args
33 ;;; still match up with their vars.
35 ;;; We also apply the declared variable type assertion to the argument
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)))
42 (let ((arg (car args))
44 (cond ((leaf-refs var)
45 (assert-continuation-type arg (leaf-type var)))
48 (setf (car args) nil)))))
52 ;;; This function handles merging the tail sets if Call is potentially
53 ;;; tail-recursive, and is a call to a function with a different TAIL-SET than
54 ;;; Call's Fun. This must be called whenever we alter IR1 so as to place a
55 ;;; local call in what might be a TR context. Note that any call which returns
56 ;;; its value to a RETURN is considered potentially TR, since any implicit
57 ;;; MV-PROG1 might be optimized away.
59 ;;; We destructively modify the set for the calling function to represent both,
60 ;;; and then change all the functions in callee's set to reference the first.
61 ;;; If we do merge, we reoptimize the RETURN-RESULT continuation to cause
62 ;;; IR1-OPTIMIZE-RETURN to recompute the tail set type.
63 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
64 (declare (type basic-combination call) (type clambda new-fun))
65 (let ((return (continuation-dest (node-cont call))))
66 (when (return-p return)
67 (let ((call-set (lambda-tail-set (node-home-lambda call)))
68 (fun-set (lambda-tail-set new-fun)))
69 (unless (eq call-set fun-set)
70 (let ((funs (tail-set-functions fun-set)))
72 (setf (lambda-tail-set fun) call-set))
73 (setf (tail-set-functions call-set)
74 (nconc (tail-set-functions call-set) funs)))
75 (reoptimize-continuation (return-result return))
78 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
79 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
80 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
81 ;;; the function in the REF node with the new function.
83 ;;; We change the REF last, since changing the reference can trigger
84 ;;; LET conversion of the new function, but will only do so if the
85 ;;; call is local. Note that the replacement may trigger LET
86 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
87 ;;; with NEW-FUN before the substitution, since after the substitution
88 ;;; (and LET conversion), the call may no longer be recognizable as
90 (defun convert-call (ref call fun)
91 (declare (type ref ref) (type combination call) (type clambda fun))
92 (propagate-to-args call fun)
93 (setf (basic-combination-kind call) :local)
94 (pushnew fun (lambda-calls (node-home-lambda call)))
95 (merge-tail-sets call fun)
96 (change-ref-leaf ref fun)
99 ;;;; external entry point creation
101 ;;; Return a Lambda form that can be used as the definition of the XEP
104 ;;; If FUN is a lambda, then we check the number of arguments
105 ;;; (conditional on policy) and call FUN with all the arguments.
107 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
108 ;;; of supplied arguments by doing do an = test for each entry-point,
109 ;;; calling the entry with the appropriate prefix of the passed
112 ;;; If there is a more arg, then there are a couple of optimizations
113 ;;; that we make (more for space than anything else):
114 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
115 ;;; no argument count error is possible.
116 ;;; -- We can omit the = clause for the last entry-point, allowing the
117 ;;; case of 0 more args to fall through to the more entry.
119 ;;; We don't bother to policy conditionalize wrong arg errors in
120 ;;; optional dispatches, since the additional overhead is negligible
121 ;;; compared to the cost of everything else going on.
123 ;;; Note that if policy indicates it, argument type declarations in
124 ;;; Fun will be verified. Since nothing is known about the type of the
125 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
126 ;;; are passed to the actual function.
127 (defun make-xep-lambda (fun)
128 (declare (type functional fun))
131 (let ((nargs (length (lambda-vars fun)))
132 (n-supplied (gensym))
133 (temps (make-gensym-list (length (lambda-vars fun)))))
134 `(lambda (,n-supplied ,@temps)
135 (declare (type index ,n-supplied))
136 ,(if (policy nil (zerop safety))
137 `(declare (ignore ,n-supplied))
138 `(%verify-argument-count ,n-supplied ,nargs))
139 (%funcall ,fun ,@temps))))
141 (let* ((min (optional-dispatch-min-args fun))
142 (max (optional-dispatch-max-args fun))
143 (more (optional-dispatch-more-entry fun))
144 (n-supplied (gensym))
145 (temps (make-gensym-list max)))
147 (do ((eps (optional-dispatch-entry-points fun) (rest eps))
150 (entries `((= ,n-supplied ,n)
151 (%funcall ,(first eps) ,@(subseq temps 0 n)))))
152 `(lambda (,n-supplied ,@temps)
153 ;; FIXME: Make sure that INDEX type distinguishes between
154 ;; target and host. (Probably just make the SB!XC:DEFTYPE
155 ;; different from CL:DEFTYPE.)
156 (declare (type index ,n-supplied))
158 ,@(if more (butlast (entries)) (entries))
160 `((,(if (zerop min) 't `(>= ,n-supplied ,max))
161 ,(let ((n-context (gensym))
163 `(multiple-value-bind (,n-context ,n-count)
164 (%more-arg-context ,n-supplied ,max)
165 (%funcall ,more ,@temps ,n-context ,n-count))))))
167 (%argument-count-error ,n-supplied)))))))))
169 ;;; Make an external entry point (XEP) for Fun and return it. We
170 ;;; convert the result of Make-XEP-Lambda in the correct environment,
171 ;;; then associate this lambda with Fun as its XEP. After the
172 ;;; conversion, we iterate over the function's associated lambdas,
173 ;;; redoing local call analysis so that the XEP calls will get
174 ;;; converted. We also bind *LEXENV* to change the compilation policy
175 ;;; over to the interface policy.
177 ;;; We set Reanalyze and Reoptimize in the component, just in case we
178 ;;; discover an XEP after the initial local call analyze pass.
179 (defun make-external-entry-point (fun)
180 (declare (type functional fun))
181 (assert (not (functional-entry-function fun)))
182 (with-ir1-environment (lambda-bind (main-entry fun))
183 (let* ((*lexenv* (make-lexenv :cookie (make-interface-cookie *lexenv*)))
184 (res (ir1-convert-lambda (make-xep-lambda fun))))
185 (setf (functional-kind res) :external)
186 (setf (leaf-ever-used res) t)
187 (setf (functional-entry-function res) fun)
188 (setf (functional-entry-function fun) res)
189 (setf (component-reanalyze *current-component*) t)
190 (setf (component-reoptimize *current-component*) t)
192 (clambda (local-call-analyze-1 fun))
194 (dolist (ep (optional-dispatch-entry-points fun))
195 (local-call-analyze-1 ep))
196 (when (optional-dispatch-more-entry fun)
197 (local-call-analyze-1 (optional-dispatch-more-entry fun)))))
200 ;;; Notice a Ref that is not in a local-call context. If the Ref is
201 ;;; already to an XEP, then do nothing, otherwise change it to the
202 ;;; XEP, making an XEP if necessary.
204 ;;; If Ref is to a special :Cleanup or :Escape function, then we treat
205 ;;; it as though it was not an XEP reference (i.e. leave it alone.)
206 (defun reference-entry-point (ref)
207 (declare (type ref ref))
208 (let ((fun (ref-leaf ref)))
209 (unless (or (external-entry-point-p fun)
210 (member (functional-kind fun) '(:escape :cleanup)))
211 (change-ref-leaf ref (or (functional-entry-function fun)
212 (make-external-entry-point fun))))))
214 ;;; Attempt to convert all references to Fun to local calls. The
215 ;;; reference must be the function for a call, and the function
216 ;;; continuation must be used only once, since otherwise we cannot be
217 ;;; sure what function is to be called. The call continuation would be
218 ;;; multiply used if there is hairy stuff such as conditionals in the
219 ;;; expression that computes the function.
221 ;;; If we cannot convert a reference, then we mark the referenced
222 ;;; function as an entry-point, creating a new XEP if necessary. We
223 ;;; don't try to convert calls that are in error (:ERROR kind.)
225 ;;; This is broken off from Local-Call-Analyze so that people can
226 ;;; force analysis of newly introduced calls. Note that we don't do
227 ;;; LET conversion here.
228 (defun local-call-analyze-1 (fun)
229 (declare (type functional fun))
230 (let ((refs (leaf-refs fun))
233 (let* ((cont (node-cont ref))
234 (dest (continuation-dest cont)))
235 (cond ((and (basic-combination-p dest)
236 (eq (basic-combination-fun dest) cont)
237 (eq (continuation-use cont) ref))
239 (convert-call-if-possible ref dest)
241 (unless (eq (basic-combination-kind dest) :local)
242 (reference-entry-point ref)))
244 (reference-entry-point ref))))
245 (setq first-time nil)))
249 ;;; We examine all New-Functions in component, attempting to convert
250 ;;; calls into local calls when it is legal. We also attempt to
251 ;;; convert each lambda to a LET. LET conversion is also triggered by
252 ;;; deletion of a function reference, but functions that start out
253 ;;; eligible for conversion must be noticed sometime.
255 ;;; Note that there is a lot of action going on behind the scenes
256 ;;; here, triggered by reference deletion. In particular, the
257 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and let
258 ;;; converted lambdas, so it is important that the lambda is added to
259 ;;; the COMPONENT-LAMBDAS when it is. Also, the
260 ;;; COMPONENT-NEW-FUNCTIONS may contain all sorts of drivel, since it
261 ;;; is not updated when we delete functions, etc. Only
262 ;;; COMPONENT-LAMBDAS is updated.
264 ;;; COMPONENT-REANALYZE-FUNCTIONS is treated similarly to
265 ;;; NEW-FUNCTIONS, but we don't add lambdas to the LAMBDAS.
266 (defun local-call-analyze (component)
267 (declare (type component component))
269 (let* ((new (pop (component-new-functions component)))
270 (fun (or new (pop (component-reanalyze-functions component)))))
271 (unless fun (return))
272 (let ((kind (functional-kind fun)))
273 (cond ((member kind '(:deleted :let :mv-let :assignment)))
274 ((and (null (leaf-refs fun)) (eq kind nil)
275 (not (functional-entry-function fun)))
276 (delete-functional fun))
278 (when (and new (lambda-p fun))
279 (push fun (component-lambdas component)))
280 (local-call-analyze-1 fun)
282 (maybe-let-convert fun)))))))
286 ;;; If policy is auspicious, Call is not in an XEP, and we don't seem
287 ;;; to be in an infinite recursive loop, then change the reference to
288 ;;; reference a fresh copy. We return whichever function we decide to
290 (defun maybe-expand-local-inline (fun ref call)
291 (if (and (policy call (>= speed space) (>= speed cspeed))
292 (not (eq (functional-kind (node-home-lambda call)) :external))
293 (not *converting-for-interpreter*)
294 (inline-expansion-ok call))
295 (with-ir1-environment call
296 (let* ((*lexenv* (functional-lexenv fun))
298 (res (catch 'local-call-lossage
300 (ir1-convert-lambda (functional-inline-expansion fun))
303 (change-ref-leaf ref res)
306 (let ((*compiler-error-context* call))
307 (compiler-note "couldn't inline expand because expansion ~
308 calls this let-converted local function:~
314 ;;; Dispatch to the appropriate function to attempt to convert a call. Ref
315 ;;; most be a reference to a FUNCTIONAL. This is called in IR1 optimize as
316 ;;; well as in local call analysis. If the call is is already :Local, we do
317 ;;; nothing. If the call is already scheduled for deletion, also do nothing
318 ;;; (in addition to saving time, this also avoids some problems with optimizing
319 ;;; collections of functions that are partially deleted.)
321 ;;; This is called both before and after FIND-INITIAL-DFO runs. When called
322 ;;; on a :INITIAL component, we don't care whether the caller and callee are in
323 ;;; the same component. Afterward, we must stick with whatever component
324 ;;; division we have chosen.
326 ;;; Before attempting to convert a call, we see whether the function is
327 ;;; supposed to be inline expanded. Call conversion proceeds as before
328 ;;; after any expansion.
330 ;;; We bind *Compiler-Error-Context* to the node for the call so that
331 ;;; warnings will get the right context.
332 (defun convert-call-if-possible (ref call)
333 (declare (type ref ref) (type basic-combination call))
334 (let* ((block (node-block call))
335 (component (block-component block))
336 (original-fun (ref-leaf ref)))
337 (assert (functional-p original-fun))
338 (unless (or (member (basic-combination-kind call) '(:local :error))
339 (block-delete-p block)
340 (eq (functional-kind (block-home-lambda block)) :deleted)
341 (member (functional-kind original-fun)
342 '(:top-level-xep :deleted))
343 (not (or (eq (component-kind component) :initial)
346 (lambda-bind (main-entry original-fun))))
348 (let ((fun (if (external-entry-point-p original-fun)
349 (functional-entry-function original-fun)
351 (*compiler-error-context* call))
353 (when (and (eq (functional-inlinep fun) :inline)
354 (rest (leaf-refs original-fun)))
355 (setq fun (maybe-expand-local-inline fun ref call)))
357 (assert (member (functional-kind fun)
358 '(nil :escape :cleanup :optional)))
359 (cond ((mv-combination-p call)
360 (convert-mv-call ref call fun))
362 (convert-lambda-call ref call fun))
364 (convert-hairy-call ref call fun))))))
368 ;;; Attempt to convert a multiple-value call. The only interesting
369 ;;; case is a call to a function that Looks-Like-An-MV-Bind, has
370 ;;; exactly one reference and no XEP, and is called with one values
373 ;;; We change the call to be to the last optional entry point and
374 ;;; change the call to be local. Due to our preconditions, the call
375 ;;; should eventually be converted to a let, but we can't do that now,
376 ;;; since there may be stray references to the e-p lambda due to
377 ;;; optional defaulting code.
379 ;;; We also use variable types for the called function to construct an
380 ;;; assertion for the values continuation.
382 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
383 (defun convert-mv-call (ref call fun)
384 (declare (type ref ref) (type mv-combination call) (type functional fun))
385 (when (and (looks-like-an-mv-bind fun)
386 (not (functional-entry-function fun))
387 (= (length (leaf-refs fun)) 1)
388 (= (length (basic-combination-args call)) 1))
389 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
390 (setf (basic-combination-kind call) :local)
391 (pushnew ep (lambda-calls (node-home-lambda call)))
392 (merge-tail-sets call ep)
393 (change-ref-leaf ref ep)
395 (assert-continuation-type
396 (first (basic-combination-args call))
397 (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
398 :rest *universal-type*))))
401 ;;; Attempt to convert a call to a lambda. If the number of args is
402 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
403 ;;; from future consideration. If the argcount is O.K. then we just
405 (defun convert-lambda-call (ref call fun)
406 (declare (type ref ref) (type combination call) (type clambda fun))
407 (let ((nargs (length (lambda-vars fun)))
408 (call-args (length (combination-args call))))
409 (cond ((= call-args nargs)
410 (convert-call ref call fun))
412 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
413 ;; Compiler" that calling a function with "the wrong number of
414 ;; arguments" be only a STYLE-ERROR. I think, though, that this
415 ;; should only apply when the number of arguments is inferred
416 ;; from a previous definition. If the number of arguments
417 ;; is DECLAIMed, surely calling with the wrong number is a
418 ;; real WARNING. As long as SBCL continues to use CMU CL's
419 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
420 ;; but as long as we continue to use that policy, that's the
421 ;; not our biggest problem.:-| When we fix that policy, this
422 ;; should come back into compliance. (So fix that policy!)
424 "function called with ~R argument~:P, but wants exactly ~R"
426 (setf (basic-combination-kind call) :error)))))
428 ;;;; optional, more and keyword calls
430 ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
431 ;;; only fixed args are supplied, then convert a call to the correct entry
432 ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
433 ;;; don't convert calls to functions that have a more (or rest) arg.
434 (defun convert-hairy-call (ref call fun)
435 (declare (type ref ref) (type combination call)
436 (type optional-dispatch fun))
437 (let ((min-args (optional-dispatch-min-args fun))
438 (max-args (optional-dispatch-max-args fun))
439 (call-args (length (combination-args call))))
440 (cond ((< call-args min-args)
441 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
442 ;; Compiler" that calling a function with "the wrong number of
443 ;; arguments" be only a STYLE-ERROR. I think, though, that this
444 ;; should only apply when the number of arguments is inferred
445 ;; from a previous definition. If the number of arguments
446 ;; is DECLAIMed, surely calling with the wrong number is a
447 ;; real WARNING. As long as SBCL continues to use CMU CL's
448 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
449 ;; but as long as we continue to use that policy, that's the
450 ;; not our biggest problem.:-| When we fix that policy, this
451 ;; should come back into compliance. (So fix that policy!)
453 "function called with ~R argument~:P, but wants at least ~R"
455 (setf (basic-combination-kind call) :error))
456 ((<= call-args max-args)
457 (convert-call ref call
458 (elt (optional-dispatch-entry-points fun)
459 (- call-args min-args))))
460 ((optional-dispatch-more-entry fun)
461 (convert-more-call ref call fun))
463 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
464 ;; Compiler" that calling a function with "the wrong number of
465 ;; arguments" be only a STYLE-ERROR. I think, though, that this
466 ;; should only apply when the number of arguments is inferred
467 ;; from a previous definition. If the number of arguments
468 ;; is DECLAIMed, surely calling with the wrong number is a
469 ;; real WARNING. As long as SBCL continues to use CMU CL's
470 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
471 ;; but as long as we continue to use that policy, that's the
472 ;; not our biggest problem.:-| When we fix that policy, this
473 ;; should come back into compliance. (So fix that policy!)
475 "function called with ~R argument~:P, but wants at most ~R"
477 (setf (basic-combination-kind call) :error))))
480 ;;; This function is used to convert a call to an entry point when complex
481 ;;; transformations need to be done on the original arguments. Entry is the
482 ;;; entry point function that we are calling. Vars is a list of variable names
483 ;;; which are bound to the original call arguments. Ignores is the subset of
484 ;;; Vars which are ignored. Args is the list of arguments to the entry point
487 ;;; In order to avoid gruesome graph grovelling, we introduce a new function
488 ;;; that rearranges the arguments and calls the entry point. We analyze the
489 ;;; new function and the entry point immediately so that everything gets
490 ;;; converted during the single pass.
491 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
492 (declare (list vars ignores args) (type ref ref) (type combination call)
493 (type clambda entry))
495 (with-ir1-environment call
498 (declare (ignorable . ,ignores))
499 (%funcall ,entry . ,args))))))
500 (convert-call ref call new-fun)
501 (dolist (ref (leaf-refs entry))
502 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
504 ;;; Use Convert-Hairy-Fun-Entry to convert a more-arg call to a known
505 ;;; function into a local call to the Main-Entry.
507 ;;; First we verify that all keywords are constant and legal. If there
508 ;;; aren't, then we warn the user and don't attempt to convert the call.
510 ;;; We massage the supplied keyword arguments into the order expected by the
511 ;;; main entry. This is done by binding all the arguments to the keyword call
512 ;;; to variables in the introduced lambda, then passing these values variables
513 ;;; in the correct order when calling the main entry. Unused arguments
514 ;;; (such as the keywords themselves) are discarded simply by not passing them
517 ;;; If there is a rest arg, then we bundle up the args and pass them to LIST.
518 (defun convert-more-call (ref call fun)
519 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
520 (let* ((max (optional-dispatch-max-args fun))
521 (arglist (optional-dispatch-arglist fun))
522 (args (combination-args call))
523 (more (nthcdr max args))
524 (flame (policy call (or (> speed brevity) (> space brevity))))
526 (temps (make-gensym-list max))
527 (more-temps (make-gensym-list (length more))))
532 (dolist (var arglist)
533 (let ((info (lambda-var-arg-info var)))
535 (ecase (arg-info-kind info)
539 ((:more-context :more-count)
540 (compiler-warning "can't local-call functions with &MORE args")
541 (setf (basic-combination-kind call) :error)
542 (return-from convert-more-call))))))
544 (when (optional-dispatch-keyp fun)
545 (when (oddp (length more))
546 (compiler-warning "function called with odd number of ~
547 arguments in keyword portion")
549 (setf (basic-combination-kind call) :error)
550 (return-from convert-more-call))
552 (do ((key more (cddr key))
553 (temp more-temps (cddr temp)))
555 (let ((cont (first key)))
556 (unless (constant-continuation-p cont)
558 (compiler-note "non-constant keyword in keyword call"))
559 (setf (basic-combination-kind call) :error)
560 (return-from convert-more-call))
562 (let ((name (continuation-value cont))
565 (dolist (var (key-vars)
569 (let ((info (lambda-var-arg-info var)))
570 (when (eq (arg-info-keyword info) name)
572 (supplied (cons var val))
575 (when (and loser (not (optional-dispatch-allowp fun)))
576 (compiler-warning "function called with unknown argument keyword ~S"
578 (setf (basic-combination-kind call) :error)
579 (return-from convert-more-call)))
581 (collect ((call-args))
582 (do ((var arglist (cdr var))
583 (temp temps (cdr temp)))
585 (let ((info (lambda-var-arg-info (car var))))
587 (ecase (arg-info-kind info)
589 (call-args (car temp))
590 (when (arg-info-supplied-p info)
593 (call-args `(list ,@more-temps))
597 (call-args (car temp)))))
599 (dolist (var (key-vars))
600 (let ((info (lambda-var-arg-info var))
601 (temp (cdr (assoc var (supplied)))))
604 (call-args (arg-info-default info)))
605 (when (arg-info-supplied-p info)
606 (call-args (not (null temp))))))
608 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
609 (append temps more-temps)
610 (ignores) (call-args)))))
616 ;;;; Converting to a LET has differing significance to various parts of the
618 ;;;; -- The body of a LET is spliced in immediately after the corresponding
619 ;;;; combination node, making the control transfer explicit and allowing
620 ;;;; LETs to be mashed together into a single block. The value of the LET is
621 ;;;; delivered directly to the original continuation for the call,
622 ;;;; eliminating the need to propagate information from the dummy result
624 ;;;; -- As far as IR1 optimization is concerned, it is interesting in that
625 ;;;; there is only one expression that the variable can be bound to, and
626 ;;;; this is easily substitited for.
627 ;;;; -- LETs are interesting to environment analysis and to the back end
628 ;;;; because in most ways a LET can be considered to be "the same function"
629 ;;;; as its home function.
630 ;;;; -- LET conversion has dynamic scope implications, since control transfers
631 ;;;; within the same environment are local. In a local control transfer,
632 ;;;; cleanup code must be emitted to remove dynamic bindings that are no
633 ;;;; longer in effect.
635 ;;; Set up the control transfer to the called lambda. We split the call
636 ;;; block immediately after the call, and link the head of FUN to the call
637 ;;; block. The successor block after splitting (where we return to) is
640 ;;; If the lambda is is a different component than the call, then we call
641 ;;; JOIN-COMPONENTS. This only happens in block compilation before
642 ;;; FIND-INITIAL-DFO.
643 (defun insert-let-body (fun call)
644 (declare (type clambda fun) (type basic-combination call))
645 (let* ((call-block (node-block call))
646 (bind-block (node-block (lambda-bind fun)))
647 (component (block-component call-block)))
648 (let ((fun-component (block-component bind-block)))
649 (unless (eq fun-component component)
650 (assert (eq (component-kind component) :initial))
651 (join-components component fun-component)))
653 (let ((*current-component* component))
654 (node-ends-block call))
655 ;; FIXME: Use PROPER-LIST-OF-LENGTH-P here, and look for other
656 ;; uses of '=.*length' which could also be converted to use
657 ;; PROPER-LIST-OF-LENGTH-P.
658 (assert (= (length (block-succ call-block)) 1))
659 (let ((next-block (first (block-succ call-block))))
660 (unlink-blocks call-block next-block)
661 (link-blocks call-block bind-block)
664 ;;; Handle the environment semantics of LET conversion. We add the
665 ;;; lambda and its LETs to lets for the CALL's home function. We merge
666 ;;; the calls for FUN with the calls for the home function, removing
667 ;;; FUN in the process. We also merge the Entries.
669 ;;; We also unlink the function head from the component head and set
670 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
672 (defun merge-lets (fun call)
673 (declare (type clambda fun) (type basic-combination call))
674 (let ((component (block-component (node-block call))))
675 (unlink-blocks (component-head component) (node-block (lambda-bind fun)))
676 (setf (component-lambdas component)
677 (delete fun (component-lambdas component)))
678 (setf (component-reanalyze component) t))
679 (setf (lambda-call-lexenv fun) (node-lexenv call))
680 (let ((tails (lambda-tail-set fun)))
681 (setf (tail-set-functions tails)
682 (delete fun (tail-set-functions tails))))
683 (setf (lambda-tail-set fun) nil)
684 (let* ((home (node-home-lambda call))
685 (home-env (lambda-environment home)))
686 (push fun (lambda-lets home))
687 (setf (lambda-home fun) home)
688 (setf (lambda-environment fun) home-env)
690 (let ((lets (lambda-lets fun)))
692 (setf (lambda-home let) home)
693 (setf (lambda-environment let) home-env))
695 (setf (lambda-lets home) (nconc lets (lambda-lets home)))
696 (setf (lambda-lets fun) ()))
698 (setf (lambda-calls home)
699 (delete fun (nunion (lambda-calls fun) (lambda-calls home))))
700 (setf (lambda-calls fun) ())
702 (setf (lambda-entries home)
703 (nconc (lambda-entries fun) (lambda-entries home)))
704 (setf (lambda-entries fun) ()))
707 ;;; Handle the value semantics of LET conversion. Delete FUN's return
708 ;;; node, and change the control flow to transfer to NEXT-BLOCK
709 ;;; instead. Move all the uses of the result continuation to CALL's
712 ;;; If the actual continuation is only used by the LET call, then we
713 ;;; intersect the type assertion on the dummy continuation with the
714 ;;; assertion for the actual continuation; in all other cases
715 ;;; assertions on the dummy continuation are lost.
717 ;;; We also intersect the derived type of the CALL with the derived
718 ;;; type of all the dummy continuation's uses. This serves mainly to
719 ;;; propagate TRULY-THE through LETs.
720 (defun move-return-uses (fun call next-block)
721 (declare (type clambda fun) (type basic-combination call)
722 (type cblock next-block))
723 (let* ((return (lambda-return fun))
724 (return-block (node-block return)))
725 (unlink-blocks return-block
726 (component-tail (block-component return-block)))
727 (link-blocks return-block next-block)
729 (delete-return return)
730 (let ((result (return-result return))
731 (cont (node-cont call))
732 (call-type (node-derived-type call)))
733 (when (eq (continuation-use cont) call)
734 (assert-continuation-type cont (continuation-asserted-type result)))
735 (unless (eq call-type *wild-type*)
736 (do-uses (use result)
737 (derive-node-type use call-type)))
738 (substitute-continuation-uses cont result)))
741 ;;; Change all CONT for all the calls to FUN to be the start
742 ;;; continuation for the bind node. This allows the blocks to be
743 ;;; joined if the caller count ever goes to one.
744 (defun move-let-call-cont (fun)
745 (declare (type clambda fun))
746 (let ((new-cont (node-prev (lambda-bind fun))))
747 (dolist (ref (leaf-refs fun))
748 (let ((dest (continuation-dest (node-cont ref))))
749 (delete-continuation-use dest)
750 (add-continuation-use dest new-cont))))
753 ;;; We are converting FUN to be a LET when the call is in a non-tail
754 ;;; position. Any previously tail calls in FUN are no longer tail
755 ;;; calls, and must be restored to normal calls which transfer to
756 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
757 ;;; the RETURN-RESULT, because the return might have been deleted (if
758 ;;; all calls were TR.)
760 ;;; The called function might be an assignment in the case where we
761 ;;; are currently converting that function. In steady-state,
762 ;;; assignments never appear in the lambda-calls.
763 (defun unconvert-tail-calls (fun call next-block)
764 (dolist (called (lambda-calls fun))
765 (dolist (ref (leaf-refs called))
766 (let ((this-call (continuation-dest (node-cont ref))))
767 (when (and (node-tail-p this-call)
768 (eq (node-home-lambda this-call) fun))
769 (setf (node-tail-p this-call) nil)
770 (ecase (functional-kind called)
771 ((nil :cleanup :optional)
772 (let ((block (node-block this-call))
773 (cont (node-cont call)))
774 (ensure-block-start cont)
775 (unlink-blocks block (first (block-succ block)))
776 (link-blocks block next-block)
777 (delete-continuation-use this-call)
778 (add-continuation-use this-call cont)))
781 (assert (eq called fun))))))))
784 ;;; Deal with returning from a LET or assignment that we are
785 ;;; converting. FUN is the function we are calling, CALL is a call to
786 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
787 ;;; NULL if call is a tail call.
789 ;;; If the call is not a tail call, then we must do
790 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
791 ;;; its value out of the enclosing non-let function. When call is
792 ;;; non-TR, we must convert it back to an ordinary local call, since
793 ;;; the value must be delivered to the receiver of CALL's value.
795 ;;; We do different things depending on whether the caller and callee
796 ;;; have returns left:
798 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT. Either
799 ;;; the function doesn't return, or all returns are via tail-recursive
801 ;;; -- If CALL is a non-tail call, or if both have returns, then we
802 ;;; delete the callee's return, move its uses to the call's result
803 ;;; continuation, and transfer control to the appropriate return point.
804 ;;; -- If the callee has a return, but the caller doesn't, then we move the
805 ;;; return to the caller.
806 (defun move-return-stuff (fun call next-block)
807 (declare (type clambda fun) (type basic-combination call)
808 (type (or cblock null) next-block))
810 (unconvert-tail-calls fun call next-block))
811 (let* ((return (lambda-return fun))
812 (call-fun (node-home-lambda call))
813 (call-return (lambda-return call-fun)))
815 ((or next-block call-return)
816 (unless (block-delete-p (node-block return))
817 (move-return-uses fun call
818 (or next-block (node-block call-return)))))
820 (assert (node-tail-p call))
821 (setf (lambda-return call-fun) return)
822 (setf (return-lambda return) call-fun))))
823 (move-let-call-cont fun)
826 ;;; Actually do LET conversion. We call subfunctions to do most of the
827 ;;; work. We change the CALL's cont to be the continuation heading the
828 ;;; bind block, and also do REOPTIMIZE-CONTINUATION on the args and
829 ;;; Cont so that let-specific IR1 optimizations get a chance. We blow
830 ;;; away any entry for the function in *FREE-FUNCTIONS* so that nobody
831 ;;; will create new reference to it.
832 (defun let-convert (fun call)
833 (declare (type clambda fun) (type basic-combination call))
834 (let ((next-block (if (node-tail-p call)
836 (insert-let-body fun call))))
837 (move-return-stuff fun call next-block)
838 (merge-lets fun call)))
840 ;;; Reoptimize all of Call's args and its result.
841 (defun reoptimize-call (call)
842 (declare (type basic-combination call))
843 (dolist (arg (basic-combination-args call))
845 (reoptimize-continuation arg)))
846 (reoptimize-continuation (node-cont call))
849 ;;; We also don't convert calls to named functions which appear in the
850 ;;; initial component, delaying this until optimization. This
851 ;;; minimizes the likelyhood that we well let-convert a function which
852 ;;; may have references added due to later local inline expansion
853 (defun ok-initial-convert-p (fun)
854 (not (and (leaf-name fun)
857 (node-block (lambda-bind fun))))
860 ;;; This function is called when there is some reason to believe that
861 ;;; the lambda Fun might be converted into a let. This is done after
862 ;;; local call analysis, and also when a reference is deleted. We only
863 ;;; convert to a let when the function is a normal local function, has
864 ;;; no XEP, and is referenced in exactly one local call. Conversion is
865 ;;; also inhibited if the only reference is in a block about to be
866 ;;; deleted. We return true if we converted.
868 ;;; These rules may seem unnecessarily restrictive, since there are
869 ;;; some cases where we could do the return with a jump that don't
870 ;;; satisfy these requirements. The reason for doing things this way
871 ;;; is that it makes the concept of a LET much more useful at the
872 ;;; level of IR1 semantics. The :ASSIGNMENT function kind provides
873 ;;; another way to optimize calls to single-return/multiple call
876 ;;; We don't attempt to convert calls to functions that have an XEP,
877 ;;; since we might be embarrassed later when we want to convert a
878 ;;; newly discovered local call. Also, see OK-INITIAL-CONVERT-P.
879 (defun maybe-let-convert (fun)
880 (declare (type clambda fun))
881 (let ((refs (leaf-refs fun)))
884 (member (functional-kind fun) '(nil :assignment))
885 (not (functional-entry-function fun)))
886 (let* ((ref-cont (node-cont (first refs)))
887 (dest (continuation-dest ref-cont)))
888 (when (and (basic-combination-p dest)
889 (eq (basic-combination-fun dest) ref-cont)
890 (eq (basic-combination-kind dest) :local)
891 (not (block-delete-p (node-block dest)))
892 (cond ((ok-initial-convert-p fun) t)
894 (reoptimize-continuation ref-cont)
896 (unless (eq (functional-kind fun) :assignment)
897 (let-convert fun dest))
898 (reoptimize-call dest)
899 (setf (functional-kind fun)
900 (if (mv-combination-p dest) :mv-let :let))))
903 ;;;; tail local calls and assignments
905 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
906 ;;; they definitely won't generate any cleanup code. Currently we
907 ;;; recognize lexical entry points that are only used locally (if at
909 (defun only-harmless-cleanups (block1 block2)
910 (declare (type cblock block1 block2))
911 (or (eq block1 block2)
912 (let ((cleanup2 (block-start-cleanup block2)))
913 (do ((cleanup (block-end-cleanup block1)
914 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
915 ((eq cleanup cleanup2) t)
916 (case (cleanup-kind cleanup)
918 (unless (null (entry-exits (cleanup-mess-up cleanup)))
920 (t (return nil)))))))
922 ;;; If a potentially TR local call really is TR, then convert it to
923 ;;; jump directly to the called function. We also call
924 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
925 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
926 ;;; the succesor (potentially deleting the RETURN node) unless:
927 ;;; -- The call has already been converted.
928 ;;; -- The call isn't TR (some implicit MV PROG1.)
929 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
930 ;;; so that we can use known return inside the component.)
931 ;;; -- There is a change in the cleanup between the call in the return,
932 ;;; so we might need to introduce cleanup code.
933 (defun maybe-convert-tail-local-call (call)
934 (declare (type combination call))
935 (let ((return (continuation-dest (node-cont call))))
936 (assert (return-p return))
937 (when (and (not (node-tail-p call))
938 (immediately-used-p (return-result return) call)
939 (not (eq (functional-kind (node-home-lambda call))
941 (only-harmless-cleanups (node-block call)
942 (node-block return)))
943 (node-ends-block call)
944 (let ((block (node-block call))
945 (fun (combination-lambda call)))
946 (setf (node-tail-p call) t)
947 (unlink-blocks block (first (block-succ block)))
948 (link-blocks block (node-block (lambda-bind fun)))
949 (values t (maybe-convert-to-assignment fun))))))
951 ;;; This is called when we believe it might make sense to convert Fun
952 ;;; to an assignment. All this function really does is determine when
953 ;;; a function with more than one call can still be combined with the
954 ;;; calling function's environment. We can convert when:
955 ;;; -- The function is a normal, non-entry function, and
956 ;;; -- Except for one call, all calls must be tail recursive calls
957 ;;; in the called function (i.e. are self-recursive tail calls)
958 ;;; -- OK-INITIAL-CONVERT-P is true.
960 ;;; There may be one outside call, and it need not be tail-recursive.
961 ;;; Since all tail local calls have already been converted to direct
962 ;;; transfers, the only control semantics needed are to splice in the
963 ;;; body at the non-tail call. If there is no non-tail call, then we
964 ;;; need only merge the environments. Both cases are handled by
967 ;;; ### It would actually be possible to allow any number of outside
968 ;;; calls as long as they all return to the same place (i.e. have the
969 ;;; same conceptual continuation.) A special case of this would be
970 ;;; when all of the outside calls are tail recursive.
971 (defun maybe-convert-to-assignment (fun)
972 (declare (type clambda fun))
973 (when (and (not (functional-kind fun))
974 (not (functional-entry-function fun)))
977 (when (and (dolist (ref (leaf-refs fun) t)
978 (let ((dest (continuation-dest (node-cont ref))))
979 (when (block-delete-p (node-block dest)) (return nil))
980 (let ((home (node-home-lambda ref)))
981 (unless (eq home fun)
982 (when call-fun (return nil))
983 (setq call-fun home))
984 (unless (node-tail-p dest)
985 (when (or non-tail (eq home fun)) (return nil))
986 (setq non-tail dest)))))
987 (ok-initial-convert-p fun))
988 (setf (functional-kind fun) :assignment)
989 (let-convert fun (or non-tail
991 (node-cont (first (leaf-refs fun))))))
992 (when non-tail (reoptimize-call non-tail))