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