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 (reoptimize-component *current-component* :maybe)
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-object 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-object
393 (if (functional-p losing-local-object)
394 (let ((*compiler-error-context* call))
395 (compiler-notify "couldn't inline expand because expansion ~
396 calls this LET-converted local function:~
398 (leaf-debug-name losing-local-object)))
399 (let ((*compiler-error-context* call))
400 (compiler-notify "implementation limitation: couldn't inline ~
401 expand because expansion refers to ~
402 the optimized away object ~S."
403 losing-local-object)))
404 (loop for block = (block-next pred) then (block-next block)
406 do (setf (block-delete-p block) t))
407 (loop for block = (block-next pred) then (block-next block)
409 do (delete-block block t))
412 (change-ref-leaf ref converted-lambda)
414 original-functional))
416 ;;; Dispatch to the appropriate function to attempt to convert a call.
417 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
418 ;;; optimization as well as in local call analysis. If the call is is
419 ;;; already :LOCAL, we do nothing. If the call is already scheduled
420 ;;; for deletion, also do nothing (in addition to saving time, this
421 ;;; also avoids some problems with optimizing collections of functions
422 ;;; that are partially deleted.)
424 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
425 ;;; called on a :INITIAL component, we don't care whether the caller
426 ;;; and callee are in the same component. Afterward, we must stick
427 ;;; with whatever component division we have chosen.
429 ;;; Before attempting to convert a call, we see whether the function
430 ;;; is supposed to be inline expanded. Call conversion proceeds as
431 ;;; before after any expansion.
433 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
434 ;;; warnings will get the right context.
435 (defun convert-call-if-possible (ref call)
436 (declare (type ref ref) (type basic-combination call))
437 (let* ((block (node-block call))
438 (component (block-component block))
439 (original-fun (ref-leaf ref)))
440 (aver (functional-p original-fun))
441 (unless (or (member (basic-combination-kind call) '(:local :error))
442 (node-to-be-deleted-p call)
443 (member (functional-kind original-fun)
444 '(:toplevel-xep :deleted))
445 (not (or (eq (component-kind component) :initial)
448 (lambda-bind (main-entry original-fun))))
450 (let ((fun (if (xep-p original-fun)
451 (functional-entry-fun original-fun)
453 (*compiler-error-context* call))
455 (when (and (eq (functional-inlinep fun) :inline)
456 (rest (leaf-refs original-fun)))
457 (setq fun (maybe-expand-local-inline fun ref call)))
459 (aver (member (functional-kind fun)
460 '(nil :escape :cleanup :optional)))
461 (cond ((mv-combination-p call)
462 (convert-mv-call ref call fun))
464 (convert-lambda-call ref call fun))
466 (convert-hairy-call ref call fun))))))
470 ;;; Attempt to convert a multiple-value call. The only interesting
471 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
472 ;;; exactly one reference and no XEP, and is called with one values
475 ;;; We change the call to be to the last optional entry point and
476 ;;; change the call to be local. Due to our preconditions, the call
477 ;;; should eventually be converted to a let, but we can't do that now,
478 ;;; since there may be stray references to the e-p lambda due to
479 ;;; optional defaulting code.
481 ;;; We also use variable types for the called function to construct an
482 ;;; assertion for the values lvar.
484 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
485 (defun convert-mv-call (ref call fun)
486 (declare (type ref ref) (type mv-combination call) (type functional fun))
487 (when (and (looks-like-an-mv-bind fun)
488 (singleton-p (leaf-refs fun))
489 (singleton-p (basic-combination-args call)))
490 (let* ((*current-component* (node-component ref))
491 (ep (optional-dispatch-entry-point-fun
492 fun (optional-dispatch-max-args fun))))
493 (when (null (leaf-refs ep))
494 (aver (= (optional-dispatch-min-args fun) 0))
495 (aver (not (functional-entry-fun fun)))
496 (setf (basic-combination-kind call) :local)
497 (pushnew ep (lambda-calls-or-closes (node-home-lambda call)))
498 (merge-tail-sets call ep)
499 (change-ref-leaf ref ep)
502 (first (basic-combination-args call))
503 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
504 (lexenv-policy (node-lexenv call))))))
507 ;;; Attempt to convert a call to a lambda. If the number of args is
508 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
509 ;;; from future consideration. If the argcount is O.K. then we just
511 (defun convert-lambda-call (ref call fun)
512 (declare (type ref ref) (type combination call) (type clambda fun))
513 (let ((nargs (length (lambda-vars fun)))
514 (n-call-args (length (combination-args call))))
515 (cond ((= n-call-args nargs)
516 (convert-call ref call fun))
519 'local-argument-mismatch
521 "function called with ~R argument~:P, but wants exactly ~R"
522 :format-arguments (list n-call-args nargs))
523 (setf (basic-combination-kind call) :error)))))
525 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
527 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
528 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
529 ;;; a call to the correct entry point. If &KEY args are supplied, then
530 ;;; dispatch to a subfunction. We don't convert calls to functions
531 ;;; that have a &MORE (or &REST) arg.
532 (defun convert-hairy-call (ref call fun)
533 (declare (type ref ref) (type combination call)
534 (type optional-dispatch fun))
535 (let ((min-args (optional-dispatch-min-args fun))
536 (max-args (optional-dispatch-max-args fun))
537 (call-args (length (combination-args call))))
538 (cond ((< call-args min-args)
540 'local-argument-mismatch
542 "function called with ~R argument~:P, but wants at least ~R"
543 :format-arguments (list call-args min-args))
544 (setf (basic-combination-kind call) :error))
545 ((<= call-args max-args)
546 (convert-call ref call
547 (let ((*current-component* (node-component ref)))
548 (optional-dispatch-entry-point-fun
549 fun (- call-args min-args)))))
550 ((optional-dispatch-more-entry fun)
551 (convert-more-call ref call fun))
554 'local-argument-mismatch
556 "function called with ~R argument~:P, but wants at most ~R"
558 (list call-args max-args))
559 (setf (basic-combination-kind call) :error))))
562 ;;; This function is used to convert a call to an entry point when
563 ;;; complex transformations need to be done on the original arguments.
564 ;;; ENTRY is the entry point function that we are calling. VARS is a
565 ;;; list of variable names which are bound to the original call
566 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
567 ;;; is the list of arguments to the entry point function.
569 ;;; In order to avoid gruesome graph grovelling, we introduce a new
570 ;;; function that rearranges the arguments and calls the entry point.
571 ;;; We analyze the new function and the entry point immediately so
572 ;;; that everything gets converted during the single pass.
573 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
574 (declare (list vars ignores args) (type ref ref) (type combination call)
575 (type clambda entry))
577 (with-ir1-environment-from-node call
580 (declare (ignorable ,@ignores))
581 (%funcall ,entry ,@args))
582 :debug-name (debug-namify "hairy function entry "
584 (basic-combination-fun call)))))))
585 (convert-call ref call new-fun)
586 (dolist (ref (leaf-refs entry))
587 (convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
589 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
590 ;;; function into a local call to the MAIN-ENTRY.
592 ;;; First we verify that all keywords are constant and legal. If there
593 ;;; aren't, then we warn the user and don't attempt to convert the call.
595 ;;; We massage the supplied &KEY arguments into the order expected
596 ;;; by the main entry. This is done by binding all the arguments to
597 ;;; the keyword call to variables in the introduced lambda, then
598 ;;; passing these values variables in the correct order when calling
599 ;;; the main entry. Unused arguments (such as the keywords themselves)
600 ;;; are discarded simply by not passing them along.
602 ;;; If there is a &REST arg, then we bundle up the args and pass them
604 (defun convert-more-call (ref call fun)
605 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
606 (let* ((max (optional-dispatch-max-args fun))
607 (arglist (optional-dispatch-arglist fun))
608 (args (combination-args call))
609 (more (nthcdr max args))
610 (flame (policy call (or (> speed inhibit-warnings)
611 (> space inhibit-warnings))))
615 (temps (make-gensym-list max))
616 (more-temps (make-gensym-list (length more))))
621 (dolist (var arglist)
622 (let ((info (lambda-var-arg-info var)))
624 (ecase (arg-info-kind info)
628 ((:more-context :more-count)
629 (compiler-warn "can't local-call functions with &MORE args")
630 (setf (basic-combination-kind call) :error)
631 (return-from convert-more-call))))))
633 (when (optional-dispatch-keyp fun)
634 (when (oddp (length more))
635 (compiler-warn "function called with odd number of ~
636 arguments in keyword portion")
637 (setf (basic-combination-kind call) :error)
638 (return-from convert-more-call))
640 (do ((key more (cddr key))
641 (temp more-temps (cddr temp)))
643 (let ((lvar (first key)))
644 (unless (constant-lvar-p lvar)
646 (compiler-notify "non-constant keyword in keyword call"))
647 (setf (basic-combination-kind call) :error)
648 (return-from convert-more-call))
650 (let ((name (lvar-value lvar))
653 ;; FIXME: check whether KEY was supplied earlier
654 (when (and (eq name :allow-other-keys) (not allow-found))
655 (let ((val (second key)))
656 (cond ((constant-lvar-p val)
658 allowp (lvar-value val)))
660 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
661 (setf (basic-combination-kind call) :error)
662 (return-from convert-more-call)))))
663 (dolist (var (key-vars)
666 (unless (eq name :allow-other-keys)
667 (setq loser (list name)))))
668 (let ((info (lambda-var-arg-info var)))
669 (when (eq (arg-info-key info) name)
671 (supplied (cons var val))
674 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
675 (compiler-warn "function called with unknown argument keyword ~S"
677 (setf (basic-combination-kind call) :error)
678 (return-from convert-more-call)))
680 (collect ((call-args))
681 (do ((var arglist (cdr var))
682 (temp temps (cdr temp)))
684 (let ((info (lambda-var-arg-info (car var))))
686 (ecase (arg-info-kind info)
688 (call-args (car temp))
689 (when (arg-info-supplied-p info)
692 (call-args `(list ,@more-temps))
696 (call-args (car temp)))))
698 (dolist (var (key-vars))
699 (let ((info (lambda-var-arg-info var))
700 (temp (cdr (assoc var (supplied)))))
703 (call-args (arg-info-default info)))
704 (when (arg-info-supplied-p info)
705 (call-args (not (null temp))))))
707 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
708 (append temps more-temps)
709 (ignores) (call-args)))))
715 ;;;; Converting to a LET has differing significance to various parts
716 ;;;; of the compiler:
717 ;;;; -- The body of a LET is spliced in immediately after the
718 ;;;; corresponding combination node, making the control transfer
719 ;;;; explicit and allowing LETs to be mashed together into a single
720 ;;;; block. The value of the LET is delivered directly to the
721 ;;;; original lvar for the call, eliminating the need to
722 ;;;; propagate information from the dummy result lvar.
723 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
724 ;;;; that there is only one expression that the variable can be bound
725 ;;;; to, and this is easily substituted for.
726 ;;;; -- LETs are interesting to environment analysis and to the back
727 ;;;; end because in most ways a LET can be considered to be "the
728 ;;;; same function" as its home function.
729 ;;;; -- LET conversion has dynamic scope implications, since control
730 ;;;; transfers within the same environment are local. In a local
731 ;;;; control transfer, cleanup code must be emitted to remove
732 ;;;; dynamic bindings that are no longer in effect.
734 ;;; Set up the control transfer to the called CLAMBDA. We split the
735 ;;; call block immediately after the call, and link the head of
736 ;;; CLAMBDA to the call block. The successor block after splitting
737 ;;; (where we return to) is returned.
739 ;;; If the lambda is is a different component than the call, then we
740 ;;; call JOIN-COMPONENTS. This only happens in block compilation
741 ;;; before FIND-INITIAL-DFO.
742 (defun insert-let-body (clambda call)
743 (declare (type clambda clambda) (type basic-combination call))
744 (let* ((call-block (node-block call))
745 (bind-block (node-block (lambda-bind clambda)))
746 (component (block-component call-block)))
747 (aver-live-component component)
748 (let ((clambda-component (block-component bind-block)))
749 (unless (eq clambda-component component)
750 (aver (eq (component-kind component) :initial))
751 (join-components component clambda-component)))
752 (let ((*current-component* component))
753 (node-ends-block call))
754 (destructuring-bind (next-block)
755 (block-succ call-block)
756 (unlink-blocks call-block next-block)
757 (link-blocks call-block bind-block)
760 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
761 ;;; same set as; but leave CLAMBDA with a valid tail set value of
762 ;;; its own, for the benefit of code which might try to pull
763 ;;; something out of it (e.g. return type).
764 (defun depart-from-tail-set (clambda)
765 ;; Until sbcl-0.pre7.37.flaky5.2, we did
766 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
767 ;; (SETF (TAIL-SET-FUNS TAILS)
768 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
769 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
770 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
771 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
772 ;; inconsistent, and perhaps unsafe, for us to think we're in the
773 ;; tail set. Unfortunately..
775 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
776 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
777 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
778 ;; which you called in order to get the address of an external LAMBDA):
779 ;; the external function was defined in terms of internal function,
780 ;; which was LET-converted, and then things blew up downstream when
781 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
782 ;; the now-NILed-out TAIL-SET. So..
784 ;; To deal with this problem, we no longer NIL out
785 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
786 ;; * If we're the only function in TAIL-SET-FUNS, it should
787 ;; be safe to leave ourself linked to it, and it to you.
788 ;; * If there are other functions in TAIL-SET-FUNS, then we're
789 ;; afraid of future optimizations on those functions causing
790 ;; the TAIL-SET object no longer to be valid to describe our
791 ;; return value. Thus, we delete ourselves from that object;
792 ;; but we save a newly-allocated tail-set, derived from the old
793 ;; one, for ourselves, for the use of later code (e.g.
794 ;; FINALIZE-XEP-DEFINITION) which might want to
795 ;; know about our return type.
796 (let* ((old-tail-set (lambda-tail-set clambda))
797 (old-tail-set-funs (tail-set-funs old-tail-set)))
798 (unless (= 1 (length old-tail-set-funs))
799 (setf (tail-set-funs old-tail-set)
800 (delete clambda old-tail-set-funs))
801 (let ((new-tail-set (copy-tail-set old-tail-set)))
802 (setf (lambda-tail-set clambda) new-tail-set
803 (tail-set-funs new-tail-set) (list clambda)))))
804 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
805 ;; remain valid in this case, so we nuke it on the theory that
806 ;; missing information tends to be less dangerous than incorrect
808 (setf (tail-set-info (lambda-tail-set clambda)) nil))
810 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
811 ;;; its LETs to LETs for the CALL's home function. We merge the calls
812 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
813 ;;; in the process. We also merge the ENTRIES.
815 ;;; We also unlink the function head from the component head and set
816 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
818 (defun merge-lets (clambda call)
820 (declare (type clambda clambda) (type basic-combination call))
822 (let ((component (node-component call)))
823 (unlink-blocks (component-head component) (lambda-block clambda))
824 (setf (component-lambdas component)
825 (delete clambda (component-lambdas component)))
826 (setf (component-reanalyze component) t))
827 (setf (lambda-call-lexenv clambda) (node-lexenv call))
829 (depart-from-tail-set clambda)
831 (let* ((home (node-home-lambda call))
832 (home-physenv (lambda-physenv home)))
834 (aver (not (eq home clambda)))
836 ;; CLAMBDA belongs to HOME now.
837 (push clambda (lambda-lets home))
838 (setf (lambda-home clambda) home)
839 (setf (lambda-physenv clambda) home-physenv)
841 ;; All of CLAMBDA's LETs belong to HOME now.
842 (let ((lets (lambda-lets clambda)))
844 (setf (lambda-home let) home)
845 (setf (lambda-physenv let) home-physenv))
846 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
847 ;; CLAMBDA no longer has an independent existence as an entity
849 (setf (lambda-lets clambda) nil)
851 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
853 (setf (lambda-calls-or-closes home)
855 (nunion (lambda-calls-or-closes clambda)
856 (lambda-calls-or-closes home))))
857 ;; CLAMBDA no longer has an independent existence as an entity
858 ;; which calls things or has DFO dependencies.
859 (setf (lambda-calls-or-closes clambda) nil)
861 ;; All of CLAMBDA's ENTRIES belong to HOME now.
862 (setf (lambda-entries home)
863 (nconc (lambda-entries clambda)
864 (lambda-entries home)))
865 ;; CLAMBDA no longer has an independent existence as an entity
867 (setf (lambda-entries clambda) nil))
871 ;;; Handle the value semantics of LET conversion. Delete FUN's return
872 ;;; node, and change the control flow to transfer to NEXT-BLOCK
873 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
874 (defun move-return-uses (fun call next-block)
875 (declare (type clambda fun) (type basic-combination call)
876 (type cblock next-block))
877 (let* ((return (lambda-return fun))
879 (ensure-block-start (node-prev return))
880 (node-block return))))
881 (unlink-blocks return-block
882 (component-tail (block-component return-block)))
883 (link-blocks return-block next-block)
885 (delete-return return)
886 (let ((result (return-result return))
887 (lvar (if (node-tail-p call)
888 (return-result (lambda-return (node-home-lambda call)))
890 (call-type (node-derived-type call)))
891 (unless (eq call-type *wild-type*)
892 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
893 (do-uses (use result)
894 (derive-node-type use call-type)))
895 (substitute-lvar-uses lvar result
896 (and lvar (eq (lvar-uses lvar) call)))))
899 ;;; We are converting FUN to be a LET when the call is in a non-tail
900 ;;; position. Any previously tail calls in FUN are no longer tail
901 ;;; calls, and must be restored to normal calls which transfer to
902 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
903 ;;; the RETURN-RESULT, because the return might have been deleted (if
904 ;;; all calls were TR.)
905 (defun unconvert-tail-calls (fun call next-block)
906 (dolist (called (lambda-calls-or-closes fun))
907 (when (lambda-p called)
908 (dolist (ref (leaf-refs called))
909 (let ((this-call (node-dest ref)))
911 (node-tail-p this-call)
912 (eq (node-home-lambda this-call) fun))
913 (setf (node-tail-p this-call) nil)
914 (ecase (functional-kind called)
915 ((nil :cleanup :optional)
916 (let ((block (node-block this-call))
917 (lvar (node-lvar call)))
918 (unlink-blocks block (first (block-succ block)))
919 (link-blocks block next-block)
920 (aver (not (node-lvar this-call)))
921 (add-lvar-use this-call lvar)))
923 ;; The called function might be an assignment in the
924 ;; case where we are currently converting that function.
925 ;; In steady-state, assignments never appear as a called
928 (aver (eq called fun)))))))))
931 ;;; Deal with returning from a LET or assignment that we are
932 ;;; converting. FUN is the function we are calling, CALL is a call to
933 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
934 ;;; NULL if call is a tail call.
936 ;;; If the call is not a tail call, then we must do
937 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
938 ;;; its value out of the enclosing non-let function. When call is
939 ;;; non-TR, we must convert it back to an ordinary local call, since
940 ;;; the value must be delivered to the receiver of CALL's value.
942 ;;; We do different things depending on whether the caller and callee
943 ;;; have returns left:
945 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
946 ;;; Either the function doesn't return, or all returns are via
947 ;;; tail-recursive local calls.
948 ;;; -- If CALL is a non-tail call, or if both have returns, then
949 ;;; we delete the callee's return, move its uses to the call's
950 ;;; result lvar, and transfer control to the appropriate
952 ;;; -- If the callee has a return, but the caller doesn't, then we
953 ;;; move the return to the caller.
954 (defun move-return-stuff (fun call next-block)
955 (declare (type clambda fun) (type basic-combination call)
956 (type (or cblock null) next-block))
958 (unconvert-tail-calls fun call next-block))
959 (let* ((return (lambda-return fun))
960 (call-fun (node-home-lambda call))
961 (call-return (lambda-return call-fun)))
962 (when (and call-return
963 (block-delete-p (node-block call-return)))
964 (delete-return call-return)
965 (unlink-node call-return)
966 (setq call-return nil))
968 ((or next-block call-return)
969 (unless (block-delete-p (node-block return))
971 (ensure-block-start (node-prev call-return))
972 (setq next-block (node-block call-return)))
973 (move-return-uses fun call next-block)))
975 (aver (node-tail-p call))
976 (setf (lambda-return call-fun) return)
977 (setf (return-lambda return) call-fun)
978 (setf (lambda-return fun) nil))))
979 (%delete-lvar-use call) ; LET call does not have value semantics
982 ;;; Actually do LET conversion. We call subfunctions to do most of the
983 ;;; work. We do REOPTIMIZE-LVAR on the args and CALL's lvar so that
984 ;;; LET-specific IR1 optimizations get a chance. We blow away any
985 ;;; entry for the function in *FREE-FUNS* so that nobody will create
986 ;;; new references to it.
987 (defun let-convert (fun call)
988 (declare (type clambda fun) (type basic-combination call))
989 (let* ((next-block (insert-let-body fun call))
990 (next-block (if (node-tail-p call)
993 (move-return-stuff fun call next-block)
994 (merge-lets fun call)
995 (setf (node-tail-p call) nil)
996 ;; If CALL has a derive type NIL, it means that "its return" is
997 ;; unreachable, but the next BIND is still reachable; in order to
998 ;; not confuse MAYBE-TERMINATE-BLOCK...
999 (setf (node-derived-type call) *wild-type*)))
1001 ;;; Reoptimize all of CALL's args and its result.
1002 (defun reoptimize-call (call)
1003 (declare (type basic-combination call))
1004 (dolist (arg (basic-combination-args call))
1006 (reoptimize-lvar arg)))
1007 (reoptimize-lvar (node-lvar call))
1010 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
1012 (defun declarations-suppress-let-conversion-p (clambda)
1013 ;; From the user's point of view, LET-converting something that
1014 ;; has a name is inlining it. (The user can't see what we're doing
1015 ;; with anonymous things, and suppressing inlining
1016 ;; for such things can easily give Python acute indigestion, so
1018 (when (leaf-has-source-name-p clambda)
1019 ;; ANSI requires that explicit NOTINLINE be respected.
1020 (or (eq (lambda-inlinep clambda) :notinline)
1021 ;; If (= LET-CONVERTION 0) we can guess that inlining
1022 ;; generally won't be appreciated, but if the user
1023 ;; specifically requests inlining, that takes precedence over
1024 ;; our general guess.
1025 (and (policy clambda (= let-convertion 0))
1026 (not (eq (lambda-inlinep clambda) :inline))))))
1028 ;;; We also don't convert calls to named functions which appear in the
1029 ;;; initial component, delaying this until optimization. This
1030 ;;; minimizes the likelihood that we will LET-convert a function which
1031 ;;; may have references added due to later local inline expansion.
1032 (defun ok-initial-convert-p (fun)
1033 (not (and (leaf-has-source-name-p fun)
1034 (or (declarations-suppress-let-conversion-p fun)
1035 (eq (component-kind (lambda-component fun))
1038 ;;; This function is called when there is some reason to believe that
1039 ;;; CLAMBDA might be converted into a LET. This is done after local
1040 ;;; call analysis, and also when a reference is deleted. We return
1041 ;;; true if we converted.
1042 (defun maybe-let-convert (clambda)
1043 (declare (type clambda clambda))
1044 (unless (declarations-suppress-let-conversion-p clambda)
1045 ;; We only convert to a LET when the function is a normal local
1046 ;; function, has no XEP, and is referenced in exactly one local
1047 ;; call. Conversion is also inhibited if the only reference is in
1048 ;; a block about to be deleted.
1050 ;; These rules limiting LET conversion may seem unnecessarily
1051 ;; restrictive, since there are some cases where we could do the
1052 ;; return with a jump that don't satisfy these requirements. The
1053 ;; reason for doing things this way is that it makes the concept
1054 ;; of a LET much more useful at the level of IR1 semantics. The
1055 ;; :ASSIGNMENT function kind provides another way to optimize
1056 ;; calls to single-return/multiple call functions.
1058 ;; We don't attempt to convert calls to functions that have an
1059 ;; XEP, since we might be embarrassed later when we want to
1060 ;; convert a newly discovered local call. Also, see
1061 ;; OK-INITIAL-CONVERT-P.
1062 (let ((refs (leaf-refs clambda)))
1065 (memq (functional-kind clambda) '(nil :assignment))
1066 (not (functional-entry-fun clambda)))
1067 (binding* ((ref (first refs))
1068 (ref-lvar (node-lvar ref) :exit-if-null)
1069 (dest (lvar-dest ref-lvar)))
1070 (when (and (basic-combination-p dest)
1071 (eq (basic-combination-fun dest) ref-lvar)
1072 (eq (basic-combination-kind dest) :local)
1073 (not (node-to-be-deleted-p dest))
1074 (not (block-delete-p (lambda-block clambda)))
1075 (cond ((ok-initial-convert-p clambda) t)
1077 (reoptimize-lvar ref-lvar)
1079 (when (eq clambda (node-home-lambda dest))
1080 (delete-lambda clambda)
1081 (return-from maybe-let-convert nil))
1082 (unless (eq (functional-kind clambda) :assignment)
1083 (let-convert clambda dest))
1084 (reoptimize-call dest)
1085 (setf (functional-kind clambda)
1086 (if (mv-combination-p dest) :mv-let :let))))
1089 ;;;; tail local calls and assignments
1091 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1092 ;;; they definitely won't generate any cleanup code. Currently we
1093 ;;; recognize lexical entry points that are only used locally (if at
1095 (defun only-harmless-cleanups (block1 block2)
1096 (declare (type cblock block1 block2))
1097 (or (eq block1 block2)
1098 (let ((cleanup2 (block-start-cleanup block2)))
1099 (do ((cleanup (block-end-cleanup block1)
1100 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1101 ((eq cleanup cleanup2) t)
1102 (case (cleanup-kind cleanup)
1104 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1106 (t (return nil)))))))
1108 ;;; If a potentially TR local call really is TR, then convert it to
1109 ;;; jump directly to the called function. We also call
1110 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1111 ;;; tail-convert. The second is the value of M-C-T-A.
1112 (defun maybe-convert-tail-local-call (call)
1113 (declare (type combination call))
1114 (let ((return (lvar-dest (node-lvar call)))
1115 (fun (combination-lambda call)))
1116 (aver (return-p return))
1117 (when (and (not (node-tail-p call)) ; otherwise already converted
1118 ;; this is a tail call
1119 (immediately-used-p (return-result return) call)
1120 (only-harmless-cleanups (node-block call)
1121 (node-block return))
1122 ;; If the call is in an XEP, we might decide to make it
1123 ;; non-tail so that we can use known return inside the
1125 (not (eq (functional-kind (node-home-lambda call))
1127 (not (block-delete-p (lambda-block fun))))
1128 (node-ends-block call)
1129 (let ((block (node-block call)))
1130 (setf (node-tail-p call) t)
1131 (unlink-blocks block (first (block-succ block)))
1132 (link-blocks block (lambda-block fun))
1133 (delete-lvar-use call)
1134 (values t (maybe-convert-to-assignment fun))))))
1136 ;;; This is called when we believe it might make sense to convert
1137 ;;; CLAMBDA to an assignment. All this function really does is
1138 ;;; determine when a function with more than one call can still be
1139 ;;; combined with the calling function's environment. We can convert
1141 ;;; -- The function is a normal, non-entry function, and
1142 ;;; -- Except for one call, all calls must be tail recursive calls
1143 ;;; in the called function (i.e. are self-recursive tail calls)
1144 ;;; -- OK-INITIAL-CONVERT-P is true.
1146 ;;; There may be one outside call, and it need not be tail-recursive.
1147 ;;; Since all tail local calls have already been converted to direct
1148 ;;; transfers, the only control semantics needed are to splice in the
1149 ;;; body at the non-tail call. If there is no non-tail call, then we
1150 ;;; need only merge the environments. Both cases are handled by
1153 ;;; ### It would actually be possible to allow any number of outside
1154 ;;; calls as long as they all return to the same place (i.e. have the
1155 ;;; same conceptual continuation.) A special case of this would be
1156 ;;; when all of the outside calls are tail recursive.
1157 (defun maybe-convert-to-assignment (clambda)
1158 (declare (type clambda clambda))
1159 (when (and (not (functional-kind clambda))
1160 (not (functional-entry-fun clambda)))
1161 (let ((outside-non-tail-call nil)
1163 (when (and (dolist (ref (leaf-refs clambda) t)
1164 (let ((dest (node-dest ref)))
1165 (when (or (not dest)
1166 (block-delete-p (node-block dest)))
1168 (let ((home (node-home-lambda ref)))
1169 (unless (eq home clambda)
1172 (setq outside-call dest))
1173 (unless (node-tail-p dest)
1174 (when (or outside-non-tail-call (eq home clambda))
1176 (setq outside-non-tail-call dest)))))
1177 (ok-initial-convert-p clambda))
1178 (cond (outside-call (setf (functional-kind clambda) :assignment)
1179 (let-convert clambda outside-call)
1180 (when outside-non-tail-call
1181 (reoptimize-call outside-non-tail-call))
1183 (t (delete-lambda clambda)