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