Index: /trunk/source/compiler/X86/x862.lisp
===================================================================
--- /trunk/source/compiler/X86/x862.lisp	(revision 15050)
+++ /trunk/source/compiler/X86/x862.lisp	(revision 15051)
@@ -1001,5 +1001,13 @@
             *x862-trust-declarations* (neq 0 (%ilogand2 $decl_trustdecls decls))))))
 
-
+(defun x862-nvr-p (reg)
+  (target-arch-case
+   (:x8664
+    ;; For the sake of argument, x8664::save3 is always an nvr (even though
+    ;; we have to keep the TCR in it on some platforms.)
+    (and (node-reg-p reg) (logbitp (hard-regspec-value reg) x8664-nonvolatile-registers-mask)))
+   (:x8632
+    ;; We might have some caller-save NVRs on x8632 someday, but for now:
+    nil)))
     
 ;;; Vpush the last N non-volatile-registers.
@@ -7536,4 +7544,38 @@
                                        ($ x8664::arg_y)
                                        ($ x8664::arg_z))))))))
+        ;; A form that's a lexical reference to X that's ultimately going
+        ;; to be stored in X is a noop.
+        (collect ((new-forms)
+                  (new-vars)
+                  (new-regs))
+          (do* ((xforms forms (cdr xforms))
+                (xvars vars (cdr xvars))
+                (xregs regs (cdr xregs))
+                (new-nargs 0))
+               ((null xforms)
+                (setq nargs new-nargs
+                      forms (new-forms)
+                      vars (new-vars)
+                      regs (new-regs)))
+            (declare (fixnum new-nargs))
+            (let* ((var (car xvars))
+                   (form (car xforms)))
+              (unless (and (eq var (nx2-lexical-reference-p form))
+                           (not (logbitp $vbitsetq (nx-var-bits var))))
+                (incf new-nargs)
+                (new-vars var)
+                (new-forms form)
+                (new-regs (car xregs))))))
+        (dotimes (i nargs)
+          (let* ((var (nth i vars))
+                 (nvr (var-nvr var)))
+            (when nvr
+              (when (dotimes (j nargs t)
+                      (unless (= i j)
+                        (let* ((form (nth j forms)))
+                          (unless (and (nx2-var-not-set-by-form-p var form)
+                                       (nx2-var-not-reffed-by-form-p var form))
+                            (return)))))
+                (setf (nth i regs) nvr)))))
         (case nargs
           (1 (x862-one-targeted-reg-form seg (car forms) (car regs)))
Index: /trunk/source/compiler/nx2.lisp
===================================================================
--- /trunk/source/compiler/nx2.lisp	(revision 15050)
+++ /trunk/source/compiler/nx2.lisp	(revision 15051)
@@ -227,4 +227,31 @@
                         (not-set-in-formlist (cadr subforms))))))))))
 
+(defun nx2-var-not-reffed-by-form-p (var form &optional closed)
+  (setq form (acode-unwrapped-form form))
+  (unless (eq var (nx2-lexical-reference-p form))
+    (or (atom form)
+        (nx2-lexical-reference-p form)  ;not us
+        (nx2-constant-form-p form)
+        (let ((op (acode-operator form))
+              (subforms nil))
+          (if (eq op (%nx1-operator setq-lexical))
+            (and (neq var (cadr form))
+                 (nx2-var-not-reffed-by-form-p var (caddr form)))
+            (and (or (not closed)
+                     (logbitp operator-side-effect-free-bit op))
+                 (flet ((not-reffed-in-formlist (formlist)
+                          (dolist (subform formlist t)
+                            (unless (nx2-var-not-reffed-by-form-p var subform closed) (return)))))
+                   (if
+                     (cond ((%ilogbitp operator-acode-subforms-bit op) (setq subforms (%cdr form)))
+                           ((%ilogbitp operator-acode-list-bit op) (setq subforms (cadr form))))
+                     (not-reffed-in-formlist subforms)
+                     (and (or (eq op (%nx1-operator call))
+                              (eq op (%nx1-operator lexical-function-call)))
+                          (nx2-var-not-reffed-by-form-p var (cadr form))
+                          (setq subforms (caddr form))
+                          (not-reffed-in-formlist (car subforms))
+                          (not-reffed-in-formlist (cadr subforms)))))))))))
+
 (defun nx2-node-gpr-p (reg)
   (and reg
