sbcl-0.8.14.11:
[sbcl.git] / src / compiler / stack.lisp
index 83ce8f9..fccf4f8 100644 (file)
@@ -1,7 +1,10 @@
 ;;;; 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 remove unused values.
+;;;; analyse lifetime of dynamically allocated object packets on stack
+;;;; and insert cleanups where necessary.
+;;;;
+;;;; Currently there are two kinds of interesting stack packets: UVLs,
+;;;; whose use and destination lie in different blocks, and LVARs of
+;;;; constructors of dynamic-extent objects.
 
 ;;;; This software is part of the SBCL system. See the README file for
 ;;;; more information.
          (when (eq node last-pop)
            (setq saw-last t))
 
-         (when lvar
-            (let ((dest (lvar-dest lvar))
-                  (2lvar (lvar-info lvar)))
-              (when (and (not (eq (node-block dest) block))
-                         2lvar
-                         (eq (ir2-lvar-kind 2lvar) :unknown))
-                (aver (or saw-last (not last-pop)))
-                (pushed lvar))))))
+         (when (and lvar
+                     (or (lvar-dynamic-extent lvar)
+                         (let ((dest (lvar-dest lvar))
+                               (2lvar (lvar-info lvar)))
+                           (and (not (eq (node-block dest) block))
+                                2lvar
+                                (eq (ir2-lvar-kind 2lvar) :unknown)))))
+            (aver (or saw-last (not last-pop)))
+            (pushed lvar))))
 
       (setf (ir2-block-pushed 2block) (pushed))))
   (values))
                                               nle-start-stack)))
                          (setq new-end (merge-uvl-live-sets
                                         new-end next-stack))))
-                     block)
+                     block
+                     (lambda (dx-cleanup)
+                       (dolist (lvar (cleanup-info dx-cleanup))
+                         (let* ((generator (lvar-use lvar))
+                                (block (node-block generator))
+                                (2block (block-info block)))
+                           (aver (eq generator (block-last block)))
+                           ;; DX objects, living in the LVAR, are
+                           ;; alive in the environment, protected by
+                           ;; the CLEANUP. We also cannot move them
+                           ;; (because, in general, we cannot track
+                           ;; all references to them). Therefore,
+                           ;; everything, allocated deeper than a DX
+                           ;; object, should be kept alive until the
+                           ;; object is deallocated.
+                           (setq new-end (merge-uvl-live-sets
+                                          new-end (ir2-block-end-stack 2block)))
+                           (setq new-end (merge-uvl-live-sets
+                                          new-end (ir2-block-pushed 2block)))))))
 
     (setf (ir2-block-end-stack 2block) new-end)
 
 
       ;; We cannot delete unused UVLs during NLX, so all UVLs live at
       ;; ENTRY will be actually live at NLE.
+      ;;
+      ;; BUT, UNWIND-PROTECTor is called in the environment, which has
+      ;; nothing in common with the environment of its entry. So we
+      ;; fictively compute its stack from the containing cleanups, but
+      ;; do not propagate additional LVARs from the entry, thus
+      ;; preveting bogus stack cleanings.
+      ;;
+      ;; TODO: Insert a check that no values are discarded in UWP. Or,
+      ;; maybe, we just don't need to create NLX-ENTRY for UWP?
       (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))))
+        (let* ((nlx-info (nle-block-nlx-info block))
+               (cleanup (nlx-info-cleanup nlx-info)))
+          (unless (eq (cleanup-kind cleanup) :unwind-protect)
+            (let* ((entry-block (node-block (cleanup-mess-up cleanup)))
+                   (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)))
 ;;; wastes only space.
 (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)))
-         (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
+  (collect ((cleanup-code))
+    (labels ((find-popped (before after)
+               ;; Returns (VALUES popped last-popped rest), where
+               ;; BEFORE = (APPEND popped rest) and
+               ;; (EQ (FIRST rest) (FIRST after))
+               (if (null after)
+                   (values before (first (last before)) nil)
+                   (loop with first-preserved = (car after)
+                         for last-popped = nil then maybe-popped
+                         for rest on before
+                         for maybe-popped = (car rest)
+                         while (neq maybe-popped first-preserved)
+                         collect maybe-popped into popped
+                         finally (return (values popped last-popped rest)))))
+             (discard (before-stack after-stack)
+               (cond
+                 ((eq (car before-stack) (car after-stack))
+                  (binding* ((moved-count (mismatch before-stack after-stack)
+                                          :exit-if-null)
+                             ((moved qmoved)
+                              (loop for moved-lvar in before-stack
+                                    repeat moved-count
+                                    collect moved-lvar into moved
+                                    collect `',moved-lvar into qmoved
+                                    finally (return (values moved qmoved))))
+                             (q-last-moved (car (last qmoved)))
+                             ((nil last-nipped rest)
+                              (find-popped (nthcdr moved-count before-stack)
+                                           (nthcdr moved-count after-stack))))
+                    (cleanup-code
+                     `(%nip-values ',last-nipped ,q-last-moved
+                       ,@qmoved))
+                    (discard (nconc moved rest) after-stack)))
+                 (t
+                  (multiple-value-bind (popped last-popped rest)
+                      (find-popped before-stack after-stack)
+                    (declare (ignore popped))
+                    (cleanup-code `(%pop-values ',last-popped))
+                    (discard rest after-stack))))))
+      (discard (ir2-block-end-stack (block-info block1))
+               (ir2-block-start-stack (block-info block2))))
+    (when (cleanup-code)
       (let* ((block (insert-cleanup-code block1 block2
                                          (block-start-node block2)
-                                         cleanup-code))
+                                         `(progn ,@(cleanup-code))))
              (2block (make-ir2-block block)))
         (setf (block-info block) 2block)
         (add-to-emit-order 2block (block-info block1))
 ;;;; stack analysis
 
 ;;; Return a list of all the blocks containing genuine uses of one of
-;;; the RECEIVERS. Exits are excluded, since they don't drop through
-;;; to the receiver.
-(defun find-values-generators (receivers)
-  (declare (list receivers))
+;;; the RECEIVERS (blocks) and DX-LVARS. Exits are excluded, since
+;;; they don't drop through to the receiver.
+(defun find-pushing-blocks (receivers dx-lvars)
+  (declare (list receivers dx-lvars))
   (collect ((res nil adjoin))
     (dolist (rec receivers)
       (dolist (pop (ir2-block-popped (block-info rec)))
        (do-uses (use pop)
          (unless (exit-p use)
            (res (node-block use))))))
+    (dolist (dx-lvar dx-lvars)
+      (do-uses (use dx-lvar)
+        (res (node-block use))))
     (res)))
 
-;;; Analyze the use of unknown-values lvars in COMPONENT, inserting
-;;; cleanup code to discard values that are generated but never
-;;; 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.
+;;; Analyze the use of unknown-values and DX lvars in COMPONENT,
+;;; inserting cleanup code to discard values that are generated but
+;;; never received. This phase doesn't need to be run when
+;;; Values-Receivers and Dx-Lvars are null, i.e. there are no
+;;; unknown-values lvars used across block boundaries and no DX LVARs.
 (defun stack-analyze (component)
   (declare (type component component))
   (let* ((2comp (component-info component))
         (receivers (ir2-component-values-receivers 2comp))
-        (generators (find-values-generators receivers)))
+        (generators (find-pushing-blocks receivers
+                                          (component-dx-lvars component))))
 
     (dolist (block generators)
       (find-pushed-lvars block))
 
-    ;;; Compute sets of live UVLs
+    ;;; Compute sets of live UVLs and DX LVARs
     (loop for did-something = nil
           do (do-blocks-backwards (block component)
                (when (update-uvl-live-sets block)