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