0.pre7.86:
[sbcl.git] / src / compiler / dfo.lisp
1 ;;;; This file contains the code that finds the initial components and
2 ;;;; DFO, and recomputes the DFO if it is invalidated.
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!C")
14
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)))
23     (do ()
24         ((dolist (ep (block-succ head) t)
25            (unless (block-flag ep)
26              (find-dfo-aux ep head component)
27              (return nil))))))
28
29   (let ((num 0))
30     (declare (fixnum num))
31     (do-blocks-backwards (block component :both)
32       (if (block-flag block)
33           (setf (block-number block) (incf num))
34           (setf (block-delete-p block) t)))
35     (do-blocks (block component)
36       (unless (block-flag block)
37         (delete-block block))))
38   (values))
39
40 ;;; Move all the code and entry points from OLD to NEW. The code in
41 ;;; OLD is inserted at the head of NEW. This is also called during LET
42 ;;; conversion when we are about in insert the body of a LET in a
43 ;;; different component. [A local call can be to a different component
44 ;;; before FIND-INITIAL-DFO runs.]
45 (declaim (ftype (function (component component) (values)) join-components))
46 (defun join-components (new old)
47   (aver (eq (component-kind new) (component-kind old)))
48   (let ((old-head (component-head old))
49         (old-tail (component-tail old))
50         (head (component-head new))
51         (tail (component-tail new)))
52
53     (do-blocks (block old)
54       (setf (block-flag block) nil)
55       (setf (block-component block) new))
56
57     (let ((old-next (block-next old-head))
58           (old-last (block-prev old-tail))
59           (next (block-next head)))
60       (unless (eq old-next old-tail)
61         (setf (block-next head) old-next)
62         (setf (block-prev old-next) head)
63         
64         (setf (block-prev next) old-last)
65         (setf (block-next old-last) next))
66
67       (setf (block-next old-head) old-tail)
68       (setf (block-prev old-tail) old-head))
69
70     (setf (component-lambdas new)
71           (nconc (component-lambdas old) (component-lambdas new)))
72     (setf (component-lambdas old) ())
73     (setf (component-new-functions new)
74           (nconc (component-new-functions old) (component-new-functions new)))
75     (setf (component-new-functions old) ())
76
77     (dolist (xp (block-pred old-tail))
78       (unlink-blocks xp old-tail)
79       (link-blocks xp tail))
80     (dolist (ep (block-succ old-head))
81       (unlink-blocks old-head ep)
82       (link-blocks head ep)))
83   (values))
84
85 ;;; Do a depth-first walk from BLOCK, inserting ourself in the DFO
86 ;;; after HEAD. If we somehow find ourselves in another component,
87 ;;; then we join that component to our component.
88 (declaim (ftype (function (cblock cblock component) (values)) find-dfo-aux))
89 (defun find-dfo-aux (block head component)
90   (unless (eq (block-component block) component)
91     (join-components component (block-component block)))
92         
93   (unless (block-flag block)
94     (setf (block-flag block) t)
95     (dolist (succ (block-succ block))
96       (find-dfo-aux succ head component))
97
98     (remove-from-dfo block)
99     (add-to-dfo block head))
100   (values))
101
102 ;;; This function is called on each block by FIND-INITIAL-DFO-AUX
103 ;;; before it walks the successors. It looks at the home lambda's bind
104 ;;; block to see whether that block is in some other component:
105
106 ;;; -- If the block is in the initial component, then do
107 ;;;    DFO-WALK-CALL-GRAPH on the home function to move it
108 ;;;    into COMPONENT.
109 ;;; -- If the block is in some other component, join COMPONENT into
110 ;;;    it and return that component.
111 ;;; -- If the home function is deleted, do nothing. BLOCK must
112 ;;;    eventually be discovered to be unreachable as well. This can
113 ;;;    happen when we have a NLX into a function with no references.
114 ;;;    The escape function still has refs (in the deleted function).
115 ;;;
116 ;;; This ensures that all the blocks in a given environment will be in
117 ;;; the same component, even when they might not seem reachable from
118 ;;; the environment entry. Consider the case of code that is only
119 ;;; reachable from a non-local exit.
120 (defun walk-home-call-graph (block component)
121   (declare (type cblock block) (type component component))
122   (let ((home (block-home-lambda block)))
123     (if (eq (functional-kind home) :deleted)
124         component
125         (let* ((bind-block (node-block (lambda-bind home)))
126                (home-component (block-component bind-block)))
127           (cond ((eq (component-kind home-component) :initial)
128                  (dfo-walk-call-graph home component))
129                 ((eq home-component component)
130                  component)
131                 (t
132                  (join-components home-component component)
133                  home-component))))))
134
135 ;;; This is somewhat similar to FIND-DFO-AUX, except that it merges
136 ;;; the current component with any strange component, rather than the
137 ;;; other way around. This is more efficient in the common case where
138 ;;; the current component doesn't have much stuff in it.
139 ;;;
140 ;;; We return the current component as a result, allowing the caller
141 ;;; to detect when the old current component has been merged with
142 ;;; another.
143 ;;;
144 ;;; We walk blocks in initial components as though they were already
145 ;;; in the current component, moving them to the current component in
146 ;;; the process. The blocks are inserted at the head of the current
147 ;;; component.
148 (defun find-initial-dfo-aux (block component)
149   (declare (type cblock block) (type component component))
150   (let ((this (block-component block)))
151     (cond
152      ((not (or (eq this component)
153                (eq (component-kind this) :initial)))
154       (join-components this component)
155       this)
156      ((block-flag block) component)
157      (t
158       (setf (block-flag block) t)
159       (let ((current (walk-home-call-graph block component)))
160         (dolist (succ (block-succ block))
161           (setq current (find-initial-dfo-aux succ current)))
162         
163         (remove-from-dfo block)
164         (add-to-dfo block (component-head current))
165         current)))))
166
167 ;;; Return a list of all the home lambdas that reference FUN (may
168 ;;; contain duplications).
169 ;;;
170 ;;; References to functions which local call analysis could not (or
171 ;;; were chosen not) to local call convert will appear as references
172 ;;; to XEP lambdas. We can ignore references to XEPs that appear in
173 ;;; :TOPLEVEL components, since environment analysis goes to special
174 ;;; effort to allow closing over of values from a separate top level
175 ;;; component. (And now that HAS-EXTERNAL-REFERENCES-P-ness
176 ;;; generalizes :TOPLEVEL-ness, we ignore those too.) All other
177 ;;; references must cause components to be joined.
178 ;;;
179 ;;; References in deleted functions are also ignored, since this code
180 ;;; will be deleted eventually.
181 (defun find-reference-functions (fun)
182   (collect ((res))
183     (dolist (ref (leaf-refs fun))
184       (let* ((home (node-home-lambda ref))
185              (home-kind (functional-kind home))
186              (home-externally-visible-p
187               (or (eq home-kind :toplevel)
188                   (functional-has-external-references-p home))))
189         (unless (or (and home-externally-visible-p
190                          (eq (functional-kind fun) :external))
191                     (eq home-kind :deleted))
192           (res home))))
193     (res)))
194
195 ;;; Move the code for FUN and all functions called by it into
196 ;;; COMPONENT. If FUN is already in COMPONENT, then we just return
197 ;;; that component.
198 ;;;
199 ;;; If the function is in an initial component, then we move its head
200 ;;; and tail to COMPONENT and add it to COMPONENT's lambdas. It is
201 ;;; harmless to move the tail (even though the return might be
202 ;;; unreachable) because if the return is unreachable it (and its
203 ;;; successor link) will be deleted in the post-deletion pass.
204 ;;;
205 ;;; We then do a FIND-DFO-AUX starting at the head of FUN. If this
206 ;;; flow-graph walk encounters another component (which can only
207 ;;; happen due to a non-local exit), then we move code into that
208 ;;; component instead. We then recurse on all functions called from
209 ;;; FUN, moving code into whichever component the preceding call
210 ;;; returned.
211 ;;;
212 ;;; If FUN is in the initial component, but the BLOCK-FLAG is set in
213 ;;; the bind block, then we just return COMPONENT, since we must have
214 ;;; already reached this function in the current walk (or the
215 ;;; component would have been changed).
216 ;;;
217 ;;; If the function is an XEP, then we also walk all functions that
218 ;;; contain references to the XEP. This is done so that environment
219 ;;; analysis doesn't need to cross component boundaries. This also
220 ;;; ensures that conversion of a full call to a local call won't
221 ;;; result in a need to join components, since the components will
222 ;;; already be one.
223 (defun dfo-walk-call-graph (fun component)
224   (declare (type clambda fun) (type component component))
225   (let* ((bind-block (node-block (lambda-bind fun)))
226          (this (block-component bind-block))
227          (return (lambda-return fun)))
228     (cond
229      ((eq this component) component)
230      ((not (eq (component-kind this) :initial))
231       (join-components this component)
232       this)
233      ((block-flag bind-block)
234       component)
235      (t
236       (push fun (component-lambdas component))
237       (setf (component-lambdas this)
238             (delete fun (component-lambdas this)))
239       (link-blocks (component-head component) bind-block)
240       (unlink-blocks (component-head this) bind-block)
241       (when return
242         (let ((return-block (node-block return)))
243           (link-blocks return-block (component-tail component))
244           (unlink-blocks return-block (component-tail this))))
245       (let ((calls (if (eq (functional-kind fun) :external)
246                        (append (find-reference-functions fun)
247                                (lambda-calls fun))
248                        (lambda-calls fun))))
249         (do ((res (find-initial-dfo-aux bind-block component)
250                   (dfo-walk-call-graph (first funs) res))
251              (funs calls (rest funs)))
252             ((null funs) res)
253           (declare (type component res))))))))
254
255 ;;; Return true if FUN is either an XEP or has EXITS to some of its
256 ;;; ENTRIES.
257 (defun has-xep-or-nlx (fun)
258   (declare (type clambda fun))
259   (or (eq (functional-kind fun) :external)
260       (let ((entries (lambda-entries fun)))
261         (and entries
262              (find-if #'entry-exits entries)))))
263
264 ;;; Compute the result of FIND-INITIAL-DFO given the list of all
265 ;;; resulting components. Components with a :TOPLEVEL lambda, but no
266 ;;; normal XEPs or potential non-local exits are marked as :TOPLEVEL.
267 ;;; If there is a :TOPLEVEL lambda, and also a normal XEP, then we
268 ;;; treat the component as normal, but also return such components in
269 ;;; a list as the third value. Components with no entry of any sort
270 ;;; are deleted.
271 (defun find-toplevel-components (components)
272   (declare (list components))
273   (collect ((real)
274             (top)
275             (real-top))
276     (dolist (com components)
277       (unless (eq (block-next (component-head com)) (component-tail com))
278         (let* ((funs (component-lambdas com))
279                (has-top (find :toplevel funs :key #'functional-kind))
280                (has-external-references
281                 (some #'functional-has-external-references-p funs)))
282           (cond (;; The FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P concept
283                  ;; is newer than the rest of this function, and
284                  ;; doesn't really seem to fit into its mindset. Here
285                  ;; we mark components which contain such FUNCTIONs
286                  ;; them as :COMPLEX-TOPLEVEL, since they do get
287                  ;; executed at run time, and since it's not valid to
288                  ;; delete them just because they don't have any
289                  ;; references from pure :TOPLEVEL components. -- WHN
290                  has-external-references
291                  (setf (component-kind com) :complex-toplevel)
292                  (real com)
293                  (real-top com))
294                 ((or (some #'has-xep-or-nlx funs)
295                      (and has-top (rest funs)))
296                  (setf (component-name com) (find-component-name com))
297                  (real com)
298                  (when has-top
299                    (setf (component-kind com) :complex-toplevel)
300                    (real-top com)))
301                 (has-top
302                  (setf (component-kind com) :toplevel)
303                  (setf (component-name com) "top level form")
304                  (top com))
305                 (t
306                  (delete-component com))))))
307
308     (values (real) (top) (real-top))))
309
310 ;;; Given a list of top level lambdas, return three lists of
311 ;;; components representing the actual component division:
312 ;;;  1. the non-top-level components,
313 ;;;  2. and the second is the top level components, and
314 ;;;  3. Components in [1] that also have a top level lambda.
315 ;;;
316 ;;; We assign the DFO for each component, and delete any unreachable
317 ;;; blocks. We assume that the Flags have already been cleared.
318 ;;;
319 ;;; We iterate over the lambdas in each initial component, trying to
320 ;;; put each function in its own component, but joining it to an
321 ;;; existing component if we find that there are references between
322 ;;; them. Any code that is left in an initial component must be
323 ;;; unreachable, so we can delete it. Stray links to the initial
324 ;;; component tail (due NIL function terminated blocks) are moved to
325 ;;; the appropriate newc component tail.
326 ;;;
327 ;;; When we are done, we assign DFNs and call
328 ;;; FIND-TOPLEVEL-COMPONENTS to pull out top level code.
329 (defun find-initial-dfo (lambdas)
330   (declare (list lambdas))
331   (collect ((components))
332     (let ((new (make-empty-component)))
333       (dolist (tll lambdas)
334         (let ((component (block-component (node-block (lambda-bind tll)))))
335           (dolist (fun (component-lambdas component))
336             (aver (member (functional-kind fun)
337                           '(:optional :external :toplevel nil :escape
338                                       :cleanup)))
339             (let ((res (dfo-walk-call-graph fun new)))
340               (when (eq res new)
341                 (components new)
342                 (setq new (make-empty-component)))))
343           (when (eq (component-kind component) :initial)
344             (aver (null (component-lambdas component)))
345             (let ((tail (component-tail component)))
346               (dolist (pred (block-pred tail))
347                 (let ((pred-component (block-component pred)))
348                   (unless (eq pred-component component)
349                     (unlink-blocks pred tail)
350                     (link-blocks pred (component-tail pred-component))))))
351             (delete-component component)))))
352
353     (dolist (com (components))
354       (let ((num 0))
355         (declare (fixnum num))
356         (do-blocks-backwards (block com :both)
357           (setf (block-number block) (incf num)))))
358
359     (find-toplevel-components (components))))
360 \f
361 ;;; Insert the code in LAMBDA at the end of RESULT-LAMBDA.
362 (defun merge-1-tl-lambda (result-lambda lambda)
363   (declare (type clambda result-lambda lambda))
364
365   ;; Delete the lambda, and combine the LETs and entries.
366   (setf (functional-kind lambda) :deleted)
367   (dolist (let (lambda-lets lambda))
368     (setf (lambda-home let) result-lambda)
369     (setf (lambda-physenv let) (lambda-physenv result-lambda))
370     (push let (lambda-lets result-lambda)))
371   (setf (lambda-entries result-lambda)
372         (nconc (lambda-entries result-lambda)
373                (lambda-entries lambda)))
374
375   (let* ((bind (lambda-bind lambda))
376          (bind-block (node-block bind))
377          (component (block-component bind-block))
378          (result-component
379           (block-component (node-block (lambda-bind result-lambda))))
380          (result-return-block (node-block (lambda-return result-lambda))))
381
382     ;; Move blocks into the new COMPONENT, and move any nodes directly
383     ;; in the old LAMBDA into the new one (with LETs implicitly moved
384     ;; by changing their home.)
385     (do-blocks (block component)
386       (do-nodes (node cont block)
387         (let ((lexenv (node-lexenv node)))
388           (when (eq (lexenv-lambda lexenv) lambda)
389             (setf (lexenv-lambda lexenv) result-lambda))))
390       (setf (block-component block) result-component))
391
392     ;; Splice the blocks into the new DFO, and unlink them from the
393     ;; old component head and tail. Non-return blocks that jump to the
394     ;; tail (NIL-returning calls) are switched to go to the new tail.
395     (let* ((head (component-head component))
396            (first (block-next head))
397            (tail (component-tail component))
398            (last (block-prev tail))
399            (prev (block-prev result-return-block)))
400       (setf (block-next prev) first)
401       (setf (block-prev first) prev)
402       (setf (block-next last) result-return-block)
403       (setf (block-prev result-return-block) last)
404       (dolist (succ (block-succ head))
405         (unlink-blocks head succ))
406       (dolist (pred (block-pred tail))
407         (unlink-blocks pred tail)
408         (let ((last (block-last pred)))
409           (unless (return-p last)
410             (aver (basic-combination-p last))
411             (link-blocks pred (component-tail result-component))))))
412
413     (let ((lambdas (component-lambdas component)))
414       (aver (and (null (rest lambdas))
415                  (eq (first lambdas) lambda))))
416
417     ;; Switch the end of the code from the return block to the start of
418     ;; the next chunk.
419     (dolist (pred (block-pred result-return-block))
420       (unlink-blocks pred result-return-block)
421       (link-blocks pred bind-block))
422     (unlink-node bind)
423
424     ;; If there is a return, then delete it (making the preceding node
425     ;; the last node) and link the block to the result return. There
426     ;; is always a preceding REF NIL node in top level lambdas.
427     (let ((return (lambda-return lambda)))
428       (when return
429         (let ((return-block (node-block return))
430               (result (return-result return)))
431           (setf (block-last return-block) (continuation-use result))
432           (flush-dest result)
433           (delete-continuation result)
434           (link-blocks return-block result-return-block))))))
435
436 ;;; Given a non-empty list of top level LAMBDAs, smash them into a
437 ;;; top level lambda and component, returning these as values. We use
438 ;;; the first lambda and its component, putting the other code in that
439 ;;; component and deleting the other lambdas.
440 (defun merge-toplevel-lambdas (lambdas)
441   (declare (cons lambdas))
442   (let* ((result-lambda (first lambdas))
443          (result-return (lambda-return result-lambda)))
444     (cond
445      (result-return
446
447       ;; Make sure the result's return node starts a block so that we
448       ;; can splice code in before it.
449       (let ((prev (node-prev
450                    (continuation-use
451                     (return-result result-return)))))
452         (when (continuation-use prev)
453           (node-ends-block (continuation-use prev)))
454         (do-uses (use prev)
455           (let ((new (make-continuation)))
456             (delete-continuation-use use)
457             (add-continuation-use use new))))
458
459       (dolist (lambda (rest lambdas))
460         (merge-1-tl-lambda result-lambda lambda)))
461      (t
462       (dolist (lambda (rest lambdas))
463         (setf (functional-entry-function lambda) nil)
464         (delete-component
465          (block-component
466           (node-block (lambda-bind lambda)))))))
467
468     (values (block-component (node-block (lambda-bind result-lambda)))
469             result-lambda)))