6ab9bfde06651702981f83c3170c4d6d4142aadc
[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   (collect ((cleanup-code))
186     (labels ((find-popped (before after)
187                ;; Returns (VALUES popped last-popped rest), where
188                ;; BEFORE = (APPEND popped rest) and
189                ;; (EQ (FIRST rest) (FIRST after))
190                (if (null after)
191                    (values before (first (last before)) nil)
192                    (loop with first-preserved = (car after)
193                          for last-popped = nil then maybe-popped
194                          for rest on before
195                          for maybe-popped = (car rest)
196                          while (neq maybe-popped first-preserved)
197                          collect maybe-popped into popped
198                          finally (return (values popped last-popped rest)))))
199              (discard (before-stack after-stack)
200                (cond
201                  ((eq (car before-stack) (car after-stack))
202                   (binding* ((moved-count (mismatch before-stack after-stack)
203                                           :exit-if-null)
204                              ((moved qmoved)
205                               (loop for moved-lvar in before-stack
206                                     repeat moved-count
207                                     collect moved-lvar into moved
208                                     collect `',moved-lvar into qmoved
209                                     finally (return (values moved qmoved))))
210                              (q-last-moved (car (last qmoved)))
211                              ((nil last-nipped rest)
212                               (find-popped (nthcdr moved-count before-stack)
213                                            (nthcdr moved-count after-stack))))
214                     (cleanup-code
215                      `(%nip-values ',last-nipped ,q-last-moved
216                        ,@qmoved))
217                     (discard (nconc moved rest) after-stack)))
218                  (t
219                   (multiple-value-bind (popped last-popped rest)
220                       (find-popped before-stack after-stack)
221                     (declare (ignore popped))
222                     (cleanup-code `(%pop-values ',last-popped))
223                     (discard rest after-stack))))))
224       (discard (ir2-block-end-stack (block-info block1))
225                (ir2-block-start-stack (block-info block2))))
226     (when (cleanup-code)
227       (let* ((block (insert-cleanup-code block1 block2
228                                          (block-start-node block2)
229                                          `(progn ,@(cleanup-code))))
230              (2block (make-ir2-block block)))
231         (setf (block-info block) 2block)
232         (add-to-emit-order 2block (block-info block1))
233         (ltn-analyze-belated-block block))))
234
235   (values))
236 \f
237 ;;;; stack analysis
238
239 ;;; Return a list of all the blocks containing genuine uses of one of
240 ;;; the RECEIVERS. Exits are excluded, since they don't drop through
241 ;;; to the receiver.
242 (defun find-values-generators (receivers)
243   (declare (list receivers))
244   (collect ((res nil adjoin))
245     (dolist (rec receivers)
246       (dolist (pop (ir2-block-popped (block-info rec)))
247         (do-uses (use pop)
248           (unless (exit-p use)
249             (res (node-block use))))))
250     (res)))
251
252 ;;; Analyze the use of unknown-values lvars in COMPONENT, inserting
253 ;;; cleanup code to discard values that are generated but never
254 ;;; received. This phase doesn't need to be run when Values-Receivers
255 ;;; is null, i.e. there are no unknown-values lvars used across block
256 ;;; boundaries.
257 (defun stack-analyze (component)
258   (declare (type component component))
259   (let* ((2comp (component-info component))
260          (receivers (ir2-component-values-receivers 2comp))
261          (generators (find-values-generators receivers)))
262
263     (dolist (block generators)
264       (find-pushed-lvars block))
265
266     ;;; Compute sets of live UVLs
267     (loop for did-something = nil
268           do (do-blocks-backwards (block component)
269                (when (update-uvl-live-sets block)
270                  (setq did-something t)))
271           while did-something)
272
273     (order-uvl-sets component)
274
275     (do-blocks (block component)
276       (let ((top (ir2-block-end-stack (block-info block))))
277         (dolist (succ (block-succ block))
278           (when (and (block-start succ)
279                      (not (eq (ir2-block-start-stack (block-info succ))
280                               top)))
281             (discard-unused-values block succ))))))
282
283   (values))