1 ;;;; This file contains the code that finds the initial components and
2 ;;;; DFO, and recomputes the DFO if it is invalidated.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;; Find the DFO for a component, deleting any unreached blocks and
16 ;;; merging any other components we reach. We repeatedly iterate over
17 ;;; the entry points, since new ones may show up during the walk.
18 (declaim (ftype (function (component) (values)) find-dfo))
19 (defun find-dfo (component)
20 (clear-flags component)
21 (setf (component-reanalyze component) nil)
22 (let ((head (component-head component)))
24 ((dolist (ep (block-succ head) t)
25 (unless (block-flag ep)
26 (find-dfo-aux ep head component)
29 (declare (fixnum num))
30 (do-blocks-backwards (block component :both)
31 (if (block-flag block)
32 (setf (block-number block) (incf num))
33 (setf (block-delete-p block) t)))
34 (do-blocks (block component)
35 (unless (block-flag block)
36 (delete-block block))))
39 ;;; Move all the code and entry points from OLD to NEW. The code in
40 ;;; OLD is inserted at the head of NEW. This is also called during LET
41 ;;; conversion when we are about in insert the body of a LET in a
42 ;;; different component. [A local call can be to a different component
43 ;;; before FIND-INITIAL-DFO runs.]
44 (declaim (ftype (function (component component) (values)) join-components))
45 (defun join-components (new old)
46 (aver (eq (component-kind new) (component-kind old)))
47 (let ((old-head (component-head old))
48 (old-tail (component-tail old))
49 (head (component-head new))
50 (tail (component-tail new)))
52 (do-blocks (block old)
53 (setf (block-flag block) nil)
54 (setf (block-component block) new))
56 (let ((old-next (block-next old-head))
57 (old-last (block-prev old-tail))
58 (next (block-next head)))
59 (unless (eq old-next old-tail)
60 (setf (block-next head) old-next)
61 (setf (block-prev old-next) head)
63 (setf (block-prev next) old-last)
64 (setf (block-next old-last) next))
66 (setf (block-next old-head) old-tail)
67 (setf (block-prev old-tail) old-head))
69 (setf (component-lambdas new)
70 (nconc (component-lambdas old) (component-lambdas new)))
71 (setf (component-lambdas old) nil)
72 (setf (component-new-funs new) (nconc (component-new-funs old)
73 (component-new-funs new))
74 (component-new-funs old) nil)
76 (dolist (xp (block-pred old-tail))
77 (unlink-blocks xp old-tail)
78 (link-blocks xp tail))
79 (dolist (ep (block-succ old-head))
80 (unlink-blocks old-head ep)
81 (link-blocks head ep)))
84 ;;; Do a depth-first walk from BLOCK, inserting ourself in the DFO
85 ;;; after HEAD. If we somehow find ourselves in another component,
86 ;;; then we join that component to our component.
87 (declaim (ftype (function (cblock cblock component) (values)) find-dfo-aux))
88 (defun find-dfo-aux (block head component)
89 (unless (eq (block-component block) component)
90 (join-components component (block-component block)))
91 (unless (block-flag block)
92 (setf (block-flag block) t)
93 (dolist (succ (block-succ block))
94 (find-dfo-aux succ head component))
95 (remove-from-dfo block)
96 (add-to-dfo block head))
99 ;;; This function is called on each block by FIND-INITIAL-DFO-AUX
100 ;;; before it walks the successors. It looks at the home CLAMBDA's
101 ;;; BIND block to see whether that block is in some other component:
102 ;;; -- If the block is in the initial component, then do
103 ;;; DFO-WALK-DEPENDENCY-GRAPH on the home function to move it
105 ;;; -- If the block is in some other component, join COMPONENT into
106 ;;; it and return that component.
107 ;;; -- If the home function is deleted, do nothing. BLOCK must
108 ;;; eventually be discovered to be unreachable as well. This can
109 ;;; happen when we have a NLX into a function with no references.
110 ;;; The escape function still has refs (in the deleted function).
112 ;;; This ensures that all the blocks in a given environment will be in
113 ;;; the same component, even when they might not seem reachable from
114 ;;; the environment entry. Consider the case of code that is only
115 ;;; reachable from a non-local exit.
116 (defun scavenge-home-dependency-graph (block component)
117 (declare (type cblock block) (type component component))
118 (let ((home-lambda (block-home-lambda block)))
119 (if (eq (functional-kind home-lambda) :deleted)
121 (let ((home-component (lambda-component home-lambda)))
122 (cond ((eq (component-kind home-component) :initial)
123 (dfo-scavenge-dependency-graph home-lambda component))
124 ((eq home-component component)
127 (join-components home-component component)
130 ;;; This is somewhat similar to FIND-DFO-AUX, except that it merges
131 ;;; the current component with any strange component, rather than the
132 ;;; other way around. This is more efficient in the common case where
133 ;;; the current component doesn't have much stuff in it.
135 ;;; We return the current component as a result, allowing the caller
136 ;;; to detect when the old current component has been merged with
139 ;;; We walk blocks in initial components as though they were already
140 ;;; in the current component, moving them to the current component in
141 ;;; the process. The blocks are inserted at the head of the current
143 (defun find-initial-dfo-aux (block component)
144 (declare (type cblock block) (type component component))
145 (let ((this (block-component block)))
147 ((not (or (eq this component)
148 (eq (component-kind this) :initial)))
149 (join-components this component)
151 ((block-flag block) component)
153 (setf (block-flag block) t)
154 (let ((current (scavenge-home-dependency-graph block component)))
155 (dolist (succ (block-succ block))
156 (setq current (find-initial-dfo-aux succ current)))
157 (remove-from-dfo block)
158 (add-to-dfo block (component-head current))
161 ;;; Return a list of all the home lambdas that reference FUN (may
162 ;;; contain duplications).
164 ;;; References to functions which local call analysis could not (or
165 ;;; were chosen not) to local call convert will appear as references
166 ;;; to XEP lambdas. We can ignore references to XEPs that appear in
167 ;;; :TOPLEVEL components, since environment analysis goes to special
168 ;;; effort to allow closing over of values from a separate top level
169 ;;; component. (And now that HAS-EXTERNAL-REFERENCES-P-ness
170 ;;; generalizes :TOPLEVEL-ness, we ignore those too.) All other
171 ;;; references must cause components to be joined.
173 ;;; References in deleted functions are also ignored, since this code
174 ;;; will be deleted eventually.
175 (defun find-reference-funs (fun)
177 (dolist (ref (leaf-refs fun))
178 (let* ((home (node-home-lambda ref))
179 (home-kind (functional-kind home))
180 (home-externally-visible-p
181 (or (eq home-kind :toplevel)
182 (functional-has-external-references-p home))))
183 (unless (or (and home-externally-visible-p
184 (eq (functional-kind fun) :external))
185 (eq home-kind :deleted))
189 ;;; If CLAMBDA is not already in COMPONENT, just return that
190 ;;; component. Otherwise, move the code for CLAMBDA and all lambdas it
191 ;;; physically depends on (either because of calls or because of
192 ;;; closure relationships) into COMPONENT, or possibly into another
193 ;;; COMPONENT that we find to be related. Return whatever COMPONENT we
194 ;;; actually merged into.
196 ;;; (Note: The analogous CMU CL code only scavenged call-based
197 ;;; dependencies, not closure dependencies. That seems to've been by
198 ;;; oversight, not by design, as per the bug reported by WHN on
199 ;;; cmucl-imp ca. 2001-11-29 and explained by DTC shortly after.)
201 ;;; FIXME: Very likely we should be scavenging NLX-based dependencies
202 ;;; here too. OTOH, there's a lot of global weirdness in NLX handling,
203 ;;; so it might be taken care of some other way that I haven't figured
204 ;;; out yet. Perhaps the best way to address this would be to try to
205 ;;; construct a NLX-based test case which fails in the same way as the
206 ;;; closure-based test case on cmucl-imp 2001-11-29.)
208 ;;; If the function is in an initial component, then we move its head
209 ;;; and tail to COMPONENT and add it to COMPONENT's lambdas. It is
210 ;;; harmless to move the tail (even though the return might be
211 ;;; unreachable) because if the return is unreachable it (and its
212 ;;; successor link) will be deleted in the post-deletion pass.
214 ;;; We then do a FIND-DFO-AUX starting at the head of CLAMBDA. If this
215 ;;; flow-graph walk encounters another component (which can only
216 ;;; happen due to a non-local exit), then we move code into that
217 ;;; component instead. We then recurse on all functions called from
218 ;;; CLAMBDA, moving code into whichever component the preceding call
221 ;;; If CLAMBDA is in the initial component, but the BLOCK-FLAG is set
222 ;;; in the bind block, then we just return COMPONENT, since we must
223 ;;; have already reached this function in the current walk (or the
224 ;;; component would have been changed).
226 ;;; If the function is an XEP, then we also walk all functions that
227 ;;; contain references to the XEP. This is done so that environment
228 ;;; analysis doesn't need to cross component boundaries. This also
229 ;;; ensures that conversion of a full call to a local call won't
230 ;;; result in a need to join components, since the components will
232 (defun dfo-scavenge-dependency-graph (clambda component)
233 (declare (type clambda clambda) (type component component))
234 (assert (not (eql (lambda-kind clambda) :deleted)))
235 (let* ((bind-block (node-block (lambda-bind clambda)))
236 (old-lambda-component (block-component bind-block))
237 (return (lambda-return clambda)))
239 ((eq old-lambda-component component)
241 ((not (eq (component-kind old-lambda-component) :initial))
242 (join-components old-lambda-component component)
243 old-lambda-component)
244 ((block-flag bind-block)
247 (push clambda (component-lambdas component))
248 (setf (component-lambdas old-lambda-component)
249 (delete clambda (component-lambdas old-lambda-component)))
250 (link-blocks (component-head component) bind-block)
251 (unlink-blocks (component-head old-lambda-component) bind-block)
253 (let ((return-block (node-block return)))
254 (link-blocks return-block (component-tail component))
255 (unlink-blocks return-block (component-tail old-lambda-component))))
256 (let ((res (find-initial-dfo-aux bind-block component)))
257 (declare (type component res))
258 ;; Scavenge related lambdas.
259 (labels ((scavenge-lambda (clambda)
261 (dfo-scavenge-dependency-graph (lambda-home clambda)
263 (scavenge-possibly-deleted-lambda (clambda)
264 (unless (eql (lambda-kind clambda) :deleted)
265 (scavenge-lambda clambda)))
266 ;; Scavenge call relationship.
267 (scavenge-call (called-lambda)
268 (scavenge-lambda called-lambda))
269 ;; Scavenge closure over a variable: if CLAMBDA
270 ;; refers to a variable whose home lambda is not
271 ;; CLAMBDA, then the home lambda should be in the
272 ;; same component as CLAMBDA. (sbcl-0.6.13, and CMU
273 ;; CL, didn't do this, leading to the occasional
274 ;; failure when physenv analysis, which is local to
275 ;; each component, would bogusly conclude that a
276 ;; closed-over variable was unused and thus delete
277 ;; it. See e.g. cmucl-imp 2001-11-29.)
278 (scavenge-closure-var (var)
279 (unless (null (lambda-var-refs var)) ; unless var deleted
280 (let ((var-home-home (lambda-home (lambda-var-home var))))
281 (scavenge-possibly-deleted-lambda var-home-home))))
282 ;; Scavenge closure over an entry for nonlocal exit.
283 ;; This is basically parallel to closure over a
285 (scavenge-entry (entry)
286 (declare (type entry entry))
287 (let ((entry-home (node-home-lambda entry)))
288 (scavenge-possibly-deleted-lambda entry-home))))
289 (dolist (cc (lambda-calls-or-closes clambda))
291 (clambda (scavenge-call cc))
292 (lambda-var (scavenge-closure-var cc))
293 (entry (scavenge-entry cc))))
294 (when (eq (lambda-kind clambda) :external)
295 (mapc #'scavenge-call (find-reference-funs clambda))))
299 ;;; Return true if CLAMBDA either is an XEP or has EXITS to some of
301 (defun has-xep-or-nlx (clambda)
302 (declare (type clambda clambda))
303 (or (eq (functional-kind clambda) :external)
304 (let ((entries (lambda-entries clambda)))
306 (find-if #'entry-exits entries)))))
308 ;;; Compute the result of FIND-INITIAL-DFO given the list of all
309 ;;; resulting components. Components with a :TOPLEVEL lambda, but no
310 ;;; normal XEPs or potential non-local exits are marked as :TOPLEVEL.
311 ;;; If there is a :TOPLEVEL lambda, and also a normal XEP, then we
312 ;;; treat the component as normal, but also return such components in
313 ;;; a list as the third value. Components with no entry of any sort
315 (defun separate-toplevelish-components (components)
316 (declare (list components))
320 (dolist (component components)
321 (unless (eq (block-next (component-head component))
322 (component-tail component))
323 (let* ((funs (component-lambdas component))
324 (has-top (find :toplevel funs :key #'functional-kind))
325 (has-external-references
326 (some #'functional-has-external-references-p funs)))
327 (cond (;; The FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P concept
328 ;; is newer than the rest of this function, and
329 ;; doesn't really seem to fit into its mindset. Here
330 ;; we mark components which contain such FUNCTIONs
331 ;; them as :COMPLEX-TOPLEVEL, since they do get
332 ;; executed at run time, and since it's not valid to
333 ;; delete them just because they don't have any
334 ;; references from pure :TOPLEVEL components. -- WHN
335 has-external-references
336 (setf (component-kind component) :complex-toplevel)
338 (real-top component))
339 ((or (some #'has-xep-or-nlx funs)
340 (and has-top (rest funs)))
341 (setf (component-name component)
342 (find-component-name component))
345 (setf (component-kind component) :complex-toplevel)
346 (real-top component)))
348 (setf (component-kind component) :toplevel)
349 (setf (component-name component) "top level form")
352 (delete-component component))))))
354 (values (real) (top) (real-top))))
356 ;; COMPONENTs want strings for names, LEAF-DEBUG-NAMEs mightn't be
358 (defun component-name-from-functional-debug-name (functional)
359 (declare (type functional functional))
360 (let ((leaf-debug-name (leaf-debug-name functional)))
362 (if (stringp leaf-debug-name)
364 (debug-namify "function ~S" leaf-debug-name)))))
366 ;;; Given a list of top level lambdas, return
367 ;;; (VALUES NONTOP-COMPONENTS TOP-COMPONENTS HAIRY-TOP-COMPONENTS).
368 ;;; Each of the three values returned is a list of COMPONENTs:
369 ;;; NONTOP-COMPONENTS = non-top-level-ish COMPONENTs;
370 ;;; TOP-COMPONENTS = top-level-ish COMPONENTs;
371 ;;; HAIRY-TOP-COMPONENTS = a subset of NONTOP-COMPONENTS, those
372 ;;; elements which include a top-level-ish lambda.
374 ;;; We assign the DFO for each component, and delete any unreachable
375 ;;; blocks. We assume that the FLAGS have already been cleared.
376 (defun find-initial-dfo (toplevel-lambdas)
377 (declare (list toplevel-lambdas))
378 (collect ((components))
379 ;; We iterate over the lambdas in each initial component, trying
380 ;; to put each function in its own component, but joining it to
381 ;; an existing component if we find that there are references
382 ;; between them. Any code that is left in an initial component
383 ;; must be unreachable, so we can delete it. Stray links to the
384 ;; initial component tail (due NIL function terminated blocks)
385 ;; are moved to the appropriate new component tail.
386 (dolist (toplevel-lambda toplevel-lambdas)
387 (let* ((block (lambda-block toplevel-lambda))
388 (old-component (block-component block))
389 (old-component-lambdas (component-lambdas old-component))
391 (aver (member toplevel-lambda old-component-lambdas))
392 (dolist (component-lambda old-component-lambdas)
393 (aver (member (functional-kind component-lambda)
394 '(:optional :external :toplevel nil :escape
396 (unless new-component
397 (setf new-component (make-empty-component))
398 (setf (component-name new-component)
399 ;; This isn't necessarily an ideal name for the
400 ;; component, since it might end up with multiple
401 ;; lambdas in it, not just this one, but it does
402 ;; seem a better name than just "<unknown>".
403 (component-name-from-functional-debug-name
405 (let ((res (dfo-scavenge-dependency-graph component-lambda
407 (when (eq res new-component)
408 (aver (not (position new-component (components))))
409 (components new-component)
410 (setq new-component nil))))
411 (when (eq (component-kind old-component) :initial)
412 (aver (null (component-lambdas old-component)))
413 (let ((tail (component-tail old-component)))
414 (dolist (pred (block-pred tail))
415 (let ((pred-component (block-component pred)))
416 (unless (eq pred-component old-component)
417 (unlink-blocks pred tail)
418 (link-blocks pred (component-tail pred-component))))))
419 (delete-component old-component))))
421 ;; When we are done, we assign DFNs.
422 (dolist (component (components))
424 (declare (fixnum num))
425 (do-blocks-backwards (block component :both)
426 (setf (block-number block) (incf num)))))
428 ;; Pull out top-level-ish code.
429 (separate-toplevelish-components (components))))
431 ;;; Insert the code in LAMBDA at the end of RESULT-LAMBDA.
432 (defun merge-1-toplevel-lambda (result-lambda lambda)
433 (declare (type clambda result-lambda lambda))
435 ;; Delete the lambda, and combine the LETs and entries.
436 (setf (functional-kind lambda) :deleted)
437 (dolist (let (lambda-lets lambda))
438 (setf (lambda-home let) result-lambda)
439 (setf (lambda-physenv let) (lambda-physenv result-lambda))
440 (push let (lambda-lets result-lambda)))
441 (setf (lambda-entries result-lambda)
442 (nconc (lambda-entries result-lambda)
443 (lambda-entries lambda)))
445 (let* ((bind (lambda-bind lambda))
446 (bind-block (node-block bind))
447 (component (block-component bind-block))
448 (result-component (lambda-component result-lambda))
449 (result-return-block (node-block (lambda-return result-lambda))))
451 ;; Move blocks into the new COMPONENT, and move any nodes directly
452 ;; in the old LAMBDA into the new one (with LETs implicitly moved
453 ;; by changing their home.)
454 (do-blocks (block component)
455 (do-nodes (node cont block)
456 (let ((lexenv (node-lexenv node)))
457 (when (eq (lexenv-lambda lexenv) lambda)
458 (setf (lexenv-lambda lexenv) result-lambda))))
459 (setf (block-component block) result-component))
461 ;; Splice the blocks into the new DFO, and unlink them from the
462 ;; old component head and tail. Non-return blocks that jump to the
463 ;; tail (NIL-returning calls) are switched to go to the new tail.
464 (let* ((head (component-head component))
465 (first (block-next head))
466 (tail (component-tail component))
467 (last (block-prev tail))
468 (prev (block-prev result-return-block)))
469 (setf (block-next prev) first)
470 (setf (block-prev first) prev)
471 (setf (block-next last) result-return-block)
472 (setf (block-prev result-return-block) last)
473 (dolist (succ (block-succ head))
474 (unlink-blocks head succ))
475 (dolist (pred (block-pred tail))
476 (unlink-blocks pred tail)
477 (let ((last (block-last pred)))
478 (unless (return-p last)
479 (aver (basic-combination-p last))
480 (link-blocks pred (component-tail result-component))))))
482 (let ((lambdas (component-lambdas component)))
483 (aver (and (null (rest lambdas))
484 (eq (first lambdas) lambda))))
486 ;; Switch the end of the code from the return block to the start of
488 (dolist (pred (block-pred result-return-block))
489 (unlink-blocks pred result-return-block)
490 (link-blocks pred bind-block))
493 ;; If there is a return, then delete it (making the preceding node
494 ;; the last node) and link the block to the result return. There
495 ;; is always a preceding REF NIL node in top level lambdas.
496 (let ((return (lambda-return lambda)))
498 (let ((return-block (node-block return))
499 (result (return-result return)))
500 (setf (block-last return-block) (continuation-use result))
502 (delete-continuation result)
503 (link-blocks return-block result-return-block))))))
505 ;;; Given a non-empty list of top level LAMBDAs, smash them into a
506 ;;; top level lambda and component, returning these as values. We use
507 ;;; the first lambda and its component, putting the other code in that
508 ;;; component and deleting the other lambdas.
509 (defun merge-toplevel-lambdas (lambdas)
510 (declare (cons lambdas))
511 (let* ((result-lambda (first lambdas))
512 (result-return (lambda-return result-lambda)))
516 ;; Make sure the result's return node starts a block so that we
517 ;; can splice code in before it.
518 (let ((prev (node-prev
520 (return-result result-return)))))
521 (when (continuation-use prev)
522 (node-ends-block (continuation-use prev)))
524 (let ((new (make-continuation)))
525 (delete-continuation-use use)
526 (add-continuation-use use new))))
528 (dolist (lambda (rest lambdas))
529 (merge-1-toplevel-lambda result-lambda lambda)))
531 (dolist (lambda (rest lambdas))
532 (setf (functional-entry-fun lambda) nil)
533 (delete-component (lambda-component lambda)))))
535 (values (lambda-component result-lambda) result-lambda)))