x86-64: Treat more symbols as having immediate storage class
[sbcl.git] / src / compiler / stack.lisp
blobab88bea2322717ae148ed1d3773769b1591d2286
1 ;;;; This file implements the stack analysis phase in the compiler. We
2 ;;;; analyse lifetime of dynamically allocated object packets on stack
3 ;;;; and insert cleanups where necessary.
4 ;;;;
5 ;;;; Currently there are two kinds of interesting stack packets: UVLs,
6 ;;;; whose use and destination lie in different blocks, and LVARs of
7 ;;;; constructors of dynamic-extent objects.
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
18 (in-package "SB!C")
20 ;;; Scan through BLOCK looking for uses of :UNKNOWN lvars that have
21 ;;; their DEST outside of the block. We do some checking to verify the
22 ;;; invariant that all pushes come after the last pop.
23 (defun find-pushed-lvars (block)
24 (let* ((2block (block-info block))
25 (popped (ir2-block-popped 2block))
26 (last-pop (if popped
27 (lvar-dest (car (last popped)))
28 nil)))
29 (collect ((pushed))
30 (let ((saw-last nil))
31 (do-nodes (node lvar block)
32 (when (eq node last-pop)
33 (setq saw-last t))
35 (when (and lvar
36 (or (lvar-dynamic-extent lvar)
37 (let ((dest (lvar-dest lvar))
38 (2lvar (lvar-info lvar)))
39 (and (not (eq (node-block dest) block))
40 2lvar
41 (eq (ir2-lvar-kind 2lvar) :unknown)))))
42 (aver (or saw-last (not last-pop)))
43 (pushed lvar))))
45 (setf (ir2-block-pushed 2block) (pushed))))
46 (values))
48 ;;;; Computation of live UVL sets
49 (defun nle-block-nlx-info (block)
50 (let* ((start-node (block-start-node block))
51 (nlx-ref (ctran-next (node-next start-node)))
52 (nlx-info (constant-value (ref-leaf nlx-ref))))
53 nlx-info))
54 (defun nle-block-entry-block (block)
55 (let* ((nlx-info (nle-block-nlx-info block))
56 (mess-up (cleanup-mess-up (nlx-info-cleanup nlx-info)))
57 (entry-block (node-block mess-up)))
58 entry-block))
60 ;;; Add LVARs from LATE to EARLY; use EQ to check whether EARLY has
61 ;;; been changed.
62 (defun merge-uvl-live-sets (early late)
63 (declare (type list early late))
64 ;; FIXME: O(N^2)
65 (dolist (e late early)
66 (pushnew e early)))
68 ;; Blocks are numbered in reverse DFO order, so the "lowest common
69 ;; dominator" of a set of blocks is the closest dominator of all of
70 ;; the blocks.
71 (defun find-lowest-common-dominator (blocks)
72 ;; FIXME: NIL is defined as a valid value for BLOCK-DOMINATORS,
73 ;; meaning "all blocks in component". Actually handle this case.
74 (let ((common-dominators (copy-sset (block-dominators (first blocks)))))
75 (dolist (block (rest blocks))
76 (sset-intersection common-dominators (block-dominators block)))
77 (let ((lowest-dominator))
78 (do-sset-elements (dominator common-dominators lowest-dominator)
79 (when (or (not lowest-dominator)
80 (< (sset-element-number dominator)
81 (sset-element-number lowest-dominator)))
82 (setf lowest-dominator dominator))))))
84 ;;; Carefully back-propagate DX LVARs from the start of their
85 ;;; environment to where they are allocated, along all code paths
86 ;;; which actually allocate said LVARs.
87 (defun back-propagate-one-dx-lvar (block dx-lvar)
88 (declare (type cblock block)
89 (type lvar dx-lvar))
90 ;; We have to back-propagate the lifetime of DX-LVAR to its USEs,
91 ;; but only along the paths which actually USE it. The naive
92 ;; solution (which we're going with for now) is a depth-first search
93 ;; over an arbitrarily complex chunk of flow graph that is known to
94 ;; have a single entry block.
95 (let* ((use-blocks (mapcar #'node-block (find-uses dx-lvar)))
96 (start-block (find-lowest-common-dominator
97 (list* block use-blocks))))
98 (labels ((mark-lvar-live-on-path (arc-list)
99 (dolist (arc arc-list)
100 (let ((2block (block-info (car arc))))
101 (pushnew dx-lvar (ir2-block-end-stack 2block))
102 (pushnew dx-lvar (ir2-block-start-stack 2block)))))
103 (back-propagate-pathwise (current-block path)
104 (cond
105 ((member current-block use-blocks)
106 ;; The LVAR is live on exit from a use-block, but
107 ;; not on entry.
108 (pushnew dx-lvar (ir2-block-end-stack
109 (block-info current-block)))
110 (mark-lvar-live-on-path path))
111 ;; Don't go back past START-BLOCK.
112 ((not (eq current-block start-block))
113 (dolist (pred-block (block-pred current-block))
114 (let ((new-arc (cons current-block pred-block)))
115 (declare (dynamic-extent new-arc))
116 ;; Never follow the same path segment twice.
117 (unless (member new-arc path :test #'equal)
118 (let ((new-path (list* new-arc path)))
119 (declare (dynamic-extent new-path))
120 (back-propagate-pathwise pred-block new-path)))))))))
121 (back-propagate-pathwise block nil))))
123 (defun back-propagate-dx-lvars (block dx-lvars)
124 (declare (type cblock block)
125 (type list dx-lvars))
126 (dolist (dx-lvar dx-lvars)
127 (back-propagate-one-dx-lvar block dx-lvar)))
129 ;;; Update information on stacks of unknown-values LVARs on the
130 ;;; boundaries of BLOCK. Return true if the start stack has been
131 ;;; changed.
133 ;;; An LVAR is live at the end iff it is live at some of blocks, which
134 ;;; BLOCK can transfer control to. There are two kind of control
135 ;;; transfers: normal, expressed with BLOCK-SUCC, and NLX.
136 (defun update-uvl-live-sets (block)
137 (declare (type cblock block))
138 (let* ((2block (block-info block))
139 (original-start (ir2-block-start-stack 2block))
140 (end (ir2-block-end-stack 2block))
141 (new-end end))
142 (dolist (succ (block-succ block))
143 (setq new-end (merge-uvl-live-sets new-end
144 ;; Don't back-propagate DX
145 ;; LVARs automatically,
146 ;; they're handled specially.
147 (remove-if #'lvar-dynamic-extent
148 (ir2-block-start-stack (block-info succ))))))
149 (map-block-nlxes (lambda (nlx-info)
150 (let* ((nle (nlx-info-target nlx-info))
151 (nle-start-stack (ir2-block-start-stack
152 (block-info nle)))
153 (exit-lvar (nlx-info-lvar nlx-info))
154 (next-stack (if exit-lvar
155 (remove exit-lvar nle-start-stack)
156 nle-start-stack)))
157 (setq new-end (merge-uvl-live-sets
158 new-end next-stack))))
159 block
160 (lambda (dx-cleanup)
161 (dolist (lvar (cleanup-info dx-cleanup))
162 (do-uses (generator lvar)
163 (let* ((block (node-block generator))
164 (2block (block-info block)))
165 ;; DX objects, living in the LVAR, are alive in
166 ;; the environment, protected by the CLEANUP. We
167 ;; also cannot move them (because, in general, we
168 ;; cannot track all references to them).
169 ;; Therefore, everything, allocated deeper than a
170 ;; DX object -- that is, before the DX object --
171 ;; should be kept alive until the object is
172 ;; deallocated.
174 ;; Since DX generators end their blocks, we can
175 ;; find out UVLs allocated before them by looking
176 ;; at the stack at the end of the block.
177 (setq new-end (merge-uvl-live-sets
178 new-end (ir2-block-end-stack 2block)))
179 (setq new-end (merge-uvl-live-sets
180 new-end (ir2-block-pushed 2block))))))))
182 (setf (ir2-block-end-stack 2block) new-end)
184 ;; If a block starts with an "entry DX" node (the start of a DX
185 ;; environment) then we need to back-propagate the DX LVARs to
186 ;; their allocation sites. We need to be clever about this
187 ;; because some code paths may not allocate all of the DX LVARs.
189 ;; FIXME: Use BLOCK-FLAG to make this happen only once.
190 (let ((first-node (ctran-next (block-start block))))
191 (when (typep first-node 'entry)
192 (let ((cleanup (entry-cleanup first-node)))
193 (when (eq (cleanup-kind cleanup) :dynamic-extent)
194 (back-propagate-dx-lvars block (cleanup-info cleanup))))))
196 (let ((start new-end))
197 (setq start (set-difference start (ir2-block-pushed 2block)))
198 (setq start (merge-uvl-live-sets start (ir2-block-popped 2block)))
200 ;; We cannot delete unused UVLs during NLX, so all UVLs live at
201 ;; ENTRY will be actually live at NLE.
203 ;; BUT, UNWIND-PROTECTor is called in the environment, which has
204 ;; nothing in common with the environment of its entry. So we
205 ;; fictively compute its stack from the containing cleanups, but
206 ;; do not propagate additional LVARs from the entry, thus
207 ;; preveting bogus stack cleanings.
209 ;; TODO: Insert a check that no values are discarded in UWP. Or,
210 ;; maybe, we just don't need to create NLX-ENTRY for UWP?
211 (when (and (eq (component-head (block-component block))
212 (first (block-pred block)))
213 (not (bind-p (block-start-node block))))
214 (let* ((nlx-info (nle-block-nlx-info block))
215 (cleanup (nlx-info-cleanup nlx-info)))
216 (unless (eq (cleanup-kind cleanup) :unwind-protect)
217 (let* ((entry-block (node-block (cleanup-mess-up cleanup)))
218 (entry-stack (ir2-block-start-stack (block-info entry-block))))
219 (setq start (merge-uvl-live-sets start entry-stack))))))
221 (when *check-consistency*
222 (aver (subsetp original-start start)))
223 (cond ((subsetp start original-start)
224 nil)
226 (setf (ir2-block-start-stack 2block) start)
227 t)))))
230 ;;;; Ordering of live UVL stacks
232 (defun ordered-list-intersection (ordered-list other-list)
233 (loop for item in ordered-list
234 when (memq item other-list)
235 collect item))
237 (defun ordered-list-union (ordered-list-1 ordered-list-2)
238 (labels ((sub-union (ol1 ol2 result)
239 (cond ((and (null ol1) (null ol2))
240 result)
241 ((and (null ol1) ol2)
242 (sub-union ol1 (cdr ol2) (cons (car ol2) result)))
243 ((and ol1 (null ol2))
244 (sub-union (cdr ol1) ol2 (cons (car ol1) result)))
245 ((eq (car ol1) (car ol2))
246 (sub-union (cdr ol1) (cdr ol2) (cons (car ol1) result)))
247 ((memq (car ol1) ol2)
248 (sub-union ol1 (cdr ol2) (cons (car ol2) result)))
250 (sub-union (cdr ol1) ol2 (cons (car ol1) result))))))
251 (nreverse (sub-union ordered-list-1 ordered-list-2 nil))))
253 ;;; Put UVLs on the start/end stacks of BLOCK in the right order. PRED
254 ;;; is a predecessor of BLOCK with already sorted stacks; if all UVLs
255 ;;; being live at the BLOCK start are live in PRED we just need to
256 ;;; delete killed UVLs, otherwise we need (thanks to conditional or
257 ;;; nested DX) to set a total order for the UVLs live at the end of
258 ;;; all predecessors.
259 (defun order-block-uvl-sets (block pred)
260 (let* ((2block (block-info block))
261 (pred-end-stack (ir2-block-end-stack (block-info pred)))
262 (start (ir2-block-start-stack 2block))
263 (start-stack (ordered-list-intersection pred-end-stack start))
264 (end (ir2-block-end-stack 2block)))
266 (when (not (subsetp start start-stack))
267 ;; If BLOCK is a control-flow join for DX allocation paths with
268 ;; different sets of DX LVARs being pushed then we cannot
269 ;; process it correctly until all of its predecessors have been
270 ;; processed.
271 (unless (every #'block-flag (block-pred block))
272 (return-from order-block-uvl-sets nil))
273 ;; If we are in the conditional-DX control-flow join case then
274 ;; we need to find an order for START-STACK that is compatible
275 ;; with all of our predecessors.
276 (dolist (end-stack (mapcar #'ir2-block-end-stack
277 (mapcar #'block-info
278 (block-pred block))))
279 (setf pred-end-stack
280 (ordered-list-union pred-end-stack end-stack)))
281 (setf start-stack (ordered-list-intersection pred-end-stack start)))
283 (when *check-consistency*
284 (aver (subsetp start start-stack)))
285 (setf (ir2-block-start-stack 2block) start-stack)
287 (let* ((last (block-last block))
288 (tailp-lvar (if (node-tail-p last) (node-lvar last)))
289 (end-stack start-stack))
290 (dolist (pop (ir2-block-popped 2block))
291 (aver (eq pop (car end-stack)))
292 (pop end-stack))
293 (dolist (push (ir2-block-pushed 2block))
294 (aver (not (memq push end-stack)))
295 (push push end-stack))
296 (aver (subsetp end end-stack))
297 (when (and tailp-lvar
298 (eq (ir2-lvar-kind (lvar-info tailp-lvar)) :unknown))
299 (aver (eq tailp-lvar (first end-stack)))
300 (pop end-stack))
301 (setf (ir2-block-end-stack 2block) end-stack)))
304 (defun order-uvl-sets (component)
305 (clear-flags component)
306 ;; KLUDGE: Workaround for lp#308914: we keep track of number of blocks
307 ;; needing repeats, and bug out if we get stuck.
308 (loop with head = (component-head component)
309 with todo = 0
310 with last-todo = 0
311 do (psetq last-todo todo
312 todo 0)
313 do (do-blocks (block component)
314 (unless (block-flag block)
315 (let ((pred (find-if #'block-flag (block-pred block))))
316 (when (and (eq pred head)
317 (not (bind-p (block-start-node block))))
318 (let ((entry (nle-block-entry-block block)))
319 (setq pred (if (block-flag entry) entry nil))))
320 (if (and pred
321 (order-block-uvl-sets block pred))
322 (setf (block-flag block) t)
323 (incf todo)))))
324 do (when (= last-todo todo)
325 ;; If the todo count is the same as on last iteration and
326 ;; there are still blocks to do, it means we are stuck,
327 ;; which in turn means the unmarked blocks are actually
328 ;; unreachable and should have been eliminated by DCE,
329 ;; and will very likely cause problems with later parts
330 ;; of STACK analysis, so abort now if we're in trouble.
331 (aver (not (plusp todo))))
332 while (plusp todo)))
334 ;;; This is called when we discover that the stack-top unknown-values
335 ;;; lvar at the end of BLOCK1 is different from that at the start of
336 ;;; BLOCK2 (its successor).
338 ;;; We insert a call to a funny function in a new cleanup block
339 ;;; introduced between BLOCK1 and BLOCK2. Since control analysis and
340 ;;; LTN have already run, we must do make an IR2 block, then do
341 ;;; ADD-TO-EMIT-ORDER and LTN-ANALYZE-BELATED-BLOCK on the new
342 ;;; block. The new block is inserted after BLOCK1 in the emit order.
344 ;;; If the control transfer between BLOCK1 and BLOCK2 represents a
345 ;;; tail-recursive return or a non-local exit, then the cleanup code
346 ;;; will never actually be executed. It doesn't seem to be worth the
347 ;;; risk of trying to optimize this, since this rarely happens and
348 ;;; wastes only space.
349 (defun insert-stack-cleanups (block1 block2)
350 (declare (type cblock block1 block2))
351 (collect ((cleanup-code))
352 (labels ((find-popped (before after)
353 ;; Returns (VALUES popped last-popped rest), where
354 ;; BEFORE = (APPEND popped rest) and
355 ;; (EQ (FIRST rest) (FIRST after))
356 (if (null after)
357 (values before (first (last before)) nil)
358 (loop with first-preserved = (car after)
359 for last-popped = nil then maybe-popped
360 for rest on before
361 for maybe-popped = (car rest)
362 while (neq maybe-popped first-preserved)
363 collect maybe-popped into popped
364 finally (return (values popped last-popped rest)))))
365 (discard (before-stack after-stack)
366 (cond
367 ((eq (car before-stack) (car after-stack))
368 (binding* ((moved-count (mismatch before-stack after-stack)
369 :exit-if-null)
370 ((moved qmoved)
371 (loop for moved-lvar in before-stack
372 repeat moved-count
373 collect moved-lvar into moved
374 collect `',moved-lvar into qmoved
375 finally (return (values moved qmoved))))
376 (q-last-moved (car (last qmoved)))
377 ((nil last-nipped rest)
378 (find-popped (nthcdr moved-count before-stack)
379 (nthcdr moved-count after-stack))))
380 (cleanup-code
381 `(%nip-values ',last-nipped ,q-last-moved
382 ,@qmoved))
383 (discard (nconc moved rest) after-stack)))
385 (multiple-value-bind (popped last-popped rest)
386 (find-popped before-stack after-stack)
387 (declare (ignore popped))
388 (cleanup-code `(%pop-values ',last-popped))
389 (discard rest after-stack)))))
390 (dummy-allocations (before-stack after-stack)
391 (loop
392 for previous-lvar = nil then lvar
393 for lvar in after-stack
394 unless (memq lvar before-stack)
395 do (cleanup-code
396 `(%dummy-dx-alloc ',lvar ',previous-lvar)))))
397 (let* ((end-stack (ir2-block-end-stack (block-info block1)))
398 (start-stack (ir2-block-start-stack (block-info block2)))
399 (pruned-start-stack (ordered-list-intersection
400 start-stack end-stack)))
401 (discard end-stack pruned-start-stack)
402 (dummy-allocations pruned-start-stack start-stack)
403 (when (cleanup-code)
404 (let* ((block (insert-cleanup-code block1 block2
405 (block-start-node block2)
406 `(progn ,@(cleanup-code))))
407 (2block (make-ir2-block block)))
408 (setf (block-info block) 2block)
409 (add-to-emit-order 2block (block-info block1))
410 (ltn-analyze-belated-block block)
411 ;; Set the start and end stacks to make traces less
412 ;; confusing. Purely cosmetic.
413 (setf (ir2-block-start-stack 2block) end-stack)
414 (setf (ir2-block-end-stack 2block) start-stack))))))
416 (values))
418 ;;;; stack analysis
420 ;;; Return a list of all the blocks containing genuine uses of one of
421 ;;; the RECEIVERS (blocks) and DX-LVARS. Exits are excluded, since
422 ;;; they don't drop through to the receiver.
423 (defun find-pushing-blocks (receivers dx-lvars)
424 (declare (list receivers dx-lvars))
425 (collect ((res nil adjoin))
426 (dolist (rec receivers)
427 (dolist (pop (ir2-block-popped (block-info rec)))
428 (do-uses (use pop)
429 (unless (exit-p use)
430 (res (node-block use))))))
431 (dolist (dx-lvar dx-lvars)
432 (do-uses (use dx-lvar)
433 (res (node-block use))))
434 (res)))
436 ;;; Analyze the use of unknown-values and DX lvars in COMPONENT,
437 ;;; inserting cleanup code to discard values that are generated but
438 ;;; never received and to set appropriate bounds for DX values that
439 ;;; are cleaned up but never allocated. This phase doesn't need to be
440 ;;; run when Values-Receivers and Dx-Lvars are null, i.e. there are no
441 ;;; unknown-values lvars used across block boundaries and no DX LVARs.
442 (defun stack-analyze (component)
443 (declare (type component component))
444 (let* ((2comp (component-info component))
445 (receivers (ir2-component-values-receivers 2comp))
446 (generators (find-pushing-blocks receivers
447 (component-dx-lvars component))))
449 (dolist (block generators)
450 (find-pushed-lvars block)))
452 ;; Compute sets of live UVLs and DX LVARs
453 (loop for did-something = nil
454 do (do-blocks-backwards (block component)
455 (when (update-uvl-live-sets block)
456 (setq did-something t)))
457 while did-something)
459 (order-uvl-sets component)
461 (do-blocks (block component)
462 (let ((top (ir2-block-end-stack (block-info block))))
463 (dolist (succ (block-succ block))
464 (when (and (block-start succ)
465 (not (eq (ir2-block-start-stack (block-info succ))
466 top)))
467 (insert-stack-cleanups block succ)))))
469 (values))