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