rename SB-SIMPLE-STREAMS utility function
[sbcl.git] / src / compiler / locall.lisp
blobd7b3a635503e6b55cef3e9a727fa0116ff9e3f0a
1 ;;;; This file implements local call analysis. A local call is a
2 ;;;; function call between functions being compiled at the same time.
3 ;;;; If we can tell at compile time that such a call is legal, then we
4 ;;;; change the combination to call the correct lambda, mark it as
5 ;;;; local, and add this link to our call graph. Once a call is local,
6 ;;;; it is then eligible for let conversion, which places the body of
7 ;;;; the function inline.
8 ;;;;
9 ;;;; We cannot always do a local call even when we do have the
10 ;;;; function being called. Calls that cannot be shown to have legal
11 ;;;; arg counts are not converted.
13 ;;;; This software is part of the SBCL system. See the README file for
14 ;;;; more information.
15 ;;;;
16 ;;;; This software is derived from the CMU CL system, which was
17 ;;;; written at Carnegie Mellon University and released into the
18 ;;;; public domain. The software is in the public domain and is
19 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
20 ;;;; files for more information.
22 (in-package "SB!C")
24 ;;; This function propagates information from the variables in the
25 ;;; function FUN to the actual arguments in CALL. This is also called
26 ;;; by the VALUES IR1 optimizer when it sleazily converts MV-BINDs to
27 ;;; LETs.
28 ;;;
29 ;;; We flush all arguments to CALL that correspond to unreferenced
30 ;;; variables in FUN. We leave NILs in the COMBINATION-ARGS so that
31 ;;; the remaining args still match up with their vars.
32 ;;;
33 ;;; We also apply the declared variable type assertion to the argument
34 ;;; lvars.
35 (defun propagate-to-args (call fun)
36 (declare (type combination call) (type clambda fun))
37 (loop with policy = (lexenv-policy (node-lexenv call))
38 for args on (basic-combination-args call)
39 and var in (lambda-vars fun)
40 do (assert-lvar-type (car args) (leaf-type var) policy)
41 do (unless (leaf-refs var)
42 (flush-dest (car args))
43 (setf (car args) nil)))
44 (values))
46 (defun recognize-dynamic-extent-lvars (call fun)
47 (declare (type combination call) (type clambda fun))
48 (loop for arg in (basic-combination-args call)
49 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
58 (make-entry)))
59 (cleanup (make-cleanup :kind :dynamic-extent
60 :mess-up entry
61 :info dx-lvars)))
62 (setf (entry-cleanup entry) cleanup)
63 (insert-node-before call entry)
64 (setf (node-lexenv call)
65 (make-lexenv :default (node-lexenv call)
66 :cleanup cleanup))
67 (push entry (lambda-entries (node-home-lambda entry)))
68 (dolist (cell dx-lvars)
69 (setf (lvar-dynamic-extent (cdr cell)) cleanup)))))
70 (values))
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.
79 ;;;
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
84 ;;; tail set type.
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)))
93 (dolist (fun funs)
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))
98 t)))))
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
111 ;;; tail-recursive.
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))
118 (when arg
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)
124 (values))
126 ;;;; external entry point creation
128 ;;; Return a LAMBDA form that can be used as the definition of the XEP
129 ;;; for FUN.
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
137 ;;; arguments.
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))
156 (etypecase fun
157 (clambda
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))
166 (%funcall ,fun ,@temps))))
167 (optional-dispatch
168 (let* ((min (optional-dispatch-min-args fun))
169 (max (optional-dispatch-max-args fun))
170 (more (optional-dispatch-more-entry fun))
171 (n-supplied (gensym))
172 (temps (make-gensym-list max)))
173 (collect ((entries))
174 ;; Force convertion of all entries
175 (optional-dispatch-entry-point-fun fun 0)
176 (loop for ep in (optional-dispatch-entry-points fun)
177 and n from min
178 do (entries `((eql ,n-supplied ,n)
179 (%funcall ,(force ep) ,@(subseq temps 0 n)))))
180 `(lambda (,n-supplied ,@temps)
181 (declare (type index ,n-supplied))
182 (cond
183 ,@(if more (butlast (entries)) (entries))
184 ,@(when more
185 ;; KLUDGE: (NOT (< ...)) instead of >= avoids one round of
186 ;; deftransforms and lambda-conversion.
187 `((,(if (zerop min) t `(not (< ,n-supplied ,max)))
188 ,(with-unique-names (n-context n-count)
189 `(multiple-value-bind (,n-context ,n-count)
190 (%more-arg-context ,n-supplied ,max)
191 (%funcall ,more ,@temps ,n-context ,n-count))))))
193 (%arg-count-error ,n-supplied)))))))))
195 ;;; Make an external entry point (XEP) for FUN and return it. We
196 ;;; convert the result of MAKE-XEP-LAMBDA in the correct environment,
197 ;;; then associate this lambda with FUN as its XEP. After the
198 ;;; conversion, we iterate over the function's associated lambdas,
199 ;;; redoing local call analysis so that the XEP calls will get
200 ;;; converted.
202 ;;; We set REANALYZE and REOPTIMIZE in the component, just in case we
203 ;;; discover an XEP after the initial local call analyze pass.
204 (defun make-xep (fun)
205 (declare (type functional fun))
206 (aver (null (functional-entry-fun fun)))
207 (with-ir1-environment-from-node (lambda-bind (main-entry fun))
208 (let ((xep (ir1-convert-lambda (make-xep-lambda-expression fun)
209 :debug-name (debug-name
210 'xep (leaf-debug-name fun))
211 :system-lambda t)))
212 (setf (functional-kind xep) :external
213 (leaf-ever-used xep) t
214 (functional-entry-fun xep) fun
215 (functional-entry-fun fun) xep
216 (component-reanalyze *current-component*) t)
217 (reoptimize-component *current-component* :maybe)
218 (locall-analyze-xep-entry-point fun)
219 xep)))
221 (defun locall-analyze-xep-entry-point (fun)
222 (declare (type functional fun))
223 (etypecase fun
224 (clambda
225 (locall-analyze-fun-1 fun))
226 (optional-dispatch
227 (dolist (ep (optional-dispatch-entry-points fun))
228 (locall-analyze-fun-1 (force ep)))
229 (when (optional-dispatch-more-entry fun)
230 (locall-analyze-fun-1 (optional-dispatch-more-entry fun))))))
232 ;;; Notice a REF that is not in a local-call context. If the REF is
233 ;;; already to an XEP, then do nothing, otherwise change it to the
234 ;;; XEP, making an XEP if necessary.
236 ;;; If REF is to a special :CLEANUP or :ESCAPE function, then we treat
237 ;;; it as though it was not an XEP reference (i.e. leave it alone).
238 (defun reference-entry-point (ref)
239 (declare (type ref ref))
240 (let ((fun (ref-leaf ref)))
241 (unless (or (xep-p fun)
242 (member (functional-kind fun) '(:escape :cleanup)))
243 (change-ref-leaf ref (or (functional-entry-fun fun)
244 (make-xep fun))))))
246 ;;; Attempt to convert all references to FUN to local calls. The
247 ;;; reference must be the function for a call, and the function lvar
248 ;;; must be used only once, since otherwise we cannot be sure what
249 ;;; function is to be called. The call lvar would be multiply used if
250 ;;; there is hairy stuff such as conditionals in the expression that
251 ;;; computes the function.
253 ;;; If we cannot convert a reference, then we mark the referenced
254 ;;; function as an entry-point, creating a new XEP if necessary. We
255 ;;; don't try to convert calls that are in error (:ERROR kind.)
257 ;;; This is broken off from LOCALL-ANALYZE-COMPONENT so that people
258 ;;; can force analysis of newly introduced calls. Note that we don't
259 ;;; do LET conversion here.
260 (defun locall-analyze-fun-1 (fun)
261 (declare (type functional fun))
262 (let ((refs (leaf-refs fun))
263 (local-p t))
264 (dolist (ref refs)
265 (let* ((lvar (node-lvar ref))
266 (dest (when lvar (lvar-dest lvar))))
267 (unless (node-to-be-deleted-p ref)
268 (cond ((and (basic-combination-p dest)
269 (eq (basic-combination-fun dest) lvar)
270 (eq (lvar-uses lvar) ref))
272 (convert-call-if-possible ref dest)
274 (unless (eq (basic-combination-kind dest) :local)
275 (reference-entry-point ref)
276 (setq local-p nil)))
278 (reference-entry-point ref)
279 (setq local-p nil))))))
280 (when local-p (note-local-functional fun)))
282 (values))
284 ;;; We examine all NEW-FUNCTIONALS in COMPONENT, attempting to convert
285 ;;; calls into local calls when it is legal. We also attempt to
286 ;;; convert each LAMBDA to a LET. LET conversion is also triggered by
287 ;;; deletion of a function reference, but functions that start out
288 ;;; eligible for conversion must be noticed sometime.
290 ;;; Note that there is a lot of action going on behind the scenes
291 ;;; here, triggered by reference deletion. In particular, the
292 ;;; COMPONENT-LAMBDAS are being hacked to remove newly deleted and LET
293 ;;; converted LAMBDAs, so it is important that the LAMBDA is added to
294 ;;; the COMPONENT-LAMBDAS when it is. Also, the
295 ;;; COMPONENT-NEW-FUNCTIONALS may contain all sorts of drivel, since
296 ;;; it is not updated when we delete functions, etc. Only
297 ;;; COMPONENT-LAMBDAS is updated.
299 ;;; COMPONENT-REANALYZE-FUNCTIONALS is treated similarly to
300 ;;; COMPONENT-NEW-FUNCTIONALS, but we don't add lambdas to the
301 ;;; LAMBDAS.
302 (defun locall-analyze-component (component)
303 (declare (type component component))
304 (aver-live-component component)
305 (loop
306 (let* ((new-functional (pop (component-new-functionals component)))
307 (functional (or new-functional
308 (pop (component-reanalyze-functionals component)))))
309 (unless functional
310 (return))
311 (let ((kind (functional-kind functional)))
312 (cond ((or (functional-somewhat-letlike-p functional)
313 (memq kind '(:deleted :zombie)))
314 (values)) ; nothing to do
315 ((and (null (leaf-refs functional)) (eq kind nil)
316 (not (functional-entry-fun functional)))
317 (delete-functional functional))
319 ;; Fix/check FUNCTIONAL's relationship to COMPONENT-LAMDBAS.
320 (cond ((not (lambda-p functional))
321 ;; Since FUNCTIONAL isn't a LAMBDA, this doesn't
322 ;; apply: no-op.
323 (values))
324 (new-functional ; FUNCTIONAL came from
325 ; NEW-FUNCTIONALS, hence is new.
326 ;; FUNCTIONAL becomes part of COMPONENT-LAMBDAS now.
327 (aver (not (member functional
328 (component-lambdas component))))
329 (push functional (component-lambdas component)))
330 (t ; FUNCTIONAL is old.
331 ;; FUNCTIONAL should be in COMPONENT-LAMBDAS already.
332 (aver (member functional (component-lambdas
333 component)))))
334 (locall-analyze-fun-1 functional)
335 (when (lambda-p functional)
336 (maybe-let-convert functional component)))))))
337 (values))
339 (defun locall-analyze-clambdas-until-done (clambdas)
340 (loop
341 (let ((did-something nil))
342 (dolist (clambda clambdas)
343 (let ((component (lambda-component clambda)))
344 ;; The original CMU CL code seemed to implicitly assume that
345 ;; COMPONENT is the only one here. Let's make that explicit.
346 (aver (= 1 (length (functional-components clambda))))
347 (aver (eql component (first (functional-components clambda))))
348 (when (or (component-new-functionals component)
349 (component-reanalyze-functionals component))
350 (setf did-something t)
351 (locall-analyze-component component))))
352 (unless did-something
353 (return))))
354 (values))
356 ;;; If policy is auspicious and CALL is not in an XEP and we don't seem
357 ;;; to be in an infinite recursive loop, then change the reference to
358 ;;; reference a fresh copy. We return whichever function we decide to
359 ;;; reference.
360 (defun maybe-expand-local-inline (original-functional ref call)
361 (if (and (policy call
362 (and (>= speed space)
363 (>= speed compilation-speed)))
364 (not (eq (functional-kind (node-home-lambda call)) :external))
365 (inline-expansion-ok call))
366 (let* ((end (component-last-block (node-component call)))
367 (pred (block-prev end)))
368 (multiple-value-bind (losing-local-object converted-lambda)
369 (catch 'locall-already-let-converted
370 (with-ir1-environment-from-node call
371 (let ((*lexenv* (functional-lexenv original-functional)))
372 (values nil
373 (ir1-convert-lambda
374 (functional-inline-expansion original-functional)
375 :debug-name (debug-name 'local-inline
376 (leaf-debug-name
377 original-functional)))))))
378 (cond (losing-local-object
379 (if (functional-p losing-local-object)
380 (let ((*compiler-error-context* call))
381 (compiler-notify "couldn't inline expand because expansion ~
382 calls this LET-converted local function:~
383 ~% ~S"
384 (leaf-debug-name losing-local-object)))
385 (let ((*compiler-error-context* call))
386 (compiler-notify "implementation limitation: couldn't inline ~
387 expand because expansion refers to ~
388 the optimized away object ~S."
389 losing-local-object)))
390 (loop for block = (block-next pred) then (block-next block)
391 until (eq block end)
392 do (setf (block-delete-p block) t))
393 (loop for block = (block-next pred) then (block-next block)
394 until (eq block end)
395 do (delete-block block t))
396 original-functional)
398 (change-ref-leaf ref converted-lambda)
399 converted-lambda))))
400 original-functional))
402 ;;; Dispatch to the appropriate function to attempt to convert a call.
403 ;;; REF must be a reference to a FUNCTIONAL. This is called in IR1
404 ;;; optimization as well as in local call analysis. If the call is is
405 ;;; already :LOCAL, we do nothing. If the call is already scheduled
406 ;;; for deletion, also do nothing (in addition to saving time, this
407 ;;; also avoids some problems with optimizing collections of functions
408 ;;; that are partially deleted.)
410 ;;; This is called both before and after FIND-INITIAL-DFO runs. When
411 ;;; called on a :INITIAL component, we don't care whether the caller
412 ;;; and callee are in the same component. Afterward, we must stick
413 ;;; with whatever component division we have chosen.
415 ;;; Before attempting to convert a call, we see whether the function
416 ;;; is supposed to be inline expanded. Call conversion proceeds as
417 ;;; before after any expansion.
419 ;;; We bind *COMPILER-ERROR-CONTEXT* to the node for the call so that
420 ;;; warnings will get the right context.
421 (defun convert-call-if-possible (ref call)
422 (declare (type ref ref) (type basic-combination call))
423 (let* ((block (node-block call))
424 (component (block-component block))
425 (original-fun (ref-leaf ref)))
426 (aver (functional-p original-fun))
427 (unless (or (member (basic-combination-kind call) '(:local :error))
428 (node-to-be-deleted-p call)
429 (member (functional-kind original-fun)
430 '(:toplevel-xep :deleted))
431 (not (or (eq (component-kind component) :initial)
432 (eq (block-component
433 (node-block
434 (lambda-bind (main-entry original-fun))))
435 component))))
436 (let ((fun (if (xep-p original-fun)
437 (functional-entry-fun original-fun)
438 original-fun))
439 (*compiler-error-context* call))
441 (when (and (eq (functional-inlinep fun) :inline)
442 (rest (leaf-refs original-fun)))
443 (setq fun (maybe-expand-local-inline fun ref call)))
445 (aver (member (functional-kind fun)
446 '(nil :escape :cleanup :optional)))
447 (cond ((mv-combination-p call)
448 (convert-mv-call ref call fun))
449 ((lambda-p fun)
450 (convert-lambda-call ref call fun))
452 (convert-hairy-call ref call fun))))))
454 (values))
456 ;;; Attempt to convert a multiple-value call. The only interesting
457 ;;; case is a call to a function that LOOKS-LIKE-AN-MV-BIND, has
458 ;;; exactly one reference and no XEP, and is called with one values
459 ;;; lvar.
461 ;;; We change the call to be to the last optional entry point and
462 ;;; change the call to be local. Due to our preconditions, the call
463 ;;; should eventually be converted to a let, but we can't do that now,
464 ;;; since there may be stray references to the e-p lambda due to
465 ;;; optional defaulting code.
467 ;;; We also use variable types for the called function to construct an
468 ;;; assertion for the values lvar.
470 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
471 (defun convert-mv-call (ref call fun)
472 (declare (type ref ref) (type mv-combination call) (type functional fun))
473 (when (and (looks-like-an-mv-bind fun)
474 (singleton-p (leaf-refs fun))
475 (singleton-p (basic-combination-args call))
476 (not (functional-entry-fun fun)))
477 (let* ((*current-component* (node-component ref))
478 (ep (optional-dispatch-entry-point-fun
479 fun (optional-dispatch-max-args fun))))
480 (when (null (leaf-refs ep))
481 (aver (= (optional-dispatch-min-args fun) 0))
482 (setf (basic-combination-kind call) :local)
483 (sset-adjoin ep (lambda-calls-or-closes (node-home-lambda call)))
484 (merge-tail-sets call ep)
485 (change-ref-leaf ref ep)
487 (assert-lvar-type
488 (first (basic-combination-args call))
489 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
490 (lexenv-policy (node-lexenv call))))))
491 (values))
493 ;;; Convenience function to mark local calls as known bad.
494 (defun transform-call-with-ir1-environment (node lambda default-name)
495 (aver (combination-p node))
496 (with-ir1-environment-from-node node
497 (transform-call node lambda
498 (or (combination-fun-source-name node nil)
499 default-name))))
501 (defun warn-invalid-local-call (node count &rest warn-arguments)
502 (aver (combination-p node))
503 (aver (typep count 'unsigned-byte))
504 (apply 'warn warn-arguments)
505 (transform-call-with-ir1-environment node
506 `(lambda (&rest args)
507 (declare (ignore args))
508 (%arg-count-error ,count))
509 '%arg-count-error))
511 ;;; Attempt to convert a call to a lambda. If the number of args is
512 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
513 ;;; from future consideration. If the argcount is O.K. then we just
514 ;;; convert it.
515 (defun convert-lambda-call (ref call fun)
516 (declare (type ref ref) (type combination call) (type clambda fun))
517 (let ((nargs (length (lambda-vars fun)))
518 (n-call-args (length (combination-args call))))
519 (cond ((= n-call-args nargs)
520 (convert-call ref call fun))
522 (warn-invalid-local-call call n-call-args
523 'local-argument-mismatch
524 :format-control
525 "function called with ~R argument~:P, but wants exactly ~R"
526 :format-arguments (list n-call-args nargs))))))
528 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
530 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
531 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
532 ;;; a call to the correct entry point. If &KEY args are supplied, then
533 ;;; dispatch to a subfunction. We don't convert calls to functions
534 ;;; that have a &MORE (or &REST) arg.
535 (defun convert-hairy-call (ref call fun)
536 (declare (type ref ref) (type combination call)
537 (type optional-dispatch fun))
538 (let ((min-args (optional-dispatch-min-args fun))
539 (max-args (optional-dispatch-max-args fun))
540 (call-args (length (combination-args call))))
541 (cond ((< call-args min-args)
542 (warn-invalid-local-call call call-args
543 'local-argument-mismatch
544 :format-control
545 "function called with ~R argument~:P, but wants at least ~R"
546 :format-arguments (list call-args min-args)))
547 ((<= call-args max-args)
548 (convert-call ref call
549 (let ((*current-component* (node-component ref)))
550 (optional-dispatch-entry-point-fun
551 fun (- call-args min-args)))))
552 ((optional-dispatch-more-entry fun)
553 (convert-more-call ref call fun))
555 (warn-invalid-local-call call call-args
556 'local-argument-mismatch
557 :format-control
558 "function called with ~R argument~:P, but wants at most ~R"
559 :format-arguments
560 (list call-args max-args)))))
561 (values))
563 ;;; This function is used to convert a call to an entry point when
564 ;;; complex transformations need to be done on the original arguments.
565 ;;; ENTRY is the entry point function that we are calling. VARS is a
566 ;;; list of variable names which are bound to the original call
567 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
568 ;;; is the list of arguments to the entry point function.
570 ;;; In order to avoid gruesome graph grovelling, we introduce a new
571 ;;; function that rearranges the arguments and calls the entry point.
572 ;;; We analyze the new function and the entry point immediately so
573 ;;; that everything gets converted during the single pass.
574 (defun convert-hairy-fun-entry (ref call entry vars ignores args indef)
575 (declare (list vars ignores args) (type ref ref) (type combination call)
576 (type clambda entry))
577 (let ((new-fun
578 (with-ir1-environment-from-node call
579 (ir1-convert-lambda
580 `(lambda ,vars
581 (declare (ignorable ,@ignores)
582 (indefinite-extent ,@indef))
583 (%funcall ,entry ,@args))
584 :debug-name (debug-name 'hairy-function-entry
585 (lvar-fun-debug-name
586 (basic-combination-fun call)))
587 :system-lambda t))))
588 (convert-call ref call new-fun)
589 (dolist (ref (leaf-refs entry))
590 (convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
592 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
593 ;;; function into a local call to the MAIN-ENTRY.
595 ;;; First we verify that all keywords are constant and legal. If there
596 ;;; aren't, then we warn the user and don't attempt to convert the call.
598 ;;; We massage the supplied &KEY arguments into the order expected
599 ;;; by the main entry. This is done by binding all the arguments to
600 ;;; the keyword call to variables in the introduced lambda, then
601 ;;; passing these values variables in the correct order when calling
602 ;;; the main entry. Unused arguments (such as the keywords themselves)
603 ;;; are discarded simply by not passing them along.
605 ;;; If there is a &REST arg, then we bundle up the args and pass them
606 ;;; to LIST.
607 (defun convert-more-call (ref call fun)
608 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
609 (let* ((max (optional-dispatch-max-args fun))
610 (arglist (optional-dispatch-arglist fun))
611 (args (combination-args call))
612 (more (nthcdr max args))
613 (flame (policy call (or (> speed inhibit-warnings)
614 (> space inhibit-warnings))))
615 (loser nil)
616 (allowp nil)
617 (allow-found nil)
618 (temps (make-gensym-list max))
619 (more-temps (make-gensym-list (length more))))
620 (collect ((ignores)
621 (supplied)
622 (key-vars))
624 (dolist (var arglist)
625 (let ((info (lambda-var-arg-info var)))
626 (when info
627 (ecase (arg-info-kind info)
628 (:keyword
629 (key-vars var))
630 ((:rest :optional))
631 ((:more-context :more-count)
632 (compiler-warn "can't local-call functions with &MORE args")
633 (setf (basic-combination-kind call) :error)
634 (return-from convert-more-call))))))
636 (when (optional-dispatch-keyp fun)
637 (when (oddp (length more))
638 (compiler-warn "function called with odd number of ~
639 arguments in keyword portion")
640 (transform-call-with-ir1-environment
641 call
642 `(lambda (&rest args)
643 (declare (ignore args))
644 (%odd-key-args-error))
645 '%odd-key-args-error)
646 (return-from convert-more-call))
648 (do ((key more (cddr key))
649 (temp more-temps (cddr temp)))
650 ((null key))
651 (let ((lvar (first key)))
652 (unless (constant-lvar-p lvar)
653 (when flame
654 (compiler-notify "non-constant keyword in keyword call"))
655 (setf (basic-combination-kind call) :error)
656 (return-from convert-more-call))
658 (let ((name (lvar-value lvar))
659 (dummy (first temp))
660 (val (second temp)))
661 (when (and (eq name :allow-other-keys) (not allow-found))
662 (let ((val (second key)))
663 (cond ((constant-lvar-p val)
664 (setq allow-found t
665 allowp (lvar-value val)))
666 (t (when flame
667 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
668 (setf (basic-combination-kind call) :error)
669 (return-from convert-more-call)))))
670 (dolist (var (key-vars)
671 (progn
672 (ignores dummy val)
673 (unless (eq name :allow-other-keys)
674 (setq loser (list name)))))
675 (let ((info (lambda-var-arg-info var)))
676 (when (eq (arg-info-key info) name)
677 (ignores dummy)
678 (if (member var (supplied) :key #'car)
679 (ignores val)
680 (supplied (cons var val)))
681 (return)))))))
683 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
684 (compiler-warn "function called with unknown argument keyword ~S"
685 (car loser))
686 (transform-call-with-ir1-environment
687 call
688 `(lambda (&rest args)
689 (declare (ignore args))
690 (%unknown-key-arg-error ',(car loser)))
691 '%unknown-key-arg-error)
692 (return-from convert-more-call)))
694 (collect ((call-args))
695 (do ((var arglist (cdr var))
696 (temp temps (cdr temp)))
697 ((null var))
698 (let ((info (lambda-var-arg-info (car var))))
699 (if info
700 (ecase (arg-info-kind info)
701 (:optional
702 (call-args (car temp))
703 (when (arg-info-supplied-p info)
704 (call-args t)))
705 (:rest
706 (call-args `(list ,@more-temps))
707 ;; &REST arguments may be accompanied by extra
708 ;; context and count arguments. We know this by
709 ;; the ARG-INFO-DEFAULT. Supply 0 and 0 or
710 ;; don't convert at all depending.
711 (let ((more (arg-info-default info)))
712 (when more
713 (unless (eq t more)
714 (destructuring-bind (context count &optional used) more
715 (declare (ignore context count))
716 (when used
717 ;; We've already converted to use the more context
718 ;; instead of the rest list.
719 (return-from convert-more-call))))
720 (call-args 0)
721 (call-args 0)
722 (setf (arg-info-default info) t)))
723 (return))
724 (:keyword
725 (return)))
726 (call-args (car temp)))))
728 (dolist (var (key-vars))
729 (let ((info (lambda-var-arg-info var))
730 (temp (cdr (assoc var (supplied)))))
731 (if temp
732 (call-args temp)
733 (call-args (arg-info-default info)))
734 (when (arg-info-supplied-p info)
735 (call-args (not (null temp))))))
737 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
738 (append temps more-temps)
739 (ignores) (call-args)
740 (when (optional-rest-p fun)
741 more-temps)))))
743 (values))
745 ;;;; LET conversion
746 ;;;;
747 ;;;; Converting to a LET has differing significance to various parts
748 ;;;; of the compiler:
749 ;;;; -- The body of a LET is spliced in immediately after the
750 ;;;; corresponding combination node, making the control transfer
751 ;;;; explicit and allowing LETs to be mashed together into a single
752 ;;;; block. The value of the LET is delivered directly to the
753 ;;;; original lvar for the call, eliminating the need to
754 ;;;; propagate information from the dummy result lvar.
755 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
756 ;;;; that there is only one expression that the variable can be bound
757 ;;;; to, and this is easily substituted for.
758 ;;;; -- LETs are interesting to environment analysis and to the back
759 ;;;; end because in most ways a LET can be considered to be "the
760 ;;;; same function" as its home function.
761 ;;;; -- LET conversion has dynamic scope implications, since control
762 ;;;; transfers within the same environment are local. In a local
763 ;;;; control transfer, cleanup code must be emitted to remove
764 ;;;; dynamic bindings that are no longer in effect.
766 ;;; Set up the control transfer to the called CLAMBDA. We split the
767 ;;; call block immediately after the call, and link the head of
768 ;;; CLAMBDA to the call block. The successor block after splitting
769 ;;; (where we return to) is returned.
771 ;;; If the lambda is is a different component than the call, then we
772 ;;; call JOIN-COMPONENTS. This only happens in block compilation
773 ;;; before FIND-INITIAL-DFO.
774 (defun insert-let-body (clambda call)
775 (declare (type clambda clambda) (type basic-combination call))
776 (let* ((call-block (node-block call))
777 (bind-block (node-block (lambda-bind clambda)))
778 (component (block-component call-block)))
779 (aver-live-component component)
780 (let ((clambda-component (block-component bind-block)))
781 (unless (eq clambda-component component)
782 (aver (eq (component-kind component) :initial))
783 (join-components component clambda-component)))
784 (let ((*current-component* component))
785 (node-ends-block call))
786 (destructuring-bind (next-block)
787 (block-succ call-block)
788 (unlink-blocks call-block next-block)
789 (link-blocks call-block bind-block)
790 next-block)))
792 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
793 ;;; same set as; but leave CLAMBDA with a valid tail set value of
794 ;;; its own, for the benefit of code which might try to pull
795 ;;; something out of it (e.g. return type).
796 (defun depart-from-tail-set (clambda)
797 ;; Until sbcl-0.pre7.37.flaky5.2, we did
798 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
799 ;; (SETF (TAIL-SET-FUNS TAILS)
800 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
801 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
802 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
803 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
804 ;; inconsistent, and perhaps unsafe, for us to think we're in the
805 ;; tail set. Unfortunately..
807 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
808 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
809 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
810 ;; which you called in order to get the address of an external LAMBDA):
811 ;; the external function was defined in terms of internal function,
812 ;; which was LET-converted, and then things blew up downstream when
813 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
814 ;; the now-NILed-out TAIL-SET. So..
816 ;; To deal with this problem, we no longer NIL out
817 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
818 ;; * If we're the only function in TAIL-SET-FUNS, it should
819 ;; be safe to leave ourself linked to it, and it to you.
820 ;; * If there are other functions in TAIL-SET-FUNS, then we're
821 ;; afraid of future optimizations on those functions causing
822 ;; the TAIL-SET object no longer to be valid to describe our
823 ;; return value. Thus, we delete ourselves from that object;
824 ;; but we save a newly-allocated tail-set, derived from the old
825 ;; one, for ourselves, for the use of later code (e.g.
826 ;; FINALIZE-XEP-DEFINITION) which might want to
827 ;; know about our return type.
828 (let* ((old-tail-set (lambda-tail-set clambda))
829 (old-tail-set-funs (tail-set-funs old-tail-set)))
830 (unless (= 1 (length old-tail-set-funs))
831 (setf (tail-set-funs old-tail-set)
832 (delete clambda old-tail-set-funs))
833 (let ((new-tail-set (copy-tail-set old-tail-set)))
834 (setf (lambda-tail-set clambda) new-tail-set
835 (tail-set-funs new-tail-set) (list clambda)))))
836 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
837 ;; remain valid in this case, so we nuke it on the theory that
838 ;; missing information tends to be less dangerous than incorrect
839 ;; information.
840 (setf (tail-set-info (lambda-tail-set clambda)) nil))
842 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
843 ;;; its LETs to LETs for the CALL's home function. We merge the calls
844 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
845 ;;; in the process. We also merge the ENTRIES.
847 ;;; We also unlink the function head from the component head and set
848 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
849 ;;; recomputed.
850 (defun merge-lets (clambda call)
852 (declare (type clambda clambda) (type basic-combination call))
854 (let ((component (node-component call)))
855 (unlink-blocks (component-head component) (lambda-block clambda))
856 (setf (component-lambdas component)
857 (delete clambda (component-lambdas component)))
858 (setf (component-reanalyze component) t))
859 (setf (lambda-call-lexenv clambda) (node-lexenv call))
861 (depart-from-tail-set clambda)
863 (let* ((home (node-home-lambda call))
864 (home-physenv (lambda-physenv home))
865 (physenv (lambda-physenv clambda)))
867 (aver (not (eq home clambda)))
869 ;; CLAMBDA belongs to HOME now.
870 (push clambda (lambda-lets home))
871 (setf (lambda-home clambda) home)
872 (setf (lambda-physenv clambda) home-physenv)
874 (when physenv
875 (unless home-physenv
876 (setf home-physenv (get-lambda-physenv home)))
877 (setf (physenv-nlx-info home-physenv)
878 (nconc (physenv-nlx-info physenv)
879 (physenv-nlx-info home-physenv))))
881 ;; All of CLAMBDA's LETs belong to HOME now.
882 (let ((lets (lambda-lets clambda)))
883 (dolist (let lets)
884 (setf (lambda-home let) home)
885 (setf (lambda-physenv let) home-physenv))
886 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
887 ;; CLAMBDA no longer has an independent existence as an entity
888 ;; which has LETs.
889 (setf (lambda-lets clambda) nil)
891 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
892 ;; DFO dependencies.
893 (sset-union (lambda-calls-or-closes home)
894 (lambda-calls-or-closes clambda))
895 (sset-delete clambda (lambda-calls-or-closes home))
896 ;; CLAMBDA no longer has an independent existence as an entity
897 ;; which calls things or has DFO dependencies.
898 (setf (lambda-calls-or-closes clambda) nil)
900 ;; All of CLAMBDA's ENTRIES belong to HOME now.
901 (setf (lambda-entries home)
902 (nconc (lambda-entries clambda)
903 (lambda-entries home)))
904 ;; CLAMBDA no longer has an independent existence as an entity
905 ;; with ENTRIES.
906 (setf (lambda-entries clambda) nil))
908 (values))
910 ;;; Handle the value semantics of LET conversion. Delete FUN's return
911 ;;; node, and change the control flow to transfer to NEXT-BLOCK
912 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
913 (defun move-return-uses (fun call next-block)
914 (declare (type clambda fun) (type basic-combination call)
915 (type cblock next-block))
916 (let* ((return (lambda-return fun))
917 (return-block (progn
918 (ensure-block-start (node-prev return))
919 (node-block return))))
920 (unlink-blocks return-block
921 (component-tail (block-component return-block)))
922 (link-blocks return-block next-block)
923 (unlink-node return)
924 (delete-return return)
925 (let ((result (return-result return))
926 (lvar (if (node-tail-p call)
927 (return-result (lambda-return (node-home-lambda call)))
928 (node-lvar call)))
929 (call-type (node-derived-type call)))
930 (unless (eq call-type *wild-type*)
931 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
932 (do-uses (use result)
933 (derive-node-type use call-type)))
934 (substitute-lvar-uses lvar result
935 (and lvar (eq (lvar-uses lvar) call)))))
936 (values))
938 ;;; We are converting FUN to be a LET when the call is in a non-tail
939 ;;; position. Any previously tail calls in FUN are no longer tail
940 ;;; calls, and must be restored to normal calls which transfer to
941 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
942 ;;; the RETURN-RESULT, because the return might have been deleted (if
943 ;;; all calls were TR.)
944 (defun unconvert-tail-calls (fun call next-block)
945 (do-sset-elements (called (lambda-calls-or-closes fun))
946 (when (lambda-p called)
947 (dolist (ref (leaf-refs called))
948 (let ((this-call (node-dest ref)))
949 (when (and this-call
950 (node-tail-p this-call)
951 (eq (node-home-lambda this-call) fun))
952 (setf (node-tail-p this-call) nil)
953 (ecase (functional-kind called)
954 ((nil :cleanup :optional)
955 (let ((block (node-block this-call))
956 (lvar (node-lvar call)))
957 (unlink-blocks block (first (block-succ block)))
958 (link-blocks block next-block)
959 (aver (not (node-lvar this-call)))
960 (add-lvar-use this-call lvar)))
961 (:deleted)
962 ;; The called function might be an assignment in the
963 ;; case where we are currently converting that function.
964 ;; In steady-state, assignments never appear as a called
965 ;; function.
966 (:assignment
967 (aver (eq called fun)))))))))
968 (values))
970 ;;; Deal with returning from a LET or assignment that we are
971 ;;; converting. FUN is the function we are calling, CALL is a call to
972 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
973 ;;; NULL if call is a tail call.
975 ;;; If the call is not a tail call, then we must do
976 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
977 ;;; its value out of the enclosing non-let function. When call is
978 ;;; non-TR, we must convert it back to an ordinary local call, since
979 ;;; the value must be delivered to the receiver of CALL's value.
981 ;;; We do different things depending on whether the caller and callee
982 ;;; have returns left:
984 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
985 ;;; Either the function doesn't return, or all returns are via
986 ;;; tail-recursive local calls.
987 ;;; -- If CALL is a non-tail call, or if both have returns, then
988 ;;; we delete the callee's return, move its uses to the call's
989 ;;; result lvar, and transfer control to the appropriate
990 ;;; return point.
991 ;;; -- If the callee has a return, but the caller doesn't, then we
992 ;;; move the return to the caller.
993 (defun move-return-stuff (fun call next-block)
994 (declare (type clambda fun) (type basic-combination call)
995 (type (or cblock null) next-block))
996 (when next-block
997 (unconvert-tail-calls fun call next-block))
998 (let* ((return (lambda-return fun))
999 (call-fun (node-home-lambda call))
1000 (call-return (lambda-return call-fun)))
1001 (when (and call-return
1002 (block-delete-p (node-block call-return)))
1003 (delete-return call-return)
1004 (unlink-node call-return)
1005 (setq call-return nil))
1006 (cond ((not return))
1007 ((or next-block call-return)
1008 (unless (block-delete-p (node-block return))
1009 (unless next-block
1010 (ensure-block-start (node-prev call-return))
1011 (setq next-block (node-block call-return)))
1012 (move-return-uses fun call next-block)))
1014 (aver (node-tail-p call))
1015 (setf (lambda-return call-fun) return)
1016 (setf (return-lambda return) call-fun)
1017 (setf (lambda-return fun) nil))))
1018 (%delete-lvar-use call) ; LET call does not have value semantics
1019 (values))
1021 ;;; Actually do LET conversion. We call subfunctions to do most of the
1022 ;;; work. We do REOPTIMIZE-LVAR on the args and CALL's lvar so that
1023 ;;; LET-specific IR1 optimizations get a chance. We blow away any
1024 ;;; entry for the function in *FREE-FUNS* so that nobody will create
1025 ;;; new references to it.
1026 (defun let-convert (fun call)
1027 (declare (type clambda fun) (type basic-combination call))
1028 (let* ((next-block (insert-let-body fun call))
1029 (next-block (if (node-tail-p call)
1031 next-block)))
1032 (move-return-stuff fun call next-block)
1033 (merge-lets fun call)
1034 (setf (node-tail-p call) nil)
1035 ;; If CALL has a derive type NIL, it means that "its return" is
1036 ;; unreachable, but the next BIND is still reachable; in order to
1037 ;; not confuse MAYBE-TERMINATE-BLOCK...
1038 (setf (node-derived-type call) *wild-type*)))
1040 ;;; Reoptimize all of CALL's args and its result.
1041 (defun reoptimize-call (call)
1042 (declare (type basic-combination call))
1043 (dolist (arg (basic-combination-args call))
1044 (when arg
1045 (reoptimize-lvar arg)))
1046 (reoptimize-lvar (node-lvar call))
1047 (values))
1049 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
1050 ;;; LET converted?
1051 (defun declarations-suppress-let-conversion-p (clambda)
1052 ;; From the user's point of view, LET-converting something that
1053 ;; has a name is inlining it. (The user can't see what we're doing
1054 ;; with anonymous things, and suppressing inlining
1055 ;; for such things can easily give Python acute indigestion, so
1056 ;; we don't.)
1058 ;; A functional that is already inline-expanded in this componsne definitely
1059 ;; deserves let-conversion -- and in case of main entry points for inline
1060 ;; expanded optional dispatch, the main-etry isn't explicitly marked :INLINE
1061 ;; even if the function really is.
1062 (when (and (leaf-has-source-name-p clambda)
1063 (not (functional-inline-expanded clambda)))
1064 ;; ANSI requires that explicit NOTINLINE be respected.
1065 (or (eq (lambda-inlinep clambda) :notinline)
1066 ;; If (= LET-CONVERSION 0) we can guess that inlining
1067 ;; generally won't be appreciated, but if the user
1068 ;; specifically requests inlining, that takes precedence over
1069 ;; our general guess.
1070 (and (policy clambda (= let-conversion 0))
1071 (not (eq (lambda-inlinep clambda) :inline))))))
1073 ;;; We also don't convert calls to named functions which appear in the
1074 ;;; initial component, delaying this until optimization. This
1075 ;;; minimizes the likelihood that we will LET-convert a function which
1076 ;;; may have references added due to later local inline expansion.
1077 (defun ok-initial-convert-p (fun)
1078 (not (and (leaf-has-source-name-p fun)
1079 (or (declarations-suppress-let-conversion-p fun)
1080 (eq (component-kind (lambda-component fun))
1081 :initial)))))
1083 ;;; ir1opt usually takes care of forwarding let-bound values directly
1084 ;;; to their destination when possible. However, locall analysis
1085 ;;; greatly benefits from that transformation, and is executed in a
1086 ;;; distinct phase from ir1opt. After let-conversion, variables
1087 ;;; bound to functional values are immediately substituted away.
1089 ;;; When called from locall, component is non-nil, and the functionals
1090 ;;; are marked for reanalysis when appropriate.
1091 (defun substitute-let-funargs (call fun component)
1092 (declare (type combination call) (type clambda fun)
1093 (type (or null component) component))
1094 (loop for arg in (combination-args call)
1095 and var in (lambda-vars fun)
1096 ;; only do that in the absence of assignment
1097 when (and arg (null (lambda-var-sets var)))
1099 (binding* ((use (lvar-uses arg))
1100 (() (ref-p use) :exit-if-null)
1101 (leaf (ref-leaf use))
1102 (done-something nil))
1103 ;; unlike propagate-let-args, we're only concerned with
1104 ;; functionals.
1105 (cond ((not (functional-p leaf)))
1106 ;; if the types match, we can mutate refs to point to
1107 ;; the functional instead of var
1108 ((csubtypep (single-value-type (node-derived-type use))
1109 (leaf-type var))
1110 (let ((use-component (node-component use)))
1111 (substitute-leaf-if
1112 (lambda (ref)
1113 (cond ((eq (node-component ref) use-component)
1114 (setf done-something t))
1116 (aver (lambda-toplevelish-p (lambda-home fun)))
1117 nil)))
1118 leaf var)))
1119 ;; otherwise, we can still play LVAR-level tricks for single
1120 ;; destination variables.
1121 ((and (singleton-p (leaf-refs var))
1122 ;; Don't substitute single-ref variables on high-debug /
1123 ;; low speed, to improve the debugging experience.
1124 (not (preserve-single-use-debug-var-p call var)))
1125 (setf done-something t)
1126 (substitute-single-use-lvar arg var)))
1127 ;; if we've done something, the functional may now be used in
1128 ;; more analysis-friendly manners. Enqueue it if we're in
1129 ;; locall.
1130 (when (and done-something
1131 component
1132 (member leaf (component-lambdas component)))
1133 (pushnew leaf (component-reanalyze-functionals component)))))
1134 (values))
1136 ;;; This function is called when there is some reason to believe that
1137 ;;; CLAMBDA might be converted into a LET. This is done after local
1138 ;;; call analysis, and also when a reference is deleted. We return
1139 ;;; true if we converted.
1141 ;;; COMPONENT is non-nil during local call analysis. It is used to
1142 ;;; re-enqueue functionals for reanalysis when they have been forwarded
1143 ;;; directly to destination nodes.
1144 (defun maybe-let-convert (clambda &optional component)
1145 (declare (type clambda clambda)
1146 (type (or null component) component))
1147 (unless (or (declarations-suppress-let-conversion-p clambda)
1148 (functional-has-external-references-p clambda))
1149 ;; We only convert to a LET when the function is a normal local
1150 ;; function, has no XEP, and is referenced in exactly one local
1151 ;; call. Conversion is also inhibited if the only reference is in
1152 ;; a block about to be deleted.
1154 ;; These rules limiting LET conversion may seem unnecessarily
1155 ;; restrictive, since there are some cases where we could do the
1156 ;; return with a jump that don't satisfy these requirements. The
1157 ;; reason for doing things this way is that it makes the concept
1158 ;; of a LET much more useful at the level of IR1 semantics. The
1159 ;; :ASSIGNMENT function kind provides another way to optimize
1160 ;; calls to single-return/multiple call functions.
1162 ;; We don't attempt to convert calls to functions that have an
1163 ;; XEP, since we might be embarrassed later when we want to
1164 ;; convert a newly discovered local call. Also, see
1165 ;; OK-INITIAL-CONVERT-P.
1166 (let ((refs (leaf-refs clambda)))
1167 (when (and refs
1168 (null (rest refs))
1169 (memq (functional-kind clambda) '(nil :assignment))
1170 (not (functional-entry-fun clambda)))
1171 (binding* ((ref (first refs))
1172 (ref-lvar (node-lvar ref) :exit-if-null)
1173 (dest (lvar-dest ref-lvar)))
1174 (when (and (basic-combination-p dest)
1175 (eq (basic-combination-fun dest) ref-lvar)
1176 (eq (basic-combination-kind dest) :local)
1177 (not (node-to-be-deleted-p dest))
1178 (not (block-delete-p (lambda-block clambda)))
1179 (cond ((ok-initial-convert-p clambda) t)
1181 (reoptimize-lvar ref-lvar)
1182 nil)))
1183 (when (eq clambda (node-home-lambda dest))
1184 (delete-lambda clambda)
1185 (return-from maybe-let-convert nil))
1186 (unless (eq (functional-kind clambda) :assignment)
1187 (let-convert clambda dest))
1188 (reoptimize-call dest)
1189 (setf (functional-kind clambda)
1190 (if (mv-combination-p dest) :mv-let :let))
1191 (when (combination-p dest) ; mv-combinations are too hairy
1192 ; for me to handle - PK 2012-05-30
1193 (substitute-let-funargs dest clambda component))))
1194 t))))
1196 ;;;; tail local calls and assignments
1198 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1199 ;;; they definitely won't generate any cleanup code. Currently we
1200 ;;; recognize lexical entry points that are only used locally (if at
1201 ;;; all).
1202 (defun only-harmless-cleanups (block1 block2)
1203 (declare (type cblock block1 block2))
1204 (or (eq block1 block2)
1205 (let ((cleanup2 (block-start-cleanup block2)))
1206 (do ((cleanup (block-end-cleanup block1)
1207 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
1208 ((eq cleanup cleanup2) t)
1209 (case (cleanup-kind cleanup)
1210 ((:block :tagbody)
1211 (unless (null (entry-exits (cleanup-mess-up cleanup)))
1212 (return nil)))
1213 (t (return nil)))))))
1215 ;;; If a potentially TR local call really is TR, then convert it to
1216 ;;; jump directly to the called function. We also call
1217 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1218 ;;; tail-convert. The second is the value of M-C-T-A.
1219 (defun maybe-convert-tail-local-call (call)
1220 (declare (type combination call))
1221 (let ((return (lvar-dest (node-lvar call)))
1222 (fun (combination-lambda call)))
1223 (aver (return-p return))
1224 (when (and (not (node-tail-p call)) ; otherwise already converted
1225 ;; this is a tail call
1226 (immediately-used-p (return-result return) call)
1227 (only-harmless-cleanups (node-block call)
1228 (node-block return))
1229 ;; If the call is in an XEP, we might decide to make it
1230 ;; non-tail so that we can use known return inside the
1231 ;; component.
1232 (not (eq (functional-kind (node-home-lambda call))
1233 :external))
1234 (not (block-delete-p (lambda-block fun))))
1235 (node-ends-block call)
1236 (let ((block (node-block call)))
1237 (setf (node-tail-p call) t)
1238 (unlink-blocks block (first (block-succ block)))
1239 (link-blocks block (lambda-block fun))
1240 (delete-lvar-use call)
1241 (values t (maybe-convert-to-assignment fun))))))
1243 ;;; This is called when we believe it might make sense to convert
1244 ;;; CLAMBDA to an assignment. All this function really does is
1245 ;;; determine when a function with more than one call can still be
1246 ;;; combined with the calling function's environment. We can convert
1247 ;;; when:
1248 ;;; -- The function is a normal, non-entry function, and
1249 ;;; -- Except for one call, all calls must be tail recursive calls
1250 ;;; in the called function (i.e. are self-recursive tail calls)
1251 ;;; -- OK-INITIAL-CONVERT-P is true.
1253 ;;; There may be one outside call, and it need not be tail-recursive.
1254 ;;; Since all tail local calls have already been converted to direct
1255 ;;; transfers, the only control semantics needed are to splice in the
1256 ;;; body at the non-tail call. If there is no non-tail call, then we
1257 ;;; need only merge the environments. Both cases are handled by
1258 ;;; LET-CONVERT.
1260 ;;; ### It would actually be possible to allow any number of outside
1261 ;;; calls as long as they all return to the same place (i.e. have the
1262 ;;; same conceptual continuation.) A special case of this would be
1263 ;;; when all of the outside calls are tail recursive.
1264 (defun maybe-convert-to-assignment (clambda)
1265 (declare (type clambda clambda))
1266 (when (and (not (functional-kind clambda))
1267 (not (functional-entry-fun clambda))
1268 (not (functional-has-external-references-p clambda)))
1269 (let ((outside-non-tail-call nil)
1270 (outside-call nil))
1271 (when (and (dolist (ref (leaf-refs clambda) t)
1272 (let ((dest (node-dest ref)))
1273 (when (or (not dest)
1274 (block-delete-p (node-block dest)))
1275 (return nil))
1276 (let ((home (node-home-lambda ref)))
1277 (unless (eq home clambda)
1278 (when outside-call
1279 (return nil))
1280 (setq outside-call dest))
1281 (unless (node-tail-p dest)
1282 (when (or outside-non-tail-call (eq home clambda))
1283 (return nil))
1284 (setq outside-non-tail-call dest)))))
1285 (ok-initial-convert-p clambda))
1286 (cond (outside-call (setf (functional-kind clambda) :assignment)
1287 (let-convert clambda outside-call)
1288 (when outside-non-tail-call
1289 (reoptimize-call outside-non-tail-call))
1291 (t (delete-lambda clambda)
1292 nil))))))