[AMDGPU] Prevent Machine Copy Propagation from replacing live copy with the dead one

Differential revision: https://reviews.llvm.org/D38754

llvm-svn: 315908
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 4655b5b..030db43 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -275,18 +275,20 @@
         ClobberRegister(Reg);
       }
 
-      // Remember Def is defined by the copy.
-      for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
-           ++SR) {
-        CopyMap[*SR] = MI;
-        AvailCopyMap[*SR] = MI;
-      }
+      if (!MI->getOperand(0).isDead() && !MI->getOperand(1).isUndef()) {
+        // Remember Def is defined by the copy.
+        for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
+             ++SR) {
+          CopyMap[*SR] = MI;
+          AvailCopyMap[*SR] = MI;
+        }
 
-      // Remember source that's copied to Def. Once it's clobbered, then
-      // it's no longer available for copy propagation.
-      RegList &DestList = SrcMap[Src];
-      if (!is_contained(DestList, Def))
-        DestList.push_back(Def);
+        // Remember source that's copied to Def. Once it's clobbered, then
+        // it's no longer available for copy propagation.
+        RegList &DestList = SrcMap[Src];
+        if (!is_contained(DestList, Def))
+          DestList.push_back(Def);
+      }
 
       continue;
     }