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 (pushnew fun (lambda-calls-or-closes (node-home-lambda call)))
93 (merge-tail-sets call fun)
94 (change-ref-leaf ref fun)
97 ;;;; external entry point creation
99 ;;; Return a LAMBDA form that can be used as the definition of the XEP
102 ;;; If FUN is a LAMBDA, then we check the number of arguments
103 ;;; (conditional on policy) and call FUN with all the arguments.
105 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
106 ;;; of supplied arguments by doing do an = test for each entry-point,
107 ;;; calling the entry with the appropriate prefix of the passed
110 ;;; If there is a &MORE arg, then there are a couple of optimizations
111 ;;; that we make (more for space than anything else):
112 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
113 ;;; no argument count error is possible.
114 ;;; -- We can omit the = clause for the last entry-point, allowing the
115 ;;; case of 0 more args to fall through to the more entry.
117 ;;; We don't bother to policy conditionalize wrong arg errors in
118 ;;; optional dispatches, since the additional overhead is negligible
119 ;;; compared to the cost of everything else going on.
121 ;;; Note that if policy indicates it, argument type declarations in
122 ;;; FUN will be verified. Since nothing is known about the type of the
123 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
124 ;;; are passed to the actual function.
125 (defun make-xep-lambda-expression (fun)
126 (declare (type functional fun))
129 (let ((nargs (length (lambda-vars fun)))
130 (n-supplied (gensym))
131 (temps (make-gensym-list (length (lambda-vars fun)))))
132 `(lambda (,n-supplied ,@temps)
133 (declare (type index ,n-supplied))
134 ,(if (policy *lexenv* (zerop verify-arg-count))
135 `(declare (ignore ,n-supplied))
136 `(%verify-arg-count ,n-supplied ,nargs))
138 (declare (optimize (merge-tail-calls 3)))
139 (%funcall ,fun ,@temps)))))
141 (let* ((min (optional-dispatch-min-args fun))
142 (max (optional-dispatch-max-args fun))
143 (more (optional-dispatch-more-entry fun))
144 (n-supplied (gensym))
145 (temps (make-gensym-list max)))
147 (do ((eps (optional-dispatch-entry-points fun) (rest eps))
150 (entries `((= ,n-supplied ,n)
151 (%funcall ,(first eps) ,@(subseq temps 0 n)))))
152 `(lambda (,n-supplied ,@temps)
153 ;; FIXME: Make sure that INDEX type distinguishes between
154 ;; target and host. (Probably just make the SB!XC:DEFTYPE
155 ;; different from CL:DEFTYPE.)
156 (declare (type index ,n-supplied))
158 ,@(if more (butlast (entries)) (entries))
160 `((,(if (zerop min) t `(>= ,n-supplied ,max))
161 ,(let ((n-context (gensym))
163 `(multiple-value-bind (,n-context ,n-count)
164 (%more-arg-context ,n-supplied ,max)
166 (declare (optimize (merge-tail-calls 3)))
167 (%funcall ,more ,@temps ,n-context ,n-count)))))))
169 (%arg-count-error ,n-supplied)))))))))
171 ;;; Make an external entry point (XEP) for FUN and return it. We
172 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
173 ;;; then associate this lambda with FUN as its XEP. After the
174 ;;; conversion, we iterate over the function's associated lambdas,
175 ;;; redoing local call analysis so that the XEP calls will get
178 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
179 ;;; discover an XEP after the initial local call analyze pass.
180 (defun make-xep (fun)
181 (declare (type functional fun))
182 (aver (null (functional-entry-fun fun)))
183 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
184 (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
185 :debug-name (debug-namify
187 (leaf-debug-name fun)))))
188 (setf (functional-kind res) :external
189 (leaf-ever-used res) t
190 (functional-entry-fun res) fun
191 (functional-entry-fun fun) res
192 (component-reanalyze *current-component*) t
193 (component-reoptimize *current-component*) t)
196 (locall-analyze-fun-1 fun))
198 (dolist (ep (optional-dispatch-entry-points fun))
199 (locall-analyze-fun-1 ep))
200 (when (optional-dispatch-more-entry fun)
201 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
204 ;;; Notice a REF that is not in a local-call context. If the REF is
205 ;;; already to an XEP, then do nothing, otherwise change it to the
206 ;;; XEP, making an XEP if necessary.
208 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
209 ;;; it as though it was not an XEP reference (i.e. leave it alone).
210 (defun reference-entry-point (ref)
211 (declare (type ref ref))
212 (let ((fun (ref-leaf ref)))
213 (unless (or (xep-p fun)
214 (member (functional-kind fun) '(:escape :cleanup)))
215 (change-ref-leaf ref (or (functional-entry-fun fun)
218 ;;; Attempt to convert all references to FUN to local calls. The
219 ;;; reference must be the function for a call, and the function
220 ;;; continuation must be used only once, since otherwise we cannot be
221 ;;; sure what function is to be called. The call continuation would be
222 ;;; multiply used if there is hairy stuff such as conditionals in the
223 ;;; expression that computes the function.
225 ;;; If we cannot convert a reference, then we mark the referenced
226 ;;; function as an entry-point, creating a new XEP if necessary. We
227 ;;; don't try to convert calls that are in error (:ERROR kind.)
229 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
230 ;;; can force analysis of newly introduced calls. Note that we don't
231 ;;; do LET conversion here.
232 (defun locall-analyze-fun-1 (fun)
233 (declare (type functional fun))
234 (let ((refs (leaf-refs fun))
237 (let* ((cont (node-cont ref))
238 (dest (continuation-dest cont)))
239 (cond ((and (basic-combination-p dest)
240 (eq (basic-combination-fun dest) cont)
241 (eq (continuation-use cont) ref))
243 (convert-call-if-possible ref dest)
245 (unless (eq (basic-combination-kind dest) :local)
246 (reference-entry-point ref)))
248 (reference-entry-point ref))))
249 (setq first-time nil)))
253 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
254 ;;; calls into local calls when it is legal. We also attempt to
255 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
256 ;;; deletion of a function reference, but functions that start out
257 ;;; eligible for conversion must be noticed sometime.
259 ;;; Note that there is a lot of action going on behind the scenes
260 ;;; here, triggered by reference deletion. In particular, the
261 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
262 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
263 ;;; the COMPONENT-LAMBDAS when it is. Also, the
264 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
265 ;;; it is not updated when we delete functions, etc. Only
266 ;;; COMPONENT-LAMBDAS is updated.
268 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
269 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
271 (defun locall-analyze-component (component)
272 (declare (type component component))
273 (aver-live-component component)
275 (let* ((new-functional (pop (component-new-functionals component)))
276 (functional (or new-functional
277 (pop (component-reanalyze-functionals component)))))
280 (let ((kind (functional-kind functional)))
281 (cond ((or (functional-somewhat-letlike-p functional)
283 (values)) ; nothing to do
284 ((and (null (leaf-refs functional)) (eq kind nil)
285 (not (functional-entry-fun functional)))
286 (delete-functional functional))
288 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
289 (cond ((not (lambda-p functional))
290 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
293 (new-functional ; FUNCTIONAL came from
294 ; NEW-FUNCTIONALS, hence is new.
295 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
296 (aver (not (member functional
297 (component-lambdas component))))
298 (push functional (component-lambdas component)))
299 (t ; FUNCTIONAL is old.
300 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
301 (aver (member functional (component-lambdas
303 (locall-analyze-fun-1 functional)
304 (when (lambda-p functional)
305 (maybe-let-convert functional)))))))
308 (defun locall-analyze-clambdas-until-done (clambdas)
310 (let ((did-something nil))
311 (dolist (clambda clambdas)
312 (let* ((component (lambda-component clambda))
313 (*all-components* (list component)))
314 ;; The original CMU CL code seemed to implicitly assume that
315 ;; COMPONENT is the only one here. Let's make that explicit.
316 (aver (= 1 (length (functional-components clambda))))
317 (aver (eql component (first (functional-components clambda))))
318 (when (component-new-functionals component)
319 (setf did-something t)
320 (locall-analyze-component component))))
321 (unless did-something
325 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
326 ;;; to be in an infinite recursive loop, then change the reference to
327 ;;; reference a fresh copy. We return whichever function we decide to
329 (defun maybe-expand-local-inline (original-functional ref call)
330 (if (and (policy call
331 (and (>= speed space)
332 (>= speed compilation-speed)))
333 (not (eq (functional-kind (node-home-lambda call)) :external))
334 (inline-expansion-ok call))
335 (multiple-value-bind (losing-local-functional converted-lambda)
336 (catch 'locall-already-let-converted
337 (with-ir1-environment-from-node call
338 (let ((*lexenv* (functional-lexenv original-functional)))
341 (functional-inline-expansion original-functional)
342 :debug-name (debug-namify
345 original-functional)))))))
346 (cond (losing-local-functional
347 (let ((*compiler-error-context* call))
348 (compiler-note "couldn't inline expand because expansion ~
349 calls this LET-converted local function:~
351 (leaf-debug-name losing-local-functional)))
354 (change-ref-leaf ref converted-lambda)
356 original-functional))
358 ;;; Dispatch to the appropriate function to attempt to convert a call.
359 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
360 ;;; optimization as well as in local call analysis. If the call is is
361 ;;; already :LOCAL, we do nothing. If the call is already scheduled
362 ;;; for deletion, also do nothing (in addition to saving time, this
363 ;;; also avoids some problems with optimizing collections of functions
364 ;;; that are partially deleted.)
366 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
367 ;;; called on a :INITIAL component, we don't care whether the caller
368 ;;; and callee are in the same component. Afterward, we must stick
369 ;;; with whatever component division we have chosen.
371 ;;; Before attempting to convert a call, we see whether the function
372 ;;; is supposed to be inline expanded. Call conversion proceeds as
373 ;;; before after any expansion.
375 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
376 ;;; warnings will get the right context.
377 (defun convert-call-if-possible (ref call)
378 (declare (type ref ref) (type basic-combination call))
379 (let* ((block (node-block call))
380 (component (block-component block))
381 (original-fun (ref-leaf ref)))
382 (aver (functional-p original-fun))
383 (unless (or (member (basic-combination-kind call) '(:local :error))
384 (block-delete-p block)
385 (eq (functional-kind (block-home-lambda block)) :deleted)
386 (member (functional-kind original-fun)
387 '(:toplevel-xep :deleted))
388 (not (or (eq (component-kind component) :initial)
391 (lambda-bind (main-entry original-fun))))
393 (let ((fun (if (xep-p original-fun)
394 (functional-entry-fun original-fun)
396 (*compiler-error-context* call))
398 (when (and (eq (functional-inlinep fun) :inline)
399 (rest (leaf-refs original-fun)))
400 (setq fun (maybe-expand-local-inline fun ref call)))
402 (aver (member (functional-kind fun)
403 '(nil :escape :cleanup :optional)))
404 (cond ((mv-combination-p call)
405 (convert-mv-call ref call fun))
407 (convert-lambda-call ref call fun))
409 (convert-hairy-call ref call fun))))))
413 ;;; Attempt to convert a multiple-value call. The only interesting
414 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
415 ;;; exactly one reference and no XEP, and is called with one values
418 ;;; We change the call to be to the last optional entry point and
419 ;;; change the call to be local. Due to our preconditions, the call
420 ;;; should eventually be converted to a let, but we can't do that now,
421 ;;; since there may be stray references to the e-p lambda due to
422 ;;; optional defaulting code.
424 ;;; We also use variable types for the called function to construct an
425 ;;; assertion for the values continuation.
427 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
428 (defun convert-mv-call (ref call fun)
429 (declare (type ref ref) (type mv-combination call) (type functional fun))
430 (when (and (looks-like-an-mv-bind fun)
431 (not (functional-entry-fun fun))
432 (= (length (leaf-refs fun)) 1)
433 (= (length (basic-combination-args call)) 1))
434 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
435 (setf (basic-combination-kind call) :local)
436 (pushnew ep (lambda-calls-or-closes (node-home-lambda call)))
437 (merge-tail-sets call ep)
438 (change-ref-leaf ref ep)
440 (assert-continuation-type
441 (first (basic-combination-args call))
442 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
443 (lexenv-policy (node-lexenv call)))))
446 ;;; Attempt to convert a call to a lambda. If the number of args is
447 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
448 ;;; from future consideration. If the argcount is O.K. then we just
450 (defun convert-lambda-call (ref call fun)
451 (declare (type ref ref) (type combination call) (type clambda fun))
452 (let ((nargs (length (lambda-vars fun)))
453 (call-args (length (combination-args call))))
454 (cond ((= call-args nargs)
455 (convert-call ref call fun))
457 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
458 ;; Compiler" that calling a function with "the wrong number of
459 ;; arguments" be only a STYLE-ERROR. I think, though, that this
460 ;; should only apply when the number of arguments is inferred
461 ;; from a previous definition. If the number of arguments
462 ;; is DECLAIMed, surely calling with the wrong number is a
463 ;; real WARNING. As long as SBCL continues to use CMU CL's
464 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
465 ;; but as long as we continue to use that policy, that's the
466 ;; not our biggest problem.:-| When we fix that policy, this
467 ;; should come back into compliance. (So fix that policy!)
469 ;; FIXME, continued: Except that section "3.2.2.3 Semantic
470 ;; Constraints" says that if it's within the same file, it's
471 ;; wrong. And we're in locall.lisp here, so it's probably
472 ;; (haven't checked this..) a call to something in the same
473 ;; file. So maybe it deserves a full warning anyway.
475 "function called with ~R argument~:P, but wants exactly ~R"
477 (setf (basic-combination-kind call) :error)))))
479 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
481 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
482 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
483 ;;; a call to the correct entry point. If &KEY args are supplied, then
484 ;;; dispatch to a subfunction. We don't convert calls to functions
485 ;;; that have a &MORE (or &REST) arg.
486 (defun convert-hairy-call (ref call fun)
487 (declare (type ref ref) (type combination call)
488 (type optional-dispatch fun))
489 (let ((min-args (optional-dispatch-min-args fun))
490 (max-args (optional-dispatch-max-args fun))
491 (call-args (length (combination-args call))))
492 (cond ((< call-args min-args)
493 ;; FIXME: See FIXME note at the previous
494 ;; wrong-number-of-arguments warnings in this file.
496 "function called with ~R argument~:P, but wants at least ~R"
498 (setf (basic-combination-kind call) :error))
499 ((<= call-args max-args)
500 (convert-call ref call
501 (elt (optional-dispatch-entry-points fun)
502 (- call-args min-args))))
503 ((optional-dispatch-more-entry fun)
504 (convert-more-call ref call fun))
506 ;; FIXME: See FIXME note at the previous
507 ;; wrong-number-of-arguments warnings in this file.
509 "function called with ~R argument~:P, but wants at most ~R"
511 (setf (basic-combination-kind call) :error))))
514 ;;; This function is used to convert a call to an entry point when
515 ;;; complex transformations need to be done on the original arguments.
516 ;;; ENTRY is the entry point function that we are calling. VARS is a
517 ;;; list of variable names which are bound to the original call
518 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
519 ;;; is the list of arguments to the entry point function.
521 ;;; In order to avoid gruesome graph grovelling, we introduce a new
522 ;;; function that rearranges the arguments and calls the entry point.
523 ;;; We analyze the new function and the entry point immediately so
524 ;;; that everything gets converted during the single pass.
525 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
526 (declare (list vars ignores args) (type ref ref) (type combination call)
527 (type clambda entry))
529 (with-ir1-environment-from-node call
532 (declare (ignorable ,@ignores))
533 (%funcall ,entry ,@args))
534 :debug-name (debug-namify "hairy function entry ~S"
535 (continuation-fun-name
536 (basic-combination-fun call)))))))
537 (convert-call ref call new-fun)
538 (dolist (ref (leaf-refs entry))
539 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
541 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
542 ;;; function into a local call to the MAIN-ENTRY.
544 ;;; First we verify that all keywords are constant and legal. If there
545 ;;; aren't, then we warn the user and don't attempt to convert the call.
547 ;;; We massage the supplied &KEY arguments into the order expected
548 ;;; by the main entry. This is done by binding all the arguments to
549 ;;; the keyword call to variables in the introduced lambda, then
550 ;;; passing these values variables in the correct order when calling
551 ;;; the main entry. Unused arguments (such as the keywords themselves)
552 ;;; are discarded simply by not passing them along.
554 ;;; If there is a &REST arg, then we bundle up the args and pass them
556 (defun convert-more-call (ref call fun)
557 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
558 (let* ((max (optional-dispatch-max-args fun))
559 (arglist (optional-dispatch-arglist fun))
560 (args (combination-args call))
561 (more (nthcdr max args))
562 (flame (policy call (or (> speed inhibit-warnings)
563 (> space inhibit-warnings))))
567 (temps (make-gensym-list max))
568 (more-temps (make-gensym-list (length more))))
573 (dolist (var arglist)
574 (let ((info (lambda-var-arg-info var)))
576 (ecase (arg-info-kind info)
580 ((:more-context :more-count)
581 (compiler-warn "can't local-call functions with &MORE args")
582 (setf (basic-combination-kind call) :error)
583 (return-from convert-more-call))))))
585 (when (optional-dispatch-keyp fun)
586 (when (oddp (length more))
587 (compiler-warn "function called with odd number of ~
588 arguments in keyword portion")
590 (setf (basic-combination-kind call) :error)
591 (return-from convert-more-call))
593 (do ((key more (cddr key))
594 (temp more-temps (cddr temp)))
596 (let ((cont (first key)))
597 (unless (constant-continuation-p cont)
599 (compiler-note "non-constant keyword in keyword call"))
600 (setf (basic-combination-kind call) :error)
601 (return-from convert-more-call))
603 (let ((name (continuation-value cont))
606 ;; FIXME: check whether KEY was supplied earlier
607 (when (and (eq name :allow-other-keys) (not allow-found))
608 (let ((val (second key)))
609 (cond ((constant-continuation-p val)
611 allowp (continuation-value val)))
613 (compiler-note "non-constant :ALLOW-OTHER-KEYS value"))
614 (setf (basic-combination-kind call) :error)
615 (return-from convert-more-call)))))
616 (dolist (var (key-vars)
619 (unless (eq name :allow-other-keys)
621 (let ((info (lambda-var-arg-info var)))
622 (when (eq (arg-info-key info) name)
624 (supplied (cons var val))
627 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
628 (compiler-warn "function called with unknown argument keyword ~S"
630 (setf (basic-combination-kind call) :error)
631 (return-from convert-more-call)))
633 (collect ((call-args))
634 (do ((var arglist (cdr var))
635 (temp temps (cdr temp)))
637 (let ((info (lambda-var-arg-info (car var))))
639 (ecase (arg-info-kind info)
641 (call-args (car temp))
642 (when (arg-info-supplied-p info)
645 (call-args `(list ,@more-temps))
649 (call-args (car temp)))))
651 (dolist (var (key-vars))
652 (let ((info (lambda-var-arg-info var))
653 (temp (cdr (assoc var (supplied)))))
656 (call-args (arg-info-default info)))
657 (when (arg-info-supplied-p info)
658 (call-args (not (null temp))))))
660 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
661 (append temps more-temps)
662 (ignores) (call-args)))))
668 ;;;; Converting to a LET has differing significance to various parts
669 ;;;; of the compiler:
670 ;;;; -- The body of a LET is spliced in immediately after the
671 ;;;; corresponding combination node, making the control transfer
672 ;;;; explicit and allowing LETs to be mashed together into a single
673 ;;;; block. The value of the LET is delivered directly to the
674 ;;;; original continuation for the call, eliminating the need to
675 ;;;; propagate information from the dummy result continuation.
676 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
677 ;;;; that there is only one expression that the variable can be bound
678 ;;;; to, and this is easily substituted for.
679 ;;;; -- LETs are interesting to environment analysis and to the back
680 ;;;; end because in most ways a LET can be considered to be "the
681 ;;;; same function" as its home function.
682 ;;;; -- LET conversion has dynamic scope implications, since control
683 ;;;; transfers within the same environment are local. In a local
684 ;;;; control transfer, cleanup code must be emitted to remove
685 ;;;; dynamic bindings that are no longer in effect.
687 ;;; Set up the control transfer to the called CLAMBDA. We split the
688 ;;; call block immediately after the call, and link the head of
689 ;;; CLAMBDA to the call block. The successor block after splitting
690 ;;; (where we return to) is returned.
692 ;;; If the lambda is is a different component than the call, then we
693 ;;; call JOIN-COMPONENTS. This only happens in block compilation
694 ;;; before FIND-INITIAL-DFO.
695 (defun insert-let-body (clambda call)
696 (declare (type clambda clambda) (type basic-combination call))
697 (let* ((call-block (node-block call))
698 (bind-block (node-block (lambda-bind clambda)))
699 (component (block-component call-block)))
700 (aver-live-component component)
701 (let ((clambda-component (block-component bind-block)))
702 (unless (eq clambda-component component)
703 (aver (eq (component-kind component) :initial))
704 (join-components component clambda-component)))
705 (let ((*current-component* component))
706 (node-ends-block call))
707 ;; FIXME: Use DESTRUCTURING-BIND here, and grep for other
708 ;; uses of '=.*length' which could also be converted to use
709 ;; DESTRUCTURING-BIND or PROPER-LIST-OF-LENGTH-P.
710 (aver (= (length (block-succ call-block)) 1))
711 (let ((next-block (first (block-succ call-block))))
712 (unlink-blocks call-block next-block)
713 (link-blocks call-block bind-block)
716 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
717 ;;; same set as; but leave CLAMBDA with a valid tail set value of
718 ;;; its own, for the benefit of code which might try to pull
719 ;;; something out of it (e.g. return type).
720 (defun depart-from-tail-set (clambda)
721 ;; Until sbcl-0.pre7.37.flaky5.2, we did
722 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
723 ;; (SETF (TAIL-SET-FUNS TAILS)
724 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
725 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
726 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
727 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
728 ;; inconsistent, and perhaps unsafe, for us to think we're in the
729 ;; tail set. Unfortunately..
731 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
732 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
733 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
734 ;; which you called in order to get the address of an external LAMBDA):
735 ;; the external function was defined in terms of internal function,
736 ;; which was LET-converted, and then things blew up downstream when
737 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
738 ;; the now-NILed-out TAIL-SET. So..
740 ;; To deal with this problem, we no longer NIL out
741 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
742 ;; * If we're the only function in TAIL-SET-FUNS, it should
743 ;; be safe to leave ourself linked to it, and it to you.
744 ;; * If there are other functions in TAIL-SET-FUNS, then we're
745 ;; afraid of future optimizations on those functions causing
746 ;; the TAIL-SET object no longer to be valid to describe our
747 ;; return value. Thus, we delete ourselves from that object;
748 ;; but we save a newly-allocated tail-set, derived from the old
749 ;; one, for ourselves, for the use of later code (e.g.
750 ;; FINALIZE-XEP-DEFINITION) which might want to
751 ;; know about our return type.
752 (let* ((old-tail-set (lambda-tail-set clambda))
753 (old-tail-set-funs (tail-set-funs old-tail-set)))
754 (unless (= 1 (length old-tail-set-funs))
755 (setf (tail-set-funs old-tail-set)
756 (delete clambda old-tail-set-funs))
757 (let ((new-tail-set (copy-tail-set old-tail-set)))
758 (setf (lambda-tail-set clambda) new-tail-set
759 (tail-set-funs new-tail-set) (list clambda)))))
760 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
761 ;; remain valid in this case, so we nuke it on the theory that
762 ;; missing information tends to be less dangerous than incorrect
764 (setf (tail-set-info (lambda-tail-set clambda)) nil))
766 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
767 ;;; its LETs to LETs for the CALL's home function. We merge the calls
768 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
769 ;;; in the process. We also merge the ENTRIES.
771 ;;; We also unlink the function head from the component head and set
772 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
774 (defun merge-lets (clambda call)
776 (declare (type clambda clambda) (type basic-combination call))
778 (let ((component (node-component call)))
779 (unlink-blocks (component-head component) (lambda-block clambda))
780 (setf (component-lambdas component)
781 (delete clambda (component-lambdas component)))
782 (setf (component-reanalyze component) t))
783 (setf (lambda-call-lexenv clambda) (node-lexenv call))
785 (depart-from-tail-set clambda)
787 (let* ((home (node-home-lambda call))
788 (home-physenv (lambda-physenv home)))
790 (aver (not (eq home clambda)))
792 ;; CLAMBDA belongs to HOME now.
793 (push clambda (lambda-lets home))
794 (setf (lambda-home clambda) home)
795 (setf (lambda-physenv clambda) home-physenv)
797 ;; All of CLAMBDA's LETs belong to HOME now.
798 (let ((lets (lambda-lets clambda)))
800 (setf (lambda-home let) home)
801 (setf (lambda-physenv let) home-physenv))
802 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
803 ;; CLAMBDA no longer has an independent existence as an entity
805 (setf (lambda-lets clambda) nil)
807 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
809 (setf (lambda-calls-or-closes home)
811 (nunion (lambda-calls-or-closes clambda)
812 (lambda-calls-or-closes home))))
813 ;; CLAMBDA no longer has an independent existence as an entity
814 ;; which calls things or has DFO dependencies.
815 (setf (lambda-calls-or-closes clambda) nil)
817 ;; All of CLAMBDA's ENTRIES belong to HOME now.
818 (setf (lambda-entries home)
819 (nconc (lambda-entries clambda)
820 (lambda-entries home)))
821 ;; CLAMBDA no longer has an independent existence as an entity
823 (setf (lambda-entries clambda) nil))
827 ;;; Handle the value semantics of LET conversion. Delete FUN's return
828 ;;; node, and change the control flow to transfer to NEXT-BLOCK
829 ;;; instead. Move all the uses of the result continuation to CALL's
831 (defun move-return-uses (fun call next-block)
832 (declare (type clambda fun) (type basic-combination call)
833 (type cblock next-block))
834 (let* ((return (lambda-return fun))
835 (return-block (node-block return)))
836 (unlink-blocks return-block
837 (component-tail (block-component return-block)))
838 (link-blocks return-block next-block)
840 (delete-return return)
841 (let ((result (return-result return))
842 (cont (node-cont call))
843 (call-type (node-derived-type call)))
844 (unless (eq call-type *wild-type*)
845 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2002-01-26
846 (do-uses (use result)
847 (derive-node-type use call-type)))
848 (substitute-continuation-uses cont result)))
851 ;;; Change all CONT for all the calls to FUN to be the start
852 ;;; continuation for the bind node. This allows the blocks to be
853 ;;; joined if the caller count ever goes to one.
854 (defun move-let-call-cont (fun)
855 (declare (type clambda fun))
856 (let ((new-cont (node-prev (lambda-bind fun))))
857 (dolist (ref (leaf-refs fun))
858 (let ((dest (continuation-dest (node-cont ref))))
859 (delete-continuation-use dest)
860 (add-continuation-use dest new-cont))))
863 ;;; We are converting FUN to be a LET when the call is in a non-tail
864 ;;; position. Any previously tail calls in FUN are no longer tail
865 ;;; calls, and must be restored to normal calls which transfer to
866 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
867 ;;; the RETURN-RESULT, because the return might have been deleted (if
868 ;;; all calls were TR.)
869 (defun unconvert-tail-calls (fun call next-block)
870 (dolist (called (lambda-calls-or-closes fun))
871 (when (lambda-p called)
872 (dolist (ref (leaf-refs called))
873 (let ((this-call (continuation-dest (node-cont ref))))
875 (node-tail-p this-call)
876 (eq (node-home-lambda this-call) fun))
877 (setf (node-tail-p this-call) nil)
878 (ecase (functional-kind called)
879 ((nil :cleanup :optional)
880 (let ((block (node-block this-call))
881 (cont (node-cont call)))
882 (ensure-block-start cont)
883 (unlink-blocks block (first (block-succ block)))
884 (link-blocks block next-block)
885 (delete-continuation-use this-call)
886 (add-continuation-use this-call cont)))
888 ;; The called function might be an assignment in the
889 ;; case where we are currently converting that function.
890 ;; In steady-state, assignments never appear as a called
893 (aver (eq called fun)))))))))
896 ;;; Deal with returning from a LET or assignment that we are
897 ;;; converting. FUN is the function we are calling, CALL is a call to
898 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
899 ;;; NULL if call is a tail call.
901 ;;; If the call is not a tail call, then we must do
902 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
903 ;;; its value out of the enclosing non-let function. When call is
904 ;;; non-TR, we must convert it back to an ordinary local call, since
905 ;;; the value must be delivered to the receiver of CALL's value.
907 ;;; We do different things depending on whether the caller and callee
908 ;;; have returns left:
910 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
911 ;;; Either the function doesn't return, or all returns are via
912 ;;; tail-recursive local calls.
913 ;;; -- If CALL is a non-tail call, or if both have returns, then
914 ;;; we delete the callee's return, move its uses to the call's
915 ;;; result continuation, and transfer control to the appropriate
917 ;;; -- If the callee has a return, but the caller doesn't, then we
918 ;;; move the return to the caller.
919 (defun move-return-stuff (fun call next-block)
920 (declare (type clambda fun) (type basic-combination call)
921 (type (or cblock null) next-block))
923 (unconvert-tail-calls fun call next-block))
924 (let* ((return (lambda-return fun))
925 (call-fun (node-home-lambda call))
926 (call-return (lambda-return call-fun)))
928 ((or next-block call-return)
929 (unless (block-delete-p (node-block return))
930 (when (and (node-tail-p call)
932 (not (eq (node-cont call)
933 (return-result call-return))))
934 ;; We do not care to give a meaningful continuation to
935 ;; a tail combination, but here we need it.
936 (delete-continuation-use call)
937 (add-continuation-use call (return-result call-return)))
938 (move-return-uses fun call
940 (let ((block (node-block call-return)))
941 (when (block-delete-p block)
942 (setf (block-delete-p block) nil))
945 (aver (node-tail-p call))
946 (setf (lambda-return call-fun) return)
947 (setf (return-lambda return) call-fun)
948 (setf (lambda-return fun) nil))))
949 (move-let-call-cont fun)
952 ;;; Actually do LET conversion. We call subfunctions to do most of the
953 ;;; work. We change the CALL's CONT to be the continuation heading the
954 ;;; BIND block, and also do REOPTIMIZE-CONTINUATION on the args and
955 ;;; CONT so that LET-specific IR1 optimizations get a chance. We blow
956 ;;; away any entry for the function in *FREE-FUNS* so that nobody
957 ;;; will create new references to it.
958 (defun let-convert (fun call)
959 (declare (type clambda fun) (type basic-combination call))
960 (let ((next-block (if (node-tail-p call)
962 (insert-let-body fun call))))
963 (move-return-stuff fun call next-block)
964 (merge-lets fun call)))
966 ;;; Reoptimize all of CALL's args and its result.
967 (defun reoptimize-call (call)
968 (declare (type basic-combination call))
969 (dolist (arg (basic-combination-args call))
971 (reoptimize-continuation arg)))
972 (reoptimize-continuation (node-cont call))
975 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
977 (defun declarations-suppress-let-conversion-p (clambda)
978 ;; From the user's point of view, LET-converting something that
979 ;; has a name is inlining it. (The user can't see what we're doing
980 ;; with anonymous things, and suppressing inlining
981 ;; for such things can easily give Python acute indigestion, so
983 (when (leaf-has-source-name-p clambda)
984 ;; ANSI requires that explicit NOTINLINE be respected.
985 (or (eq (lambda-inlinep clambda) :notinline)
986 ;; If (= LET-CONVERTION 0) we can guess that inlining
987 ;; generally won't be appreciated, but if the user
988 ;; specifically requests inlining, that takes precedence over
989 ;; our general guess.
990 (and (policy clambda (= let-convertion 0))
991 (not (eq (lambda-inlinep clambda) :inline))))))
993 ;;; We also don't convert calls to named functions which appear in the
994 ;;; initial component, delaying this until optimization. This
995 ;;; minimizes the likelihood that we will LET-convert a function which
996 ;;; may have references added due to later local inline expansion.
997 (defun ok-initial-convert-p (fun)
998 (not (and (leaf-has-source-name-p fun)
999 (or (declarations-suppress-let-conversion-p fun)
1000 (eq (component-kind (lambda-component fun))
1003 ;;; This function is called when there is some reason to believe that
1004 ;;; CLAMBDA might be converted into a LET. This is done after local
1005 ;;; call analysis, and also when a reference is deleted. We return
1006 ;;; true if we converted.
1007 (defun maybe-let-convert (clambda)
1008 (declare (type clambda clambda))
1009 (unless (declarations-suppress-let-conversion-p clambda)
1010 ;; We only convert to a LET when the function is a normal local
1011 ;; function, has no XEP, and is referenced in exactly one local
1012 ;; call. Conversion is also inhibited if the only reference is in
1013 ;; a block about to be deleted.
1015 ;; These rules limiting LET conversion may seem unnecessarily
1016 ;; restrictive, since there are some cases where we could do the
1017 ;; return with a jump that don't satisfy these requirements. The
1018 ;; reason for doing things this way is that it makes the concept
1019 ;; of a LET much more useful at the level of IR1 semantics. The
1020 ;; :ASSIGNMENT function kind provides another way to optimize
1021 ;; calls to single-return/multiple call functions.
1023 ;; We don't attempt to convert calls to functions that have an
1024 ;; XEP, since we might be embarrassed later when we want to
1025 ;; convert a newly discovered local call. Also, see
1026 ;; OK-INITIAL-CONVERT-P.
1027 (let ((refs (leaf-refs clambda)))
1030 (member (functional-kind clambda) '(nil :assignment))
1031 (not (functional-entry-fun clambda)))
1032 (let* ((ref (first refs))
1033 (ref-cont (node-cont ref))
1034 (dest (continuation-dest ref-cont)))
1036 (basic-combination-p dest)
1037 (eq (basic-combination-fun dest) ref-cont)
1038 (eq (basic-combination-kind dest) :local)
1039 (not (block-delete-p (node-block dest)))
1040 (cond ((ok-initial-convert-p clambda) t)
1042 (reoptimize-continuation ref-cont)
1044 (when (eq clambda (node-home-lambda dest))
1045 (delete-lambda clambda)
1046 (return-from maybe-let-convert nil))
1047 (unless (eq (functional-kind clambda) :assignment)
1048 (let-convert clambda dest))
1049 (reoptimize-call dest)
1050 (setf (functional-kind clambda)
1051 (if (mv-combination-p dest) :mv-let :let))))
1054 ;;;; tail local calls and assignments
1056 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1057 ;;; they definitely won't generate any cleanup code. Currently we
1058 ;;; recognize lexical entry points that are only used locally (if at
1060 (defun only-harmless-cleanups (block1 block2)
1061 (declare (type cblock block1 block2))
1062 (or (eq block1 block2)
1063 (let ((cleanup2 (block-start-cleanup block2)))
1064 (do ((cleanup (block-end-cleanup block1)
1065 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1066 ((eq cleanup cleanup2) t)
1067 (case (cleanup-kind cleanup)
1069 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1071 (t (return nil)))))))
1073 ;;; If a potentially TR local call really is TR, then convert it to
1074 ;;; jump directly to the called function. We also call
1075 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1076 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
1077 ;;; the succesor (potentially deleting the RETURN node) unless:
1078 ;;; -- The call has already been converted.
1079 ;;; -- The call isn't TR (some implicit MV PROG1.)
1080 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
1081 ;;; so that we can use known return inside the component.)
1082 ;;; -- There is a change in the cleanup between the call in the return,
1083 ;;; so we might need to introduce cleanup code.
1084 (defun maybe-convert-tail-local-call (call)
1085 (declare (type combination call))
1086 (let ((return (continuation-dest (node-cont call))))
1087 (aver (return-p return))
1088 (when (and (not (node-tail-p call))
1089 (immediately-used-p (return-result return) call)
1090 (not (eq (functional-kind (node-home-lambda call))
1092 (only-harmless-cleanups (node-block call)
1093 (node-block return)))
1094 (node-ends-block call)
1095 (let ((block (node-block call))
1096 (fun (combination-lambda call)))
1097 (setf (node-tail-p call) t)
1098 (unlink-blocks block (first (block-succ block)))
1099 (link-blocks block (lambda-block fun))
1100 (values t (maybe-convert-to-assignment fun))))))
1102 ;;; This is called when we believe it might make sense to convert
1103 ;;; CLAMBDA to an assignment. All this function really does is
1104 ;;; determine when a function with more than one call can still be
1105 ;;; combined with the calling function's environment. We can convert
1107 ;;; -- The function is a normal, non-entry function, and
1108 ;;; -- Except for one call, all calls must be tail recursive calls
1109 ;;; in the called function (i.e. are self-recursive tail calls)
1110 ;;; -- OK-INITIAL-CONVERT-P is true.
1112 ;;; There may be one outside call, and it need not be tail-recursive.
1113 ;;; Since all tail local calls have already been converted to direct
1114 ;;; transfers, the only control semantics needed are to splice in the
1115 ;;; body at the non-tail call. If there is no non-tail call, then we
1116 ;;; need only merge the environments. Both cases are handled by
1119 ;;; ### It would actually be possible to allow any number of outside
1120 ;;; calls as long as they all return to the same place (i.e. have the
1121 ;;; same conceptual continuation.) A special case of this would be
1122 ;;; when all of the outside calls are tail recursive.
1123 (defun maybe-convert-to-assignment (clambda)
1124 (declare (type clambda clambda))
1125 (when (and (not (functional-kind clambda))
1126 (not (functional-entry-fun clambda)))
1127 (let ((outside-non-tail-call nil)
1129 (when (and (dolist (ref (leaf-refs clambda) t)
1130 (let ((dest (continuation-dest (node-cont ref))))
1131 (when (or (not dest)
1132 (block-delete-p (node-block dest)))
1134 (let ((home (node-home-lambda ref)))
1135 (unless (eq home clambda)
1138 (setq outside-call dest))
1139 (unless (node-tail-p dest)
1140 (when (or outside-non-tail-call (eq home clambda))
1142 (setq outside-non-tail-call dest)))))
1143 (ok-initial-convert-p clambda))
1144 (cond (outside-call (setf (functional-kind clambda) :assignment)
1145 (let-convert clambda outside-call)
1146 (when outside-non-tail-call
1147 (reoptimize-call outside-non-tail-call))
1149 (t (delete-lambda clambda)