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 (do ((args (basic-combination-args call) (cdr args))
38 (vars (lambda-vars fun) (cdr vars)))
40 (let ((arg (car args))
42 (cond ((leaf-refs var)
43 (assert-continuation-type arg (leaf-type var)
44 (lexenv-policy (node-lexenv call))))
47 (setf (car args) nil)))))
51 ;;; This function handles merging the tail sets if CALL is potentially
52 ;;; tail-recursive, and is a call to a function with a different
53 ;;; TAIL-SET than CALL's FUN. This must be called whenever we alter
54 ;;; IR1 so as to place a local call in what might be a tail-recursive
55 ;;; context. Note that any call which returns its value to a RETURN is
56 ;;; considered potentially tail-recursive, since any implicit MV-PROG1
57 ;;; might be optimized away.
59 ;;; We destructively modify the set for the calling function to
60 ;;; represent both, and then change all the functions in callee's set
61 ;;; to reference the first. If we do merge, we reoptimize the
62 ;;; RETURN-RESULT continuation to cause IR1-OPTIMIZE-RETURN to
63 ;;; recompute the tail set type.
64 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
65 (declare (type basic-combination call) (type clambda new-fun))
66 (let ((return (continuation-dest (node-cont call))))
67 (when (return-p return)
68 (let ((call-set (lambda-tail-set (node-home-lambda call)))
69 (fun-set (lambda-tail-set new-fun)))
70 (unless (eq call-set fun-set)
71 (let ((funs (tail-set-funs fun-set)))
73 (setf (lambda-tail-set fun) call-set))
74 (setf (tail-set-funs call-set)
75 (nconc (tail-set-funs call-set) funs)))
76 (reoptimize-continuation (return-result return))
79 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
80 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
81 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
82 ;;; the function in the REF node with the new function.
84 ;;; We change the REF last, since changing the reference can trigger
85 ;;; LET conversion of the new function, but will only do so if the
86 ;;; call is local. Note that the replacement may trigger LET
87 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
88 ;;; with NEW-FUN before the substitution, since after the substitution
89 ;;; (and LET conversion), the call may no longer be recognizable as
91 (defun convert-call (ref call fun)
92 (declare (type ref ref) (type combination call) (type clambda fun))
93 (propagate-to-args call fun)
94 (setf (basic-combination-kind call) :local)
95 (pushnew fun (lambda-calls-or-closes (node-home-lambda call)))
96 (merge-tail-sets call fun)
97 (change-ref-leaf ref fun)
100 ;;;; external entry point creation
102 ;;; Return a LAMBDA form that can be used as the definition of the XEP
105 ;;; If FUN is a LAMBDA, then we check the number of arguments
106 ;;; (conditional on policy) and call FUN with all the arguments.
108 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
109 ;;; of supplied arguments by doing do an = test for each entry-point,
110 ;;; calling the entry with the appropriate prefix of the passed
113 ;;; If there is a &MORE arg, then there are a couple of optimizations
114 ;;; that we make (more for space than anything else):
115 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
116 ;;; no argument count error is possible.
117 ;;; -- We can omit the = clause for the last entry-point, allowing the
118 ;;; case of 0 more args to fall through to the more entry.
120 ;;; We don't bother to policy conditionalize wrong arg errors in
121 ;;; optional dispatches, since the additional overhead is negligible
122 ;;; compared to the cost of everything else going on.
124 ;;; Note that if policy indicates it, argument type declarations in
125 ;;; FUN will be verified. Since nothing is known about the type of the
126 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
127 ;;; are passed to the actual function.
128 (defun make-xep-lambda-expression (fun)
129 (declare (type functional fun))
132 (let ((nargs (length (lambda-vars fun)))
133 (n-supplied (gensym))
134 (temps (make-gensym-list (length (lambda-vars fun)))))
135 `(lambda (,n-supplied ,@temps)
136 (declare (type index ,n-supplied))
137 ,(if (policy *lexenv* (zerop safety))
138 `(declare (ignore ,n-supplied))
139 `(%verify-arg-count ,n-supplied ,nargs))
141 ;; KLUDGE: The intent here is to enable tail recursion
142 ;; optimization, since leaving frames for wrapper
143 ;; functions like this on the stack is actually more
144 ;; annoying than helpful for debugging. Unfortunately
145 ;; trying to express this by messing with the
146 ;; ANSI-standard declarations is a little awkward, since
147 ;; no matter how we do it we'll tend to have side-effects
148 ;; on things like SPEED-vs.-SAFETY comparisons. Perhaps
149 ;; it'd be better to define a new SB-EXT:TAIL-RECURSIVELY
150 ;; declaration and use that? -- WHN 2002-07-08
151 (declare (optimize (speed 2) (debug 1)))
152 (%funcall ,fun ,@temps)))))
154 (let* ((min (optional-dispatch-min-args fun))
155 (max (optional-dispatch-max-args fun))
156 (more (optional-dispatch-more-entry fun))
157 (n-supplied (gensym))
158 (temps (make-gensym-list max)))
160 (do ((eps (optional-dispatch-entry-points fun) (rest eps))
163 (entries `((= ,n-supplied ,n)
164 (%funcall ,(first eps) ,@(subseq temps 0 n)))))
165 `(lambda (,n-supplied ,@temps)
166 ;; FIXME: Make sure that INDEX type distinguishes between
167 ;; target and host. (Probably just make the SB!XC:DEFTYPE
168 ;; different from CL:DEFTYPE.)
169 (declare (type index ,n-supplied))
171 ,@(if more (butlast (entries)) (entries))
173 `((,(if (zerop min) t `(>= ,n-supplied ,max))
174 ,(let ((n-context (gensym))
176 `(multiple-value-bind (,n-context ,n-count)
177 (%more-arg-context ,n-supplied ,max)
179 ;; KLUDGE: As above, we're trying to
180 ;; enable tail recursion optimization and
181 ;; any other effects of this declaration
182 ;; are accidental. -- WHN 2002-07-08
183 (declare (optimize (speed 2) (debug 1)))
184 (%funcall ,more ,@temps ,n-context ,n-count)))))))
186 (%arg-count-error ,n-supplied)))))))))
188 ;;; Make an external entry point (XEP) for FUN and return it. We
189 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
190 ;;; then associate this lambda with FUN as its XEP. After the
191 ;;; conversion, we iterate over the function's associated lambdas,
192 ;;; redoing local call analysis so that the XEP calls will get
195 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
196 ;;; discover an XEP after the initial local call analyze pass.
197 (defun make-xep (fun)
198 (declare (type functional fun))
199 (aver (null (functional-entry-fun fun)))
200 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
201 (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
202 :debug-name (debug-namify
204 (leaf-debug-name fun)))))
205 (setf (functional-kind res) :external
206 (leaf-ever-used res) t
207 (functional-entry-fun res) fun
208 (functional-entry-fun fun) res
209 (component-reanalyze *current-component*) t
210 (component-reoptimize *current-component*) t)
213 (locall-analyze-fun-1 fun))
215 (dolist (ep (optional-dispatch-entry-points fun))
216 (locall-analyze-fun-1 ep))
217 (when (optional-dispatch-more-entry fun)
218 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
221 ;;; Notice a REF that is not in a local-call context. If the REF is
222 ;;; already to an XEP, then do nothing, otherwise change it to the
223 ;;; XEP, making an XEP if necessary.
225 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
226 ;;; it as though it was not an XEP reference (i.e. leave it alone).
227 (defun reference-entry-point (ref)
228 (declare (type ref ref))
229 (let ((fun (ref-leaf ref)))
230 (unless (or (xep-p fun)
231 (member (functional-kind fun) '(:escape :cleanup)))
232 (change-ref-leaf ref (or (functional-entry-fun fun)
235 ;;; Attempt to convert all references to FUN to local calls. The
236 ;;; reference must be the function for a call, and the function
237 ;;; continuation must be used only once, since otherwise we cannot be
238 ;;; sure what function is to be called. The call continuation would be
239 ;;; multiply used if there is hairy stuff such as conditionals in the
240 ;;; expression that computes the function.
242 ;;; If we cannot convert a reference, then we mark the referenced
243 ;;; function as an entry-point, creating a new XEP if necessary. We
244 ;;; don't try to convert calls that are in error (:ERROR kind.)
246 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
247 ;;; can force analysis of newly introduced calls. Note that we don't
248 ;;; do LET conversion here.
249 (defun locall-analyze-fun-1 (fun)
250 (declare (type functional fun))
251 (let ((refs (leaf-refs fun))
254 (let* ((cont (node-cont ref))
255 (dest (continuation-dest cont)))
256 (cond ((and (basic-combination-p dest)
257 (eq (basic-combination-fun dest) cont)
258 (eq (continuation-use cont) ref))
260 (convert-call-if-possible ref dest)
262 (unless (eq (basic-combination-kind dest) :local)
263 (reference-entry-point ref)))
265 (reference-entry-point ref))))
266 (setq first-time nil)))
270 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
271 ;;; calls into local calls when it is legal. We also attempt to
272 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
273 ;;; deletion of a function reference, but functions that start out
274 ;;; eligible for conversion must be noticed sometime.
276 ;;; Note that there is a lot of action going on behind the scenes
277 ;;; here, triggered by reference deletion. In particular, the
278 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
279 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
280 ;;; the COMPONENT-LAMBDAS when it is. Also, the
281 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
282 ;;; it is not updated when we delete functions, etc. Only
283 ;;; COMPONENT-LAMBDAS is updated.
285 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
286 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
288 (defun locall-analyze-component (component)
289 (declare (type component component))
290 (aver-live-component component)
292 (let* ((new-functional (pop (component-new-functionals component)))
293 (functional (or new-functional
294 (pop (component-reanalyze-functionals component)))))
297 (let ((kind (functional-kind functional)))
298 (cond ((or (functional-somewhat-letlike-p functional)
300 (values)) ; nothing to do
301 ((and (null (leaf-refs functional)) (eq kind nil)
302 (not (functional-entry-fun functional)))
303 (delete-functional functional))
305 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
306 (cond ((not (lambda-p functional))
307 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
310 (new-functional ; FUNCTIONAL came from
311 ; NEW-FUNCTIONALS, hence is new.
312 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
313 (aver (not (member functional
314 (component-lambdas component))))
315 (push functional (component-lambdas component)))
316 (t ; FUNCTIONAL is old.
317 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
318 (aver (member functional (component-lambdas
320 (locall-analyze-fun-1 functional)
321 (when (lambda-p functional)
322 (maybe-let-convert functional)))))))
325 (defun locall-analyze-clambdas-until-done (clambdas)
327 (let ((did-something nil))
328 (dolist (clambda clambdas)
329 (let* ((component (lambda-component clambda))
330 (*all-components* (list component)))
331 ;; The original CMU CL code seemed to implicitly assume that
332 ;; COMPONENT is the only one here. Let's make that explicit.
333 (aver (= 1 (length (functional-components clambda))))
334 (aver (eql component (first (functional-components clambda))))
335 (when (component-new-functionals component)
336 (setf did-something t)
337 (locall-analyze-component component))))
338 (unless did-something
342 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
343 ;;; to be in an infinite recursive loop, then change the reference to
344 ;;; reference a fresh copy. We return whichever function we decide to
346 (defun maybe-expand-local-inline (original-functional ref call)
347 (if (and (policy call
348 (and (>= speed space)
349 (>= speed compilation-speed)))
350 (not (eq (functional-kind (node-home-lambda call)) :external))
351 (inline-expansion-ok call))
352 (multiple-value-bind (losing-local-functional converted-lambda)
353 (catch 'locall-already-let-converted
354 (with-ir1-environment-from-node call
355 (let ((*lexenv* (functional-lexenv original-functional)))
358 (functional-inline-expansion original-functional)
359 :debug-name (debug-namify
362 original-functional)))))))
363 (cond (losing-local-functional
364 (let ((*compiler-error-context* call))
365 (compiler-note "couldn't inline expand because expansion ~
366 calls this LET-converted local function:~
368 (leaf-debug-name losing-local-functional)))
371 (change-ref-leaf ref converted-lambda)
373 original-functional))
375 ;;; Dispatch to the appropriate function to attempt to convert a call.
376 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
377 ;;; optimization as well as in local call analysis. If the call is is
378 ;;; already :LOCAL, we do nothing. If the call is already scheduled
379 ;;; for deletion, also do nothing (in addition to saving time, this
380 ;;; also avoids some problems with optimizing collections of functions
381 ;;; that are partially deleted.)
383 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
384 ;;; called on a :INITIAL component, we don't care whether the caller
385 ;;; and callee are in the same component. Afterward, we must stick
386 ;;; with whatever component division we have chosen.
388 ;;; Before attempting to convert a call, we see whether the function
389 ;;; is supposed to be inline expanded. Call conversion proceeds as
390 ;;; before after any expansion.
392 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
393 ;;; warnings will get the right context.
394 (defun convert-call-if-possible (ref call)
395 (declare (type ref ref) (type basic-combination call))
396 (let* ((block (node-block call))
397 (component (block-component block))
398 (original-fun (ref-leaf ref)))
399 (aver (functional-p original-fun))
400 (unless (or (member (basic-combination-kind call) '(:local :error))
401 (block-delete-p block)
402 (eq (functional-kind (block-home-lambda block)) :deleted)
403 (member (functional-kind original-fun)
404 '(:toplevel-xep :deleted))
405 (not (or (eq (component-kind component) :initial)
408 (lambda-bind (main-entry original-fun))))
410 (let ((fun (if (xep-p original-fun)
411 (functional-entry-fun original-fun)
413 (*compiler-error-context* call))
415 (when (and (eq (functional-inlinep fun) :inline)
416 (rest (leaf-refs original-fun)))
417 (setq fun (maybe-expand-local-inline fun ref call)))
419 (aver (member (functional-kind fun)
420 '(nil :escape :cleanup :optional)))
421 (cond ((mv-combination-p call)
422 (convert-mv-call ref call fun))
424 (convert-lambda-call ref call fun))
426 (convert-hairy-call ref call fun))))))
430 ;;; Attempt to convert a multiple-value call. The only interesting
431 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
432 ;;; exactly one reference and no XEP, and is called with one values
435 ;;; We change the call to be to the last optional entry point and
436 ;;; change the call to be local. Due to our preconditions, the call
437 ;;; should eventually be converted to a let, but we can't do that now,
438 ;;; since there may be stray references to the e-p lambda due to
439 ;;; optional defaulting code.
441 ;;; We also use variable types for the called function to construct an
442 ;;; assertion for the values continuation.
444 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
445 (defun convert-mv-call (ref call fun)
446 (declare (type ref ref) (type mv-combination call) (type functional fun))
447 (when (and (looks-like-an-mv-bind fun)
448 (not (functional-entry-fun fun))
449 (= (length (leaf-refs fun)) 1)
450 (= (length (basic-combination-args call)) 1))
451 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
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-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
460 :rest *universal-type*)
461 (lexenv-policy (node-lexenv call)))))
464 ;;; Attempt to convert a call to a lambda. If the number of args is
465 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
466 ;;; from future consideration. If the argcount is O.K. then we just
468 (defun convert-lambda-call (ref call fun)
469 (declare (type ref ref) (type combination call) (type clambda fun))
470 (let ((nargs (length (lambda-vars fun)))
471 (call-args (length (combination-args call))))
472 (cond ((= call-args nargs)
473 (convert-call ref call fun))
475 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
476 ;; Compiler" that calling a function with "the wrong number of
477 ;; arguments" be only a STYLE-ERROR. I think, though, that this
478 ;; should only apply when the number of arguments is inferred
479 ;; from a previous definition. If the number of arguments
480 ;; is DECLAIMed, surely calling with the wrong number is a
481 ;; real WARNING. As long as SBCL continues to use CMU CL's
482 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
483 ;; but as long as we continue to use that policy, that's the
484 ;; not our biggest problem.:-| When we fix that policy, this
485 ;; should come back into compliance. (So fix that policy!)
487 ;; FIXME, continued: Except that section "3.2.2.3 Semantic
488 ;; Constraints" says that if it's within the same file, it's
489 ;; wrong. And we're in locall.lisp here, so it's probably
490 ;; (haven't checked this..) a call to something in the same
491 ;; file. So maybe it deserves a full warning anyway.
493 "function called with ~R argument~:P, but wants exactly ~R"
495 (setf (basic-combination-kind call) :error)))))
497 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
499 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
500 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
501 ;;; a call to the correct entry point. If &KEY args are supplied, then
502 ;;; dispatch to a subfunction. We don't convert calls to functions
503 ;;; that have a &MORE (or &REST) arg.
504 (defun convert-hairy-call (ref call fun)
505 (declare (type ref ref) (type combination call)
506 (type optional-dispatch fun))
507 (let ((min-args (optional-dispatch-min-args fun))
508 (max-args (optional-dispatch-max-args fun))
509 (call-args (length (combination-args call))))
510 (cond ((< call-args min-args)
511 ;; FIXME: See FIXME note at the previous
512 ;; wrong-number-of-arguments warnings in this file.
514 "function called with ~R argument~:P, but wants at least ~R"
516 (setf (basic-combination-kind call) :error))
517 ((<= call-args max-args)
518 (convert-call ref call
519 (elt (optional-dispatch-entry-points fun)
520 (- 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-note "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-note "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
850 ;;; If the actual continuation is only used by the LET call, then we
851 ;;; intersect the type assertion on the dummy continuation with the
852 ;;; assertion for the actual continuation; in all other cases
853 ;;; assertions on the dummy continuation are lost.
855 ;;; We also intersect the derived type of the CALL with the derived
856 ;;; type of all the dummy continuation's uses. This serves mainly to
857 ;;; propagate TRULY-THE through LETs.
858 (defun move-return-uses (fun call next-block)
859 (declare (type clambda fun) (type basic-combination call)
860 (type cblock next-block))
861 (let* ((return (lambda-return fun))
862 (return-block (node-block return)))
863 (unlink-blocks return-block
864 (component-tail (block-component return-block)))
865 (link-blocks return-block next-block)
867 (delete-return return)
868 (let ((result (return-result return))
869 (cont (node-cont call))
870 (call-type (node-derived-type call)))
871 (when (eq (continuation-use cont) call)
872 (set-continuation-type-assertion
874 (continuation-asserted-type result)
875 (continuation-type-to-check result)))
876 (unless (eq call-type *wild-type*)
877 (do-uses (use result)
878 (derive-node-type use call-type)))
879 (substitute-continuation-uses cont result)))
882 ;;; Change all CONT for all the calls to FUN to be the start
883 ;;; continuation for the bind node. This allows the blocks to be
884 ;;; joined if the caller count ever goes to one.
885 (defun move-let-call-cont (fun)
886 (declare (type clambda fun))
887 (let ((new-cont (node-prev (lambda-bind fun))))
888 (dolist (ref (leaf-refs fun))
889 (let ((dest (continuation-dest (node-cont ref))))
890 (delete-continuation-use dest)
891 (add-continuation-use dest new-cont))))
894 ;;; We are converting FUN to be a LET when the call is in a non-tail
895 ;;; position. Any previously tail calls in FUN are no longer tail
896 ;;; calls, and must be restored to normal calls which transfer to
897 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
898 ;;; the RETURN-RESULT, because the return might have been deleted (if
899 ;;; all calls were TR.)
900 (defun unconvert-tail-calls (fun call next-block)
901 (dolist (called (lambda-calls-or-closes fun))
902 (when (lambda-p called)
903 (dolist (ref (leaf-refs called))
904 (let ((this-call (continuation-dest (node-cont ref))))
906 (node-tail-p this-call)
907 (eq (node-home-lambda this-call) fun))
908 (setf (node-tail-p this-call) nil)
909 (ecase (functional-kind called)
910 ((nil :cleanup :optional)
911 (let ((block (node-block this-call))
912 (cont (node-cont call)))
913 (ensure-block-start cont)
914 (unlink-blocks block (first (block-succ block)))
915 (link-blocks block next-block)
916 (delete-continuation-use this-call)
917 (add-continuation-use this-call cont)))
919 ;; The called function might be an assignment in the
920 ;; case where we are currently converting that function.
921 ;; In steady-state, assignments never appear as a called
924 (aver (eq called fun)))))))))
927 ;;; Deal with returning from a LET or assignment that we are
928 ;;; converting. FUN is the function we are calling, CALL is a call to
929 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
930 ;;; NULL if call is a tail call.
932 ;;; If the call is not a tail call, then we must do
933 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
934 ;;; its value out of the enclosing non-let function. When call is
935 ;;; non-TR, we must convert it back to an ordinary local call, since
936 ;;; the value must be delivered to the receiver of CALL's value.
938 ;;; We do different things depending on whether the caller and callee
939 ;;; have returns left:
941 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
942 ;;; Either the function doesn't return, or all returns are via
943 ;;; tail-recursive local calls.
944 ;;; -- If CALL is a non-tail call, or if both have returns, then
945 ;;; we delete the callee's return, move its uses to the call's
946 ;;; result continuation, and transfer control to the appropriate
948 ;;; -- If the callee has a return, but the caller doesn't, then we
949 ;;; move the return to the caller.
950 (defun move-return-stuff (fun call next-block)
951 (declare (type clambda fun) (type basic-combination call)
952 (type (or cblock null) next-block))
954 (unconvert-tail-calls fun call next-block))
955 (let* ((return (lambda-return fun))
956 (call-fun (node-home-lambda call))
957 (call-return (lambda-return call-fun)))
959 ((or next-block call-return)
960 (unless (block-delete-p (node-block return))
961 (move-return-uses fun call
962 (or next-block (node-block call-return)))))
964 (aver (node-tail-p call))
965 (setf (lambda-return call-fun) return)
966 (setf (return-lambda return) call-fun))))
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 (> DEBUG SPEED) we can guess that inlining generally
1005 ;; won't be appreciated, but if the user specifically requests
1006 ;; inlining, that takes precedence over our general guess.
1007 (and (policy clambda (> debug speed))
1008 (not (eq (lambda-inlinep clambda) :inline))))))
1010 ;;; We also don't convert calls to named functions which appear in the
1011 ;;; initial component, delaying this until optimization. This
1012 ;;; minimizes the likelihood that we will LET-convert a function which
1013 ;;; may have references added due to later local inline expansion.
1014 (defun ok-initial-convert-p (fun)
1015 (not (and (leaf-has-source-name-p fun)
1016 (or (declarations-suppress-let-conversion-p fun)
1017 (eq (component-kind (lambda-component fun))
1020 ;;; This function is called when there is some reason to believe that
1021 ;;; CLAMBDA might be converted into a LET. This is done after local
1022 ;;; call analysis, and also when a reference is deleted. We return
1023 ;;; true if we converted.
1024 (defun maybe-let-convert (clambda)
1025 (declare (type clambda clambda))
1026 (unless (declarations-suppress-let-conversion-p clambda)
1027 ;; We only convert to a LET when the function is a normal local
1028 ;; function, has no XEP, and is referenced in exactly one local
1029 ;; call. Conversion is also inhibited if the only reference is in
1030 ;; a block about to be deleted.
1032 ;; These rules limiting LET conversion may seem unnecessarily
1033 ;; restrictive, since there are some cases where we could do the
1034 ;; return with a jump that don't satisfy these requirements. The
1035 ;; reason for doing things this way is that it makes the concept
1036 ;; of a LET much more useful at the level of IR1 semantics. The
1037 ;; :ASSIGNMENT function kind provides another way to optimize
1038 ;; calls to single-return/multiple call functions.
1040 ;; We don't attempt to convert calls to functions that have an
1041 ;; XEP, since we might be embarrassed later when we want to
1042 ;; convert a newly discovered local call. Also, see
1043 ;; OK-INITIAL-CONVERT-P.
1044 (let ((refs (leaf-refs clambda)))
1047 (member (functional-kind clambda) '(nil :assignment))
1048 (not (functional-entry-fun clambda)))
1049 (let* ((ref (first refs))
1050 (ref-cont (node-cont ref))
1051 (dest (continuation-dest ref-cont)))
1053 (basic-combination-p dest)
1054 (eq (basic-combination-fun dest) ref-cont)
1055 (eq (basic-combination-kind dest) :local)
1056 (not (block-delete-p (node-block dest)))
1057 (cond ((ok-initial-convert-p clambda) t)
1059 (reoptimize-continuation ref-cont)
1061 (when (eq clambda (node-home-lambda dest))
1062 (delete-lambda clambda)
1063 (return-from maybe-let-convert nil))
1064 (unless (eq (functional-kind clambda) :assignment)
1065 (let-convert clambda dest))
1066 (reoptimize-call dest)
1067 (setf (functional-kind clambda)
1068 (if (mv-combination-p dest) :mv-let :let))))
1071 ;;;; tail local calls and assignments
1073 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1074 ;;; they definitely won't generate any cleanup code. Currently we
1075 ;;; recognize lexical entry points that are only used locally (if at
1077 (defun only-harmless-cleanups (block1 block2)
1078 (declare (type cblock block1 block2))
1079 (or (eq block1 block2)
1080 (let ((cleanup2 (block-start-cleanup block2)))
1081 (do ((cleanup (block-end-cleanup block1)
1082 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1083 ((eq cleanup cleanup2) t)
1084 (case (cleanup-kind cleanup)
1086 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1088 (t (return nil)))))))
1090 ;;; If a potentially TR local call really is TR, then convert it to
1091 ;;; jump directly to the called function. We also call
1092 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1093 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
1094 ;;; the succesor (potentially deleting the RETURN node) unless:
1095 ;;; -- The call has already been converted.
1096 ;;; -- The call isn't TR (some implicit MV PROG1.)
1097 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
1098 ;;; so that we can use known return inside the component.)
1099 ;;; -- There is a change in the cleanup between the call in the return,
1100 ;;; so we might need to introduce cleanup code.
1101 (defun maybe-convert-tail-local-call (call)
1102 (declare (type combination call))
1103 (let ((return (continuation-dest (node-cont call))))
1104 (aver (return-p return))
1105 (when (and (not (node-tail-p call))
1106 (immediately-used-p (return-result return) call)
1107 (not (eq (functional-kind (node-home-lambda call))
1109 (only-harmless-cleanups (node-block call)
1110 (node-block return)))
1111 (node-ends-block call)
1112 (let ((block (node-block call))
1113 (fun (combination-lambda call)))
1114 (setf (node-tail-p call) t)
1115 (unlink-blocks block (first (block-succ block)))
1116 (link-blocks block (lambda-block fun))
1117 (values t (maybe-convert-to-assignment fun))))))
1119 ;;; This is called when we believe it might make sense to convert
1120 ;;; CLAMBDA to an assignment. All this function really does is
1121 ;;; determine when a function with more than one call can still be
1122 ;;; combined with the calling function's environment. We can convert
1124 ;;; -- The function is a normal, non-entry function, and
1125 ;;; -- Except for one call, all calls must be tail recursive calls
1126 ;;; in the called function (i.e. are self-recursive tail calls)
1127 ;;; -- OK-INITIAL-CONVERT-P is true.
1129 ;;; There may be one outside call, and it need not be tail-recursive.
1130 ;;; Since all tail local calls have already been converted to direct
1131 ;;; transfers, the only control semantics needed are to splice in the
1132 ;;; body at the non-tail call. If there is no non-tail call, then we
1133 ;;; need only merge the environments. Both cases are handled by
1136 ;;; ### It would actually be possible to allow any number of outside
1137 ;;; calls as long as they all return to the same place (i.e. have the
1138 ;;; same conceptual continuation.) A special case of this would be
1139 ;;; when all of the outside calls are tail recursive.
1140 (defun maybe-convert-to-assignment (clambda)
1141 (declare (type clambda clambda))
1142 (when (and (not (functional-kind clambda))
1143 (not (functional-entry-fun clambda)))
1144 (let ((outside-non-tail-call nil)
1146 (when (and (dolist (ref (leaf-refs clambda) t)
1147 (let ((dest (continuation-dest (node-cont ref))))
1148 (when (or (not dest)
1149 (block-delete-p (node-block dest)))
1151 (let ((home (node-home-lambda ref)))
1152 (unless (eq home clambda)
1155 (setq outside-call dest))
1156 (unless (node-tail-p dest)
1157 (when (or outside-non-tail-call (eq home clambda))
1159 (setq outside-non-tail-call dest)))))
1160 (ok-initial-convert-p clambda))
1161 (cond (outside-call (setf (functional-kind clambda) :assignment)
1162 (let-convert clambda outside-call)
1163 (when outside-non-tail-call
1164 (reoptimize-call outside-non-tail-call))
1166 (t (delete-lambda clambda)