0.8.10.3:
[sbcl.git] / src / compiler / stack.lisp
1 ;;;; This file implements the stack analysis phase in the compiler. We
2 ;;;; do a graph walk to determine which unknown-values lvars are on
3 ;;;; the stack at each point in the program, and then we insert
4 ;;;; cleanup code to remove unused values.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
14
15 (in-package "SB!C")
16 \f
17 ;;; Scan through BLOCK looking for uses of :UNKNOWN lvars that have
18 ;;; their DEST outside of the block. We do some checking to verify the
19 ;;; invariant that all pushes come after the last pop.
20 (defun find-pushed-lvars (block)
21   (let* ((2block (block-info block))
22          (popped (ir2-block-popped 2block))
23          (last-pop (if popped
24                        (lvar-dest (car (last popped)))
25                        nil)))
26     (collect ((pushed))
27       (let ((saw-last nil))
28         (do-nodes (node lvar block)
29           (when (eq node last-pop)
30             (setq saw-last t))
31
32           (when lvar
33             (let ((dest (lvar-dest lvar))
34                   (2lvar (lvar-info lvar)))
35               (when (and (not (eq (node-block dest) block))
36                          2lvar
37                          (eq (ir2-lvar-kind 2lvar) :unknown))
38                 (aver (or saw-last (not last-pop)))
39                 (pushed lvar))))))
40
41       (setf (ir2-block-pushed 2block) (pushed))))
42   (values))
43 \f
44 ;;;; Computation of live UVL sets
45 (defun nle-block-nlx-info (block)
46   (let* ((start-node (block-start-node block))
47          (nlx-ref (ctran-next (node-next start-node)))
48          (nlx-info (constant-value (ref-leaf nlx-ref))))
49     nlx-info))
50 (defun nle-block-entry-block (block)
51   (let* ((nlx-info (nle-block-nlx-info block))
52          (mess-up (cleanup-mess-up (nlx-info-cleanup nlx-info)))
53          (entry-block (node-block mess-up)))
54     entry-block))
55
56 ;;; Add LVARs from LATE to EARLY; use EQ to check whether EARLY has
57 ;;; been changed.
58 (defun merge-uvl-live-sets (early late)
59   (declare (type list early late))
60   (dolist (e late early)
61     (pushnew e early)))
62
63 ;;; Update information on stacks of unknown-values LVARs on the
64 ;;; boundaries of BLOCK. Return true if the start stack has been
65 ;;; changed.
66 ;;;
67 ;;; An LVAR is live at the end iff it is live at some of blocks, which
68 ;;; BLOCK can transfer control to. There are two kind of control
69 ;;; transfers: normal, expressed with BLOCK-SUCC, and NLX.
70 (defun update-uvl-live-sets (block)
71   (declare (type cblock block))
72   (let* ((2block (block-info block))
73          (original-start (ir2-block-start-stack 2block))
74          (end (ir2-block-end-stack 2block))
75          (new-end end))
76     (dolist (succ (block-succ block))
77       (setq new-end (merge-uvl-live-sets new-end
78                                          (ir2-block-start-stack (block-info succ)))))
79     (map-block-nlxes (lambda (nlx-info)
80                        (let* ((nle (nlx-info-target nlx-info))
81                               (nle-start-stack (ir2-block-start-stack
82                                                 (block-info nle)))
83                               (exit-lvar (nlx-info-lvar nlx-info))
84                               (next-stack (if exit-lvar
85                                               (remove exit-lvar nle-start-stack)
86                                               nle-start-stack)))
87                          (setq new-end (merge-uvl-live-sets
88                                         new-end next-stack))))
89                      block)
90
91     (setf (ir2-block-end-stack 2block) new-end)
92
93     (let ((start new-end))
94       (setq start (set-difference start (ir2-block-pushed 2block)))
95       (setq start (merge-uvl-live-sets start (ir2-block-popped 2block)))
96
97       ;; We cannot delete unused UVLs during NLX, so all UVLs live at
98       ;; ENTRY will be actually live at NLE.
99       (when (and (eq (component-head (block-component block))
100                      (first (block-pred block)))
101                  (not (bind-p (block-start-node block))))
102         (let* ((entry-block (nle-block-entry-block block))
103                (entry-stack (ir2-block-start-stack (block-info entry-block))))
104           (setq start (merge-uvl-live-sets start entry-stack))))
105
106       (when *check-consistency*
107         (aver (subsetp original-start start)))
108       (cond ((subsetp start original-start)
109              nil)
110             (t
111              (setf (ir2-block-start-stack 2block) start)
112              t)))))
113
114 \f
115 ;;;; Ordering of live UVL stacks
116
117 ;;; Put UVLs on the start/end stacks of BLOCK in the right order. PRED
118 ;;; is a predecessor of BLOCK with already sorted stacks; because all
119 ;;; UVLs being live at the BLOCK start are live in PRED, we just need
120 ;;; to delete dead UVLs.
121 (defun order-block-uvl-sets (block pred)
122   (let* ((2block (block-info block))
123          (pred-end-stack (ir2-block-end-stack (block-info pred)))
124          (start (ir2-block-start-stack 2block))
125          (start-stack (loop for lvar in pred-end-stack
126                             when (memq lvar start)
127                             collect lvar))
128          (end (ir2-block-end-stack 2block)))
129     (when *check-consistency*
130       (aver (subsetp start start-stack)))
131     (setf (ir2-block-start-stack 2block) start-stack)
132
133     (let* ((last (block-last block))
134            (tailp-lvar (if (node-tail-p last) (node-lvar last)))
135            (end-stack start-stack))
136       (dolist (pop (ir2-block-popped 2block))
137         (aver (eq pop (car end-stack)))
138         (pop end-stack))
139       (dolist (push (ir2-block-pushed 2block))
140         (aver (not (memq push end-stack)))
141         (push push end-stack))
142       (aver (subsetp end end-stack))
143       (when (and tailp-lvar
144                  (eq (ir2-lvar-kind (lvar-info tailp-lvar)) :unknown))
145         (aver (eq tailp-lvar (first end-stack)))
146         (pop end-stack))
147       (setf (ir2-block-end-stack 2block) end-stack))))
148
149 (defun order-uvl-sets (component)
150   (clear-flags component)
151   (loop with head = (component-head component)
152         with repeat-p do
153         (setq repeat-p nil)
154         (do-blocks (block component)
155           (unless (block-flag block)
156             (let ((pred (find-if #'block-flag (block-pred block))))
157               (when (and (eq pred head)
158                          (not (bind-p (block-start-node block))))
159                 (let ((entry (nle-block-entry-block block)))
160                   (setq pred (if (block-flag entry) entry nil))))
161               (cond (pred
162                      (setf (block-flag block) t)
163                      (order-block-uvl-sets block pred))
164                     (t
165                      (setq repeat-p t))))))
166         while repeat-p))
167 \f
168 ;;; This is called when we discover that the stack-top unknown-values
169 ;;; lvar at the end of BLOCK1 is different from that at the start of
170 ;;; BLOCK2 (its successor).
171 ;;;
172 ;;; We insert a call to a funny function in a new cleanup block
173 ;;; introduced between BLOCK1 and BLOCK2. Since control analysis and
174 ;;; LTN have already run, we must do make an IR2 block, then do
175 ;;; ADD-TO-EMIT-ORDER and LTN-ANALYZE-BELATED-BLOCK on the new
176 ;;; block. The new block is inserted after BLOCK1 in the emit order.
177 ;;;
178 ;;; If the control transfer between BLOCK1 and BLOCK2 represents a
179 ;;; tail-recursive return or a non-local exit, then the cleanup code
180 ;;; will never actually be executed. It doesn't seem to be worth the
181 ;;; risk of trying to optimize this, since this rarely happens and
182 ;;; wastes only space.
183 (defun discard-unused-values (block1 block2)
184   (declare (type cblock block1 block2))
185   (let* ((block1-stack (ir2-block-end-stack (block-info block1)))
186          (block2-stack (ir2-block-start-stack (block-info block2)))
187          (cleanup-code
188           (cond ((eq (car block1-stack) (car block2-stack))
189                  (binding* ((preserved-count (mismatch block1-stack block2-stack)
190                               :exit-if-null)
191                             (n-last-preserved (1- preserved-count))
192                             (nipped-count (- (length block1-stack)
193                                              (length block2-stack)))
194                             (n-last-nipped (+ n-last-preserved nipped-count)))
195                    (aver (equal (nthcdr (1+ n-last-nipped) block1-stack)
196                                 (nthcdr preserved-count block2-stack)))
197                    (compiler-notify "%NIP-VALUES emitted")
198                    `(%nip-values ',(elt block1-stack n-last-nipped)
199                                  ',(elt block1-stack n-last-preserved)
200                                  ,@(loop for moved in block1-stack
201                                          repeat preserved-count
202                                          collect `',moved))))
203                 (t
204                  (let* ((n-popped (- (length block1-stack)
205                                      (length block2-stack)))
206                        (last-popped (elt block1-stack (1- n-popped))))
207                    (when *check-consistency*
208                      (aver (equal block2-stack (nthcdr n-popped block1-stack))))
209                    `(%pop-values ',last-popped))))))
210     (when cleanup-code
211       (let* ((block (insert-cleanup-code block1 block2
212                                          (block-start-node block2)
213                                          cleanup-code))
214              (2block (make-ir2-block block)))
215         (setf (block-info block) 2block)
216         (add-to-emit-order 2block (block-info block1))
217         (ltn-analyze-belated-block block))))
218
219   (values))
220 \f
221 ;;;; stack analysis
222
223 ;;; Return a list of all the blocks containing genuine uses of one of
224 ;;; the RECEIVERS. Exits are excluded, since they don't drop through
225 ;;; to the receiver.
226 (defun find-values-generators (receivers)
227   (declare (list receivers))
228   (collect ((res nil adjoin))
229     (dolist (rec receivers)
230       (dolist (pop (ir2-block-popped (block-info rec)))
231         (do-uses (use pop)
232           (unless (exit-p use)
233             (res (node-block use))))))
234     (res)))
235
236 ;;; Analyze the use of unknown-values lvars in COMPONENT, inserting
237 ;;; cleanup code to discard values that are generated but never
238 ;;; received. This phase doesn't need to be run when Values-Receivers
239 ;;; is null, i.e. there are no unknown-values lvars used across block
240 ;;; boundaries.
241 (defun stack-analyze (component)
242   (declare (type component component))
243   (let* ((2comp (component-info component))
244          (receivers (ir2-component-values-receivers 2comp))
245          (generators (find-values-generators receivers)))
246
247     (dolist (block generators)
248       (find-pushed-lvars block))
249
250     ;;; Compute sets of live UVLs
251     (loop for did-something = nil
252           do (do-blocks-backwards (block component)
253                (when (update-uvl-live-sets block)
254                  (setq did-something t)))
255           while did-something)
256
257     (order-uvl-sets component)
258
259     (do-blocks (block component)
260       (let ((top (ir2-block-end-stack (block-info block))))
261         (dolist (succ (block-succ block))
262           (when (and (block-start succ)
263                      (not (eq (ir2-block-start-stack (block-info succ))
264                               top)))
265             (discard-unused-values block succ))))))
266
267   (values))