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