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 nil (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
171 ;;; converted. We also bind *LEXENV* to change the compilation policy
172 ;;; over to the interface policy.
174 ;;; We set Reanalyze and Reoptimize in the component, just in case we
175 ;;; discover an XEP after the initial local call analyze pass.
176 (defun make-external-entry-point (fun)
177 (declare (type functional fun))
178 (assert (not (functional-entry-function fun)))
179 (with-ir1-environment (lambda-bind (main-entry fun))
180 (let* ((*lexenv* (make-lexenv :cookie (make-interface-cookie *lexenv*)))
181 (res (ir1-convert-lambda (make-xep-lambda fun))))
182 (setf (functional-kind res) :external)
183 (setf (leaf-ever-used res) t)
184 (setf (functional-entry-function res) fun)
185 (setf (functional-entry-function fun) res)
186 (setf (component-reanalyze *current-component*) t)
187 (setf (component-reoptimize *current-component*) t)
189 (clambda (local-call-analyze-1 fun))
191 (dolist (ep (optional-dispatch-entry-points fun))
192 (local-call-analyze-1 ep))
193 (when (optional-dispatch-more-entry fun)
194 (local-call-analyze-1 (optional-dispatch-more-entry fun)))))
197 ;;; Notice a Ref that is not in a local-call context. If the Ref is
198 ;;; already to an XEP, then do nothing, otherwise change it to the
199 ;;; XEP, making an XEP if necessary.
201 ;;; If Ref is to a special :Cleanup or :Escape function, then we treat
202 ;;; it as though it was not an XEP reference (i.e. leave it alone.)
203 (defun reference-entry-point (ref)
204 (declare (type ref ref))
205 (let ((fun (ref-leaf ref)))
206 (unless (or (external-entry-point-p fun)
207 (member (functional-kind fun) '(:escape :cleanup)))
208 (change-ref-leaf ref (or (functional-entry-function fun)
209 (make-external-entry-point fun))))))
211 ;;; Attempt to convert all references to Fun to local calls. The
212 ;;; reference must be the function for a call, and the function
213 ;;; continuation must be used only once, since otherwise we cannot be
214 ;;; sure what function is to be called. The call continuation would be
215 ;;; multiply used if there is hairy stuff such as conditionals in the
216 ;;; expression that computes the function.
218 ;;; If we cannot convert a reference, then we mark the referenced
219 ;;; function as an entry-point, creating a new XEP if necessary. We
220 ;;; don't try to convert calls that are in error (:ERROR kind.)
222 ;;; This is broken off from Local-Call-Analyze so that people can
223 ;;; force analysis of newly introduced calls. Note that we don't do
224 ;;; LET conversion here.
225 (defun local-call-analyze-1 (fun)
226 (declare (type functional fun))
227 (let ((refs (leaf-refs fun))
230 (let* ((cont (node-cont ref))
231 (dest (continuation-dest cont)))
232 (cond ((and (basic-combination-p dest)
233 (eq (basic-combination-fun dest) cont)
234 (eq (continuation-use cont) ref))
236 (convert-call-if-possible ref dest)
238 (unless (eq (basic-combination-kind dest) :local)
239 (reference-entry-point ref)))
241 (reference-entry-point ref))))
242 (setq first-time nil)))
246 ;;; We examine all New-Functions in component, attempting to convert
247 ;;; calls into local calls when it is legal. We also attempt to
248 ;;; convert each lambda to a LET. LET conversion is also triggered by
249 ;;; deletion of a function reference, but functions that start out
250 ;;; eligible for conversion must be noticed sometime.
252 ;;; Note that there is a lot of action going on behind the scenes
253 ;;; here, triggered by reference deletion. In particular, the
254 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and let
255 ;;; converted lambdas, so it is important that the lambda is added to
256 ;;; the COMPONENT-LAMBDAS when it is. Also, the
257 ;;; COMPONENT-NEW-FUNCTIONS may contain all sorts of drivel, since it
258 ;;; is not updated when we delete functions, etc. Only
259 ;;; COMPONENT-LAMBDAS is updated.
261 ;;; COMPONENT-REANALYZE-FUNCTIONS is treated similarly to
262 ;;; NEW-FUNCTIONS, but we don't add lambdas to the LAMBDAS.
263 (defun local-call-analyze (component)
264 (declare (type component component))
266 (let* ((new (pop (component-new-functions component)))
267 (fun (or new (pop (component-reanalyze-functions component)))))
268 (unless fun (return))
269 (let ((kind (functional-kind fun)))
270 (cond ((member kind '(:deleted :let :mv-let :assignment)))
271 ((and (null (leaf-refs fun)) (eq kind nil)
272 (not (functional-entry-function fun)))
273 (delete-functional fun))
275 (when (and new (lambda-p fun))
276 (push fun (component-lambdas component)))
277 (local-call-analyze-1 fun)
279 (maybe-let-convert fun)))))))
283 ;;; If policy is auspicious, Call is not in an XEP, and we don't seem
284 ;;; to be in an infinite recursive loop, then change the reference to
285 ;;; reference a fresh copy. We return whichever function we decide to
287 (defun maybe-expand-local-inline (fun ref call)
288 (if (and (policy call (>= speed space) (>= speed cspeed))
289 (not (eq (functional-kind (node-home-lambda call)) :external))
290 (not *converting-for-interpreter*)
291 (inline-expansion-ok call))
292 (with-ir1-environment call
293 (let* ((*lexenv* (functional-lexenv fun))
295 (res (catch 'local-call-lossage
297 (ir1-convert-lambda (functional-inline-expansion fun))
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 (assert (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 (assert (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 "function called with ~R argument~:P, but wants exactly ~R"
423 (setf (basic-combination-kind call) :error)))))
425 ;;;; optional, more and keyword calls
427 ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
428 ;;; only fixed args are supplied, then convert a call to the correct entry
429 ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
430 ;;; don't convert calls to functions that have a more (or rest) arg.
431 (defun convert-hairy-call (ref call fun)
432 (declare (type ref ref) (type combination call)
433 (type optional-dispatch fun))
434 (let ((min-args (optional-dispatch-min-args fun))
435 (max-args (optional-dispatch-max-args fun))
436 (call-args (length (combination-args call))))
437 (cond ((< call-args min-args)
438 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
439 ;; Compiler" that calling a function with "the wrong number of
440 ;; arguments" be only a STYLE-ERROR. I think, though, that this
441 ;; should only apply when the number of arguments is inferred
442 ;; from a previous definition. If the number of arguments
443 ;; is DECLAIMed, surely calling with the wrong number is a
444 ;; real WARNING. As long as SBCL continues to use CMU CL's
445 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
446 ;; but as long as we continue to use that policy, that's the
447 ;; not our biggest problem.:-| When we fix that policy, this
448 ;; should come back into compliance. (So fix that policy!)
450 "function called with ~R argument~:P, but wants at least ~R"
452 (setf (basic-combination-kind call) :error))
453 ((<= call-args max-args)
454 (convert-call ref call
455 (elt (optional-dispatch-entry-points fun)
456 (- call-args min-args))))
457 ((optional-dispatch-more-entry fun)
458 (convert-more-call ref call fun))
460 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
461 ;; Compiler" that calling a function with "the wrong number of
462 ;; arguments" be only a STYLE-ERROR. I think, though, that this
463 ;; should only apply when the number of arguments is inferred
464 ;; from a previous definition. If the number of arguments
465 ;; is DECLAIMed, surely calling with the wrong number is a
466 ;; real WARNING. As long as SBCL continues to use CMU CL's
467 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
468 ;; but as long as we continue to use that policy, that's the
469 ;; not our biggest problem.:-| When we fix that policy, this
470 ;; should come back into compliance. (So fix that policy!)
472 "function called with ~R argument~:P, but wants at most ~R"
474 (setf (basic-combination-kind call) :error))))
477 ;;; This function is used to convert a call to an entry point when complex
478 ;;; transformations need to be done on the original arguments. Entry is the
479 ;;; entry point function that we are calling. Vars is a list of variable names
480 ;;; which are bound to the original call arguments. Ignores is the subset of
481 ;;; Vars which are ignored. Args is the list of arguments to the entry point
484 ;;; In order to avoid gruesome graph grovelling, we introduce a new function
485 ;;; that rearranges the arguments and calls the entry point. We analyze the
486 ;;; new function and the entry point immediately so that everything gets
487 ;;; converted during the single pass.
488 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
489 (declare (list vars ignores args) (type ref ref) (type combination call)
490 (type clambda entry))
492 (with-ir1-environment call
495 (declare (ignorable . ,ignores))
496 (%funcall ,entry . ,args))))))
497 (convert-call ref call new-fun)
498 (dolist (ref (leaf-refs entry))
499 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
501 ;;; Use Convert-Hairy-Fun-Entry to convert a more-arg call to a known
502 ;;; function into a local call to the Main-Entry.
504 ;;; First we verify that all keywords are constant and legal. If there
505 ;;; aren't, then we warn the user and don't attempt to convert the call.
507 ;;; We massage the supplied keyword arguments into the order expected by the
508 ;;; main entry. This is done by binding all the arguments to the keyword call
509 ;;; to variables in the introduced lambda, then passing these values variables
510 ;;; in the correct order when calling the main entry. Unused arguments
511 ;;; (such as the keywords themselves) are discarded simply by not passing them
514 ;;; If there is a rest arg, then we bundle up the args and pass them to LIST.
515 (defun convert-more-call (ref call fun)
516 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
517 (let* ((max (optional-dispatch-max-args fun))
518 (arglist (optional-dispatch-arglist fun))
519 (args (combination-args call))
520 (more (nthcdr max args))
521 (flame (policy call (or (> speed brevity) (> space brevity))))
523 (temps (make-gensym-list max))
524 (more-temps (make-gensym-list (length more))))
529 (dolist (var arglist)
530 (let ((info (lambda-var-arg-info var)))
532 (ecase (arg-info-kind info)
536 ((:more-context :more-count)
537 (compiler-warning "can't local-call functions with &MORE args")
538 (setf (basic-combination-kind call) :error)
539 (return-from convert-more-call))))))
541 (when (optional-dispatch-keyp fun)
542 (when (oddp (length more))
543 (compiler-warning "function called with odd number of ~
544 arguments in keyword portion")
546 (setf (basic-combination-kind call) :error)
547 (return-from convert-more-call))
549 (do ((key more (cddr key))
550 (temp more-temps (cddr temp)))
552 (let ((cont (first key)))
553 (unless (constant-continuation-p cont)
555 (compiler-note "non-constant keyword in keyword call"))
556 (setf (basic-combination-kind call) :error)
557 (return-from convert-more-call))
559 (let ((name (continuation-value cont))
562 (dolist (var (key-vars)
566 (let ((info (lambda-var-arg-info var)))
567 (when (eq (arg-info-keyword info) name)
569 (supplied (cons var val))
572 (when (and loser (not (optional-dispatch-allowp fun)))
573 (compiler-warning "function called with unknown argument keyword ~S"
575 (setf (basic-combination-kind call) :error)
576 (return-from convert-more-call)))
578 (collect ((call-args))
579 (do ((var arglist (cdr var))
580 (temp temps (cdr temp)))
582 (let ((info (lambda-var-arg-info (car var))))
584 (ecase (arg-info-kind info)
586 (call-args (car temp))
587 (when (arg-info-supplied-p info)
590 (call-args `(list ,@more-temps))
594 (call-args (car temp)))))
596 (dolist (var (key-vars))
597 (let ((info (lambda-var-arg-info var))
598 (temp (cdr (assoc var (supplied)))))
601 (call-args (arg-info-default info)))
602 (when (arg-info-supplied-p info)
603 (call-args (not (null temp))))))
605 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
606 (append temps more-temps)
607 (ignores) (call-args)))))
613 ;;;; Converting to a LET has differing significance to various parts of the
615 ;;;; -- The body of a LET is spliced in immediately after the corresponding
616 ;;;; combination node, making the control transfer explicit and allowing
617 ;;;; LETs to be mashed together into a single block. The value of the LET is
618 ;;;; delivered directly to the original continuation for the call,
619 ;;;; eliminating the need to propagate information from the dummy result
621 ;;;; -- As far as IR1 optimization is concerned, it is interesting in that
622 ;;;; there is only one expression that the variable can be bound to, and
623 ;;;; this is easily substitited for.
624 ;;;; -- LETs are interesting to environment analysis and to the back end
625 ;;;; because in most ways a LET can be considered to be "the same function"
626 ;;;; as its home function.
627 ;;;; -- LET conversion has dynamic scope implications, since control transfers
628 ;;;; within the same environment are local. In a local control transfer,
629 ;;;; cleanup code must be emitted to remove dynamic bindings that are no
630 ;;;; longer in effect.
632 ;;; Set up the control transfer to the called lambda. We split the call
633 ;;; block immediately after the call, and link the head of FUN to the call
634 ;;; block. The successor block after splitting (where we return to) is
637 ;;; If the lambda is is a different component than the call, then we call
638 ;;; JOIN-COMPONENTS. This only happens in block compilation before
639 ;;; FIND-INITIAL-DFO.
640 (defun insert-let-body (fun call)
641 (declare (type clambda fun) (type basic-combination call))
642 (let* ((call-block (node-block call))
643 (bind-block (node-block (lambda-bind fun)))
644 (component (block-component call-block)))
645 (let ((fun-component (block-component bind-block)))
646 (unless (eq fun-component component)
647 (assert (eq (component-kind component) :initial))
648 (join-components component fun-component)))
650 (let ((*current-component* component))
651 (node-ends-block call))
652 ;; FIXME: Use PROPER-LIST-OF-LENGTH-P here, and look for other
653 ;; uses of '=.*length' which could also be converted to use
654 ;; PROPER-LIST-OF-LENGTH-P.
655 (assert (= (length (block-succ call-block)) 1))
656 (let ((next-block (first (block-succ call-block))))
657 (unlink-blocks call-block next-block)
658 (link-blocks call-block bind-block)
661 ;;; Handle the environment semantics of LET conversion. We add the
662 ;;; lambda and its LETs to lets for the CALL's home function. We merge
663 ;;; the calls for FUN with the calls for the home function, removing
664 ;;; FUN in the process. We also merge the Entries.
666 ;;; We also unlink the function head from the component head and set
667 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
669 (defun merge-lets (fun call)
670 (declare (type clambda fun) (type basic-combination call))
671 (let ((component (block-component (node-block call))))
672 (unlink-blocks (component-head component) (node-block (lambda-bind fun)))
673 (setf (component-lambdas component)
674 (delete fun (component-lambdas component)))
675 (setf (component-reanalyze component) t))
676 (setf (lambda-call-lexenv fun) (node-lexenv call))
677 (let ((tails (lambda-tail-set fun)))
678 (setf (tail-set-functions tails)
679 (delete fun (tail-set-functions tails))))
680 (setf (lambda-tail-set fun) nil)
681 (let* ((home (node-home-lambda call))
682 (home-env (lambda-environment home)))
683 (push fun (lambda-lets home))
684 (setf (lambda-home fun) home)
685 (setf (lambda-environment fun) home-env)
687 (let ((lets (lambda-lets fun)))
689 (setf (lambda-home let) home)
690 (setf (lambda-environment let) home-env))
692 (setf (lambda-lets home) (nconc lets (lambda-lets home)))
693 (setf (lambda-lets fun) ()))
695 (setf (lambda-calls home)
696 (delete fun (nunion (lambda-calls fun) (lambda-calls home))))
697 (setf (lambda-calls fun) ())
699 (setf (lambda-entries home)
700 (nconc (lambda-entries fun) (lambda-entries home)))
701 (setf (lambda-entries fun) ()))
704 ;;; Handle the value semantics of LET conversion. Delete FUN's return
705 ;;; node, and change the control flow to transfer to NEXT-BLOCK
706 ;;; instead. Move all the uses of the result continuation to CALL's
709 ;;; If the actual continuation is only used by the LET call, then we
710 ;;; intersect the type assertion on the dummy continuation with the
711 ;;; assertion for the actual continuation; in all other cases
712 ;;; assertions on the dummy continuation are lost.
714 ;;; We also intersect the derived type of the CALL with the derived
715 ;;; type of all the dummy continuation's uses. This serves mainly to
716 ;;; propagate TRULY-THE through LETs.
717 (defun move-return-uses (fun call next-block)
718 (declare (type clambda fun) (type basic-combination call)
719 (type cblock next-block))
720 (let* ((return (lambda-return fun))
721 (return-block (node-block return)))
722 (unlink-blocks return-block
723 (component-tail (block-component return-block)))
724 (link-blocks return-block next-block)
726 (delete-return return)
727 (let ((result (return-result return))
728 (cont (node-cont call))
729 (call-type (node-derived-type call)))
730 (when (eq (continuation-use cont) call)
731 (assert-continuation-type cont (continuation-asserted-type result)))
732 (unless (eq call-type *wild-type*)
733 (do-uses (use result)
734 (derive-node-type use call-type)))
735 (substitute-continuation-uses cont result)))
738 ;;; Change all CONT for all the calls to FUN to be the start
739 ;;; continuation for the bind node. This allows the blocks to be
740 ;;; joined if the caller count ever goes to one.
741 (defun move-let-call-cont (fun)
742 (declare (type clambda fun))
743 (let ((new-cont (node-prev (lambda-bind fun))))
744 (dolist (ref (leaf-refs fun))
745 (let ((dest (continuation-dest (node-cont ref))))
746 (delete-continuation-use dest)
747 (add-continuation-use dest new-cont))))
750 ;;; We are converting FUN to be a LET when the call is in a non-tail
751 ;;; position. Any previously tail calls in FUN are no longer tail
752 ;;; calls, and must be restored to normal calls which transfer to
753 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
754 ;;; the RETURN-RESULT, because the return might have been deleted (if
755 ;;; all calls were TR.)
757 ;;; The called function might be an assignment in the case where we
758 ;;; are currently converting that function. In steady-state,
759 ;;; assignments never appear in the lambda-calls.
760 (defun unconvert-tail-calls (fun call next-block)
761 (dolist (called (lambda-calls fun))
762 (dolist (ref (leaf-refs called))
763 (let ((this-call (continuation-dest (node-cont ref))))
764 (when (and (node-tail-p this-call)
765 (eq (node-home-lambda this-call) fun))
766 (setf (node-tail-p this-call) nil)
767 (ecase (functional-kind called)
768 ((nil :cleanup :optional)
769 (let ((block (node-block this-call))
770 (cont (node-cont call)))
771 (ensure-block-start cont)
772 (unlink-blocks block (first (block-succ block)))
773 (link-blocks block next-block)
774 (delete-continuation-use this-call)
775 (add-continuation-use this-call cont)))
778 (assert (eq called fun))))))))
781 ;;; Deal with returning from a LET or assignment that we are
782 ;;; converting. FUN is the function we are calling, CALL is a call to
783 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
784 ;;; NULL if call is a tail call.
786 ;;; If the call is not a tail call, then we must do
787 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
788 ;;; its value out of the enclosing non-let function. When call is
789 ;;; non-TR, we must convert it back to an ordinary local call, since
790 ;;; the value must be delivered to the receiver of CALL's value.
792 ;;; We do different things depending on whether the caller and callee
793 ;;; have returns left:
795 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT. Either
796 ;;; the function doesn't return, or all returns are via tail-recursive
798 ;;; -- If CALL is a non-tail call, or if both have returns, then we
799 ;;; delete the callee's return, move its uses to the call's result
800 ;;; continuation, and transfer control to the appropriate return point.
801 ;;; -- If the callee has a return, but the caller doesn't, then we move the
802 ;;; return to the caller.
803 (defun move-return-stuff (fun call next-block)
804 (declare (type clambda fun) (type basic-combination call)
805 (type (or cblock null) next-block))
807 (unconvert-tail-calls fun call next-block))
808 (let* ((return (lambda-return fun))
809 (call-fun (node-home-lambda call))
810 (call-return (lambda-return call-fun)))
812 ((or next-block call-return)
813 (unless (block-delete-p (node-block return))
814 (move-return-uses fun call
815 (or next-block (node-block call-return)))))
817 (assert (node-tail-p call))
818 (setf (lambda-return call-fun) return)
819 (setf (return-lambda return) call-fun))))
820 (move-let-call-cont fun)
823 ;;; Actually do LET conversion. We call subfunctions to do most of the
824 ;;; work. We change the CALL's cont to be the continuation heading the
825 ;;; bind block, and also do REOPTIMIZE-CONTINUATION on the args and
826 ;;; Cont so that let-specific IR1 optimizations get a chance. We blow
827 ;;; away any entry for the function in *FREE-FUNCTIONS* so that nobody
828 ;;; will create new reference to it.
829 (defun let-convert (fun call)
830 (declare (type clambda fun) (type basic-combination call))
831 (let ((next-block (if (node-tail-p call)
833 (insert-let-body fun call))))
834 (move-return-stuff fun call next-block)
835 (merge-lets fun call)))
837 ;;; Reoptimize all of Call's args and its result.
838 (defun reoptimize-call (call)
839 (declare (type basic-combination call))
840 (dolist (arg (basic-combination-args call))
842 (reoptimize-continuation arg)))
843 (reoptimize-continuation (node-cont call))
846 ;;; We also don't convert calls to named functions which appear in the
847 ;;; initial component, delaying this until optimization. This
848 ;;; minimizes the likelyhood that we well let-convert a function which
849 ;;; may have references added due to later local inline expansion
850 (defun ok-initial-convert-p (fun)
851 (not (and (leaf-name fun)
854 (node-block (lambda-bind fun))))
857 ;;; This function is called when there is some reason to believe that
858 ;;; the lambda Fun might be converted into a let. This is done after
859 ;;; local call analysis, and also when a reference is deleted. We only
860 ;;; convert to a let when the function is a normal local function, has
861 ;;; no XEP, and is referenced in exactly one local call. Conversion is
862 ;;; also inhibited if the only reference is in a block about to be
863 ;;; deleted. We return true if we converted.
865 ;;; These rules may seem unnecessarily restrictive, since there are
866 ;;; some cases where we could do the return with a jump that don't
867 ;;; satisfy these requirements. The reason for doing things this way
868 ;;; is that it makes the concept of a LET much more useful at the
869 ;;; level of IR1 semantics. The :ASSIGNMENT function kind provides
870 ;;; another way to optimize calls to single-return/multiple call
873 ;;; We don't attempt to convert calls to functions that have an XEP,
874 ;;; since we might be embarrassed later when we want to convert a
875 ;;; newly discovered local call. Also, see OK-INITIAL-CONVERT-P.
876 (defun maybe-let-convert (fun)
877 (declare (type clambda fun))
878 (let ((refs (leaf-refs fun)))
881 (member (functional-kind fun) '(nil :assignment))
882 (not (functional-entry-function fun)))
883 (let* ((ref-cont (node-cont (first refs)))
884 (dest (continuation-dest ref-cont)))
885 (when (and (basic-combination-p dest)
886 (eq (basic-combination-fun dest) ref-cont)
887 (eq (basic-combination-kind dest) :local)
888 (not (block-delete-p (node-block dest)))
889 (cond ((ok-initial-convert-p fun) t)
891 (reoptimize-continuation ref-cont)
893 (unless (eq (functional-kind fun) :assignment)
894 (let-convert fun dest))
895 (reoptimize-call dest)
896 (setf (functional-kind fun)
897 (if (mv-combination-p dest) :mv-let :let))))
900 ;;;; tail local calls and assignments
902 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
903 ;;; they definitely won't generate any cleanup code. Currently we
904 ;;; recognize lexical entry points that are only used locally (if at
906 (defun only-harmless-cleanups (block1 block2)
907 (declare (type cblock block1 block2))
908 (or (eq block1 block2)
909 (let ((cleanup2 (block-start-cleanup block2)))
910 (do ((cleanup (block-end-cleanup block1)
911 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
912 ((eq cleanup cleanup2) t)
913 (case (cleanup-kind cleanup)
915 (unless (null (entry-exits (cleanup-mess-up cleanup)))
917 (t (return nil)))))))
919 ;;; If a potentially TR local call really is TR, then convert it to
920 ;;; jump directly to the called function. We also call
921 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
922 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
923 ;;; the succesor (potentially deleting the RETURN node) unless:
924 ;;; -- The call has already been converted.
925 ;;; -- The call isn't TR (some implicit MV PROG1.)
926 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
927 ;;; so that we can use known return inside the component.)
928 ;;; -- There is a change in the cleanup between the call in the return,
929 ;;; so we might need to introduce cleanup code.
930 (defun maybe-convert-tail-local-call (call)
931 (declare (type combination call))
932 (let ((return (continuation-dest (node-cont call))))
933 (assert (return-p return))
934 (when (and (not (node-tail-p call))
935 (immediately-used-p (return-result return) call)
936 (not (eq (functional-kind (node-home-lambda call))
938 (only-harmless-cleanups (node-block call)
939 (node-block return)))
940 (node-ends-block call)
941 (let ((block (node-block call))
942 (fun (combination-lambda call)))
943 (setf (node-tail-p call) t)
944 (unlink-blocks block (first (block-succ block)))
945 (link-blocks block (node-block (lambda-bind fun)))
946 (values t (maybe-convert-to-assignment fun))))))
948 ;;; This is called when we believe it might make sense to convert Fun
949 ;;; to an assignment. All this function really does is determine when
950 ;;; a function with more than one call can still be combined with the
951 ;;; calling function's environment. We can convert when:
952 ;;; -- The function is a normal, non-entry function, and
953 ;;; -- Except for one call, all calls must be tail recursive calls
954 ;;; in the called function (i.e. are self-recursive tail calls)
955 ;;; -- OK-INITIAL-CONVERT-P is true.
957 ;;; There may be one outside call, and it need not be tail-recursive.
958 ;;; Since all tail local calls have already been converted to direct
959 ;;; transfers, the only control semantics needed are to splice in the
960 ;;; body at the non-tail call. If there is no non-tail call, then we
961 ;;; need only merge the environments. Both cases are handled by
964 ;;; ### It would actually be possible to allow any number of outside
965 ;;; calls as long as they all return to the same place (i.e. have the
966 ;;; same conceptual continuation.) A special case of this would be
967 ;;; when all of the outside calls are tail recursive.
968 (defun maybe-convert-to-assignment (fun)
969 (declare (type clambda fun))
970 (when (and (not (functional-kind fun))
971 (not (functional-entry-function fun)))
974 (when (and (dolist (ref (leaf-refs fun) t)
975 (let ((dest (continuation-dest (node-cont ref))))
976 (when (block-delete-p (node-block dest)) (return nil))
977 (let ((home (node-home-lambda ref)))
978 (unless (eq home fun)
979 (when call-fun (return nil))
980 (setq call-fun home))
981 (unless (node-tail-p dest)
982 (when (or non-tail (eq home fun)) (return nil))
983 (setq non-tail dest)))))
984 (ok-initial-convert-p fun))
985 (setf (functional-kind fun) :assignment)
986 (let-convert fun (or non-tail
988 (node-cont (first (leaf-refs fun))))))
989 (when non-tail (reoptimize-call non-tail))