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