0.6.11.23:
[sbcl.git] / src / compiler / control.lisp
1 ;;;; This file contains the control analysis pass in the compiler. This
2 ;;;; pass determines the order in which the IR2 blocks are to be
3 ;;;; emitted, attempting to minimize the associated branching costs.
4 ;;;;
5 ;;;; At this point, we commit to generating IR2 (and ultimately
6 ;;;; assembler) for reachable blocks. Before this phase there might be
7 ;;;; blocks that are unreachable but still appear in the DFO, due in
8 ;;;; inadequate optimization, etc.
9
10 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; more information.
12 ;;;;
13 ;;;; This software is derived from the CMU CL system, which was
14 ;;;; written at Carnegie Mellon University and released into the
15 ;;;; public domain. The software is in the public domain and is
16 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
17 ;;;; files for more information.
18
19 (in-package "SB!C")
20
21 ;;; Insert Block in the emission order after the block After.
22 (defun add-to-emit-order (block after)
23   (declare (type block-annotation block after))
24   (let ((next (block-annotation-next after)))
25     (setf (block-annotation-next after) block)
26     (setf (block-annotation-prev block) after)
27     (setf (block-annotation-next block) next)
28     (setf (block-annotation-prev next) block))
29   (values))
30
31 ;;; If Block looks like the head of a loop, then attempt to rotate it.
32 ;;; A block looks like a loop head if the number of some predecessor
33 ;;; is less than the block's number. Since blocks are numbered in
34 ;;; reverse DFN, this will identify loop heads in a reducible flow
35 ;;; graph.
36 ;;;
37 ;;; When we find a suspected loop head, we scan back from the tail to
38 ;;; find an alternate loop head. This substitution preserves the
39 ;;; correctness of the walk, since the old head can be reached from
40 ;;; the new head. We determine the new head by scanning as far back as
41 ;;; we can find increasing block numbers. Beats me if this is in
42 ;;; general optimal, but it works in simple cases.
43 ;;;
44 ;;; This optimization is inhibited in functions with NLX EPs, since it
45 ;;; is hard to do this without possibly messing up the special-case
46 ;;; walking from NLX EPs described in CONTROL-ANALYZE-1-FUN. We also
47 ;;; suppress rotation of loop heads which are the start of a function
48 ;;; (i.e. tail calls), as the debugger wants functions to start at the
49 ;;; start.
50 (defun find-rotated-loop-head (block)
51   (declare (type cblock block))
52   (let* ((num (block-number block))
53          (env (block-environment block))
54          (pred (dolist (pred (block-pred block) nil)
55                  (when (and (not (block-flag pred))
56                             (eq (block-environment pred) env)
57                             (< (block-number pred) num))
58                    (return pred)))))
59     (cond
60      ((and pred
61            (not (environment-nlx-info env))
62            (not (eq (node-block (lambda-bind (block-home-lambda block)))
63                     block)))
64       (let ((current pred)
65             (current-num (block-number pred)))
66         (block DONE
67           (loop
68             (dolist (pred (block-pred current) (return-from DONE))
69               (when (eq pred block)
70                 (return-from DONE))
71               (when (and (not (block-flag pred))
72                          (eq (block-environment pred) env)
73                          (> (block-number pred) current-num))
74                 (setq current pred   current-num (block-number pred))
75                 (return)))))
76         (aver (not (block-flag current)))
77         current))
78      (t
79       block))))
80
81 ;;; Do a graph walk linking blocks into the emit order as we go. We call
82 ;;; FIND-ROTATED-LOOP-HEAD to do while-loop optimization.
83 ;;;
84 ;;; We treat blocks ending in tail local calls to other environments
85 ;;; specially. We can't walked the called function immediately, since it is in
86 ;;; a different function and we must keep the code for a function contiguous.
87 ;;; Instead, we return the function that we want to call so that it can be
88 ;;; walked as soon as possible, which is hopefully immediately.
89 ;;;
90 ;;; If any of the recursive calls ends in a tail local call, then we return
91 ;;; the last such function, since it is the only one we can possibly drop
92 ;;; through to. (But it doesn't have to be from the last block walked, since
93 ;;; that call might not have added anything.)
94 ;;;
95 ;;; We defer walking successors whose successor is the component tail (end
96 ;;; in an error, NLX or tail full call.)  This is to discourage making error
97 ;;; code the drop-through.
98 (defun control-analyze-block (block tail block-info-constructor)
99   (declare (type cblock block) (type block-annotation tail))
100   (unless (block-flag block)
101     (let ((block (find-rotated-loop-head block)))
102       (setf (block-flag block) t)
103       (aver (and (block-component block) (not (block-delete-p block))))
104       (add-to-emit-order (or (block-info block)
105                              (setf (block-info block)
106                                    (funcall block-info-constructor block)))
107                          (block-annotation-prev tail))
108
109       (let ((last (block-last block)))
110         (cond ((and (combination-p last) (node-tail-p last)
111                     (eq (basic-combination-kind last) :local)
112                     (not (eq (node-environment last)
113                              (lambda-environment (combination-lambda last)))))
114                (combination-lambda last))
115               (t
116                (let ((component-tail (component-tail (block-component block)))
117                      (block-succ (block-succ block))
118                      (fun nil))
119                  (dolist (succ block-succ)
120                    (unless (eq (first (block-succ succ)) component-tail)
121                      (let ((res (control-analyze-block
122                                  succ tail block-info-constructor)))
123                        (when res (setq fun res)))))
124                  (dolist (succ block-succ)
125                    (control-analyze-block succ tail block-info-constructor))
126                  fun)))))))
127
128 ;;; Analyze all of the NLX EPs first to ensure that code reachable only from
129 ;;; a NLX is emitted contiguously with the code reachable from the Bind. Code
130 ;;; reachable from the Bind is inserted *before* the NLX code so that the Bind
131 ;;; marks the beginning of the code for the function. If the walks from NLX
132 ;;; EPs reach the bind block, then we just move it to the beginning.
133 ;;;
134 ;;; If the walk from the bind node encountered a tail local call, then we
135 ;;; start over again there to help the call drop through. Of course, it will
136 ;;; never get a drop-through if either function has NLX code.
137 (defun control-analyze-1-fun (fun component block-info-constructor)
138   (declare (type clambda fun) (type component component))
139   (let* ((tail-block (block-info (component-tail component)))
140          (prev-block (block-annotation-prev tail-block))
141          (bind-block (node-block (lambda-bind fun))))
142     (unless (block-flag bind-block)
143       (dolist (nlx (environment-nlx-info (lambda-environment fun)))
144         (control-analyze-block (nlx-info-target nlx) tail-block
145                                block-info-constructor))
146       (cond
147        ((block-flag bind-block)
148         (let* ((block-note (block-info bind-block))
149                (prev (block-annotation-prev block-note))
150                (next (block-annotation-next block-note)))
151           (setf (block-annotation-prev next) prev)
152           (setf (block-annotation-next prev) next)
153           (add-to-emit-order block-note prev-block)))
154        (t
155         (let ((new-fun (control-analyze-block bind-block
156                                               (block-annotation-next
157                                                prev-block)
158                                               block-info-constructor)))
159           (when new-fun
160             (control-analyze-1-fun new-fun component
161                                    block-info-constructor)))))))
162   (values))
163
164 ;;; Do control analysis on Component, finding the emit order. Our only
165 ;;; cleverness here is that we walk XEP's first to increase the probability
166 ;;; that the tail call will be a drop-through.
167 ;;;
168 ;;; When we are done, we delete blocks that weren't reached by the walk.
169 ;;; Some return blocks are made unreachable by LTN without setting
170 ;;; COMPONENT-REANALYZE. We remove all deleted blocks from the IR2-COMPONENT
171 ;;; VALUES-RECEIVERS to keep stack analysis from getting confused.
172 (defevent control-deleted-block "control analysis deleted dead block")
173 (defun control-analyze (component block-info-constructor)
174   (declare (type component component)
175            (type function block-info-constructor))
176   (let* ((head (component-head component))
177          (head-block (funcall block-info-constructor head))
178          (tail (component-tail component))
179          (tail-block (funcall block-info-constructor tail)))
180     (setf (block-info head) head-block)
181     (setf (block-info tail) tail-block)
182     (setf (block-annotation-prev tail-block) head-block)
183     (setf (block-annotation-next head-block) tail-block)
184
185     (clear-flags component)
186
187     (dolist (fun (component-lambdas component))
188       (when (external-entry-point-p fun)
189         (control-analyze-1-fun fun component block-info-constructor)))
190
191     (dolist (fun (component-lambdas component))
192       (control-analyze-1-fun fun component block-info-constructor))
193
194     (do-blocks (block component)
195       (unless (block-flag block)
196         (event control-deleted-block (continuation-next (block-start block)))
197         (delete-block block))))
198
199   (let ((2comp (component-info component)))
200     (when (ir2-component-p 2comp)
201       ;; If it's not an ir2-component, don't worry about it.
202       (setf (ir2-component-values-receivers 2comp)
203             (delete-if-not #'block-component
204                            (ir2-component-values-receivers 2comp)))))
205
206   (values))