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