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.
6 ;;;; This software is part of the SBCL system. See the README file for
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.
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))
24 (lvar-dest (car (last popped)))
28 (do-nodes (node lvar block)
29 (when (eq node last-pop)
33 (let ((dest (lvar-dest lvar))
34 (2lvar (lvar-info lvar)))
35 (when (and (not (eq (node-block dest) block))
37 (eq (ir2-lvar-kind 2lvar) :unknown))
38 (aver (or saw-last (not last-pop)))
41 (setf (ir2-block-pushed 2block) (pushed))))
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))))
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)))
56 ;;; Add LVARs from LATE to EARLY; use EQ to check whether EARLY has
58 (defun merge-uvl-live-sets (early late)
59 (declare (type list early late))
60 (dolist (e late early)
63 ;;; Update information on stacks of unknown-values LVARs on the
64 ;;; boundaries of BLOCK. Return true if the start stack has been
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))
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
83 (exit-lvar (nlx-info-lvar nlx-info))
84 (next-stack (if exit-lvar
85 (remove exit-lvar nle-start-stack)
87 (setq new-end (merge-uvl-live-sets
88 new-end next-stack))))
91 (setf (ir2-block-end-stack 2block) new-end)
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)))
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))))
106 (when *check-consistency*
107 (aver (subsetp original-start start)))
108 (cond ((subsetp start original-start)
111 (setf (ir2-block-start-stack 2block) start)
115 ;;;; Ordering of live UVL stacks
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)
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)
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)))
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)))
147 (setf (ir2-block-end-stack 2block) end-stack))))
149 (defun order-uvl-sets (component)
150 (clear-flags component)
151 (loop with head = (component-head component)
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))))
162 (setf (block-flag block) t)
163 (order-block-uvl-sets block pred))
165 (setq repeat-p t))))))
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).
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.
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))
191 (values before (first (last before)) nil)
192 (loop with first-preserved = (car after)
193 for last-popped = nil then maybe-popped
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)
201 ((eq (car before-stack) (car after-stack))
202 (binding* ((moved-count (mismatch before-stack after-stack)
205 (loop for moved-lvar in before-stack
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))))
215 `(%nip-values ',last-nipped ,q-last-moved
217 (discard (nconc moved rest) after-stack)))
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))))
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))))
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
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)))
249 (res (node-block use))))))
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
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)))
263 (dolist (block generators)
264 (find-pushed-lvars block))
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)))
273 (order-uvl-sets component)
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))
281 (discard-unused-values block succ))))))