[llvm] c59f9ea - [MCP] Optimize copies from undef

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 29 06:12:32 PDT 2023


Author: pvanhout
Date: 2023-06-29T15:12:27+02:00
New Revision: c59f9eada1abe369c0470aa2dad8b20ac5269020

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

LOG: [MCP] Optimize copies from undef

Revert D152502 and instead optimize away copy from undefs, but clear the undef flag on the original copy.
Apparently, not optimizing the COPY can cause performance issues in some cases.

Fixes SWDEV-405813, SWDEV-405899

Reviewed By: arsenm

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

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 26110a1f2c233..3453e6c0b8be7 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -462,8 +462,7 @@ 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() ||
-      PrevCopyOperands->Source->isUndef())
+  if (PrevCopyOperands->Destination->isDead())
     return false;
   if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr))
     return false;
@@ -482,6 +481,12 @@ bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy,
        make_range(PrevCopy->getIterator(), Copy.getIterator()))
     MI.clearRegisterKills(CopyDef, TRI);
 
+  // Clear undef flag from remaining copy if needed.
+  if (!CopyOperands->Source->isUndef()) {
+    PrevCopy->getOperand(PrevCopyOperands->Source->getOperandNo())
+        .setIsUndef(false);
+  }
+
   Copy.eraseFromParent();
   Changed = true;
   ++NumDeletes;

diff  --git a/llvm/test/CodeGen/X86/machine-copy-prop.mir b/llvm/test/CodeGen/X86/machine-copy-prop.mir
index b38cb3c2c5806..f4fed03a75e67 100644
--- a/llvm/test/CodeGen/X86/machine-copy-prop.mir
+++ b/llvm/test/CodeGen/X86/machine-copy-prop.mir
@@ -8,13 +8,14 @@
   define void @copyprop0() { ret void }
   define void @copyprop1() { ret void }
   define void @copyprop2() { ret void }
+  define void @copyprop3() { ret void }
+  define void @copyprop4() { ret void }
   define void @nocopyprop0() { ret void }
   define void @nocopyprop1() { ret void }
   define void @nocopyprop2() { ret void }
   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
@@ -118,6 +119,44 @@ body: |
     NOOP implicit $rax, implicit $rdi
 ...
 ---
+# Check that undef is removed from the remaining copy because
+# the deleted COPY did not have it.
+# CHECK-LABEL: name: copyprop3
+# CHECK: bb.0:
+# CHECK:      $rax = COPY $rdi
+# CHECK-NEXT: NOOP implicit $rax
+# CHECK-NEXT: NOOP implicit $rax, implicit $rdi
+name: copyprop3
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $rdi
+
+    $rax = COPY undef $rdi
+    NOOP implicit killed $rax
+    $rax = COPY $rdi
+    NOOP implicit $rax, implicit $rdi
+...
+---
+# Check that undef is NOT removed from the remaining copy because
+# the deleted COPY also had it.
+# CHECK-LABEL: name: copyprop4
+# CHECK: bb.0:
+# CHECK:      $rax = COPY undef $rdi
+# CHECK-NEXT: NOOP implicit $rax
+# CHECK-NEXT: NOOP implicit $rax, implicit $rdi
+name: copyprop4
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $rdi
+
+    $rax = COPY undef $rdi
+    NOOP implicit killed $rax
+    $rax = COPY undef $rdi
+    NOOP implicit $rax, implicit $rdi
+...
+---
 # The second copy is not redundant if the source register ($rax) is clobbered
 # even if the dest ($rbp) is not.
 # CHECK-LABEL: name: nocopyprop0
@@ -214,24 +253,3 @@ 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