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