0.8.7.13:
[sbcl.git] / src / compiler / stack.lisp
index b71a128..275cf22 100644 (file)
 \f
 ;;;; annotation graph walk
 
-;;; 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)))))
+;;; Add LVARs from LATE to EARLY; use EQ to check whether EARLY has
+;;; been changed.
+(defun merge-stacks (early late)
+  (declare (type list early late))
+  (cond ((null early) late)
+        ((null late) early)
+        ((tailp early late) late)
+        ((tailp late early) early)
+        ;; FIXME
+        (t (bug "Lexical unwinding of UVL stack is not implemented."))))
 
-      (dolist (pop (reverse (ir2-block-popped 2block)))
-       (push pop new-stack))
+;;; Update information on stacks of unknown-values LVARs on the
+;;; boundaries of BLOCK. Return true if the start stack has been
+;;; changed.
+(defun stack-update (block)
+  (declare (type cblock block))
+  (declare (optimize (debug 3)))
+  (let* ((2block (block-info block))
+         (end (ir2-block-end-stack 2block))
+         (new-end end)
+         (cleanup (block-end-cleanup block))
+         (found-similar-p nil))
+    (dolist (succ (block-succ block))
+      #+nil
+      (when (and (< block succ)
+                 (eq cleanup (block-end-cleanup succ)))
+        (setq found-similar-p t))
+      (setq new-end (merge-stacks new-end (ir2-block-start-stack (block-info succ)))))
+    (unless found-similar-p
+      (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)))
+                           (when (eq exit-lvar (car nle-start-stack))
+                             (pop nle-start-stack))
+                           (setq new-end (merge-stacks new-end
+                                                       nle-start-stack))))
+                       block))
 
-      (setf (ir2-block-start-stack 2block) new-stack)
+    (setf (ir2-block-end-stack 2block) new-end)
+    (let ((start new-end))
+      (dolist (push (reverse (ir2-block-pushed 2block)))
+        (if (eq (car start) push)
+            (pop start)
+            (aver (not (member push start)))))
 
-      (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))))))))
+      (dolist (pop (reverse (ir2-block-popped 2block)))
+        (push pop start))
 
-  (values))
+      (cond ((equal-but-no-car-recursion start
+                                         (ir2-block-start-stack 2block))
+             nil)
+            (t
+             (setf (ir2-block-start-stack 2block) start)
+             t)))))
 
 ;;; 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
 ;;; 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.
+;;;
+;;; [This function also fixes End-Stack in NLEs.]
 (defun annotate-dead-values (block)
   (declare (type cblock block))
   (let* ((2block (block-info block))
               (setq popping t))))))
 
   (values))
+
+;;; For every NLE block push all LVARs that are live in its ENTRY to
+;;; its start stack. (We cannot pop unused LVARs on a control transfer
+;;; to an NLE block, so we must do it later.)
+(defun fix-nle-block-stacks (component)
+  (declare (type component component))
+  (dolist (block (block-succ (component-head component)))
+    (let ((start-node (block-start-node block)))
+      (unless (bind-p start-node)
+        (let* ((2block (block-info block))
+               (start-stack (block-start-stack 2block))
+               (nlx-ref (ctran-next (node-next start-node)))
+               (nlx-info (constant-value (ref-leaf nlx-ref)))
+               (mess-up (cleanup-mess-up (nlx-info-cleanup nlx-info)))
+               (entry-block (node-block mess-up))
+               (entry-stack (ir2-block-start-stack (block-info entry-block)))
+               (exit-lvar (nlx-info-lvar nlx-info)))
+          (when (and exit-lvar
+                     (eq exit-lvar (car start-stack)))
+            (when *check-consistency*
+              (aver (not (memq exit-var entry-stack))))
+            (push exit-var entry-stack))
+          (when *check-consistency*
+            (aver (subsetp start-stack entry-stack)))
+          (setf (ir2-block-start-stack 2block) entry-stack)
+          (setf (ir2-block-end-stack 2block) entry-stack)
+                                        ; ANNOTATE-DEAD-VALUES will do the rest
+          )))))
 \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
     (dolist (block generators)
       (find-pushed-lvars block))
 
-    (dolist (block receivers)
-      (unless (ir2-block-start-stack (block-info block))
-       (stack-simulation-walk block ())))
+    (loop for did-something = nil
+          do (do-blocks-backwards (block component)
+               (when (stack-update block)
+                 (setq did-something t)))
+          while did-something)
+
+    (when *check-consistency*
+      (dolist (block (block-succ (component-head component)))
+        (when (bind-p (block-start-node block))
+          (aver (null (ir2-block-start-stack (block-info block)))))))
 
     (dolist (block generators)
       (annotate-dead-values block))