1 ;;;; This file implements local call analysis. A local call is a
2 ;;;; function call between functions being compiled at the same time.
3 ;;;; If we can tell at compile time that such a call is legal, then we
4 ;;;; change the combination to call the correct lambda, mark it as
5 ;;;; local, and add this link to our call graph. Once a call is local,
6 ;;;; it is then eligible for let conversion, which places the body of
7 ;;;; the function inline.
9 ;;;; We cannot always do a local call even when we do have the
10 ;;;; function being called. Calls that cannot be shown to have legal
11 ;;;; arg counts are not converted.
13 ;;;; This software is part of the SBCL system. See the README file for
14 ;;;; more information.
16 ;;;; This software is derived from the CMU CL system, which was
17 ;;;; written at Carnegie Mellon University and released into the
18 ;;;; public domain. The software is in the public domain and is
19 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
20 ;;;; files for more information.
24 ;;; This function propagates information from the variables in the
25 ;;; function FUN to the actual arguments in CALL. This is also called
26 ;;; by the VALUES IR1 optimizer when it sleazily converts MV-BINDs to
29 ;;; We flush all arguments to CALL that correspond to unreferenced
30 ;;; variables in FUN. We leave NILs in the COMBINATION-ARGS so that
31 ;;; the remaining args still match up with their vars.
33 ;;; We also apply the declared variable type assertion to the argument
35 (defun propagate-to-args (call fun
)
36 (declare (type combination call
) (type clambda fun
))
37 (loop with policy
= (lexenv-policy (node-lexenv call
))
38 for args on
(basic-combination-args call
)
39 and var in
(lambda-vars fun
)
40 do
(assert-lvar-type (car args
) (leaf-type var
) policy
41 (lambda-var-%source-name var
))
42 do
(unless (leaf-refs var
)
43 (flush-dest (car args
))
44 (setf (car args
) nil
)))
47 (defun recognize-dynamic-extent-lvars (call fun
)
48 (declare (type combination call
) (type clambda fun
))
49 (loop for arg in
(basic-combination-args call
)
50 for var in
(lambda-vars fun
)
51 for dx
= (leaf-dynamic-extent var
)
52 when
(and dx arg
(not (lvar-dynamic-extent arg
)))
53 append
(handle-nested-dynamic-extent-lvars dx arg
) into dx-lvars
54 ;; The block may end up being deleted due to cast optimization
55 ;; caused by USE-GOOD-FOR-DX-P
56 when
(node-to-be-deleted-p call
) return nil
57 finally
(when dx-lvars
58 ;; Stack analysis requires that the CALL ends the block, so
59 ;; that MAP-BLOCK-NLXES sees the cleanup we insert here.
60 (node-ends-block call
)
61 (let* ((entry (with-ir1-environment-from-node call
63 (cleanup (make-cleanup :kind
:dynamic-extent
66 (setf (entry-cleanup entry
) cleanup
)
67 (insert-node-before call entry
)
68 (setf (node-lexenv call
)
69 (make-lexenv :default
(node-lexenv call
)
71 (push entry
(lambda-entries (node-home-lambda entry
)))
72 (dolist (cell dx-lvars
)
73 (setf (lvar-dynamic-extent (cdr cell
)) cleanup
)))))
76 ;;; This function handles merging the tail sets if CALL is potentially
77 ;;; tail-recursive, and is a call to a function with a different
78 ;;; TAIL-SET than CALL's FUN. This must be called whenever we alter
79 ;;; IR1 so as to place a local call in what might be a tail-recursive
80 ;;; context. Note that any call which returns its value to a RETURN is
81 ;;; considered potentially tail-recursive, since any implicit MV-PROG1
82 ;;; might be optimized away.
84 ;;; We destructively modify the set for the calling function to
85 ;;; represent both, and then change all the functions in callee's set
86 ;;; to reference the first. If we do merge, we reoptimize the
87 ;;; RETURN-RESULT lvar to cause IR1-OPTIMIZE-RETURN to recompute the
89 (defun merge-tail-sets (call &optional
(new-fun (combination-lambda call
)))
90 (declare (type basic-combination call
) (type clambda new-fun
))
91 (let ((return (node-dest call
)))
92 (when (and (return-p return
)
93 (not (memq (functional-kind new-fun
) '(:deleted
:zombie
))))
94 (let ((call-set (lambda-tail-set (node-home-lambda call
)))
95 (fun-set (lambda-tail-set new-fun
)))
96 (unless (eq call-set fun-set
)
97 (let ((funs (tail-set-funs fun-set
)))
99 (setf (lambda-tail-set fun
) call-set
))
100 (setf (tail-set-funs call-set
)
101 (nconc (tail-set-funs call-set
) funs
)))
102 (reoptimize-lvar (return-result return
))
105 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set
106 ;;; the combination kind to :LOCAL, add FUN to the CALLS of the
107 ;;; function that the call is in, call MERGE-TAIL-SETS, then replace
108 ;;; the function in the REF node with the new function.
110 ;;; We change the REF last, since changing the reference can trigger
111 ;;; LET conversion of the new function, but will only do so if the
112 ;;; call is local. Note that the replacement may trigger LET
113 ;;; conversion or other changes in IR1. We must call MERGE-TAIL-SETS
114 ;;; with NEW-FUN before the substitution, since after the substitution
115 ;;; (and LET conversion), the call may no longer be recognizable as
117 (defun convert-call (ref call fun
)
118 (declare (type ref ref
) (type combination call
) (type clambda fun
))
119 (propagate-to-args call fun
)
120 (unless (call-full-like-p call
)
121 (dolist (arg (basic-combination-args call
))
123 (flush-lvar-externally-checkable-type arg
))))
124 (setf (basic-combination-kind call
) :local
)
125 (sset-adjoin fun
(lambda-calls-or-closes (node-home-lambda call
)))
126 (recognize-dynamic-extent-lvars call fun
)
127 (merge-tail-sets call fun
)
128 (change-ref-leaf ref fun
)
131 ;;;; external entry point creation
133 ;;; Return a LAMBDA form that can be used as the definition of the XEP
136 ;;; If FUN is a LAMBDA, then we check the number of arguments
137 ;;; (conditional on policy) and call FUN with all the arguments.
139 ;;; If FUN is an OPTIONAL-DISPATCH, then we dispatch off of the number
140 ;;; of supplied arguments by doing do an = test for each entry-point,
141 ;;; calling the entry with the appropriate prefix of the passed
144 ;;; If there is a &MORE arg, then there are a couple of optimizations
145 ;;; that we make (more for space than anything else):
146 ;;; -- If MIN-ARGS is 0, then we make the more entry a T clause, since
147 ;;; no argument count error is possible.
148 ;;; -- We can omit the = clause for the last entry-point, allowing the
149 ;;; case of 0 more args to fall through to the more entry.
151 ;;; We don't bother to policy conditionalize wrong arg errors in
152 ;;; optional dispatches, since the additional overhead is negligible
153 ;;; compared to the cost of everything else going on.
155 ;;; Note that if policy indicates it, argument type declarations in
156 ;;; FUN will be verified. Since nothing is known about the type of the
157 ;;; XEP arg vars, type checks will be emitted when the XEP's arg vars
158 ;;; are passed to the actual function.
159 (defun make-xep-lambda-expression (fun)
160 (declare (type functional fun
))
163 (let* ((n-supplied (gensym))
164 (nargs (length (lambda-vars fun
)))
165 (temps (make-gensym-list nargs
)))
166 `(lambda (,n-supplied
,@temps
)
167 (declare (type index
,n-supplied
)
168 (ignore ,n-supplied
))
169 (%funcall
,fun
,@temps
))))
171 ;; Force convertion of all entries
172 (optional-dispatch-entry-point-fun fun
0)
173 (let* ((min (optional-dispatch-min-args fun
))
174 (max (optional-dispatch-max-args fun
))
175 (more (optional-dispatch-more-entry fun
))
176 (n-supplied (gensym))
177 (temps (make-gensym-list max
))
178 (main (optional-dispatch-main-entry fun
))
179 (optional-vars (nthcdr min
(lambda-vars main
)))
180 (keyp (optional-dispatch-keyp fun
))
182 ;; Ignore only the entries at the tail, can't
183 ;; deal with the values being used by
184 ;; subsequent default forms at the moment
185 (loop with previous-unused
= t
186 for last
= t then nil
187 for promise in
(reverse (optional-dispatch-entry-points fun
))
188 for ep
= (force promise
)
190 for optional-n downfrom
(- max min
)
191 unless
(and previous-unused
192 (can-ignore-optional-ep optional-n optional-vars
195 and do
(setf previous-unused
(eq ep main
))
196 else if
(and last more
)
197 collect
(cons main
(1+ n
))))))
198 `(lambda (,n-supplied
,@temps
)
199 (declare (type index
,n-supplied
)
200 (ignorable ,n-supplied
))
202 ,@(loop for previous-n
= (1- min
) then n
203 for
((ep . n
) . next
) on used-eps
206 `(,(if (= (1+ previous-n
) n
)
207 `(eql ,n-supplied
,n
)
208 `(<= ,n-supplied
,n
))
209 (%funcall
,ep
,@(subseq temps
0 n
))))
211 (with-unique-names (n-context n-count
)
214 `(multiple-value-bind (,n-context
,n-count
)
215 (%more-arg-context
,n-supplied
,max
)
216 (%funcall
,more
,@temps
,n-context
,n-count
))
217 ;; The &rest var is unused, call the main entry point directly
219 ,@(loop for supplied-p
= nil
221 (arg-info-supplied-p info
))
223 for x in
(lambda-vars ep
)
224 for info
= (lambda-var-arg-info x
)
229 (eq (arg-info-kind info
)
236 ;; Arg-checking is performed before this step,
237 ;; arranged by INIT-XEP-ENVIRONMENT,
238 ;; perform the last action unconditionally,
239 ;; and without this the function derived type will be bad.
240 (%funcall
,ep
,@(subseq temps
0 n
))))))))))))
242 (defun can-ignore-optional-ep (n vars keyp
)
243 (let ((var (loop with i
= n
245 when
(and (lambda-var-arg-info var
)
249 (not (lambda-var-refs var
))
250 (eql (lambda-var-type var
) *universal-type
*))
251 (let* ((info (lambda-var-arg-info var
))
252 (kind (arg-info-kind info
)))
253 (or (and (eq kind
:optional
)
254 (not (arg-info-supplied-p info
))
255 (constantp (arg-info-default info
)))
259 ;;; Make an external entry point (XEP) for FUN and return it. We
260 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
261 ;;; then associate this lambda with FUN as its XEP. After the
262 ;;; conversion, we iterate over the function's associated lambdas,
263 ;;; redoing local call analysis so that the XEP calls will get
266 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
267 ;;; discover an XEP after the initial local call analyze pass.
268 (defun make-xep (fun)
269 (declare (type functional fun
))
270 (aver (null (functional-entry-fun fun
)))
271 (with-ir1-environment-from-node (lambda-bind (main-entry fun
))
272 (let ((xep (ir1-convert-lambda (make-xep-lambda-expression fun
)
273 :debug-name
(debug-name
274 'xep
(leaf-debug-name fun
))
276 (setf (functional-kind xep
) :external
277 (leaf-ever-used xep
) t
278 (functional-entry-fun xep
) fun
279 (functional-entry-fun fun
) xep
280 (component-reanalyze *current-component
*) t
)
281 (reoptimize-component *current-component
* :maybe
)
282 (locall-analyze-xep-entry-point fun
)
285 (defun locall-analyze-xep-entry-point (fun)
286 (declare (type functional fun
))
289 (locall-analyze-fun-1 fun
))
291 (dolist (ep (optional-dispatch-entry-points fun
))
292 (locall-analyze-fun-1 (force ep
)))
293 (when (optional-dispatch-more-entry fun
)
294 (locall-analyze-fun-1 (optional-dispatch-more-entry fun
))))))
296 ;;; Notice a REF that is not in a local-call context. If the REF is
297 ;;; already to an XEP, then do nothing, otherwise change it to the
298 ;;; XEP, making an XEP if necessary.
300 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
301 ;;; it as though it was not an XEP reference (i.e. leave it alone).
302 (defun reference-entry-point (ref)
303 (declare (type ref ref
))
304 (let ((fun (ref-leaf ref
)))
305 (unless (or (xep-p fun
)
306 (member (functional-kind fun
) '(:escape
:cleanup
308 (change-ref-leaf ref
(or (functional-entry-fun fun
)
311 ;;; Attempt to convert all references to FUN to local calls. The
312 ;;; reference must be the function for a call, and the function lvar
313 ;;; must be used only once, since otherwise we cannot be sure what
314 ;;; function is to be called. The call lvar would be multiply used if
315 ;;; there is hairy stuff such as conditionals in the expression that
316 ;;; computes the function.
318 ;;; If we cannot convert a reference, then we mark the referenced
319 ;;; function as an entry-point, creating a new XEP if necessary. We
320 ;;; don't try to convert calls that are in error (:ERROR kind.)
322 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
323 ;;; can force analysis of newly introduced calls. Note that we don't
324 ;;; do LET conversion here.
325 (defun locall-analyze-fun-1 (fun)
326 (declare (type functional fun
))
327 (let ((refs (leaf-refs fun
))
330 (let* ((lvar (node-lvar ref
))
331 (dest (when lvar
(lvar-dest lvar
))))
332 (unless (node-to-be-deleted-p ref
)
333 (cond ((and (basic-combination-p dest
)
334 (eq (basic-combination-fun dest
) lvar
)
335 (eq (lvar-uses lvar
) ref
))
337 (convert-call-if-possible ref dest
)
338 ;; It might have been deleted by CONVERT-CALL-IF-POSSIBLE
339 (when (eq (functional-kind fun
) :deleted
)
340 (return-from locall-analyze-fun-1
))
341 (unless (eq (basic-combination-kind dest
) :local
)
342 (reference-entry-point ref
)
345 (reference-entry-point ref
)
346 (setq local-p nil
))))))
347 (when local-p
(note-local-functional fun
)))
351 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
352 ;;; calls into local calls when it is legal. We also attempt to
353 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
354 ;;; deletion of a function reference, but functions that start out
355 ;;; eligible for conversion must be noticed sometime.
357 ;;; Note that there is a lot of action going on behind the scenes
358 ;;; here, triggered by reference deletion. In particular, the
359 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
360 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
361 ;;; the COMPONENT-LAMBDAS when it is. Also, the
362 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
363 ;;; it is not updated when we delete functions, etc. Only
364 ;;; COMPONENT-LAMBDAS is updated.
366 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
367 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
369 (defun locall-analyze-component (component)
370 (declare (type component component
))
371 (aver-live-component component
)
373 (let* ((new-functional (pop (component-new-functionals component
)))
374 (functional (or new-functional
375 (pop (component-reanalyze-functionals component
)))))
378 (let ((kind (functional-kind functional
)))
379 (cond ((or (functional-somewhat-letlike-p functional
)
380 (memq kind
'(:deleted
:zombie
)))
381 (values)) ; nothing to do
382 ((and (null (leaf-refs functional
)) (eq kind nil
)
383 (not (functional-entry-fun functional
)))
384 (delete-functional functional
))
386 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
387 (cond ((not (lambda-p functional
))
388 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
391 (new-functional ; FUNCTIONAL came from
392 ; NEW-FUNCTIONALS, hence is new.
393 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
394 (aver (not (member functional
395 (component-lambdas component
))))
396 (push functional
(component-lambdas component
)))
397 (t ; FUNCTIONAL is old.
398 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
399 (aver (member functional
(component-lambdas
401 (locall-analyze-fun-1 functional
)
402 (when (lambda-p functional
)
403 (maybe-let-convert functional component
)))))))
406 (defun locall-analyze-clambdas-until-done (clambdas)
408 (let ((did-something nil
))
409 (dolist (clambda clambdas
)
410 (let ((component (lambda-component clambda
)))
411 ;; The original CMU CL code seemed to implicitly assume that
412 ;; COMPONENT is the only one here. Let's make that explicit.
413 (aver (= 1 (length (functional-components clambda
))))
414 (aver (eql component
(first (functional-components clambda
))))
415 (when (or (component-new-functionals component
)
416 (component-reanalyze-functionals component
))
417 (setf did-something t
)
418 (locall-analyze-component component
))))
419 (unless did-something
423 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
424 ;;; to be in an infinite recursive loop, then change the reference to
425 ;;; reference a fresh copy. We return whichever function we decide to
427 (defun maybe-expand-local-inline (original-functional ref call
)
428 (if (and (policy call
429 (and (>= speed space
)
430 (>= speed compilation-speed
)))
431 (not (eq (functional-kind (node-home-lambda call
)) :external
))
432 (inline-expansion-ok call
))
433 (let* ((end (component-last-block (node-component call
)))
434 (pred (block-prev end
)))
435 (multiple-value-bind (losing-local-object converted-lambda
)
436 (catch 'locall-already-let-converted
437 (with-ir1-environment-from-node call
438 (let ((*lexenv
* (functional-lexenv original-functional
)))
441 (functional-inline-expansion original-functional
)
442 :debug-name
(debug-name 'local-inline
444 original-functional
)))))))
445 (cond (losing-local-object
446 (if (functional-p losing-local-object
)
447 (let ((*compiler-error-context
* call
))
448 (compiler-notify "couldn't inline expand because expansion ~
449 calls this LET-converted local function:~
451 (leaf-debug-name losing-local-object
)))
452 (let ((*compiler-error-context
* call
))
453 (compiler-notify "implementation limitation: couldn't inline ~
454 expand because expansion refers to ~
455 the optimized away object ~S."
456 losing-local-object
)))
457 (loop for block
= (block-next pred
) then
(block-next block
)
459 do
(setf (block-delete-p block
) t
))
460 (loop for block
= (block-next pred
) then
(block-next block
)
462 do
(delete-block block t
))
465 (change-ref-leaf ref converted-lambda
)
467 original-functional
))
469 ;;; Dispatch to the appropriate function to attempt to convert a call.
470 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
471 ;;; optimization as well as in local call analysis. If the call is is
472 ;;; already :LOCAL, we do nothing. If the call is already scheduled
473 ;;; for deletion, also do nothing (in addition to saving time, this
474 ;;; also avoids some problems with optimizing collections of functions
475 ;;; that are partially deleted.)
477 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
478 ;;; called on a :INITIAL component, we don't care whether the caller
479 ;;; and callee are in the same component. Afterward, we must stick
480 ;;; with whatever component division we have chosen.
482 ;;; Before attempting to convert a call, we see whether the function
483 ;;; is supposed to be inline expanded. Call conversion proceeds as
484 ;;; before after any expansion.
486 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
487 ;;; warnings will get the right context.
488 (defun convert-call-if-possible (ref call
)
489 (declare (type ref ref
) (type basic-combination call
))
490 (let* ((block (node-block call
))
491 (component (block-component block
))
492 (original-fun (ref-leaf ref
)))
493 (aver (functional-p original-fun
))
494 (unless (or (member (basic-combination-kind call
) '(:local
:error
))
495 (node-to-be-deleted-p call
)
496 (member (functional-kind original-fun
)
497 '(:toplevel-xep
:deleted
))
498 (not (or (eq (component-kind component
) :initial
)
501 (lambda-bind (main-entry original-fun
))))
503 (let ((fun (if (xep-p original-fun
)
504 (functional-entry-fun original-fun
)
506 (*compiler-error-context
* call
))
508 (when (and (eq (functional-inlinep fun
) :inline
)
509 (rest (leaf-refs original-fun
)))
510 (setq fun
(maybe-expand-local-inline fun ref call
)))
512 (aver (member (functional-kind fun
)
513 '(nil :escape
:cleanup
:optional
)))
514 (cond ((mv-combination-p call
)
515 (convert-mv-call ref call fun
))
517 (convert-lambda-call ref call fun
))
519 (convert-hairy-call ref call fun
))))))
523 ;;; Attempt to convert a multiple-value call. The only interesting
524 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
525 ;;; exactly one reference and no XEP, and is called with one values
528 ;;; We change the call to be to the last optional entry point and
529 ;;; change the call to be local. Due to our preconditions, the call
530 ;;; should eventually be converted to a let, but we can't do that now,
531 ;;; since there may be stray references to the e-p lambda due to
532 ;;; optional defaulting code.
534 ;;; We also use variable types for the called function to construct an
535 ;;; assertion for the values lvar.
537 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
538 (defun convert-mv-call (ref call fun
)
539 (declare (type ref ref
) (type mv-combination call
) (type functional fun
))
540 (when (and (looks-like-an-mv-bind fun
)
541 (singleton-p (leaf-refs fun
))
542 (not (functional-entry-fun fun
)))
543 (let* ((*current-component
* (node-component ref
))
544 (ep (optional-dispatch-entry-point-fun
545 fun
(optional-dispatch-max-args fun
)))
546 (args (basic-combination-args call
)))
547 (when (and (null (leaf-refs ep
))
548 (or (singleton-p args
)
549 (call-all-args-fixed-p call
)))
550 (aver (= (optional-dispatch-min-args fun
) 0))
551 (setf (basic-combination-kind call
) :local
)
552 (sset-adjoin ep
(lambda-calls-or-closes (node-home-lambda call
)))
553 (merge-tail-sets call ep
)
554 (change-ref-leaf ref ep
)
555 (if (singleton-p args
)
558 (make-short-values-type (mapcar #'leaf-type
(lambda-vars ep
)))
559 (lexenv-policy (node-lexenv call
)))
560 (let ((vars (lambda-vars ep
)))
561 (loop for arg in args
566 (make-short-values-type
568 (loop repeat
(nth-value 1 (values-types
569 (lvar-derived-type arg
)))
571 collect
(leaf-type var
))))
572 (lexenv-policy (node-lexenv call
)))))))))
575 ;;; Convenience function to mark local calls as known bad.
576 (defun transform-call-with-ir1-environment (node lambda default-name
)
577 (aver (combination-p node
))
578 (with-ir1-environment-from-node node
579 (transform-call node lambda
580 (or (combination-fun-source-name node nil
)
583 (defun warn-invalid-local-call (node count
&rest warn-arguments
)
584 (declare (notinline warn
)) ; See COMPILER-WARN for rationale
585 (aver (combination-p node
))
586 (aver (typep count
'unsigned-byte
))
587 (apply 'warn warn-arguments
) ; XXX: Should this be COMPILER-WARN?
588 (transform-call-with-ir1-environment
590 `(lambda (&rest args
)
591 (declare (ignore args
))
592 (%local-arg-count-error
,count
',(combination-fun-debug-name node
)))
593 '%local-arg-count-error
))
595 ;;; Attempt to convert a call to a lambda. If the number of args is
596 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
597 ;;; from future consideration. If the argcount is O.K. then we just
599 (defun convert-lambda-call (ref call fun
)
600 (declare (type ref ref
) (type combination call
) (type clambda fun
))
601 (let ((nargs (length (lambda-vars fun
)))
602 (n-call-args (length (combination-args call
))))
603 (cond ((= n-call-args nargs
)
604 (convert-call ref call fun
))
606 (warn-invalid-local-call call n-call-args
607 'local-argument-mismatch
609 "function called with ~R argument~:P, but wants exactly ~R"
610 :format-arguments
(list n-call-args nargs
))))))
612 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
614 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
615 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
616 ;;; a call to the correct entry point. If &KEY args are supplied, then
617 ;;; dispatch to a subfunction. We don't convert calls to functions
618 ;;; that have a &MORE (or &REST) arg.
619 (defun convert-hairy-call (ref call fun
)
620 (declare (type ref ref
) (type combination call
)
621 (type optional-dispatch fun
))
622 (let ((min-args (optional-dispatch-min-args fun
))
623 (max-args (optional-dispatch-max-args fun
))
624 (call-args (length (combination-args call
))))
625 (cond ((< call-args min-args
)
626 (warn-invalid-local-call call call-args
627 'local-argument-mismatch
629 "function called with ~R argument~:P, but wants at least ~R"
630 :format-arguments
(list call-args min-args
)))
631 ((<= call-args max-args
)
632 (convert-call ref call
633 (let ((*current-component
* (node-component ref
)))
634 (optional-dispatch-entry-point-fun
635 fun
(- call-args min-args
)))))
636 ((optional-dispatch-more-entry fun
)
637 (convert-more-call ref call fun
))
639 (warn-invalid-local-call call call-args
640 'local-argument-mismatch
642 "function called with ~R argument~:P, but wants at most ~R"
644 (list call-args max-args
)))))
647 ;;; This function is used to convert a call to an entry point when
648 ;;; complex transformations need to be done on the original arguments.
649 ;;; ENTRY is the entry point function that we are calling. VARS is a
650 ;;; list of variable names which are bound to the original call
651 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
652 ;;; is the list of arguments to the entry point function.
654 ;;; In order to avoid gruesome graph grovelling, we introduce a new
655 ;;; function that rearranges the arguments and calls the entry point.
656 ;;; We analyze the new function and the entry point immediately so
657 ;;; that everything gets converted during the single pass.
658 (defun convert-hairy-fun-entry (ref call entry vars ignores args indef
)
659 (declare (list vars ignores args
) (type ref ref
) (type combination call
)
660 (type clambda entry
))
662 (with-ir1-environment-from-node call
665 (declare (ignorable ,@ignores
)
666 (indefinite-extent ,@indef
))
667 (%funcall
,entry
,@args
))
668 :debug-name
(debug-name 'hairy-function-entry
670 (basic-combination-fun call
)))
672 (convert-call ref call new-fun
)
673 (dolist (ref (leaf-refs entry
))
674 (convert-call-if-possible ref
(lvar-dest (node-lvar ref
))))))
676 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
677 ;;; function into a local call to the MAIN-ENTRY.
679 ;;; First we verify that all keywords are constant and legal. If there
680 ;;; aren't, then we warn the user and don't attempt to convert the call.
682 ;;; We massage the supplied &KEY arguments into the order expected
683 ;;; by the main entry. This is done by binding all the arguments to
684 ;;; the keyword call to variables in the introduced lambda, then
685 ;;; passing these values variables in the correct order when calling
686 ;;; the main entry. Unused arguments (such as the keywords themselves)
687 ;;; are discarded simply by not passing them along.
689 ;;; If there is a &REST arg, then we bundle up the args and pass them
691 (defun convert-more-call (ref call fun
)
692 (declare (type ref ref
) (type combination call
) (type optional-dispatch fun
))
693 (let* ((max (optional-dispatch-max-args fun
))
694 (arglist (optional-dispatch-arglist fun
))
695 (args (combination-args call
))
696 (more (nthcdr max args
))
697 (flame (policy call
(or (> speed inhibit-warnings
)
698 (> space inhibit-warnings
))))
702 (temps (make-gensym-list max
))
703 (more-temps (make-gensym-list (length more
))))
708 (dolist (var arglist
)
709 (let ((info (lambda-var-arg-info var
)))
711 (ecase (arg-info-kind info
)
715 ((:more-context
:more-count
)
716 (compiler-warn "can't local-call functions with &MORE args")
717 (setf (basic-combination-kind call
) :error
)
718 (return-from convert-more-call
))))))
720 (when (optional-dispatch-keyp fun
)
721 (when (oddp (length more
))
722 (compiler-warn "function called with odd number of ~
723 arguments in keyword portion")
724 (transform-call-with-ir1-environment
726 `(lambda (&rest args
)
727 (declare (ignore args
))
728 (%odd-key-args-error
))
729 '%odd-key-args-error
)
730 (return-from convert-more-call
))
732 (do ((key more
(cddr key
))
733 (temp more-temps
(cddr temp
)))
735 (let ((lvar (first key
)))
736 (unless (constant-lvar-p lvar
)
738 (compiler-notify "non-constant keyword in keyword call"))
739 (setf (basic-combination-kind call
) :error
)
740 (return-from convert-more-call
))
742 (let ((name (lvar-value lvar
))
745 (when (and (eq name
:allow-other-keys
) (not allow-found
))
746 (let ((val (second key
)))
747 (cond ((constant-lvar-p val
)
749 allowp
(lvar-value val
)))
751 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
752 (setf (basic-combination-kind call
) :error
)
753 (return-from convert-more-call
)))))
754 (dolist (var (key-vars)
757 (unless (eq name
:allow-other-keys
)
758 (setq loser
(list name
)))))
759 (let ((info (lambda-var-arg-info var
)))
760 (when (eq (arg-info-key info
) name
)
762 (if (member var
(supplied) :key
#'car
)
764 (supplied (cons var val
)))
767 (when (and loser
(not (optional-dispatch-allowp fun
)) (not allowp
))
768 (compiler-warn "function called with unknown argument keyword ~S"
770 (transform-call-with-ir1-environment
772 `(lambda (&rest args
)
773 (declare (ignore args
))
774 (%unknown-key-arg-error
',(car loser
) nil
))
775 '%unknown-key-arg-error
)
776 (return-from convert-more-call
)))
778 (collect ((call-args))
779 (do ((var arglist
(cdr var
))
780 (temp temps
(cdr temp
)))
782 (let ((info (lambda-var-arg-info (car var
))))
784 (ecase (arg-info-kind info
)
786 (call-args (car temp
))
787 (when (arg-info-supplied-p info
)
788 (call-args (if (arg-info-supplied-used-p info
)
792 (call-args `(list ,@more-temps
))
793 ;; &REST arguments may be accompanied by extra
794 ;; context and count arguments. We know this by
795 ;; the ARG-INFO-DEFAULT. Supply 0 and 0 or
796 ;; don't convert at all depending.
797 (let ((more (arg-info-default info
)))
800 (destructuring-bind (context count
&optional used
) more
801 (declare (ignore context count
))
803 ;; We've already converted to use the more context
804 ;; instead of the rest list.
805 (return-from convert-more-call
))))
808 (setf (arg-info-default info
) t
)))
812 (call-args (car temp
)))))
814 (dolist (var (key-vars))
815 (let ((info (lambda-var-arg-info var
))
816 (temp (cdr (assoc var
(supplied)))))
819 (call-args (arg-info-default info
)))
820 (when (arg-info-supplied-p info
)
821 (call-args (cond ((arg-info-supplied-used-p info
)
828 (convert-hairy-fun-entry ref call
(optional-dispatch-main-entry fun
)
829 (append temps more-temps
)
830 (ignores) (call-args)
831 (when (optional-rest-p fun
)
838 ;;;; Converting to a LET has differing significance to various parts
839 ;;;; of the compiler:
840 ;;;; -- The body of a LET is spliced in immediately after the
841 ;;;; corresponding combination node, making the control transfer
842 ;;;; explicit and allowing LETs to be mashed together into a single
843 ;;;; block. The value of the LET is delivered directly to the
844 ;;;; original lvar for the call, eliminating the need to
845 ;;;; propagate information from the dummy result lvar.
846 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
847 ;;;; that there is only one expression that the variable can be bound
848 ;;;; to, and this is easily substituted for.
849 ;;;; -- LETs are interesting to environment analysis and to the back
850 ;;;; end because in most ways a LET can be considered to be "the
851 ;;;; same function" as its home function.
852 ;;;; -- LET conversion has dynamic scope implications, since control
853 ;;;; transfers within the same environment are local. In a local
854 ;;;; control transfer, cleanup code must be emitted to remove
855 ;;;; dynamic bindings that are no longer in effect.
857 ;;; Set up the control transfer to the called CLAMBDA. We split the
858 ;;; call block immediately after the call, and link the head of
859 ;;; CLAMBDA to the call block. The successor block after splitting
860 ;;; (where we return to) is returned.
862 ;;; If the lambda is is a different component than the call, then we
863 ;;; call JOIN-COMPONENTS. This only happens in block compilation
864 ;;; before FIND-INITIAL-DFO.
865 (defun insert-let-body (clambda call
)
866 (declare (type clambda clambda
) (type basic-combination call
))
867 (let* ((call-block (node-block call
))
868 (bind-block (node-block (lambda-bind clambda
)))
869 (component (block-component call-block
)))
870 (aver-live-component component
)
871 (let ((clambda-component (block-component bind-block
)))
872 (unless (eq clambda-component component
)
873 (aver (eq (component-kind component
) :initial
))
874 (join-components component clambda-component
)))
875 (let ((*current-component
* component
))
876 (node-ends-block call
))
877 (destructuring-bind (next-block)
878 (block-succ call-block
)
879 (unlink-blocks call-block next-block
)
880 (link-blocks call-block bind-block
)
883 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
884 ;;; same set as; but leave CLAMBDA with a valid tail set value of
885 ;;; its own, for the benefit of code which might try to pull
886 ;;; something out of it (e.g. return type).
887 (defun depart-from-tail-set (clambda)
888 ;; Until sbcl-0.pre7.37.flaky5.2, we did
889 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
890 ;; (SETF (TAIL-SET-FUNS TAILS)
891 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
892 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
893 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
894 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
895 ;; inconsistent, and perhaps unsafe, for us to think we're in the
896 ;; tail set. Unfortunately..
898 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
899 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
900 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
901 ;; which you called in order to get the address of an external LAMBDA):
902 ;; the external function was defined in terms of internal function,
903 ;; which was LET-converted, and then things blew up downstream when
904 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
905 ;; the now-NILed-out TAIL-SET. So..
907 ;; To deal with this problem, we no longer NIL out
908 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
909 ;; * If we're the only function in TAIL-SET-FUNS, it should
910 ;; be safe to leave ourself linked to it, and it to you.
911 ;; * If there are other functions in TAIL-SET-FUNS, then we're
912 ;; afraid of future optimizations on those functions causing
913 ;; the TAIL-SET object no longer to be valid to describe our
914 ;; return value. Thus, we delete ourselves from that object;
915 ;; but we save a newly-allocated tail-set, derived from the old
916 ;; one, for ourselves, for the use of later code (e.g.
917 ;; FINALIZE-XEP-DEFINITION) which might want to
918 ;; know about our return type.
919 (let* ((old-tail-set (lambda-tail-set clambda
))
920 (old-tail-set-funs (tail-set-funs old-tail-set
)))
921 (unless (= 1 (length old-tail-set-funs
))
922 (setf (tail-set-funs old-tail-set
)
923 (delete clambda old-tail-set-funs
))
924 (let ((new-tail-set (copy-tail-set old-tail-set
)))
925 (setf (lambda-tail-set clambda
) new-tail-set
926 (tail-set-funs new-tail-set
) (list clambda
)))))
927 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
928 ;; remain valid in this case, so we nuke it on the theory that
929 ;; missing information tends to be less dangerous than incorrect
931 (setf (tail-set-info (lambda-tail-set clambda
)) nil
))
933 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
934 ;;; its LETs to LETs for the CALL's home function. We merge the calls
935 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
936 ;;; in the process. We also merge the ENTRIES.
938 ;;; We also unlink the function head from the component head and set
939 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
941 (defun merge-lets (clambda call
)
943 (declare (type clambda clambda
) (type basic-combination call
))
945 (let ((component (node-component call
)))
946 (unlink-blocks (component-head component
) (lambda-block clambda
))
947 (setf (component-lambdas component
)
948 (delete clambda
(component-lambdas component
)))
949 (setf (component-reanalyze component
) t
))
950 (setf (lambda-call-lexenv clambda
) (node-lexenv call
))
952 (depart-from-tail-set clambda
)
954 (let* ((home (node-home-lambda call
))
955 (home-physenv (lambda-physenv home
))
956 (physenv (lambda-physenv clambda
)))
958 (aver (not (eq home clambda
)))
960 ;; CLAMBDA belongs to HOME now.
961 (push clambda
(lambda-lets home
))
962 (setf (lambda-home clambda
) home
)
963 (setf (lambda-physenv clambda
) home-physenv
)
967 (setf home-physenv
(get-lambda-physenv home
)))
968 (setf (physenv-nlx-info home-physenv
)
969 (nconc (physenv-nlx-info physenv
)
970 (physenv-nlx-info home-physenv
))))
972 ;; All of CLAMBDA's LETs belong to HOME now.
973 (let ((lets (lambda-lets clambda
)))
975 (setf (lambda-home let
) home
)
976 (setf (lambda-physenv let
) home-physenv
))
977 (setf (lambda-lets home
) (nconc lets
(lambda-lets home
))))
978 ;; CLAMBDA no longer has an independent existence as an entity
980 (setf (lambda-lets clambda
) nil
)
982 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
984 (sset-union (lambda-calls-or-closes home
)
985 (lambda-calls-or-closes clambda
))
986 (sset-delete clambda
(lambda-calls-or-closes home
))
987 ;; CLAMBDA no longer has an independent existence as an entity
988 ;; which calls things or has DFO dependencies.
989 (setf (lambda-calls-or-closes clambda
) nil
)
991 ;; All of CLAMBDA's ENTRIES belong to HOME now.
992 (setf (lambda-entries home
)
993 (nconc (lambda-entries clambda
)
994 (lambda-entries home
)))
995 ;; CLAMBDA no longer has an independent existence as an entity
997 (setf (lambda-entries clambda
) nil
))
1001 ;;; Handle the value semantics of LET conversion. Delete FUN's return
1002 ;;; node, and change the control flow to transfer to NEXT-BLOCK
1003 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
1004 (defun move-return-uses (fun call next-block
)
1005 (declare (type clambda fun
) (type basic-combination call
)
1006 (type cblock next-block
))
1007 (let* ((return (lambda-return fun
))
1008 (return-block (progn
1009 (ensure-block-start (node-prev return
))
1010 (node-block return
))))
1011 (unlink-blocks return-block
1012 (component-tail (block-component return-block
)))
1013 (link-blocks return-block next-block
)
1014 (unlink-node return
)
1015 (delete-return return
)
1016 (let ((result (return-result return
))
1017 (lvar (if (node-tail-p call
)
1018 (return-result (lambda-return (node-home-lambda call
)))
1020 (call-type (node-derived-type call
)))
1021 (unless (eq call-type
*wild-type
*)
1022 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
1023 (do-uses (use result
)
1024 (derive-node-type use call-type
)))
1025 (substitute-lvar-uses lvar result
1026 (and lvar
(eq (lvar-uses lvar
) call
)))))
1029 ;;; We are converting FUN to be a LET when the call is in a non-tail
1030 ;;; position. Any previously tail calls in FUN are no longer tail
1031 ;;; calls, and must be restored to normal calls which transfer to
1032 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
1033 ;;; the RETURN-RESULT, because the return might have been deleted (if
1034 ;;; all calls were TR.)
1035 (defun unconvert-tail-calls (fun call next-block
)
1036 (do-sset-elements (called (lambda-calls-or-closes fun
))
1037 (when (lambda-p called
)
1038 (dolist (ref (leaf-refs called
))
1039 (let ((this-call (node-dest ref
)))
1040 (when (and this-call
1041 (node-tail-p this-call
)
1042 (not (node-to-be-deleted-p this-call
))
1043 (eq (node-home-lambda this-call
) fun
))
1044 (setf (node-tail-p this-call
) nil
)
1045 (ecase (functional-kind called
)
1046 ((nil :cleanup
:optional
)
1047 (let ((block (node-block this-call
))
1048 (lvar (node-lvar call
)))
1049 (unlink-blocks block
(first (block-succ block
)))
1050 (link-blocks block next-block
)
1051 (if (eq (node-derived-type this-call
) *empty-type
*)
1052 (maybe-terminate-block this-call nil
)
1053 (add-lvar-use this-call lvar
))))
1055 ;; The called function might be an assignment in the
1056 ;; case where we are currently converting that function.
1057 ;; In steady-state, assignments never appear as a called
1060 (aver (eq called fun
)))))))))
1063 ;;; Deal with returning from a LET or assignment that we are
1064 ;;; converting. FUN is the function we are calling, CALL is a call to
1065 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
1066 ;;; NULL if call is a tail call.
1068 ;;; If the call is not a tail call, then we must do
1069 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
1070 ;;; its value out of the enclosing non-let function. When call is
1071 ;;; non-TR, we must convert it back to an ordinary local call, since
1072 ;;; the value must be delivered to the receiver of CALL's value.
1074 ;;; We do different things depending on whether the caller and callee
1075 ;;; have returns left:
1077 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
1078 ;;; Either the function doesn't return, or all returns are via
1079 ;;; tail-recursive local calls.
1080 ;;; -- If CALL is a non-tail call, or if both have returns, then
1081 ;;; we delete the callee's return, move its uses to the call's
1082 ;;; result lvar, and transfer control to the appropriate
1084 ;;; -- If the callee has a return, but the caller doesn't, then we
1085 ;;; move the return to the caller.
1086 (defun move-return-stuff (fun call next-block
)
1087 (declare (type clambda fun
) (type basic-combination call
)
1088 (type (or cblock null
) next-block
))
1090 (unconvert-tail-calls fun call next-block
))
1091 (let* ((return (lambda-return fun
))
1092 (call-fun (node-home-lambda call
))
1093 (call-return (lambda-return call-fun
)))
1094 (when (and call-return
1095 (block-delete-p (node-block call-return
)))
1096 (flush-dest (return-result call-return
))
1097 (delete-return call-return
)
1098 ;; A new return will be put into that lambda, don't want
1099 ;; DELETE-RETURN called by DELETE-BLOCK to delete the new return
1101 ;; (Previously, UNLINK-NODE was called on the return, but it
1102 ;; doesn't work well on deleted blocks)
1103 (setf (return-lambda call-return
) nil
1105 (cond ((not return
))
1106 ((or next-block call-return
)
1107 (unless (block-delete-p (node-block return
))
1109 (ensure-block-start (node-prev call-return
))
1110 (setq next-block
(node-block call-return
)))
1111 (move-return-uses fun call next-block
)))
1113 (aver (node-tail-p call
))
1114 (setf (lambda-return call-fun
) return
)
1115 (setf (return-lambda return
) call-fun
)
1116 (setf (lambda-return fun
) nil
))))
1117 (delete-lvar-use call
) ; LET call does not have value semantics
1120 ;;; Actually do LET conversion. We call subfunctions to do most of the
1121 ;;; work. We do REOPTIMIZE-LVAR on the args and CALL's lvar so that
1122 ;;; LET-specific IR1 optimizations get a chance. We blow away any
1123 ;;; entry for the function in *FREE-FUNS* so that nobody will create
1124 ;;; new references to it.
1125 (defun let-convert (fun call
)
1126 (declare (type clambda fun
) (type basic-combination call
))
1127 (let* ((next-block (insert-let-body fun call
))
1128 (next-block (if (node-tail-p call
)
1131 (move-return-stuff fun call next-block
)
1132 (merge-lets fun call
)
1133 (setf (node-tail-p call
) nil
)
1134 ;; If CALL has a derive type NIL, it means that "its return" is
1135 ;; unreachable, but the next BIND is still reachable; in order to
1136 ;; not confuse MAYBE-TERMINATE-BLOCK...
1137 (setf (node-derived-type call
) *wild-type
*)))
1139 ;;; Reoptimize all of CALL's args and its result.
1140 (defun reoptimize-call (call)
1141 (declare (type basic-combination call
))
1142 (dolist (arg (basic-combination-args call
))
1144 (reoptimize-lvar arg
)))
1145 (reoptimize-lvar (node-lvar call
))
1148 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
1150 (defun declarations-suppress-let-conversion-p (clambda)
1151 ;; From the user's point of view, LET-converting something that
1152 ;; has a name is inlining it. (The user can't see what we're doing
1153 ;; with anonymous things, and suppressing inlining
1154 ;; for such things can easily give Python acute indigestion, so
1157 ;; A functional that is already inline-expanded in this componsne definitely
1158 ;; deserves let-conversion -- and in case of main entry points for inline
1159 ;; expanded optional dispatch, the main-etry isn't explicitly marked :INLINE
1160 ;; even if the function really is.
1161 (when (and (leaf-has-source-name-p clambda
)
1162 (not (functional-inline-expanded clambda
)))
1163 ;; ANSI requires that explicit NOTINLINE be respected.
1164 (or (eq (lambda-inlinep clambda
) :notinline
)
1165 ;; If (= LET-CONVERSION 0) we can guess that inlining
1166 ;; generally won't be appreciated, but if the user
1167 ;; specifically requests inlining, that takes precedence over
1168 ;; our general guess.
1169 (and (policy clambda
(= let-conversion
0))
1170 (not (eq (lambda-inlinep clambda
) :inline
))))))
1172 ;;; We also don't convert calls to named functions which appear in the
1173 ;;; initial component, delaying this until optimization. This
1174 ;;; minimizes the likelihood that we will LET-convert a function which
1175 ;;; may have references added due to later local inline expansion.
1176 (defun ok-initial-convert-p (fun)
1177 (not (and (leaf-has-source-name-p fun
)
1178 (or (declarations-suppress-let-conversion-p fun
)
1179 (eq (component-kind (lambda-component fun
))
1182 ;;; ir1opt usually takes care of forwarding let-bound values directly
1183 ;;; to their destination when possible. However, locall analysis
1184 ;;; greatly benefits from that transformation, and is executed in a
1185 ;;; distinct phase from ir1opt. After let-conversion, variables
1186 ;;; bound to functional values are immediately substituted away.
1188 ;;; When called from locall, component is non-nil, and the functionals
1189 ;;; are marked for reanalysis when appropriate.
1190 (defun substitute-let-funargs (call fun component
)
1191 (declare (type combination call
) (type clambda fun
)
1192 (type (or null component
) component
))
1193 (loop for arg in
(combination-args call
)
1194 and var in
(lambda-vars fun
)
1195 ;; only do that in the absence of assignment
1196 when
(and arg
(null (lambda-var-sets var
)))
1198 (binding* ((use (lvar-uses arg
))
1199 (() (ref-p use
) :exit-if-null
)
1200 (leaf (ref-leaf use
))
1201 (done-something nil
))
1202 ;; unlike propagate-let-args, we're only concerned with
1204 (cond ((not (functional-p leaf
)))
1205 ;; if the types match, we can mutate refs to point to
1206 ;; the functional instead of var
1207 ((csubtypep (single-value-type (node-derived-type use
))
1209 (let ((use-component (node-component use
)))
1212 (when (eq (node-component ref
) use-component
)
1213 (setf done-something t
)))
1215 ;; otherwise, we can still play LVAR-level tricks for single
1216 ;; destination variables.
1217 ((and (singleton-p (leaf-refs var
))
1218 ;; Don't substitute single-ref variables on high-debug /
1219 ;; low speed, to improve the debugging experience.
1220 (not (preserve-single-use-debug-var-p call var
)))
1221 (setf done-something t
)
1222 (substitute-single-use-lvar arg var
)))
1223 ;; if we've done something, the functional may now be used in
1224 ;; more analysis-friendly manners. Enqueue it if we're in
1226 (when (and done-something
1228 (member leaf
(component-lambdas component
)))
1229 (pushnew leaf
(component-reanalyze-functionals component
)))))
1232 ;;; This function is called when there is some reason to believe that
1233 ;;; CLAMBDA might be converted into a LET. This is done after local
1234 ;;; call analysis, and also when a reference is deleted. We return
1235 ;;; true if we converted.
1237 ;;; COMPONENT is non-nil during local call analysis. It is used to
1238 ;;; re-enqueue functionals for reanalysis when they have been forwarded
1239 ;;; directly to destination nodes.
1240 (defun maybe-let-convert (clambda &optional component
)
1241 (declare (type clambda clambda
)
1242 (type (or null component
) component
))
1243 (unless (or (declarations-suppress-let-conversion-p clambda
)
1244 (functional-has-external-references-p clambda
))
1245 ;; We only convert to a LET when the function is a normal local
1246 ;; function, has no XEP, and is referenced in exactly one local
1247 ;; call. Conversion is also inhibited if the only reference is in
1248 ;; a block about to be deleted.
1250 ;; These rules limiting LET conversion may seem unnecessarily
1251 ;; restrictive, since there are some cases where we could do the
1252 ;; return with a jump that don't satisfy these requirements. The
1253 ;; reason for doing things this way is that it makes the concept
1254 ;; of a LET much more useful at the level of IR1 semantics. The
1255 ;; :ASSIGNMENT function kind provides another way to optimize
1256 ;; calls to single-return/multiple call functions.
1258 ;; We don't attempt to convert calls to functions that have an
1259 ;; XEP, since we might be embarrassed later when we want to
1260 ;; convert a newly discovered local call. Also, see
1261 ;; OK-INITIAL-CONVERT-P.
1262 (let ((refs (leaf-refs clambda
)))
1265 (memq (functional-kind clambda
) '(nil :assignment
))
1266 (not (functional-entry-fun clambda
)))
1267 (binding* ((ref (first refs
))
1268 (ref-lvar (node-lvar ref
) :exit-if-null
)
1269 (dest (lvar-dest ref-lvar
)))
1270 (when (and (basic-combination-p dest
)
1271 (eq (basic-combination-fun dest
) ref-lvar
)
1272 (eq (basic-combination-kind dest
) :local
)
1273 (not (node-to-be-deleted-p dest
))
1274 (not (block-delete-p (lambda-block clambda
)))
1275 (cond ((ok-initial-convert-p clambda
) t
)
1277 (reoptimize-lvar ref-lvar
)
1279 (when (eq clambda
(node-home-lambda dest
))
1280 (delete-lambda clambda
)
1281 (return-from maybe-let-convert nil
))
1282 (unless (eq (functional-kind clambda
) :assignment
)
1283 (let-convert clambda dest
))
1284 (reoptimize-call dest
)
1285 (setf (functional-kind clambda
)
1286 (if (mv-combination-p dest
) :mv-let
:let
))
1287 (when (combination-p dest
) ; mv-combinations are too hairy
1288 ; for me to handle - PK 2012-05-30
1289 (substitute-let-funargs dest clambda component
))))
1292 ;;;; tail local calls and assignments
1294 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1295 ;;; they definitely won't generate any cleanup code. Currently we
1296 ;;; recognize lexical entry points that are only used locally (if at
1298 (defun only-harmless-cleanups (block1 block2
)
1299 (declare (type cblock block1 block2
))
1300 (or (eq block1 block2
)
1301 (let ((cleanup2 (block-start-cleanup block2
)))
1302 (do-nested-cleanups (cleanup (block-end-lexenv block1
) t
)
1303 (when (eq cleanup cleanup2
)
1305 (case (cleanup-kind cleanup
)
1307 (when (entry-exits (cleanup-mess-up cleanup
))
1309 (t (return nil
)))))))
1311 ;;; If a potentially TR local call really is TR, then convert it to
1312 ;;; jump directly to the called function. We also call
1313 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1314 ;;; tail-convert. The second is the value of M-C-T-A.
1315 (defun maybe-convert-tail-local-call (call)
1316 (declare (type combination call
))
1317 (let ((return (lvar-dest (node-lvar call
)))
1318 (fun (combination-lambda call
)))
1319 (aver (return-p return
))
1320 (when (and (not (node-tail-p call
)) ; otherwise already converted
1321 ;; this is a tail call
1322 (immediately-used-p (return-result return
) call
)
1323 (only-harmless-cleanups (node-block call
)
1324 (node-block return
))
1325 ;; If the call is in an XEP, we might decide to make it
1326 ;; non-tail so that we can use known return inside the
1328 (not (eq (functional-kind (node-home-lambda call
))
1330 (not (block-delete-p (lambda-block fun
))))
1331 (node-ends-block call
)
1332 (let ((block (node-block call
)))
1333 (setf (node-tail-p call
) t
)
1334 (unlink-blocks block
(first (block-succ block
)))
1335 (link-blocks block
(lambda-block fun
))
1336 (delete-lvar-use call
)
1337 (values t
(maybe-convert-to-assignment fun
))))))
1339 ;;; This is called when we believe it might make sense to convert
1340 ;;; CLAMBDA to an assignment. All this function really does is
1341 ;;; determine when a function with more than one call can still be
1342 ;;; combined with the calling function's environment. We can convert
1344 ;;; -- The function is a normal, non-entry function, and
1345 ;;; -- Except for one call, all calls must be tail recursive calls
1346 ;;; in the called function (i.e. are self-recursive tail calls)
1347 ;;; -- OK-INITIAL-CONVERT-P is true.
1349 ;;; There may be one outside call, and it need not be tail-recursive.
1350 ;;; Since all tail local calls have already been converted to direct
1351 ;;; transfers, the only control semantics needed are to splice in the
1352 ;;; body at the non-tail call. If there is no non-tail call, then we
1353 ;;; need only merge the environments. Both cases are handled by
1356 ;;; ### It would actually be possible to allow any number of outside
1357 ;;; calls as long as they all return to the same place (i.e. have the
1358 ;;; same conceptual continuation.) A special case of this would be
1359 ;;; when all of the outside calls are tail recursive.
1360 (defun maybe-convert-to-assignment (clambda)
1361 (declare (type clambda clambda
))
1362 (when (and (not (functional-kind clambda
))
1363 (not (functional-entry-fun clambda
))
1364 (not (functional-has-external-references-p clambda
)))
1365 (let ((outside-non-tail-call nil
)
1367 (when (and (dolist (ref (leaf-refs clambda
) t
)
1368 (let ((dest (node-dest ref
)))
1369 (when (or (not dest
)
1370 (node-to-be-deleted-p ref
)
1371 (node-to-be-deleted-p dest
))
1373 (let ((home (node-home-lambda ref
)))
1374 (unless (eq home clambda
)
1377 (setq outside-call dest
))
1378 (unless (node-tail-p dest
)
1379 (when (or outside-non-tail-call
(eq home clambda
))
1381 (setq outside-non-tail-call dest
)))))
1382 (ok-initial-convert-p clambda
))
1383 (cond (outside-call (setf (functional-kind clambda
) :assignment
)
1384 (let-convert clambda outside-call
)
1385 (when outside-non-tail-call
1386 (reoptimize-call outside-non-tail-call
))
1388 (t (delete-lambda clambda
)