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