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 for arg = (assert-continuation-type (car args)
41 (leaf-type var) policy)
42 do (unless (leaf-refs var)
43 (flush-dest (car args))
44 (setf (car args) nil)))
48 ;;; This function handles merging the tail sets if CALL is potentially
49 ;;; tail-recursive, and is a call to a function with a different
50 ;;; TAIL-SET than CALL's FUN. This must be called whenever we alter
51 ;;; IR1 so as to place a local call in what might be a tail-recursive
52 ;;; context. Note that any call which returns its value to a RETURN is
53 ;;; considered potentially tail-recursive, since any implicit MV-PROG1
54 ;;; might be optimized away.
56 ;;; We destructively modify the set for the calling function to
57 ;;; represent both, and then change all the functions in callee's set
58 ;;; to reference the first. If we do merge, we reoptimize the
59 ;;; RETURN-RESULT continuation to cause IR1-OPTIMIZE-RETURN to
60 ;;; recompute the tail set type.
61 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
62 (declare (type basic-combination call) (type clambda new-fun))
63 (let ((return (continuation-dest (node-cont call))))
64 (when (return-p return)
65 (let ((call-set (lambda-tail-set (node-home-lambda call)))
66 (fun-set (lambda-tail-set new-fun)))
67 (unless (eq call-set fun-set)
68 (let ((funs (tail-set-funs fun-set)))
70 (setf (lambda-tail-set fun) call-set))
71 (setf (tail-set-funs call-set)
72 (nconc (tail-set-funs call-set) funs)))
73 (reoptimize-continuation (return-result return))
76 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
77 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
78 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
79 ;;; the function in the REF node with the new function.
81 ;;; We change the REF last, since changing the reference can trigger
82 ;;; LET conversion of the new function, but will only do so if the
83 ;;; call is local. Note that the replacement may trigger LET
84 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
85 ;;; with NEW-FUN before the substitution, since after the substitution
86 ;;; (and LET conversion), the call may no longer be recognizable as
88 (defun convert-call (ref call fun)
89 (declare (type ref ref) (type combination call) (type clambda fun))
90 (propagate-to-args call fun)
91 (setf (basic-combination-kind call) :local)
92 (unless (call-full-like-p call)
93 (dolist (arg (basic-combination-args call))
95 (flush-continuation-externally-checkable-type arg))))
96 (pushnew fun (lambda-calls-or-closes (node-home-lambda call)))
97 (merge-tail-sets call fun)
98 (change-ref-leaf ref fun)
101 ;;;; external entry point creation
103 ;;; Return a LAMBDA form that can be used as the definition of the XEP
106 ;;; If FUN is a LAMBDA, then we check the number of arguments
107 ;;; (conditional on policy) and call FUN with all the arguments.
109 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
110 ;;; of supplied arguments by doing do an = test for each entry-point,
111 ;;; calling the entry with the appropriate prefix of the passed
114 ;;; If there is a &MORE arg, then there are a couple of optimizations
115 ;;; that we make (more for space than anything else):
116 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
117 ;;; no argument count error is possible.
118 ;;; -- We can omit the = clause for the last entry-point, allowing the
119 ;;; case of 0 more args to fall through to the more entry.
121 ;;; We don't bother to policy conditionalize wrong arg errors in
122 ;;; optional dispatches, since the additional overhead is negligible
123 ;;; compared to the cost of everything else going on.
125 ;;; Note that if policy indicates it, argument type declarations in
126 ;;; FUN will be verified. Since nothing is known about the type of the
127 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
128 ;;; are passed to the actual function.
129 (defun make-xep-lambda-expression (fun)
130 (declare (type functional fun))
133 (let ((nargs (length (lambda-vars fun)))
134 (n-supplied (gensym))
135 (temps (make-gensym-list (length (lambda-vars fun)))))
136 `(lambda (,n-supplied ,@temps)
137 (declare (type index ,n-supplied))
138 ,(if (policy *lexenv* (zerop verify-arg-count))
139 `(declare (ignore ,n-supplied))
140 `(%verify-arg-count ,n-supplied ,nargs))
142 (declare (optimize (merge-tail-calls 3)))
143 (%funcall ,fun ,@temps)))))
145 (let* ((min (optional-dispatch-min-args fun))
146 (max (optional-dispatch-max-args fun))
147 (more (optional-dispatch-more-entry fun))
148 (n-supplied (gensym))
149 (temps (make-gensym-list max)))
151 ;; Force convertion of all entries
152 (optional-dispatch-entry-point-fun fun 0)
153 (loop for ep in (optional-dispatch-entry-points fun)
155 do (entries `((= ,n-supplied ,n)
156 (%funcall ,(force ep) ,@(subseq temps 0 n)))))
157 `(lambda (,n-supplied ,@temps)
158 ;; FIXME: Make sure that INDEX type distinguishes between
159 ;; target and host. (Probably just make the SB!XC:DEFTYPE
160 ;; different from CL:DEFTYPE.)
161 (declare (type index ,n-supplied))
163 ,@(if more (butlast (entries)) (entries))
165 `((,(if (zerop min) t `(>= ,n-supplied ,max))
166 ,(let ((n-context (gensym))
168 `(multiple-value-bind (,n-context ,n-count)
169 (%more-arg-context ,n-supplied ,max)
171 (declare (optimize (merge-tail-calls 3)))
172 (%funcall ,more ,@temps ,n-context ,n-count)))))))
174 (%arg-count-error ,n-supplied)))))))))
176 ;;; Make an external entry point (XEP) for FUN and return it. We
177 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
178 ;;; then associate this lambda with FUN as its XEP. After the
179 ;;; conversion, we iterate over the function's associated lambdas,
180 ;;; redoing local call analysis so that the XEP calls will get
183 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
184 ;;; discover an XEP after the initial local call analyze pass.
185 (defun make-xep (fun)
186 (declare (type functional fun))
187 (aver (null (functional-entry-fun fun)))
188 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
189 (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
190 :debug-name (debug-namify
192 (leaf-debug-name fun)))))
193 (setf (functional-kind res) :external
194 (leaf-ever-used res) t
195 (functional-entry-fun res) fun
196 (functional-entry-fun fun) res
197 (component-reanalyze *current-component*) t
198 (component-reoptimize *current-component*) t)
201 (locall-analyze-fun-1 fun))
203 (dolist (ep (optional-dispatch-entry-points fun))
204 (locall-analyze-fun-1 (force ep)))
205 (when (optional-dispatch-more-entry fun)
206 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
209 ;;; Notice a REF that is not in a local-call context. If the REF is
210 ;;; already to an XEP, then do nothing, otherwise change it to the
211 ;;; XEP, making an XEP if necessary.
213 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
214 ;;; it as though it was not an XEP reference (i.e. leave it alone).
215 (defun reference-entry-point (ref)
216 (declare (type ref ref))
217 (let ((fun (ref-leaf ref)))
218 (unless (or (xep-p fun)
219 (member (functional-kind fun) '(:escape :cleanup)))
220 (change-ref-leaf ref (or (functional-entry-fun fun)
223 ;;; Attempt to convert all references to FUN to local calls. The
224 ;;; reference must be the function for a call, and the function
225 ;;; continuation must be used only once, since otherwise we cannot be
226 ;;; sure what function is to be called. The call continuation would be
227 ;;; multiply used if there is hairy stuff such as conditionals in the
228 ;;; expression that computes the function.
230 ;;; If we cannot convert a reference, then we mark the referenced
231 ;;; function as an entry-point, creating a new XEP if necessary. We
232 ;;; don't try to convert calls that are in error (:ERROR kind.)
234 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
235 ;;; can force analysis of newly introduced calls. Note that we don't
236 ;;; do LET conversion here.
237 (defun locall-analyze-fun-1 (fun)
238 (declare (type functional fun))
239 (let ((refs (leaf-refs fun))
242 (let* ((cont (node-cont ref))
243 (dest (continuation-dest cont)))
244 (cond ((and (basic-combination-p dest)
245 (eq (basic-combination-fun dest) cont)
246 (eq (continuation-use cont) ref))
248 (convert-call-if-possible ref dest)
250 (unless (eq (basic-combination-kind dest) :local)
251 (reference-entry-point ref)))
253 (reference-entry-point ref))))
254 (setq first-time nil)))
258 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
259 ;;; calls into local calls when it is legal. We also attempt to
260 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
261 ;;; deletion of a function reference, but functions that start out
262 ;;; eligible for conversion must be noticed sometime.
264 ;;; Note that there is a lot of action going on behind the scenes
265 ;;; here, triggered by reference deletion. In particular, the
266 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
267 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
268 ;;; the COMPONENT-LAMBDAS when it is. Also, the
269 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
270 ;;; it is not updated when we delete functions, etc. Only
271 ;;; COMPONENT-LAMBDAS is updated.
273 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
274 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
276 (defun locall-analyze-component (component)
277 (declare (type component component))
278 (aver-live-component component)
280 (let* ((new-functional (pop (component-new-functionals component)))
281 (functional (or new-functional
282 (pop (component-reanalyze-functionals component)))))
285 (let ((kind (functional-kind functional)))
286 (cond ((or (functional-somewhat-letlike-p functional)
288 (values)) ; nothing to do
289 ((and (null (leaf-refs functional)) (eq kind nil)
290 (not (functional-entry-fun functional)))
291 (delete-functional functional))
293 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
294 (cond ((not (lambda-p functional))
295 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
298 (new-functional ; FUNCTIONAL came from
299 ; NEW-FUNCTIONALS, hence is new.
300 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
301 (aver (not (member functional
302 (component-lambdas component))))
303 (push functional (component-lambdas component)))
304 (t ; FUNCTIONAL is old.
305 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
306 (aver (member functional (component-lambdas
308 (locall-analyze-fun-1 functional)
309 (when (lambda-p functional)
310 (maybe-let-convert functional)))))))
313 (defun locall-analyze-clambdas-until-done (clambdas)
315 (let ((did-something nil))
316 (dolist (clambda clambdas)
317 (let* ((component (lambda-component clambda))
318 (*all-components* (list component)))
319 ;; The original CMU CL code seemed to implicitly assume that
320 ;; COMPONENT is the only one here. Let's make that explicit.
321 (aver (= 1 (length (functional-components clambda))))
322 (aver (eql component (first (functional-components clambda))))
323 (when (or (component-new-functionals component)
324 (component-reanalyze-functionals component))
325 (setf did-something t)
326 (locall-analyze-component component))))
327 (unless did-something
331 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
332 ;;; to be in an infinite recursive loop, then change the reference to
333 ;;; reference a fresh copy. We return whichever function we decide to
335 (defun maybe-expand-local-inline (original-functional ref call)
336 (if (and (policy call
337 (and (>= speed space)
338 (>= speed compilation-speed)))
339 (not (eq (functional-kind (node-home-lambda call)) :external))
340 (inline-expansion-ok call))
341 (let* ((end (component-last-block (node-component call)))
342 (pred (block-prev end)))
343 (multiple-value-bind (losing-local-functional converted-lambda)
344 (catch 'locall-already-let-converted
345 (with-ir1-environment-from-node call
346 (let ((*lexenv* (functional-lexenv original-functional)))
349 (functional-inline-expansion original-functional)
350 :debug-name (debug-namify
353 original-functional)))))))
354 (cond (losing-local-functional
355 (let ((*compiler-error-context* call))
356 (compiler-notify "couldn't inline expand because expansion ~
357 calls this LET-converted local function:~
359 (leaf-debug-name losing-local-functional)))
360 (loop for block = (block-next pred) then (block-next block)
362 do (setf (block-delete-p block) t))
363 (loop for block = (block-next pred) then (block-next block)
365 do (delete-block block t))
368 (change-ref-leaf ref converted-lambda)
370 original-functional))
372 ;;; Dispatch to the appropriate function to attempt to convert a call.
373 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
374 ;;; optimization as well as in local call analysis. If the call is is
375 ;;; already :LOCAL, we do nothing. If the call is already scheduled
376 ;;; for deletion, also do nothing (in addition to saving time, this
377 ;;; also avoids some problems with optimizing collections of functions
378 ;;; that are partially deleted.)
380 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
381 ;;; called on a :INITIAL component, we don't care whether the caller
382 ;;; and callee are in the same component. Afterward, we must stick
383 ;;; with whatever component division we have chosen.
385 ;;; Before attempting to convert a call, we see whether the function
386 ;;; is supposed to be inline expanded. Call conversion proceeds as
387 ;;; before after any expansion.
389 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
390 ;;; warnings will get the right context.
391 (defun convert-call-if-possible (ref call)
392 (declare (type ref ref) (type basic-combination call))
393 (let* ((block (node-block call))
394 (component (block-component block))
395 (original-fun (ref-leaf ref)))
396 (aver (functional-p original-fun))
397 (unless (or (member (basic-combination-kind call) '(:local :error))
398 (block-delete-p block)
399 (eq (functional-kind (block-home-lambda block)) :deleted)
400 (member (functional-kind original-fun)
401 '(:toplevel-xep :deleted))
402 (not (or (eq (component-kind component) :initial)
405 (lambda-bind (main-entry original-fun))))
407 (let ((fun (if (xep-p original-fun)
408 (functional-entry-fun original-fun)
410 (*compiler-error-context* call))
412 (when (and (eq (functional-inlinep fun) :inline)
413 (rest (leaf-refs original-fun)))
414 (setq fun (maybe-expand-local-inline fun ref call)))
416 (aver (member (functional-kind fun)
417 '(nil :escape :cleanup :optional)))
418 (cond ((mv-combination-p call)
419 (convert-mv-call ref call fun))
421 (convert-lambda-call ref call fun))
423 (convert-hairy-call ref call fun))))))
427 ;;; Attempt to convert a multiple-value call. The only interesting
428 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
429 ;;; exactly one reference and no XEP, and is called with one values
432 ;;; We change the call to be to the last optional entry point and
433 ;;; change the call to be local. Due to our preconditions, the call
434 ;;; should eventually be converted to a let, but we can't do that now,
435 ;;; since there may be stray references to the e-p lambda due to
436 ;;; optional defaulting code.
438 ;;; We also use variable types for the called function to construct an
439 ;;; assertion for the values continuation.
441 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
442 (defun convert-mv-call (ref call fun)
443 (declare (type ref ref) (type mv-combination call) (type functional fun))
444 (when (and (looks-like-an-mv-bind fun)
445 (not (functional-entry-fun fun))
446 (= (length (leaf-refs fun)) 1)
447 (= (length (basic-combination-args call)) 1))
448 (let* ((*current-component* (node-component ref))
449 (ep (optional-dispatch-entry-point-fun
450 fun (optional-dispatch-max-args fun))))
451 (aver (= (optional-dispatch-min-args fun) 0))
452 (setf (basic-combination-kind call) :local)
453 (pushnew ep (lambda-calls-or-closes (node-home-lambda call)))
454 (merge-tail-sets call ep)
455 (change-ref-leaf ref ep)
457 (assert-continuation-type
458 (first (basic-combination-args call))
459 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
460 (lexenv-policy (node-lexenv call)))))
463 ;;; Attempt to convert a call to a lambda. If the number of args is
464 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
465 ;;; from future consideration. If the argcount is O.K. then we just
467 (defun convert-lambda-call (ref call fun)
468 (declare (type ref ref) (type combination call) (type clambda fun))
469 (let ((nargs (length (lambda-vars fun)))
470 (call-args (length (combination-args call))))
471 (cond ((= call-args nargs)
472 (convert-call ref call fun))
474 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
475 ;; Compiler" that calling a function with "the wrong number of
476 ;; arguments" be only a STYLE-ERROR. I think, though, that this
477 ;; should only apply when the number of arguments is inferred
478 ;; from a previous definition. If the number of arguments
479 ;; is DECLAIMed, surely calling with the wrong number is a
480 ;; real WARNING. As long as SBCL continues to use CMU CL's
481 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
482 ;; but as long as we continue to use that policy, that's the
483 ;; not our biggest problem.:-| When we fix that policy, this
484 ;; should come back into compliance. (So fix that policy!)
486 ;; FIXME, continued: Except that section "3.2.2.3 Semantic
487 ;; Constraints" says that if it's within the same file, it's
488 ;; wrong. And we're in locall.lisp here, so it's probably
489 ;; (haven't checked this..) a call to something in the same
490 ;; file. So maybe it deserves a full warning anyway.
492 "function called with ~R argument~:P, but wants exactly ~R"
494 (setf (basic-combination-kind call) :error)))))
496 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
498 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
499 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
500 ;;; a call to the correct entry point. If &KEY args are supplied, then
501 ;;; dispatch to a subfunction. We don't convert calls to functions
502 ;;; that have a &MORE (or &REST) arg.
503 (defun convert-hairy-call (ref call fun)
504 (declare (type ref ref) (type combination call)
505 (type optional-dispatch fun))
506 (let ((min-args (optional-dispatch-min-args fun))
507 (max-args (optional-dispatch-max-args fun))
508 (call-args (length (combination-args call))))
509 (cond ((< call-args min-args)
510 ;; FIXME: See FIXME note at the previous
511 ;; wrong-number-of-arguments warnings in this file.
513 "function called with ~R argument~:P, but wants at least ~R"
515 (setf (basic-combination-kind call) :error))
516 ((<= call-args max-args)
517 (convert-call ref call
518 (let ((*current-component* (node-component ref)))
519 (optional-dispatch-entry-point-fun
520 fun (- call-args min-args)))))
521 ((optional-dispatch-more-entry fun)
522 (convert-more-call ref call fun))
524 ;; FIXME: See FIXME note at the previous
525 ;; wrong-number-of-arguments warnings in this file.
527 "function called with ~R argument~:P, but wants at most ~R"
529 (setf (basic-combination-kind call) :error))))
532 ;;; This function is used to convert a call to an entry point when
533 ;;; complex transformations need to be done on the original arguments.
534 ;;; ENTRY is the entry point function that we are calling. VARS is a
535 ;;; list of variable names which are bound to the original call
536 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
537 ;;; is the list of arguments to the entry point function.
539 ;;; In order to avoid gruesome graph grovelling, we introduce a new
540 ;;; function that rearranges the arguments and calls the entry point.
541 ;;; We analyze the new function and the entry point immediately so
542 ;;; that everything gets converted during the single pass.
543 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
544 (declare (list vars ignores args) (type ref ref) (type combination call)
545 (type clambda entry))
547 (with-ir1-environment-from-node call
550 (declare (ignorable ,@ignores))
551 (%funcall ,entry ,@args))
552 :debug-name (debug-namify "hairy function entry ~S"
553 (continuation-fun-name
554 (basic-combination-fun call)))))))
555 (convert-call ref call new-fun)
556 (dolist (ref (leaf-refs entry))
557 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
559 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
560 ;;; function into a local call to the MAIN-ENTRY.
562 ;;; First we verify that all keywords are constant and legal. If there
563 ;;; aren't, then we warn the user and don't attempt to convert the call.
565 ;;; We massage the supplied &KEY arguments into the order expected
566 ;;; by the main entry. This is done by binding all the arguments to
567 ;;; the keyword call to variables in the introduced lambda, then
568 ;;; passing these values variables in the correct order when calling
569 ;;; the main entry. Unused arguments (such as the keywords themselves)
570 ;;; are discarded simply by not passing them along.
572 ;;; If there is a &REST arg, then we bundle up the args and pass them
574 (defun convert-more-call (ref call fun)
575 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
576 (let* ((max (optional-dispatch-max-args fun))
577 (arglist (optional-dispatch-arglist fun))
578 (args (combination-args call))
579 (more (nthcdr max args))
580 (flame (policy call (or (> speed inhibit-warnings)
581 (> space inhibit-warnings))))
585 (temps (make-gensym-list max))
586 (more-temps (make-gensym-list (length more))))
591 (dolist (var arglist)
592 (let ((info (lambda-var-arg-info var)))
594 (ecase (arg-info-kind info)
598 ((:more-context :more-count)
599 (compiler-warn "can't local-call functions with &MORE args")
600 (setf (basic-combination-kind call) :error)
601 (return-from convert-more-call))))))
603 (when (optional-dispatch-keyp fun)
604 (when (oddp (length more))
605 (compiler-warn "function called with odd number of ~
606 arguments in keyword portion")
608 (setf (basic-combination-kind call) :error)
609 (return-from convert-more-call))
611 (do ((key more (cddr key))
612 (temp more-temps (cddr temp)))
614 (let ((cont (first key)))
615 (unless (constant-continuation-p cont)
617 (compiler-notify "non-constant keyword in keyword call"))
618 (setf (basic-combination-kind call) :error)
619 (return-from convert-more-call))
621 (let ((name (continuation-value cont))
624 ;; FIXME: check whether KEY was supplied earlier
625 (when (and (eq name :allow-other-keys) (not allow-found))
626 (let ((val (second key)))
627 (cond ((constant-continuation-p val)
629 allowp (continuation-value val)))
631 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
632 (setf (basic-combination-kind call) :error)
633 (return-from convert-more-call)))))
634 (dolist (var (key-vars)
637 (unless (eq name :allow-other-keys)
639 (let ((info (lambda-var-arg-info var)))
640 (when (eq (arg-info-key info) name)
642 (supplied (cons var val))
645 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
646 (compiler-warn "function called with unknown argument keyword ~S"
648 (setf (basic-combination-kind call) :error)
649 (return-from convert-more-call)))
651 (collect ((call-args))
652 (do ((var arglist (cdr var))
653 (temp temps (cdr temp)))
655 (let ((info (lambda-var-arg-info (car var))))
657 (ecase (arg-info-kind info)
659 (call-args (car temp))
660 (when (arg-info-supplied-p info)
663 (call-args `(list ,@more-temps))
667 (call-args (car temp)))))
669 (dolist (var (key-vars))
670 (let ((info (lambda-var-arg-info var))
671 (temp (cdr (assoc var (supplied)))))
674 (call-args (arg-info-default info)))
675 (when (arg-info-supplied-p info)
676 (call-args (not (null temp))))))
678 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
679 (append temps more-temps)
680 (ignores) (call-args)))))
686 ;;;; Converting to a LET has differing significance to various parts
687 ;;;; of the compiler:
688 ;;;; -- The body of a LET is spliced in immediately after the
689 ;;;; corresponding combination node, making the control transfer
690 ;;;; explicit and allowing LETs to be mashed together into a single
691 ;;;; block. The value of the LET is delivered directly to the
692 ;;;; original continuation for the call, eliminating the need to
693 ;;;; propagate information from the dummy result continuation.
694 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
695 ;;;; that there is only one expression that the variable can be bound
696 ;;;; to, and this is easily substituted for.
697 ;;;; -- LETs are interesting to environment analysis and to the back
698 ;;;; end because in most ways a LET can be considered to be "the
699 ;;;; same function" as its home function.
700 ;;;; -- LET conversion has dynamic scope implications, since control
701 ;;;; transfers within the same environment are local. In a local
702 ;;;; control transfer, cleanup code must be emitted to remove
703 ;;;; dynamic bindings that are no longer in effect.
705 ;;; Set up the control transfer to the called CLAMBDA. We split the
706 ;;; call block immediately after the call, and link the head of
707 ;;; CLAMBDA to the call block. The successor block after splitting
708 ;;; (where we return to) is returned.
710 ;;; If the lambda is is a different component than the call, then we
711 ;;; call JOIN-COMPONENTS. This only happens in block compilation
712 ;;; before FIND-INITIAL-DFO.
713 (defun insert-let-body (clambda call)
714 (declare (type clambda clambda) (type basic-combination call))
715 (let* ((call-block (node-block call))
716 (bind-block (node-block (lambda-bind clambda)))
717 (component (block-component call-block)))
718 (aver-live-component component)
719 (let ((clambda-component (block-component bind-block)))
720 (unless (eq clambda-component component)
721 (aver (eq (component-kind component) :initial))
722 (join-components component clambda-component)))
723 (let ((*current-component* component))
724 (node-ends-block call))
725 ;; FIXME: Use DESTRUCTURING-BIND here, and grep for other
726 ;; uses of '=.*length' which could also be converted to use
727 ;; DESTRUCTURING-BIND or PROPER-LIST-OF-LENGTH-P.
728 (aver (= (length (block-succ call-block)) 1))
729 (let ((next-block (first (block-succ call-block))))
730 (unlink-blocks call-block next-block)
731 (link-blocks call-block bind-block)
734 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
735 ;;; same set as; but leave CLAMBDA with a valid tail set value of
736 ;;; its own, for the benefit of code which might try to pull
737 ;;; something out of it (e.g. return type).
738 (defun depart-from-tail-set (clambda)
739 ;; Until sbcl-0.pre7.37.flaky5.2, we did
740 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
741 ;; (SETF (TAIL-SET-FUNS TAILS)
742 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
743 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
744 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
745 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
746 ;; inconsistent, and perhaps unsafe, for us to think we're in the
747 ;; tail set. Unfortunately..
749 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
750 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
751 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
752 ;; which you called in order to get the address of an external LAMBDA):
753 ;; the external function was defined in terms of internal function,
754 ;; which was LET-converted, and then things blew up downstream when
755 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
756 ;; the now-NILed-out TAIL-SET. So..
758 ;; To deal with this problem, we no longer NIL out
759 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
760 ;; * If we're the only function in TAIL-SET-FUNS, it should
761 ;; be safe to leave ourself linked to it, and it to you.
762 ;; * If there are other functions in TAIL-SET-FUNS, then we're
763 ;; afraid of future optimizations on those functions causing
764 ;; the TAIL-SET object no longer to be valid to describe our
765 ;; return value. Thus, we delete ourselves from that object;
766 ;; but we save a newly-allocated tail-set, derived from the old
767 ;; one, for ourselves, for the use of later code (e.g.
768 ;; FINALIZE-XEP-DEFINITION) which might want to
769 ;; know about our return type.
770 (let* ((old-tail-set (lambda-tail-set clambda))
771 (old-tail-set-funs (tail-set-funs old-tail-set)))
772 (unless (= 1 (length old-tail-set-funs))
773 (setf (tail-set-funs old-tail-set)
774 (delete clambda old-tail-set-funs))
775 (let ((new-tail-set (copy-tail-set old-tail-set)))
776 (setf (lambda-tail-set clambda) new-tail-set
777 (tail-set-funs new-tail-set) (list clambda)))))
778 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
779 ;; remain valid in this case, so we nuke it on the theory that
780 ;; missing information tends to be less dangerous than incorrect
782 (setf (tail-set-info (lambda-tail-set clambda)) nil))
784 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
785 ;;; its LETs to LETs for the CALL's home function. We merge the calls
786 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
787 ;;; in the process. We also merge the ENTRIES.
789 ;;; We also unlink the function head from the component head and set
790 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
792 (defun merge-lets (clambda call)
794 (declare (type clambda clambda) (type basic-combination call))
796 (let ((component (node-component call)))
797 (unlink-blocks (component-head component) (lambda-block clambda))
798 (setf (component-lambdas component)
799 (delete clambda (component-lambdas component)))
800 (setf (component-reanalyze component) t))
801 (setf (lambda-call-lexenv clambda) (node-lexenv call))
803 (depart-from-tail-set clambda)
805 (let* ((home (node-home-lambda call))
806 (home-physenv (lambda-physenv home)))
808 (aver (not (eq home clambda)))
810 ;; CLAMBDA belongs to HOME now.
811 (push clambda (lambda-lets home))
812 (setf (lambda-home clambda) home)
813 (setf (lambda-physenv clambda) home-physenv)
815 ;; All of CLAMBDA's LETs belong to HOME now.
816 (let ((lets (lambda-lets clambda)))
818 (setf (lambda-home let) home)
819 (setf (lambda-physenv let) home-physenv))
820 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
821 ;; CLAMBDA no longer has an independent existence as an entity
823 (setf (lambda-lets clambda) nil)
825 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
827 (setf (lambda-calls-or-closes home)
829 (nunion (lambda-calls-or-closes clambda)
830 (lambda-calls-or-closes home))))
831 ;; CLAMBDA no longer has an independent existence as an entity
832 ;; which calls things or has DFO dependencies.
833 (setf (lambda-calls-or-closes clambda) nil)
835 ;; All of CLAMBDA's ENTRIES belong to HOME now.
836 (setf (lambda-entries home)
837 (nconc (lambda-entries clambda)
838 (lambda-entries home)))
839 ;; CLAMBDA no longer has an independent existence as an entity
841 (setf (lambda-entries clambda) nil))
845 ;;; Handle the value semantics of LET conversion. Delete FUN's return
846 ;;; node, and change the control flow to transfer to NEXT-BLOCK
847 ;;; instead. Move all the uses of the result continuation to CALL's
849 (defun move-return-uses (fun call next-block)
850 (declare (type clambda fun) (type basic-combination call)
851 (type cblock next-block))
852 (let* ((return (lambda-return fun))
853 (return-block (node-block return)))
854 (unlink-blocks return-block
855 (component-tail (block-component return-block)))
856 (link-blocks return-block next-block)
858 (delete-return return)
859 (let ((result (return-result return))
860 (cont (node-cont call))
861 (call-type (node-derived-type call)))
862 (unless (eq call-type *wild-type*)
863 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2002-01-26
864 (do-uses (use result)
865 (derive-node-type use call-type)))
866 (substitute-continuation-uses cont result)))
869 ;;; Change all CONT for all the calls to FUN to be the start
870 ;;; continuation for the bind node. This allows the blocks to be
871 ;;; joined if the caller count ever goes to one.
872 (defun move-let-call-cont (fun)
873 (declare (type clambda fun))
874 (let ((new-cont (node-prev (lambda-bind fun))))
875 (dolist (ref (leaf-refs fun))
876 (let ((dest (continuation-dest (node-cont ref))))
877 (delete-continuation-use dest)
878 (add-continuation-use dest new-cont))))
881 ;;; We are converting FUN to be a LET when the call is in a non-tail
882 ;;; position. Any previously tail calls in FUN are no longer tail
883 ;;; calls, and must be restored to normal calls which transfer to
884 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
885 ;;; the RETURN-RESULT, because the return might have been deleted (if
886 ;;; all calls were TR.)
887 (defun unconvert-tail-calls (fun call next-block)
888 (dolist (called (lambda-calls-or-closes fun))
889 (when (lambda-p called)
890 (dolist (ref (leaf-refs called))
891 (let ((this-call (continuation-dest (node-cont ref))))
893 (node-tail-p this-call)
894 (eq (node-home-lambda this-call) fun))
895 (setf (node-tail-p this-call) nil)
896 (ecase (functional-kind called)
897 ((nil :cleanup :optional)
898 (let ((block (node-block this-call))
899 (cont (node-cont call)))
900 (ensure-block-start cont)
901 (unlink-blocks block (first (block-succ block)))
902 (link-blocks block next-block)
903 (delete-continuation-use this-call)
904 (add-continuation-use this-call cont)))
906 ;; The called function might be an assignment in the
907 ;; case where we are currently converting that function.
908 ;; In steady-state, assignments never appear as a called
911 (aver (eq called fun)))))))))
914 ;;; Deal with returning from a LET or assignment that we are
915 ;;; converting. FUN is the function we are calling, CALL is a call to
916 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
917 ;;; NULL if call is a tail call.
919 ;;; If the call is not a tail call, then we must do
920 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
921 ;;; its value out of the enclosing non-let function. When call is
922 ;;; non-TR, we must convert it back to an ordinary local call, since
923 ;;; the value must be delivered to the receiver of CALL's value.
925 ;;; We do different things depending on whether the caller and callee
926 ;;; have returns left:
928 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
929 ;;; Either the function doesn't return, or all returns are via
930 ;;; tail-recursive local calls.
931 ;;; -- If CALL is a non-tail call, or if both have returns, then
932 ;;; we delete the callee's return, move its uses to the call's
933 ;;; result continuation, and transfer control to the appropriate
935 ;;; -- If the callee has a return, but the caller doesn't, then we
936 ;;; move the return to the caller.
937 (defun move-return-stuff (fun call next-block)
938 (declare (type clambda fun) (type basic-combination call)
939 (type (or cblock null) next-block))
941 (unconvert-tail-calls fun call next-block))
942 (let* ((return (lambda-return fun))
943 (call-fun (node-home-lambda call))
944 (call-return (lambda-return call-fun)))
946 ((or next-block call-return)
947 (unless (block-delete-p (node-block return))
948 (when (and (node-tail-p call)
950 (not (eq (node-cont call)
951 (return-result call-return))))
952 ;; We do not care to give a meaningful continuation to
953 ;; a tail combination, but here we need it.
954 (delete-continuation-use call)
955 (add-continuation-use call (return-result call-return)))
956 (move-return-uses fun call
958 (let ((block (node-block call-return)))
959 (when (block-delete-p block)
960 (setf (block-delete-p block) nil))
963 (aver (node-tail-p call))
964 (setf (lambda-return call-fun) return)
965 (setf (return-lambda return) call-fun)
966 (setf (lambda-return fun) nil))))
967 (move-let-call-cont fun)
970 ;;; Actually do LET conversion. We call subfunctions to do most of the
971 ;;; work. We change the CALL's CONT to be the continuation heading the
972 ;;; BIND block, and also do REOPTIMIZE-CONTINUATION on the args and
973 ;;; CONT so that LET-specific IR1 optimizations get a chance. We blow
974 ;;; away any entry for the function in *FREE-FUNS* so that nobody
975 ;;; will create new references to it.
976 (defun let-convert (fun call)
977 (declare (type clambda fun) (type basic-combination call))
978 (let ((next-block (if (node-tail-p call)
980 (insert-let-body fun call))))
981 (move-return-stuff fun call next-block)
982 (merge-lets fun call)))
984 ;;; Reoptimize all of CALL's args and its result.
985 (defun reoptimize-call (call)
986 (declare (type basic-combination call))
987 (dolist (arg (basic-combination-args call))
989 (reoptimize-continuation arg)))
990 (reoptimize-continuation (node-cont call))
993 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
995 (defun declarations-suppress-let-conversion-p (clambda)
996 ;; From the user's point of view, LET-converting something that
997 ;; has a name is inlining it. (The user can't see what we're doing
998 ;; with anonymous things, and suppressing inlining
999 ;; for such things can easily give Python acute indigestion, so
1001 (when (leaf-has-source-name-p clambda)
1002 ;; ANSI requires that explicit NOTINLINE be respected.
1003 (or (eq (lambda-inlinep clambda) :notinline)
1004 ;; If (= LET-CONVERTION 0) we can guess that inlining
1005 ;; generally won't be appreciated, but if the user
1006 ;; specifically requests inlining, that takes precedence over
1007 ;; our general guess.
1008 (and (policy clambda (= let-convertion 0))
1009 (not (eq (lambda-inlinep clambda) :inline))))))
1011 ;;; We also don't convert calls to named functions which appear in the
1012 ;;; initial component, delaying this until optimization. This
1013 ;;; minimizes the likelihood that we will LET-convert a function which
1014 ;;; may have references added due to later local inline expansion.
1015 (defun ok-initial-convert-p (fun)
1016 (not (and (leaf-has-source-name-p fun)
1017 (or (declarations-suppress-let-conversion-p fun)
1018 (eq (component-kind (lambda-component fun))
1021 ;;; This function is called when there is some reason to believe that
1022 ;;; CLAMBDA might be converted into a LET. This is done after local
1023 ;;; call analysis, and also when a reference is deleted. We return
1024 ;;; true if we converted.
1025 (defun maybe-let-convert (clambda)
1026 (declare (type clambda clambda))
1027 (unless (declarations-suppress-let-conversion-p clambda)
1028 ;; We only convert to a LET when the function is a normal local
1029 ;; function, has no XEP, and is referenced in exactly one local
1030 ;; call. Conversion is also inhibited if the only reference is in
1031 ;; a block about to be deleted.
1033 ;; These rules limiting LET conversion may seem unnecessarily
1034 ;; restrictive, since there are some cases where we could do the
1035 ;; return with a jump that don't satisfy these requirements. The
1036 ;; reason for doing things this way is that it makes the concept
1037 ;; of a LET much more useful at the level of IR1 semantics. The
1038 ;; :ASSIGNMENT function kind provides another way to optimize
1039 ;; calls to single-return/multiple call functions.
1041 ;; We don't attempt to convert calls to functions that have an
1042 ;; XEP, since we might be embarrassed later when we want to
1043 ;; convert a newly discovered local call. Also, see
1044 ;; OK-INITIAL-CONVERT-P.
1045 (let ((refs (leaf-refs clambda)))
1048 (member (functional-kind clambda) '(nil :assignment))
1049 (not (functional-entry-fun clambda)))
1050 (let* ((ref (first refs))
1051 (ref-cont (node-cont ref))
1052 (dest (continuation-dest ref-cont)))
1054 (basic-combination-p dest)
1055 (eq (basic-combination-fun dest) ref-cont)
1056 (eq (basic-combination-kind dest) :local)
1057 (not (block-delete-p (node-block dest)))
1058 (cond ((ok-initial-convert-p clambda) t)
1060 (reoptimize-continuation ref-cont)
1062 (when (eq clambda (node-home-lambda dest))
1063 (delete-lambda clambda)
1064 (return-from maybe-let-convert nil))
1065 (unless (eq (functional-kind clambda) :assignment)
1066 (let-convert clambda dest))
1067 (reoptimize-call dest)
1068 (setf (functional-kind clambda)
1069 (if (mv-combination-p dest) :mv-let :let))))
1072 ;;;; tail local calls and assignments
1074 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1075 ;;; they definitely won't generate any cleanup code. Currently we
1076 ;;; recognize lexical entry points that are only used locally (if at
1078 (defun only-harmless-cleanups (block1 block2)
1079 (declare (type cblock block1 block2))
1080 (or (eq block1 block2)
1081 (let ((cleanup2 (block-start-cleanup block2)))
1082 (do ((cleanup (block-end-cleanup block1)
1083 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1084 ((eq cleanup cleanup2) t)
1085 (case (cleanup-kind cleanup)
1087 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1089 (t (return nil)))))))
1091 ;;; If a potentially TR local call really is TR, then convert it to
1092 ;;; jump directly to the called function. We also call
1093 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1094 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
1095 ;;; the succesor (potentially deleting the RETURN node) unless:
1096 ;;; -- The call has already been converted.
1097 ;;; -- The call isn't TR (some implicit MV PROG1.)
1098 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
1099 ;;; so that we can use known return inside the component.)
1100 ;;; -- There is a change in the cleanup between the call in the return,
1101 ;;; so we might need to introduce cleanup code.
1102 (defun maybe-convert-tail-local-call (call)
1103 (declare (type combination call))
1104 (let ((return (continuation-dest (node-cont call))))
1105 (aver (return-p return))
1106 (when (and (not (node-tail-p call))
1107 (immediately-used-p (return-result return) call)
1108 (not (eq (functional-kind (node-home-lambda call))
1110 (only-harmless-cleanups (node-block call)
1111 (node-block return)))
1112 (node-ends-block call)
1113 (let ((block (node-block call))
1114 (fun (combination-lambda call)))
1115 (setf (node-tail-p call) t)
1116 (unlink-blocks block (first (block-succ block)))
1117 (link-blocks block (lambda-block fun))
1118 (values t (maybe-convert-to-assignment fun))))))
1120 ;;; This is called when we believe it might make sense to convert
1121 ;;; CLAMBDA to an assignment. All this function really does is
1122 ;;; determine when a function with more than one call can still be
1123 ;;; combined with the calling function's environment. We can convert
1125 ;;; -- The function is a normal, non-entry function, and
1126 ;;; -- Except for one call, all calls must be tail recursive calls
1127 ;;; in the called function (i.e. are self-recursive tail calls)
1128 ;;; -- OK-INITIAL-CONVERT-P is true.
1130 ;;; There may be one outside call, and it need not be tail-recursive.
1131 ;;; Since all tail local calls have already been converted to direct
1132 ;;; transfers, the only control semantics needed are to splice in the
1133 ;;; body at the non-tail call. If there is no non-tail call, then we
1134 ;;; need only merge the environments. Both cases are handled by
1137 ;;; ### It would actually be possible to allow any number of outside
1138 ;;; calls as long as they all return to the same place (i.e. have the
1139 ;;; same conceptual continuation.) A special case of this would be
1140 ;;; when all of the outside calls are tail recursive.
1141 (defun maybe-convert-to-assignment (clambda)
1142 (declare (type clambda clambda))
1143 (when (and (not (functional-kind clambda))
1144 (not (functional-entry-fun clambda)))
1145 (let ((outside-non-tail-call nil)
1147 (when (and (dolist (ref (leaf-refs clambda) t)
1148 (let ((dest (continuation-dest (node-cont ref))))
1149 (when (or (not dest)
1150 (block-delete-p (node-block dest)))
1152 (let ((home (node-home-lambda ref)))
1153 (unless (eq home clambda)
1156 (setq outside-call dest))
1157 (unless (node-tail-p dest)
1158 (when (or outside-non-tail-call (eq home clambda))
1160 (setq outside-non-tail-call dest)))))
1161 (ok-initial-convert-p clambda))
1162 (cond (outside-call (setf (functional-kind clambda) :assignment)
1163 (let-convert clambda outside-call)
1164 (when outside-non-tail-call
1165 (reoptimize-call outside-non-tail-call))
1167 (t (delete-lambda clambda)