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