1.0.9.54: clean up old pv updating code
[sbcl/lichteblau.git] / src / compiler / locall.lisp
blob2c413654ba3949ad8f52b3320adc34b2df3b25fb
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.
8 ;;;;
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.
15 ;;;;
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.
22 (in-package "SB!C")
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
27 ;;; LETs.
28 ;;;
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.
32 ;;;
33 ;;; We also apply the declared variable type assertion to the argument
34 ;;; lvars.
35 (defun propagate-to-args (call fun)
36 (declare (type combination call) (type clambda fun))
37 (loop with policy = (lexenv-policy (node-lexenv call))
38 for args on (basic-combination-args call)
39 and var in (lambda-vars fun)
40 do (assert-lvar-type (car args) (leaf-type var) policy)
41 do (unless (leaf-refs var)
42 (flush-dest (car args))
43 (setf (car args) nil)))
44 (values))
46 (defun recognize-dynamic-extent-lvars (call fun)
47 (declare (type combination call) (type clambda fun))
48 (loop for arg in (basic-combination-args call)
49 and var in (lambda-vars fun)
50 when (and arg
51 (lambda-var-dynamic-extent var)
52 (not (lvar-dynamic-extent arg)))
53 collect arg into dx-lvars
54 and do (let ((use (lvar-uses arg)))
55 ;; Stack analysis wants DX value generators to end
56 ;; their blocks. Uses of mupltiple used LVARs already
57 ;; end their blocks, so we just need to process
58 ;; used-once LVARs.
59 (when (node-p use)
60 (node-ends-block use)))
61 finally (when dx-lvars
62 (binding* ((before-ctran (node-prev call))
63 (nil (ensure-block-start before-ctran))
64 (block (ctran-block before-ctran))
65 (new-call-ctran (make-ctran :kind :inside-block
66 :next call
67 :block block))
68 (entry (with-ir1-environment-from-node call
69 (make-entry :prev before-ctran
70 :next new-call-ctran)))
71 (cleanup (make-cleanup :kind :dynamic-extent
72 :mess-up entry
73 :info dx-lvars)))
74 (setf (node-prev call) new-call-ctran)
75 (setf (ctran-next before-ctran) entry)
76 (setf (ctran-use new-call-ctran) entry)
77 (setf (entry-cleanup entry) cleanup)
78 (setf (node-lexenv call)
79 (make-lexenv :default (node-lexenv call)
80 :cleanup cleanup))
81 (push entry (lambda-entries (node-home-lambda entry)))
82 (dolist (lvar dx-lvars)
83 (setf (lvar-dynamic-extent lvar) cleanup)))))
84 (values))
86 ;;; This function handles merging the tail sets if CALL is potentially
87 ;;; tail-recursive, and is a call to a function with a different
88 ;;; TAIL-SET than CALL's FUN. This must be called whenever we alter
89 ;;; IR1 so as to place a local call in what might be a tail-recursive
90 ;;; context. Note that any call which returns its value to a RETURN is
91 ;;; considered potentially tail-recursive, since any implicit MV-PROG1
92 ;;; might be optimized away.
93 ;;;
94 ;;; We destructively modify the set for the calling function to
95 ;;; represent both, and then change all the functions in callee's set
96 ;;; to reference the first. If we do merge, we reoptimize the
97 ;;; RETURN-RESULT lvar to cause IR1-OPTIMIZE-RETURN to recompute the
98 ;;; tail set type.
99 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
100 (declare (type basic-combination call) (type clambda new-fun))
101 (let ((return (node-dest call)))
102 (when (return-p return)
103 (let ((call-set (lambda-tail-set (node-home-lambda call)))
104 (fun-set (lambda-tail-set new-fun)))
105 (unless (eq call-set fun-set)
106 (let ((funs (tail-set-funs fun-set)))
107 (dolist (fun funs)
108 (setf (lambda-tail-set fun) call-set))
109 (setf (tail-set-funs call-set)
110 (nconc (tail-set-funs call-set) funs)))
111 (reoptimize-lvar (return-result return))
112 t)))))
114 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
115 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
116 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
117 ;;; the function in the REF node with the new function.
119 ;;; We change the REF last, since changing the reference can trigger
120 ;;; LET conversion of the new function, but will only do so if the
121 ;;; call is local. Note that the replacement may trigger LET
122 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
123 ;;; with NEW-FUN before the substitution, since after the substitution
124 ;;; (and LET conversion), the call may no longer be recognizable as
125 ;;; tail-recursive.
126 (defun convert-call (ref call fun)
127 (declare (type ref ref) (type combination call) (type clambda fun))
128 (propagate-to-args call fun)
129 (setf (basic-combination-kind call) :local)
130 (unless (call-full-like-p call)
131 (dolist (arg (basic-combination-args call))
132 (when arg
133 (flush-lvar-externally-checkable-type arg))))
134 (sset-adjoin fun (lambda-calls-or-closes (node-home-lambda call)))
135 (recognize-dynamic-extent-lvars call fun)
136 (merge-tail-sets call fun)
137 (change-ref-leaf ref fun)
138 (values))
140 ;;;; external entry point creation
142 ;;; Return a LAMBDA form that can be used as the definition of the XEP
143 ;;; for FUN.
145 ;;; If FUN is a LAMBDA, then we check the number of arguments
146 ;;; (conditional on policy) and call FUN with all the arguments.
148 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
149 ;;; of supplied arguments by doing do an = test for each entry-point,
150 ;;; calling the entry with the appropriate prefix of the passed
151 ;;; arguments.
153 ;;; If there is a &MORE arg, then there are a couple of optimizations
154 ;;; that we make (more for space than anything else):
155 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
156 ;;; no argument count error is possible.
157 ;;; -- We can omit the = clause for the last entry-point, allowing the
158 ;;; case of 0 more args to fall through to the more entry.
160 ;;; We don't bother to policy conditionalize wrong arg errors in
161 ;;; optional dispatches, since the additional overhead is negligible
162 ;;; compared to the cost of everything else going on.
164 ;;; Note that if policy indicates it, argument type declarations in
165 ;;; FUN will be verified. Since nothing is known about the type of the
166 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
167 ;;; are passed to the actual function.
168 (defun make-xep-lambda-expression (fun)
169 (declare (type functional fun))
170 (etypecase fun
171 (clambda
172 (let ((nargs (length (lambda-vars fun)))
173 (n-supplied (gensym))
174 (temps (make-gensym-list (length (lambda-vars fun)))))
175 `(lambda (,n-supplied ,@temps)
176 (declare (type index ,n-supplied))
177 ,(if (policy *lexenv* (zerop verify-arg-count))
178 `(declare (ignore ,n-supplied))
179 `(%verify-arg-count ,n-supplied ,nargs))
180 (locally
181 (declare (optimize (merge-tail-calls 3)))
182 (%funcall ,fun ,@temps)))))
183 (optional-dispatch
184 (let* ((min (optional-dispatch-min-args fun))
185 (max (optional-dispatch-max-args fun))
186 (more (optional-dispatch-more-entry fun))
187 (n-supplied (gensym))
188 (temps (make-gensym-list max)))
189 (collect ((entries))
190 ;; Force convertion of all entries
191 (optional-dispatch-entry-point-fun fun 0)
192 (loop for ep in (optional-dispatch-entry-points fun)
193 and n from min
194 do (entries `((eql ,n-supplied ,n)
195 (%funcall ,(force ep) ,@(subseq temps 0 n)))))
196 `(lambda (,n-supplied ,@temps)
197 ;; FIXME: Make sure that INDEX type distinguishes between
198 ;; target and host. (Probably just make the SB!XC:DEFTYPE
199 ;; different from CL:DEFTYPE.)
200 (declare (type index ,n-supplied))
201 (cond
202 ,@(if more (butlast (entries)) (entries))
203 ,@(when more
204 ;; KLUDGE: (NOT (< ...)) instead of >= avoids one round of
205 ;; deftransforms and lambda-conversion.
206 `((,(if (zerop min) t `(not (< ,n-supplied ,max)))
207 ,(let ((n-context (gensym))
208 (n-count (gensym)))
209 `(multiple-value-bind (,n-context ,n-count)
210 (%more-arg-context ,n-supplied ,max)
211 (locally
212 (declare (optimize (merge-tail-calls 3)))
213 (%funcall ,more ,@temps ,n-context ,n-count)))))))
215 (%arg-count-error ,n-supplied)))))))))
217 ;;; Make an external entry point (XEP) for FUN and return it. We
218 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
219 ;;; then associate this lambda with FUN as its XEP. After the
220 ;;; conversion, we iterate over the function's associated lambdas,
221 ;;; redoing local call analysis so that the XEP calls will get
222 ;;; converted.
224 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
225 ;;; discover an XEP after the initial local call analyze pass.
226 (defun make-xep (fun)
227 (declare (type functional fun))
228 (aver (null (functional-entry-fun fun)))
229 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
230 (let ((res (ir1-convert-lambda (make-xep-lambda-expression fun)
231 :debug-name (debug-name
232 'xep (leaf-debug-name fun)))))
233 (setf (functional-kind res) :external
234 (leaf-ever-used res) t
235 (functional-entry-fun res) fun
236 (functional-entry-fun fun) res
237 (component-reanalyze *current-component*) t)
238 (reoptimize-component *current-component* :maybe)
239 (etypecase fun
240 (clambda
241 (locall-analyze-fun-1 fun))
242 (optional-dispatch
243 (dolist (ep (optional-dispatch-entry-points fun))
244 (locall-analyze-fun-1 (force ep)))
245 (when (optional-dispatch-more-entry fun)
246 (locall-analyze-fun-1 (optional-dispatch-more-entry fun)))))
247 res)))
249 ;;; Notice a REF that is not in a local-call context. If the REF is
250 ;;; already to an XEP, then do nothing, otherwise change it to the
251 ;;; XEP, making an XEP if necessary.
253 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
254 ;;; it as though it was not an XEP reference (i.e. leave it alone).
255 (defun reference-entry-point (ref)
256 (declare (type ref ref))
257 (let ((fun (ref-leaf ref)))
258 (unless (or (xep-p fun)
259 (member (functional-kind fun) '(:escape :cleanup)))
260 (change-ref-leaf ref (or (functional-entry-fun fun)
261 (make-xep fun))))))
263 ;;; Attempt to convert all references to FUN to local calls. The
264 ;;; reference must be the function for a call, and the function lvar
265 ;;; must be used only once, since otherwise we cannot be sure what
266 ;;; function is to be called. The call lvar would be multiply used if
267 ;;; there is hairy stuff such as conditionals in the expression that
268 ;;; computes the function.
270 ;;; If we cannot convert a reference, then we mark the referenced
271 ;;; function as an entry-point, creating a new XEP if necessary. We
272 ;;; don't try to convert calls that are in error (:ERROR kind.)
274 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
275 ;;; can force analysis of newly introduced calls. Note that we don't
276 ;;; do LET conversion here.
277 (defun locall-analyze-fun-1 (fun)
278 (declare (type functional fun))
279 (let ((refs (leaf-refs fun))
280 (local-p t))
281 (dolist (ref refs)
282 (let* ((lvar (node-lvar ref))
283 (dest (when lvar (lvar-dest lvar))))
284 (unless (node-to-be-deleted-p ref)
285 (cond ((and (basic-combination-p dest)
286 (eq (basic-combination-fun dest) lvar)
287 (eq (lvar-uses lvar) ref))
289 (convert-call-if-possible ref dest)
291 (unless (eq (basic-combination-kind dest) :local)
292 (reference-entry-point ref)
293 (setq local-p nil)))
295 (reference-entry-point ref)
296 (setq local-p nil))))))
297 (when local-p (note-local-functional fun)))
299 (values))
301 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
302 ;;; calls into local calls when it is legal. We also attempt to
303 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
304 ;;; deletion of a function reference, but functions that start out
305 ;;; eligible for conversion must be noticed sometime.
307 ;;; Note that there is a lot of action going on behind the scenes
308 ;;; here, triggered by reference deletion. In particular, the
309 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
310 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
311 ;;; the COMPONENT-LAMBDAS when it is. Also, the
312 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
313 ;;; it is not updated when we delete functions, etc. Only
314 ;;; COMPONENT-LAMBDAS is updated.
316 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
317 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
318 ;;; LAMBDAS.
319 (defun locall-analyze-component (component)
320 (declare (type component component))
321 (aver-live-component component)
322 (loop
323 (let* ((new-functional (pop (component-new-functionals component)))
324 (functional (or new-functional
325 (pop (component-reanalyze-functionals component)))))
326 (unless functional
327 (return))
328 (let ((kind (functional-kind functional)))
329 (cond ((or (functional-somewhat-letlike-p functional)
330 (memq kind '(:deleted :zombie)))
331 (values)) ; nothing to do
332 ((and (null (leaf-refs functional)) (eq kind nil)
333 (not (functional-entry-fun functional)))
334 (delete-functional functional))
336 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
337 (cond ((not (lambda-p functional))
338 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
339 ;; apply: no-op.
340 (values))
341 (new-functional ; FUNCTIONAL came from
342 ; NEW-FUNCTIONALS, hence is new.
343 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
344 (aver (not (member functional
345 (component-lambdas component))))
346 (push functional (component-lambdas component)))
347 (t ; FUNCTIONAL is old.
348 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
349 (aver (member functional (component-lambdas
350 component)))))
351 (locall-analyze-fun-1 functional)
352 (when (lambda-p functional)
353 (maybe-let-convert functional)))))))
354 (values))
356 (defun locall-analyze-clambdas-until-done (clambdas)
357 (loop
358 (let ((did-something nil))
359 (dolist (clambda clambdas)
360 (let ((component (lambda-component clambda)))
361 ;; The original CMU CL code seemed to implicitly assume that
362 ;; COMPONENT is the only one here. Let's make that explicit.
363 (aver (= 1 (length (functional-components clambda))))
364 (aver (eql component (first (functional-components clambda))))
365 (when (or (component-new-functionals component)
366 (component-reanalyze-functionals component))
367 (setf did-something t)
368 (locall-analyze-component component))))
369 (unless did-something
370 (return))))
371 (values))
373 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
374 ;;; to be in an infinite recursive loop, then change the reference to
375 ;;; reference a fresh copy. We return whichever function we decide to
376 ;;; reference.
377 (defun maybe-expand-local-inline (original-functional ref call)
378 (if (and (policy call
379 (and (>= speed space)
380 (>= speed compilation-speed)))
381 (not (eq (functional-kind (node-home-lambda call)) :external))
382 (inline-expansion-ok call))
383 (let* ((end (component-last-block (node-component call)))
384 (pred (block-prev end)))
385 (multiple-value-bind (losing-local-object converted-lambda)
386 (catch 'locall-already-let-converted
387 (with-ir1-environment-from-node call
388 (let ((*lexenv* (functional-lexenv original-functional)))
389 (values nil
390 (ir1-convert-lambda
391 (functional-inline-expansion original-functional)
392 :debug-name (debug-name 'local-inline
393 (leaf-debug-name
394 original-functional)))))))
395 (cond (losing-local-object
396 (if (functional-p losing-local-object)
397 (let ((*compiler-error-context* call))
398 (compiler-notify "couldn't inline expand because expansion ~
399 calls this LET-converted local function:~
400 ~% ~S"
401 (leaf-debug-name losing-local-object)))
402 (let ((*compiler-error-context* call))
403 (compiler-notify "implementation limitation: couldn't inline ~
404 expand because expansion refers to ~
405 the optimized away object ~S."
406 losing-local-object)))
407 (loop for block = (block-next pred) then (block-next block)
408 until (eq block end)
409 do (setf (block-delete-p block) t))
410 (loop for block = (block-next pred) then (block-next block)
411 until (eq block end)
412 do (delete-block block t))
413 original-functional)
415 (change-ref-leaf ref converted-lambda)
416 converted-lambda))))
417 original-functional))
419 ;;; Dispatch to the appropriate function to attempt to convert a call.
420 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
421 ;;; optimization as well as in local call analysis. If the call is is
422 ;;; already :LOCAL, we do nothing. If the call is already scheduled
423 ;;; for deletion, also do nothing (in addition to saving time, this
424 ;;; also avoids some problems with optimizing collections of functions
425 ;;; that are partially deleted.)
427 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
428 ;;; called on a :INITIAL component, we don't care whether the caller
429 ;;; and callee are in the same component. Afterward, we must stick
430 ;;; with whatever component division we have chosen.
432 ;;; Before attempting to convert a call, we see whether the function
433 ;;; is supposed to be inline expanded. Call conversion proceeds as
434 ;;; before after any expansion.
436 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
437 ;;; warnings will get the right context.
438 (defun convert-call-if-possible (ref call)
439 (declare (type ref ref) (type basic-combination call))
440 (let* ((block (node-block call))
441 (component (block-component block))
442 (original-fun (ref-leaf ref)))
443 (aver (functional-p original-fun))
444 (unless (or (member (basic-combination-kind call) '(:local :error))
445 (node-to-be-deleted-p call)
446 (member (functional-kind original-fun)
447 '(:toplevel-xep :deleted))
448 (not (or (eq (component-kind component) :initial)
449 (eq (block-component
450 (node-block
451 (lambda-bind (main-entry original-fun))))
452 component))))
453 (let ((fun (if (xep-p original-fun)
454 (functional-entry-fun original-fun)
455 original-fun))
456 (*compiler-error-context* call))
458 (when (and (eq (functional-inlinep fun) :inline)
459 (rest (leaf-refs original-fun)))
460 (setq fun (maybe-expand-local-inline fun ref call)))
462 (aver (member (functional-kind fun)
463 '(nil :escape :cleanup :optional)))
464 (cond ((mv-combination-p call)
465 (convert-mv-call ref call fun))
466 ((lambda-p fun)
467 (convert-lambda-call ref call fun))
469 (convert-hairy-call ref call fun))))))
471 (values))
473 ;;; Attempt to convert a multiple-value call. The only interesting
474 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
475 ;;; exactly one reference and no XEP, and is called with one values
476 ;;; lvar.
478 ;;; We change the call to be to the last optional entry point and
479 ;;; change the call to be local. Due to our preconditions, the call
480 ;;; should eventually be converted to a let, but we can't do that now,
481 ;;; since there may be stray references to the e-p lambda due to
482 ;;; optional defaulting code.
484 ;;; We also use variable types for the called function to construct an
485 ;;; assertion for the values lvar.
487 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
488 (defun convert-mv-call (ref call fun)
489 (declare (type ref ref) (type mv-combination call) (type functional fun))
490 (when (and (looks-like-an-mv-bind fun)
491 (singleton-p (leaf-refs fun))
492 (singleton-p (basic-combination-args call)))
493 (let* ((*current-component* (node-component ref))
494 (ep (optional-dispatch-entry-point-fun
495 fun (optional-dispatch-max-args fun))))
496 (when (null (leaf-refs ep))
497 (aver (= (optional-dispatch-min-args fun) 0))
498 (aver (not (functional-entry-fun fun)))
499 (setf (basic-combination-kind call) :local)
500 (sset-adjoin ep (lambda-calls-or-closes (node-home-lambda call)))
501 (merge-tail-sets call ep)
502 (change-ref-leaf ref ep)
504 (assert-lvar-type
505 (first (basic-combination-args call))
506 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
507 (lexenv-policy (node-lexenv call))))))
508 (values))
510 ;;; Attempt to convert a call to a lambda. If the number of args is
511 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
512 ;;; from future consideration. If the argcount is O.K. then we just
513 ;;; convert it.
514 (defun convert-lambda-call (ref call fun)
515 (declare (type ref ref) (type combination call) (type clambda fun))
516 (let ((nargs (length (lambda-vars fun)))
517 (n-call-args (length (combination-args call))))
518 (cond ((= n-call-args nargs)
519 (convert-call ref call fun))
521 (warn
522 'local-argument-mismatch
523 :format-control
524 "function called with ~R argument~:P, but wants exactly ~R"
525 :format-arguments (list n-call-args nargs))
526 (setf (basic-combination-kind call) :error)))))
528 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
530 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
531 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
532 ;;; a call to the correct entry point. If &KEY args are supplied, then
533 ;;; dispatch to a subfunction. We don't convert calls to functions
534 ;;; that have a &MORE (or &REST) arg.
535 (defun convert-hairy-call (ref call fun)
536 (declare (type ref ref) (type combination call)
537 (type optional-dispatch fun))
538 (let ((min-args (optional-dispatch-min-args fun))
539 (max-args (optional-dispatch-max-args fun))
540 (call-args (length (combination-args call))))
541 (cond ((< call-args min-args)
542 (warn
543 'local-argument-mismatch
544 :format-control
545 "function called with ~R argument~:P, but wants at least ~R"
546 :format-arguments (list call-args min-args))
547 (setf (basic-combination-kind call) :error))
548 ((<= call-args max-args)
549 (convert-call ref call
550 (let ((*current-component* (node-component ref)))
551 (optional-dispatch-entry-point-fun
552 fun (- call-args min-args)))))
553 ((optional-dispatch-more-entry fun)
554 (convert-more-call ref call fun))
556 (warn
557 'local-argument-mismatch
558 :format-control
559 "function called with ~R argument~:P, but wants at most ~R"
560 :format-arguments
561 (list call-args max-args))
562 (setf (basic-combination-kind call) :error))))
563 (values))
565 ;;; This function is used to convert a call to an entry point when
566 ;;; complex transformations need to be done on the original arguments.
567 ;;; ENTRY is the entry point function that we are calling. VARS is a
568 ;;; list of variable names which are bound to the original call
569 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
570 ;;; is the list of arguments to the entry point function.
572 ;;; In order to avoid gruesome graph grovelling, we introduce a new
573 ;;; function that rearranges the arguments and calls the entry point.
574 ;;; We analyze the new function and the entry point immediately so
575 ;;; that everything gets converted during the single pass.
576 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
577 (declare (list vars ignores args) (type ref ref) (type combination call)
578 (type clambda entry))
579 (let ((new-fun
580 (with-ir1-environment-from-node call
581 (ir1-convert-lambda
582 `(lambda ,vars
583 (declare (ignorable ,@ignores))
584 (%funcall ,entry ,@args))
585 :debug-name (debug-name 'hairy-function-entry
586 (lvar-fun-name
587 (basic-combination-fun call)))))))
588 (convert-call ref call new-fun)
589 (dolist (ref (leaf-refs entry))
590 (convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
592 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
593 ;;; function into a local call to the MAIN-ENTRY.
595 ;;; First we verify that all keywords are constant and legal. If there
596 ;;; aren't, then we warn the user and don't attempt to convert the call.
598 ;;; We massage the supplied &KEY arguments into the order expected
599 ;;; by the main entry. This is done by binding all the arguments to
600 ;;; the keyword call to variables in the introduced lambda, then
601 ;;; passing these values variables in the correct order when calling
602 ;;; the main entry. Unused arguments (such as the keywords themselves)
603 ;;; are discarded simply by not passing them along.
605 ;;; If there is a &REST arg, then we bundle up the args and pass them
606 ;;; to LIST.
607 (defun convert-more-call (ref call fun)
608 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
609 (let* ((max (optional-dispatch-max-args fun))
610 (arglist (optional-dispatch-arglist fun))
611 (args (combination-args call))
612 (more (nthcdr max args))
613 (flame (policy call (or (> speed inhibit-warnings)
614 (> space inhibit-warnings))))
615 (loser nil)
616 (allowp nil)
617 (allow-found nil)
618 (temps (make-gensym-list max))
619 (more-temps (make-gensym-list (length more))))
620 (collect ((ignores)
621 (supplied)
622 (key-vars))
624 (dolist (var arglist)
625 (let ((info (lambda-var-arg-info var)))
626 (when info
627 (ecase (arg-info-kind info)
628 (:keyword
629 (key-vars var))
630 ((:rest :optional))
631 ((:more-context :more-count)
632 (compiler-warn "can't local-call functions with &MORE args")
633 (setf (basic-combination-kind call) :error)
634 (return-from convert-more-call))))))
636 (when (optional-dispatch-keyp fun)
637 (when (oddp (length more))
638 (compiler-warn "function called with odd number of ~
639 arguments in keyword portion")
640 (setf (basic-combination-kind call) :error)
641 (return-from convert-more-call))
643 (do ((key more (cddr key))
644 (temp more-temps (cddr temp)))
645 ((null key))
646 (let ((lvar (first key)))
647 (unless (constant-lvar-p lvar)
648 (when flame
649 (compiler-notify "non-constant keyword in keyword call"))
650 (setf (basic-combination-kind call) :error)
651 (return-from convert-more-call))
653 (let ((name (lvar-value lvar))
654 (dummy (first temp))
655 (val (second temp)))
656 (when (and (eq name :allow-other-keys) (not allow-found))
657 (let ((val (second key)))
658 (cond ((constant-lvar-p val)
659 (setq allow-found t
660 allowp (lvar-value val)))
661 (t (when flame
662 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
663 (setf (basic-combination-kind call) :error)
664 (return-from convert-more-call)))))
665 (dolist (var (key-vars)
666 (progn
667 (ignores dummy val)
668 (unless (eq name :allow-other-keys)
669 (setq loser (list name)))))
670 (let ((info (lambda-var-arg-info var)))
671 (when (eq (arg-info-key info) name)
672 (ignores dummy)
673 (if (member var (supplied) :key #'car)
674 (ignores val)
675 (supplied (cons var val)))
676 (return)))))))
678 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
679 (compiler-warn "function called with unknown argument keyword ~S"
680 (car loser))
681 (setf (basic-combination-kind call) :error)
682 (return-from convert-more-call)))
684 (collect ((call-args))
685 (do ((var arglist (cdr var))
686 (temp temps (cdr temp)))
687 ((null var))
688 (let ((info (lambda-var-arg-info (car var))))
689 (if info
690 (ecase (arg-info-kind info)
691 (:optional
692 (call-args (car temp))
693 (when (arg-info-supplied-p info)
694 (call-args t)))
695 (:rest
696 (call-args `(list ,@more-temps))
697 (return))
698 (:keyword
699 (return)))
700 (call-args (car temp)))))
702 (dolist (var (key-vars))
703 (let ((info (lambda-var-arg-info var))
704 (temp (cdr (assoc var (supplied)))))
705 (if temp
706 (call-args temp)
707 (call-args (arg-info-default info)))
708 (when (arg-info-supplied-p info)
709 (call-args (not (null temp))))))
711 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
712 (append temps more-temps)
713 (ignores) (call-args)))))
715 (values))
717 ;;;; LET conversion
718 ;;;;
719 ;;;; Converting to a LET has differing significance to various parts
720 ;;;; of the compiler:
721 ;;;; -- The body of a LET is spliced in immediately after the
722 ;;;; corresponding combination node, making the control transfer
723 ;;;; explicit and allowing LETs to be mashed together into a single
724 ;;;; block. The value of the LET is delivered directly to the
725 ;;;; original lvar for the call, eliminating the need to
726 ;;;; propagate information from the dummy result lvar.
727 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
728 ;;;; that there is only one expression that the variable can be bound
729 ;;;; to, and this is easily substituted for.
730 ;;;; -- LETs are interesting to environment analysis and to the back
731 ;;;; end because in most ways a LET can be considered to be "the
732 ;;;; same function" as its home function.
733 ;;;; -- LET conversion has dynamic scope implications, since control
734 ;;;; transfers within the same environment are local. In a local
735 ;;;; control transfer, cleanup code must be emitted to remove
736 ;;;; dynamic bindings that are no longer in effect.
738 ;;; Set up the control transfer to the called CLAMBDA. We split the
739 ;;; call block immediately after the call, and link the head of
740 ;;; CLAMBDA to the call block. The successor block after splitting
741 ;;; (where we return to) is returned.
743 ;;; If the lambda is is a different component than the call, then we
744 ;;; call JOIN-COMPONENTS. This only happens in block compilation
745 ;;; before FIND-INITIAL-DFO.
746 (defun insert-let-body (clambda call)
747 (declare (type clambda clambda) (type basic-combination call))
748 (let* ((call-block (node-block call))
749 (bind-block (node-block (lambda-bind clambda)))
750 (component (block-component call-block)))
751 (aver-live-component component)
752 (let ((clambda-component (block-component bind-block)))
753 (unless (eq clambda-component component)
754 (aver (eq (component-kind component) :initial))
755 (join-components component clambda-component)))
756 (let ((*current-component* component))
757 (node-ends-block call))
758 (destructuring-bind (next-block)
759 (block-succ call-block)
760 (unlink-blocks call-block next-block)
761 (link-blocks call-block bind-block)
762 next-block)))
764 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
765 ;;; same set as; but leave CLAMBDA with a valid tail set value of
766 ;;; its own, for the benefit of code which might try to pull
767 ;;; something out of it (e.g. return type).
768 (defun depart-from-tail-set (clambda)
769 ;; Until sbcl-0.pre7.37.flaky5.2, we did
770 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
771 ;; (SETF (TAIL-SET-FUNS TAILS)
772 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
773 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
774 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
775 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
776 ;; inconsistent, and perhaps unsafe, for us to think we're in the
777 ;; tail set. Unfortunately..
779 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
780 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
781 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
782 ;; which you called in order to get the address of an external LAMBDA):
783 ;; the external function was defined in terms of internal function,
784 ;; which was LET-converted, and then things blew up downstream when
785 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
786 ;; the now-NILed-out TAIL-SET. So..
788 ;; To deal with this problem, we no longer NIL out
789 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
790 ;; * If we're the only function in TAIL-SET-FUNS, it should
791 ;; be safe to leave ourself linked to it, and it to you.
792 ;; * If there are other functions in TAIL-SET-FUNS, then we're
793 ;; afraid of future optimizations on those functions causing
794 ;; the TAIL-SET object no longer to be valid to describe our
795 ;; return value. Thus, we delete ourselves from that object;
796 ;; but we save a newly-allocated tail-set, derived from the old
797 ;; one, for ourselves, for the use of later code (e.g.
798 ;; FINALIZE-XEP-DEFINITION) which might want to
799 ;; know about our return type.
800 (let* ((old-tail-set (lambda-tail-set clambda))
801 (old-tail-set-funs (tail-set-funs old-tail-set)))
802 (unless (= 1 (length old-tail-set-funs))
803 (setf (tail-set-funs old-tail-set)
804 (delete clambda old-tail-set-funs))
805 (let ((new-tail-set (copy-tail-set old-tail-set)))
806 (setf (lambda-tail-set clambda) new-tail-set
807 (tail-set-funs new-tail-set) (list clambda)))))
808 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
809 ;; remain valid in this case, so we nuke it on the theory that
810 ;; missing information tends to be less dangerous than incorrect
811 ;; information.
812 (setf (tail-set-info (lambda-tail-set clambda)) nil))
814 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
815 ;;; its LETs to LETs for the CALL's home function. We merge the calls
816 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
817 ;;; in the process. We also merge the ENTRIES.
819 ;;; We also unlink the function head from the component head and set
820 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
821 ;;; recomputed.
822 (defun merge-lets (clambda call)
824 (declare (type clambda clambda) (type basic-combination call))
826 (let ((component (node-component call)))
827 (unlink-blocks (component-head component) (lambda-block clambda))
828 (setf (component-lambdas component)
829 (delete clambda (component-lambdas component)))
830 (setf (component-reanalyze component) t))
831 (setf (lambda-call-lexenv clambda) (node-lexenv call))
833 (depart-from-tail-set clambda)
835 (let* ((home (node-home-lambda call))
836 (home-physenv (lambda-physenv home))
837 (physenv (lambda-physenv clambda)))
839 (aver (not (eq home clambda)))
841 ;; CLAMBDA belongs to HOME now.
842 (push clambda (lambda-lets home))
843 (setf (lambda-home clambda) home)
844 (setf (lambda-physenv clambda) home-physenv)
846 (when physenv
847 (setf (physenv-nlx-info home-physenv)
848 (nconc (physenv-nlx-info physenv)
849 (physenv-nlx-info home-physenv))))
851 ;; All of CLAMBDA's LETs belong to HOME now.
852 (let ((lets (lambda-lets clambda)))
853 (dolist (let lets)
854 (setf (lambda-home let) home)
855 (setf (lambda-physenv let) home-physenv))
856 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
857 ;; CLAMBDA no longer has an independent existence as an entity
858 ;; which has LETs.
859 (setf (lambda-lets clambda) nil)
861 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
862 ;; DFO dependencies.
863 (sset-union (lambda-calls-or-closes home)
864 (lambda-calls-or-closes clambda))
865 (sset-delete clambda (lambda-calls-or-closes home))
866 ;; CLAMBDA no longer has an independent existence as an entity
867 ;; which calls things or has DFO dependencies.
868 (setf (lambda-calls-or-closes clambda) nil)
870 ;; All of CLAMBDA's ENTRIES belong to HOME now.
871 (setf (lambda-entries home)
872 (nconc (lambda-entries clambda)
873 (lambda-entries home)))
874 ;; CLAMBDA no longer has an independent existence as an entity
875 ;; with ENTRIES.
876 (setf (lambda-entries clambda) nil))
878 (values))
880 ;;; Handle the value semantics of LET conversion. Delete FUN's return
881 ;;; node, and change the control flow to transfer to NEXT-BLOCK
882 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
883 (defun move-return-uses (fun call next-block)
884 (declare (type clambda fun) (type basic-combination call)
885 (type cblock next-block))
886 (let* ((return (lambda-return fun))
887 (return-block (progn
888 (ensure-block-start (node-prev return))
889 (node-block return))))
890 (unlink-blocks return-block
891 (component-tail (block-component return-block)))
892 (link-blocks return-block next-block)
893 (unlink-node return)
894 (delete-return return)
895 (let ((result (return-result return))
896 (lvar (if (node-tail-p call)
897 (return-result (lambda-return (node-home-lambda call)))
898 (node-lvar call)))
899 (call-type (node-derived-type call)))
900 (unless (eq call-type *wild-type*)
901 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
902 (do-uses (use result)
903 (derive-node-type use call-type)))
904 (substitute-lvar-uses lvar result
905 (and lvar (eq (lvar-uses lvar) call)))))
906 (values))
908 ;;; We are converting FUN to be a LET when the call is in a non-tail
909 ;;; position. Any previously tail calls in FUN are no longer tail
910 ;;; calls, and must be restored to normal calls which transfer to
911 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
912 ;;; the RETURN-RESULT, because the return might have been deleted (if
913 ;;; all calls were TR.)
914 (defun unconvert-tail-calls (fun call next-block)
915 (do-sset-elements (called (lambda-calls-or-closes fun))
916 (when (lambda-p called)
917 (dolist (ref (leaf-refs called))
918 (let ((this-call (node-dest ref)))
919 (when (and this-call
920 (node-tail-p this-call)
921 (eq (node-home-lambda this-call) fun))
922 (setf (node-tail-p this-call) nil)
923 (ecase (functional-kind called)
924 ((nil :cleanup :optional)
925 (let ((block (node-block this-call))
926 (lvar (node-lvar call)))
927 (unlink-blocks block (first (block-succ block)))
928 (link-blocks block next-block)
929 (aver (not (node-lvar this-call)))
930 (add-lvar-use this-call lvar)))
931 (:deleted)
932 ;; The called function might be an assignment in the
933 ;; case where we are currently converting that function.
934 ;; In steady-state, assignments never appear as a called
935 ;; function.
936 (:assignment
937 (aver (eq called fun)))))))))
938 (values))
940 ;;; Deal with returning from a LET or assignment that we are
941 ;;; converting. FUN is the function we are calling, CALL is a call to
942 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
943 ;;; NULL if call is a tail call.
945 ;;; If the call is not a tail call, then we must do
946 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
947 ;;; its value out of the enclosing non-let function. When call is
948 ;;; non-TR, we must convert it back to an ordinary local call, since
949 ;;; the value must be delivered to the receiver of CALL's value.
951 ;;; We do different things depending on whether the caller and callee
952 ;;; have returns left:
954 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
955 ;;; Either the function doesn't return, or all returns are via
956 ;;; tail-recursive local calls.
957 ;;; -- If CALL is a non-tail call, or if both have returns, then
958 ;;; we delete the callee's return, move its uses to the call's
959 ;;; result lvar, and transfer control to the appropriate
960 ;;; return point.
961 ;;; -- If the callee has a return, but the caller doesn't, then we
962 ;;; move the return to the caller.
963 (defun move-return-stuff (fun call next-block)
964 (declare (type clambda fun) (type basic-combination call)
965 (type (or cblock null) next-block))
966 (when next-block
967 (unconvert-tail-calls fun call next-block))
968 (let* ((return (lambda-return fun))
969 (call-fun (node-home-lambda call))
970 (call-return (lambda-return call-fun)))
971 (when (and call-return
972 (block-delete-p (node-block call-return)))
973 (delete-return call-return)
974 (unlink-node call-return)
975 (setq call-return nil))
976 (cond ((not return))
977 ((or next-block call-return)
978 (unless (block-delete-p (node-block return))
979 (unless next-block
980 (ensure-block-start (node-prev call-return))
981 (setq next-block (node-block call-return)))
982 (move-return-uses fun call next-block)))
984 (aver (node-tail-p call))
985 (setf (lambda-return call-fun) return)
986 (setf (return-lambda return) call-fun)
987 (setf (lambda-return fun) nil))))
988 (%delete-lvar-use call) ; LET call does not have value semantics
989 (values))
991 ;;; Actually do LET conversion. We call subfunctions to do most of the
992 ;;; work. We do REOPTIMIZE-LVAR on the args and CALL's lvar so that
993 ;;; LET-specific IR1 optimizations get a chance. We blow away any
994 ;;; entry for the function in *FREE-FUNS* so that nobody will create
995 ;;; new references to it.
996 (defun let-convert (fun call)
997 (declare (type clambda fun) (type basic-combination call))
998 (let* ((next-block (insert-let-body fun call))
999 (next-block (if (node-tail-p call)
1001 next-block)))
1002 (move-return-stuff fun call next-block)
1003 (merge-lets fun call)
1004 (setf (node-tail-p call) nil)
1005 ;; If CALL has a derive type NIL, it means that "its return" is
1006 ;; unreachable, but the next BIND is still reachable; in order to
1007 ;; not confuse MAYBE-TERMINATE-BLOCK...
1008 (setf (node-derived-type call) *wild-type*)))
1010 ;;; Reoptimize all of CALL's args and its result.
1011 (defun reoptimize-call (call)
1012 (declare (type basic-combination call))
1013 (dolist (arg (basic-combination-args call))
1014 (when arg
1015 (reoptimize-lvar arg)))
1016 (reoptimize-lvar (node-lvar call))
1017 (values))
1019 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
1020 ;;; LET converted?
1021 (defun declarations-suppress-let-conversion-p (clambda)
1022 ;; From the user's point of view, LET-converting something that
1023 ;; has a name is inlining it. (The user can't see what we're doing
1024 ;; with anonymous things, and suppressing inlining
1025 ;; for such things can easily give Python acute indigestion, so
1026 ;; we don't.)
1027 (when (leaf-has-source-name-p clambda)
1028 ;; ANSI requires that explicit NOTINLINE be respected.
1029 (or (eq (lambda-inlinep clambda) :notinline)
1030 ;; If (= LET-CONVERSION 0) we can guess that inlining
1031 ;; generally won't be appreciated, but if the user
1032 ;; specifically requests inlining, that takes precedence over
1033 ;; our general guess.
1034 (and (policy clambda (= let-conversion 0))
1035 (not (eq (lambda-inlinep clambda) :inline))))))
1037 ;;; We also don't convert calls to named functions which appear in the
1038 ;;; initial component, delaying this until optimization. This
1039 ;;; minimizes the likelihood that we will LET-convert a function which
1040 ;;; may have references added due to later local inline expansion.
1041 (defun ok-initial-convert-p (fun)
1042 (not (and (leaf-has-source-name-p fun)
1043 (or (declarations-suppress-let-conversion-p fun)
1044 (eq (component-kind (lambda-component fun))
1045 :initial)))))
1047 ;;; This function is called when there is some reason to believe that
1048 ;;; CLAMBDA might be converted into a LET. This is done after local
1049 ;;; call analysis, and also when a reference is deleted. We return
1050 ;;; true if we converted.
1051 (defun maybe-let-convert (clambda)
1052 (declare (type clambda clambda))
1053 (unless (or (declarations-suppress-let-conversion-p clambda)
1054 (functional-has-external-references-p clambda))
1055 ;; We only convert to a LET when the function is a normal local
1056 ;; function, has no XEP, and is referenced in exactly one local
1057 ;; call. Conversion is also inhibited if the only reference is in
1058 ;; a block about to be deleted.
1060 ;; These rules limiting LET conversion may seem unnecessarily
1061 ;; restrictive, since there are some cases where we could do the
1062 ;; return with a jump that don't satisfy these requirements. The
1063 ;; reason for doing things this way is that it makes the concept
1064 ;; of a LET much more useful at the level of IR1 semantics. The
1065 ;; :ASSIGNMENT function kind provides another way to optimize
1066 ;; calls to single-return/multiple call functions.
1068 ;; We don't attempt to convert calls to functions that have an
1069 ;; XEP, since we might be embarrassed later when we want to
1070 ;; convert a newly discovered local call. Also, see
1071 ;; OK-INITIAL-CONVERT-P.
1072 (let ((refs (leaf-refs clambda)))
1073 (when (and refs
1074 (null (rest refs))
1075 (memq (functional-kind clambda) '(nil :assignment))
1076 (not (functional-entry-fun clambda)))
1077 (binding* ((ref (first refs))
1078 (ref-lvar (node-lvar ref) :exit-if-null)
1079 (dest (lvar-dest ref-lvar)))
1080 (when (and (basic-combination-p dest)
1081 (eq (basic-combination-fun dest) ref-lvar)
1082 (eq (basic-combination-kind dest) :local)
1083 (not (node-to-be-deleted-p dest))
1084 (not (block-delete-p (lambda-block clambda)))
1085 (cond ((ok-initial-convert-p clambda) t)
1087 (reoptimize-lvar ref-lvar)
1088 nil)))
1089 (when (eq clambda (node-home-lambda dest))
1090 (delete-lambda clambda)
1091 (return-from maybe-let-convert nil))
1092 (unless (eq (functional-kind clambda) :assignment)
1093 (let-convert clambda dest))
1094 (reoptimize-call dest)
1095 (setf (functional-kind clambda)
1096 (if (mv-combination-p dest) :mv-let :let))))
1097 t))))
1099 ;;;; tail local calls and assignments
1101 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1102 ;;; they definitely won't generate any cleanup code. Currently we
1103 ;;; recognize lexical entry points that are only used locally (if at
1104 ;;; all).
1105 (defun only-harmless-cleanups (block1 block2)
1106 (declare (type cblock block1 block2))
1107 (or (eq block1 block2)
1108 (let ((cleanup2 (block-start-cleanup block2)))
1109 (do ((cleanup (block-end-cleanup block1)
1110 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1111 ((eq cleanup cleanup2) t)
1112 (case (cleanup-kind cleanup)
1113 ((:block :tagbody)
1114 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1115 (return nil)))
1116 (t (return nil)))))))
1118 ;;; If a potentially TR local call really is TR, then convert it to
1119 ;;; jump directly to the called function. We also call
1120 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1121 ;;; tail-convert. The second is the value of M-C-T-A.
1122 (defun maybe-convert-tail-local-call (call)
1123 (declare (type combination call))
1124 (let ((return (lvar-dest (node-lvar call)))
1125 (fun (combination-lambda call)))
1126 (aver (return-p return))
1127 (when (and (not (node-tail-p call)) ; otherwise already converted
1128 ;; this is a tail call
1129 (immediately-used-p (return-result return) call)
1130 (only-harmless-cleanups (node-block call)
1131 (node-block return))
1132 ;; If the call is in an XEP, we might decide to make it
1133 ;; non-tail so that we can use known return inside the
1134 ;; component.
1135 (not (eq (functional-kind (node-home-lambda call))
1136 :external))
1137 (not (block-delete-p (lambda-block fun))))
1138 (node-ends-block call)
1139 (let ((block (node-block call)))
1140 (setf (node-tail-p call) t)
1141 (unlink-blocks block (first (block-succ block)))
1142 (link-blocks block (lambda-block fun))
1143 (delete-lvar-use call)
1144 (values t (maybe-convert-to-assignment fun))))))
1146 ;;; This is called when we believe it might make sense to convert
1147 ;;; CLAMBDA to an assignment. All this function really does is
1148 ;;; determine when a function with more than one call can still be
1149 ;;; combined with the calling function's environment. We can convert
1150 ;;; when:
1151 ;;; -- The function is a normal, non-entry function, and
1152 ;;; -- Except for one call, all calls must be tail recursive calls
1153 ;;; in the called function (i.e. are self-recursive tail calls)
1154 ;;; -- OK-INITIAL-CONVERT-P is true.
1156 ;;; There may be one outside call, and it need not be tail-recursive.
1157 ;;; Since all tail local calls have already been converted to direct
1158 ;;; transfers, the only control semantics needed are to splice in the
1159 ;;; body at the non-tail call. If there is no non-tail call, then we
1160 ;;; need only merge the environments. Both cases are handled by
1161 ;;; LET-CONVERT.
1163 ;;; ### It would actually be possible to allow any number of outside
1164 ;;; calls as long as they all return to the same place (i.e. have the
1165 ;;; same conceptual continuation.) A special case of this would be
1166 ;;; when all of the outside calls are tail recursive.
1167 (defun maybe-convert-to-assignment (clambda)
1168 (declare (type clambda clambda))
1169 (when (and (not (functional-kind clambda))
1170 (not (functional-entry-fun clambda))
1171 (not (functional-has-external-references-p clambda)))
1172 (let ((outside-non-tail-call nil)
1173 (outside-call nil))
1174 (when (and (dolist (ref (leaf-refs clambda) t)
1175 (let ((dest (node-dest ref)))
1176 (when (or (not dest)
1177 (block-delete-p (node-block dest)))
1178 (return nil))
1179 (let ((home (node-home-lambda ref)))
1180 (unless (eq home clambda)
1181 (when outside-call
1182 (return nil))
1183 (setq outside-call dest))
1184 (unless (node-tail-p dest)
1185 (when (or outside-non-tail-call (eq home clambda))
1186 (return nil))
1187 (setq outside-non-tail-call dest)))))
1188 (ok-initial-convert-p clambda))
1189 (cond (outside-call (setf (functional-kind clambda) :assignment)
1190 (let-convert clambda outside-call)
1191 (when outside-non-tail-call
1192 (reoptimize-call outside-non-tail-call))
1194 (t (delete-lambda clambda)
1195 nil))))))