Fix make-sequence type derivation with unknown types.
[sbcl.git] / src / compiler / physenvanal.lisp
blob188926f0c1fe394bb530f670de68b7b7059fe9fd
1 ;;;; This file implements the environment analysis phase for the
2 ;;;; compiler. This phase annotates IR1 with a hierarchy environment
3 ;;;; structures, determining the physical environment that each LAMBDA
4 ;;;; allocates its variables and finding what values are closed over
5 ;;;; by each physical environment.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
16 (in-package "SB!C")
18 ;;; Do environment analysis on the code in COMPONENT. This involves
19 ;;; various things:
20 ;;; 1. Make a PHYSENV structure for each non-LET LAMBDA, assigning
21 ;;; the LAMBDA-PHYSENV for all LAMBDAs.
22 ;;; 2. Find all values that need to be closed over by each
23 ;;; physical environment.
24 ;;; 3. Scan the blocks in the component closing over non-local-exit
25 ;;; continuations.
26 ;;; 4. Delete all non-top-level functions with no references. This
27 ;;; should only get functions with non-NULL kinds, since normal
28 ;;; functions are deleted when their references go to zero.
29 (defun physenv-analyze (component)
30 (declare (type component component))
31 (aver (every (lambda (x)
32 (eq (functional-kind x) :deleted))
33 (component-new-functionals component)))
34 (setf (component-new-functionals component) ())
35 (dolist (clambda (component-lambdas component))
36 (reinit-lambda-physenv clambda))
37 (mapc #'add-lambda-vars-and-let-vars-to-closures
38 (component-lambdas component))
40 (find-non-local-exits component)
41 (recheck-dynamic-extent-lvars component)
42 (find-cleanup-points component)
43 (tail-annotate component)
44 (analyze-indirect-lambda-vars component)
46 (dolist (fun (component-lambdas component))
47 (when (null (leaf-refs fun))
48 (let ((kind (functional-kind fun)))
49 (unless (or (eq kind :toplevel)
50 (functional-has-external-references-p fun))
51 (aver (member kind '(:optional :cleanup :escape)))
52 (setf (functional-kind fun) nil)
53 (delete-functional fun)))))
55 (setf (component-nlx-info-generated-p component) t)
56 (values))
58 ;;; This is to be called on a COMPONENT with top level LAMBDAs before
59 ;;; the compilation of the associated non-top-level code to detect
60 ;;; closed over top level variables. We just do COMPUTE-CLOSURE on all
61 ;;; the lambdas. This will pre-allocate environments for all the
62 ;;; functions with closed-over top level variables. The post-pass will
63 ;;; use the existing structure, rather than allocating a new one. We
64 ;;; return true if we discover any possible closure vars.
65 (defun pre-physenv-analyze-toplevel (component)
66 (declare (type component component))
67 (let ((found-it nil))
68 (dolist (lambda (component-lambdas component))
69 (when (add-lambda-vars-and-let-vars-to-closures lambda)
70 (setq found-it t)))
71 found-it))
73 ;;; If CLAMBDA has a PHYSENV, return it, otherwise assign an empty one
74 ;;; and return that.
75 (defun get-lambda-physenv (clambda)
76 (declare (type clambda clambda))
77 (let ((homefun (lambda-home clambda)))
78 (or (lambda-physenv homefun)
79 (let ((res (make-physenv :lambda homefun)))
80 (setf (lambda-physenv homefun) res)
81 ;; All the LETLAMBDAs belong to HOMEFUN, and share the same
82 ;; PHYSENV. Thus, (1) since HOMEFUN's PHYSENV was NIL,
83 ;; theirs should be NIL too, and (2) since we're modifying
84 ;; HOMEFUN's PHYSENV, we should modify theirs, too.
85 (dolist (letlambda (lambda-lets homefun))
86 (aver (eql (lambda-home letlambda) homefun))
87 (aver (null (lambda-physenv letlambda)))
88 (setf (lambda-physenv letlambda) res))
89 res))))
91 ;;; If FUN has no physical environment, assign one, otherwise clean up
92 ;;; the old physical environment and the INDIRECT flag on LAMBDA-VARs.
93 ;;; This is necessary because pre-analysis is done before
94 ;;; optimization.
95 (defun reinit-lambda-physenv (fun)
96 (let ((old (lambda-physenv (lambda-home fun))))
97 (cond (old
98 (setf (physenv-closure old) nil)
99 (flet ((clear (fun)
100 (dolist (var (lambda-vars fun))
101 (setf (lambda-var-indirect var) nil))))
102 (clear fun)
103 (map nil #'clear (lambda-lets fun))))
105 (get-lambda-physenv fun))))
106 (values))
108 ;;; Get NODE's environment, assigning one if necessary.
109 (defun get-node-physenv (node)
110 (declare (type node node))
111 (get-lambda-physenv (node-home-lambda node)))
113 ;;; private guts of ADD-LAMBDA-VARS-AND-LET-VARS-TO-CLOSURES
115 ;;; This is the old CMU CL COMPUTE-CLOSURE, which only works on
116 ;;; LAMBDA-VARS directly, not on the LAMBDA-VARS of LAMBDA-LETS. It
117 ;;; seems never to be valid to use this operation alone, so in SBCL,
118 ;;; it's private, and the public interface,
119 ;;; ADD-LAMBDA-VARS-AND-LET-VARS-TO-CLOSURES, always runs over all the
120 ;;; variables, not only the LAMBDA-VARS of CLAMBDA itself but also
121 ;;; the LAMBDA-VARS of CLAMBDA's LAMBDA-LETS.
122 (defun %add-lambda-vars-to-closures (clambda)
123 (let ((physenv (get-lambda-physenv clambda))
124 (did-something nil))
125 (note-unreferenced-fun-vars clambda)
126 (dolist (var (lambda-vars clambda))
127 (dolist (ref (leaf-refs var))
128 (let ((ref-physenv (get-node-physenv ref)))
129 (unless (eq ref-physenv physenv)
130 (when (lambda-var-sets var)
131 (setf (lambda-var-indirect var) t))
132 (setq did-something t)
133 (close-over var ref-physenv physenv))))
134 (dolist (set (basic-var-sets var))
136 ;; Variables which are set but never referenced can be
137 ;; optimized away, and closing over them here would just
138 ;; interfere with that. (In bug 147, it *did* interfere with
139 ;; that, causing confusion later. This UNLESS solves that
140 ;; problem, but I (WHN) am not 100% sure it's best to solve
141 ;; the problem this way instead of somehow solving it
142 ;; somewhere upstream and just doing (AVER (LEAF-REFS VAR))
143 ;; here.)
144 (unless (null (leaf-refs var))
146 (let ((set-physenv (get-node-physenv set)))
147 (unless (eq set-physenv physenv)
148 (setf did-something t
149 (lambda-var-indirect var) t)
150 (close-over var set-physenv physenv))))))
151 did-something))
153 ;;; Find any variables in CLAMBDA -- either directly in LAMBDA-VARS or
154 ;;; in the LAMBDA-VARS of elements of LAMBDA-LETS -- with references
155 ;;; outside of the home environment and close over them. If a
156 ;;; closed-over variable is set, then we set the INDIRECT flag so that
157 ;;; we will know the closed over value is really a pointer to the
158 ;;; value cell. We also warn about unreferenced variables here, just
159 ;;; because it's a convenient place to do it. We return true if we
160 ;;; close over anything.
161 (defun add-lambda-vars-and-let-vars-to-closures (clambda)
162 (declare (type clambda clambda))
163 (let ((did-something nil))
164 (when (%add-lambda-vars-to-closures clambda)
165 (setf did-something t))
166 (dolist (lambda-let (lambda-lets clambda))
167 ;; There's no need to recurse through full COMPUTE-CLOSURE
168 ;; here, since LETS only go one layer deep.
169 (aver (null (lambda-lets lambda-let)))
170 (when (%add-lambda-vars-to-closures lambda-let)
171 (setf did-something t)))
172 did-something))
174 (defun xep-allocator (xep)
175 (let ((entry (functional-entry-fun xep)))
176 (functional-allocator entry)))
178 ;;; Make sure that THING is closed over in REF-PHYSENV and in all
179 ;;; PHYSENVs for the functions that reference REF-PHYSENV's function
180 ;;; (not just calls). HOME-PHYSENV is THING's home environment. When we
181 ;;; reach the home environment, we stop propagating the closure.
182 (defun close-over (thing ref-physenv home-physenv)
183 (declare (type physenv ref-physenv home-physenv))
184 (let ((flooded-physenvs nil))
185 (labels ((flood (flooded-physenv)
186 (unless (or (eql flooded-physenv home-physenv)
187 (member flooded-physenv flooded-physenvs))
188 (push flooded-physenv flooded-physenvs)
189 (unless (memq thing (physenv-closure flooded-physenv))
190 (push thing (physenv-closure flooded-physenv))
191 (let ((lambda (physenv-lambda flooded-physenv)))
192 (cond ((eq (functional-kind lambda) :external)
193 (let* ((alloc-node (xep-allocator lambda))
194 (alloc-lambda (node-home-lambda alloc-node))
195 (alloc-physenv (get-lambda-physenv alloc-lambda)))
196 (flood alloc-physenv)
197 (dolist (ref (leaf-refs lambda))
198 (close-over lambda
199 (get-node-physenv ref) alloc-physenv))))
200 (t (dolist (ref (leaf-refs lambda))
201 ;; FIXME: This assertion looks
202 ;; reasonable, but does not work for
203 ;; :CLEANUPs.
204 #+nil
205 (let ((dest (node-dest ref)))
206 (aver (basic-combination-p dest))
207 (aver (eq (basic-combination-kind dest) :local)))
208 (flood (get-node-physenv ref))))))))))
209 (flood ref-physenv)))
210 (values))
212 ;;; Find LAMBDA-VARs that are marked as needing to support indirect
213 ;;; access (SET at some point after initial creation) that are present
214 ;;; in CLAMBDAs not marked as being DYNAMIC-EXTENT (meaning that the
215 ;;; value-cell involved must be able to survive past the extent of the
216 ;;; allocating frame), and mark them (the LAMBDA-VARs) as needing
217 ;;; explicit value-cells. Because they are already closed-over, the
218 ;;; LAMBDA-VARs already appear in the closures of all of the CLAMBDAs
219 ;;; that need checking.
220 (defun analyze-indirect-lambda-vars (component)
221 (dolist (fun (component-lambdas component))
222 (let ((entry-fun (functional-entry-fun fun)))
223 ;; We also check the ENTRY-FUN, as XEPs for LABELS or FLET
224 ;; functions aren't set to be DX even if their underlying
225 ;; CLAMBDAs are, and if we ever get LET-bound anonymous function
226 ;; DX working, it would mark the XEP as being DX but not the
227 ;; "real" CLAMBDA. This works because a FUNCTIONAL-ENTRY-FUN is
228 ;; either NULL, a self-pointer (for :TOPLEVEL functions), a
229 ;; pointer from an XEP to its underlying function (for :EXTERNAL
230 ;; functions), or a pointer from an underlying function to its
231 ;; XEP (for non-:TOPLEVEL functions with XEPs).
232 (unless (or (leaf-dynamic-extent fun)
233 ;; Functions without XEPs can be treated as if they
234 ;; are DYNAMIC-EXTENT, even without being so
235 ;; declared, as any escaping closure which /isn't/
236 ;; DYNAMIC-EXTENT but calls one of these functions
237 ;; will also close over the required variables, thus
238 ;; forcing the allocation of value cells. Since the
239 ;; XEP is stored in the ENTRY-FUN slot, we can pick
240 ;; off the non-XEP case here.
241 (not entry-fun)
242 (leaf-dynamic-extent entry-fun))
243 (let ((closure (physenv-closure (lambda-physenv fun))))
244 (dolist (var closure)
245 (when (and (lambda-var-p var)
246 (lambda-var-indirect var))
247 (setf (lambda-var-explicit-value-cell var) t))))))))
249 ;;;; non-local exit
251 (defvar *functional-escape-info*)
253 (defun functional-may-escape-p (functional)
254 (binding* ((table (or *functional-escape-info*
255 ;; Many components have no escapes, so we
256 ;; allocate it lazily.
257 (setf *functional-escape-info*
258 (make-hash-table))))
259 ((bool ok) (gethash functional table)))
260 (if ok
261 bool
262 (let ((entry (functional-entry-fun functional)))
263 ;; First stick a NIL in there: break cycles.
264 (setf (gethash functional table) nil)
265 ;; Then compute the real value.
266 (setf (gethash functional table)
268 ;; If the functional has a XEP, it's kind is :EXTERNAL --
269 ;; which means it may escape. ...but if it
270 ;; HAS-EXTERNAL-REFERENCES-P, then that XEP is actually a
271 ;; TL-XEP, which means it's a toplevel function -- which in
272 ;; turn means our search has bottomed out without an escape
273 ;; path. AVER just to make sure, though.
274 (and (eq :external (functional-kind functional))
275 (if (functional-has-external-references-p functional)
276 (aver (eq 'tl-xep (car (functional-debug-name functional))))
278 ;; If it has an entry point that may escape, that just as bad.
279 (and entry (functional-may-escape-p entry))
280 ;; If it has references to it in functions that may escape, that's bad
281 ;; too.
282 (dolist (ref (functional-refs functional) nil)
283 (binding* ((lvar (ref-lvar ref) :exit-if-null)
284 (dest (lvar-dest lvar) :exit-if-null))
285 (when (functional-may-escape-p (node-home-lambda dest))
286 (return t))))))))))
288 (defun exit-should-check-tag-p (exit)
289 (declare (type exit exit))
290 (let ((exit-lambda (lexenv-lambda (node-lexenv exit))))
291 (unless (or
292 ;; Unsafe but fast...
293 (policy exit (zerop check-tag-existence))
294 ;; Dynamic extent is a promise things won't escape --
295 ;; and an explicit request to avoid heap consing.
296 (member (lambda-extent exit-lambda) '(:always-dynamic :maybe-dynamic))
297 ;; If the exit lambda cannot escape, then we should be safe.
298 ;; ...since the escape analysis is kinda new, and not particularly
299 ;; exhaustively tested, let alone proven, disable it for SAFETY 3.
300 (and (policy exit (< safety 3))
301 (not (functional-may-escape-p exit-lambda))))
302 (when (policy exit (> speed safety))
303 (let ((*compiler-error-context* (exit-entry exit)))
304 (compiler-notify "~@<Allocating a value-cell at runtime for ~
305 checking possibly out of extent exit via ~S. Use ~
306 GO/RETURN-FROM with SAFETY 0, or declare the exit ~
307 function DYNAMIC-EXTENT to avoid.~:@>"
308 (node-source-form exit))))
309 t)))
311 ;;; Insert the entry stub before the original exit target, and add a
312 ;;; new entry to the PHYSENV-NLX-INFO. The %NLX-ENTRY call in the
313 ;;; stub is passed the NLX-INFO as an argument so that the back end
314 ;;; knows what entry is being done.
316 ;;; The link from the EXIT block to the entry stub is changed to be a
317 ;;; link from the component head. Similarly, the EXIT block is linked
318 ;;; to the component tail. This leaves the entry stub reachable, but
319 ;;; makes the flow graph less confusing to flow analysis.
321 ;;; If a CATCH or an UNWIND-protect, then we set the LEXENV for the
322 ;;; last node in the cleanup code to be the enclosing environment, to
323 ;;; represent the fact that the binding was undone as a side effect of
324 ;;; the exit. This will cause a lexical exit to be broken up if we are
325 ;;; actually exiting the scope (i.e. a BLOCK), and will also do any
326 ;;; other cleanups that may have to be done on the way.
327 (defun insert-nlx-entry-stub (exit env)
328 (declare (type physenv env) (type exit exit))
329 (let* ((exit-block (node-block exit))
330 (next-block (first (block-succ exit-block)))
331 (entry (exit-entry exit))
332 (cleanup (entry-cleanup entry))
333 (info (make-nlx-info cleanup exit))
334 (new-block (insert-cleanup-code exit-block next-block
335 entry
336 `(%nlx-entry ',info)
337 cleanup))
338 (component (block-component new-block)))
339 (unlink-blocks exit-block new-block)
340 (link-blocks exit-block (component-tail component))
341 (link-blocks (component-head component) new-block)
343 (setf (exit-nlx-info exit) info)
344 (setf (nlx-info-target info) new-block)
345 (setf (nlx-info-safe-p info) (exit-should-check-tag-p exit))
346 (push info (physenv-nlx-info env))
347 (push info (cleanup-info cleanup))
348 (when (member (cleanup-kind cleanup) '(:catch :unwind-protect))
349 (setf (node-lexenv (block-last new-block))
350 (node-lexenv entry))))
352 (values))
354 ;;; Do stuff necessary to represent a non-local exit from the node
355 ;;; EXIT into ENV. This is called for each non-local exit node, of
356 ;;; which there may be several per exit continuation. This is what we
357 ;;; do:
358 ;;; -- If there isn't any NLX-INFO entry in the environment, make
359 ;;; an entry stub, otherwise just move the exit block link to
360 ;;; the component tail.
361 ;;; -- Close over the NLX-INFO in the exit environment.
362 ;;; -- If the exit is from an :ESCAPE function, then substitute a
363 ;;; constant reference to NLX-INFO structure for the escape
364 ;;; function reference. This will cause the escape function to
365 ;;; be deleted (although not removed from the DFO.) The escape
366 ;;; function is no longer needed, and we don't want to emit code
367 ;;; for it.
368 ;;; -- Change the %NLX-ENTRY call to use the NLX lvar so that 1) there
369 ;;; will be a use to represent the NLX use; 2) make life easier for
370 ;;; the stack analysis.
371 (defun note-non-local-exit (env exit)
372 (declare (type physenv env) (type exit exit))
373 (let ((lvar (node-lvar exit))
374 (exit-fun (node-home-lambda exit))
375 (info (find-nlx-info exit)))
376 (cond (info
377 (let ((block (node-block exit)))
378 (aver (= (length (block-succ block)) 1))
379 (unlink-blocks block (first (block-succ block)))
380 (link-blocks block (component-tail (block-component block)))
381 (setf (exit-nlx-info exit) info)
382 (unless (nlx-info-safe-p info)
383 (setf (nlx-info-safe-p info)
384 (exit-should-check-tag-p exit)))))
386 (insert-nlx-entry-stub exit env)
387 (setq info (exit-nlx-info exit))
388 (aver info)))
389 (close-over info (node-physenv exit) env)
390 (when (eq (functional-kind exit-fun) :escape)
391 (mapc (lambda (x)
392 (setf (node-derived-type x) *wild-type*))
393 (leaf-refs exit-fun))
394 (substitute-leaf (find-constant info) exit-fun))
395 (when lvar
396 (let ((node (block-last (nlx-info-target info))))
397 (unless (node-lvar node)
398 (aver (eq lvar (node-lvar exit)))
399 (setf (node-derived-type node) (lvar-derived-type lvar))
400 (add-lvar-use node lvar)))))
401 (values))
403 ;;; Iterate over the EXITs in COMPONENT, calling NOTE-NON-LOCAL-EXIT
404 ;;; when we find a block that ends in a non-local EXIT node. We also
405 ;;; ensure that all EXIT nodes are either non-local or degenerate by
406 ;;; calling IR1-OPTIMIZE-EXIT on local exits. This makes life simpler
407 ;;; for later phases.
408 (defun find-non-local-exits (component)
409 (declare (type component component))
410 (let ((*functional-escape-info* nil))
411 (dolist (lambda (component-lambdas component))
412 (dolist (entry (lambda-entries lambda))
413 (dolist (exit (entry-exits entry))
414 (let ((target-physenv (node-physenv entry)))
415 (if (eq (node-physenv exit) target-physenv)
416 (maybe-delete-exit exit)
417 (note-non-local-exit target-physenv exit)))))))
418 (values))
420 ;;;; final decision on stack allocation of dynamic-extent structures
421 (defun recheck-dynamic-extent-lvars (component)
422 (declare (type component component))
423 (dolist (lambda (component-lambdas component))
424 (loop for entry in (lambda-entries lambda)
425 for cleanup = (entry-cleanup entry)
426 do (when (eq (cleanup-kind cleanup) :dynamic-extent)
427 (collect ((real-dx-lvars))
428 (loop for what in (cleanup-info cleanup)
429 do (etypecase what
430 (cons
431 (let ((dx (car what))
432 (lvar (cdr what)))
433 (cond ((lvar-good-for-dx-p lvar dx component)
434 ;; Since the above check does deep
435 ;; checks. we need to deal with the deep
436 ;; results in here as well.
437 (dolist (cell (handle-nested-dynamic-extent-lvars
438 dx lvar component))
439 (let ((real (principal-lvar (cdr cell))))
440 (setf (lvar-dynamic-extent real) cleanup)
441 (real-dx-lvars real))))
443 (note-no-stack-allocation lvar)
444 (setf (lvar-dynamic-extent lvar) nil)))))
445 (node ; DX closure
446 (let* ((call what)
447 (arg (first (basic-combination-args call)))
448 (funs (lvar-value arg))
449 (dx nil))
450 (dolist (fun funs)
451 (binding* ((() (leaf-dynamic-extent fun)
452 :exit-if-null)
453 (xep (functional-entry-fun fun)
454 :exit-if-null)
455 (closure (physenv-closure
456 (get-lambda-physenv xep))))
457 (cond (closure
458 (setq dx t))
460 (setf (leaf-extent fun) nil)))))
461 (when dx
462 (setf (lvar-dynamic-extent arg) cleanup)
463 (real-dx-lvars arg))))))
464 (let ((real-dx-lvars (delete-duplicates (real-dx-lvars))))
465 (setf (cleanup-info cleanup) real-dx-lvars)
466 (setf (component-dx-lvars component)
467 (append real-dx-lvars (component-dx-lvars component))))))))
468 (values))
470 ;;;; cleanup emission
472 ;;; Zoom up the cleanup nesting until we hit CLEANUP1, accumulating
473 ;;; cleanup code as we go. When we are done, convert the cleanup code
474 ;;; in an implicit MV-PROG1. We have to force local call analysis of
475 ;;; new references to UNWIND-PROTECT cleanup functions. If we don't
476 ;;; actually have to do anything, then we don't insert any cleanup
477 ;;; code. (FIXME: There's some confusion here, left over from CMU CL
478 ;;; comments. CLEANUP1 isn't mentioned in the code of this function.
479 ;;; It is in code elsewhere, but if the comments for this function
480 ;;; mention it they should explain the relationship to the other code.)
482 ;;; If we do insert cleanup code, we check that BLOCK1 doesn't end in
483 ;;; a "tail" local call.
485 ;;; We don't need to adjust the ending cleanup of the cleanup block,
486 ;;; since the cleanup blocks are inserted at the start of the DFO, and
487 ;;; are thus never scanned.
488 (defun emit-cleanups (block1 block2)
489 (declare (type cblock block1 block2))
490 (collect ((code)
491 (reanalyze-funs))
492 (let ((cleanup2 (block-start-cleanup block2)))
493 (do-nested-cleanups (cleanup block1)
494 (when (eq cleanup cleanup2)
495 (return))
496 (let* ((node (cleanup-mess-up cleanup))
497 (args (when (basic-combination-p node)
498 (basic-combination-args node))))
499 (ecase (cleanup-kind cleanup)
500 (:special-bind
501 (code `(%special-unbind ',(lvar-value (first args)))))
502 (:catch
503 (code `(%catch-breakup)))
504 (:unwind-protect
505 (code `(%unwind-protect-breakup))
506 (let ((fun (ref-leaf (lvar-uses (second args)))))
507 (reanalyze-funs fun)
508 (code `(%funcall ,fun))))
509 ((:block :tagbody)
510 (dolist (nlx (cleanup-info cleanup))
511 (code `(%lexical-exit-breakup ',nlx))))
512 (:dynamic-extent
513 (when (cleanup-info cleanup)
514 (code `(%cleanup-point))))))))
516 (when (code)
517 (aver (not (node-tail-p (block-last block1))))
518 (insert-cleanup-code
519 block1 block2 (block-last block1) `(progn ,@(code)))
520 (dolist (fun (reanalyze-funs))
521 (locall-analyze-fun-1 fun))))
523 (values))
525 ;;; Loop over the blocks in COMPONENT, calling EMIT-CLEANUPS when we
526 ;;; see a successor in the same environment with a different cleanup.
527 ;;; We ignore the cleanup transition if it is to a cleanup enclosed by
528 ;;; the current cleanup, since in that case we are just messing up the
529 ;;; environment, hence this is not the place to clean it.
530 (defun find-cleanup-points (component)
531 (declare (type component component))
532 (do-blocks (block1 component)
533 (let ((env1 (block-physenv block1))
534 (cleanup1 (block-end-cleanup block1)))
535 (dolist (block2 (block-succ block1))
536 (when (block-start block2)
537 (let ((env2 (block-physenv block2))
538 (cleanup2 (block-start-cleanup block2)))
539 (unless (or (not (eq env2 env1))
540 (eq cleanup1 cleanup2)
541 (and cleanup2
542 (eq (node-enclosing-cleanup
543 (cleanup-mess-up cleanup2))
544 cleanup1)))
545 (emit-cleanups block1 block2)))))))
546 (values))
548 ;;; Mark optimizable tail-recursive uses of function result
549 ;;; continuations with the corresponding TAIL-SET.
551 ;;; Regarding the suppression of TAIL-P for nil-returning calls,
552 ;;; a partial history of the changes affecting this is as follows:
554 ;;; WHN said [in 85f9c92558538b85540ff420fa8970af91e241a2]
555 ;;; ;; Nodes whose type is NIL (i.e. don't return) such as calls to
556 ;;; ;; ERROR are never annotated as TAIL-P, in order to preserve
557 ;;; ;; debugging information.
559 ;;; NS added [in bea5b384106a6734a4b280a76e8ebdd4d51b5323]
560 ;;; ;; Why is that bad? Because this non-elimination of
561 ;;; ;; non-returning tail calls causes the XEP for FOO [to] appear in
562 ;;; ;; backtrace for (defun foo (x) (error "foo ~S" x)) w[h]ich seems
563 ;;; ;; less then optimal. --NS 2005-02-28
564 ;;; (not considering that the point of non-elimination was specifically
565 ;;; to allow FOO to appear in the backtrace?)
567 (defun tail-annotate (component)
568 (declare (type component component))
569 (dolist (fun (component-lambdas component))
570 (let ((ret (lambda-return fun)))
571 ;; The code below assumes that a lambda whose final node is a call to
572 ;; a non-returning function gets a lambda-return. But it doesn't always,
573 ;; and it's not clear whether that means "always doesn't".
574 ;; If it never does, then (WHEN RET ..) will never execute, so we won't
575 ;; even see the call that might be be annotated as tail-p, regardless
576 ;; of whether we *want* to annotate it as such.
577 (when ret
578 (let ((result (return-result ret)))
579 (do-uses (use result)
580 (when (and (basic-combination-p use)
581 (immediately-used-p result use)
582 (or (eq (basic-combination-kind use) :local)
583 ;; Nodes whose type is NIL (i.e. don't return) such
584 ;; as calls to ERROR are never annotated as TAIL-P,
585 ;; in order to preserve debugging information, so that
587 ;; We spread this net wide enough to catch
588 ;; untrusted NIL return types as well, so that
589 ;; frames calling functions such as FOO-ERROR are
590 ;; kept in backtraces:
592 ;; (defun foo-error (x) (error "oops: ~S" x))
594 (not (or (eq *empty-type* (node-derived-type use))
595 (eq *empty-type* (combination-defined-type use))))))
596 (setf (node-tail-p use) t)))))))
597 ;; The above loop does not find all calls to ERROR.
598 (do-blocks (block component)
599 (do-nodes (node lvar block)
600 ;; CAUTION: This looks scary because it affects all known nil-returning
601 ;; calls even if not in tail position. Use of the policy quality which
602 ;; enables tail-p must be confined to a very restricted lexical scope.
603 ;; This might be better implemented as a local declaration about
604 ;; function names at the call site: (declare (uninhibit-tco error))
605 ;; but adding new kinds of declarations is fairly invasive surgery.
606 (when (and (combination-p node)
607 (combination-fun-info node) ; must be a known fun
608 (eq (combination-defined-type node) *empty-type*)
609 (policy node (= allow-non-returning-tail-call 3)))
610 (setf (node-tail-p node) t))))
611 (values))