[llvm] df1782c - [MCP] Do not remove redundant copy for COPY from undef

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 05:24:04 PDT 2023


Author: pvanhout
Date: 2023-06-09T14:23:57+02:00
New Revision: df1782c2a2af9938ba4c5bacfab20d1ddebc82dd

URL: https://github.com/llvm/llvm-project/commit/df1782c2a2af9938ba4c5bacfab20d1ddebc82dd
DIFF: https://github.com/llvm/llvm-project/commit/df1782c2a2af9938ba4c5bacfab20d1ddebc82dd.diff

LOG: [MCP] Do not remove redundant copy for COPY from undef

I don't think we can safely remove the second COPY as redundant in such cases.
The first COPY (which has undef src) may be lowered to a KILL instruction instead, resulting in no COPY being emitted at all.

Testcase is X86 so it's in the same place as other testcases for this function, but this was initially spotted on AMDGPU with the following:
```
 renamable $vgpr24 = PRED_COPY undef renamable $vgpr25, implicit $exec
 renamable $vgpr24 = PRED_COPY killed renamable $vgpr25, implicit $exec
```
The second COPY waas removed as redundant, and the first one was lowered to a KILL (= removed too), causing $vgpr24 to not have $vgpr25's value.

Fixes SWDEV-401507

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D152502

Added: 
    

Modified: 
    llvm/lib/CodeGen/MachineCopyPropagation.cpp
    llvm/test/CodeGen/X86/machine-copy-prop.mir

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 1f1601b12c2c1..03eac55e39792 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -462,7 +462,8 @@ bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy,
 
   auto PrevCopyOperands = isCopyInstr(*PrevCopy, *TII, UseCopyInstr);
   // Check that the existing copy uses the correct sub registers.
-  if (PrevCopyOperands->Destination->isDead())
+  if (PrevCopyOperands->Destination->isDead() ||
+      PrevCopyOperands->Source->isUndef())
     return false;
   if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr))
     return false;

diff  --git a/llvm/test/CodeGen/X86/machine-copy-prop.mir b/llvm/test/CodeGen/X86/machine-copy-prop.mir
index cf6ce3d6c821a..b38cb3c2c5806 100644
--- a/llvm/test/CodeGen/X86/machine-copy-prop.mir
+++ b/llvm/test/CodeGen/X86/machine-copy-prop.mir
@@ -14,6 +14,7 @@
   define void @nocopyprop3() { ret void }
   define void @nocopyprop4() { ret void }
   define void @nocopyprop5() { ret void }
+  define void @nocopyprop6() { ret void }
 ...
 ---
 # The second copy is redundant and will be removed, check that we also remove
@@ -213,3 +214,24 @@ body: |
     $rip = COPY $rax
     $rip = COPY $rax
 ...
+---
+# The copies are obviously redundant, but the earlier COPY copies from "undef".
+# That copy may be lowered to a KILL instead and thus we cannot remove the
+# second COPY, otherwise we risk making $rax undef.
+# CHECK-LABEL: name: nocopyprop6
+# CHECK: bb.0:
+# CHECK:      $rax = COPY undef $rdi
+# CHECK-NEXT: NOOP implicit killed $rax
+# CHECK-NEXT: $rax = COPY $rdi
+# CHECK-NEXT: NOOP implicit $rax, implicit $rdi
+name: nocopyprop6
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $rdi
+
+    $rax = COPY undef $rdi
+    NOOP implicit killed $rax
+    $rax = COPY $rdi
+    NOOP implicit $rax, implicit $rdi
+...


        


More information about the llvm-commits mailing list