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