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 ;;; This function handles merging the tail sets if CALL is potentially
47 ;;; tail-recursive, and is a call to a function with a different
48 ;;; TAIL-SET than CALL's FUN. This must be called whenever we alter
49 ;;; IR1 so as to place a local call in what might be a tail-recursive
50 ;;; context. Note that any call which returns its value to a RETURN is
51 ;;; considered potentially tail-recursive, since any implicit MV-PROG1
52 ;;; might be optimized away.
54 ;;; We destructively modify the set for the calling function to
55 ;;; represent both, and then change all the functions in callee's set
56 ;;; to reference the first. If we do merge, we reoptimize the
57 ;;; RETURN-RESULT lvar to cause IR1-OPTIMIZE-RETURN to recompute the
59 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
60 (declare (type basic-combination call) (type clambda new-fun))
61 (let ((return (node-dest call)))
62 (when (return-p return)
63 (let ((call-set (lambda-tail-set (node-home-lambda call)))
64 (fun-set (lambda-tail-set new-fun)))
65 (unless (eq call-set fun-set)
66 (let ((funs (tail-set-funs fun-set)))
68 (setf (lambda-tail-set fun) call-set))
69 (setf (tail-set-funs call-set)
70 (nconc (tail-set-funs call-set) funs)))
71 (reoptimize-lvar (return-result return))
74 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
75 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
76 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
77 ;;; the function in the REF node with the new function.
79 ;;; We change the REF last, since changing the reference can trigger
80 ;;; LET conversion of the new function, but will only do so if the
81 ;;; call is local. Note that the replacement may trigger LET
82 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
83 ;;; with NEW-FUN before the substitution, since after the substitution
84 ;;; (and LET conversion), the call may no longer be recognizable as
86 (defun convert-call (ref call fun)
87 (declare (type ref ref) (type combination call) (type clambda fun))
88 (propagate-to-args call fun)
89 (setf (basic-combination-kind call) :local)
90 (unless (call-full-like-p call)
91 (dolist (arg (basic-combination-args call))
93 (flush-lvar-externally-checkable-type arg))))
94 (pushnew fun (lambda-calls-or-closes (node-home-lambda call)))
95 (merge-tail-sets call fun)
96 (change-ref-leaf ref fun)
99 ;;;; external entry point creation
101 ;;; Return a LAMBDA form that can be used as the definition of the XEP
104 ;;; If FUN is a LAMBDA, then we check the number of arguments
105 ;;; (conditional on policy) and call FUN with all the arguments.
107 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
108 ;;; of supplied arguments by doing do an = test for each entry-point,
109 ;;; calling the entry with the appropriate prefix of the passed
112 ;;; If there is a &MORE arg, then there are a couple of optimizations
113 ;;; that we make (more for space than anything else):
114 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
115 ;;; no argument count error is possible.
116 ;;; -- We can omit the = clause for the last entry-point, allowing the
117 ;;; case of 0 more args to fall through to the more entry.
119 ;;; We don't bother to policy conditionalize wrong arg errors in
120 ;;; optional dispatches, since the additional overhead is negligible
121 ;;; compared to the cost of everything else going on.
123 ;;; Note that if policy indicates it, argument type declarations in
124 ;;; FUN will be verified. Since nothing is known about the type of the
125 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
126 ;;; are passed to the actual function.
127 (defun make-xep-lambda-expression (fun)
128 (declare (type functional fun))
131 (let ((nargs (length (lambda-vars fun)))
132 (n-supplied (gensym))
133 (temps (make-gensym-list (length (lambda-vars fun)))))
134 `(lambda (,n-supplied ,@temps)
135 (declare (type index ,n-supplied))
136 ,(if (policy *lexenv* (zerop verify-arg-count))
137 `(declare (ignore ,n-supplied))
138 `(%verify-arg-count ,n-supplied ,nargs))
140 (declare (optimize (merge-tail-calls 3)))
141 (%funcall ,fun ,@temps)))))
143 (let* ((min (optional-dispatch-min-args fun))
144 (max (optional-dispatch-max-args fun))
145 (more (optional-dispatch-more-entry fun))
146 (n-supplied (gensym))
147 (temps (make-gensym-list max)))
149 ;; Force convertion of all entries
150 (optional-dispatch-entry-point-fun fun 0)
151 (loop for ep in (optional-dispatch-entry-points fun)
153 do (entries `((= ,n-supplied ,n)
154 (%funcall ,(force ep) ,@(subseq temps 0 n)))))
155 `(lambda (,n-supplied ,@temps)
156 ;; FIXME: Make sure that INDEX type distinguishes between
157 ;; target and host. (Probably just make the SB!XC:DEFTYPE
158 ;; different from CL:DEFTYPE.)
159 (declare (type index ,n-supplied))
161 ,@(if more (butlast (entries)) (entries))
163 `((,(if (zerop min) t `(>= ,n-supplied ,max))
164 ,(let ((n-context (gensym))
166 `(multiple-value-bind (,n-context ,n-count)
167 (%more-arg-context ,n-supplied ,max)
169 (declare (optimize (merge-tail-calls 3)))
170 (%funcall ,more ,@temps ,n-context ,n-count)))))))
172 (%arg-count-error ,n-supplied)))))))))
174 ;;; Make an external entry point (XEP) for FUN and return it. We
175 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
176 ;;; then associate this lambda with FUN as its XEP. After the
177 ;;; conversion, we iterate over the function's associated lambdas,
178 ;;; redoing local call analysis so that the XEP calls will get
181 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
182 ;;; discover an XEP after the initial local call analyze pass.
183 (defun make-xep (fun)
184 (declare (type functional fun))
185 (aver (null (functional-entry-fun fun)))
186 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
187 (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
188 :debug-name (debug-namify
190 (leaf-debug-name fun)))))
191 (setf (functional-kind res) :external
192 (leaf-ever-used res) t
193 (functional-entry-fun res) fun
194 (functional-entry-fun fun) res
195 (component-reanalyze *current-component*) t
196 (component-reoptimize *current-component*) t)
199 (locall-analyze-fun-1 fun))
201 (dolist (ep (optional-dispatch-entry-points fun))
202 (locall-analyze-fun-1 (force ep)))
203 (when (optional-dispatch-more-entry fun)
204 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
207 ;;; Notice a REF that is not in a local-call context. If the REF is
208 ;;; already to an XEP, then do nothing, otherwise change it to the
209 ;;; XEP, making an XEP if necessary.
211 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
212 ;;; it as though it was not an XEP reference (i.e. leave it alone).
213 (defun reference-entry-point (ref)
214 (declare (type ref ref))
215 (let ((fun (ref-leaf ref)))
216 (unless (or (xep-p fun)
217 (member (functional-kind fun) '(:escape :cleanup)))
218 (change-ref-leaf ref (or (functional-entry-fun fun)
221 ;;; Attempt to convert all references to FUN to local calls. The
222 ;;; reference must be the function for a call, and the function lvar
223 ;;; must be used only once, since otherwise we cannot be sure what
224 ;;; function is to be called. The call lvar would be multiply used if
225 ;;; there is hairy stuff such as conditionals in the expression that
226 ;;; computes the function.
228 ;;; If we cannot convert a reference, then we mark the referenced
229 ;;; function as an entry-point, creating a new XEP if necessary. We
230 ;;; don't try to convert calls that are in error (:ERROR kind.)
232 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
233 ;;; can force analysis of newly introduced calls. Note that we don't
234 ;;; do LET conversion here.
235 (defun locall-analyze-fun-1 (fun)
236 (declare (type functional fun))
237 (let ((refs (leaf-refs fun))
240 (let* ((lvar (node-lvar ref))
241 (dest (when lvar (lvar-dest lvar))))
242 (cond ((and (basic-combination-p dest)
243 (eq (basic-combination-fun dest) lvar)
244 (eq (lvar-uses lvar) ref))
246 (convert-call-if-possible ref dest)
248 (unless (eq (basic-combination-kind dest) :local)
249 (reference-entry-point ref)))
251 (reference-entry-point ref))))
252 (setq first-time nil)))
256 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
257 ;;; calls into local calls when it is legal. We also attempt to
258 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
259 ;;; deletion of a function reference, but functions that start out
260 ;;; eligible for conversion must be noticed sometime.
262 ;;; Note that there is a lot of action going on behind the scenes
263 ;;; here, triggered by reference deletion. In particular, the
264 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
265 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
266 ;;; the COMPONENT-LAMBDAS when it is. Also, the
267 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
268 ;;; it is not updated when we delete functions, etc. Only
269 ;;; COMPONENT-LAMBDAS is updated.
271 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
272 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
274 (defun locall-analyze-component (component)
275 (declare (type component component))
276 (aver-live-component component)
278 (let* ((new-functional (pop (component-new-functionals component)))
279 (functional (or new-functional
280 (pop (component-reanalyze-functionals component)))))
283 (let ((kind (functional-kind functional)))
284 (cond ((or (functional-somewhat-letlike-p functional)
286 (values)) ; nothing to do
287 ((and (null (leaf-refs functional)) (eq kind nil)
288 (not (functional-entry-fun functional)))
289 (delete-functional functional))
291 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
292 (cond ((not (lambda-p functional))
293 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
296 (new-functional ; FUNCTIONAL came from
297 ; NEW-FUNCTIONALS, hence is new.
298 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
299 (aver (not (member functional
300 (component-lambdas component))))
301 (push functional (component-lambdas component)))
302 (t ; FUNCTIONAL is old.
303 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
304 (aver (member functional (component-lambdas
306 (locall-analyze-fun-1 functional)
307 (when (lambda-p functional)
308 (maybe-let-convert functional)))))))
311 (defun locall-analyze-clambdas-until-done (clambdas)
313 (let ((did-something nil))
314 (dolist (clambda clambdas)
315 (let* ((component (lambda-component clambda))
316 (*all-components* (list component)))
317 ;; The original CMU CL code seemed to implicitly assume that
318 ;; COMPONENT is the only one here. Let's make that explicit.
319 (aver (= 1 (length (functional-components clambda))))
320 (aver (eql component (first (functional-components clambda))))
321 (when (or (component-new-functionals component)
322 (component-reanalyze-functionals component))
323 (setf did-something t)
324 (locall-analyze-component component))))
325 (unless did-something
329 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
330 ;;; to be in an infinite recursive loop, then change the reference to
331 ;;; reference a fresh copy. We return whichever function we decide to
333 (defun maybe-expand-local-inline (original-functional ref call)
334 (if (and (policy call
335 (and (>= speed space)
336 (>= speed compilation-speed)))
337 (not (eq (functional-kind (node-home-lambda call)) :external))
338 (inline-expansion-ok call))
339 (let* ((end (component-last-block (node-component call)))
340 (pred (block-prev end)))
341 (multiple-value-bind (losing-local-functional converted-lambda)
342 (catch 'locall-already-let-converted
343 (with-ir1-environment-from-node call
344 (let ((*lexenv* (functional-lexenv original-functional)))
347 (functional-inline-expansion original-functional)
348 :debug-name (debug-namify
351 original-functional)))))))
352 (cond (losing-local-functional
353 (let ((*compiler-error-context* call))
354 (compiler-notify "couldn't inline expand because expansion ~
355 calls this LET-converted local function:~
357 (leaf-debug-name losing-local-functional)))
358 (loop for block = (block-next pred) then (block-next block)
360 do (setf (block-delete-p block) t))
361 (loop for block = (block-next pred) then (block-next block)
363 do (delete-block block t))
366 (change-ref-leaf ref converted-lambda)
368 original-functional))
370 ;;; Dispatch to the appropriate function to attempt to convert a call.
371 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
372 ;;; optimization as well as in local call analysis. If the call is is
373 ;;; already :LOCAL, we do nothing. If the call is already scheduled
374 ;;; for deletion, also do nothing (in addition to saving time, this
375 ;;; also avoids some problems with optimizing collections of functions
376 ;;; that are partially deleted.)
378 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
379 ;;; called on a :INITIAL component, we don't care whether the caller
380 ;;; and callee are in the same component. Afterward, we must stick
381 ;;; with whatever component division we have chosen.
383 ;;; Before attempting to convert a call, we see whether the function
384 ;;; is supposed to be inline expanded. Call conversion proceeds as
385 ;;; before after any expansion.
387 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
388 ;;; warnings will get the right context.
389 (defun convert-call-if-possible (ref call)
390 (declare (type ref ref) (type basic-combination call))
391 (let* ((block (node-block call))
392 (component (block-component block))
393 (original-fun (ref-leaf ref)))
394 (aver (functional-p original-fun))
395 (unless (or (member (basic-combination-kind call) '(:local :error))
396 (block-delete-p block)
397 (eq (functional-kind (block-home-lambda block)) :deleted)
398 (member (functional-kind original-fun)
399 '(:toplevel-xep :deleted))
400 (not (or (eq (component-kind component) :initial)
403 (lambda-bind (main-entry original-fun))))
405 (let ((fun (if (xep-p original-fun)
406 (functional-entry-fun original-fun)
408 (*compiler-error-context* call))
410 (when (and (eq (functional-inlinep fun) :inline)
411 (rest (leaf-refs original-fun)))
412 (setq fun (maybe-expand-local-inline fun ref call)))
414 (aver (member (functional-kind fun)
415 '(nil :escape :cleanup :optional)))
416 (cond ((mv-combination-p call)
417 (convert-mv-call ref call fun))
419 (convert-lambda-call ref call fun))
421 (convert-hairy-call ref call fun))))))
425 ;;; Attempt to convert a multiple-value call. The only interesting
426 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
427 ;;; exactly one reference and no XEP, and is called with one values
430 ;;; We change the call to be to the last optional entry point and
431 ;;; change the call to be local. Due to our preconditions, the call
432 ;;; should eventually be converted to a let, but we can't do that now,
433 ;;; since there may be stray references to the e-p lambda due to
434 ;;; optional defaulting code.
436 ;;; We also use variable types for the called function to construct an
437 ;;; assertion for the values lvar.
439 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
440 (defun convert-mv-call (ref call fun)
441 (declare (type ref ref) (type mv-combination call) (type functional fun))
442 (when (and (looks-like-an-mv-bind fun)
443 (not (functional-entry-fun fun))
444 (singleton-p (leaf-refs fun))
445 (singleton-p (basic-combination-args call)))
446 (let* ((*current-component* (node-component ref))
447 (ep (optional-dispatch-entry-point-fun
448 fun (optional-dispatch-max-args fun))))
449 (aver (= (optional-dispatch-min-args fun) 0))
450 (setf (basic-combination-kind call) :local)
451 (pushnew ep (lambda-calls-or-closes (node-home-lambda call)))
452 (merge-tail-sets call ep)
453 (change-ref-leaf ref ep)
456 (first (basic-combination-args call))
457 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
458 (lexenv-policy (node-lexenv call)))))
461 ;;; Attempt to convert a call to a lambda. If the number of args is
462 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
463 ;;; from future consideration. If the argcount is O.K. then we just
465 (defun convert-lambda-call (ref call fun)
466 (declare (type ref ref) (type combination call) (type clambda fun))
467 (let ((nargs (length (lambda-vars fun)))
468 (n-call-args (length (combination-args call))))
469 (cond ((= n-call-args nargs)
470 (convert-call ref call fun))
472 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
473 ;; Compiler" that calling a function with "the wrong number of
474 ;; arguments" be only a STYLE-ERROR. I think, though, that this
475 ;; should only apply when the number of arguments is inferred
476 ;; from a previous definition. If the number of arguments
477 ;; is DECLAIMed, surely calling with the wrong number is a
478 ;; real WARNING. As long as SBCL continues to use CMU CL's
479 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
480 ;; but as long as we continue to use that policy, that's the
481 ;; not our biggest problem.:-| When we fix that policy, this
482 ;; should come back into compliance. (So fix that policy!)
484 ;; FIXME, continued: Except that section "3.2.2.3 Semantic
485 ;; Constraints" says that if it's within the same file, it's
486 ;; wrong. And we're in locall.lisp here, so it's probably
487 ;; (haven't checked this..) a call to something in the same
488 ;; file. So maybe it deserves a full warning anyway.
490 "function called with ~R argument~:P, but wants exactly ~R"
492 (setf (basic-combination-kind call) :error)))))
494 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
496 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
497 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
498 ;;; a call to the correct entry point. If &KEY args are supplied, then
499 ;;; dispatch to a subfunction. We don't convert calls to functions
500 ;;; that have a &MORE (or &REST) arg.
501 (defun convert-hairy-call (ref call fun)
502 (declare (type ref ref) (type combination call)
503 (type optional-dispatch fun))
504 (let ((min-args (optional-dispatch-min-args fun))
505 (max-args (optional-dispatch-max-args fun))
506 (call-args (length (combination-args call))))
507 (cond ((< call-args min-args)
508 ;; FIXME: See FIXME note at the previous
509 ;; wrong-number-of-arguments warnings in this file.
511 "function called with ~R argument~:P, but wants at least ~R"
513 (setf (basic-combination-kind call) :error))
514 ((<= call-args max-args)
515 (convert-call ref call
516 (let ((*current-component* (node-component ref)))
517 (optional-dispatch-entry-point-fun
518 fun (- call-args min-args)))))
519 ((optional-dispatch-more-entry fun)
520 (convert-more-call ref call fun))
522 ;; FIXME: See FIXME note at the previous
523 ;; wrong-number-of-arguments warnings in this file.
525 "function called with ~R argument~:P, but wants at most ~R"
527 (setf (basic-combination-kind call) :error))))
530 ;;; This function is used to convert a call to an entry point when
531 ;;; complex transformations need to be done on the original arguments.
532 ;;; ENTRY is the entry point function that we are calling. VARS is a
533 ;;; list of variable names which are bound to the original call
534 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
535 ;;; is the list of arguments to the entry point function.
537 ;;; In order to avoid gruesome graph grovelling, we introduce a new
538 ;;; function that rearranges the arguments and calls the entry point.
539 ;;; We analyze the new function and the entry point immediately so
540 ;;; that everything gets converted during the single pass.
541 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
542 (declare (list vars ignores args) (type ref ref) (type combination call)
543 (type clambda entry))
545 (with-ir1-environment-from-node call
548 (declare (ignorable ,@ignores))
549 (%funcall ,entry ,@args))
550 :debug-name (debug-namify "hairy function entry ~S"
552 (basic-combination-fun call)))))))
553 (convert-call ref call new-fun)
554 (dolist (ref (leaf-refs entry))
555 (convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
557 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
558 ;;; function into a local call to the MAIN-ENTRY.
560 ;;; First we verify that all keywords are constant and legal. If there
561 ;;; aren't, then we warn the user and don't attempt to convert the call.
563 ;;; We massage the supplied &KEY arguments into the order expected
564 ;;; by the main entry. This is done by binding all the arguments to
565 ;;; the keyword call to variables in the introduced lambda, then
566 ;;; passing these values variables in the correct order when calling
567 ;;; the main entry. Unused arguments (such as the keywords themselves)
568 ;;; are discarded simply by not passing them along.
570 ;;; If there is a &REST arg, then we bundle up the args and pass them
572 (defun convert-more-call (ref call fun)
573 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
574 (let* ((max (optional-dispatch-max-args fun))
575 (arglist (optional-dispatch-arglist fun))
576 (args (combination-args call))
577 (more (nthcdr max args))
578 (flame (policy call (or (> speed inhibit-warnings)
579 (> space inhibit-warnings))))
583 (temps (make-gensym-list max))
584 (more-temps (make-gensym-list (length more))))
589 (dolist (var arglist)
590 (let ((info (lambda-var-arg-info var)))
592 (ecase (arg-info-kind info)
596 ((:more-context :more-count)
597 (compiler-warn "can't local-call functions with &MORE args")
598 (setf (basic-combination-kind call) :error)
599 (return-from convert-more-call))))))
601 (when (optional-dispatch-keyp fun)
602 (when (oddp (length more))
603 (compiler-warn "function called with odd number of ~
604 arguments in keyword portion")
606 (setf (basic-combination-kind call) :error)
607 (return-from convert-more-call))
609 (do ((key more (cddr key))
610 (temp more-temps (cddr temp)))
612 (let ((lvar (first key)))
613 (unless (constant-lvar-p lvar)
615 (compiler-notify "non-constant keyword in keyword call"))
616 (setf (basic-combination-kind call) :error)
617 (return-from convert-more-call))
619 (let ((name (lvar-value lvar))
622 ;; FIXME: check whether KEY was supplied earlier
623 (when (and (eq name :allow-other-keys) (not allow-found))
624 (let ((val (second key)))
625 (cond ((constant-lvar-p val)
627 allowp (lvar-value val)))
629 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
630 (setf (basic-combination-kind call) :error)
631 (return-from convert-more-call)))))
632 (dolist (var (key-vars)
635 (unless (eq name :allow-other-keys)
637 (let ((info (lambda-var-arg-info var)))
638 (when (eq (arg-info-key info) name)
640 (supplied (cons var val))
643 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
644 (compiler-warn "function called with unknown argument keyword ~S"
646 (setf (basic-combination-kind call) :error)
647 (return-from convert-more-call)))
649 (collect ((call-args))
650 (do ((var arglist (cdr var))
651 (temp temps (cdr temp)))
653 (let ((info (lambda-var-arg-info (car var))))
655 (ecase (arg-info-kind info)
657 (call-args (car temp))
658 (when (arg-info-supplied-p info)
661 (call-args `(list ,@more-temps))
665 (call-args (car temp)))))
667 (dolist (var (key-vars))
668 (let ((info (lambda-var-arg-info var))
669 (temp (cdr (assoc var (supplied)))))
672 (call-args (arg-info-default info)))
673 (when (arg-info-supplied-p info)
674 (call-args (not (null temp))))))
676 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
677 (append temps more-temps)
678 (ignores) (call-args)))))
684 ;;;; Converting to a LET has differing significance to various parts
685 ;;;; of the compiler:
686 ;;;; -- The body of a LET is spliced in immediately after the
687 ;;;; corresponding combination node, making the control transfer
688 ;;;; explicit and allowing LETs to be mashed together into a single
689 ;;;; block. The value of the LET is delivered directly to the
690 ;;;; original lvar for the call, eliminating the need to
691 ;;;; propagate information from the dummy result lvar.
692 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
693 ;;;; that there is only one expression that the variable can be bound
694 ;;;; to, and this is easily substituted for.
695 ;;;; -- LETs are interesting to environment analysis and to the back
696 ;;;; end because in most ways a LET can be considered to be "the
697 ;;;; same function" as its home function.
698 ;;;; -- LET conversion has dynamic scope implications, since control
699 ;;;; transfers within the same environment are local. In a local
700 ;;;; control transfer, cleanup code must be emitted to remove
701 ;;;; dynamic bindings that are no longer in effect.
703 ;;; Set up the control transfer to the called CLAMBDA. We split the
704 ;;; call block immediately after the call, and link the head of
705 ;;; CLAMBDA to the call block. The successor block after splitting
706 ;;; (where we return to) is returned.
708 ;;; If the lambda is is a different component than the call, then we
709 ;;; call JOIN-COMPONENTS. This only happens in block compilation
710 ;;; before FIND-INITIAL-DFO.
711 (defun insert-let-body (clambda call)
712 (declare (type clambda clambda) (type basic-combination call))
713 (let* ((call-block (node-block call))
714 (bind-block (node-block (lambda-bind clambda)))
715 (component (block-component call-block)))
716 (aver-live-component component)
717 (let ((clambda-component (block-component bind-block)))
718 (unless (eq clambda-component component)
719 (aver (eq (component-kind component) :initial))
720 (join-components component clambda-component)))
721 (let ((*current-component* component))
722 (node-ends-block call))
723 (destructuring-bind (next-block)
724 (block-succ call-block)
725 (unlink-blocks call-block next-block)
726 (link-blocks call-block bind-block)
729 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
730 ;;; same set as; but leave CLAMBDA with a valid tail set value of
731 ;;; its own, for the benefit of code which might try to pull
732 ;;; something out of it (e.g. return type).
733 (defun depart-from-tail-set (clambda)
734 ;; Until sbcl-0.pre7.37.flaky5.2, we did
735 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
736 ;; (SETF (TAIL-SET-FUNS TAILS)
737 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
738 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
739 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
740 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
741 ;; inconsistent, and perhaps unsafe, for us to think we're in the
742 ;; tail set. Unfortunately..
744 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
745 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
746 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
747 ;; which you called in order to get the address of an external LAMBDA):
748 ;; the external function was defined in terms of internal function,
749 ;; which was LET-converted, and then things blew up downstream when
750 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
751 ;; the now-NILed-out TAIL-SET. So..
753 ;; To deal with this problem, we no longer NIL out
754 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
755 ;; * If we're the only function in TAIL-SET-FUNS, it should
756 ;; be safe to leave ourself linked to it, and it to you.
757 ;; * If there are other functions in TAIL-SET-FUNS, then we're
758 ;; afraid of future optimizations on those functions causing
759 ;; the TAIL-SET object no longer to be valid to describe our
760 ;; return value. Thus, we delete ourselves from that object;
761 ;; but we save a newly-allocated tail-set, derived from the old
762 ;; one, for ourselves, for the use of later code (e.g.
763 ;; FINALIZE-XEP-DEFINITION) which might want to
764 ;; know about our return type.
765 (let* ((old-tail-set (lambda-tail-set clambda))
766 (old-tail-set-funs (tail-set-funs old-tail-set)))
767 (unless (= 1 (length old-tail-set-funs))
768 (setf (tail-set-funs old-tail-set)
769 (delete clambda old-tail-set-funs))
770 (let ((new-tail-set (copy-tail-set old-tail-set)))
771 (setf (lambda-tail-set clambda) new-tail-set
772 (tail-set-funs new-tail-set) (list clambda)))))
773 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
774 ;; remain valid in this case, so we nuke it on the theory that
775 ;; missing information tends to be less dangerous than incorrect
777 (setf (tail-set-info (lambda-tail-set clambda)) nil))
779 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
780 ;;; its LETs to LETs for the CALL's home function. We merge the calls
781 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
782 ;;; in the process. We also merge the ENTRIES.
784 ;;; We also unlink the function head from the component head and set
785 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
787 (defun merge-lets (clambda call)
789 (declare (type clambda clambda) (type basic-combination call))
791 (let ((component (node-component call)))
792 (unlink-blocks (component-head component) (lambda-block clambda))
793 (setf (component-lambdas component)
794 (delete clambda (component-lambdas component)))
795 (setf (component-reanalyze component) t))
796 (setf (lambda-call-lexenv clambda) (node-lexenv call))
798 (depart-from-tail-set clambda)
800 (let* ((home (node-home-lambda call))
801 (home-physenv (lambda-physenv home)))
803 (aver (not (eq home clambda)))
805 ;; CLAMBDA belongs to HOME now.
806 (push clambda (lambda-lets home))
807 (setf (lambda-home clambda) home)
808 (setf (lambda-physenv clambda) home-physenv)
810 ;; All of CLAMBDA's LETs belong to HOME now.
811 (let ((lets (lambda-lets clambda)))
813 (setf (lambda-home let) home)
814 (setf (lambda-physenv let) home-physenv))
815 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
816 ;; CLAMBDA no longer has an independent existence as an entity
818 (setf (lambda-lets clambda) nil)
820 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
822 (setf (lambda-calls-or-closes home)
824 (nunion (lambda-calls-or-closes clambda)
825 (lambda-calls-or-closes home))))
826 ;; CLAMBDA no longer has an independent existence as an entity
827 ;; which calls things or has DFO dependencies.
828 (setf (lambda-calls-or-closes clambda) nil)
830 ;; All of CLAMBDA's ENTRIES belong to HOME now.
831 (setf (lambda-entries home)
832 (nconc (lambda-entries clambda)
833 (lambda-entries home)))
834 ;; CLAMBDA no longer has an independent existence as an entity
836 (setf (lambda-entries clambda) nil))
840 ;;; Handle the value semantics of LET conversion. Delete FUN's return
841 ;;; node, and change the control flow to transfer to NEXT-BLOCK
842 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
843 (defun move-return-uses (fun call next-block)
844 (declare (type clambda fun) (type basic-combination call)
845 (type cblock next-block))
846 (let* ((return (lambda-return fun))
848 (ensure-block-start (node-prev return))
849 (node-block return))))
850 (unlink-blocks return-block
851 (component-tail (block-component return-block)))
852 (link-blocks return-block next-block)
854 (delete-return return)
855 (let ((result (return-result return))
856 (lvar (if (node-tail-p call)
857 (return-result (lambda-return (node-home-lambda call)))
859 (call-type (node-derived-type call)))
860 (unless (eq call-type *wild-type*)
861 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
862 (do-uses (use result)
863 (derive-node-type use call-type)))
864 (substitute-lvar-uses lvar result)))
867 ;;; We are converting FUN to be a LET when the call is in a non-tail
868 ;;; position. Any previously tail calls in FUN are no longer tail
869 ;;; calls, and must be restored to normal calls which transfer to
870 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
871 ;;; the RETURN-RESULT, because the return might have been deleted (if
872 ;;; all calls were TR.)
873 (defun unconvert-tail-calls (fun call next-block)
874 (dolist (called (lambda-calls-or-closes fun))
875 (when (lambda-p called)
876 (dolist (ref (leaf-refs called))
877 (let ((this-call (node-dest ref)))
879 (node-tail-p this-call)
880 (eq (node-home-lambda this-call) fun))
881 (setf (node-tail-p this-call) nil)
882 (ecase (functional-kind called)
883 ((nil :cleanup :optional)
884 (let ((block (node-block this-call))
885 (lvar (node-lvar call)))
886 (unlink-blocks block (first (block-succ block)))
887 (link-blocks block next-block)
888 (aver (not (node-lvar this-call)))
889 (add-lvar-use this-call lvar)))
891 ;; The called function might be an assignment in the
892 ;; case where we are currently converting that function.
893 ;; In steady-state, assignments never appear as a called
896 (aver (eq called fun)))))))))
899 ;;; Deal with returning from a LET or assignment that we are
900 ;;; converting. FUN is the function we are calling, CALL is a call to
901 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
902 ;;; NULL if call is a tail call.
904 ;;; If the call is not a tail call, then we must do
905 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
906 ;;; its value out of the enclosing non-let function. When call is
907 ;;; non-TR, we must convert it back to an ordinary local call, since
908 ;;; the value must be delivered to the receiver of CALL's value.
910 ;;; We do different things depending on whether the caller and callee
911 ;;; have returns left:
913 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
914 ;;; Either the function doesn't return, or all returns are via
915 ;;; tail-recursive local calls.
916 ;;; -- If CALL is a non-tail call, or if both have returns, then
917 ;;; we delete the callee's return, move its uses to the call's
918 ;;; result lvar, and transfer control to the appropriate
920 ;;; -- If the callee has a return, but the caller doesn't, then we
921 ;;; move the return to the caller.
922 (defun move-return-stuff (fun call next-block)
923 (declare (type clambda fun) (type basic-combination call)
924 (type (or cblock null) next-block))
926 (unconvert-tail-calls fun call next-block))
927 (let* ((return (lambda-return fun))
928 (call-fun (node-home-lambda call))
929 (call-return (lambda-return call-fun)))
930 (when (and call-return
931 (block-delete-p (node-block call-return)))
932 (delete-return call-return)
933 (unlink-node call-return)
934 (setq call-return nil))
936 ((or next-block call-return)
937 (unless (block-delete-p (node-block return))
939 (ensure-block-start (node-prev call-return))
940 (setq next-block (node-block call-return)))
941 (move-return-uses fun call next-block)))
943 (aver (node-tail-p call))
944 (setf (lambda-return call-fun) return)
945 (setf (return-lambda return) call-fun)
946 (setf (lambda-return fun) nil))))
947 (%delete-lvar-use call) ; LET call does not have value semantics
950 ;;; Actually do LET conversion. We call subfunctions to do most of the
951 ;;; work. We change the CALL's CONT to be the continuation heading the
952 ;;; BIND block, and also do REOPTIMIZE-LVAR on the args and
953 ;;; CONT so that LET-specific IR1 optimizations get a chance. We blow
954 ;;; away any entry for the function in *FREE-FUNS* so that nobody
955 ;;; will create new references to it.
956 (defun let-convert (fun call)
957 (declare (type clambda fun) (type basic-combination call))
958 (let ((next-block (if (node-tail-p call)
960 (insert-let-body fun call))))
961 (move-return-stuff fun call next-block)
962 (merge-lets fun call)))
964 ;;; Reoptimize all of CALL's args and its result.
965 (defun reoptimize-call (call)
966 (declare (type basic-combination call))
967 (dolist (arg (basic-combination-args call))
969 (reoptimize-lvar arg)))
970 (reoptimize-lvar (node-lvar call))
973 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
975 (defun declarations-suppress-let-conversion-p (clambda)
976 ;; From the user's point of view, LET-converting something that
977 ;; has a name is inlining it. (The user can't see what we're doing
978 ;; with anonymous things, and suppressing inlining
979 ;; for such things can easily give Python acute indigestion, so
981 (when (leaf-has-source-name-p clambda)
982 ;; ANSI requires that explicit NOTINLINE be respected.
983 (or (eq (lambda-inlinep clambda) :notinline)
984 ;; If (= LET-CONVERTION 0) we can guess that inlining
985 ;; generally won't be appreciated, but if the user
986 ;; specifically requests inlining, that takes precedence over
987 ;; our general guess.
988 (and (policy clambda (= let-convertion 0))
989 (not (eq (lambda-inlinep clambda) :inline))))))
991 ;;; We also don't convert calls to named functions which appear in the
992 ;;; initial component, delaying this until optimization. This
993 ;;; minimizes the likelihood that we will LET-convert a function which
994 ;;; may have references added due to later local inline expansion.
995 (defun ok-initial-convert-p (fun)
996 (not (and (leaf-has-source-name-p fun)
997 (or (declarations-suppress-let-conversion-p fun)
998 (eq (component-kind (lambda-component fun))
1001 ;;; This function is called when there is some reason to believe that
1002 ;;; CLAMBDA might be converted into a LET. This is done after local
1003 ;;; call analysis, and also when a reference is deleted. We return
1004 ;;; true if we converted.
1005 (defun maybe-let-convert (clambda)
1006 (declare (type clambda clambda))
1007 (unless (declarations-suppress-let-conversion-p clambda)
1008 ;; We only convert to a LET when the function is a normal local
1009 ;; function, has no XEP, and is referenced in exactly one local
1010 ;; call. Conversion is also inhibited if the only reference is in
1011 ;; a block about to be deleted.
1013 ;; These rules limiting LET conversion may seem unnecessarily
1014 ;; restrictive, since there are some cases where we could do the
1015 ;; return with a jump that don't satisfy these requirements. The
1016 ;; reason for doing things this way is that it makes the concept
1017 ;; of a LET much more useful at the level of IR1 semantics. The
1018 ;; :ASSIGNMENT function kind provides another way to optimize
1019 ;; calls to single-return/multiple call functions.
1021 ;; We don't attempt to convert calls to functions that have an
1022 ;; XEP, since we might be embarrassed later when we want to
1023 ;; convert a newly discovered local call. Also, see
1024 ;; OK-INITIAL-CONVERT-P.
1025 (let ((refs (leaf-refs clambda)))
1028 (memq (functional-kind clambda) '(nil :assignment))
1029 (not (functional-entry-fun clambda)))
1030 (binding* ((ref (first refs))
1031 (ref-lvar (node-lvar ref) :exit-if-null)
1032 (dest (lvar-dest ref-lvar)))
1033 (when (and (basic-combination-p dest)
1034 (eq (basic-combination-fun dest) ref-lvar)
1035 (eq (basic-combination-kind dest) :local)
1036 (not (block-delete-p (node-block dest)))
1037 (cond ((ok-initial-convert-p clambda) t)
1039 (reoptimize-lvar ref-lvar)
1041 (when (eq clambda (node-home-lambda dest))
1042 (delete-lambda clambda)
1043 (return-from maybe-let-convert nil))
1044 (unless (eq (functional-kind clambda) :assignment)
1045 (let-convert clambda dest))
1046 (reoptimize-call dest)
1047 (setf (functional-kind clambda)
1048 (if (mv-combination-p dest) :mv-let :let))))
1051 ;;;; tail local calls and assignments
1053 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1054 ;;; they definitely won't generate any cleanup code. Currently we
1055 ;;; recognize lexical entry points that are only used locally (if at
1057 (defun only-harmless-cleanups (block1 block2)
1058 (declare (type cblock block1 block2))
1059 (or (eq block1 block2)
1060 (let ((cleanup2 (block-start-cleanup block2)))
1061 (do ((cleanup (block-end-cleanup block1)
1062 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1063 ((eq cleanup cleanup2) t)
1064 (case (cleanup-kind cleanup)
1066 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1068 (t (return nil)))))))
1070 ;;; If a potentially TR local call really is TR, then convert it to
1071 ;;; jump directly to the called function. We also call
1072 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1073 ;;; tail-convert. The second is the value of M-C-T-A.
1074 (defun maybe-convert-tail-local-call (call)
1075 (declare (type combination call))
1076 (let ((return (lvar-dest (node-lvar call))))
1077 (aver (return-p return))
1078 (when (and (not (node-tail-p call)) ; otherwise already converted
1079 ;; this is a tail call
1080 (immediately-used-p (return-result return) call)
1081 (only-harmless-cleanups (node-block call)
1082 (node-block return))
1083 ;; If the call is in an XEP, we might decide to make it
1084 ;; non-tail so that we can use known return inside the
1086 (not (eq (functional-kind (node-home-lambda call))
1088 (node-ends-block call)
1089 (let ((block (node-block call))
1090 (fun (combination-lambda call)))
1091 (setf (node-tail-p call) t)
1092 (unlink-blocks block (first (block-succ block)))
1093 (link-blocks block (lambda-block fun))
1094 (delete-lvar-use call)
1095 (values t (maybe-convert-to-assignment fun))))))
1097 ;;; This is called when we believe it might make sense to convert
1098 ;;; CLAMBDA to an assignment. All this function really does is
1099 ;;; determine when a function with more than one call can still be
1100 ;;; combined with the calling function's environment. We can convert
1102 ;;; -- The function is a normal, non-entry function, and
1103 ;;; -- Except for one call, all calls must be tail recursive calls
1104 ;;; in the called function (i.e. are self-recursive tail calls)
1105 ;;; -- OK-INITIAL-CONVERT-P is true.
1107 ;;; There may be one outside call, and it need not be tail-recursive.
1108 ;;; Since all tail local calls have already been converted to direct
1109 ;;; transfers, the only control semantics needed are to splice in the
1110 ;;; body at the non-tail call. If there is no non-tail call, then we
1111 ;;; need only merge the environments. Both cases are handled by
1114 ;;; ### It would actually be possible to allow any number of outside
1115 ;;; calls as long as they all return to the same place (i.e. have the
1116 ;;; same conceptual continuation.) A special case of this would be
1117 ;;; when all of the outside calls are tail recursive.
1118 (defun maybe-convert-to-assignment (clambda)
1119 (declare (type clambda clambda))
1120 (when (and (not (functional-kind clambda))
1121 (not (functional-entry-fun clambda)))
1122 (let ((outside-non-tail-call nil)
1124 (when (and (dolist (ref (leaf-refs clambda) t)
1125 (let ((dest (lvar-dest (node-lvar ref))))
1126 (when (or (not dest)
1127 (block-delete-p (node-block dest)))
1129 (let ((home (node-home-lambda ref)))
1130 (unless (eq home clambda)
1133 (setq outside-call dest))
1134 (unless (node-tail-p dest)
1135 (when (or outside-non-tail-call (eq home clambda))
1137 (setq outside-non-tail-call dest)))))
1138 (ok-initial-convert-p clambda))
1139 (cond (outside-call (setf (functional-kind clambda) :assignment)
1140 (let-convert clambda outside-call)
1141 (when outside-non-tail-call
1142 (reoptimize-call outside-non-tail-call))
1144 (t (delete-lambda clambda)