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
25 ;;; function FUN to the actual arguments in CALL. This is also called
26 ;;; by the VALUES IR1 optimizer when it sleazily converts MV-BINDs to
29 ;;; We flush all arguments to CALL that correspond to unreferenced
30 ;;; variables in FUN. We leave NILs in the COMBINATION-ARGS so that
31 ;;; the remaining args still match up with their vars.
33 ;;; We also apply the declared variable type assertion to the argument
35 (defun propagate-to-args (call fun)
36 (declare (type combination call) (type clambda fun))
37 (loop with policy = (lexenv-policy (node-lexenv call))
38 for args on (basic-combination-args call)
39 and var in (lambda-vars fun)
40 do (assert-lvar-type (car args) (leaf-type var) policy)
41 do (unless (leaf-refs var)
42 (flush-dest (car args))
43 (setf (car args) nil)))
46 (defun handle-nested-dynamic-extent-lvars (dx lvar)
47 (let ((uses (lvar-uses lvar)))
48 ;; DX value generators must end their blocks: see UPDATE-UVL-LIVE-SETS.
49 ;; Uses of mupltiple-use LVARs already end their blocks, so we just need
50 ;; to process uses of single-use LVARs.
52 (node-ends-block uses))
53 ;; If this LVAR's USE is good for DX, it is either a CAST, or it
54 ;; must be a regular combination whose arguments are potentially DX as well.
58 (handle-nested-dynamic-extent-lvars dx (cast-value use)))
60 (loop for arg in (combination-args use)
61 ;; deleted args show up as NIL here
62 when (and arg (lvar-good-for-dx-p arg dx))
63 append (handle-nested-dynamic-extent-lvars dx arg)))
65 (let* ((other (trivial-lambda-var-ref-lvar use)))
66 (unless (eq other lvar)
67 (handle-nested-dynamic-extent-lvars dx other)))))))
71 when (use-good-for-dx-p use dx)
73 (when (use-good-for-dx-p uses dx)
76 (defun recognize-dynamic-extent-lvars (call fun)
77 (declare (type combination call) (type clambda fun))
78 (loop for arg in (basic-combination-args call)
79 for var in (lambda-vars fun)
80 for dx = (lambda-var-dynamic-extent var)
81 when (and dx arg (not (lvar-dynamic-extent arg)))
82 append (handle-nested-dynamic-extent-lvars dx arg) into dx-lvars
83 finally (when dx-lvars
84 ;; Stack analysis requires that the CALL ends the block, so
85 ;; that MAP-BLOCK-NLXES sees the cleanup we insert here.
86 (node-ends-block call)
87 (let* ((entry (with-ir1-environment-from-node call
89 (cleanup (make-cleanup :kind :dynamic-extent
92 (setf (entry-cleanup entry) cleanup)
93 (insert-node-before call entry)
94 (setf (node-lexenv call)
95 (make-lexenv :default (node-lexenv call)
97 (push entry (lambda-entries (node-home-lambda entry)))
98 (dolist (cell dx-lvars)
99 (setf (lvar-dynamic-extent (cdr cell)) cleanup)))))
102 ;;; Called after a transform has been applied to CALL: if the call has a DX
103 ;;; result, propagate the DXness to the new functional as well.
105 ;;; This is needed in case an earlier call to LOCALL-ANALYZE-COMPONENT
106 ;;; collected DX information before the transformation, in which case a later
107 ;;; call to LOCALL-ANALYZE-COMPONENT would not pick up the DX declaration
108 ;;; again, since the call has already been converted. (In other words, work
109 ;;; around the fact that optimization iterates, and locall analysis may have
110 ;;; already run by the time we are able to transform something.)
111 (defun maybe-propagate-dynamic-extent (call fun)
113 (let* ((lvar (combination-lvar call))
114 (cleanup (or (and lvar (lvar-dynamic-extent lvar))
115 (return-from maybe-propagate-dynamic-extent)))
116 (ret (lambda-return fun))
119 (return-from maybe-propagate-dynamic-extent)))
120 (dx (car (rassoc lvar (cleanup-info cleanup) :test #'eq)))
121 (new-dx-lvars (if (and dx res)
122 (handle-nested-dynamic-extent-lvars dx res)
123 (return-from maybe-propagate-dynamic-extent))))
125 ;; This builds on what RECOGNIZE-DYNAMIC-EXTENT-LVARS does above.
126 (aver (eq call (block-last (node-block call))))
127 (dolist (cell new-dx-lvars)
128 (let ((lvar (cdr cell)))
129 (aver (not (lvar-dynamic-extent lvar)))
130 (push cell (cleanup-info cleanup))
131 (setf (lvar-dynamic-extent (cdr cell)) cleanup)))))))
133 ;;; This function handles merging the tail sets if CALL is potentially
134 ;;; tail-recursive, and is a call to a function with a different
135 ;;; TAIL-SET than CALL's FUN. This must be called whenever we alter
136 ;;; IR1 so as to place a local call in what might be a tail-recursive
137 ;;; context. Note that any call which returns its value to a RETURN is
138 ;;; considered potentially tail-recursive, since any implicit MV-PROG1
139 ;;; might be optimized away.
141 ;;; We destructively modify the set for the calling function to
142 ;;; represent both, and then change all the functions in callee's set
143 ;;; to reference the first. If we do merge, we reoptimize the
144 ;;; RETURN-RESULT lvar to cause IR1-OPTIMIZE-RETURN to recompute the
146 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
147 (declare (type basic-combination call) (type clambda new-fun))
148 (let ((return (node-dest call)))
149 (when (return-p return)
150 (let ((call-set (lambda-tail-set (node-home-lambda call)))
151 (fun-set (lambda-tail-set new-fun)))
152 (unless (eq call-set fun-set)
153 (let ((funs (tail-set-funs fun-set)))
155 (setf (lambda-tail-set fun) call-set))
156 (setf (tail-set-funs call-set)
157 (nconc (tail-set-funs call-set) funs)))
158 (reoptimize-lvar (return-result return))
161 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
162 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
163 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
164 ;;; the function in the REF node with the new function.
166 ;;; We change the REF last, since changing the reference can trigger
167 ;;; LET conversion of the new function, but will only do so if the
168 ;;; call is local. Note that the replacement may trigger LET
169 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
170 ;;; with NEW-FUN before the substitution, since after the substitution
171 ;;; (and LET conversion), the call may no longer be recognizable as
173 (defun convert-call (ref call fun)
174 (declare (type ref ref) (type combination call) (type clambda fun))
175 (propagate-to-args call fun)
176 (setf (basic-combination-kind call) :local)
177 (unless (call-full-like-p call)
178 (dolist (arg (basic-combination-args call))
180 (flush-lvar-externally-checkable-type arg))))
181 (sset-adjoin fun (lambda-calls-or-closes (node-home-lambda call)))
182 (recognize-dynamic-extent-lvars call fun)
183 (merge-tail-sets call fun)
184 (change-ref-leaf ref fun)
187 ;;;; external entry point creation
189 ;;; Return a LAMBDA form that can be used as the definition of the XEP
192 ;;; If FUN is a LAMBDA, then we check the number of arguments
193 ;;; (conditional on policy) and call FUN with all the arguments.
195 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
196 ;;; of supplied arguments by doing do an = test for each entry-point,
197 ;;; calling the entry with the appropriate prefix of the passed
200 ;;; If there is a &MORE arg, then there are a couple of optimizations
201 ;;; that we make (more for space than anything else):
202 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
203 ;;; no argument count error is possible.
204 ;;; -- We can omit the = clause for the last entry-point, allowing the
205 ;;; case of 0 more args to fall through to the more entry.
207 ;;; We don't bother to policy conditionalize wrong arg errors in
208 ;;; optional dispatches, since the additional overhead is negligible
209 ;;; compared to the cost of everything else going on.
211 ;;; Note that if policy indicates it, argument type declarations in
212 ;;; FUN will be verified. Since nothing is known about the type of the
213 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
214 ;;; are passed to the actual function.
215 (defun make-xep-lambda-expression (fun)
216 (declare (type functional fun))
219 (let ((nargs (length (lambda-vars fun)))
220 (n-supplied (gensym))
221 (temps (make-gensym-list (length (lambda-vars fun)))))
222 `(lambda (,n-supplied ,@temps)
223 (declare (type index ,n-supplied))
224 ,(if (policy *lexenv* (zerop verify-arg-count))
225 `(declare (ignore ,n-supplied))
226 `(%verify-arg-count ,n-supplied ,nargs))
228 (declare (optimize (merge-tail-calls 3)))
229 (%funcall ,fun ,@temps)))))
231 (let* ((min (optional-dispatch-min-args fun))
232 (max (optional-dispatch-max-args fun))
233 (more (optional-dispatch-more-entry fun))
234 (n-supplied (gensym))
235 (temps (make-gensym-list max)))
237 ;; Force convertion of all entries
238 (optional-dispatch-entry-point-fun fun 0)
239 (loop for ep in (optional-dispatch-entry-points fun)
241 do (entries `((eql ,n-supplied ,n)
242 (%funcall ,(force ep) ,@(subseq temps 0 n)))))
243 `(lambda (,n-supplied ,@temps)
244 (declare (type index ,n-supplied))
246 ,@(if more (butlast (entries)) (entries))
248 ;; KLUDGE: (NOT (< ...)) instead of >= avoids one round of
249 ;; deftransforms and lambda-conversion.
250 `((,(if (zerop min) t `(not (< ,n-supplied ,max)))
251 ,(with-unique-names (n-context n-count)
252 `(multiple-value-bind (,n-context ,n-count)
253 (%more-arg-context ,n-supplied ,max)
255 (declare (optimize (merge-tail-calls 3)))
256 (%funcall ,more ,@temps ,n-context ,n-count)))))))
258 (%arg-count-error ,n-supplied)))))))))
260 ;;; Make an external entry point (XEP) for FUN and return it. We
261 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
262 ;;; then associate this lambda with FUN as its XEP. After the
263 ;;; conversion, we iterate over the function's associated lambdas,
264 ;;; redoing local call analysis so that the XEP calls will get
267 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
268 ;;; discover an XEP after the initial local call analyze pass.
269 (defun make-xep (fun)
270 (declare (type functional fun))
271 (aver (null (functional-entry-fun fun)))
272 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
273 (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
274 :debug-name (debug-name
275 'xep (leaf-debug-name fun))
277 (setf (functional-kind res) :external
278 (leaf-ever-used res) t
279 (functional-entry-fun res) fun
280 (functional-entry-fun fun) res
281 (component-reanalyze *current-component*) t)
282 (reoptimize-component *current-component* :maybe)
285 (locall-analyze-fun-1 fun))
287 (dolist (ep (optional-dispatch-entry-points fun))
288 (locall-analyze-fun-1 (force ep)))
289 (when (optional-dispatch-more-entry fun)
290 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
293 ;;; Notice a REF that is not in a local-call context. If the REF is
294 ;;; already to an XEP, then do nothing, otherwise change it to the
295 ;;; XEP, making an XEP if necessary.
297 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
298 ;;; it as though it was not an XEP reference (i.e. leave it alone).
299 (defun reference-entry-point (ref)
300 (declare (type ref ref))
301 (let ((fun (ref-leaf ref)))
302 (unless (or (xep-p fun)
303 (member (functional-kind fun) '(:escape :cleanup)))
304 (change-ref-leaf ref (or (functional-entry-fun fun)
307 ;;; Attempt to convert all references to FUN to local calls. The
308 ;;; reference must be the function for a call, and the function lvar
309 ;;; must be used only once, since otherwise we cannot be sure what
310 ;;; function is to be called. The call lvar would be multiply used if
311 ;;; there is hairy stuff such as conditionals in the expression that
312 ;;; computes the function.
314 ;;; If we cannot convert a reference, then we mark the referenced
315 ;;; function as an entry-point, creating a new XEP if necessary. We
316 ;;; don't try to convert calls that are in error (:ERROR kind.)
318 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
319 ;;; can force analysis of newly introduced calls. Note that we don't
320 ;;; do LET conversion here.
321 (defun locall-analyze-fun-1 (fun)
322 (declare (type functional fun))
323 (let ((refs (leaf-refs fun))
326 (let* ((lvar (node-lvar ref))
327 (dest (when lvar (lvar-dest lvar))))
328 (unless (node-to-be-deleted-p ref)
329 (cond ((and (basic-combination-p dest)
330 (eq (basic-combination-fun dest) lvar)
331 (eq (lvar-uses lvar) ref))
333 (convert-call-if-possible ref dest)
335 (unless (eq (basic-combination-kind dest) :local)
336 (reference-entry-point ref)
339 (reference-entry-point ref)
340 (setq local-p nil))))))
341 (when local-p (note-local-functional fun)))
345 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
346 ;;; calls into local calls when it is legal. We also attempt to
347 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
348 ;;; deletion of a function reference, but functions that start out
349 ;;; eligible for conversion must be noticed sometime.
351 ;;; Note that there is a lot of action going on behind the scenes
352 ;;; here, triggered by reference deletion. In particular, the
353 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
354 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
355 ;;; the COMPONENT-LAMBDAS when it is. Also, the
356 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
357 ;;; it is not updated when we delete functions, etc. Only
358 ;;; COMPONENT-LAMBDAS is updated.
360 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
361 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
363 (defun locall-analyze-component (component)
364 (declare (type component component))
365 (aver-live-component component)
367 (let* ((new-functional (pop (component-new-functionals component)))
368 (functional (or new-functional
369 (pop (component-reanalyze-functionals component)))))
372 (let ((kind (functional-kind functional)))
373 (cond ((or (functional-somewhat-letlike-p functional)
374 (memq kind '(:deleted :zombie)))
375 (values)) ; nothing to do
376 ((and (null (leaf-refs functional)) (eq kind nil)
377 (not (functional-entry-fun functional)))
378 (delete-functional functional))
380 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
381 (cond ((not (lambda-p functional))
382 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
385 (new-functional ; FUNCTIONAL came from
386 ; NEW-FUNCTIONALS, hence is new.
387 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
388 (aver (not (member functional
389 (component-lambdas component))))
390 (push functional (component-lambdas component)))
391 (t ; FUNCTIONAL is old.
392 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
393 (aver (member functional (component-lambdas
395 (locall-analyze-fun-1 functional)
396 (when (lambda-p functional)
397 (maybe-let-convert functional)))))))
400 (defun locall-analyze-clambdas-until-done (clambdas)
402 (let ((did-something nil))
403 (dolist (clambda clambdas)
404 (let ((component (lambda-component clambda)))
405 ;; The original CMU CL code seemed to implicitly assume that
406 ;; COMPONENT is the only one here. Let's make that explicit.
407 (aver (= 1 (length (functional-components clambda))))
408 (aver (eql component (first (functional-components clambda))))
409 (when (or (component-new-functionals component)
410 (component-reanalyze-functionals component))
411 (setf did-something t)
412 (locall-analyze-component component))))
413 (unless did-something
417 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
418 ;;; to be in an infinite recursive loop, then change the reference to
419 ;;; reference a fresh copy. We return whichever function we decide to
421 (defun maybe-expand-local-inline (original-functional ref call)
422 (if (and (policy call
423 (and (>= speed space)
424 (>= speed compilation-speed)))
425 (not (eq (functional-kind (node-home-lambda call)) :external))
426 (inline-expansion-ok call))
427 (let* ((end (component-last-block (node-component call)))
428 (pred (block-prev end)))
429 (multiple-value-bind (losing-local-object converted-lambda)
430 (catch 'locall-already-let-converted
431 (with-ir1-environment-from-node call
432 (let ((*lexenv* (functional-lexenv original-functional)))
435 (functional-inline-expansion original-functional)
436 :debug-name (debug-name 'local-inline
438 original-functional)))))))
439 (cond (losing-local-object
440 (if (functional-p losing-local-object)
441 (let ((*compiler-error-context* call))
442 (compiler-notify "couldn't inline expand because expansion ~
443 calls this LET-converted local function:~
445 (leaf-debug-name losing-local-object)))
446 (let ((*compiler-error-context* call))
447 (compiler-notify "implementation limitation: couldn't inline ~
448 expand because expansion refers to ~
449 the optimized away object ~S."
450 losing-local-object)))
451 (loop for block = (block-next pred) then (block-next block)
453 do (setf (block-delete-p block) t))
454 (loop for block = (block-next pred) then (block-next block)
456 do (delete-block block t))
459 (change-ref-leaf ref converted-lambda)
461 original-functional))
463 ;;; Dispatch to the appropriate function to attempt to convert a call.
464 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
465 ;;; optimization as well as in local call analysis. If the call is is
466 ;;; already :LOCAL, we do nothing. If the call is already scheduled
467 ;;; for deletion, also do nothing (in addition to saving time, this
468 ;;; also avoids some problems with optimizing collections of functions
469 ;;; that are partially deleted.)
471 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
472 ;;; called on a :INITIAL component, we don't care whether the caller
473 ;;; and callee are in the same component. Afterward, we must stick
474 ;;; with whatever component division we have chosen.
476 ;;; Before attempting to convert a call, we see whether the function
477 ;;; is supposed to be inline expanded. Call conversion proceeds as
478 ;;; before after any expansion.
480 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
481 ;;; warnings will get the right context.
482 (defun convert-call-if-possible (ref call)
483 (declare (type ref ref) (type basic-combination call))
484 (let* ((block (node-block call))
485 (component (block-component block))
486 (original-fun (ref-leaf ref)))
487 (aver (functional-p original-fun))
488 (unless (or (member (basic-combination-kind call) '(:local :error))
489 (node-to-be-deleted-p call)
490 (member (functional-kind original-fun)
491 '(:toplevel-xep :deleted))
492 (not (or (eq (component-kind component) :initial)
495 (lambda-bind (main-entry original-fun))))
497 (let ((fun (if (xep-p original-fun)
498 (functional-entry-fun original-fun)
500 (*compiler-error-context* call))
502 (when (and (eq (functional-inlinep fun) :inline)
503 (rest (leaf-refs original-fun)))
504 (setq fun (maybe-expand-local-inline fun ref call)))
506 (aver (member (functional-kind fun)
507 '(nil :escape :cleanup :optional)))
508 (cond ((mv-combination-p call)
509 (convert-mv-call ref call fun))
511 (convert-lambda-call ref call fun))
513 (convert-hairy-call ref call fun))))))
517 ;;; Attempt to convert a multiple-value call. The only interesting
518 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
519 ;;; exactly one reference and no XEP, and is called with one values
522 ;;; We change the call to be to the last optional entry point and
523 ;;; change the call to be local. Due to our preconditions, the call
524 ;;; should eventually be converted to a let, but we can't do that now,
525 ;;; since there may be stray references to the e-p lambda due to
526 ;;; optional defaulting code.
528 ;;; We also use variable types for the called function to construct an
529 ;;; assertion for the values lvar.
531 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
532 (defun convert-mv-call (ref call fun)
533 (declare (type ref ref) (type mv-combination call) (type functional fun))
534 (when (and (looks-like-an-mv-bind fun)
535 (singleton-p (leaf-refs fun))
536 (singleton-p (basic-combination-args call)))
537 (let* ((*current-component* (node-component ref))
538 (ep (optional-dispatch-entry-point-fun
539 fun (optional-dispatch-max-args fun))))
540 (when (null (leaf-refs ep))
541 (aver (= (optional-dispatch-min-args fun) 0))
542 (aver (not (functional-entry-fun fun)))
543 (setf (basic-combination-kind call) :local)
544 (sset-adjoin ep (lambda-calls-or-closes (node-home-lambda call)))
545 (merge-tail-sets call ep)
546 (change-ref-leaf ref ep)
549 (first (basic-combination-args call))
550 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
551 (lexenv-policy (node-lexenv call))))))
554 ;;; Attempt to convert a call to a lambda. If the number of args is
555 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
556 ;;; from future consideration. If the argcount is O.K. then we just
558 (defun convert-lambda-call (ref call fun)
559 (declare (type ref ref) (type combination call) (type clambda fun))
560 (let ((nargs (length (lambda-vars fun)))
561 (n-call-args (length (combination-args call))))
562 (cond ((= n-call-args nargs)
563 (convert-call ref call fun))
566 'local-argument-mismatch
568 "function called with ~R argument~:P, but wants exactly ~R"
569 :format-arguments (list n-call-args nargs))
570 (setf (basic-combination-kind call) :error)))))
572 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
574 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
575 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
576 ;;; a call to the correct entry point. If &KEY args are supplied, then
577 ;;; dispatch to a subfunction. We don't convert calls to functions
578 ;;; that have a &MORE (or &REST) arg.
579 (defun convert-hairy-call (ref call fun)
580 (declare (type ref ref) (type combination call)
581 (type optional-dispatch fun))
582 (let ((min-args (optional-dispatch-min-args fun))
583 (max-args (optional-dispatch-max-args fun))
584 (call-args (length (combination-args call))))
585 (cond ((< call-args min-args)
587 'local-argument-mismatch
589 "function called with ~R argument~:P, but wants at least ~R"
590 :format-arguments (list call-args min-args))
591 (setf (basic-combination-kind call) :error))
592 ((<= call-args max-args)
593 (convert-call ref call
594 (let ((*current-component* (node-component ref)))
595 (optional-dispatch-entry-point-fun
596 fun (- call-args min-args)))))
597 ((optional-dispatch-more-entry fun)
598 (convert-more-call ref call fun))
601 'local-argument-mismatch
603 "function called with ~R argument~:P, but wants at most ~R"
605 (list call-args max-args))
606 (setf (basic-combination-kind call) :error))))
609 ;;; This function is used to convert a call to an entry point when
610 ;;; complex transformations need to be done on the original arguments.
611 ;;; ENTRY is the entry point function that we are calling. VARS is a
612 ;;; list of variable names which are bound to the original call
613 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
614 ;;; is the list of arguments to the entry point function.
616 ;;; In order to avoid gruesome graph grovelling, we introduce a new
617 ;;; function that rearranges the arguments and calls the entry point.
618 ;;; We analyze the new function and the entry point immediately so
619 ;;; that everything gets converted during the single pass.
620 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
621 (declare (list vars ignores args) (type ref ref) (type combination call)
622 (type clambda entry))
624 (with-ir1-environment-from-node call
627 (declare (ignorable ,@ignores))
628 (%funcall ,entry ,@args))
629 :debug-name (debug-name 'hairy-function-entry
631 (basic-combination-fun call)))
633 (convert-call ref call new-fun)
634 (dolist (ref (leaf-refs entry))
635 (convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
637 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
638 ;;; function into a local call to the MAIN-ENTRY.
640 ;;; First we verify that all keywords are constant and legal. If there
641 ;;; aren't, then we warn the user and don't attempt to convert the call.
643 ;;; We massage the supplied &KEY arguments into the order expected
644 ;;; by the main entry. This is done by binding all the arguments to
645 ;;; the keyword call to variables in the introduced lambda, then
646 ;;; passing these values variables in the correct order when calling
647 ;;; the main entry. Unused arguments (such as the keywords themselves)
648 ;;; are discarded simply by not passing them along.
650 ;;; If there is a &REST arg, then we bundle up the args and pass them
652 (defun convert-more-call (ref call fun)
653 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
654 (let* ((max (optional-dispatch-max-args fun))
655 (arglist (optional-dispatch-arglist fun))
656 (args (combination-args call))
657 (more (nthcdr max args))
658 (flame (policy call (or (> speed inhibit-warnings)
659 (> space inhibit-warnings))))
663 (temps (make-gensym-list max))
664 (more-temps (make-gensym-list (length more))))
669 (dolist (var arglist)
670 (let ((info (lambda-var-arg-info var)))
672 (ecase (arg-info-kind info)
676 ((:more-context :more-count)
677 (compiler-warn "can't local-call functions with &MORE args")
678 (setf (basic-combination-kind call) :error)
679 (return-from convert-more-call))))))
681 (when (optional-dispatch-keyp fun)
682 (when (oddp (length more))
683 (compiler-warn "function called with odd number of ~
684 arguments in keyword portion")
685 (setf (basic-combination-kind call) :error)
686 (return-from convert-more-call))
688 (do ((key more (cddr key))
689 (temp more-temps (cddr temp)))
691 (let ((lvar (first key)))
692 (unless (constant-lvar-p lvar)
694 (compiler-notify "non-constant keyword in keyword call"))
695 (setf (basic-combination-kind call) :error)
696 (return-from convert-more-call))
698 (let ((name (lvar-value lvar))
701 (when (and (eq name :allow-other-keys) (not allow-found))
702 (let ((val (second key)))
703 (cond ((constant-lvar-p val)
705 allowp (lvar-value val)))
707 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
708 (setf (basic-combination-kind call) :error)
709 (return-from convert-more-call)))))
710 (dolist (var (key-vars)
713 (unless (eq name :allow-other-keys)
714 (setq loser (list name)))))
715 (let ((info (lambda-var-arg-info var)))
716 (when (eq (arg-info-key info) name)
718 (if (member var (supplied) :key #'car)
720 (supplied (cons var val)))
723 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
724 (compiler-warn "function called with unknown argument keyword ~S"
726 (setf (basic-combination-kind call) :error)
727 (return-from convert-more-call)))
729 (collect ((call-args))
730 (do ((var arglist (cdr var))
731 (temp temps (cdr temp)))
733 (let ((info (lambda-var-arg-info (car var))))
735 (ecase (arg-info-kind info)
737 (call-args (car temp))
738 (when (arg-info-supplied-p info)
741 (call-args `(list ,@more-temps))
745 (call-args (car temp)))))
747 (dolist (var (key-vars))
748 (let ((info (lambda-var-arg-info var))
749 (temp (cdr (assoc var (supplied)))))
752 (call-args (arg-info-default info)))
753 (when (arg-info-supplied-p info)
754 (call-args (not (null temp))))))
756 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
757 (append temps more-temps)
758 (ignores) (call-args)))))
764 ;;;; Converting to a LET has differing significance to various parts
765 ;;;; of the compiler:
766 ;;;; -- The body of a LET is spliced in immediately after the
767 ;;;; corresponding combination node, making the control transfer
768 ;;;; explicit and allowing LETs to be mashed together into a single
769 ;;;; block. The value of the LET is delivered directly to the
770 ;;;; original lvar for the call, eliminating the need to
771 ;;;; propagate information from the dummy result lvar.
772 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
773 ;;;; that there is only one expression that the variable can be bound
774 ;;;; to, and this is easily substituted for.
775 ;;;; -- LETs are interesting to environment analysis and to the back
776 ;;;; end because in most ways a LET can be considered to be "the
777 ;;;; same function" as its home function.
778 ;;;; -- LET conversion has dynamic scope implications, since control
779 ;;;; transfers within the same environment are local. In a local
780 ;;;; control transfer, cleanup code must be emitted to remove
781 ;;;; dynamic bindings that are no longer in effect.
783 ;;; Set up the control transfer to the called CLAMBDA. We split the
784 ;;; call block immediately after the call, and link the head of
785 ;;; CLAMBDA to the call block. The successor block after splitting
786 ;;; (where we return to) is returned.
788 ;;; If the lambda is is a different component than the call, then we
789 ;;; call JOIN-COMPONENTS. This only happens in block compilation
790 ;;; before FIND-INITIAL-DFO.
791 (defun insert-let-body (clambda call)
792 (declare (type clambda clambda) (type basic-combination call))
793 (let* ((call-block (node-block call))
794 (bind-block (node-block (lambda-bind clambda)))
795 (component (block-component call-block)))
796 (aver-live-component component)
797 (let ((clambda-component (block-component bind-block)))
798 (unless (eq clambda-component component)
799 (aver (eq (component-kind component) :initial))
800 (join-components component clambda-component)))
801 (let ((*current-component* component))
802 (node-ends-block call))
803 (destructuring-bind (next-block)
804 (block-succ call-block)
805 (unlink-blocks call-block next-block)
806 (link-blocks call-block bind-block)
809 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
810 ;;; same set as; but leave CLAMBDA with a valid tail set value of
811 ;;; its own, for the benefit of code which might try to pull
812 ;;; something out of it (e.g. return type).
813 (defun depart-from-tail-set (clambda)
814 ;; Until sbcl-0.pre7.37.flaky5.2, we did
815 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
816 ;; (SETF (TAIL-SET-FUNS TAILS)
817 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
818 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
819 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
820 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
821 ;; inconsistent, and perhaps unsafe, for us to think we're in the
822 ;; tail set. Unfortunately..
824 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
825 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
826 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
827 ;; which you called in order to get the address of an external LAMBDA):
828 ;; the external function was defined in terms of internal function,
829 ;; which was LET-converted, and then things blew up downstream when
830 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
831 ;; the now-NILed-out TAIL-SET. So..
833 ;; To deal with this problem, we no longer NIL out
834 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
835 ;; * If we're the only function in TAIL-SET-FUNS, it should
836 ;; be safe to leave ourself linked to it, and it to you.
837 ;; * If there are other functions in TAIL-SET-FUNS, then we're
838 ;; afraid of future optimizations on those functions causing
839 ;; the TAIL-SET object no longer to be valid to describe our
840 ;; return value. Thus, we delete ourselves from that object;
841 ;; but we save a newly-allocated tail-set, derived from the old
842 ;; one, for ourselves, for the use of later code (e.g.
843 ;; FINALIZE-XEP-DEFINITION) which might want to
844 ;; know about our return type.
845 (let* ((old-tail-set (lambda-tail-set clambda))
846 (old-tail-set-funs (tail-set-funs old-tail-set)))
847 (unless (= 1 (length old-tail-set-funs))
848 (setf (tail-set-funs old-tail-set)
849 (delete clambda old-tail-set-funs))
850 (let ((new-tail-set (copy-tail-set old-tail-set)))
851 (setf (lambda-tail-set clambda) new-tail-set
852 (tail-set-funs new-tail-set) (list clambda)))))
853 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
854 ;; remain valid in this case, so we nuke it on the theory that
855 ;; missing information tends to be less dangerous than incorrect
857 (setf (tail-set-info (lambda-tail-set clambda)) nil))
859 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
860 ;;; its LETs to LETs for the CALL's home function. We merge the calls
861 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
862 ;;; in the process. We also merge the ENTRIES.
864 ;;; We also unlink the function head from the component head and set
865 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
867 (defun merge-lets (clambda call)
869 (declare (type clambda clambda) (type basic-combination call))
871 (let ((component (node-component call)))
872 (unlink-blocks (component-head component) (lambda-block clambda))
873 (setf (component-lambdas component)
874 (delete clambda (component-lambdas component)))
875 (setf (component-reanalyze component) t))
876 (setf (lambda-call-lexenv clambda) (node-lexenv call))
878 (depart-from-tail-set clambda)
880 (let* ((home (node-home-lambda call))
881 (home-physenv (lambda-physenv home))
882 (physenv (lambda-physenv clambda)))
884 (aver (not (eq home clambda)))
886 ;; CLAMBDA belongs to HOME now.
887 (push clambda (lambda-lets home))
888 (setf (lambda-home clambda) home)
889 (setf (lambda-physenv clambda) home-physenv)
893 (setf home-physenv (get-lambda-physenv home)))
894 (setf (physenv-nlx-info home-physenv)
895 (nconc (physenv-nlx-info physenv)
896 (physenv-nlx-info home-physenv))))
898 ;; All of CLAMBDA's LETs belong to HOME now.
899 (let ((lets (lambda-lets clambda)))
901 (setf (lambda-home let) home)
902 (setf (lambda-physenv let) home-physenv))
903 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
904 ;; CLAMBDA no longer has an independent existence as an entity
906 (setf (lambda-lets clambda) nil)
908 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
910 (sset-union (lambda-calls-or-closes home)
911 (lambda-calls-or-closes clambda))
912 (sset-delete clambda (lambda-calls-or-closes home))
913 ;; CLAMBDA no longer has an independent existence as an entity
914 ;; which calls things or has DFO dependencies.
915 (setf (lambda-calls-or-closes clambda) nil)
917 ;; All of CLAMBDA's ENTRIES belong to HOME now.
918 (setf (lambda-entries home)
919 (nconc (lambda-entries clambda)
920 (lambda-entries home)))
921 ;; CLAMBDA no longer has an independent existence as an entity
923 (setf (lambda-entries clambda) nil))
927 ;;; Handle the value semantics of LET conversion. Delete FUN's return
928 ;;; node, and change the control flow to transfer to NEXT-BLOCK
929 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
930 (defun move-return-uses (fun call next-block)
931 (declare (type clambda fun) (type basic-combination call)
932 (type cblock next-block))
933 (let* ((return (lambda-return fun))
935 (ensure-block-start (node-prev return))
936 (node-block return))))
937 (unlink-blocks return-block
938 (component-tail (block-component return-block)))
939 (link-blocks return-block next-block)
941 (delete-return return)
942 (let ((result (return-result return))
943 (lvar (if (node-tail-p call)
944 (return-result (lambda-return (node-home-lambda call)))
946 (call-type (node-derived-type call)))
947 (unless (eq call-type *wild-type*)
948 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
949 (do-uses (use result)
950 (derive-node-type use call-type)))
951 (substitute-lvar-uses lvar result
952 (and lvar (eq (lvar-uses lvar) call)))))
955 ;;; We are converting FUN to be a LET when the call is in a non-tail
956 ;;; position. Any previously tail calls in FUN are no longer tail
957 ;;; calls, and must be restored to normal calls which transfer to
958 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
959 ;;; the RETURN-RESULT, because the return might have been deleted (if
960 ;;; all calls were TR.)
961 (defun unconvert-tail-calls (fun call next-block)
962 (do-sset-elements (called (lambda-calls-or-closes fun))
963 (when (lambda-p called)
964 (dolist (ref (leaf-refs called))
965 (let ((this-call (node-dest ref)))
967 (node-tail-p this-call)
968 (eq (node-home-lambda this-call) fun))
969 (setf (node-tail-p this-call) nil)
970 (ecase (functional-kind called)
971 ((nil :cleanup :optional)
972 (let ((block (node-block this-call))
973 (lvar (node-lvar call)))
974 (unlink-blocks block (first (block-succ block)))
975 (link-blocks block next-block)
976 (aver (not (node-lvar this-call)))
977 (add-lvar-use this-call lvar)))
979 ;; The called function might be an assignment in the
980 ;; case where we are currently converting that function.
981 ;; In steady-state, assignments never appear as a called
984 (aver (eq called fun)))))))))
987 ;;; Deal with returning from a LET or assignment that we are
988 ;;; converting. FUN is the function we are calling, CALL is a call to
989 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
990 ;;; NULL if call is a tail call.
992 ;;; If the call is not a tail call, then we must do
993 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
994 ;;; its value out of the enclosing non-let function. When call is
995 ;;; non-TR, we must convert it back to an ordinary local call, since
996 ;;; the value must be delivered to the receiver of CALL's value.
998 ;;; We do different things depending on whether the caller and callee
999 ;;; have returns left:
1001 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
1002 ;;; Either the function doesn't return, or all returns are via
1003 ;;; tail-recursive local calls.
1004 ;;; -- If CALL is a non-tail call, or if both have returns, then
1005 ;;; we delete the callee's return, move its uses to the call's
1006 ;;; result lvar, and transfer control to the appropriate
1008 ;;; -- If the callee has a return, but the caller doesn't, then we
1009 ;;; move the return to the caller.
1010 (defun move-return-stuff (fun call next-block)
1011 (declare (type clambda fun) (type basic-combination call)
1012 (type (or cblock null) next-block))
1014 (unconvert-tail-calls fun call next-block))
1015 (let* ((return (lambda-return fun))
1016 (call-fun (node-home-lambda call))
1017 (call-return (lambda-return call-fun)))
1018 (when (and call-return
1019 (block-delete-p (node-block call-return)))
1020 (delete-return call-return)
1021 (unlink-node call-return)
1022 (setq call-return nil))
1023 (cond ((not return))
1024 ((or next-block call-return)
1025 (unless (block-delete-p (node-block return))
1027 (ensure-block-start (node-prev call-return))
1028 (setq next-block (node-block call-return)))
1029 (move-return-uses fun call next-block)))
1031 (aver (node-tail-p call))
1032 (setf (lambda-return call-fun) return)
1033 (setf (return-lambda return) call-fun)
1034 (setf (lambda-return fun) nil))))
1035 (%delete-lvar-use call) ; LET call does not have value semantics
1038 ;;; Actually do LET conversion. We call subfunctions to do most of the
1039 ;;; work. We do REOPTIMIZE-LVAR on the args and CALL's lvar so that
1040 ;;; LET-specific IR1 optimizations get a chance. We blow away any
1041 ;;; entry for the function in *FREE-FUNS* so that nobody will create
1042 ;;; new references to it.
1043 (defun let-convert (fun call)
1044 (declare (type clambda fun) (type basic-combination call))
1045 (let* ((next-block (insert-let-body fun call))
1046 (next-block (if (node-tail-p call)
1049 (move-return-stuff fun call next-block)
1050 (merge-lets fun call)
1051 (setf (node-tail-p call) nil)
1052 ;; If CALL has a derive type NIL, it means that "its return" is
1053 ;; unreachable, but the next BIND is still reachable; in order to
1054 ;; not confuse MAYBE-TERMINATE-BLOCK...
1055 (setf (node-derived-type call) *wild-type*)))
1057 ;;; Reoptimize all of CALL's args and its result.
1058 (defun reoptimize-call (call)
1059 (declare (type basic-combination call))
1060 (dolist (arg (basic-combination-args call))
1062 (reoptimize-lvar arg)))
1063 (reoptimize-lvar (node-lvar call))
1066 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
1068 (defun declarations-suppress-let-conversion-p (clambda)
1069 ;; From the user's point of view, LET-converting something that
1070 ;; has a name is inlining it. (The user can't see what we're doing
1071 ;; with anonymous things, and suppressing inlining
1072 ;; for such things can easily give Python acute indigestion, so
1074 (when (leaf-has-source-name-p clambda)
1075 ;; ANSI requires that explicit NOTINLINE be respected.
1076 (or (eq (lambda-inlinep clambda) :notinline)
1077 ;; If (= LET-CONVERSION 0) we can guess that inlining
1078 ;; generally won't be appreciated, but if the user
1079 ;; specifically requests inlining, that takes precedence over
1080 ;; our general guess.
1081 (and (policy clambda (= let-conversion 0))
1082 (not (eq (lambda-inlinep clambda) :inline))))))
1084 ;;; We also don't convert calls to named functions which appear in the
1085 ;;; initial component, delaying this until optimization. This
1086 ;;; minimizes the likelihood that we will LET-convert a function which
1087 ;;; may have references added due to later local inline expansion.
1088 (defun ok-initial-convert-p (fun)
1089 (not (and (leaf-has-source-name-p fun)
1090 (or (declarations-suppress-let-conversion-p fun)
1091 (eq (component-kind (lambda-component fun))
1094 ;;; This function is called when there is some reason to believe that
1095 ;;; CLAMBDA might be converted into a LET. This is done after local
1096 ;;; call analysis, and also when a reference is deleted. We return
1097 ;;; true if we converted.
1098 (defun maybe-let-convert (clambda)
1099 (declare (type clambda clambda))
1100 (unless (or (declarations-suppress-let-conversion-p clambda)
1101 (functional-has-external-references-p clambda))
1102 ;; We only convert to a LET when the function is a normal local
1103 ;; function, has no XEP, and is referenced in exactly one local
1104 ;; call. Conversion is also inhibited if the only reference is in
1105 ;; a block about to be deleted.
1107 ;; These rules limiting LET conversion may seem unnecessarily
1108 ;; restrictive, since there are some cases where we could do the
1109 ;; return with a jump that don't satisfy these requirements. The
1110 ;; reason for doing things this way is that it makes the concept
1111 ;; of a LET much more useful at the level of IR1 semantics. The
1112 ;; :ASSIGNMENT function kind provides another way to optimize
1113 ;; calls to single-return/multiple call functions.
1115 ;; We don't attempt to convert calls to functions that have an
1116 ;; XEP, since we might be embarrassed later when we want to
1117 ;; convert a newly discovered local call. Also, see
1118 ;; OK-INITIAL-CONVERT-P.
1119 (let ((refs (leaf-refs clambda)))
1122 (memq (functional-kind clambda) '(nil :assignment))
1123 (not (functional-entry-fun clambda)))
1124 (binding* ((ref (first refs))
1125 (ref-lvar (node-lvar ref) :exit-if-null)
1126 (dest (lvar-dest ref-lvar)))
1127 (when (and (basic-combination-p dest)
1128 (eq (basic-combination-fun dest) ref-lvar)
1129 (eq (basic-combination-kind dest) :local)
1130 (not (node-to-be-deleted-p dest))
1131 (not (block-delete-p (lambda-block clambda)))
1132 (cond ((ok-initial-convert-p clambda) t)
1134 (reoptimize-lvar ref-lvar)
1136 (when (eq clambda (node-home-lambda dest))
1137 (delete-lambda clambda)
1138 (return-from maybe-let-convert nil))
1139 (unless (eq (functional-kind clambda) :assignment)
1140 (let-convert clambda dest))
1141 (reoptimize-call dest)
1142 (setf (functional-kind clambda)
1143 (if (mv-combination-p dest) :mv-let :let))))
1146 ;;;; tail local calls and assignments
1148 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1149 ;;; they definitely won't generate any cleanup code. Currently we
1150 ;;; recognize lexical entry points that are only used locally (if at
1152 (defun only-harmless-cleanups (block1 block2)
1153 (declare (type cblock block1 block2))
1154 (or (eq block1 block2)
1155 (let ((cleanup2 (block-start-cleanup block2)))
1156 (do ((cleanup (block-end-cleanup block1)
1157 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1158 ((eq cleanup cleanup2) t)
1159 (case (cleanup-kind cleanup)
1161 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1163 (t (return nil)))))))
1165 ;;; If a potentially TR local call really is TR, then convert it to
1166 ;;; jump directly to the called function. We also call
1167 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1168 ;;; tail-convert. The second is the value of M-C-T-A.
1169 (defun maybe-convert-tail-local-call (call)
1170 (declare (type combination call))
1171 (let ((return (lvar-dest (node-lvar call)))
1172 (fun (combination-lambda call)))
1173 (aver (return-p return))
1174 (when (and (not (node-tail-p call)) ; otherwise already converted
1175 ;; this is a tail call
1176 (immediately-used-p (return-result return) call)
1177 (only-harmless-cleanups (node-block call)
1178 (node-block return))
1179 ;; If the call is in an XEP, we might decide to make it
1180 ;; non-tail so that we can use known return inside the
1182 (not (eq (functional-kind (node-home-lambda call))
1184 (not (block-delete-p (lambda-block fun))))
1185 (node-ends-block call)
1186 (let ((block (node-block call)))
1187 (setf (node-tail-p call) t)
1188 (unlink-blocks block (first (block-succ block)))
1189 (link-blocks block (lambda-block fun))
1190 (delete-lvar-use call)
1191 (values t (maybe-convert-to-assignment fun))))))
1193 ;;; This is called when we believe it might make sense to convert
1194 ;;; CLAMBDA to an assignment. All this function really does is
1195 ;;; determine when a function with more than one call can still be
1196 ;;; combined with the calling function's environment. We can convert
1198 ;;; -- The function is a normal, non-entry function, and
1199 ;;; -- Except for one call, all calls must be tail recursive calls
1200 ;;; in the called function (i.e. are self-recursive tail calls)
1201 ;;; -- OK-INITIAL-CONVERT-P is true.
1203 ;;; There may be one outside call, and it need not be tail-recursive.
1204 ;;; Since all tail local calls have already been converted to direct
1205 ;;; transfers, the only control semantics needed are to splice in the
1206 ;;; body at the non-tail call. If there is no non-tail call, then we
1207 ;;; need only merge the environments. Both cases are handled by
1210 ;;; ### It would actually be possible to allow any number of outside
1211 ;;; calls as long as they all return to the same place (i.e. have the
1212 ;;; same conceptual continuation.) A special case of this would be
1213 ;;; when all of the outside calls are tail recursive.
1214 (defun maybe-convert-to-assignment (clambda)
1215 (declare (type clambda clambda))
1216 (when (and (not (functional-kind clambda))
1217 (not (functional-entry-fun clambda))
1218 (not (functional-has-external-references-p clambda)))
1219 (let ((outside-non-tail-call nil)
1221 (when (and (dolist (ref (leaf-refs clambda) t)
1222 (let ((dest (node-dest ref)))
1223 (when (or (not dest)
1224 (block-delete-p (node-block dest)))
1226 (let ((home (node-home-lambda ref)))
1227 (unless (eq home clambda)
1230 (setq outside-call dest))
1231 (unless (node-tail-p dest)
1232 (when (or outside-non-tail-call (eq home clambda))
1234 (setq outside-non-tail-call dest)))))
1235 (ok-initial-convert-p clambda))
1236 (cond (outside-call (setf (functional-kind clambda) :assignment)
1237 (let-convert clambda outside-call)
1238 (when outside-non-tail-call
1239 (reoptimize-call outside-non-tail-call))
1241 (t (delete-lambda clambda)