5351b3244aa359f206331f6cc4a105fe00d10a2f
[sbcl.git] / src / compiler / locall.lisp
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.
8 ;;;;
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.
12
13 ;;;; This software is part of the SBCL system. See the README file for
14 ;;;; more information.
15 ;;;;
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.
21
22 (in-package "SB!C")
23
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
27 ;;; LETs.
28 ;;;
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.
32 ;;;
33 ;;; We also apply the declared variable type assertion to the argument
34 ;;; continuations.
35 (defun propagate-to-args (call fun)
36   (declare (type combination call) (type clambda fun))
37   (do ((args (basic-combination-args call) (cdr args))
38        (vars (lambda-vars fun) (cdr vars)))
39       ((null args))
40     (let ((arg (car args))
41           (var (car vars)))
42       (cond ((leaf-refs var)
43              (assert-continuation-type arg (leaf-type var)))
44             (t
45              (flush-dest arg)
46              (setf (car args) nil)))))
47
48   (values))
49
50 ;;; This function handles merging the tail sets if CALL is potentially
51 ;;; tail-recursive, and is a call to a function with a different
52 ;;; TAIL-SET than CALL's FUN. This must be called whenever we alter
53 ;;; IR1 so as to place a local call in what might be a tail-recursive
54 ;;; context. Note that any call which returns its value to a RETURN is
55 ;;; considered potentially tail-recursive, since any implicit MV-PROG1
56 ;;; might be optimized away.
57 ;;;
58 ;;; We destructively modify the set for the calling function to
59 ;;; represent both, and then change all the functions in callee's set
60 ;;; to reference the first. If we do merge, we reoptimize the
61 ;;; RETURN-RESULT continuation to cause IR1-OPTIMIZE-RETURN to
62 ;;; 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-funs fun-set)))
71             (dolist (fun funs)
72               (setf (lambda-tail-set fun) call-set))
73             (setf (tail-set-funs call-set)
74                   (nconc (tail-set-funs call-set) funs)))
75           (reoptimize-continuation (return-result return))
76           t)))))
77
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.
82 ;;;
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
89 ;;; tail-recursive.
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-or-closes (node-home-lambda call)))
95   (merge-tail-sets call fun)
96   (change-ref-leaf ref fun)
97   (values))
98 \f
99 ;;;; external entry point creation
100
101 ;;; Return a LAMBDA form that can be used as the definition of the XEP
102 ;;; for FUN.
103 ;;;
104 ;;; If FUN is a LAMBDA, then we check the number of arguments
105 ;;; (conditional on policy) and call FUN with all the arguments.
106 ;;;
107 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
108 ;;; of supplied arguments by doing do an = test for each entry-point,
109 ;;; calling the entry with the appropriate prefix of the passed
110 ;;; arguments.
111 ;;;
112 ;;; If there is a &MORE arg, then there are a couple of optimizations
113 ;;; that we make (more for space than anything else):
114 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since 
115 ;;;    no argument count error is possible.
116 ;;; -- We can omit the = clause for the last entry-point, allowing the 
117 ;;;    case of 0 more args to fall through to the more entry.
118 ;;;
119 ;;; We don't bother to policy conditionalize wrong arg errors in
120 ;;; optional dispatches, since the additional overhead is negligible
121 ;;; compared to the cost of everything else going on.
122 ;;;
123 ;;; Note that if policy indicates it, argument type declarations in
124 ;;; FUN will be verified. Since nothing is known about the type of the
125 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
126 ;;; are passed to the actual function.
127 (defun make-xep-lambda-expression (fun)
128   (declare (type functional fun))
129   (etypecase fun
130     (clambda
131      (let ((nargs (length (lambda-vars fun)))
132            (n-supplied (gensym))
133            (temps (make-gensym-list (length (lambda-vars fun)))))
134        `(lambda (,n-supplied ,@temps)
135           (declare (type index ,n-supplied))
136           ,(if (policy *lexenv* (zerop safety))
137                `(declare (ignore ,n-supplied))
138                `(%verify-argument-count ,n-supplied ,nargs))
139           (%funcall ,fun ,@temps))))
140     (optional-dispatch
141      (let* ((min (optional-dispatch-min-args fun))
142             (max (optional-dispatch-max-args fun))
143             (more (optional-dispatch-more-entry fun))
144             (n-supplied (gensym))
145             (temps (make-gensym-list max)))
146        (collect ((entries))
147          (do ((eps (optional-dispatch-entry-points fun) (rest eps))
148               (n min (1+ n)))
149              ((null eps))
150            (entries `((= ,n-supplied ,n)
151                       (%funcall ,(first eps) ,@(subseq temps 0 n)))))
152          `(lambda (,n-supplied ,@temps)
153             ;; FIXME: Make sure that INDEX type distinguishes between
154             ;; target and host. (Probably just make the SB!XC:DEFTYPE
155             ;; different from CL:DEFTYPE.)
156             (declare (type index ,n-supplied))
157             (cond
158              ,@(if more (butlast (entries)) (entries))
159              ,@(when more
160                  `((,(if (zerop min) t `(>= ,n-supplied ,max))
161                     ,(let ((n-context (gensym))
162                            (n-count (gensym)))
163                        `(multiple-value-bind (,n-context ,n-count)
164                             (%more-arg-context ,n-supplied ,max)
165                           (%funcall ,more ,@temps ,n-context ,n-count))))))
166              (t
167               (%argument-count-error ,n-supplied)))))))))
168
169 ;;; Make an external entry point (XEP) for FUN and return it. We
170 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
171 ;;; then associate this lambda with FUN as its XEP. After the
172 ;;; conversion, we iterate over the function's associated lambdas,
173 ;;; redoing local call analysis so that the XEP calls will get
174 ;;; converted. 
175 ;;;
176 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
177 ;;; discover an XEP after the initial local call analyze pass.
178 (defun make-external-entry-point (fun)
179   (declare (type functional fun))
180   (aver (not (functional-entry-fun fun)))
181   (with-belated-ir1-environment (lambda-bind (main-entry fun))
182     (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
183                                    :debug-name (debug-namify
184                                                 "XEP for ~A"
185                                                 (leaf-debug-name fun)))))
186       (setf (functional-kind res) :external
187             (leaf-ever-used res) t
188             (functional-entry-fun res) fun
189             (functional-entry-fun fun) res
190             (component-reanalyze *current-component*) t
191             (component-reoptimize *current-component*) t)
192       (etypecase fun
193         (clambda (locall-analyze-fun-1 fun))
194         (optional-dispatch
195          (dolist (ep (optional-dispatch-entry-points fun))
196            (locall-analyze-fun-1 ep))
197          (when (optional-dispatch-more-entry fun)
198            (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
199       res)))
200
201 ;;; Notice a REF that is not in a local-call context. If the REF is
202 ;;; already to an XEP, then do nothing, otherwise change it to the
203 ;;; XEP, making an XEP if necessary.
204 ;;;
205 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
206 ;;; it as though it was not an XEP reference (i.e. leave it alone).
207 (defun reference-entry-point (ref)
208   (declare (type ref ref))
209   (let ((fun (ref-leaf ref)))
210     (unless (or (external-entry-point-p fun)
211                 (member (functional-kind fun) '(:escape :cleanup)))
212       (change-ref-leaf ref (or (functional-entry-fun fun)
213                                (make-external-entry-point fun))))))
214 \f
215 ;;; Attempt to convert all references to FUN to local calls. The
216 ;;; reference must be the function for a call, and the function
217 ;;; continuation must be used only once, since otherwise we cannot be
218 ;;; sure what function is to be called. The call continuation would be
219 ;;; multiply used if there is hairy stuff such as conditionals in the
220 ;;; expression that computes the function.
221 ;;;
222 ;;; If we cannot convert a reference, then we mark the referenced
223 ;;; function as an entry-point, creating a new XEP if necessary. We
224 ;;; don't try to convert calls that are in error (:ERROR kind.)
225 ;;;
226 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
227 ;;; can force analysis of newly introduced calls. Note that we don't
228 ;;; do LET conversion here.
229 (defun locall-analyze-fun-1 (fun)
230   (declare (type functional fun))
231   (let ((refs (leaf-refs fun))
232         (first-time t))
233     (dolist (ref refs)
234       (let* ((cont (node-cont ref))
235              (dest (continuation-dest cont)))
236         (cond ((and (basic-combination-p dest)
237                     (eq (basic-combination-fun dest) cont)
238                     (eq (continuation-use cont) ref))
239
240                (convert-call-if-possible ref dest)
241
242                (unless (eq (basic-combination-kind dest) :local)
243                  (reference-entry-point ref)))
244               (t
245                (reference-entry-point ref))))
246       (setq first-time nil)))
247
248   (values))
249
250 ;;; We examine all NEW-FUNS in COMPONENT, attempting to convert calls
251 ;;; into local calls when it is legal. We also attempt to convert each
252 ;;; LAMBDA to a LET. LET conversion is also triggered by deletion of a
253 ;;; function reference, but functions that start out eligible for
254 ;;; conversion must be noticed sometime.
255 ;;;
256 ;;; Note that there is a lot of action going on behind the scenes
257 ;;; here, triggered by reference deletion. In particular, the
258 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
259 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
260 ;;; the COMPONENT-LAMBDAS when it is. Also, the COMPONENT-NEW-FUNS may
261 ;;; contain all sorts of drivel, since it is not updated when we
262 ;;; delete functions, etc. Only COMPONENT-LAMBDAS is updated.
263 ;;;
264 ;;; COMPONENT-REANALYZE-FUNS is treated similarly to
265 ;;; NEW-FUNS, but we don't add lambdas to the LAMBDAS.
266 (defun locall-analyze-component (component)
267   (declare (type component component))
268   (loop
269     (let* ((new-fun (pop (component-new-funs component)))
270            (fun (or new-fun (pop (component-reanalyze-funs component)))))
271       (unless fun (return))
272       (let ((kind (functional-kind fun)))
273         (cond ((member kind '(:deleted :let :mv-let :assignment)))
274               ((and (null (leaf-refs fun)) (eq kind nil)
275                     (not (functional-entry-fun fun)))
276                (delete-functional fun))
277               (t
278                ;; Fix/check FUN's relationship to COMPONENT-LAMDBAS.
279                (cond ((not (lambda-p fun))
280                       ;; Since FUN isn't a LAMBDA, this doesn't apply: no-op.
281                       (values))
282                      (new-fun ; FUN came from NEW-FUNS, hence is new.
283                       ;; FUN becomes part of COMPONENT-LAMBDAS now.
284                       (aver (not (member fun (component-lambdas component))))
285                       (push fun (component-lambdas component)))
286                      ((eql (lambda-inlinep fun) :inline)
287                       ;; FUNs marked :INLINE are sometimes in
288                       ;; COMPONENT-LAMBDAS and sometimes not. I (WHN
289                       ;; 2002-01-01) haven't figured this one out yet,
290                       ;; so don't assert anything.
291                       ;;
292                       ;; (One possibility: LAMBDAs to represent the
293                       ;; inline expansions of things which are defined
294                       ;; elsewhere might not be in COMPONENT-LAMBDAS,
295                       ;; which LAMBDAs to represent the inline
296                       ;; expansions of local functions might in
297                       ;; COMPONENT-LAMBDAS?)
298                       (values))
299                      (t ; FUN is old.
300                       ;; FUN should be in COMPONENT-LAMBDAS already.
301                       (aver (member fun (component-lambdas component)))))
302                (locall-analyze-fun-1 fun)
303                (when (lambda-p fun)
304                  (maybe-let-convert fun)))))))
305   (values))
306
307 (defun locall-analyze-clambdas-until-done (clambdas)
308   (loop
309    (let ((did-something nil))
310      (dolist (clambda clambdas)
311        (let* ((component (lambda-component clambda))
312               (*all-components* (list component)))
313          ;; The original CMU CL code seemed to implicitly assume that
314          ;; COMPONENT is the only one here. Let's make that explicit.
315          (aver (= 1 (length (functional-components clambda))))
316          (aver (eql component (first (functional-components clambda))))
317          (when (component-new-funs component)
318            (setf did-something t)
319            (locall-analyze-component component))))
320      (unless did-something
321        (return))))
322   (values))
323
324 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
325 ;;; to be in an infinite recursive loop, then change the reference to
326 ;;; reference a fresh copy. We return whichever function we decide to
327 ;;; reference.
328 (defun maybe-expand-local-inline (fun ref call)
329   (if (and (policy call
330                    (and (>= speed space) (>= speed compilation-speed)))
331            (not (eq (functional-kind (node-home-lambda call)) :external))
332            (inline-expansion-ok call))
333       (with-belated-ir1-environment call
334         (let* ((*lexenv* (functional-lexenv fun))
335                (won nil)
336                (res (catch 'local-call-lossage
337                       (prog1
338                           (ir1-convert-lambda
339                            (functional-inline-expansion fun)
340                            :debug-name (debug-namify "local inline ~A"
341                                                      (leaf-debug-name fun)))
342                         (setq won t)))))
343           (cond (won
344                  (change-ref-leaf ref res)
345                  res)
346                 (t
347                  (let ((*compiler-error-context* call))
348                    (compiler-note "couldn't inline expand because expansion ~
349                                    calls this LET-converted local function:~
350                                    ~%  ~S"
351                                   (leaf-debug-name res)))
352                  fun))))
353       fun))
354
355 ;;; Dispatch to the appropriate function to attempt to convert a call.
356 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
357 ;;; optimize as well as in local call analysis. If the call is is
358 ;;; already :LOCAL, we do nothing. If the call is already scheduled
359 ;;; for deletion, also do nothing (in addition to saving time, this
360 ;;; also avoids some problems with optimizing collections of functions
361 ;;; that are partially deleted.)
362 ;;;
363 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
364 ;;; called on a :INITIAL component, we don't care whether the caller
365 ;;; and callee are in the same component. Afterward, we must stick
366 ;;; with whatever component division we have chosen.
367 ;;;
368 ;;; Before attempting to convert a call, we see whether the function
369 ;;; is supposed to be inline expanded. Call conversion proceeds as
370 ;;; before after any expansion.
371 ;;;
372 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
373 ;;; warnings will get the right context.
374 (defun convert-call-if-possible (ref call)
375   (declare (type ref ref) (type basic-combination call))
376   (let* ((block (node-block call))
377          (component (block-component block))
378          (original-fun (ref-leaf ref)))
379     (aver (functional-p original-fun))
380     (unless (or (member (basic-combination-kind call) '(:local :error))
381                 (block-delete-p block)
382                 (eq (functional-kind (block-home-lambda block)) :deleted)
383                 (member (functional-kind original-fun)
384                         '(:toplevel-xep :deleted))
385                 (not (or (eq (component-kind component) :initial)
386                          (eq (block-component
387                               (node-block
388                                (lambda-bind (main-entry original-fun))))
389                              component))))
390       (let ((fun (if (external-entry-point-p original-fun)
391                      (functional-entry-fun original-fun)
392                      original-fun))
393             (*compiler-error-context* call))
394
395         (when (and (eq (functional-inlinep fun) :inline)
396                    (rest (leaf-refs original-fun)))
397           (setq fun (maybe-expand-local-inline fun ref call)))
398
399         (aver (member (functional-kind fun)
400                       '(nil :escape :cleanup :optional)))
401         (cond ((mv-combination-p call)
402                (convert-mv-call ref call fun))
403               ((lambda-p fun)
404                (convert-lambda-call ref call fun))
405               (t
406                (convert-hairy-call ref call fun))))))
407
408   (values))
409
410 ;;; Attempt to convert a multiple-value call. The only interesting
411 ;;; case is a call to a function that Looks-Like-An-MV-Bind, has
412 ;;; exactly one reference and no XEP, and is called with one values
413 ;;; continuation.
414 ;;;
415 ;;; We change the call to be to the last optional entry point and
416 ;;; change the call to be local. Due to our preconditions, the call
417 ;;; should eventually be converted to a let, but we can't do that now,
418 ;;; since there may be stray references to the e-p lambda due to
419 ;;; optional defaulting code.
420 ;;;
421 ;;; We also use variable types for the called function to construct an
422 ;;; assertion for the values continuation.
423 ;;;
424 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
425 (defun convert-mv-call (ref call fun)
426   (declare (type ref ref) (type mv-combination call) (type functional fun))
427   (when (and (looks-like-an-mv-bind fun)
428              (not (functional-entry-fun fun))
429              (= (length (leaf-refs fun)) 1)
430              (= (length (basic-combination-args call)) 1))
431     (let ((ep (car (last (optional-dispatch-entry-points fun)))))
432       (setf (basic-combination-kind call) :local)
433       (pushnew ep (lambda-calls-or-closes (node-home-lambda call)))
434       (merge-tail-sets call ep)
435       (change-ref-leaf ref ep)
436
437       (assert-continuation-type
438        (first (basic-combination-args call))
439        (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
440                          :rest *universal-type*))))
441   (values))
442
443 ;;; Attempt to convert a call to a lambda. If the number of args is
444 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
445 ;;; from future consideration. If the argcount is O.K. then we just
446 ;;; convert it.
447 (defun convert-lambda-call (ref call fun)
448   (declare (type ref ref) (type combination call) (type clambda fun))
449   (let ((nargs (length (lambda-vars fun)))
450         (call-args (length (combination-args call))))
451     (cond ((= call-args nargs)
452            (convert-call ref call fun))
453           (t
454            ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
455            ;; Compiler" that calling a function with "the wrong number of
456            ;; arguments" be only a STYLE-ERROR. I think, though, that this
457            ;; should only apply when the number of arguments is inferred
458            ;; from a previous definition. If the number of arguments
459            ;; is DECLAIMed, surely calling with the wrong number is a
460            ;; real WARNING. As long as SBCL continues to use CMU CL's
461            ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
462            ;; but as long as we continue to use that policy, that's the
463            ;; not our biggest problem.:-| When we fix that policy, this
464            ;; should come back into compliance. (So fix that policy!)
465            ;;   ..but..
466            ;; FIXME, continued: Except that section "3.2.2.3 Semantic
467            ;; Constraints" says that if it's within the same file, it's
468            ;; wrong. And we're in locall.lisp here, so it's probably
469            ;; (haven't checked this..) a call to something in the same
470            ;; file. So maybe it deserves a full warning anyway.
471            (compiler-warning
472             "function called with ~R argument~:P, but wants exactly ~R"
473             call-args nargs)
474            (setf (basic-combination-kind call) :error)))))
475 \f
476 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
477
478 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
479 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
480 ;;; a call to the correct entry point. If &KEY args are supplied, then
481 ;;; dispatch to a subfunction. We don't convert calls to functions
482 ;;; that have a &MORE (or &REST) arg.
483 (defun convert-hairy-call (ref call fun)
484   (declare (type ref ref) (type combination call)
485            (type optional-dispatch fun))
486   (let ((min-args (optional-dispatch-min-args fun))
487         (max-args (optional-dispatch-max-args fun))
488         (call-args (length (combination-args call))))
489     (cond ((< call-args min-args)
490            ;; FIXME: See FIXME note at the previous
491            ;; wrong-number-of-arguments warnings in this file.
492            (compiler-warning
493             "function called with ~R argument~:P, but wants at least ~R"
494             call-args min-args)
495            (setf (basic-combination-kind call) :error))
496           ((<= call-args max-args)
497            (convert-call ref call
498                          (elt (optional-dispatch-entry-points fun)
499                               (- call-args min-args))))
500           ((optional-dispatch-more-entry fun)
501            (convert-more-call ref call fun))
502           (t
503            ;; FIXME: See FIXME note at the previous
504            ;; wrong-number-of-arguments warnings in this file.
505            (compiler-warning
506             "function called with ~R argument~:P, but wants at most ~R"
507             call-args max-args)
508            (setf (basic-combination-kind call) :error))))
509   (values))
510
511 ;;; This function is used to convert a call to an entry point when
512 ;;; complex transformations need to be done on the original arguments.
513 ;;; ENTRY is the entry point function that we are calling. VARS is a
514 ;;; list of variable names which are bound to the original call
515 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
516 ;;; is the list of arguments to the entry point function.
517 ;;;
518 ;;; In order to avoid gruesome graph grovelling, we introduce a new
519 ;;; function that rearranges the arguments and calls the entry point.
520 ;;; We analyze the new function and the entry point immediately so
521 ;;; that everything gets converted during the single pass.
522 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
523   (declare (list vars ignores args) (type ref ref) (type combination call)
524            (type clambda entry))
525   (let ((new-fun
526          (with-belated-ir1-environment call
527            (ir1-convert-lambda
528             `(lambda ,vars
529                (declare (ignorable . ,ignores))
530                (%funcall ,entry . ,args))
531             :debug-name (debug-namify "hairy fun entry ~S"
532                                       (continuation-fun-name
533                                        (basic-combination-fun call)))))))
534     (convert-call ref call new-fun)
535     (dolist (ref (leaf-refs entry))
536       (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
537
538 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
539 ;;; function into a local call to the MAIN-ENTRY.
540 ;;;
541 ;;; First we verify that all keywords are constant and legal. If there
542 ;;; aren't, then we warn the user and don't attempt to convert the call.
543 ;;;
544 ;;; We massage the supplied &KEY arguments into the order expected
545 ;;; by the main entry. This is done by binding all the arguments to
546 ;;; the keyword call to variables in the introduced lambda, then
547 ;;; passing these values variables in the correct order when calling
548 ;;; the main entry. Unused arguments (such as the keywords themselves)
549 ;;; are discarded simply by not passing them along.
550 ;;;
551 ;;; If there is a &REST arg, then we bundle up the args and pass them
552 ;;; to LIST.
553 (defun convert-more-call (ref call fun)
554   (declare (type ref ref) (type combination call) (type optional-dispatch fun))
555   (let* ((max (optional-dispatch-max-args fun))
556          (arglist (optional-dispatch-arglist fun))
557          (args (combination-args call))
558          (more (nthcdr max args))
559          (flame (policy call (or (> speed inhibit-warnings)
560                                  (> space inhibit-warnings))))
561          (loser nil)
562          (temps (make-gensym-list max))
563          (more-temps (make-gensym-list (length more))))
564     (collect ((ignores)
565               (supplied)
566               (key-vars))
567
568       (dolist (var arglist)
569         (let ((info (lambda-var-arg-info var)))
570           (when info
571             (ecase (arg-info-kind info)
572               (:keyword
573                (key-vars var))
574               ((:rest :optional))
575               ((:more-context :more-count)
576                (compiler-warning "can't local-call functions with &MORE args")
577                (setf (basic-combination-kind call) :error)
578                (return-from convert-more-call))))))
579
580       (when (optional-dispatch-keyp fun)
581         (when (oddp (length more))
582           (compiler-warning "function called with odd number of ~
583                              arguments in keyword portion")
584
585           (setf (basic-combination-kind call) :error)
586           (return-from convert-more-call))
587
588         (do ((key more (cddr key))
589              (temp more-temps (cddr temp)))
590             ((null key))
591           (let ((cont (first key)))
592             (unless (constant-continuation-p cont)
593               (when flame
594                 (compiler-note "non-constant keyword in keyword call"))
595               (setf (basic-combination-kind call) :error)
596               (return-from convert-more-call))
597
598             (let ((name (continuation-value cont))
599                   (dummy (first temp))
600                   (val (second temp)))
601               (dolist (var (key-vars)
602                            (progn
603                              (ignores dummy val)
604                              (setq loser name)))
605                 (let ((info (lambda-var-arg-info var)))
606                   (when (eq (arg-info-key info) name)
607                     (ignores dummy)
608                     (supplied (cons var val))
609                     (return)))))))
610
611         (when (and loser (not (optional-dispatch-allowp fun)))
612           (compiler-warning "function called with unknown argument keyword ~S"
613                             loser)
614           (setf (basic-combination-kind call) :error)
615           (return-from convert-more-call)))
616
617       (collect ((call-args))
618         (do ((var arglist (cdr var))
619              (temp temps (cdr temp)))
620             (())
621           (let ((info (lambda-var-arg-info (car var))))
622             (if info
623                 (ecase (arg-info-kind info)
624                   (:optional
625                    (call-args (car temp))
626                    (when (arg-info-supplied-p info)
627                      (call-args t)))
628                   (:rest
629                    (call-args `(list ,@more-temps))
630                    (return))
631                   (:keyword
632                    (return)))
633                 (call-args (car temp)))))
634
635         (dolist (var (key-vars))
636           (let ((info (lambda-var-arg-info var))
637                 (temp (cdr (assoc var (supplied)))))
638             (if temp
639                 (call-args temp)
640                 (call-args (arg-info-default info)))
641             (when (arg-info-supplied-p info)
642               (call-args (not (null temp))))))
643
644         (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
645                                  (append temps more-temps)
646                                  (ignores) (call-args)))))
647
648   (values))
649 \f
650 ;;;; LET conversion
651 ;;;;
652 ;;;; Converting to a LET has differing significance to various parts
653 ;;;; of the compiler:
654 ;;;; -- The body of a LET is spliced in immediately after the
655 ;;;;    corresponding combination node, making the control transfer
656 ;;;;    explicit and allowing LETs to be mashed together into a single
657 ;;;;    block. The value of the LET is delivered directly to the
658 ;;;;    original continuation for the call, eliminating the need to
659 ;;;;    propagate information from the dummy result continuation.
660 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
661 ;;;;    that there is only one expression that the variable can be bound
662 ;;;;    to, and this is easily substituted for.
663 ;;;; -- LETs are interesting to environment analysis and to the back
664 ;;;;    end because in most ways a LET can be considered to be "the
665 ;;;;    same function" as its home function.
666 ;;;; -- LET conversion has dynamic scope implications, since control
667 ;;;;    transfers within the same environment are local. In a local
668 ;;;;    control transfer, cleanup code must be emitted to remove
669 ;;;;    dynamic bindings that are no longer in effect.
670
671 ;;; Set up the control transfer to the called CLAMBDA. We split the
672 ;;; call block immediately after the call, and link the head of
673 ;;; CLAMBDA to the call block. The successor block after splitting
674 ;;; (where we return to) is returned.
675 ;;;
676 ;;; If the lambda is is a different component than the call, then we
677 ;;; call JOIN-COMPONENTS. This only happens in block compilation
678 ;;; before FIND-INITIAL-DFO.
679 (defun insert-let-body (clambda call)
680   (declare (type clambda clambda) (type basic-combination call))
681   (let* ((call-block (node-block call))
682          (bind-block (node-block (lambda-bind clambda)))
683          (component (block-component call-block)))
684     (let ((clambda-component (block-component bind-block)))
685       (unless (eq clambda-component component)
686         (aver (eq (component-kind component) :initial))
687         (join-components component clambda-component)))
688
689     (let ((*current-component* component))
690       (node-ends-block call))
691     ;; FIXME: Use PROPER-LIST-OF-LENGTH-P here, and look for other
692     ;; uses of '=.*length' which could also be converted to use
693     ;; PROPER-LIST-OF-LENGTH-P.
694     (aver (= (length (block-succ call-block)) 1))
695     (let ((next-block (first (block-succ call-block))))
696       (unlink-blocks call-block next-block)
697       (link-blocks call-block bind-block)
698       next-block)))
699
700 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
701 ;;; same set as; but leave CLAMBDA with a valid tail set value of
702 ;;; its own, for the benefit of code which might try to pull
703 ;;; something out of it (e.g. return type).
704 (defun depart-from-tail-set (clambda)
705   ;; Until sbcl-0.pre7.37.flaky5.2, we did
706   ;;   (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
707   ;;     (SETF (TAIL-SET-FUNS TAILS)
708   ;;           (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
709   ;;   (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
710   ;; here. Apparently the idea behind the (SETF .. NIL) was that since
711   ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
712   ;; inconsistent, and perhaps unsafe, for us to think we're in the
713   ;; tail set. Unfortunately..
714   ;;
715   ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
716   ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
717   ;; (instead of only being able to emit funny little :TOPLEVEL stubs
718   ;; which you called in order to get the address of an external LAMBDA):
719   ;; the external function was defined in terms of internal function,
720   ;; which was LET-converted, and then things blew up downstream when
721   ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
722   ;; the now-NILed-out TAIL-SET. So..
723   ;;
724   ;; To deal with this problem, we no longer NIL out 
725   ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
726   ;;   * If we're the only function in TAIL-SET-FUNS, it should
727   ;;     be safe to leave ourself linked to it, and it to you.
728   ;;   * If there are other functions in TAIL-SET-FUNS, then we're
729   ;;     afraid of future optimizations on those functions causing
730   ;;     the TAIL-SET object no longer to be valid to describe our
731   ;;     return value. Thus, we delete ourselves from that object;
732   ;;     but we save a newly-allocated tail-set, derived from the old
733   ;;     one, for ourselves, for the use of later code (e.g.
734   ;;     FINALIZE-XEP-DEFINITION) which might want to
735   ;;     know about our return type.
736   (let* ((old-tail-set (lambda-tail-set clambda))
737          (old-tail-set-funs (tail-set-funs old-tail-set)))
738     (unless (= 1 (length old-tail-set-funs))
739       (setf (tail-set-funs old-tail-set)
740             (delete clambda old-tail-set-funs))
741       (let ((new-tail-set (copy-tail-set old-tail-set)))
742         (setf (lambda-tail-set clambda) new-tail-set
743               (tail-set-funs new-tail-set) (list clambda)))))
744   ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
745   ;; remain valid in this case, so we nuke it on the theory that
746   ;; missing information tends to be less dangerous than incorrect
747   ;; information.
748   (setf (tail-set-info (lambda-tail-set clambda)) nil))
749
750 ;;; Handle the environment semantics of LET conversion. We add CLAMBDA
751 ;;; and its LETs to LETs for the CALL's home function. We merge the
752 ;;; calls for CLAMBDA with the calls for the home function, removing
753 ;;; CLAMBDA in the process. We also merge the ENTRIES.
754 ;;;
755 ;;; We also unlink the function head from the component head and set
756 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
757 ;;; recomputed.
758 (defun merge-lets (clambda call)
759
760   (declare (type clambda clambda) (type basic-combination call))
761
762   (let ((component (block-component (node-block call))))
763     (unlink-blocks (component-head component) (lambda-block clambda))
764     (setf (component-lambdas component)
765           (delete clambda (component-lambdas component)))
766     (setf (component-reanalyze component) t))
767   (setf (lambda-call-lexenv clambda) (node-lexenv call))
768
769   (depart-from-tail-set clambda)
770
771   (let* ((home (node-home-lambda call))
772          (home-env (lambda-physenv home)))
773
774     ;; CLAMBDA belongs to HOME now.
775     (push clambda (lambda-lets home))
776     (setf (lambda-home clambda) home)
777     (setf (lambda-physenv clambda) home-env)
778
779     ;; All of CLAMBDA's LETs belong to HOME now.
780     (let ((lets (lambda-lets clambda)))
781       (dolist (let lets)
782         (setf (lambda-home let) home)
783         (setf (lambda-physenv let) home-env))
784       (setf (lambda-lets home) (nconc lets (lambda-lets home))))
785     ;; CLAMBDA no longer has an independent existence as an entity
786     ;; which has LETs.
787     (setf (lambda-lets clambda) nil)
788
789     ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
790     ;; DFO dependencies.
791     (setf (lambda-calls-or-closes home)
792           (delete clambda
793                   (nunion (lambda-calls-or-closes clambda)
794                           (lambda-calls-or-closes home))))
795     ;; CLAMBDA no longer has an independent existence as an entity
796     ;; which calls things or has DFO dependencies.
797     (setf (lambda-calls-or-closes clambda) nil)
798
799     ;; All of CLAMBDA's ENTRIES belong to HOME now.
800     (setf (lambda-entries home)
801           (nconc (lambda-entries clambda)
802                  (lambda-entries home)))
803     ;; CLAMBDA no longer has an independent existence as an entity
804     ;; with ENTRIES.
805     (setf (lambda-entries clambda) nil))
806
807   (values))
808
809 ;;; Handle the value semantics of LET conversion. Delete FUN's return
810 ;;; node, and change the control flow to transfer to NEXT-BLOCK
811 ;;; instead. Move all the uses of the result continuation to CALL's
812 ;;; CONT.
813 ;;;
814 ;;; If the actual continuation is only used by the LET call, then we
815 ;;; intersect the type assertion on the dummy continuation with the
816 ;;; assertion for the actual continuation; in all other cases
817 ;;; assertions on the dummy continuation are lost.
818 ;;;
819 ;;; We also intersect the derived type of the CALL with the derived
820 ;;; type of all the dummy continuation's uses. This serves mainly to
821 ;;; propagate TRULY-THE through LETs.
822 (defun move-return-uses (fun call next-block)
823   (declare (type clambda fun) (type basic-combination call)
824            (type cblock next-block))
825   (let* ((return (lambda-return fun))
826          (return-block (node-block return)))
827     (unlink-blocks return-block
828                    (component-tail (block-component return-block)))
829     (link-blocks return-block next-block)
830     (unlink-node return)
831     (delete-return return)
832     (let ((result (return-result return))
833           (cont (node-cont call))
834           (call-type (node-derived-type call)))
835       (when (eq (continuation-use cont) call)
836         (assert-continuation-type cont (continuation-asserted-type result)))
837       (unless (eq call-type *wild-type*)
838         (do-uses (use result)
839           (derive-node-type use call-type)))
840       (substitute-continuation-uses cont result)))
841   (values))
842
843 ;;; Change all CONT for all the calls to FUN to be the start
844 ;;; continuation for the bind node. This allows the blocks to be
845 ;;; joined if the caller count ever goes to one.
846 (defun move-let-call-cont (fun)
847   (declare (type clambda fun))
848   (let ((new-cont (node-prev (lambda-bind fun))))
849     (dolist (ref (leaf-refs fun))
850       (let ((dest (continuation-dest (node-cont ref))))
851         (delete-continuation-use dest)
852         (add-continuation-use dest new-cont))))
853   (values))
854
855 ;;; We are converting FUN to be a LET when the call is in a non-tail
856 ;;; position. Any previously tail calls in FUN are no longer tail
857 ;;; calls, and must be restored to normal calls which transfer to
858 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
859 ;;; the RETURN-RESULT, because the return might have been deleted (if
860 ;;; all calls were TR.)
861 (defun unconvert-tail-calls (fun call next-block)
862   (dolist (called (lambda-calls-or-closes fun))
863     (when (lambda-p called)
864       (dolist (ref (leaf-refs called))
865         (let ((this-call (continuation-dest (node-cont ref))))
866           (when (and this-call
867                      (node-tail-p this-call)
868                      (eq (node-home-lambda this-call) fun))
869             (setf (node-tail-p this-call) nil)
870             (ecase (functional-kind called)
871               ((nil :cleanup :optional)
872                (let ((block (node-block this-call))
873                      (cont (node-cont call)))
874                  (ensure-block-start cont)
875                  (unlink-blocks block (first (block-succ block)))
876                  (link-blocks block next-block)
877                  (delete-continuation-use this-call)
878                  (add-continuation-use this-call cont)))
879               (:deleted)
880               ;; The called function might be an assignment in the
881               ;; case where we are currently converting that function.
882               ;; In steady-state, assignments never appear as a called
883               ;; function.
884               (:assignment
885                (aver (eq called fun)))))))))
886   (values))
887
888 ;;; Deal with returning from a LET or assignment that we are
889 ;;; converting. FUN is the function we are calling, CALL is a call to
890 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
891 ;;; NULL if call is a tail call.
892 ;;;
893 ;;; If the call is not a tail call, then we must do
894 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
895 ;;; its value out of the enclosing non-let function. When call is
896 ;;; non-TR, we must convert it back to an ordinary local call, since
897 ;;; the value must be delivered to the receiver of CALL's value.
898 ;;;
899 ;;; We do different things depending on whether the caller and callee
900 ;;; have returns left:
901
902 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
903 ;;;    Either the function doesn't return, or all returns are via
904 ;;;    tail-recursive local calls.
905 ;;; -- If CALL is a non-tail call, or if both have returns, then
906 ;;;    we delete the callee's return, move its uses to the call's
907 ;;;    result continuation, and transfer control to the appropriate
908 ;;;    return point.
909 ;;; -- If the callee has a return, but the caller doesn't, then we
910 ;;;    move the return to the caller.
911 (defun move-return-stuff (fun call next-block)
912   (declare (type clambda fun) (type basic-combination call)
913            (type (or cblock null) next-block))
914   (when next-block
915     (unconvert-tail-calls fun call next-block))
916   (let* ((return (lambda-return fun))
917          (call-fun (node-home-lambda call))
918          (call-return (lambda-return call-fun)))
919     (cond ((not return))
920           ((or next-block call-return)
921            (unless (block-delete-p (node-block return))
922              (move-return-uses fun call
923                                (or next-block (node-block call-return)))))
924           (t
925            (aver (node-tail-p call))
926            (setf (lambda-return call-fun) return)
927            (setf (return-lambda return) call-fun))))
928   (move-let-call-cont fun)
929   (values))
930
931 ;;; Actually do LET conversion. We call subfunctions to do most of the
932 ;;; work. We change the CALL's CONT to be the continuation heading the
933 ;;; BIND block, and also do REOPTIMIZE-CONTINUATION on the args and
934 ;;; CONT so that LET-specific IR1 optimizations get a chance. We blow
935 ;;; away any entry for the function in *FREE-FUNCTIONS* so that nobody
936 ;;; will create new references to it.
937 (defun let-convert (fun call)
938   (declare (type clambda fun) (type basic-combination call))
939   (let ((next-block (if (node-tail-p call)
940                         nil
941                         (insert-let-body fun call))))
942     (move-return-stuff fun call next-block)
943     (merge-lets fun call)))
944
945 ;;; Reoptimize all of CALL's args and its result.
946 (defun reoptimize-call (call)
947   (declare (type basic-combination call))
948   (dolist (arg (basic-combination-args call))
949     (when arg
950       (reoptimize-continuation arg)))
951   (reoptimize-continuation (node-cont call))
952   (values))
953
954 ;;; We also don't convert calls to named functions which appear in the
955 ;;; initial component, delaying this until optimization. This
956 ;;; minimizes the likelihood that we will LET-convert a function which
957 ;;; may have references added due to later local inline expansion.
958 (defun ok-initial-convert-p (fun)
959   (not (and (leaf-has-source-name-p fun)
960             (eq (component-kind (lambda-component fun))
961                 :initial))))
962
963 ;;; This function is called when there is some reason to believe that
964 ;;; CLAMBDA might be converted into a LET. This is done after local
965 ;;; call analysis, and also when a reference is deleted. We only
966 ;;; convert to a let when the function is a normal local function, has
967 ;;; no XEP, and is referenced in exactly one local call. Conversion is
968 ;;; also inhibited if the only reference is in a block about to be
969 ;;; deleted. We return true if we converted.
970 ;;;
971 ;;; These rules may seem unnecessarily restrictive, since there are
972 ;;; some cases where we could do the return with a jump that don't
973 ;;; satisfy these requirements. The reason for doing things this way
974 ;;; is that it makes the concept of a LET much more useful at the
975 ;;; level of IR1 semantics. The :ASSIGNMENT function kind provides
976 ;;; another way to optimize calls to single-return/multiple call
977 ;;; functions.
978 ;;;
979 ;;; We don't attempt to convert calls to functions that have an XEP,
980 ;;; since we might be embarrassed later when we want to convert a
981 ;;; newly discovered local call. Also, see OK-INITIAL-CONVERT-P.
982 (defun maybe-let-convert (clambda)
983   (declare (type clambda clambda))
984   (let ((refs (leaf-refs clambda)))
985     (when (and refs
986                (null (rest refs))
987                (member (functional-kind clambda) '(nil :assignment))
988                (not (functional-entry-fun clambda)))
989       (let* ((ref-cont (node-cont (first refs)))
990              (dest (continuation-dest ref-cont)))
991         (when (and dest
992                    (basic-combination-p dest)
993                    (eq (basic-combination-fun dest) ref-cont)
994                    (eq (basic-combination-kind dest) :local)
995                    (not (block-delete-p (node-block dest)))
996                    (cond ((ok-initial-convert-p clambda) t)
997                          (t
998                           (reoptimize-continuation ref-cont)
999                           nil)))
1000           (unless (eq (functional-kind clambda) :assignment)
1001             (let-convert clambda dest))
1002           (reoptimize-call dest)
1003           (setf (functional-kind clambda)
1004                 (if (mv-combination-p dest) :mv-let :let))))
1005       t)))
1006 \f
1007 ;;;; tail local calls and assignments
1008
1009 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1010 ;;; they definitely won't generate any cleanup code. Currently we
1011 ;;; recognize lexical entry points that are only used locally (if at
1012 ;;; all).
1013 (defun only-harmless-cleanups (block1 block2)
1014   (declare (type cblock block1 block2))
1015   (or (eq block1 block2)
1016       (let ((cleanup2 (block-start-cleanup block2)))
1017         (do ((cleanup (block-end-cleanup block1)
1018                       (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1019             ((eq cleanup cleanup2) t)
1020           (case (cleanup-kind cleanup)
1021             ((:block :tagbody)
1022              (unless (null (entry-exits (cleanup-mess-up cleanup)))
1023                (return nil)))
1024             (t (return nil)))))))
1025
1026 ;;; If a potentially TR local call really is TR, then convert it to
1027 ;;; jump directly to the called function. We also call
1028 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1029 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
1030 ;;; the succesor (potentially deleting the RETURN node) unless:
1031 ;;; -- The call has already been converted.
1032 ;;; -- The call isn't TR (some implicit MV PROG1.)
1033 ;;; -- The call is in an XEP (thus we might decide to make it non-tail 
1034 ;;;    so that we can use known return inside the component.)
1035 ;;; -- There is a change in the cleanup between the call in the return, 
1036 ;;;    so we might need to introduce cleanup code.
1037 (defun maybe-convert-tail-local-call (call)
1038   (declare (type combination call))
1039   (let ((return (continuation-dest (node-cont call))))
1040     (aver (return-p return))
1041     (when (and (not (node-tail-p call))
1042                (immediately-used-p (return-result return) call)
1043                (not (eq (functional-kind (node-home-lambda call))
1044                         :external))
1045                (only-harmless-cleanups (node-block call)
1046                                        (node-block return)))
1047       (node-ends-block call)
1048       (let ((block (node-block call))
1049             (fun (combination-lambda call)))
1050         (setf (node-tail-p call) t)
1051         (unlink-blocks block (first (block-succ block)))
1052         (link-blocks block (lambda-block fun))
1053         (values t (maybe-convert-to-assignment fun))))))
1054
1055 ;;; This is called when we believe it might make sense to convert Fun
1056 ;;; to an assignment. All this function really does is determine when
1057 ;;; a function with more than one call can still be combined with the
1058 ;;; calling function's environment. We can convert when:
1059 ;;; -- The function is a normal, non-entry function, and
1060 ;;; -- Except for one call, all calls must be tail recursive calls 
1061 ;;;    in the called function (i.e. are self-recursive tail calls)
1062 ;;; -- OK-INITIAL-CONVERT-P is true.
1063 ;;;
1064 ;;; There may be one outside call, and it need not be tail-recursive.
1065 ;;; Since all tail local calls have already been converted to direct
1066 ;;; transfers, the only control semantics needed are to splice in the
1067 ;;; body at the non-tail call. If there is no non-tail call, then we
1068 ;;; need only merge the environments. Both cases are handled by
1069 ;;; LET-CONVERT.
1070 ;;;
1071 ;;; ### It would actually be possible to allow any number of outside
1072 ;;; calls as long as they all return to the same place (i.e. have the
1073 ;;; same conceptual continuation.) A special case of this would be
1074 ;;; when all of the outside calls are tail recursive.
1075 (defun maybe-convert-to-assignment (fun)
1076   (declare (type clambda fun))
1077   (when (and (not (functional-kind fun))
1078              (not (functional-entry-fun fun)))
1079     (let ((non-tail nil)
1080           (call-fun nil))
1081       (when (and (dolist (ref (leaf-refs fun) t)
1082                    (let ((dest (continuation-dest (node-cont ref))))
1083                      (when (or (not dest)
1084                                (block-delete-p (node-block dest)))
1085                        (return nil))
1086                      (let ((home (node-home-lambda ref)))
1087                        (unless (eq home fun)
1088                          (when call-fun (return nil))
1089                          (setq call-fun home))
1090                        (unless (node-tail-p dest)
1091                          (when (or non-tail (eq home fun)) (return nil))
1092                          (setq non-tail dest)))))
1093                  (ok-initial-convert-p fun))
1094         (setf (functional-kind fun) :assignment)
1095         (let-convert fun (or non-tail
1096                              (continuation-dest
1097                               (node-cont (first (leaf-refs fun))))))
1098         (when non-tail (reoptimize-call non-tail))
1099         t))))