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