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 for Fun.
103 ;;; If Fun is a lambda, then we check the number of arguments (conditional
104 ;;; on policy) and call Fun with all the arguments.
106 ;;; If Fun is an Optional-Dispatch, then we dispatch off of the number of
107 ;;; supplied arguments by doing do an = test for each entry-point, calling the
108 ;;; entry with the appropriate prefix of the passed arguments.
110 ;;; If there is a more arg, then there are a couple of optimizations that we
111 ;;; make (more for space than anything else):
112 ;;; -- If Min-Args is 0, then we make the more entry a T clause, since no
113 ;;; argument count error is possible.
114 ;;; -- We can omit the = clause for the last entry-point, allowing the case of
115 ;;; 0 more args to fall through to the more entry.
117 ;;; We don't bother to policy conditionalize wrong arg errors in optional
118 ;;; dispatches, since the additional overhead is negligible compared to the
119 ;;; other hair going down.
121 ;;; Note that if policy indicates it, argument type declarations in Fun will
122 ;;; be verified. Since nothing is known about the type of the XEP arg vars,
123 ;;; type checks will be emitted when the XEP's arg vars are passed to the
125 (defun make-xep-lambda (fun)
126 (declare (type functional fun))
129 (let ((nargs (length (lambda-vars fun)))
130 (n-supplied (gensym)))
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)))
150 (do ((eps (optional-dispatch-entry-points fun) (rest eps))
153 (entries `((= ,n-supplied ,n)
154 (%funcall ,(first eps) ,@(subseq (temps) 0 n)))))
156 `(lambda (,n-supplied ,@(temps))
157 ;; FIXME: Make sure that INDEX type distinguishes between target
158 ;; and host. (Probably just make the SB!XC:DEFTYPE different from
160 (declare (type index ,n-supplied))
162 ,@(if more (butlast (entries)) (entries))
164 `((,(if (zerop min) 't `(>= ,n-supplied ,max))
165 ,(let ((n-context (gensym))
167 `(multiple-value-bind (,n-context ,n-count)
168 (%more-arg-context ,n-supplied ,max)
169 (%funcall ,more ,@(temps) ,n-context ,n-count))))))
171 (%argument-count-error ,n-supplied)))))))))
173 ;;; Make an external entry point (XEP) for Fun and return it. We
174 ;;; convert the result of Make-XEP-Lambda in the correct environment,
175 ;;; then associate this lambda with Fun as its XEP. After the
176 ;;; conversion, we iterate over the function's associated lambdas,
177 ;;; redoing local call analysis so that the XEP calls will get
178 ;;; converted. We also bind *LEXENV* to change the compilation policy
179 ;;; over to the interface policy.
181 ;;; We set Reanalyze and Reoptimize in the component, just in case we
182 ;;; discover an XEP after the initial local call analyze pass.
183 (defun make-external-entry-point (fun)
184 (declare (type functional fun))
185 (assert (not (functional-entry-function fun)))
186 (with-ir1-environment (lambda-bind (main-entry fun))
187 (let* ((*lexenv* (make-lexenv :cookie (make-interface-cookie *lexenv*)))
188 (res (ir1-convert-lambda (make-xep-lambda fun))))
189 (setf (functional-kind res) :external)
190 (setf (leaf-ever-used res) t)
191 (setf (functional-entry-function res) fun)
192 (setf (functional-entry-function fun) res)
193 (setf (component-reanalyze *current-component*) t)
194 (setf (component-reoptimize *current-component*) t)
196 (clambda (local-call-analyze-1 fun))
198 (dolist (ep (optional-dispatch-entry-points fun))
199 (local-call-analyze-1 ep))
200 (when (optional-dispatch-more-entry fun)
201 (local-call-analyze-1 (optional-dispatch-more-entry fun)))))
204 ;;; Notice a Ref that is not in a local-call context. If the Ref is
205 ;;; already to an XEP, then do nothing, otherwise change it to the
206 ;;; XEP, making an XEP if necessary.
208 ;;; If Ref is to a special :Cleanup or :Escape function, then we treat
209 ;;; it as though it was not an XEP reference (i.e. leave it alone.)
210 (defun reference-entry-point (ref)
211 (declare (type ref ref))
212 (let ((fun (ref-leaf ref)))
213 (unless (or (external-entry-point-p fun)
214 (member (functional-kind fun) '(:escape :cleanup)))
215 (change-ref-leaf ref (or (functional-entry-function fun)
216 (make-external-entry-point fun))))))
218 ;;; Attempt to convert all references to Fun to local calls. The
219 ;;; reference must be the function for a call, and the function
220 ;;; continuation must be used only once, since otherwise we cannot be
221 ;;; sure what function is to be called. The call continuation would be
222 ;;; multiply used if there is hairy stuff such as conditionals in the
223 ;;; expression that computes the function.
225 ;;; If we cannot convert a reference, then we mark the referenced
226 ;;; function as an entry-point, creating a new XEP if necessary. We
227 ;;; don't try to convert calls that are in error (:ERROR kind.)
229 ;;; This is broken off from Local-Call-Analyze so that people can
230 ;;; force analysis of newly introduced calls. Note that we don't do
231 ;;; LET conversion here.
232 (defun local-call-analyze-1 (fun)
233 (declare (type functional fun))
234 (let ((refs (leaf-refs fun))
237 (let* ((cont (node-cont ref))
238 (dest (continuation-dest cont)))
239 (cond ((and (basic-combination-p dest)
240 (eq (basic-combination-fun dest) cont)
241 (eq (continuation-use cont) ref))
243 (convert-call-if-possible ref dest)
245 (unless (eq (basic-combination-kind dest) :local)
246 (reference-entry-point ref)))
248 (reference-entry-point ref))))
249 (setq first-time nil)))
253 ;;; We examine all New-Functions in component, attempting to convert
254 ;;; calls into local calls when it is legal. We also attempt to
255 ;;; convert each lambda to a LET. LET conversion is also triggered by
256 ;;; deletion of a function reference, but functions that start out
257 ;;; eligible for conversion must be noticed sometime.
259 ;;; Note that there is a lot of action going on behind the scenes
260 ;;; here, triggered by reference deletion. In particular, the
261 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and let
262 ;;; converted lambdas, so it is important that the lambda is added to
263 ;;; the COMPONENT-LAMBDAS when it is. Also, the
264 ;;; COMPONENT-NEW-FUNCTIONS may contain all sorts of drivel, since it
265 ;;; is not updated when we delete functions, etc. Only
266 ;;; COMPONENT-LAMBDAS is updated.
268 ;;; COMPONENT-REANALYZE-FUNCTIONS is treated similarly to
269 ;;; NEW-FUNCTIONS, but we don't add lambdas to the LAMBDAS.
270 (defun local-call-analyze (component)
271 (declare (type component component))
273 (let* ((new (pop (component-new-functions component)))
274 (fun (or new (pop (component-reanalyze-functions component)))))
275 (unless fun (return))
276 (let ((kind (functional-kind fun)))
277 (cond ((member kind '(:deleted :let :mv-let :assignment)))
278 ((and (null (leaf-refs fun)) (eq kind nil)
279 (not (functional-entry-function fun)))
280 (delete-functional fun))
282 (when (and new (lambda-p fun))
283 (push fun (component-lambdas component)))
284 (local-call-analyze-1 fun)
286 (maybe-let-convert fun)))))))
290 ;;; If policy is auspicious, Call is not in an XEP, and we don't seem
291 ;;; to be in an infinite recursive loop, then change the reference to
292 ;;; reference a fresh copy. We return whichever function we decide to
294 (defun maybe-expand-local-inline (fun ref call)
295 (if (and (policy call (>= speed space) (>= speed cspeed))
296 (not (eq (functional-kind (node-home-lambda call)) :external))
297 (not *converting-for-interpreter*)
298 (inline-expansion-ok call))
299 (with-ir1-environment call
300 (let* ((*lexenv* (functional-lexenv fun))
302 (res (catch 'local-call-lossage
304 (ir1-convert-lambda (functional-inline-expansion fun))
307 (change-ref-leaf ref res)
310 (let ((*compiler-error-context* call))
311 (compiler-note "couldn't inline expand because expansion ~
312 calls this let-converted local function:~
318 ;;; Dispatch to the appropriate function to attempt to convert a call. Ref
319 ;;; most be a reference to a FUNCTIONAL. This is called in IR1 optimize as
320 ;;; well as in local call analysis. If the call is is already :Local, we do
321 ;;; nothing. If the call is already scheduled for deletion, also do nothing
322 ;;; (in addition to saving time, this also avoids some problems with optimizing
323 ;;; collections of functions that are partially deleted.)
325 ;;; This is called both before and after FIND-INITIAL-DFO runs. When called
326 ;;; on a :INITIAL component, we don't care whether the caller and callee are in
327 ;;; the same component. Afterward, we must stick with whatever component
328 ;;; division we have chosen.
330 ;;; Before attempting to convert a call, we see whether the function is
331 ;;; supposed to be inline expanded. Call conversion proceeds as before
332 ;;; after any expansion.
334 ;;; We bind *Compiler-Error-Context* to the node for the call so that
335 ;;; warnings will get the right context.
336 (defun convert-call-if-possible (ref call)
337 (declare (type ref ref) (type basic-combination call))
338 (let* ((block (node-block call))
339 (component (block-component block))
340 (original-fun (ref-leaf ref)))
341 (assert (functional-p original-fun))
342 (unless (or (member (basic-combination-kind call) '(:local :error))
343 (block-delete-p block)
344 (eq (functional-kind (block-home-lambda block)) :deleted)
345 (member (functional-kind original-fun)
346 '(:top-level-xep :deleted))
347 (not (or (eq (component-kind component) :initial)
350 (lambda-bind (main-entry original-fun))))
352 (let ((fun (if (external-entry-point-p original-fun)
353 (functional-entry-function original-fun)
355 (*compiler-error-context* call))
357 (when (and (eq (functional-inlinep fun) :inline)
358 (rest (leaf-refs original-fun)))
359 (setq fun (maybe-expand-local-inline fun ref call)))
361 (assert (member (functional-kind fun)
362 '(nil :escape :cleanup :optional)))
363 (cond ((mv-combination-p call)
364 (convert-mv-call ref call fun))
366 (convert-lambda-call ref call fun))
368 (convert-hairy-call ref call fun))))))
372 ;;; Attempt to convert a multiple-value call. The only interesting
373 ;;; case is a call to a function that Looks-Like-An-MV-Bind, has
374 ;;; exactly one reference and no XEP, and is called with one values
377 ;;; We change the call to be to the last optional entry point and
378 ;;; change the call to be local. Due to our preconditions, the call
379 ;;; should eventually be converted to a let, but we can't do that now,
380 ;;; since there may be stray references to the e-p lambda due to
381 ;;; optional defaulting code.
383 ;;; We also use variable types for the called function to construct an
384 ;;; assertion for the values continuation.
386 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
387 (defun convert-mv-call (ref call fun)
388 (declare (type ref ref) (type mv-combination call) (type functional fun))
389 (when (and (looks-like-an-mv-bind fun)
390 (not (functional-entry-function fun))
391 (= (length (leaf-refs fun)) 1)
392 (= (length (basic-combination-args call)) 1))
393 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
394 (setf (basic-combination-kind call) :local)
395 (pushnew ep (lambda-calls (node-home-lambda call)))
396 (merge-tail-sets call ep)
397 (change-ref-leaf ref ep)
399 (assert-continuation-type
400 (first (basic-combination-args call))
401 (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
402 :rest *universal-type*))))
405 ;;; Attempt to convert a call to a lambda. If the number of args is
406 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
407 ;;; from future consideration. If the argcount is O.K. then we just
409 (defun convert-lambda-call (ref call fun)
410 (declare (type ref ref) (type combination call) (type clambda fun))
411 (let ((nargs (length (lambda-vars fun)))
412 (call-args (length (combination-args call))))
413 (cond ((= call-args nargs)
414 (convert-call ref call fun))
416 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
417 ;; Compiler" that calling a function with "the wrong number of
418 ;; arguments" be only a STYLE-ERROR. I think, though, that this
419 ;; should only apply when the number of arguments is inferred
420 ;; from a previous definition. If the number of arguments
421 ;; is DECLAIMed, surely calling with the wrong number is a
422 ;; real WARNING. As long as SBCL continues to use CMU CL's
423 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
424 ;; but as long as we continue to use that policy, that's the
425 ;; not our biggest problem.:-| When we fix that policy, this
426 ;; should come back into compliance. (So fix that policy!)
428 "function called with ~R argument~:P, but wants exactly ~R"
430 (setf (basic-combination-kind call) :error)))))
432 ;;;; optional, more and keyword calls
434 ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
435 ;;; only fixed args are supplied, then convert a call to the correct entry
436 ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
437 ;;; don't convert calls to functions 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: ANSI requires in "3.2.5 Exceptional Situations in the
446 ;; Compiler" that calling a function with "the wrong number of
447 ;; arguments" be only a STYLE-ERROR. I think, though, that this
448 ;; should only apply when the number of arguments is inferred
449 ;; from a previous definition. If the number of arguments
450 ;; is DECLAIMed, surely calling with the wrong number is a
451 ;; real WARNING. As long as SBCL continues to use CMU CL's
452 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
453 ;; but as long as we continue to use that policy, that's the
454 ;; not our biggest problem.:-| When we fix that policy, this
455 ;; should come back into compliance. (So fix that policy!)
457 "function called with ~R argument~:P, but wants at least ~R"
459 (setf (basic-combination-kind call) :error))
460 ((<= call-args max-args)
461 (convert-call ref call
462 (elt (optional-dispatch-entry-points fun)
463 (- call-args min-args))))
464 ((optional-dispatch-more-entry fun)
465 (convert-more-call ref call fun))
467 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
468 ;; Compiler" that calling a function with "the wrong number of
469 ;; arguments" be only a STYLE-ERROR. I think, though, that this
470 ;; should only apply when the number of arguments is inferred
471 ;; from a previous definition. If the number of arguments
472 ;; is DECLAIMed, surely calling with the wrong number is a
473 ;; real WARNING. As long as SBCL continues to use CMU CL's
474 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
475 ;; but as long as we continue to use that policy, that's the
476 ;; not our biggest problem.:-| When we fix that policy, this
477 ;; should come back into compliance. (So fix that policy!)
479 "function called with ~R argument~:P, but wants at most ~R"
481 (setf (basic-combination-kind call) :error))))
484 ;;; This function is used to convert a call to an entry point when complex
485 ;;; transformations need to be done on the original arguments. Entry is the
486 ;;; entry point function that we are calling. Vars is a list of variable names
487 ;;; which are bound to the original call arguments. Ignores is the subset of
488 ;;; Vars which are ignored. Args is the list of arguments to the entry point
491 ;;; In order to avoid gruesome graph grovelling, we introduce a new function
492 ;;; that rearranges the arguments and calls the entry point. We analyze the
493 ;;; new function and the entry point immediately so that everything gets
494 ;;; converted during the single pass.
495 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
496 (declare (list vars ignores args) (type ref ref) (type combination call)
497 (type clambda entry))
499 (with-ir1-environment call
502 (declare (ignorable . ,ignores))
503 (%funcall ,entry . ,args))))))
504 (convert-call ref call new-fun)
505 (dolist (ref (leaf-refs entry))
506 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
508 ;;; Use Convert-Hairy-Fun-Entry to convert a more-arg call to a known
509 ;;; function into a local call to the Main-Entry.
511 ;;; First we verify that all keywords are constant and legal. If there
512 ;;; aren't, then we warn the user and don't attempt to convert the call.
514 ;;; We massage the supplied keyword arguments into the order expected by the
515 ;;; main entry. This is done by binding all the arguments to the keyword call
516 ;;; to variables in the introduced lambda, then passing these values variables
517 ;;; in the correct order when calling the main entry. Unused arguments
518 ;;; (such as the keywords themselves) are discarded simply by not passing them
521 ;;; If there is a rest arg, then we bundle up the args and pass them to LIST.
522 (defun convert-more-call (ref call fun)
523 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
524 (let* ((max (optional-dispatch-max-args fun))
525 (arglist (optional-dispatch-arglist fun))
526 (args (combination-args call))
527 (more (nthcdr max args))
528 (flame (policy call (or (> speed brevity) (> space brevity))))
536 (dolist (var arglist)
537 (let ((info (lambda-var-arg-info var)))
539 (ecase (arg-info-kind info)
543 ((:more-context :more-count)
544 (compiler-warning "can't local-call functions with &MORE args")
545 (setf (basic-combination-kind call) :error)
546 (return-from convert-more-call))))))
549 (temps (gensym "FIXED-ARG-TEMP-")))
551 (dotimes (i (length more))
552 (more-temps (gensym "MORE-ARG-TEMP-")))
554 (when (optional-dispatch-keyp fun)
555 (when (oddp (length more))
556 (compiler-warning "function called with odd number of ~
557 arguments in keyword portion")
559 (setf (basic-combination-kind call) :error)
560 (return-from convert-more-call))
562 (do ((key more (cddr key))
563 (temp (more-temps) (cddr temp)))
565 (let ((cont (first key)))
566 (unless (constant-continuation-p cont)
568 (compiler-note "non-constant keyword in keyword call"))
569 (setf (basic-combination-kind call) :error)
570 (return-from convert-more-call))
572 (let ((name (continuation-value cont))
575 (dolist (var (key-vars)
579 (let ((info (lambda-var-arg-info var)))
580 (when (eq (arg-info-keyword info) name)
582 (supplied (cons var val))
585 (when (and loser (not (optional-dispatch-allowp fun)))
586 (compiler-warning "function called with unknown argument keyword ~S"
588 (setf (basic-combination-kind call) :error)
589 (return-from convert-more-call)))
591 (collect ((call-args))
592 (do ((var arglist (cdr var))
593 (temp (temps) (cdr temp)))
595 (let ((info (lambda-var-arg-info (car var))))
597 (ecase (arg-info-kind info)
599 (call-args (car temp))
600 (when (arg-info-supplied-p info)
603 (call-args `(list ,@(more-temps)))
607 (call-args (car temp)))))
609 (dolist (var (key-vars))
610 (let ((info (lambda-var-arg-info var))
611 (temp (cdr (assoc var (supplied)))))
614 (call-args (arg-info-default info)))
615 (when (arg-info-supplied-p info)
616 (call-args (not (null temp))))))
618 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
619 (append (temps) (more-temps))
620 (ignores) (call-args)))))
626 ;;;; Converting to a LET has differing significance to various parts of the
628 ;;;; -- The body of a LET is spliced in immediately after the corresponding
629 ;;;; combination node, making the control transfer explicit and allowing
630 ;;;; LETs to be mashed together into a single block. The value of the LET is
631 ;;;; delivered directly to the original continuation for the call,
632 ;;;; eliminating the need to propagate information from the dummy result
634 ;;;; -- As far as IR1 optimization is concerned, it is interesting in that
635 ;;;; there is only one expression that the variable can be bound to, and
636 ;;;; this is easily substitited for.
637 ;;;; -- LETs are interesting to environment analysis and to the back end
638 ;;;; because in most ways a LET can be considered to be "the same function"
639 ;;;; as its home function.
640 ;;;; -- LET conversion has dynamic scope implications, since control transfers
641 ;;;; within the same environment are local. In a local control transfer,
642 ;;;; cleanup code must be emitted to remove dynamic bindings that are no
643 ;;;; longer in effect.
645 ;;; Set up the control transfer to the called lambda. We split the call
646 ;;; block immediately after the call, and link the head of FUN to the call
647 ;;; block. The successor block after splitting (where we return to) is
650 ;;; If the lambda is is a different component than the call, then we call
651 ;;; JOIN-COMPONENTS. This only happens in block compilation before
652 ;;; FIND-INITIAL-DFO.
653 (defun insert-let-body (fun call)
654 (declare (type clambda fun) (type basic-combination call))
655 (let* ((call-block (node-block call))
656 (bind-block (node-block (lambda-bind fun)))
657 (component (block-component call-block)))
658 (let ((fun-component (block-component bind-block)))
659 (unless (eq fun-component component)
660 (assert (eq (component-kind component) :initial))
661 (join-components component fun-component)))
663 (let ((*current-component* component))
664 (node-ends-block call))
665 ;; FIXME: Use PROPER-LIST-OF-LENGTH-P here, and look for other
666 ;; uses of '=.*length' which could also be converted to use
667 ;; PROPER-LIST-OF-LENGTH-P.
668 (assert (= (length (block-succ call-block)) 1))
669 (let ((next-block (first (block-succ call-block))))
670 (unlink-blocks call-block next-block)
671 (link-blocks call-block bind-block)
674 ;;; Handle the environment semantics of LET conversion. We add the lambda
675 ;;; and its LETs to lets for the Call's home function. We merge the calls for
676 ;;; Fun with the calls for the home function, removing Fun in the process. We
677 ;;; also merge the Entries.
679 ;;; We also unlink the function head from the component head and set
680 ;;; Component-Reanalyze to true to indicate that the DFO should be recomputed.
681 (defun merge-lets (fun call)
682 (declare (type clambda fun) (type basic-combination call))
683 (let ((component (block-component (node-block call))))
684 (unlink-blocks (component-head component) (node-block (lambda-bind fun)))
685 (setf (component-lambdas component)
686 (delete fun (component-lambdas component)))
687 (setf (component-reanalyze component) t))
688 (setf (lambda-call-lexenv fun) (node-lexenv call))
689 (let ((tails (lambda-tail-set fun)))
690 (setf (tail-set-functions tails)
691 (delete fun (tail-set-functions tails))))
692 (setf (lambda-tail-set fun) nil)
693 (let* ((home (node-home-lambda call))
694 (home-env (lambda-environment home)))
695 (push fun (lambda-lets home))
696 (setf (lambda-home fun) home)
697 (setf (lambda-environment fun) home-env)
699 (let ((lets (lambda-lets fun)))
701 (setf (lambda-home let) home)
702 (setf (lambda-environment let) home-env))
704 (setf (lambda-lets home) (nconc lets (lambda-lets home)))
705 (setf (lambda-lets fun) ()))
707 (setf (lambda-calls home)
708 (nunion (lambda-calls fun)
709 (delete fun (lambda-calls home))))
710 (setf (lambda-calls fun) ())
712 (setf (lambda-entries home)
713 (nconc (lambda-entries fun) (lambda-entries home)))
714 (setf (lambda-entries fun) ()))
717 ;;; Handle the value semantics of let conversion. Delete Fun's return node,
718 ;;; and change the control flow to transfer to Next-Block instead. Move all
719 ;;; the uses of the result continuation to Call's Cont.
721 ;;; If the actual continuation is only used by the let call, then we
722 ;;; intersect the type assertion on the dummy continuation with the assertion
723 ;;; for the actual continuation; in all other cases assertions on the dummy
724 ;;; continuation are lost.
726 ;;; We also intersect the derived type of the call with the derived type of
727 ;;; all the dummy continuation's uses. This serves mainly to propagate
728 ;;; TRULY-THE through lets.
729 (defun move-return-uses (fun call next-block)
730 (declare (type clambda fun) (type basic-combination call)
731 (type cblock next-block))
732 (let* ((return (lambda-return fun))
733 (return-block (node-block return)))
734 (unlink-blocks return-block
735 (component-tail (block-component return-block)))
736 (link-blocks return-block next-block)
738 (delete-return return)
739 (let ((result (return-result return))
740 (cont (node-cont call))
741 (call-type (node-derived-type call)))
742 (when (eq (continuation-use cont) call)
743 (assert-continuation-type cont (continuation-asserted-type result)))
744 (unless (eq call-type *wild-type*)
745 (do-uses (use result)
746 (derive-node-type use call-type)))
747 (substitute-continuation-uses cont result)))
750 ;;; Change all Cont for all the calls to Fun to be the start continuation
751 ;;; for the bind node. This allows the blocks to be joined if the caller count
752 ;;; ever goes to one.
753 (defun move-let-call-cont (fun)
754 (declare (type clambda fun))
755 (let ((new-cont (node-prev (lambda-bind fun))))
756 (dolist (ref (leaf-refs fun))
757 (let ((dest (continuation-dest (node-cont ref))))
758 (delete-continuation-use dest)
759 (add-continuation-use dest new-cont))))
762 ;;; We are converting Fun to be a let when the call is in a non-tail
763 ;;; position. Any previously tail calls in Fun are no longer tail calls, and
764 ;;; must be restored to normal calls which transfer to Next-Block (Fun's
765 ;;; return point.) We can't do this by DO-USES on the RETURN-RESULT, because
766 ;;; the return might have been deleted (if all calls were TR.)
768 ;;; The called function might be an assignment in the case where we are
769 ;;; currently converting that function. In steady-state, assignments never
770 ;;; appear in the lambda-calls.
771 (defun unconvert-tail-calls (fun call next-block)
772 (dolist (called (lambda-calls fun))
773 (dolist (ref (leaf-refs called))
774 (let ((this-call (continuation-dest (node-cont ref))))
775 (when (and (node-tail-p this-call)
776 (eq (node-home-lambda this-call) fun))
777 (setf (node-tail-p this-call) nil)
778 (ecase (functional-kind called)
779 ((nil :cleanup :optional)
780 (let ((block (node-block this-call))
781 (cont (node-cont call)))
782 (ensure-block-start cont)
783 (unlink-blocks block (first (block-succ block)))
784 (link-blocks block next-block)
785 (delete-continuation-use this-call)
786 (add-continuation-use this-call cont)))
789 (assert (eq called fun))))))))
792 ;;; Deal with returning from a let or assignment that we are converting.
793 ;;; FUN is the function we are calling, CALL is a call to FUN, and NEXT-BLOCK
794 ;;; is the return point for a non-tail call, or NULL if call is a tail call.
796 ;;; If the call is not a tail call, then we must do UNCONVERT-TAIL-CALLS, since
797 ;;; a tail call is a call which returns its value out of the enclosing non-let
798 ;;; function. When call is non-TR, we must convert it back to an ordinary
799 ;;; local call, since the value must be delivered to the receiver of CALL's
802 ;;; We do different things depending on whether the caller and callee have
804 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT. Either the
805 ;;; function doesn't return, or all returns are via tail-recursive local
807 ;;; -- If CALL is a non-tail call, or if both have returns, then we
808 ;;; delete the callee's return, move its uses to the call's result
809 ;;; continuation, and transfer control to the appropriate return point.
810 ;;; -- If the callee has a return, but the caller doesn't, then we move the
811 ;;; return to the caller.
812 (defun move-return-stuff (fun call next-block)
813 (declare (type clambda fun) (type basic-combination call)
814 (type (or cblock null) next-block))
816 (unconvert-tail-calls fun call next-block))
817 (let* ((return (lambda-return fun))
818 (call-fun (node-home-lambda call))
819 (call-return (lambda-return call-fun)))
821 ((or next-block call-return)
822 (unless (block-delete-p (node-block return))
823 (move-return-uses fun call
824 (or next-block (node-block call-return)))))
826 (assert (node-tail-p call))
827 (setf (lambda-return call-fun) return)
828 (setf (return-lambda return) call-fun))))
829 (move-let-call-cont fun)
832 ;;; Actually do LET conversion. We call subfunctions to do most of the
833 ;;; work. We change the CALL's cont to be the continuation heading the bind
834 ;;; block, and also do REOPTIMIZE-CONTINUATION on the args and Cont so that
835 ;;; let-specific IR1 optimizations get a chance. We blow away any entry for
836 ;;; the function in *FREE-FUNCTIONS* so that nobody will create new reference
838 (defun let-convert (fun call)
839 (declare (type clambda fun) (type basic-combination call))
840 (let ((next-block (if (node-tail-p call)
842 (insert-let-body fun call))))
843 (move-return-stuff fun call next-block)
844 (merge-lets fun call)))
846 ;;; Reoptimize all of Call's args and its result.
847 (defun reoptimize-call (call)
848 (declare (type basic-combination call))
849 (dolist (arg (basic-combination-args call))
851 (reoptimize-continuation arg)))
852 (reoptimize-continuation (node-cont call))
855 ;;; We also don't convert calls to named functions which appear in the initial
856 ;;; component, delaying this until optimization. This minimizes the likelyhood
857 ;;; that we well let-convert a function which may have references added due to
858 ;;; later local inline expansion
859 (defun ok-initial-convert-p (fun)
860 (not (and (leaf-name fun)
863 (node-block (lambda-bind fun))))
866 ;;; This function is called when there is some reason to believe that
867 ;;; the lambda Fun might be converted into a let. This is done after local
868 ;;; call analysis, and also when a reference is deleted. We only convert to a
869 ;;; let when the function is a normal local function, has no XEP, and is
870 ;;; referenced in exactly one local call. Conversion is also inhibited if the
871 ;;; only reference is in a block about to be deleted. We return true if we
874 ;;; These rules may seem unnecessarily restrictive, since there are some
875 ;;; cases where we could do the return with a jump that don't satisfy these
876 ;;; requirements. The reason for doing things this way is that it makes the
877 ;;; concept of a let much more useful at the level of IR1 semantics. The
878 ;;; :ASSIGNMENT function kind provides another way to optimize calls to
879 ;;; single-return/multiple call functions.
881 ;;; We don't attempt to convert calls to functions that have an XEP, since
882 ;;; we might be embarrassed later when we want to convert a newly discovered
883 ;;; local call. Also, see OK-INITIAL-CONVERT-P.
884 (defun maybe-let-convert (fun)
885 (declare (type clambda fun))
886 (let ((refs (leaf-refs fun)))
889 (member (functional-kind fun) '(nil :assignment))
890 (not (functional-entry-function fun)))
891 (let* ((ref-cont (node-cont (first refs)))
892 (dest (continuation-dest ref-cont)))
893 (when (and (basic-combination-p dest)
894 (eq (basic-combination-fun dest) ref-cont)
895 (eq (basic-combination-kind dest) :local)
896 (not (block-delete-p (node-block dest)))
897 (cond ((ok-initial-convert-p fun) t)
899 (reoptimize-continuation ref-cont)
901 (unless (eq (functional-kind fun) :assignment)
902 (let-convert fun dest))
903 (reoptimize-call dest)
904 (setf (functional-kind fun)
905 (if (mv-combination-p dest) :mv-let :let))))
908 ;;;; tail local calls and assignments
910 ;;; Return T if there are no cleanups between Block1 and Block2, or if they
911 ;;; definitely won't generate any cleanup code. Currently we recognize lexical
912 ;;; entry points that are only used locally (if at all).
913 (defun only-harmless-cleanups (block1 block2)
914 (declare (type cblock block1 block2))
915 (or (eq block1 block2)
916 (let ((cleanup2 (block-start-cleanup block2)))
917 (do ((cleanup (block-end-cleanup block1)
918 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
919 ((eq cleanup cleanup2) t)
920 (case (cleanup-kind cleanup)
922 (unless (null (entry-exits (cleanup-mess-up cleanup)))
924 (t (return nil)))))))
926 ;;; If a potentially TR local call really is TR, then convert it to jump
927 ;;; directly to the called function. We also call MAYBE-CONVERT-TO-ASSIGNMENT.
928 ;;; The first value is true if we tail-convert. The second is the value of
929 ;;; M-C-T-A. We can switch the succesor (potentially deleting the RETURN node)
931 ;;; -- The call has already been converted.
932 ;;; -- The call isn't TR (some implicit MV PROG1.)
933 ;;; -- The call is in an XEP (thus we might decide to make it non-tail so that
934 ;;; we can use known return inside the component.)
935 ;;; -- There is a change in the cleanup between the call in the return, so we
936 ;;; might need to introduce cleanup code.
937 (defun maybe-convert-tail-local-call (call)
938 (declare (type combination call))
939 (let ((return (continuation-dest (node-cont call))))
940 (assert (return-p return))
941 (when (and (not (node-tail-p call))
942 (immediately-used-p (return-result return) call)
943 (not (eq (functional-kind (node-home-lambda call))
945 (only-harmless-cleanups (node-block call)
946 (node-block return)))
947 (node-ends-block call)
948 (let ((block (node-block call))
949 (fun (combination-lambda call)))
950 (setf (node-tail-p call) t)
951 (unlink-blocks block (first (block-succ block)))
952 (link-blocks block (node-block (lambda-bind fun)))
953 (values t (maybe-convert-to-assignment fun))))))
955 ;;; Called when we believe it might make sense to convert Fun to an
956 ;;; assignment. All this function really does is determine when a function
957 ;;; with more than one call can still be combined with the calling function's
958 ;;; environment. We can convert when:
959 ;;; -- The function is a normal, non-entry function, and
960 ;;; -- Except for one call, all calls must be tail recursive calls in the
961 ;;; called function (i.e. are self-recursive tail calls)
962 ;;; -- OK-INITIAL-CONVERT-P is true.
964 ;;; There may be one outside call, and it need not be tail-recursive. Since
965 ;;; all tail local calls have already been converted to direct transfers, the
966 ;;; only control semantics needed are to splice in the body at the non-tail
967 ;;; call. If there is no non-tail call, then we need only merge the
968 ;;; environments. Both cases are handled by LET-CONVERT.
970 ;;; ### It would actually be possible to allow any number of outside calls as
971 ;;; long as they all return to the same place (i.e. have the same conceptual
972 ;;; continuation.) A special case of this would be when all of the outside
973 ;;; calls are tail recursive.
974 (defun maybe-convert-to-assignment (fun)
975 (declare (type clambda fun))
976 (when (and (not (functional-kind fun))
977 (not (functional-entry-function fun)))
980 (when (and (dolist (ref (leaf-refs fun) t)
981 (let ((dest (continuation-dest (node-cont ref))))
982 (when (block-delete-p (node-block dest)) (return nil))
983 (let ((home (node-home-lambda ref)))
984 (unless (eq home fun)
985 (when call-fun (return nil))
986 (setq call-fun home))
987 (unless (node-tail-p dest)
988 (when (or non-tail (eq home fun)) (return nil))
989 (setq non-tail dest)))))
990 (ok-initial-convert-p fun))
991 (setf (functional-kind fun) :assignment)
992 (let-convert fun (or non-tail
994 (node-cont (first (leaf-refs fun))))))
995 (when non-tail (reoptimize-call non-tail))