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 ;; Force convertion of all entries
148 (optional-dispatch-entry-point-fun fun 0)
149 (loop for ep in (optional-dispatch-entry-points fun)
151 do (entries `((= ,n-supplied ,n)
152 (%funcall ,(force ep) ,@(subseq temps 0 n)))))
153 `(lambda (,n-supplied ,@temps)
154 ;; FIXME: Make sure that INDEX type distinguishes between
155 ;; target and host. (Probably just make the SB!XC:DEFTYPE
156 ;; different from CL:DEFTYPE.)
157 (declare (type index ,n-supplied))
159 ,@(if more (butlast (entries)) (entries))
161 `((,(if (zerop min) t `(>= ,n-supplied ,max))
162 ,(let ((n-context (gensym))
164 `(multiple-value-bind (,n-context ,n-count)
165 (%more-arg-context ,n-supplied ,max)
167 (declare (optimize (merge-tail-calls 3)))
168 (%funcall ,more ,@temps ,n-context ,n-count)))))))
170 (%arg-count-error ,n-supplied)))))))))
172 ;;; Make an external entry point (XEP) for FUN and return it. We
173 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
174 ;;; then associate this lambda with FUN as its XEP. After the
175 ;;; conversion, we iterate over the function's associated lambdas,
176 ;;; redoing local call analysis so that the XEP calls will get
179 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
180 ;;; discover an XEP after the initial local call analyze pass.
181 (defun make-xep (fun)
182 (declare (type functional fun))
183 (aver (null (functional-entry-fun fun)))
184 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
185 (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
186 :debug-name (debug-namify
188 (leaf-debug-name fun)))))
189 (setf (functional-kind res) :external
190 (leaf-ever-used res) t
191 (functional-entry-fun res) fun
192 (functional-entry-fun fun) res
193 (component-reanalyze *current-component*) t
194 (component-reoptimize *current-component*) t)
197 (locall-analyze-fun-1 fun))
199 (dolist (ep (optional-dispatch-entry-points fun))
200 (locall-analyze-fun-1 (force ep)))
201 (when (optional-dispatch-more-entry fun)
202 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
205 ;;; Notice a REF that is not in a local-call context. If the REF is
206 ;;; already to an XEP, then do nothing, otherwise change it to the
207 ;;; XEP, making an XEP if necessary.
209 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
210 ;;; it as though it was not an XEP reference (i.e. leave it alone).
211 (defun reference-entry-point (ref)
212 (declare (type ref ref))
213 (let ((fun (ref-leaf ref)))
214 (unless (or (xep-p fun)
215 (member (functional-kind fun) '(:escape :cleanup)))
216 (change-ref-leaf ref (or (functional-entry-fun fun)
219 ;;; Attempt to convert all references to FUN to local calls. The
220 ;;; reference must be the function for a call, and the function
221 ;;; continuation must be used only once, since otherwise we cannot be
222 ;;; sure what function is to be called. The call continuation would be
223 ;;; multiply used if there is hairy stuff such as conditionals in the
224 ;;; expression that computes the function.
226 ;;; If we cannot convert a reference, then we mark the referenced
227 ;;; function as an entry-point, creating a new XEP if necessary. We
228 ;;; don't try to convert calls that are in error (:ERROR kind.)
230 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
231 ;;; can force analysis of newly introduced calls. Note that we don't
232 ;;; do LET conversion here.
233 (defun locall-analyze-fun-1 (fun)
234 (declare (type functional fun))
235 (let ((refs (leaf-refs fun))
238 (let* ((cont (node-cont ref))
239 (dest (continuation-dest cont)))
240 (cond ((and (basic-combination-p dest)
241 (eq (basic-combination-fun dest) cont)
242 (eq (continuation-use cont) ref))
244 (convert-call-if-possible ref dest)
246 (unless (eq (basic-combination-kind dest) :local)
247 (reference-entry-point ref)))
249 (reference-entry-point ref))))
250 (setq first-time nil)))
254 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
255 ;;; calls into local calls when it is legal. We also attempt to
256 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
257 ;;; deletion of a function reference, but functions that start out
258 ;;; eligible for conversion must be noticed sometime.
260 ;;; Note that there is a lot of action going on behind the scenes
261 ;;; here, triggered by reference deletion. In particular, the
262 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
263 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
264 ;;; the COMPONENT-LAMBDAS when it is. Also, the
265 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
266 ;;; it is not updated when we delete functions, etc. Only
267 ;;; COMPONENT-LAMBDAS is updated.
269 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
270 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
272 (defun locall-analyze-component (component)
273 (declare (type component component))
274 (aver-live-component component)
276 (let* ((new-functional (pop (component-new-functionals component)))
277 (functional (or new-functional
278 (pop (component-reanalyze-functionals component)))))
281 (let ((kind (functional-kind functional)))
282 (cond ((or (functional-somewhat-letlike-p functional)
284 (values)) ; nothing to do
285 ((and (null (leaf-refs functional)) (eq kind nil)
286 (not (functional-entry-fun functional)))
287 (delete-functional functional))
289 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
290 (cond ((not (lambda-p functional))
291 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
294 (new-functional ; FUNCTIONAL came from
295 ; NEW-FUNCTIONALS, hence is new.
296 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
297 (aver (not (member functional
298 (component-lambdas component))))
299 (push functional (component-lambdas component)))
300 (t ; FUNCTIONAL is old.
301 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
302 (aver (member functional (component-lambdas
304 (locall-analyze-fun-1 functional)
305 (when (lambda-p functional)
306 (maybe-let-convert functional)))))))
309 (defun locall-analyze-clambdas-until-done (clambdas)
311 (let ((did-something nil))
312 (dolist (clambda clambdas)
313 (let* ((component (lambda-component clambda))
314 (*all-components* (list component)))
315 ;; The original CMU CL code seemed to implicitly assume that
316 ;; COMPONENT is the only one here. Let's make that explicit.
317 (aver (= 1 (length (functional-components clambda))))
318 (aver (eql component (first (functional-components clambda))))
319 (when (or (component-new-functionals component)
320 (component-reanalyze-functionals component))
321 (setf did-something t)
322 (locall-analyze-component component))))
323 (unless did-something
327 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
328 ;;; to be in an infinite recursive loop, then change the reference to
329 ;;; reference a fresh copy. We return whichever function we decide to
331 (defun maybe-expand-local-inline (original-functional ref call)
332 (if (and (policy call
333 (and (>= speed space)
334 (>= speed compilation-speed)))
335 (not (eq (functional-kind (node-home-lambda call)) :external))
336 (inline-expansion-ok call))
337 (let* ((end (component-last-block (node-component call)))
338 (pred (block-prev end)))
339 (multiple-value-bind (losing-local-functional converted-lambda)
340 (catch 'locall-already-let-converted
341 (with-ir1-environment-from-node call
342 (let ((*lexenv* (functional-lexenv original-functional)))
345 (functional-inline-expansion original-functional)
346 :debug-name (debug-namify
349 original-functional)))))))
350 (cond (losing-local-functional
351 (let ((*compiler-error-context* call))
352 (compiler-notify "couldn't inline expand because expansion ~
353 calls this LET-converted local function:~
355 (leaf-debug-name losing-local-functional)))
356 (loop for block = (block-next pred) then (block-next block)
358 do (setf (block-delete-p block) t))
359 (loop for block = (block-next pred) then (block-next block)
361 do (delete-block block t))
364 (change-ref-leaf ref converted-lambda)
366 original-functional))
368 ;;; Dispatch to the appropriate function to attempt to convert a call.
369 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
370 ;;; optimization as well as in local call analysis. If the call is is
371 ;;; already :LOCAL, we do nothing. If the call is already scheduled
372 ;;; for deletion, also do nothing (in addition to saving time, this
373 ;;; also avoids some problems with optimizing collections of functions
374 ;;; that are partially deleted.)
376 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
377 ;;; called on a :INITIAL component, we don't care whether the caller
378 ;;; and callee are in the same component. Afterward, we must stick
379 ;;; with whatever component division we have chosen.
381 ;;; Before attempting to convert a call, we see whether the function
382 ;;; is supposed to be inline expanded. Call conversion proceeds as
383 ;;; before after any expansion.
385 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
386 ;;; warnings will get the right context.
387 (defun convert-call-if-possible (ref call)
388 (declare (type ref ref) (type basic-combination call))
389 (let* ((block (node-block call))
390 (component (block-component block))
391 (original-fun (ref-leaf ref)))
392 (aver (functional-p original-fun))
393 (unless (or (member (basic-combination-kind call) '(:local :error))
394 (block-delete-p block)
395 (eq (functional-kind (block-home-lambda block)) :deleted)
396 (member (functional-kind original-fun)
397 '(:toplevel-xep :deleted))
398 (not (or (eq (component-kind component) :initial)
401 (lambda-bind (main-entry original-fun))))
403 (let ((fun (if (xep-p original-fun)
404 (functional-entry-fun original-fun)
406 (*compiler-error-context* call))
408 (when (and (eq (functional-inlinep fun) :inline)
409 (rest (leaf-refs original-fun)))
410 (setq fun (maybe-expand-local-inline fun ref call)))
412 (aver (member (functional-kind fun)
413 '(nil :escape :cleanup :optional)))
414 (cond ((mv-combination-p call)
415 (convert-mv-call ref call fun))
417 (convert-lambda-call ref call fun))
419 (convert-hairy-call ref call fun))))))
423 ;;; Attempt to convert a multiple-value call. The only interesting
424 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
425 ;;; exactly one reference and no XEP, and is called with one values
428 ;;; We change the call to be to the last optional entry point and
429 ;;; change the call to be local. Due to our preconditions, the call
430 ;;; should eventually be converted to a let, but we can't do that now,
431 ;;; since there may be stray references to the e-p lambda due to
432 ;;; optional defaulting code.
434 ;;; We also use variable types for the called function to construct an
435 ;;; assertion for the values continuation.
437 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
438 (defun convert-mv-call (ref call fun)
439 (declare (type ref ref) (type mv-combination call) (type functional fun))
440 (when (and (looks-like-an-mv-bind fun)
441 (not (functional-entry-fun fun))
442 (= (length (leaf-refs fun)) 1)
443 (= (length (basic-combination-args call)) 1))
444 (let* ((*current-component* (node-component ref))
445 (ep (optional-dispatch-entry-point-fun
446 fun (optional-dispatch-max-args fun))))
447 (aver (= (optional-dispatch-min-args fun) 0))
448 (setf (basic-combination-kind call) :local)
449 (pushnew ep (lambda-calls-or-closes (node-home-lambda call)))
450 (merge-tail-sets call ep)
451 (change-ref-leaf ref ep)
453 (assert-continuation-type
454 (first (basic-combination-args call))
455 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
456 (lexenv-policy (node-lexenv call)))))
459 ;;; Attempt to convert a call to a lambda. If the number of args is
460 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
461 ;;; from future consideration. If the argcount is O.K. then we just
463 (defun convert-lambda-call (ref call fun)
464 (declare (type ref ref) (type combination call) (type clambda fun))
465 (let ((nargs (length (lambda-vars fun)))
466 (call-args (length (combination-args call))))
467 (cond ((= call-args nargs)
468 (convert-call ref call fun))
470 ;; FIXME: ANSI requires in "3.2.5 Exceptional Situations in the
471 ;; Compiler" that calling a function with "the wrong number of
472 ;; arguments" be only a STYLE-ERROR. I think, though, that this
473 ;; should only apply when the number of arguments is inferred
474 ;; from a previous definition. If the number of arguments
475 ;; is DECLAIMed, surely calling with the wrong number is a
476 ;; real WARNING. As long as SBCL continues to use CMU CL's
477 ;; non-ANSI DEFUN-is-a-DECLAIM policy, we're in violation here,
478 ;; but as long as we continue to use that policy, that's the
479 ;; not our biggest problem.:-| When we fix that policy, this
480 ;; should come back into compliance. (So fix that policy!)
482 ;; FIXME, continued: Except that section "3.2.2.3 Semantic
483 ;; Constraints" says that if it's within the same file, it's
484 ;; wrong. And we're in locall.lisp here, so it's probably
485 ;; (haven't checked this..) a call to something in the same
486 ;; file. So maybe it deserves a full warning anyway.
488 "function called with ~R argument~:P, but wants exactly ~R"
490 (setf (basic-combination-kind call) :error)))))
492 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
494 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
495 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
496 ;;; a call to the correct entry point. If &KEY args are supplied, then
497 ;;; dispatch to a subfunction. We don't convert calls to functions
498 ;;; that have a &MORE (or &REST) arg.
499 (defun convert-hairy-call (ref call fun)
500 (declare (type ref ref) (type combination call)
501 (type optional-dispatch fun))
502 (let ((min-args (optional-dispatch-min-args fun))
503 (max-args (optional-dispatch-max-args fun))
504 (call-args (length (combination-args call))))
505 (cond ((< call-args min-args)
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 least ~R"
511 (setf (basic-combination-kind call) :error))
512 ((<= call-args max-args)
513 (convert-call ref call
514 (let ((*current-component* (node-component ref)))
515 (optional-dispatch-entry-point-fun
516 fun (- call-args min-args)))))
517 ((optional-dispatch-more-entry fun)
518 (convert-more-call ref call fun))
520 ;; FIXME: See FIXME note at the previous
521 ;; wrong-number-of-arguments warnings in this file.
523 "function called with ~R argument~:P, but wants at most ~R"
525 (setf (basic-combination-kind call) :error))))
528 ;;; This function is used to convert a call to an entry point when
529 ;;; complex transformations need to be done on the original arguments.
530 ;;; ENTRY is the entry point function that we are calling. VARS is a
531 ;;; list of variable names which are bound to the original call
532 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
533 ;;; is the list of arguments to the entry point function.
535 ;;; In order to avoid gruesome graph grovelling, we introduce a new
536 ;;; function that rearranges the arguments and calls the entry point.
537 ;;; We analyze the new function and the entry point immediately so
538 ;;; that everything gets converted during the single pass.
539 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
540 (declare (list vars ignores args) (type ref ref) (type combination call)
541 (type clambda entry))
543 (with-ir1-environment-from-node call
546 (declare (ignorable ,@ignores))
547 (%funcall ,entry ,@args))
548 :debug-name (debug-namify "hairy function entry ~S"
549 (continuation-fun-name
550 (basic-combination-fun call)))))))
551 (convert-call ref call new-fun)
552 (dolist (ref (leaf-refs entry))
553 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
555 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
556 ;;; function into a local call to the MAIN-ENTRY.
558 ;;; First we verify that all keywords are constant and legal. If there
559 ;;; aren't, then we warn the user and don't attempt to convert the call.
561 ;;; We massage the supplied &KEY arguments into the order expected
562 ;;; by the main entry. This is done by binding all the arguments to
563 ;;; the keyword call to variables in the introduced lambda, then
564 ;;; passing these values variables in the correct order when calling
565 ;;; the main entry. Unused arguments (such as the keywords themselves)
566 ;;; are discarded simply by not passing them along.
568 ;;; If there is a &REST arg, then we bundle up the args and pass them
570 (defun convert-more-call (ref call fun)
571 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
572 (let* ((max (optional-dispatch-max-args fun))
573 (arglist (optional-dispatch-arglist fun))
574 (args (combination-args call))
575 (more (nthcdr max args))
576 (flame (policy call (or (> speed inhibit-warnings)
577 (> space inhibit-warnings))))
581 (temps (make-gensym-list max))
582 (more-temps (make-gensym-list (length more))))
587 (dolist (var arglist)
588 (let ((info (lambda-var-arg-info var)))
590 (ecase (arg-info-kind info)
594 ((:more-context :more-count)
595 (compiler-warn "can't local-call functions with &MORE args")
596 (setf (basic-combination-kind call) :error)
597 (return-from convert-more-call))))))
599 (when (optional-dispatch-keyp fun)
600 (when (oddp (length more))
601 (compiler-warn "function called with odd number of ~
602 arguments in keyword portion")
604 (setf (basic-combination-kind call) :error)
605 (return-from convert-more-call))
607 (do ((key more (cddr key))
608 (temp more-temps (cddr temp)))
610 (let ((cont (first key)))
611 (unless (constant-continuation-p cont)
613 (compiler-notify "non-constant keyword in keyword call"))
614 (setf (basic-combination-kind call) :error)
615 (return-from convert-more-call))
617 (let ((name (continuation-value cont))
620 ;; FIXME: check whether KEY was supplied earlier
621 (when (and (eq name :allow-other-keys) (not allow-found))
622 (let ((val (second key)))
623 (cond ((constant-continuation-p val)
625 allowp (continuation-value val)))
627 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
628 (setf (basic-combination-kind call) :error)
629 (return-from convert-more-call)))))
630 (dolist (var (key-vars)
633 (unless (eq name :allow-other-keys)
635 (let ((info (lambda-var-arg-info var)))
636 (when (eq (arg-info-key info) name)
638 (supplied (cons var val))
641 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
642 (compiler-warn "function called with unknown argument keyword ~S"
644 (setf (basic-combination-kind call) :error)
645 (return-from convert-more-call)))
647 (collect ((call-args))
648 (do ((var arglist (cdr var))
649 (temp temps (cdr temp)))
651 (let ((info (lambda-var-arg-info (car var))))
653 (ecase (arg-info-kind info)
655 (call-args (car temp))
656 (when (arg-info-supplied-p info)
659 (call-args `(list ,@more-temps))
663 (call-args (car temp)))))
665 (dolist (var (key-vars))
666 (let ((info (lambda-var-arg-info var))
667 (temp (cdr (assoc var (supplied)))))
670 (call-args (arg-info-default info)))
671 (when (arg-info-supplied-p info)
672 (call-args (not (null temp))))))
674 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
675 (append temps more-temps)
676 (ignores) (call-args)))))
682 ;;;; Converting to a LET has differing significance to various parts
683 ;;;; of the compiler:
684 ;;;; -- The body of a LET is spliced in immediately after the
685 ;;;; corresponding combination node, making the control transfer
686 ;;;; explicit and allowing LETs to be mashed together into a single
687 ;;;; block. The value of the LET is delivered directly to the
688 ;;;; original continuation for the call, eliminating the need to
689 ;;;; propagate information from the dummy result continuation.
690 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
691 ;;;; that there is only one expression that the variable can be bound
692 ;;;; to, and this is easily substituted for.
693 ;;;; -- LETs are interesting to environment analysis and to the back
694 ;;;; end because in most ways a LET can be considered to be "the
695 ;;;; same function" as its home function.
696 ;;;; -- LET conversion has dynamic scope implications, since control
697 ;;;; transfers within the same environment are local. In a local
698 ;;;; control transfer, cleanup code must be emitted to remove
699 ;;;; dynamic bindings that are no longer in effect.
701 ;;; Set up the control transfer to the called CLAMBDA. We split the
702 ;;; call block immediately after the call, and link the head of
703 ;;; CLAMBDA to the call block. The successor block after splitting
704 ;;; (where we return to) is returned.
706 ;;; If the lambda is is a different component than the call, then we
707 ;;; call JOIN-COMPONENTS. This only happens in block compilation
708 ;;; before FIND-INITIAL-DFO.
709 (defun insert-let-body (clambda call)
710 (declare (type clambda clambda) (type basic-combination call))
711 (let* ((call-block (node-block call))
712 (bind-block (node-block (lambda-bind clambda)))
713 (component (block-component call-block)))
714 (aver-live-component component)
715 (let ((clambda-component (block-component bind-block)))
716 (unless (eq clambda-component component)
717 (aver (eq (component-kind component) :initial))
718 (join-components component clambda-component)))
719 (let ((*current-component* component))
720 (node-ends-block call))
721 ;; FIXME: Use DESTRUCTURING-BIND here, and grep for other
722 ;; uses of '=.*length' which could also be converted to use
723 ;; DESTRUCTURING-BIND or PROPER-LIST-OF-LENGTH-P.
724 (aver (= (length (block-succ call-block)) 1))
725 (let ((next-block (first (block-succ call-block))))
726 (unlink-blocks call-block next-block)
727 (link-blocks call-block bind-block)
730 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
731 ;;; same set as; but leave CLAMBDA with a valid tail set value of
732 ;;; its own, for the benefit of code which might try to pull
733 ;;; something out of it (e.g. return type).
734 (defun depart-from-tail-set (clambda)
735 ;; Until sbcl-0.pre7.37.flaky5.2, we did
736 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
737 ;; (SETF (TAIL-SET-FUNS TAILS)
738 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
739 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
740 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
741 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
742 ;; inconsistent, and perhaps unsafe, for us to think we're in the
743 ;; tail set. Unfortunately..
745 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
746 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
747 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
748 ;; which you called in order to get the address of an external LAMBDA):
749 ;; the external function was defined in terms of internal function,
750 ;; which was LET-converted, and then things blew up downstream when
751 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
752 ;; the now-NILed-out TAIL-SET. So..
754 ;; To deal with this problem, we no longer NIL out
755 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
756 ;; * If we're the only function in TAIL-SET-FUNS, it should
757 ;; be safe to leave ourself linked to it, and it to you.
758 ;; * If there are other functions in TAIL-SET-FUNS, then we're
759 ;; afraid of future optimizations on those functions causing
760 ;; the TAIL-SET object no longer to be valid to describe our
761 ;; return value. Thus, we delete ourselves from that object;
762 ;; but we save a newly-allocated tail-set, derived from the old
763 ;; one, for ourselves, for the use of later code (e.g.
764 ;; FINALIZE-XEP-DEFINITION) which might want to
765 ;; know about our return type.
766 (let* ((old-tail-set (lambda-tail-set clambda))
767 (old-tail-set-funs (tail-set-funs old-tail-set)))
768 (unless (= 1 (length old-tail-set-funs))
769 (setf (tail-set-funs old-tail-set)
770 (delete clambda old-tail-set-funs))
771 (let ((new-tail-set (copy-tail-set old-tail-set)))
772 (setf (lambda-tail-set clambda) new-tail-set
773 (tail-set-funs new-tail-set) (list clambda)))))
774 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
775 ;; remain valid in this case, so we nuke it on the theory that
776 ;; missing information tends to be less dangerous than incorrect
778 (setf (tail-set-info (lambda-tail-set clambda)) nil))
780 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
781 ;;; its LETs to LETs for the CALL's home function. We merge the calls
782 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
783 ;;; in the process. We also merge the ENTRIES.
785 ;;; We also unlink the function head from the component head and set
786 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
788 (defun merge-lets (clambda call)
790 (declare (type clambda clambda) (type basic-combination call))
792 (let ((component (node-component call)))
793 (unlink-blocks (component-head component) (lambda-block clambda))
794 (setf (component-lambdas component)
795 (delete clambda (component-lambdas component)))
796 (setf (component-reanalyze component) t))
797 (setf (lambda-call-lexenv clambda) (node-lexenv call))
799 (depart-from-tail-set clambda)
801 (let* ((home (node-home-lambda call))
802 (home-physenv (lambda-physenv home)))
804 (aver (not (eq home clambda)))
806 ;; CLAMBDA belongs to HOME now.
807 (push clambda (lambda-lets home))
808 (setf (lambda-home clambda) home)
809 (setf (lambda-physenv clambda) home-physenv)
811 ;; All of CLAMBDA's LETs belong to HOME now.
812 (let ((lets (lambda-lets clambda)))
814 (setf (lambda-home let) home)
815 (setf (lambda-physenv let) home-physenv))
816 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
817 ;; CLAMBDA no longer has an independent existence as an entity
819 (setf (lambda-lets clambda) nil)
821 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
823 (setf (lambda-calls-or-closes home)
825 (nunion (lambda-calls-or-closes clambda)
826 (lambda-calls-or-closes home))))
827 ;; CLAMBDA no longer has an independent existence as an entity
828 ;; which calls things or has DFO dependencies.
829 (setf (lambda-calls-or-closes clambda) nil)
831 ;; All of CLAMBDA's ENTRIES belong to HOME now.
832 (setf (lambda-entries home)
833 (nconc (lambda-entries clambda)
834 (lambda-entries home)))
835 ;; CLAMBDA no longer has an independent existence as an entity
837 (setf (lambda-entries clambda) nil))
841 ;;; Handle the value semantics of LET conversion. Delete FUN's return
842 ;;; node, and change the control flow to transfer to NEXT-BLOCK
843 ;;; instead. Move all the uses of the result continuation to CALL's
845 (defun move-return-uses (fun call next-block)
846 (declare (type clambda fun) (type basic-combination call)
847 (type cblock next-block))
848 (let* ((return (lambda-return fun))
849 (return-block (node-block return)))
850 (unlink-blocks return-block
851 (component-tail (block-component return-block)))
852 (link-blocks return-block next-block)
854 (delete-return return)
855 (let ((result (return-result return))
856 (cont (node-cont call))
857 (call-type (node-derived-type call)))
858 (unless (eq call-type *wild-type*)
859 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2002-01-26
860 (do-uses (use result)
861 (derive-node-type use call-type)))
862 (substitute-continuation-uses cont result)))
865 ;;; Change all CONT for all the calls to FUN to be the start
866 ;;; continuation for the bind node. This allows the blocks to be
867 ;;; joined if the caller count ever goes to one.
868 (defun move-let-call-cont (fun)
869 (declare (type clambda fun))
870 (let ((new-cont (node-prev (lambda-bind fun))))
871 (dolist (ref (leaf-refs fun))
872 (let ((dest (continuation-dest (node-cont ref))))
873 (delete-continuation-use dest)
874 (add-continuation-use dest new-cont))))
877 ;;; We are converting FUN to be a LET when the call is in a non-tail
878 ;;; position. Any previously tail calls in FUN are no longer tail
879 ;;; calls, and must be restored to normal calls which transfer to
880 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
881 ;;; the RETURN-RESULT, because the return might have been deleted (if
882 ;;; all calls were TR.)
883 (defun unconvert-tail-calls (fun call next-block)
884 (dolist (called (lambda-calls-or-closes fun))
885 (when (lambda-p called)
886 (dolist (ref (leaf-refs called))
887 (let ((this-call (continuation-dest (node-cont ref))))
889 (node-tail-p this-call)
890 (eq (node-home-lambda this-call) fun))
891 (setf (node-tail-p this-call) nil)
892 (ecase (functional-kind called)
893 ((nil :cleanup :optional)
894 (let ((block (node-block this-call))
895 (cont (node-cont call)))
896 (ensure-block-start cont)
897 (unlink-blocks block (first (block-succ block)))
898 (link-blocks block next-block)
899 (delete-continuation-use this-call)
900 (add-continuation-use this-call cont)))
902 ;; The called function might be an assignment in the
903 ;; case where we are currently converting that function.
904 ;; In steady-state, assignments never appear as a called
907 (aver (eq called fun)))))))))
910 ;;; Deal with returning from a LET or assignment that we are
911 ;;; converting. FUN is the function we are calling, CALL is a call to
912 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
913 ;;; NULL if call is a tail call.
915 ;;; If the call is not a tail call, then we must do
916 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
917 ;;; its value out of the enclosing non-let function. When call is
918 ;;; non-TR, we must convert it back to an ordinary local call, since
919 ;;; the value must be delivered to the receiver of CALL's value.
921 ;;; We do different things depending on whether the caller and callee
922 ;;; have returns left:
924 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
925 ;;; Either the function doesn't return, or all returns are via
926 ;;; tail-recursive local calls.
927 ;;; -- If CALL is a non-tail call, or if both have returns, then
928 ;;; we delete the callee's return, move its uses to the call's
929 ;;; result continuation, and transfer control to the appropriate
931 ;;; -- If the callee has a return, but the caller doesn't, then we
932 ;;; move the return to the caller.
933 (defun move-return-stuff (fun call next-block)
934 (declare (type clambda fun) (type basic-combination call)
935 (type (or cblock null) next-block))
937 (unconvert-tail-calls fun call next-block))
938 (let* ((return (lambda-return fun))
939 (call-fun (node-home-lambda call))
940 (call-return (lambda-return call-fun)))
942 ((or next-block call-return)
943 (unless (block-delete-p (node-block return))
944 (when (and (node-tail-p call)
946 (not (eq (node-cont call)
947 (return-result call-return))))
948 ;; We do not care to give a meaningful continuation to
949 ;; a tail combination, but here we need it.
950 (delete-continuation-use call)
951 (add-continuation-use call (return-result call-return)))
952 (move-return-uses fun call
954 (let ((block (node-block call-return)))
955 (when (block-delete-p block)
956 (setf (block-delete-p block) nil))
959 (aver (node-tail-p call))
960 (setf (lambda-return call-fun) return)
961 (setf (return-lambda return) call-fun)
962 (setf (lambda-return fun) nil))))
963 (move-let-call-cont fun)
966 ;;; Actually do LET conversion. We call subfunctions to do most of the
967 ;;; work. We change the CALL's CONT to be the continuation heading the
968 ;;; BIND block, and also do REOPTIMIZE-CONTINUATION on the args and
969 ;;; CONT so that LET-specific IR1 optimizations get a chance. We blow
970 ;;; away any entry for the function in *FREE-FUNS* so that nobody
971 ;;; will create new references to it.
972 (defun let-convert (fun call)
973 (declare (type clambda fun) (type basic-combination call))
974 (let ((next-block (if (node-tail-p call)
976 (insert-let-body fun call))))
977 (move-return-stuff fun call next-block)
978 (merge-lets fun call)))
980 ;;; Reoptimize all of CALL's args and its result.
981 (defun reoptimize-call (call)
982 (declare (type basic-combination call))
983 (dolist (arg (basic-combination-args call))
985 (reoptimize-continuation arg)))
986 (reoptimize-continuation (node-cont call))
989 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
991 (defun declarations-suppress-let-conversion-p (clambda)
992 ;; From the user's point of view, LET-converting something that
993 ;; has a name is inlining it. (The user can't see what we're doing
994 ;; with anonymous things, and suppressing inlining
995 ;; for such things can easily give Python acute indigestion, so
997 (when (leaf-has-source-name-p clambda)
998 ;; ANSI requires that explicit NOTINLINE be respected.
999 (or (eq (lambda-inlinep clambda) :notinline)
1000 ;; If (= LET-CONVERTION 0) we can guess that inlining
1001 ;; generally won't be appreciated, but if the user
1002 ;; specifically requests inlining, that takes precedence over
1003 ;; our general guess.
1004 (and (policy clambda (= let-convertion 0))
1005 (not (eq (lambda-inlinep clambda) :inline))))))
1007 ;;; We also don't convert calls to named functions which appear in the
1008 ;;; initial component, delaying this until optimization. This
1009 ;;; minimizes the likelihood that we will LET-convert a function which
1010 ;;; may have references added due to later local inline expansion.
1011 (defun ok-initial-convert-p (fun)
1012 (not (and (leaf-has-source-name-p fun)
1013 (or (declarations-suppress-let-conversion-p fun)
1014 (eq (component-kind (lambda-component fun))
1017 ;;; This function is called when there is some reason to believe that
1018 ;;; CLAMBDA might be converted into a LET. This is done after local
1019 ;;; call analysis, and also when a reference is deleted. We return
1020 ;;; true if we converted.
1021 (defun maybe-let-convert (clambda)
1022 (declare (type clambda clambda))
1023 (unless (declarations-suppress-let-conversion-p clambda)
1024 ;; We only convert to a LET when the function is a normal local
1025 ;; function, has no XEP, and is referenced in exactly one local
1026 ;; call. Conversion is also inhibited if the only reference is in
1027 ;; a block about to be deleted.
1029 ;; These rules limiting LET conversion may seem unnecessarily
1030 ;; restrictive, since there are some cases where we could do the
1031 ;; return with a jump that don't satisfy these requirements. The
1032 ;; reason for doing things this way is that it makes the concept
1033 ;; of a LET much more useful at the level of IR1 semantics. The
1034 ;; :ASSIGNMENT function kind provides another way to optimize
1035 ;; calls to single-return/multiple call functions.
1037 ;; We don't attempt to convert calls to functions that have an
1038 ;; XEP, since we might be embarrassed later when we want to
1039 ;; convert a newly discovered local call. Also, see
1040 ;; OK-INITIAL-CONVERT-P.
1041 (let ((refs (leaf-refs clambda)))
1044 (member (functional-kind clambda) '(nil :assignment))
1045 (not (functional-entry-fun clambda)))
1046 (let* ((ref (first refs))
1047 (ref-cont (node-cont ref))
1048 (dest (continuation-dest ref-cont)))
1050 (basic-combination-p dest)
1051 (eq (basic-combination-fun dest) ref-cont)
1052 (eq (basic-combination-kind dest) :local)
1053 (not (block-delete-p (node-block dest)))
1054 (cond ((ok-initial-convert-p clambda) t)
1056 (reoptimize-continuation ref-cont)
1058 (when (eq clambda (node-home-lambda dest))
1059 (delete-lambda clambda)
1060 (return-from maybe-let-convert nil))
1061 (unless (eq (functional-kind clambda) :assignment)
1062 (let-convert clambda dest))
1063 (reoptimize-call dest)
1064 (setf (functional-kind clambda)
1065 (if (mv-combination-p dest) :mv-let :let))))
1068 ;;;; tail local calls and assignments
1070 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1071 ;;; they definitely won't generate any cleanup code. Currently we
1072 ;;; recognize lexical entry points that are only used locally (if at
1074 (defun only-harmless-cleanups (block1 block2)
1075 (declare (type cblock block1 block2))
1076 (or (eq block1 block2)
1077 (let ((cleanup2 (block-start-cleanup block2)))
1078 (do ((cleanup (block-end-cleanup block1)
1079 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1080 ((eq cleanup cleanup2) t)
1081 (case (cleanup-kind cleanup)
1083 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1085 (t (return nil)))))))
1087 ;;; If a potentially TR local call really is TR, then convert it to
1088 ;;; jump directly to the called function. We also call
1089 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1090 ;;; tail-convert. The second is the value of M-C-T-A. We can switch
1091 ;;; the succesor (potentially deleting the RETURN node) unless:
1092 ;;; -- The call has already been converted.
1093 ;;; -- The call isn't TR (some implicit MV PROG1.)
1094 ;;; -- The call is in an XEP (thus we might decide to make it non-tail
1095 ;;; so that we can use known return inside the component.)
1096 ;;; -- There is a change in the cleanup between the call in the return,
1097 ;;; so we might need to introduce cleanup code.
1098 (defun maybe-convert-tail-local-call (call)
1099 (declare (type combination call))
1100 (let ((return (continuation-dest (node-cont call))))
1101 (aver (return-p return))
1102 (when (and (not (node-tail-p call))
1103 (immediately-used-p (return-result return) call)
1104 (not (eq (functional-kind (node-home-lambda call))
1106 (only-harmless-cleanups (node-block call)
1107 (node-block return)))
1108 (node-ends-block call)
1109 (let ((block (node-block call))
1110 (fun (combination-lambda call)))
1111 (setf (node-tail-p call) t)
1112 (unlink-blocks block (first (block-succ block)))
1113 (link-blocks block (lambda-block fun))
1114 (values t (maybe-convert-to-assignment fun))))))
1116 ;;; This is called when we believe it might make sense to convert
1117 ;;; CLAMBDA to an assignment. All this function really does is
1118 ;;; determine when a function with more than one call can still be
1119 ;;; combined with the calling function's environment. We can convert
1121 ;;; -- The function is a normal, non-entry function, and
1122 ;;; -- Except for one call, all calls must be tail recursive calls
1123 ;;; in the called function (i.e. are self-recursive tail calls)
1124 ;;; -- OK-INITIAL-CONVERT-P is true.
1126 ;;; There may be one outside call, and it need not be tail-recursive.
1127 ;;; Since all tail local calls have already been converted to direct
1128 ;;; transfers, the only control semantics needed are to splice in the
1129 ;;; body at the non-tail call. If there is no non-tail call, then we
1130 ;;; need only merge the environments. Both cases are handled by
1133 ;;; ### It would actually be possible to allow any number of outside
1134 ;;; calls as long as they all return to the same place (i.e. have the
1135 ;;; same conceptual continuation.) A special case of this would be
1136 ;;; when all of the outside calls are tail recursive.
1137 (defun maybe-convert-to-assignment (clambda)
1138 (declare (type clambda clambda))
1139 (when (and (not (functional-kind clambda))
1140 (not (functional-entry-fun clambda)))
1141 (let ((outside-non-tail-call nil)
1143 (when (and (dolist (ref (leaf-refs clambda) t)
1144 (let ((dest (continuation-dest (node-cont ref))))
1145 (when (or (not dest)
1146 (block-delete-p (node-block dest)))
1148 (let ((home (node-home-lambda ref)))
1149 (unless (eq home clambda)
1152 (setq outside-call dest))
1153 (unless (node-tail-p dest)
1154 (when (or outside-non-tail-call (eq home clambda))
1156 (setq outside-non-tail-call dest)))))
1157 (ok-initial-convert-p clambda))
1158 (cond (outside-call (setf (functional-kind clambda) :assignment)
1159 (let-convert clambda outside-call)
1160 (when outside-non-tail-call
1161 (reoptimize-call outside-non-tail-call))
1163 (t (delete-lambda clambda)