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