[llvm] [MachineCopyPropagation] When the source of PreviousCopy is undef, we cannot replace sub register (PR #74682)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 7 06:43:57 PST 2023


https://github.com/DianQK updated https://github.com/llvm/llvm-project/pull/74682

>From 115fd0030bf62372bfd0e3dc08881c165d08a29b Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Thu, 7 Dec 2023 22:40:33 +0800
Subject: [PATCH 1/2] [MachineCopyPropagation] Pre-commit test case

---
 .../test/CodeGen/AArch64/machine-cp-undef.mir | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)
 create mode 100644 llvm/test/CodeGen/AArch64/machine-cp-undef.mir

diff --git a/llvm/test/CodeGen/AArch64/machine-cp-undef.mir b/llvm/test/CodeGen/AArch64/machine-cp-undef.mir
new file mode 100644
index 0000000000000..e11b7055a2261
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/machine-cp-undef.mir
@@ -0,0 +1,23 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
+# RUN: llc -o - %s -O3 --run-pass=machine-cp -mcp-use-is-copy-instr -mtriple=arm64-apple-macos -mcpu=apple-m1 --verify-machineinstrs | FileCheck %s
+
+---
+name: test
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $x0, $x8
+
+    ; CHECK-LABEL: name: test
+    ; CHECK: liveins: $w0, $x0, $x8
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: $x8 = ORRXrs $xzr, $x0, 0, implicit $w0
+    ; CHECK-NEXT: $x0 = ADDXri $x8, 1, 0
+    ; CHECK-NEXT: RET undef $lr, implicit $x0
+    $x8 = ORRXrs $xzr, undef $x0, 0, implicit $w0
+    $w8 = ORRWrs $wzr, $w0, 0, implicit-def $x8
+    $x0 = ADDXri $x8, 1, 0
+
+    RET undef $lr, implicit $x0
+
+...

>From c35fa4073b6a97b14d45e1cb0a08c22acaa81955 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Thu, 7 Dec 2023 08:09:49 +0800
Subject: [PATCH 2/2] [MachineCopyPropagation] When the source of PreviousCopy
 is undef, we cannot replace sub register

The `postrapseudos` may replace `mov w8, w0` with `mov x8, x0`.
---
 llvm/lib/CodeGen/MachineCopyPropagation.cpp    | 3 +++
 llvm/test/CodeGen/AArch64/machine-cp-undef.mir | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index a032b31a1fc7c..fdbdaa0a6d641 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -443,6 +443,9 @@ static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src,
     return true;
   if (!TRI->isSubRegister(PreviousSrc, Src))
     return false;
+  // When the source of PreviousCopy is undef, we cannot replace sub register.
+  if (CopyOperands->Source->isUndef())
+    return false;
   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
 }
diff --git a/llvm/test/CodeGen/AArch64/machine-cp-undef.mir b/llvm/test/CodeGen/AArch64/machine-cp-undef.mir
index e11b7055a2261..0d68d1df47de8 100644
--- a/llvm/test/CodeGen/AArch64/machine-cp-undef.mir
+++ b/llvm/test/CodeGen/AArch64/machine-cp-undef.mir
@@ -11,7 +11,7 @@ body:             |
     ; CHECK-LABEL: name: test
     ; CHECK: liveins: $w0, $x0, $x8
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: $x8 = ORRXrs $xzr, $x0, 0, implicit $w0
+    ; CHECK-NEXT: $w8 = ORRWrs $wzr, $w0, 0, implicit-def $x8
     ; CHECK-NEXT: $x0 = ADDXri $x8, 1, 0
     ; CHECK-NEXT: RET undef $lr, implicit $x0
     $x8 = ORRXrs $xzr, undef $x0, 0, implicit $w0



More information about the llvm-commits mailing list