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