;;;; This file implements the stack analysis phase in the compiler. We
;;;; do a graph walk to determine which unknown-values lvars are on
;;;; the stack at each point in the program, and then we insert
-;;;; cleanup code to pop off unused values.
+;;;; cleanup code to remove unused values.
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
(setf (ir2-block-pushed 2block) (pushed))))
(values))
\f
-;;;; annotation graph walk
+;;;; Computation of live UVL sets
+(defun nle-block-nlx-info (block)
+ (let* ((start-node (block-start-node block))
+ (nlx-ref (ctran-next (node-next start-node)))
+ (nlx-info (constant-value (ref-leaf nlx-ref))))
+ nlx-info))
+(defun nle-block-entry-block (block)
+ (let* ((nlx-info (nle-block-nlx-info block))
+ (mess-up (cleanup-mess-up (nlx-info-cleanup nlx-info)))
+ (entry-block (node-block mess-up)))
+ entry-block))
-;;; Do a backward walk in the flow graph simulating the run-time stack
-;;; of unknown-values lvars and annotating the blocks with the result.
-;;;
-;;; BLOCK is the block that is currently being walked and STACK is the
-;;; stack of unknown-values lvars in effect immediately after
-;;; block. We simulate the stack by popping off the unknown-values
-;;; generated by this block (if any) and pushing the lvars for
-;;; values received by this block. (The role of push and pop are
-;;; interchanged because we are doing a backward walk.)
-;;;
-;;; If we run into a values generator whose lvar isn't on
-;;; stack top, then the receiver hasn't yet been reached on any walk
-;;; to this use. In this case, we ignore the push for now, counting on
-;;; Annotate-Dead-Values to clean it up if we discover that it isn't
-;;; reachable at all.
-;;;
-;;; If our final stack isn't empty, then we walk all the predecessor
-;;; blocks that don't have all the lvars that we have on our
-;;; START-STACK on their END-STACK. This is our termination condition
-;;; for the graph walk. We put the test around the recursive call so
-;;; that the initial call to this function will do something even
-;;; though there isn't initially anything on the stack.
-;;;
-;;; We can use the tailp test, since the only time we want to bottom
-;;; out with a non-empty stack is when we intersect with another path
-;;; from the same top level call to this function that has more values
-;;; receivers on that path. When we bottom out in this way, we are
-;;; counting on DISCARD-UNUSED-VALUES doing its thing.
-;;;
-;;; When we do recurse, we check that predecessor's END-STACK is a
-;;; subsequence of our START-STACK. There may be extra stuff on the
-;;; top of our stack because the last path to the predecessor may have
-;;; discarded some values that we use. There may be extra stuff on the
-;;; bottom of our stack because this walk may be from a values
-;;; receiver whose lifetime encloses that of the previous walk.
-;;;
-;;; If a predecessor block is the component head, then it must be the
-;;; case that this is a NLX entry stub. If so, we just stop our walk,
-;;; since the stack at the exit point doesn't have anything to do with
-;;; our stack.
-(defun stack-simulation-walk (block stack)
- (declare (type cblock block) (list stack))
- (let ((2block (block-info block)))
- (setf (ir2-block-end-stack 2block) stack)
- (let ((new-stack stack))
- (dolist (push (reverse (ir2-block-pushed 2block)))
- (if (eq (car new-stack) push)
- (pop new-stack)
- (aver (not (member push new-stack)))))
-
- (dolist (pop (reverse (ir2-block-popped 2block)))
- (push pop new-stack))
-
- (setf (ir2-block-start-stack 2block) new-stack)
-
- (when new-stack
- (dolist (pred (block-pred block))
- (if (eq pred (component-head (block-component block)))
- (aver (find block
- (physenv-nlx-info (block-physenv block))
- :key #'nlx-info-target))
- (let ((pred-stack (ir2-block-end-stack (block-info pred))))
- (unless (tailp new-stack pred-stack)
- (aver (search pred-stack new-stack))
- (stack-simulation-walk pred new-stack))))))))
+;;; Add LVARs from LATE to EARLY; use EQ to check whether EARLY has
+;;; been changed.
+(defun merge-uvl-live-sets (early late)
+ (declare (type list early late))
+ (dolist (e late early)
+ (pushnew e early)))
- (values))
-
-;;; Do stack annotation for any values generators in Block that were
-;;; unreached by all walks (i.e. the lvar isn't live at the point that
-;;; it is generated.) This will only happen when the values receiver cannot be
-;;; reached from this particular generator (due to an unconditional control
-;;; transfer.)
-;;;
-;;; What we do is push on the End-Stack all lvars in Pushed that
-;;; aren't already present in the End-Stack. When we find any pushed
-;;; lvar that isn't live, it must be the case that all lvars
-;;; pushed after (on top of) it aren't live.
+;;; Update information on stacks of unknown-values LVARs on the
+;;; boundaries of BLOCK. Return true if the start stack has been
+;;; changed.
;;;
-;;; If we see a pushed lvar that is the LVAR of a tail call, then we
-;;; ignore it, since the tail call didn't actually push anything. The
-;;; tail call must always the last in the block.
-(defun annotate-dead-values (block)
+;;; An LVAR is live at the end iff it is live at some of blocks, which
+;;; BLOCK can transfer control to. There are two kind of control
+;;; transfers: normal, expressed with BLOCK-SUCC, and NLX.
+(defun update-uvl-live-sets (block)
(declare (type cblock block))
(let* ((2block (block-info block))
- (stack (ir2-block-end-stack 2block))
- (last (block-last block))
- (tailp-lvar (if (node-tail-p last) (node-lvar last))))
- (do ((pushes (ir2-block-pushed 2block) (rest pushes))
- (popping nil))
- ((null pushes))
- (let ((push (first pushes)))
- (cond ((member push stack)
- (aver (not popping)))
- ((eq push tailp-lvar)
- (aver (null (rest pushes))))
- (t
- (push push (ir2-block-end-stack 2block))
- (setq popping t))))))
+ (original-start (ir2-block-start-stack 2block))
+ (end (ir2-block-end-stack 2block))
+ (new-end end))
+ (dolist (succ (block-succ block))
+ (setq new-end (merge-uvl-live-sets new-end
+ (ir2-block-start-stack (block-info succ)))))
+ (map-block-nlxes (lambda (nlx-info)
+ (let* ((nle (nlx-info-target nlx-info))
+ (nle-start-stack (ir2-block-start-stack
+ (block-info nle)))
+ (exit-lvar (nlx-info-lvar nlx-info))
+ (next-stack (if exit-lvar
+ (remove exit-lvar nle-start-stack)
+ nle-start-stack)))
+ (setq new-end (merge-uvl-live-sets
+ new-end next-stack))))
+ block)
- (values))
+ (setf (ir2-block-end-stack 2block) new-end)
+
+ (let ((start new-end))
+ (setq start (set-difference start (ir2-block-pushed 2block)))
+ (setq start (merge-uvl-live-sets start (ir2-block-popped 2block)))
+
+ ;; We cannot delete unused UVLs during NLX, so all UVLs live at
+ ;; ENTRY will be actually live at NLE.
+ (when (and (eq (component-head (block-component block))
+ (first (block-pred block)))
+ (not (bind-p (block-start-node block))))
+ (let* ((entry-block (nle-block-entry-block block))
+ (entry-stack (ir2-block-start-stack (block-info entry-block))))
+ (setq start (merge-uvl-live-sets start entry-stack))))
+
+ (when *check-consistency*
+ (aver (subsetp original-start start)))
+ (cond ((subsetp start original-start)
+ nil)
+ (t
+ (setf (ir2-block-start-stack 2block) start)
+ t)))))
+
+\f
+;;;; Ordering of live UVL stacks
+
+;;; Put UVLs on the start/end stacks of BLOCK in the right order. PRED
+;;; is a predecessor of BLOCK with already sorted stacks; because all
+;;; UVLs being live at the BLOCK start are live in PRED, we just need
+;;; to delete dead UVLs.
+(defun order-block-uvl-sets (block pred)
+ (let* ((2block (block-info block))
+ (pred-end-stack (ir2-block-end-stack (block-info pred)))
+ (start (ir2-block-start-stack 2block))
+ (start-stack (loop for lvar in pred-end-stack
+ when (memq lvar start)
+ collect lvar))
+ (end (ir2-block-end-stack 2block)))
+ (when *check-consistency*
+ (aver (subsetp start start-stack)))
+ (setf (ir2-block-start-stack 2block) start-stack)
+
+ (let* ((last (block-last block))
+ (tailp-lvar (if (node-tail-p last) (node-lvar last)))
+ (end-stack start-stack))
+ (dolist (pop (ir2-block-popped 2block))
+ (aver (eq pop (car end-stack)))
+ (pop end-stack))
+ (dolist (push (ir2-block-pushed 2block))
+ (aver (not (memq push end-stack)))
+ (push push end-stack))
+ (aver (subsetp end end-stack))
+ (when (and tailp-lvar
+ (eq (ir2-lvar-kind (lvar-info tailp-lvar)) :unknown))
+ (aver (eq tailp-lvar (first end-stack)))
+ (pop end-stack))
+ (setf (ir2-block-end-stack 2block) end-stack))))
+
+(defun order-uvl-sets (component)
+ (clear-flags component)
+ (loop with head = (component-head component)
+ with repeat-p do
+ (setq repeat-p nil)
+ (do-blocks (block component)
+ (unless (block-flag block)
+ (let ((pred (find-if #'block-flag (block-pred block))))
+ (when (and (eq pred head)
+ (not (bind-p (block-start-node block))))
+ (let ((entry (nle-block-entry-block block)))
+ (setq pred (if (block-flag entry) entry nil))))
+ (cond (pred
+ (setf (block-flag block) t)
+ (order-block-uvl-sets block pred))
+ (t
+ (setq repeat-p t))))))
+ while repeat-p))
\f
;;; This is called when we discover that the stack-top unknown-values
;;; lvar at the end of BLOCK1 is different from that at the start of
(defun discard-unused-values (block1 block2)
(declare (type cblock block1 block2))
(let* ((block1-stack (ir2-block-end-stack (block-info block1)))
- (block2-stack (ir2-block-start-stack (block-info block2)))
- (last-popped (elt block1-stack
- (- (length block1-stack)
- (length block2-stack)
- 1))))
- (aver (tailp block2-stack block1-stack))
-
- (let* ((block (insert-cleanup-code block1 block2
- (block-start-node block2)
- `(%pop-values ',last-popped)))
- (2block (make-ir2-block block)))
- (setf (block-info block) 2block)
- (add-to-emit-order 2block (block-info block1))
- (ltn-analyze-belated-block block)))
+ (block2-stack (ir2-block-start-stack (block-info block2)))
+ (cleanup-code
+ (cond ((eq (car block1-stack) (car block2-stack))
+ (binding* ((preserved-count (mismatch block1-stack block2-stack)
+ :exit-if-null)
+ (n-last-preserved (1- preserved-count))
+ (nipped-count (- (length block1-stack)
+ (length block2-stack)))
+ (n-last-nipped (+ n-last-preserved nipped-count)))
+ (aver (equal (nthcdr (1+ n-last-nipped) block1-stack)
+ (nthcdr preserved-count block2-stack)))
+ (compiler-notify "%NIP-VALUES emitted")
+ `(%nip-values ',(elt block1-stack n-last-nipped)
+ ',(elt block1-stack n-last-preserved)
+ ,@(loop for moved in block1-stack
+ repeat preserved-count
+ collect `',moved))))
+ (t
+ (let* ((n-popped (- (length block1-stack)
+ (length block2-stack)))
+ (last-popped (elt block1-stack (1- n-popped))))
+ (when *check-consistency*
+ (aver (equal block2-stack (nthcdr n-popped block1-stack))))
+ `(%pop-values ',last-popped))))))
+ (when cleanup-code
+ (let* ((block (insert-cleanup-code block1 block2
+ (block-start-node block2)
+ cleanup-code))
+ (2block (make-ir2-block block)))
+ (setf (block-info block) 2block)
+ (add-to-emit-order 2block (block-info block1))
+ (ltn-analyze-belated-block block))))
(values))
\f
;;; received. This phase doesn't need to be run when Values-Receivers
;;; is null, i.e. there are no unknown-values lvars used across block
;;; boundaries.
-;;;
-;;; Do the backward graph walk, starting at each values receiver. We
-;;; ignore receivers that already have a non-null START-STACK. These
-;;; are nested values receivers that have already been reached on
-;;; another walk. We don't want to clobber that result with our null
-;;; initial stack.
(defun stack-analyze (component)
(declare (type component component))
(let* ((2comp (component-info component))
(dolist (block generators)
(find-pushed-lvars block))
- (dolist (block receivers)
- (unless (ir2-block-start-stack (block-info block))
- (stack-simulation-walk block ())))
+ ;;; Compute sets of live UVLs
+ (loop for did-something = nil
+ do (do-blocks-backwards (block component)
+ (when (update-uvl-live-sets block)
+ (setq did-something t)))
+ while did-something)
- (dolist (block generators)
- (annotate-dead-values block))
+ (order-uvl-sets component)
(do-blocks (block component)
- (let ((top (car (ir2-block-end-stack (block-info block)))))
+ (let ((top (ir2-block-end-stack (block-info block))))
(dolist (succ (block-succ block))
(when (and (block-start succ)
- (not (eq (car (ir2-block-start-stack (block-info succ)))
+ (not (eq (ir2-block-start-stack (block-info succ))
top)))
(discard-unused-values block succ))))))