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 (sset-adjoin 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-name
230 'xep (leaf-debug-name fun)))))
231 (setf (functional-kind res) :external
232 (leaf-ever-used res) t
233 (functional-entry-fun res) fun
234 (functional-entry-fun fun) res
235 (component-reanalyze *current-component*) t)
236 (reoptimize-component *current-component* :maybe)
239 (locall-analyze-fun-1 fun))
241 (dolist (ep (optional-dispatch-entry-points fun))
242 (locall-analyze-fun-1 (force ep)))
243 (when (optional-dispatch-more-entry fun)
244 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
247 ;;; Notice a REF that is not in a local-call context. If the REF is
248 ;;; already to an XEP, then do nothing, otherwise change it to the
249 ;;; XEP, making an XEP if necessary.
251 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
252 ;;; it as though it was not an XEP reference (i.e. leave it alone).
253 (defun reference-entry-point (ref)
254 (declare (type ref ref))
255 (let ((fun (ref-leaf ref)))
256 (unless (or (xep-p fun)
257 (member (functional-kind fun) '(:escape :cleanup)))
258 (change-ref-leaf ref (or (functional-entry-fun fun)
261 ;;; Attempt to convert all references to FUN to local calls. The
262 ;;; reference must be the function for a call, and the function lvar
263 ;;; must be used only once, since otherwise we cannot be sure what
264 ;;; function is to be called. The call lvar would be multiply used if
265 ;;; there is hairy stuff such as conditionals in the expression that
266 ;;; computes the function.
268 ;;; If we cannot convert a reference, then we mark the referenced
269 ;;; function as an entry-point, creating a new XEP if necessary. We
270 ;;; don't try to convert calls that are in error (:ERROR kind.)
272 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
273 ;;; can force analysis of newly introduced calls. Note that we don't
274 ;;; do LET conversion here.
275 (defun locall-analyze-fun-1 (fun)
276 (declare (type functional fun))
277 (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)
293 (reference-entry-point ref)
294 (setq local-p nil))))))
295 (when local-p (note-local-functional fun)))
299 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
300 ;;; calls into local calls when it is legal. We also attempt to
301 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
302 ;;; deletion of a function reference, but functions that start out
303 ;;; eligible for conversion must be noticed sometime.
305 ;;; Note that there is a lot of action going on behind the scenes
306 ;;; here, triggered by reference deletion. In particular, the
307 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
308 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
309 ;;; the COMPONENT-LAMBDAS when it is. Also, the
310 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
311 ;;; it is not updated when we delete functions, etc. Only
312 ;;; COMPONENT-LAMBDAS is updated.
314 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
315 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
317 (defun locall-analyze-component (component)
318 (declare (type component component))
319 (aver-live-component component)
321 (let* ((new-functional (pop (component-new-functionals component)))
322 (functional (or new-functional
323 (pop (component-reanalyze-functionals component)))))
326 (let ((kind (functional-kind functional)))
327 (cond ((or (functional-somewhat-letlike-p functional)
328 (memq kind '(:deleted :zombie)))
329 (values)) ; nothing to do
330 ((and (null (leaf-refs functional)) (eq kind nil)
331 (not (functional-entry-fun functional)))
332 (delete-functional functional))
334 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
335 (cond ((not (lambda-p functional))
336 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
339 (new-functional ; FUNCTIONAL came from
340 ; NEW-FUNCTIONALS, hence is new.
341 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
342 (aver (not (member functional
343 (component-lambdas component))))
344 (push functional (component-lambdas component)))
345 (t ; FUNCTIONAL is old.
346 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
347 (aver (member functional (component-lambdas
349 (locall-analyze-fun-1 functional)
350 (when (lambda-p functional)
351 (maybe-let-convert functional)))))))
354 (defun locall-analyze-clambdas-until-done (clambdas)
356 (let ((did-something nil))
357 (dolist (clambda clambdas)
358 (let ((component (lambda-component clambda)))
359 ;; The original CMU CL code seemed to implicitly assume that
360 ;; COMPONENT is the only one here. Let's make that explicit.
361 (aver (= 1 (length (functional-components clambda))))
362 (aver (eql component (first (functional-components clambda))))
363 (when (or (component-new-functionals component)
364 (component-reanalyze-functionals component))
365 (setf did-something t)
366 (locall-analyze-component component))))
367 (unless did-something
371 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
372 ;;; to be in an infinite recursive loop, then change the reference to
373 ;;; reference a fresh copy. We return whichever function we decide to
375 (defun maybe-expand-local-inline (original-functional ref call)
376 (if (and (policy call
377 (and (>= speed space)
378 (>= speed compilation-speed)))
379 (not (eq (functional-kind (node-home-lambda call)) :external))
380 (inline-expansion-ok call))
381 (let* ((end (component-last-block (node-component call)))
382 (pred (block-prev end)))
383 (multiple-value-bind (losing-local-object converted-lambda)
384 (catch 'locall-already-let-converted
385 (with-ir1-environment-from-node call
386 (let ((*lexenv* (functional-lexenv original-functional)))
389 (functional-inline-expansion original-functional)
390 :debug-name (debug-name 'local-inline
392 original-functional)))))))
393 (cond (losing-local-object
394 (if (functional-p losing-local-object)
395 (let ((*compiler-error-context* call))
396 (compiler-notify "couldn't inline expand because expansion ~
397 calls this LET-converted local function:~
399 (leaf-debug-name losing-local-object)))
400 (let ((*compiler-error-context* call))
401 (compiler-notify "implementation limitation: couldn't inline ~
402 expand because expansion refers to ~
403 the optimized away object ~S."
404 losing-local-object)))
405 (loop for block = (block-next pred) then (block-next block)
407 do (setf (block-delete-p block) t))
408 (loop for block = (block-next pred) then (block-next block)
410 do (delete-block block t))
413 (change-ref-leaf ref converted-lambda)
415 original-functional))
417 ;;; Dispatch to the appropriate function to attempt to convert a call.
418 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
419 ;;; optimization as well as in local call analysis. If the call is is
420 ;;; already :LOCAL, we do nothing. If the call is already scheduled
421 ;;; for deletion, also do nothing (in addition to saving time, this
422 ;;; also avoids some problems with optimizing collections of functions
423 ;;; that are partially deleted.)
425 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
426 ;;; called on a :INITIAL component, we don't care whether the caller
427 ;;; and callee are in the same component. Afterward, we must stick
428 ;;; with whatever component division we have chosen.
430 ;;; Before attempting to convert a call, we see whether the function
431 ;;; is supposed to be inline expanded. Call conversion proceeds as
432 ;;; before after any expansion.
434 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
435 ;;; warnings will get the right context.
436 (defun convert-call-if-possible (ref call)
437 (declare (type ref ref) (type basic-combination call))
438 (let* ((block (node-block call))
439 (component (block-component block))
440 (original-fun (ref-leaf ref)))
441 (aver (functional-p original-fun))
442 (unless (or (member (basic-combination-kind call) '(:local :error))
443 (node-to-be-deleted-p call)
444 (member (functional-kind original-fun)
445 '(:toplevel-xep :deleted))
446 (not (or (eq (component-kind component) :initial)
449 (lambda-bind (main-entry original-fun))))
451 (let ((fun (if (xep-p original-fun)
452 (functional-entry-fun original-fun)
454 (*compiler-error-context* call))
456 (when (and (eq (functional-inlinep fun) :inline)
457 (rest (leaf-refs original-fun)))
458 (setq fun (maybe-expand-local-inline fun ref call)))
460 (aver (member (functional-kind fun)
461 '(nil :escape :cleanup :optional)))
462 (cond ((mv-combination-p call)
463 (convert-mv-call ref call fun))
465 (convert-lambda-call ref call fun))
467 (convert-hairy-call ref call fun))))))
471 ;;; Attempt to convert a multiple-value call. The only interesting
472 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
473 ;;; exactly one reference and no XEP, and is called with one values
476 ;;; We change the call to be to the last optional entry point and
477 ;;; change the call to be local. Due to our preconditions, the call
478 ;;; should eventually be converted to a let, but we can't do that now,
479 ;;; since there may be stray references to the e-p lambda due to
480 ;;; optional defaulting code.
482 ;;; We also use variable types for the called function to construct an
483 ;;; assertion for the values lvar.
485 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
486 (defun convert-mv-call (ref call fun)
487 (declare (type ref ref) (type mv-combination call) (type functional fun))
488 (when (and (looks-like-an-mv-bind fun)
489 (singleton-p (leaf-refs fun))
490 (singleton-p (basic-combination-args call)))
491 (let* ((*current-component* (node-component ref))
492 (ep (optional-dispatch-entry-point-fun
493 fun (optional-dispatch-max-args fun))))
494 (when (null (leaf-refs ep))
495 (aver (= (optional-dispatch-min-args fun) 0))
496 (aver (not (functional-entry-fun fun)))
497 (setf (basic-combination-kind call) :local)
498 (sset-adjoin ep (lambda-calls-or-closes (node-home-lambda call)))
499 (merge-tail-sets call ep)
500 (change-ref-leaf ref ep)
503 (first (basic-combination-args call))
504 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
505 (lexenv-policy (node-lexenv call))))))
508 ;;; Attempt to convert a call to a lambda. If the number of args is
509 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
510 ;;; from future consideration. If the argcount is O.K. then we just
512 (defun convert-lambda-call (ref call fun)
513 (declare (type ref ref) (type combination call) (type clambda fun))
514 (let ((nargs (length (lambda-vars fun)))
515 (n-call-args (length (combination-args call))))
516 (cond ((= n-call-args nargs)
517 (convert-call ref call fun))
520 'local-argument-mismatch
522 "function called with ~R argument~:P, but wants exactly ~R"
523 :format-arguments (list n-call-args nargs))
524 (setf (basic-combination-kind call) :error)))))
526 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
528 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
529 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
530 ;;; a call to the correct entry point. If &KEY args are supplied, then
531 ;;; dispatch to a subfunction. We don't convert calls to functions
532 ;;; that have a &MORE (or &REST) arg.
533 (defun convert-hairy-call (ref call fun)
534 (declare (type ref ref) (type combination call)
535 (type optional-dispatch fun))
536 (let ((min-args (optional-dispatch-min-args fun))
537 (max-args (optional-dispatch-max-args fun))
538 (call-args (length (combination-args call))))
539 (cond ((< call-args min-args)
541 'local-argument-mismatch
543 "function called with ~R argument~:P, but wants at least ~R"
544 :format-arguments (list call-args min-args))
545 (setf (basic-combination-kind call) :error))
546 ((<= call-args max-args)
547 (convert-call ref call
548 (let ((*current-component* (node-component ref)))
549 (optional-dispatch-entry-point-fun
550 fun (- call-args min-args)))))
551 ((optional-dispatch-more-entry fun)
552 (convert-more-call ref call fun))
555 'local-argument-mismatch
557 "function called with ~R argument~:P, but wants at most ~R"
559 (list call-args max-args))
560 (setf (basic-combination-kind call) :error))))
563 ;;; This function is used to convert a call to an entry point when
564 ;;; complex transformations need to be done on the original arguments.
565 ;;; ENTRY is the entry point function that we are calling. VARS is a
566 ;;; list of variable names which are bound to the original call
567 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
568 ;;; is the list of arguments to the entry point function.
570 ;;; In order to avoid gruesome graph grovelling, we introduce a new
571 ;;; function that rearranges the arguments and calls the entry point.
572 ;;; We analyze the new function and the entry point immediately so
573 ;;; that everything gets converted during the single pass.
574 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
575 (declare (list vars ignores args) (type ref ref) (type combination call)
576 (type clambda entry))
578 (with-ir1-environment-from-node call
581 (declare (ignorable ,@ignores))
582 (%funcall ,entry ,@args))
583 :debug-name (debug-name 'hairy-function-entry
585 (basic-combination-fun call)))))))
586 (convert-call ref call new-fun)
587 (dolist (ref (leaf-refs entry))
588 (convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
590 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
591 ;;; function into a local call to the MAIN-ENTRY.
593 ;;; First we verify that all keywords are constant and legal. If there
594 ;;; aren't, then we warn the user and don't attempt to convert the call.
596 ;;; We massage the supplied &KEY arguments into the order expected
597 ;;; by the main entry. This is done by binding all the arguments to
598 ;;; the keyword call to variables in the introduced lambda, then
599 ;;; passing these values variables in the correct order when calling
600 ;;; the main entry. Unused arguments (such as the keywords themselves)
601 ;;; are discarded simply by not passing them along.
603 ;;; If there is a &REST arg, then we bundle up the args and pass them
605 (defun convert-more-call (ref call fun)
606 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
607 (let* ((max (optional-dispatch-max-args fun))
608 (arglist (optional-dispatch-arglist fun))
609 (args (combination-args call))
610 (more (nthcdr max args))
611 (flame (policy call (or (> speed inhibit-warnings)
612 (> space inhibit-warnings))))
616 (temps (make-gensym-list max))
617 (more-temps (make-gensym-list (length more))))
622 (dolist (var arglist)
623 (let ((info (lambda-var-arg-info var)))
625 (ecase (arg-info-kind info)
629 ((:more-context :more-count)
630 (compiler-warn "can't local-call functions with &MORE args")
631 (setf (basic-combination-kind call) :error)
632 (return-from convert-more-call))))))
634 (when (optional-dispatch-keyp fun)
635 (when (oddp (length more))
636 (compiler-warn "function called with odd number of ~
637 arguments in keyword portion")
638 (setf (basic-combination-kind call) :error)
639 (return-from convert-more-call))
641 (do ((key more (cddr key))
642 (temp more-temps (cddr temp)))
644 (let ((lvar (first key)))
645 (unless (constant-lvar-p lvar)
647 (compiler-notify "non-constant keyword in keyword call"))
648 (setf (basic-combination-kind call) :error)
649 (return-from convert-more-call))
651 (let ((name (lvar-value lvar))
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 (if (member var (supplied) :key #'car)
673 (supplied (cons var val)))
676 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
677 (compiler-warn "function called with unknown argument keyword ~S"
679 (setf (basic-combination-kind call) :error)
680 (return-from convert-more-call)))
682 (collect ((call-args))
683 (do ((var arglist (cdr var))
684 (temp temps (cdr temp)))
686 (let ((info (lambda-var-arg-info (car var))))
688 (ecase (arg-info-kind info)
690 (call-args (car temp))
691 (when (arg-info-supplied-p info)
694 (call-args `(list ,@more-temps))
698 (call-args (car temp)))))
700 (dolist (var (key-vars))
701 (let ((info (lambda-var-arg-info var))
702 (temp (cdr (assoc var (supplied)))))
705 (call-args (arg-info-default info)))
706 (when (arg-info-supplied-p info)
707 (call-args (not (null temp))))))
709 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
710 (append temps more-temps)
711 (ignores) (call-args)))))
717 ;;;; Converting to a LET has differing significance to various parts
718 ;;;; of the compiler:
719 ;;;; -- The body of a LET is spliced in immediately after the
720 ;;;; corresponding combination node, making the control transfer
721 ;;;; explicit and allowing LETs to be mashed together into a single
722 ;;;; block. The value of the LET is delivered directly to the
723 ;;;; original lvar for the call, eliminating the need to
724 ;;;; propagate information from the dummy result lvar.
725 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
726 ;;;; that there is only one expression that the variable can be bound
727 ;;;; to, and this is easily substituted for.
728 ;;;; -- LETs are interesting to environment analysis and to the back
729 ;;;; end because in most ways a LET can be considered to be "the
730 ;;;; same function" as its home function.
731 ;;;; -- LET conversion has dynamic scope implications, since control
732 ;;;; transfers within the same environment are local. In a local
733 ;;;; control transfer, cleanup code must be emitted to remove
734 ;;;; dynamic bindings that are no longer in effect.
736 ;;; Set up the control transfer to the called CLAMBDA. We split the
737 ;;; call block immediately after the call, and link the head of
738 ;;; CLAMBDA to the call block. The successor block after splitting
739 ;;; (where we return to) is returned.
741 ;;; If the lambda is is a different component than the call, then we
742 ;;; call JOIN-COMPONENTS. This only happens in block compilation
743 ;;; before FIND-INITIAL-DFO.
744 (defun insert-let-body (clambda call)
745 (declare (type clambda clambda) (type basic-combination call))
746 (let* ((call-block (node-block call))
747 (bind-block (node-block (lambda-bind clambda)))
748 (component (block-component call-block)))
749 (aver-live-component component)
750 (let ((clambda-component (block-component bind-block)))
751 (unless (eq clambda-component component)
752 (aver (eq (component-kind component) :initial))
753 (join-components component clambda-component)))
754 (let ((*current-component* component))
755 (node-ends-block call))
756 (destructuring-bind (next-block)
757 (block-succ call-block)
758 (unlink-blocks call-block next-block)
759 (link-blocks call-block bind-block)
762 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
763 ;;; same set as; but leave CLAMBDA with a valid tail set value of
764 ;;; its own, for the benefit of code which might try to pull
765 ;;; something out of it (e.g. return type).
766 (defun depart-from-tail-set (clambda)
767 ;; Until sbcl-0.pre7.37.flaky5.2, we did
768 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
769 ;; (SETF (TAIL-SET-FUNS TAILS)
770 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
771 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
772 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
773 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
774 ;; inconsistent, and perhaps unsafe, for us to think we're in the
775 ;; tail set. Unfortunately..
777 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
778 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
779 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
780 ;; which you called in order to get the address of an external LAMBDA):
781 ;; the external function was defined in terms of internal function,
782 ;; which was LET-converted, and then things blew up downstream when
783 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
784 ;; the now-NILed-out TAIL-SET. So..
786 ;; To deal with this problem, we no longer NIL out
787 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
788 ;; * If we're the only function in TAIL-SET-FUNS, it should
789 ;; be safe to leave ourself linked to it, and it to you.
790 ;; * If there are other functions in TAIL-SET-FUNS, then we're
791 ;; afraid of future optimizations on those functions causing
792 ;; the TAIL-SET object no longer to be valid to describe our
793 ;; return value. Thus, we delete ourselves from that object;
794 ;; but we save a newly-allocated tail-set, derived from the old
795 ;; one, for ourselves, for the use of later code (e.g.
796 ;; FINALIZE-XEP-DEFINITION) which might want to
797 ;; know about our return type.
798 (let* ((old-tail-set (lambda-tail-set clambda))
799 (old-tail-set-funs (tail-set-funs old-tail-set)))
800 (unless (= 1 (length old-tail-set-funs))
801 (setf (tail-set-funs old-tail-set)
802 (delete clambda old-tail-set-funs))
803 (let ((new-tail-set (copy-tail-set old-tail-set)))
804 (setf (lambda-tail-set clambda) new-tail-set
805 (tail-set-funs new-tail-set) (list clambda)))))
806 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
807 ;; remain valid in this case, so we nuke it on the theory that
808 ;; missing information tends to be less dangerous than incorrect
810 (setf (tail-set-info (lambda-tail-set clambda)) nil))
812 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
813 ;;; its LETs to LETs for the CALL's home function. We merge the calls
814 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
815 ;;; in the process. We also merge the ENTRIES.
817 ;;; We also unlink the function head from the component head and set
818 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
820 (defun merge-lets (clambda call)
822 (declare (type clambda clambda) (type basic-combination call))
824 (let ((component (node-component call)))
825 (unlink-blocks (component-head component) (lambda-block clambda))
826 (setf (component-lambdas component)
827 (delete clambda (component-lambdas component)))
828 (setf (component-reanalyze component) t))
829 (setf (lambda-call-lexenv clambda) (node-lexenv call))
831 (depart-from-tail-set clambda)
833 (let* ((home (node-home-lambda call))
834 (home-physenv (lambda-physenv home))
835 (physenv (lambda-physenv clambda)))
837 (aver (not (eq home clambda)))
839 ;; CLAMBDA belongs to HOME now.
840 (push clambda (lambda-lets home))
841 (setf (lambda-home clambda) home)
842 (setf (lambda-physenv clambda) home-physenv)
845 (setf (physenv-nlx-info home-physenv)
846 (nconc (physenv-nlx-info physenv)
847 (physenv-nlx-info home-physenv))))
849 ;; All of CLAMBDA's LETs belong to HOME now.
850 (let ((lets (lambda-lets clambda)))
852 (setf (lambda-home let) home)
853 (setf (lambda-physenv let) home-physenv))
854 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
855 ;; CLAMBDA no longer has an independent existence as an entity
857 (setf (lambda-lets clambda) nil)
859 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
861 (sset-union (lambda-calls-or-closes home)
862 (lambda-calls-or-closes clambda))
863 (sset-delete clambda (lambda-calls-or-closes home))
864 ;; CLAMBDA no longer has an independent existence as an entity
865 ;; which calls things or has DFO dependencies.
866 (setf (lambda-calls-or-closes clambda) nil)
868 ;; All of CLAMBDA's ENTRIES belong to HOME now.
869 (setf (lambda-entries home)
870 (nconc (lambda-entries clambda)
871 (lambda-entries home)))
872 ;; CLAMBDA no longer has an independent existence as an entity
874 (setf (lambda-entries clambda) nil))
878 ;;; Handle the value semantics of LET conversion. Delete FUN's return
879 ;;; node, and change the control flow to transfer to NEXT-BLOCK
880 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
881 (defun move-return-uses (fun call next-block)
882 (declare (type clambda fun) (type basic-combination call)
883 (type cblock next-block))
884 (let* ((return (lambda-return fun))
886 (ensure-block-start (node-prev return))
887 (node-block return))))
888 (unlink-blocks return-block
889 (component-tail (block-component return-block)))
890 (link-blocks return-block next-block)
892 (delete-return return)
893 (let ((result (return-result return))
894 (lvar (if (node-tail-p call)
895 (return-result (lambda-return (node-home-lambda call)))
897 (call-type (node-derived-type call)))
898 (unless (eq call-type *wild-type*)
899 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
900 (do-uses (use result)
901 (derive-node-type use call-type)))
902 (substitute-lvar-uses lvar result
903 (and lvar (eq (lvar-uses lvar) call)))))
906 ;;; We are converting FUN to be a LET when the call is in a non-tail
907 ;;; position. Any previously tail calls in FUN are no longer tail
908 ;;; calls, and must be restored to normal calls which transfer to
909 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
910 ;;; the RETURN-RESULT, because the return might have been deleted (if
911 ;;; all calls were TR.)
912 (defun unconvert-tail-calls (fun call next-block)
913 (do-sset-elements (called (lambda-calls-or-closes fun))
914 (when (lambda-p called)
915 (dolist (ref (leaf-refs called))
916 (let ((this-call (node-dest ref)))
918 (node-tail-p this-call)
919 (eq (node-home-lambda this-call) fun))
920 (setf (node-tail-p this-call) nil)
921 (ecase (functional-kind called)
922 ((nil :cleanup :optional)
923 (let ((block (node-block this-call))
924 (lvar (node-lvar call)))
925 (unlink-blocks block (first (block-succ block)))
926 (link-blocks block next-block)
927 (aver (not (node-lvar this-call)))
928 (add-lvar-use this-call lvar)))
930 ;; The called function might be an assignment in the
931 ;; case where we are currently converting that function.
932 ;; In steady-state, assignments never appear as a called
935 (aver (eq called fun)))))))))
938 ;;; Deal with returning from a LET or assignment that we are
939 ;;; converting. FUN is the function we are calling, CALL is a call to
940 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
941 ;;; NULL if call is a tail call.
943 ;;; If the call is not a tail call, then we must do
944 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
945 ;;; its value out of the enclosing non-let function. When call is
946 ;;; non-TR, we must convert it back to an ordinary local call, since
947 ;;; the value must be delivered to the receiver of CALL's value.
949 ;;; We do different things depending on whether the caller and callee
950 ;;; have returns left:
952 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
953 ;;; Either the function doesn't return, or all returns are via
954 ;;; tail-recursive local calls.
955 ;;; -- If CALL is a non-tail call, or if both have returns, then
956 ;;; we delete the callee's return, move its uses to the call's
957 ;;; result lvar, and transfer control to the appropriate
959 ;;; -- If the callee has a return, but the caller doesn't, then we
960 ;;; move the return to the caller.
961 (defun move-return-stuff (fun call next-block)
962 (declare (type clambda fun) (type basic-combination call)
963 (type (or cblock null) next-block))
965 (unconvert-tail-calls fun call next-block))
966 (let* ((return (lambda-return fun))
967 (call-fun (node-home-lambda call))
968 (call-return (lambda-return call-fun)))
969 (when (and call-return
970 (block-delete-p (node-block call-return)))
971 (delete-return call-return)
972 (unlink-node call-return)
973 (setq call-return nil))
975 ((or next-block call-return)
976 (unless (block-delete-p (node-block return))
978 (ensure-block-start (node-prev call-return))
979 (setq next-block (node-block call-return)))
980 (move-return-uses fun call next-block)))
982 (aver (node-tail-p call))
983 (setf (lambda-return call-fun) return)
984 (setf (return-lambda return) call-fun)
985 (setf (lambda-return fun) nil))))
986 (%delete-lvar-use call) ; LET call does not have value semantics
989 ;;; Actually do LET conversion. We call subfunctions to do most of the
990 ;;; work. We do REOPTIMIZE-LVAR on the args and CALL's lvar so that
991 ;;; LET-specific IR1 optimizations get a chance. We blow away any
992 ;;; entry for the function in *FREE-FUNS* so that nobody will create
993 ;;; new references to it.
994 (defun let-convert (fun call)
995 (declare (type clambda fun) (type basic-combination call))
996 (let* ((next-block (insert-let-body fun call))
997 (next-block (if (node-tail-p call)
1000 (move-return-stuff fun call next-block)
1001 (merge-lets fun call)
1002 (setf (node-tail-p call) nil)
1003 ;; If CALL has a derive type NIL, it means that "its return" is
1004 ;; unreachable, but the next BIND is still reachable; in order to
1005 ;; not confuse MAYBE-TERMINATE-BLOCK...
1006 (setf (node-derived-type call) *wild-type*)))
1008 ;;; Reoptimize all of CALL's args and its result.
1009 (defun reoptimize-call (call)
1010 (declare (type basic-combination call))
1011 (dolist (arg (basic-combination-args call))
1013 (reoptimize-lvar arg)))
1014 (reoptimize-lvar (node-lvar call))
1017 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
1019 (defun declarations-suppress-let-conversion-p (clambda)
1020 ;; From the user's point of view, LET-converting something that
1021 ;; has a name is inlining it. (The user can't see what we're doing
1022 ;; with anonymous things, and suppressing inlining
1023 ;; for such things can easily give Python acute indigestion, so
1025 (when (leaf-has-source-name-p clambda)
1026 ;; ANSI requires that explicit NOTINLINE be respected.
1027 (or (eq (lambda-inlinep clambda) :notinline)
1028 ;; If (= LET-CONVERSION 0) we can guess that inlining
1029 ;; generally won't be appreciated, but if the user
1030 ;; specifically requests inlining, that takes precedence over
1031 ;; our general guess.
1032 (and (policy clambda (= let-conversion 0))
1033 (not (eq (lambda-inlinep clambda) :inline))))))
1035 ;;; We also don't convert calls to named functions which appear in the
1036 ;;; initial component, delaying this until optimization. This
1037 ;;; minimizes the likelihood that we will LET-convert a function which
1038 ;;; may have references added due to later local inline expansion.
1039 (defun ok-initial-convert-p (fun)
1040 (not (and (leaf-has-source-name-p fun)
1041 (or (declarations-suppress-let-conversion-p fun)
1042 (eq (component-kind (lambda-component fun))
1045 ;;; This function is called when there is some reason to believe that
1046 ;;; CLAMBDA might be converted into a LET. This is done after local
1047 ;;; call analysis, and also when a reference is deleted. We return
1048 ;;; true if we converted.
1049 (defun maybe-let-convert (clambda)
1050 (declare (type clambda clambda))
1051 (unless (or (declarations-suppress-let-conversion-p clambda)
1052 (functional-has-external-references-p clambda))
1053 ;; We only convert to a LET when the function is a normal local
1054 ;; function, has no XEP, and is referenced in exactly one local
1055 ;; call. Conversion is also inhibited if the only reference is in
1056 ;; a block about to be deleted.
1058 ;; These rules limiting LET conversion may seem unnecessarily
1059 ;; restrictive, since there are some cases where we could do the
1060 ;; return with a jump that don't satisfy these requirements. The
1061 ;; reason for doing things this way is that it makes the concept
1062 ;; of a LET much more useful at the level of IR1 semantics. The
1063 ;; :ASSIGNMENT function kind provides another way to optimize
1064 ;; calls to single-return/multiple call functions.
1066 ;; We don't attempt to convert calls to functions that have an
1067 ;; XEP, since we might be embarrassed later when we want to
1068 ;; convert a newly discovered local call. Also, see
1069 ;; OK-INITIAL-CONVERT-P.
1070 (let ((refs (leaf-refs clambda)))
1073 (memq (functional-kind clambda) '(nil :assignment))
1074 (not (functional-entry-fun clambda)))
1075 (binding* ((ref (first refs))
1076 (ref-lvar (node-lvar ref) :exit-if-null)
1077 (dest (lvar-dest ref-lvar)))
1078 (when (and (basic-combination-p dest)
1079 (eq (basic-combination-fun dest) ref-lvar)
1080 (eq (basic-combination-kind dest) :local)
1081 (not (node-to-be-deleted-p dest))
1082 (not (block-delete-p (lambda-block clambda)))
1083 (cond ((ok-initial-convert-p clambda) t)
1085 (reoptimize-lvar ref-lvar)
1087 (when (eq clambda (node-home-lambda dest))
1088 (delete-lambda clambda)
1089 (return-from maybe-let-convert nil))
1090 (unless (eq (functional-kind clambda) :assignment)
1091 (let-convert clambda dest))
1092 (reoptimize-call dest)
1093 (setf (functional-kind clambda)
1094 (if (mv-combination-p dest) :mv-let :let))))
1097 ;;;; tail local calls and assignments
1099 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1100 ;;; they definitely won't generate any cleanup code. Currently we
1101 ;;; recognize lexical entry points that are only used locally (if at
1103 (defun only-harmless-cleanups (block1 block2)
1104 (declare (type cblock block1 block2))
1105 (or (eq block1 block2)
1106 (let ((cleanup2 (block-start-cleanup block2)))
1107 (do ((cleanup (block-end-cleanup block1)
1108 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1109 ((eq cleanup cleanup2) t)
1110 (case (cleanup-kind cleanup)
1112 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1114 (t (return nil)))))))
1116 ;;; If a potentially TR local call really is TR, then convert it to
1117 ;;; jump directly to the called function. We also call
1118 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1119 ;;; tail-convert. The second is the value of M-C-T-A.
1120 (defun maybe-convert-tail-local-call (call)
1121 (declare (type combination call))
1122 (let ((return (lvar-dest (node-lvar call)))
1123 (fun (combination-lambda call)))
1124 (aver (return-p return))
1125 (when (and (not (node-tail-p call)) ; otherwise already converted
1126 ;; this is a tail call
1127 (immediately-used-p (return-result return) call)
1128 (only-harmless-cleanups (node-block call)
1129 (node-block return))
1130 ;; If the call is in an XEP, we might decide to make it
1131 ;; non-tail so that we can use known return inside the
1133 (not (eq (functional-kind (node-home-lambda call))
1135 (not (block-delete-p (lambda-block fun))))
1136 (node-ends-block call)
1137 (let ((block (node-block call)))
1138 (setf (node-tail-p call) t)
1139 (unlink-blocks block (first (block-succ block)))
1140 (link-blocks block (lambda-block fun))
1141 (delete-lvar-use call)
1142 (values t (maybe-convert-to-assignment fun))))))
1144 ;;; This is called when we believe it might make sense to convert
1145 ;;; CLAMBDA to an assignment. All this function really does is
1146 ;;; determine when a function with more than one call can still be
1147 ;;; combined with the calling function's environment. We can convert
1149 ;;; -- The function is a normal, non-entry function, and
1150 ;;; -- Except for one call, all calls must be tail recursive calls
1151 ;;; in the called function (i.e. are self-recursive tail calls)
1152 ;;; -- OK-INITIAL-CONVERT-P is true.
1154 ;;; There may be one outside call, and it need not be tail-recursive.
1155 ;;; Since all tail local calls have already been converted to direct
1156 ;;; transfers, the only control semantics needed are to splice in the
1157 ;;; body at the non-tail call. If there is no non-tail call, then we
1158 ;;; need only merge the environments. Both cases are handled by
1161 ;;; ### It would actually be possible to allow any number of outside
1162 ;;; calls as long as they all return to the same place (i.e. have the
1163 ;;; same conceptual continuation.) A special case of this would be
1164 ;;; when all of the outside calls are tail recursive.
1165 (defun maybe-convert-to-assignment (clambda)
1166 (declare (type clambda clambda))
1167 (when (and (not (functional-kind clambda))
1168 (not (functional-entry-fun clambda))
1169 (not (functional-has-external-references-p clambda)))
1170 (let ((outside-non-tail-call nil)
1172 (when (and (dolist (ref (leaf-refs clambda) t)
1173 (let ((dest (node-dest ref)))
1174 (when (or (not dest)
1175 (block-delete-p (node-block dest)))
1177 (let ((home (node-home-lambda ref)))
1178 (unless (eq home clambda)
1181 (setq outside-call dest))
1182 (unless (node-tail-p dest)
1183 (when (or outside-non-tail-call (eq home clambda))
1185 (setq outside-non-tail-call dest)))))
1186 (ok-initial-convert-p clambda))
1187 (cond (outside-call (setf (functional-kind clambda) :assignment)
1188 (let-convert clambda outside-call)
1189 (when outside-non-tail-call
1190 (reoptimize-call outside-non-tail-call))
1192 (t (delete-lambda clambda)