Replace %CODE-ENTRY-POINTS with an array, remove %SIMPLE-FUN-NEXT.
[sbcl.git] / src / compiler / locall.lisp
blobb58d35b9272c63ae10e45efc5775dffdc91161ac
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 (%local-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 (not (functional-entry-fun fun)))
502 (let* ((*current-component* (node-component ref))
503 (ep (optional-dispatch-entry-point-fun
504 fun (optional-dispatch-max-args fun)))
505 (args (basic-combination-args call)))
506 (when (and (null (leaf-refs ep))
507 (or (singleton-p args)
508 (call-all-args-fixed-p call)))
509 (aver (= (optional-dispatch-min-args fun) 0))
510 (setf (basic-combination-kind call) :local)
511 (sset-adjoin ep (lambda-calls-or-closes (node-home-lambda call)))
512 (merge-tail-sets call ep)
513 (change-ref-leaf ref ep)
514 (if (singleton-p args)
515 (assert-lvar-type
516 (first args)
517 (make-short-values-type (mapcar #'leaf-type (lambda-vars ep)))
518 (lexenv-policy (node-lexenv call)))
519 (let ((vars (lambda-vars ep)))
520 (loop for arg in args
521 while vars
523 (assert-lvar-type
525 (make-short-values-type
526 (and vars
527 (loop repeat (nth-value 1 (values-types
528 (lvar-derived-type arg)))
529 collect (leaf-type (pop vars)))))
530 (lexenv-policy (node-lexenv call)))))))))
531 (values))
533 ;;; Convenience function to mark local calls as known bad.
534 (defun transform-call-with-ir1-environment (node lambda default-name)
535 (aver (combination-p node))
536 (with-ir1-environment-from-node node
537 (transform-call node lambda
538 (or (combination-fun-source-name node nil)
539 default-name))))
541 (defun warn-invalid-local-call (node count &rest warn-arguments)
542 (aver (combination-p node))
543 (aver (typep count 'unsigned-byte))
544 (apply 'warn warn-arguments)
545 (transform-call-with-ir1-environment
546 node
547 `(lambda (&rest args)
548 (declare (ignore args))
549 (%local-arg-count-error ,count ',(combination-fun-debug-name node)))
550 '%local-arg-count-error))
552 ;;; Attempt to convert a call to a lambda. If the number of args is
553 ;;; wrong, we give a warning and mark the call as :ERROR to remove it
554 ;;; from future consideration. If the argcount is O.K. then we just
555 ;;; convert it.
556 (defun convert-lambda-call (ref call fun)
557 (declare (type ref ref) (type combination call) (type clambda fun))
558 (let ((nargs (length (lambda-vars fun)))
559 (n-call-args (length (combination-args call))))
560 (cond ((= n-call-args nargs)
561 (convert-call ref call fun))
563 (warn-invalid-local-call call n-call-args
564 'local-argument-mismatch
565 :format-control
566 "function called with ~R argument~:P, but wants exactly ~R"
567 :format-arguments (list n-call-args nargs))))))
569 ;;;; &OPTIONAL, &MORE and &KEYWORD calls
571 ;;; This is similar to CONVERT-LAMBDA-CALL, but deals with
572 ;;; OPTIONAL-DISPATCHes. If only fixed args are supplied, then convert
573 ;;; a call to the correct entry point. If &KEY args are supplied, then
574 ;;; dispatch to a subfunction. We don't convert calls to functions
575 ;;; that have a &MORE (or &REST) arg.
576 (defun convert-hairy-call (ref call fun)
577 (declare (type ref ref) (type combination call)
578 (type optional-dispatch fun))
579 (let ((min-args (optional-dispatch-min-args fun))
580 (max-args (optional-dispatch-max-args fun))
581 (call-args (length (combination-args call))))
582 (cond ((< call-args min-args)
583 (warn-invalid-local-call call call-args
584 'local-argument-mismatch
585 :format-control
586 "function called with ~R argument~:P, but wants at least ~R"
587 :format-arguments (list call-args min-args)))
588 ((<= call-args max-args)
589 (convert-call ref call
590 (let ((*current-component* (node-component ref)))
591 (optional-dispatch-entry-point-fun
592 fun (- call-args min-args)))))
593 ((optional-dispatch-more-entry fun)
594 (convert-more-call ref call fun))
596 (warn-invalid-local-call call call-args
597 'local-argument-mismatch
598 :format-control
599 "function called with ~R argument~:P, but wants at most ~R"
600 :format-arguments
601 (list call-args max-args)))))
602 (values))
604 ;;; This function is used to convert a call to an entry point when
605 ;;; complex transformations need to be done on the original arguments.
606 ;;; ENTRY is the entry point function that we are calling. VARS is a
607 ;;; list of variable names which are bound to the original call
608 ;;; arguments. IGNORES is the subset of VARS which are ignored. ARGS
609 ;;; is the list of arguments to the entry point function.
611 ;;; In order to avoid gruesome graph grovelling, we introduce a new
612 ;;; function that rearranges the arguments and calls the entry point.
613 ;;; We analyze the new function and the entry point immediately so
614 ;;; that everything gets converted during the single pass.
615 (defun convert-hairy-fun-entry (ref call entry vars ignores args indef)
616 (declare (list vars ignores args) (type ref ref) (type combination call)
617 (type clambda entry))
618 (let ((new-fun
619 (with-ir1-environment-from-node call
620 (ir1-convert-lambda
621 `(lambda ,vars
622 (declare (ignorable ,@ignores)
623 (indefinite-extent ,@indef))
624 (%funcall ,entry ,@args))
625 :debug-name (debug-name 'hairy-function-entry
626 (lvar-fun-debug-name
627 (basic-combination-fun call)))
628 :system-lambda t))))
629 (convert-call ref call new-fun)
630 (dolist (ref (leaf-refs entry))
631 (convert-call-if-possible ref (lvar-dest (node-lvar ref))))))
633 ;;; Use CONVERT-HAIRY-FUN-ENTRY to convert a &MORE-arg call to a known
634 ;;; function into a local call to the MAIN-ENTRY.
636 ;;; First we verify that all keywords are constant and legal. If there
637 ;;; aren't, then we warn the user and don't attempt to convert the call.
639 ;;; We massage the supplied &KEY arguments into the order expected
640 ;;; by the main entry. This is done by binding all the arguments to
641 ;;; the keyword call to variables in the introduced lambda, then
642 ;;; passing these values variables in the correct order when calling
643 ;;; the main entry. Unused arguments (such as the keywords themselves)
644 ;;; are discarded simply by not passing them along.
646 ;;; If there is a &REST arg, then we bundle up the args and pass them
647 ;;; to LIST.
648 (defun convert-more-call (ref call fun)
649 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
650 (let* ((max (optional-dispatch-max-args fun))
651 (arglist (optional-dispatch-arglist fun))
652 (args (combination-args call))
653 (more (nthcdr max args))
654 (flame (policy call (or (> speed inhibit-warnings)
655 (> space inhibit-warnings))))
656 (loser nil)
657 (allowp nil)
658 (allow-found nil)
659 (temps (make-gensym-list max))
660 (more-temps (make-gensym-list (length more))))
661 (collect ((ignores)
662 (supplied)
663 (key-vars))
665 (dolist (var arglist)
666 (let ((info (lambda-var-arg-info var)))
667 (when info
668 (ecase (arg-info-kind info)
669 (:keyword
670 (key-vars var))
671 ((:rest :optional))
672 ((:more-context :more-count)
673 (compiler-warn "can't local-call functions with &MORE args")
674 (setf (basic-combination-kind call) :error)
675 (return-from convert-more-call))))))
677 (when (optional-dispatch-keyp fun)
678 (when (oddp (length more))
679 (compiler-warn "function called with odd number of ~
680 arguments in keyword portion")
681 (transform-call-with-ir1-environment
682 call
683 `(lambda (&rest args)
684 (declare (ignore args))
685 (%odd-key-args-error))
686 '%odd-key-args-error)
687 (return-from convert-more-call))
689 (do ((key more (cddr key))
690 (temp more-temps (cddr temp)))
691 ((null key))
692 (let ((lvar (first key)))
693 (unless (constant-lvar-p lvar)
694 (when flame
695 (compiler-notify "non-constant keyword in keyword call"))
696 (setf (basic-combination-kind call) :error)
697 (return-from convert-more-call))
699 (let ((name (lvar-value lvar))
700 (dummy (first temp))
701 (val (second temp)))
702 (when (and (eq name :allow-other-keys) (not allow-found))
703 (let ((val (second key)))
704 (cond ((constant-lvar-p val)
705 (setq allow-found t
706 allowp (lvar-value val)))
707 (t (when flame
708 (compiler-notify "non-constant :ALLOW-OTHER-KEYS value"))
709 (setf (basic-combination-kind call) :error)
710 (return-from convert-more-call)))))
711 (dolist (var (key-vars)
712 (progn
713 (ignores dummy val)
714 (unless (eq name :allow-other-keys)
715 (setq loser (list name)))))
716 (let ((info (lambda-var-arg-info var)))
717 (when (eq (arg-info-key info) name)
718 (ignores dummy)
719 (if (member var (supplied) :key #'car)
720 (ignores val)
721 (supplied (cons var val)))
722 (return)))))))
724 (when (and loser (not (optional-dispatch-allowp fun)) (not allowp))
725 (compiler-warn "function called with unknown argument keyword ~S"
726 (car loser))
727 (transform-call-with-ir1-environment
728 call
729 `(lambda (&rest args)
730 (declare (ignore args))
731 (%unknown-key-arg-error ',(car loser)))
732 '%unknown-key-arg-error)
733 (return-from convert-more-call)))
735 (collect ((call-args))
736 (do ((var arglist (cdr var))
737 (temp temps (cdr temp)))
738 ((null var))
739 (let ((info (lambda-var-arg-info (car var))))
740 (if info
741 (ecase (arg-info-kind info)
742 (:optional
743 (call-args (car temp))
744 (when (arg-info-supplied-p info)
745 (call-args (if (arg-info-supplied-used-p info)
747 1))))
748 (:rest
749 (call-args `(list ,@more-temps))
750 ;; &REST arguments may be accompanied by extra
751 ;; context and count arguments. We know this by
752 ;; the ARG-INFO-DEFAULT. Supply 0 and 0 or
753 ;; don't convert at all depending.
754 (let ((more (arg-info-default info)))
755 (when more
756 (unless (eq t more)
757 (destructuring-bind (context count &optional used) more
758 (declare (ignore context count))
759 (when used
760 ;; We've already converted to use the more context
761 ;; instead of the rest list.
762 (return-from convert-more-call))))
763 (call-args 0)
764 (call-args 0)
765 (setf (arg-info-default info) t)))
766 (return))
767 (:keyword
768 (return)))
769 (call-args (car temp)))))
771 (dolist (var (key-vars))
772 (let ((info (lambda-var-arg-info var))
773 (temp (cdr (assoc var (supplied)))))
774 (if temp
775 (call-args temp)
776 (call-args (arg-info-default info)))
777 (when (arg-info-supplied-p info)
778 (call-args (cond ((arg-info-supplied-used-p info)
779 (not (null temp)))
780 (temp
783 0))))))
785 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
786 (append temps more-temps)
787 (ignores) (call-args)
788 (when (optional-rest-p fun)
789 more-temps)))))
791 (values))
793 ;;;; LET conversion
794 ;;;;
795 ;;;; Converting to a LET has differing significance to various parts
796 ;;;; of the compiler:
797 ;;;; -- The body of a LET is spliced in immediately after the
798 ;;;; corresponding combination node, making the control transfer
799 ;;;; explicit and allowing LETs to be mashed together into a single
800 ;;;; block. The value of the LET is delivered directly to the
801 ;;;; original lvar for the call, eliminating the need to
802 ;;;; propagate information from the dummy result lvar.
803 ;;;; -- As far as IR1 optimization is concerned, it is interesting in
804 ;;;; that there is only one expression that the variable can be bound
805 ;;;; to, and this is easily substituted for.
806 ;;;; -- LETs are interesting to environment analysis and to the back
807 ;;;; end because in most ways a LET can be considered to be "the
808 ;;;; same function" as its home function.
809 ;;;; -- LET conversion has dynamic scope implications, since control
810 ;;;; transfers within the same environment are local. In a local
811 ;;;; control transfer, cleanup code must be emitted to remove
812 ;;;; dynamic bindings that are no longer in effect.
814 ;;; Set up the control transfer to the called CLAMBDA. We split the
815 ;;; call block immediately after the call, and link the head of
816 ;;; CLAMBDA to the call block. The successor block after splitting
817 ;;; (where we return to) is returned.
819 ;;; If the lambda is is a different component than the call, then we
820 ;;; call JOIN-COMPONENTS. This only happens in block compilation
821 ;;; before FIND-INITIAL-DFO.
822 (defun insert-let-body (clambda call)
823 (declare (type clambda clambda) (type basic-combination call))
824 (let* ((call-block (node-block call))
825 (bind-block (node-block (lambda-bind clambda)))
826 (component (block-component call-block)))
827 (aver-live-component component)
828 (let ((clambda-component (block-component bind-block)))
829 (unless (eq clambda-component component)
830 (aver (eq (component-kind component) :initial))
831 (join-components component clambda-component)))
832 (let ((*current-component* component))
833 (node-ends-block call))
834 (destructuring-bind (next-block)
835 (block-succ call-block)
836 (unlink-blocks call-block next-block)
837 (link-blocks call-block bind-block)
838 next-block)))
840 ;;; Remove CLAMBDA from the tail set of anything it used to be in the
841 ;;; same set as; but leave CLAMBDA with a valid tail set value of
842 ;;; its own, for the benefit of code which might try to pull
843 ;;; something out of it (e.g. return type).
844 (defun depart-from-tail-set (clambda)
845 ;; Until sbcl-0.pre7.37.flaky5.2, we did
846 ;; (LET ((TAILS (LAMBDA-TAIL-SET CLAMBDA)))
847 ;; (SETF (TAIL-SET-FUNS TAILS)
848 ;; (DELETE CLAMBDA (TAIL-SET-FUNS TAILS))))
849 ;; (SETF (LAMBDA-TAIL-SET CLAMBDA) NIL)
850 ;; here. Apparently the idea behind the (SETF .. NIL) was that since
851 ;; TAIL-SET-FUNS no longer thinks we're in the tail set, it's
852 ;; inconsistent, and perhaps unsafe, for us to think we're in the
853 ;; tail set. Unfortunately..
855 ;; The (SETF .. NIL) caused problems in sbcl-0.pre7.37.flaky5.2 when
856 ;; I was trying to get Python to emit :EXTERNAL LAMBDAs directly
857 ;; (instead of only being able to emit funny little :TOPLEVEL stubs
858 ;; which you called in order to get the address of an external LAMBDA):
859 ;; the external function was defined in terms of internal function,
860 ;; which was LET-converted, and then things blew up downstream when
861 ;; FINALIZE-XEP-DEFINITION tried to find out its DEFINED-TYPE from
862 ;; the now-NILed-out TAIL-SET. So..
864 ;; To deal with this problem, we no longer NIL out
865 ;; (LAMBDA-TAIL-SET CLAMBDA) here. Instead:
866 ;; * If we're the only function in TAIL-SET-FUNS, it should
867 ;; be safe to leave ourself linked to it, and it to you.
868 ;; * If there are other functions in TAIL-SET-FUNS, then we're
869 ;; afraid of future optimizations on those functions causing
870 ;; the TAIL-SET object no longer to be valid to describe our
871 ;; return value. Thus, we delete ourselves from that object;
872 ;; but we save a newly-allocated tail-set, derived from the old
873 ;; one, for ourselves, for the use of later code (e.g.
874 ;; FINALIZE-XEP-DEFINITION) which might want to
875 ;; know about our return type.
876 (let* ((old-tail-set (lambda-tail-set clambda))
877 (old-tail-set-funs (tail-set-funs old-tail-set)))
878 (unless (= 1 (length old-tail-set-funs))
879 (setf (tail-set-funs old-tail-set)
880 (delete clambda old-tail-set-funs))
881 (let ((new-tail-set (copy-tail-set old-tail-set)))
882 (setf (lambda-tail-set clambda) new-tail-set
883 (tail-set-funs new-tail-set) (list clambda)))))
884 ;; The documentation on TAIL-SET-INFO doesn't tell whether it could
885 ;; remain valid in this case, so we nuke it on the theory that
886 ;; missing information tends to be less dangerous than incorrect
887 ;; information.
888 (setf (tail-set-info (lambda-tail-set clambda)) nil))
890 ;;; Handle the PHYSENV semantics of LET conversion. We add CLAMBDA and
891 ;;; its LETs to LETs for the CALL's home function. We merge the calls
892 ;;; for CLAMBDA with the calls for the home function, removing CLAMBDA
893 ;;; in the process. We also merge the ENTRIES.
895 ;;; We also unlink the function head from the component head and set
896 ;;; COMPONENT-REANALYZE to true to indicate that the DFO should be
897 ;;; recomputed.
898 (defun merge-lets (clambda call)
900 (declare (type clambda clambda) (type basic-combination call))
902 (let ((component (node-component call)))
903 (unlink-blocks (component-head component) (lambda-block clambda))
904 (setf (component-lambdas component)
905 (delete clambda (component-lambdas component)))
906 (setf (component-reanalyze component) t))
907 (setf (lambda-call-lexenv clambda) (node-lexenv call))
909 (depart-from-tail-set clambda)
911 (let* ((home (node-home-lambda call))
912 (home-physenv (lambda-physenv home))
913 (physenv (lambda-physenv clambda)))
915 (aver (not (eq home clambda)))
917 ;; CLAMBDA belongs to HOME now.
918 (push clambda (lambda-lets home))
919 (setf (lambda-home clambda) home)
920 (setf (lambda-physenv clambda) home-physenv)
922 (when physenv
923 (unless home-physenv
924 (setf home-physenv (get-lambda-physenv home)))
925 (setf (physenv-nlx-info home-physenv)
926 (nconc (physenv-nlx-info physenv)
927 (physenv-nlx-info home-physenv))))
929 ;; All of CLAMBDA's LETs belong to HOME now.
930 (let ((lets (lambda-lets clambda)))
931 (dolist (let lets)
932 (setf (lambda-home let) home)
933 (setf (lambda-physenv let) home-physenv))
934 (setf (lambda-lets home) (nconc lets (lambda-lets home))))
935 ;; CLAMBDA no longer has an independent existence as an entity
936 ;; which has LETs.
937 (setf (lambda-lets clambda) nil)
939 ;; HOME no longer calls CLAMBDA, and owns all of CLAMBDA's old
940 ;; DFO dependencies.
941 (sset-union (lambda-calls-or-closes home)
942 (lambda-calls-or-closes clambda))
943 (sset-delete clambda (lambda-calls-or-closes home))
944 ;; CLAMBDA no longer has an independent existence as an entity
945 ;; which calls things or has DFO dependencies.
946 (setf (lambda-calls-or-closes clambda) nil)
948 ;; All of CLAMBDA's ENTRIES belong to HOME now.
949 (setf (lambda-entries home)
950 (nconc (lambda-entries clambda)
951 (lambda-entries home)))
952 ;; CLAMBDA no longer has an independent existence as an entity
953 ;; with ENTRIES.
954 (setf (lambda-entries clambda) nil))
956 (values))
958 ;;; Handle the value semantics of LET conversion. Delete FUN's return
959 ;;; node, and change the control flow to transfer to NEXT-BLOCK
960 ;;; instead. Move all the uses of the result lvar to CALL's lvar.
961 (defun move-return-uses (fun call next-block)
962 (declare (type clambda fun) (type basic-combination call)
963 (type cblock next-block))
964 (let* ((return (lambda-return fun))
965 (return-block (progn
966 (ensure-block-start (node-prev return))
967 (node-block return))))
968 (unlink-blocks return-block
969 (component-tail (block-component return-block)))
970 (link-blocks return-block next-block)
971 (unlink-node return)
972 (delete-return return)
973 (let ((result (return-result return))
974 (lvar (if (node-tail-p call)
975 (return-result (lambda-return (node-home-lambda call)))
976 (node-lvar call)))
977 (call-type (node-derived-type call)))
978 (unless (eq call-type *wild-type*)
979 ;; FIXME: Replace the call with unsafe CAST. -- APD, 2003-01-26
980 (do-uses (use result)
981 (derive-node-type use call-type)))
982 (substitute-lvar-uses lvar result
983 (and lvar (eq (lvar-uses lvar) call)))))
984 (values))
986 ;;; We are converting FUN to be a LET when the call is in a non-tail
987 ;;; position. Any previously tail calls in FUN are no longer tail
988 ;;; calls, and must be restored to normal calls which transfer to
989 ;;; NEXT-BLOCK (FUN's return point.) We can't do this by DO-USES on
990 ;;; the RETURN-RESULT, because the return might have been deleted (if
991 ;;; all calls were TR.)
992 (defun unconvert-tail-calls (fun call next-block)
993 (do-sset-elements (called (lambda-calls-or-closes fun))
994 (when (lambda-p called)
995 (dolist (ref (leaf-refs called))
996 (let ((this-call (node-dest ref)))
997 (when (and this-call
998 (node-tail-p this-call)
999 (eq (node-home-lambda this-call) fun))
1000 (setf (node-tail-p this-call) nil)
1001 (ecase (functional-kind called)
1002 ((nil :cleanup :optional)
1003 (let ((block (node-block this-call))
1004 (lvar (node-lvar call)))
1005 (unlink-blocks block (first (block-succ block)))
1006 (link-blocks block next-block)
1007 (aver (not (node-lvar this-call)))
1008 (add-lvar-use this-call lvar)))
1009 (:deleted)
1010 ;; The called function might be an assignment in the
1011 ;; case where we are currently converting that function.
1012 ;; In steady-state, assignments never appear as a called
1013 ;; function.
1014 (:assignment
1015 (aver (eq called fun)))))))))
1016 (values))
1018 ;;; Deal with returning from a LET or assignment that we are
1019 ;;; converting. FUN is the function we are calling, CALL is a call to
1020 ;;; FUN, and NEXT-BLOCK is the return point for a non-tail call, or
1021 ;;; NULL if call is a tail call.
1023 ;;; If the call is not a tail call, then we must do
1024 ;;; UNCONVERT-TAIL-CALLS, since a tail call is a call which returns
1025 ;;; its value out of the enclosing non-let function. When call is
1026 ;;; non-TR, we must convert it back to an ordinary local call, since
1027 ;;; the value must be delivered to the receiver of CALL's value.
1029 ;;; We do different things depending on whether the caller and callee
1030 ;;; have returns left:
1032 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT.
1033 ;;; Either the function doesn't return, or all returns are via
1034 ;;; tail-recursive local calls.
1035 ;;; -- If CALL is a non-tail call, or if both have returns, then
1036 ;;; we delete the callee's return, move its uses to the call's
1037 ;;; result lvar, and transfer control to the appropriate
1038 ;;; return point.
1039 ;;; -- If the callee has a return, but the caller doesn't, then we
1040 ;;; move the return to the caller.
1041 (defun move-return-stuff (fun call next-block)
1042 (declare (type clambda fun) (type basic-combination call)
1043 (type (or cblock null) next-block))
1044 (when next-block
1045 (unconvert-tail-calls fun call next-block))
1046 (let* ((return (lambda-return fun))
1047 (call-fun (node-home-lambda call))
1048 (call-return (lambda-return call-fun)))
1049 (when (and call-return
1050 (block-delete-p (node-block call-return)))
1051 (delete-return call-return)
1052 (unlink-node call-return)
1053 (setq call-return nil))
1054 (cond ((not return))
1055 ((or next-block call-return)
1056 (unless (block-delete-p (node-block return))
1057 (unless next-block
1058 (ensure-block-start (node-prev call-return))
1059 (setq next-block (node-block call-return)))
1060 (move-return-uses fun call next-block)))
1062 (aver (node-tail-p call))
1063 (setf (lambda-return call-fun) return)
1064 (setf (return-lambda return) call-fun)
1065 (setf (lambda-return fun) nil))))
1066 (%delete-lvar-use call) ; LET call does not have value semantics
1067 (values))
1069 ;;; Actually do LET conversion. We call subfunctions to do most of the
1070 ;;; work. We do REOPTIMIZE-LVAR on the args and CALL's lvar so that
1071 ;;; LET-specific IR1 optimizations get a chance. We blow away any
1072 ;;; entry for the function in *FREE-FUNS* so that nobody will create
1073 ;;; new references to it.
1074 (defun let-convert (fun call)
1075 (declare (type clambda fun) (type basic-combination call))
1076 (let* ((next-block (insert-let-body fun call))
1077 (next-block (if (node-tail-p call)
1079 next-block)))
1080 (move-return-stuff fun call next-block)
1081 (merge-lets fun call)
1082 (setf (node-tail-p call) nil)
1083 ;; If CALL has a derive type NIL, it means that "its return" is
1084 ;; unreachable, but the next BIND is still reachable; in order to
1085 ;; not confuse MAYBE-TERMINATE-BLOCK...
1086 (setf (node-derived-type call) *wild-type*)))
1088 ;;; Reoptimize all of CALL's args and its result.
1089 (defun reoptimize-call (call)
1090 (declare (type basic-combination call))
1091 (dolist (arg (basic-combination-args call))
1092 (when arg
1093 (reoptimize-lvar arg)))
1094 (reoptimize-lvar (node-lvar call))
1095 (values))
1097 ;;; Are there any declarations in force to say CLAMBDA shouldn't be
1098 ;;; LET converted?
1099 (defun declarations-suppress-let-conversion-p (clambda)
1100 ;; From the user's point of view, LET-converting something that
1101 ;; has a name is inlining it. (The user can't see what we're doing
1102 ;; with anonymous things, and suppressing inlining
1103 ;; for such things can easily give Python acute indigestion, so
1104 ;; we don't.)
1106 ;; A functional that is already inline-expanded in this componsne definitely
1107 ;; deserves let-conversion -- and in case of main entry points for inline
1108 ;; expanded optional dispatch, the main-etry isn't explicitly marked :INLINE
1109 ;; even if the function really is.
1110 (when (and (leaf-has-source-name-p clambda)
1111 (not (functional-inline-expanded clambda)))
1112 ;; ANSI requires that explicit NOTINLINE be respected.
1113 (or (eq (lambda-inlinep clambda) :notinline)
1114 ;; If (= LET-CONVERSION 0) we can guess that inlining
1115 ;; generally won't be appreciated, but if the user
1116 ;; specifically requests inlining, that takes precedence over
1117 ;; our general guess.
1118 (and (policy clambda (= let-conversion 0))
1119 (not (eq (lambda-inlinep clambda) :inline))))))
1121 ;;; We also don't convert calls to named functions which appear in the
1122 ;;; initial component, delaying this until optimization. This
1123 ;;; minimizes the likelihood that we will LET-convert a function which
1124 ;;; may have references added due to later local inline expansion.
1125 (defun ok-initial-convert-p (fun)
1126 (not (and (leaf-has-source-name-p fun)
1127 (or (declarations-suppress-let-conversion-p fun)
1128 (eq (component-kind (lambda-component fun))
1129 :initial)))))
1131 ;;; ir1opt usually takes care of forwarding let-bound values directly
1132 ;;; to their destination when possible. However, locall analysis
1133 ;;; greatly benefits from that transformation, and is executed in a
1134 ;;; distinct phase from ir1opt. After let-conversion, variables
1135 ;;; bound to functional values are immediately substituted away.
1137 ;;; When called from locall, component is non-nil, and the functionals
1138 ;;; are marked for reanalysis when appropriate.
1139 (defun substitute-let-funargs (call fun component)
1140 (declare (type combination call) (type clambda fun)
1141 (type (or null component) component))
1142 (loop for arg in (combination-args call)
1143 and var in (lambda-vars fun)
1144 ;; only do that in the absence of assignment
1145 when (and arg (null (lambda-var-sets var)))
1147 (binding* ((use (lvar-uses arg))
1148 (() (ref-p use) :exit-if-null)
1149 (leaf (ref-leaf use))
1150 (done-something nil))
1151 ;; unlike propagate-let-args, we're only concerned with
1152 ;; functionals.
1153 (cond ((not (functional-p leaf)))
1154 ;; if the types match, we can mutate refs to point to
1155 ;; the functional instead of var
1156 ((csubtypep (single-value-type (node-derived-type use))
1157 (leaf-type var))
1158 (let ((use-component (node-component use)))
1159 (substitute-leaf-if
1160 (lambda (ref)
1161 (cond ((eq (node-component ref) use-component)
1162 (setf done-something t))
1164 (aver (lambda-toplevelish-p (lambda-home fun)))
1165 nil)))
1166 leaf var)))
1167 ;; otherwise, we can still play LVAR-level tricks for single
1168 ;; destination variables.
1169 ((and (singleton-p (leaf-refs var))
1170 ;; Don't substitute single-ref variables on high-debug /
1171 ;; low speed, to improve the debugging experience.
1172 (not (preserve-single-use-debug-var-p call var)))
1173 (setf done-something t)
1174 (substitute-single-use-lvar arg var)))
1175 ;; if we've done something, the functional may now be used in
1176 ;; more analysis-friendly manners. Enqueue it if we're in
1177 ;; locall.
1178 (when (and done-something
1179 component
1180 (member leaf (component-lambdas component)))
1181 (pushnew leaf (component-reanalyze-functionals component)))))
1182 (values))
1184 ;;; This function is called when there is some reason to believe that
1185 ;;; CLAMBDA might be converted into a LET. This is done after local
1186 ;;; call analysis, and also when a reference is deleted. We return
1187 ;;; true if we converted.
1189 ;;; COMPONENT is non-nil during local call analysis. It is used to
1190 ;;; re-enqueue functionals for reanalysis when they have been forwarded
1191 ;;; directly to destination nodes.
1192 (defun maybe-let-convert (clambda &optional component)
1193 (declare (type clambda clambda)
1194 (type (or null component) component))
1195 (unless (or (declarations-suppress-let-conversion-p clambda)
1196 (functional-has-external-references-p clambda))
1197 ;; We only convert to a LET when the function is a normal local
1198 ;; function, has no XEP, and is referenced in exactly one local
1199 ;; call. Conversion is also inhibited if the only reference is in
1200 ;; a block about to be deleted.
1202 ;; These rules limiting LET conversion may seem unnecessarily
1203 ;; restrictive, since there are some cases where we could do the
1204 ;; return with a jump that don't satisfy these requirements. The
1205 ;; reason for doing things this way is that it makes the concept
1206 ;; of a LET much more useful at the level of IR1 semantics. The
1207 ;; :ASSIGNMENT function kind provides another way to optimize
1208 ;; calls to single-return/multiple call functions.
1210 ;; We don't attempt to convert calls to functions that have an
1211 ;; XEP, since we might be embarrassed later when we want to
1212 ;; convert a newly discovered local call. Also, see
1213 ;; OK-INITIAL-CONVERT-P.
1214 (let ((refs (leaf-refs clambda)))
1215 (when (and refs
1216 (null (rest refs))
1217 (memq (functional-kind clambda) '(nil :assignment))
1218 (not (functional-entry-fun clambda)))
1219 (binding* ((ref (first refs))
1220 (ref-lvar (node-lvar ref) :exit-if-null)
1221 (dest (lvar-dest ref-lvar)))
1222 (when (and (basic-combination-p dest)
1223 (eq (basic-combination-fun dest) ref-lvar)
1224 (eq (basic-combination-kind dest) :local)
1225 (not (node-to-be-deleted-p dest))
1226 (not (block-delete-p (lambda-block clambda)))
1227 (cond ((ok-initial-convert-p clambda) t)
1229 (reoptimize-lvar ref-lvar)
1230 nil)))
1231 (when (eq clambda (node-home-lambda dest))
1232 (delete-lambda clambda)
1233 (return-from maybe-let-convert nil))
1234 (unless (eq (functional-kind clambda) :assignment)
1235 (let-convert clambda dest))
1236 (reoptimize-call dest)
1237 (setf (functional-kind clambda)
1238 (if (mv-combination-p dest) :mv-let :let))
1239 (when (combination-p dest) ; mv-combinations are too hairy
1240 ; for me to handle - PK 2012-05-30
1241 (substitute-let-funargs dest clambda component))))
1242 t))))
1244 ;;;; tail local calls and assignments
1246 ;;; Return T if there are no cleanups between BLOCK1 and BLOCK2, or if
1247 ;;; they definitely won't generate any cleanup code. Currently we
1248 ;;; recognize lexical entry points that are only used locally (if at
1249 ;;; all).
1250 (defun only-harmless-cleanups (block1 block2)
1251 (declare (type cblock block1 block2))
1252 (or (eq block1 block2)
1253 (let ((cleanup2 (block-start-cleanup block2)))
1254 (do-nested-cleanups (cleanup block1 t)
1255 (when (eq cleanup cleanup2)
1256 (return t))
1257 (case (cleanup-kind cleanup)
1258 ((:block :tagbody)
1259 (when (entry-exits (cleanup-mess-up cleanup))
1260 (return nil)))
1261 (t (return nil)))))))
1263 ;;; If a potentially TR local call really is TR, then convert it to
1264 ;;; jump directly to the called function. We also call
1265 ;;; MAYBE-CONVERT-TO-ASSIGNMENT. The first value is true if we
1266 ;;; tail-convert. The second is the value of M-C-T-A.
1267 (defun maybe-convert-tail-local-call (call)
1268 (declare (type combination call))
1269 (let ((return (lvar-dest (node-lvar call)))
1270 (fun (combination-lambda call)))
1271 (aver (return-p return))
1272 (when (and (not (node-tail-p call)) ; otherwise already converted
1273 ;; this is a tail call
1274 (immediately-used-p (return-result return) call)
1275 (only-harmless-cleanups (node-block call)
1276 (node-block return))
1277 ;; If the call is in an XEP, we might decide to make it
1278 ;; non-tail so that we can use known return inside the
1279 ;; component.
1280 (not (eq (functional-kind (node-home-lambda call))
1281 :external))
1282 (not (block-delete-p (lambda-block fun))))
1283 (node-ends-block call)
1284 (let ((block (node-block call)))
1285 (setf (node-tail-p call) t)
1286 (unlink-blocks block (first (block-succ block)))
1287 (link-blocks block (lambda-block fun))
1288 (delete-lvar-use call)
1289 (values t (maybe-convert-to-assignment fun))))))
1291 ;;; This is called when we believe it might make sense to convert
1292 ;;; CLAMBDA to an assignment. All this function really does is
1293 ;;; determine when a function with more than one call can still be
1294 ;;; combined with the calling function's environment. We can convert
1295 ;;; when:
1296 ;;; -- The function is a normal, non-entry function, and
1297 ;;; -- Except for one call, all calls must be tail recursive calls
1298 ;;; in the called function (i.e. are self-recursive tail calls)
1299 ;;; -- OK-INITIAL-CONVERT-P is true.
1301 ;;; There may be one outside call, and it need not be tail-recursive.
1302 ;;; Since all tail local calls have already been converted to direct
1303 ;;; transfers, the only control semantics needed are to splice in the
1304 ;;; body at the non-tail call. If there is no non-tail call, then we
1305 ;;; need only merge the environments. Both cases are handled by
1306 ;;; LET-CONVERT.
1308 ;;; ### It would actually be possible to allow any number of outside
1309 ;;; calls as long as they all return to the same place (i.e. have the
1310 ;;; same conceptual continuation.) A special case of this would be
1311 ;;; when all of the outside calls are tail recursive.
1312 (defun maybe-convert-to-assignment (clambda)
1313 (declare (type clambda clambda))
1314 (when (and (not (functional-kind clambda))
1315 (not (functional-entry-fun clambda))
1316 (not (functional-has-external-references-p clambda)))
1317 (let ((outside-non-tail-call nil)
1318 (outside-call nil))
1319 (when (and (dolist (ref (leaf-refs clambda) t)
1320 (let ((dest (node-dest ref)))
1321 (when (or (not dest)
1322 (block-delete-p (node-block dest)))
1323 (return nil))
1324 (let ((home (node-home-lambda ref)))
1325 (unless (eq home clambda)
1326 (when outside-call
1327 (return nil))
1328 (setq outside-call dest))
1329 (unless (node-tail-p dest)
1330 (when (or outside-non-tail-call (eq home clambda))
1331 (return nil))
1332 (setq outside-non-tail-call dest)))))
1333 (ok-initial-convert-p clambda))
1334 (cond (outside-call (setf (functional-kind clambda) :assignment)
1335 (let-convert clambda outside-call)
1336 (when outside-non-tail-call
1337 (reoptimize-call outside-non-tail-call))
1339 (t (delete-lambda clambda)
1340 nil))))))