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-or-closes (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-or-closes (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 :debug-name (debug-namify "hairy fun entry ~S"
519 (continuation-fun-name
520 (basic-combination-fun call)))))))
521 (convert-call ref call new-fun)
522 (dolist (ref (leaf-refs entry))
523 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
525 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
526 ;;; function into a local call to the MAIN-ENTRY.
528 ;;; First we verify that all keywords are constant and legal. If there
529 ;;; aren't, then we warn the user and don't attempt to convert the call.
531 ;;; We massage the supplied &KEY arguments into the order expected
532 ;;; by the main entry. This is done by binding all the arguments to
533 ;;; the keyword call to variables in the introduced lambda, then
534 ;;; passing these values variables in the correct order when calling
535 ;;; the main entry. Unused arguments (such as the keywords themselves)
536 ;;; are discarded simply by not passing them along.
538 ;;; If there is a &REST arg, then we bundle up the args and pass them
540 (defun convert-more-call (ref call fun)
541 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
542 (let* ((max (optional-dispatch-max-args fun))
543 (arglist (optional-dispatch-arglist fun))
544 (args (combination-args call))
545 (more (nthcdr max args))
546 (flame (policy call (or (> speed inhibit-warnings)
547 (> space inhibit-warnings))))
549 (temps (make-gensym-list max))
550 (more-temps (make-gensym-list (length more))))
555 (dolist (var arglist)
556 (let ((info (lambda-var-arg-info var)))
558 (ecase (arg-info-kind info)
562 ((:more-context :more-count)
563 (compiler-warning "can't local-call functions with &MORE args")
564 (setf (basic-combination-kind call) :error)
565 (return-from convert-more-call))))))
567 (when (optional-dispatch-keyp fun)
568 (when (oddp (length more))
569 (compiler-warning "function called with odd number of ~
570 arguments in keyword portion")
572 (setf (basic-combination-kind call) :error)
573 (return-from convert-more-call))
575 (do ((key more (cddr key))
576 (temp more-temps (cddr temp)))
578 (let ((cont (first key)))
579 (unless (constant-continuation-p cont)
581 (compiler-note "non-constant keyword in keyword call"))
582 (setf (basic-combination-kind call) :error)
583 (return-from convert-more-call))
585 (let ((name (continuation-value cont))
588 (dolist (var (key-vars)
592 (let ((info (lambda-var-arg-info var)))
593 (when (eq (arg-info-key info) name)
595 (supplied (cons var val))
598 (when (and loser (not (optional-dispatch-allowp fun)))
599 (compiler-warning "function called with unknown argument keyword ~S"
601 (setf (basic-combination-kind call) :error)
602 (return-from convert-more-call)))
604 (collect ((call-args))
605 (do ((var arglist (cdr var))
606 (temp temps (cdr temp)))
608 (let ((info (lambda-var-arg-info (car var))))
610 (ecase (arg-info-kind info)
612 (call-args (car temp))
613 (when (arg-info-supplied-p info)
616 (call-args `(list ,@more-temps))
620 (call-args (car temp)))))
622 (dolist (var (key-vars))
623 (let ((info (lambda-var-arg-info var))
624 (temp (cdr (assoc var (supplied)))))
627 (call-args (arg-info-default info)))
628 (when (arg-info-supplied-p info)
629 (call-args (not (null temp))))))
631 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
632 (append temps more-temps)
633 (ignores) (call-args)))))
639 ;;;; Converting to a LET has differing significance to various parts
640 ;;;; of the compiler:
641 ;;;; -- The body of a LET is spliced in immediately after the
642 ;;;; corresponding combination node, making the control transfer
643 ;;;; explicit and allowing LETs to be mashed together into a single
644 ;;;; block. The value of the LET is delivered directly to the
645 ;;;; original continuation for the call, eliminating the need to
646 ;;;; propagate information from the dummy result continuation.
647 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
648 ;;;; that there is only one expression that the variable can be bound
649 ;;;; to, and this is easily substituted for.
650 ;;;; -- LETs are interesting to environment analysis and to the back
651 ;;;; end because in most ways a LET can be considered to be "the
652 ;;;; same function" as its home function.
653 ;;;; -- LET conversion has dynamic scope implications, since control
654 ;;;; transfers within the same environment are local. In a local
655 ;;;; control transfer, cleanup code must be emitted to remove
656 ;;;; dynamic bindings that are no longer in effect.
658 ;;; Set up the control transfer to the called CLAMBDA. We split the
659 ;;; call block immediately after the call, and link the head of
660 ;;; CLAMBDA to the call block. The successor block after splitting
661 ;;; (where we return to) is returned.
663 ;;; If the lambda is is a different component than the call, then we
664 ;;; call JOIN-COMPONENTS. This only happens in block compilation
665 ;;; before FIND-INITIAL-DFO.
666 (defun insert-let-body (clambda call)
667 (declare (type clambda clambda) (type basic-combination call))
668 (let* ((call-block (node-block call))
669 (bind-block (node-block (lambda-bind clambda)))
670 (component (block-component call-block)))
671 (let ((clambda-component (block-component bind-block)))
672 (unless (eq clambda-component component)
673 (aver (eq (component-kind component) :initial))
674 (join-components component clambda-component)))
676 (let ((*current-component* component))
677 (node-ends-block call))
678 ;; FIXME: Use PROPER-LIST-OF-LENGTH-P here, and look for other
679 ;; uses of '=.*length' which could also be converted to use
680 ;; PROPER-LIST-OF-LENGTH-P.
681 (aver (= (length (block-succ call-block)) 1))
682 (let ((next-block (first (block-succ call-block))))
683 (unlink-blocks call-block next-block)
684 (link-blocks call-block bind-block)
687 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
688 ;;; same set as; but leave CLAMBDA with a valid tail set value of
689 ;;; its own, for the benefit of code which might try to pull
690 ;;; something out of it (e.g. return type).
691 (defun depart-from-tail-set (clambda)
692 ;; Until sbcl-0.pre7.37.flaky5.2, we did
693 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
694 ;; (SETF (TAIL-SET-FUNS TAILS)
695 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
696 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
697 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
698 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
699 ;; inconsistent, and perhaps unsafe, for us to think we're in the
700 ;; tail set. Unfortunately..
702 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
703 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
704 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
705 ;; which you called in order to get the address of an external LAMBDA):
706 ;; the external function was defined in terms of internal function,
707 ;; which was LET-converted, and then things blew up downstream when
708 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
709 ;; the now-NILed-out TAIL-SET. So..
711 ;; To deal with this problem, we no longer NIL out
712 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
713 ;; * If we're the only function in TAIL-SET-FUNS, it should
714 ;; be safe to leave ourself linked to it, and it to you.
715 ;; * If there are other functions in TAIL-SET-FUNS, then we're
716 ;; afraid of future optimizations on those functions causing
717 ;; the TAIL-SET object no longer to be valid to describe our
718 ;; return value. Thus, we delete ourselves from that object;
719 ;; but we save a newly-allocated tail-set, derived from the old
720 ;; one, for ourselves, for the use of later code (e.g.
721 ;; FINALIZE-XEP-DEFINITION) which might want to
722 ;; know about our return type.
723 (let* ((old-tail-set (lambda-tail-set clambda))
724 (old-tail-set-funs (tail-set-funs old-tail-set)))
725 (unless (= 1 (length old-tail-set-funs))
726 (setf (tail-set-funs old-tail-set)
727 (delete clambda old-tail-set-funs))
728 (let ((new-tail-set (copy-tail-set old-tail-set)))
729 (setf (lambda-tail-set clambda) new-tail-set
730 (tail-set-funs new-tail-set) (list clambda)))))
731 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
732 ;; remain valid in this case, so we nuke it on the theory that
733 ;; missing information tends to be less dangerous than incorrect
735 (setf (tail-set-info (lambda-tail-set clambda)) nil))
737 ;;; Handle the environment semantics of LET conversion. We add CLAMBDA
738 ;;; and its LETs to LETs for the CALL's home function. We merge the
739 ;;; calls for CLAMBDA with the calls for the home function, removing
740 ;;; CLAMBDA in the process. We also merge the ENTRIES.
742 ;;; We also unlink the function head from the component head and set
743 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
745 (defun merge-lets (clambda call)
747 (declare (type clambda clambda) (type basic-combination call))
749 (let ((component (block-component (node-block call))))
750 (unlink-blocks (component-head component) (lambda-block clambda))
751 (setf (component-lambdas component)
752 (delete clambda (component-lambdas component)))
753 (setf (component-reanalyze component) t))
754 (setf (lambda-call-lexenv clambda) (node-lexenv call))
756 (depart-from-tail-set clambda)
758 (let* ((home (node-home-lambda call))
759 (home-env (lambda-physenv home)))
761 ;; CLAMBDA belongs to HOME now.
762 (push clambda (lambda-lets home))
763 (setf (lambda-home clambda) home)
764 (setf (lambda-physenv clambda) home-env)
766 ;; All of CLAMBDA's LETs belong to HOME now.
767 (let ((lets (lambda-lets clambda)))
769 (setf (lambda-home let) home)
770 (setf (lambda-physenv let) home-env))
771 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
772 ;; CLAMBDA no longer has an independent existence as an entity
774 (setf (lambda-lets clambda) nil)
776 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
778 (setf (lambda-calls-or-closes home)
780 (nunion (lambda-calls-or-closes clambda)
781 (lambda-calls-or-closes home))))
782 ;; CLAMBDA no longer has an independent existence as an entity
783 ;; which calls things or has DFO dependencies.
784 (setf (lambda-calls-or-closes clambda) nil)
786 ;; All of CLAMBDA's ENTRIES belong to HOME now.
787 (setf (lambda-entries home)
788 (nconc (lambda-entries clambda)
789 (lambda-entries home)))
790 ;; CLAMBDA no longer has an independent existence as an entity
792 (setf (lambda-entries clambda) nil))
796 ;;; Handle the value semantics of LET conversion. Delete FUN's return
797 ;;; node, and change the control flow to transfer to NEXT-BLOCK
798 ;;; instead. Move all the uses of the result continuation to CALL's
801 ;;; If the actual continuation is only used by the LET call, then we
802 ;;; intersect the type assertion on the dummy continuation with the
803 ;;; assertion for the actual continuation; in all other cases
804 ;;; assertions on the dummy continuation are lost.
806 ;;; We also intersect the derived type of the CALL with the derived
807 ;;; type of all the dummy continuation's uses. This serves mainly to
808 ;;; propagate TRULY-THE through LETs.
809 (defun move-return-uses (fun call next-block)
810 (declare (type clambda fun) (type basic-combination call)
811 (type cblock next-block))
812 (let* ((return (lambda-return fun))
813 (return-block (node-block return)))
814 (unlink-blocks return-block
815 (component-tail (block-component return-block)))
816 (link-blocks return-block next-block)
818 (delete-return return)
819 (let ((result (return-result return))
820 (cont (node-cont call))
821 (call-type (node-derived-type call)))
822 (when (eq (continuation-use cont) call)
823 (assert-continuation-type cont (continuation-asserted-type result)))
824 (unless (eq call-type *wild-type*)
825 (do-uses (use result)
826 (derive-node-type use call-type)))
827 (substitute-continuation-uses cont result)))
830 ;;; Change all CONT for all the calls to FUN to be the start
831 ;;; continuation for the bind node. This allows the blocks to be
832 ;;; joined if the caller count ever goes to one.
833 (defun move-let-call-cont (fun)
834 (declare (type clambda fun))
835 (let ((new-cont (node-prev (lambda-bind fun))))
836 (dolist (ref (leaf-refs fun))
837 (let ((dest (continuation-dest (node-cont ref))))
838 (delete-continuation-use dest)
839 (add-continuation-use dest new-cont))))
842 ;;; We are converting FUN to be a LET when the call is in a non-tail
843 ;;; position. Any previously tail calls in FUN are no longer tail
844 ;;; calls, and must be restored to normal calls which transfer to
845 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
846 ;;; the RETURN-RESULT, because the return might have been deleted (if
847 ;;; all calls were TR.)
848 (defun unconvert-tail-calls (fun call next-block)
849 (dolist (called (lambda-calls-or-closes fun))
850 (when (lambda-p called)
851 (dolist (ref (leaf-refs called))
852 (let ((this-call (continuation-dest (node-cont ref))))
854 (node-tail-p this-call)
855 (eq (node-home-lambda this-call) fun))
856 (setf (node-tail-p this-call) nil)
857 (ecase (functional-kind called)
858 ((nil :cleanup :optional)
859 (let ((block (node-block this-call))
860 (cont (node-cont call)))
861 (ensure-block-start cont)
862 (unlink-blocks block (first (block-succ block)))
863 (link-blocks block next-block)
864 (delete-continuation-use this-call)
865 (add-continuation-use this-call cont)))
867 ;; The called function might be an assignment in the
868 ;; case where we are currently converting that function.
869 ;; In steady-state, assignments never appear as a called
872 (aver (eq called fun)))))))))
875 ;;; Deal with returning from a LET or assignment that we are
876 ;;; converting. FUN is the function we are calling, CALL is a call to
877 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
878 ;;; NULL if call is a tail call.
880 ;;; If the call is not a tail call, then we must do
881 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
882 ;;; its value out of the enclosing non-let function. When call is
883 ;;; non-TR, we must convert it back to an ordinary local call, since
884 ;;; the value must be delivered to the receiver of CALL's value.
886 ;;; We do different things depending on whether the caller and callee
887 ;;; have returns left:
889 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
890 ;;; Either the function doesn't return, or all returns are via
891 ;;; tail-recursive local calls.
892 ;;; -- If CALL is a non-tail call, or if both have returns, then
893 ;;; we delete the callee's return, move its uses to the call's
894 ;;; result continuation, and transfer control to the appropriate
896 ;;; -- If the callee has a return, but the caller doesn't, then we
897 ;;; move the return to the caller.
898 (defun move-return-stuff (fun call next-block)
899 (declare (type clambda fun) (type basic-combination call)
900 (type (or cblock null) next-block))
902 (unconvert-tail-calls fun call next-block))
903 (let* ((return (lambda-return fun))
904 (call-fun (node-home-lambda call))
905 (call-return (lambda-return call-fun)))
907 ((or next-block call-return)
908 (unless (block-delete-p (node-block return))
909 (move-return-uses fun call
910 (or next-block (node-block call-return)))))
912 (aver (node-tail-p call))
913 (setf (lambda-return call-fun) return)
914 (setf (return-lambda return) call-fun))))
915 (move-let-call-cont fun)
918 ;;; Actually do LET conversion. We call subfunctions to do most of the
919 ;;; work. We change the CALL's CONT to be the continuation heading the
920 ;;; BIND block, and also do REOPTIMIZE-CONTINUATION on the args and
921 ;;; CONT so that LET-specific IR1 optimizations get a chance. We blow
922 ;;; away any entry for the function in *FREE-FUNCTIONS* so that nobody
923 ;;; will create new references to it.
924 (defun let-convert (fun call)
925 (declare (type clambda fun) (type basic-combination call))
926 (let ((next-block (if (node-tail-p call)
928 (insert-let-body fun call))))
929 (move-return-stuff fun call next-block)
930 (merge-lets fun call)))
932 ;;; Reoptimize all of CALL's args and its result.
933 (defun reoptimize-call (call)
934 (declare (type basic-combination call))
935 (dolist (arg (basic-combination-args call))
937 (reoptimize-continuation arg)))
938 (reoptimize-continuation (node-cont call))
941 ;;; We also don't convert calls to named functions which appear in the
942 ;;; initial component, delaying this until optimization. This
943 ;;; minimizes the likelihood that we will LET-convert a function which
944 ;;; may have references added due to later local inline expansion.
945 (defun ok-initial-convert-p (fun)
946 (not (and (leaf-has-source-name-p fun)
947 (eq (component-kind (lambda-component fun))
950 ;;; This function is called when there is some reason to believe that
951 ;;; CLAMBDA might be converted into a LET. This is done after local
952 ;;; call analysis, and also when a reference is deleted. We only
953 ;;; convert to a let when the function is a normal local function, has
954 ;;; no XEP, and is referenced in exactly one local call. Conversion is
955 ;;; also inhibited if the only reference is in a block about to be
956 ;;; deleted. We return true if we converted.
958 ;;; These rules may seem unnecessarily restrictive, since there are
959 ;;; some cases where we could do the return with a jump that don't
960 ;;; satisfy these requirements. The reason for doing things this way
961 ;;; is that it makes the concept of a LET much more useful at the
962 ;;; level of IR1 semantics. The :ASSIGNMENT function kind provides
963 ;;; another way to optimize calls to single-return/multiple call
966 ;;; We don't attempt to convert calls to functions that have an XEP,
967 ;;; since we might be embarrassed later when we want to convert a
968 ;;; newly discovered local call. Also, see OK-INITIAL-CONVERT-P.
969 (defun maybe-let-convert (clambda)
970 (declare (type clambda clambda))
971 (let ((refs (leaf-refs clambda)))
974 (member (functional-kind clambda) '(nil :assignment))
975 (not (functional-entry-fun clambda)))
976 (let* ((ref-cont (node-cont (first refs)))
977 (dest (continuation-dest ref-cont)))
979 (basic-combination-p dest)
980 (eq (basic-combination-fun dest) ref-cont)
981 (eq (basic-combination-kind dest) :local)
982 (not (block-delete-p (node-block dest)))
983 (cond ((ok-initial-convert-p clambda) t)
985 (reoptimize-continuation ref-cont)
987 (unless (eq (functional-kind clambda) :assignment)
988 (let-convert clambda dest))
989 (reoptimize-call dest)
990 (setf (functional-kind clambda)
991 (if (mv-combination-p dest) :mv-let :let))))
994 ;;;; tail local calls and assignments
996 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
997 ;;; they definitely won't generate any cleanup code. Currently we
998 ;;; recognize lexical entry points that are only used locally (if at
1000 (defun only-harmless-cleanups (block1 block2)
1001 (declare (type cblock block1 block2))
1002 (or (eq block1 block2)
1003 (let ((cleanup2 (block-start-cleanup block2)))
1004 (do ((cleanup (block-end-cleanup block1)
1005 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1006 ((eq cleanup cleanup2) t)
1007 (case (cleanup-kind cleanup)
1009 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1011 (t (return nil)))))))
1013 ;;; If a potentially TR local call really is TR, then convert it to
1014 ;;; jump directly to the called function. We also call
1015 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1016 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
1017 ;;; the succesor (potentially deleting the RETURN node) unless:
1018 ;;; -- The call has already been converted.
1019 ;;; -- The call isn't TR (some implicit MV PROG1.)
1020 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
1021 ;;; so that we can use known return inside the component.)
1022 ;;; -- There is a change in the cleanup between the call in the return,
1023 ;;; so we might need to introduce cleanup code.
1024 (defun maybe-convert-tail-local-call (call)
1025 (declare (type combination call))
1026 (let ((return (continuation-dest (node-cont call))))
1027 (aver (return-p return))
1028 (when (and (not (node-tail-p call))
1029 (immediately-used-p (return-result return) call)
1030 (not (eq (functional-kind (node-home-lambda call))
1032 (only-harmless-cleanups (node-block call)
1033 (node-block return)))
1034 (node-ends-block call)
1035 (let ((block (node-block call))
1036 (fun (combination-lambda call)))
1037 (setf (node-tail-p call) t)
1038 (unlink-blocks block (first (block-succ block)))
1039 (link-blocks block (lambda-block fun))
1040 (values t (maybe-convert-to-assignment fun))))))
1042 ;;; This is called when we believe it might make sense to convert Fun
1043 ;;; to an assignment. All this function really does is determine when
1044 ;;; a function with more than one call can still be combined with the
1045 ;;; calling function's environment. We can convert when:
1046 ;;; -- The function is a normal, non-entry function, and
1047 ;;; -- Except for one call, all calls must be tail recursive calls
1048 ;;; in the called function (i.e. are self-recursive tail calls)
1049 ;;; -- OK-INITIAL-CONVERT-P is true.
1051 ;;; There may be one outside call, and it need not be tail-recursive.
1052 ;;; Since all tail local calls have already been converted to direct
1053 ;;; transfers, the only control semantics needed are to splice in the
1054 ;;; body at the non-tail call. If there is no non-tail call, then we
1055 ;;; need only merge the environments. Both cases are handled by
1058 ;;; ### It would actually be possible to allow any number of outside
1059 ;;; calls as long as they all return to the same place (i.e. have the
1060 ;;; same conceptual continuation.) A special case of this would be
1061 ;;; when all of the outside calls are tail recursive.
1062 (defun maybe-convert-to-assignment (fun)
1063 (declare (type clambda fun))
1064 (when (and (not (functional-kind fun))
1065 (not (functional-entry-fun fun)))
1066 (let ((non-tail nil)
1068 (when (and (dolist (ref (leaf-refs fun) t)
1069 (let ((dest (continuation-dest (node-cont ref))))
1070 (when (or (not dest)
1071 (block-delete-p (node-block dest)))
1073 (let ((home (node-home-lambda ref)))
1074 (unless (eq home fun)
1075 (when call-fun (return nil))
1076 (setq call-fun home))
1077 (unless (node-tail-p dest)
1078 (when (or non-tail (eq home fun)) (return nil))
1079 (setq non-tail dest)))))
1080 (ok-initial-convert-p fun))
1081 (setf (functional-kind fun) :assignment)
1082 (let-convert fun (or non-tail
1084 (node-cont (first (leaf-refs fun))))))
1085 (when non-tail (reoptimize-call non-tail))