[llvm] [MachineSink] Allow readReg instead of isUse (PR #204610)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 08:27:11 PDT 2026


https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/204610

>From 50f4c80ae6c126de5bb66a0af5cec84697b57b11 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 18 Jun 2026 10:34:10 -0400
Subject: [PATCH 1/2] Pre-commit test (NFC)

---
 .../X86/postra-machine-sink-undef-use.mir     | 57 +++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir

diff --git a/llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir b/llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir
new file mode 100644
index 0000000000000..0b09f16a2b25c
--- /dev/null
+++ b/llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir
@@ -0,0 +1,57 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=x86_64-linux-gnu -run-pass=postra-machine-sink -o - %s | FileCheck %s
+
+---
+name:            test_undef_use_sinking
+tracksRegLiveness: true
+body:             |
+  ; CHECK-LABEL: name: test_undef_use_sinking
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   renamable $ebx = MOV32ri 1
+  ; CHECK-NEXT:   renamable $eax = COPY killed renamable $ebx, implicit undef $ecx
+  ; CHECK-NEXT:   $ecx = MOV32ri 0
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   liveins: $eax
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   RET 0, $eax
+  bb.0:
+    successors: %bb.1
+
+    renamable $ebx = MOV32ri 1
+    renamable $eax = COPY killed renamable $ebx, implicit undef $ecx
+    $ecx = MOV32ri 0
+
+  bb.1:
+    liveins: $eax
+    RET 0, $eax
+...
+---
+name:            test_internal_read_sinking
+tracksRegLiveness: true
+body:             |
+  ; CHECK-LABEL: name: test_internal_read_sinking
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   renamable $ebx = MOV32ri 1
+  ; CHECK-NEXT:   $edx = MOV32ri 0
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   liveins: $ebx, $ecx
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   renamable $eax = COPY killed renamable $ebx, implicit-def $ecx, implicit internal $ecx
+  ; CHECK-NEXT:   RET 0, $eax
+  bb.0:
+    successors: %bb.1
+
+    renamable $ebx = MOV32ri 1
+    renamable $eax = COPY killed renamable $ebx, implicit-def $ecx, implicit internal $ecx
+    $edx = MOV32ri 0
+
+  bb.1:
+    liveins: $eax, $ecx
+    RET 0, $eax
+...

>From f221a6465741f7371fdd0fc1995f618be2bb6ed5 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 18 Jun 2026 10:34:18 -0400
Subject: [PATCH 2/2] [MachineSink] Allow readReg instead of isUse

When an instruction (or bundle) is sunk, the entire instruction moves together. An internal read (isInternalRead()) indicates that the operand reads a value defined inside the same instruction or bundle. Because both the definition and the read move together, the internal read doesn't introduce any data dependency on the register's state from outside the instruction.

Therefore, ignoring internal reads when evaluating register dependencies for sinking is fundamentally safe across all targets.

Similarly, ignoring undef uses (which readsReg() also does) is safe because an undef operand explicitly doesn't care about the previous register value, meaning it doesn't create any cross-instruction data dependencies that would prevent sinking.
---
 llvm/lib/CodeGen/MachineSink.cpp                        | 7 +------
 llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir | 6 +++---
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp
index da9eaa68d22e0..b711d5c90281f 100644
--- a/llvm/lib/CodeGen/MachineSink.cpp
+++ b/llvm/lib/CodeGen/MachineSink.cpp
@@ -2223,12 +2223,7 @@ static bool hasRegisterDependency(MachineInstr *MI,
         break;
       }
       DefedRegsInCopy.push_back(Reg);
-
-      // FIXME: instead of isUse(), readsReg() would be a better fix here,
-      // For example, we can ignore modifications in reg with undef. However,
-      // it's not perfectly clear if skipping the internal read is safe in all
-      // other targets.
-    } else if (MO.isUse()) {
+    } else if (MO.readsReg()) {
       if (!ModifiedRegUnits.available(Reg)) {
         HasRegDependency = true;
         break;
diff --git a/llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir b/llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir
index 0b09f16a2b25c..7a59548076561 100644
--- a/llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir
+++ b/llvm/test/CodeGen/X86/postra-machine-sink-undef-use.mir
@@ -10,12 +10,12 @@ body:             |
   ; CHECK-NEXT:   successors: %bb.1(0x80000000)
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   renamable $ebx = MOV32ri 1
-  ; CHECK-NEXT:   renamable $eax = COPY killed renamable $ebx, implicit undef $ecx
   ; CHECK-NEXT:   $ecx = MOV32ri 0
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.1:
-  ; CHECK-NEXT:   liveins: $eax
+  ; CHECK-NEXT:   liveins: $ebx
   ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   renamable $eax = COPY killed renamable $ebx, implicit undef $ecx
   ; CHECK-NEXT:   RET 0, $eax
   bb.0:
     successors: %bb.1
@@ -40,7 +40,7 @@ body:             |
   ; CHECK-NEXT:   $edx = MOV32ri 0
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.1:
-  ; CHECK-NEXT:   liveins: $ebx, $ecx
+  ; CHECK-NEXT:   liveins: $ebx
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   renamable $eax = COPY killed renamable $ebx, implicit-def $ecx, implicit internal $ecx
   ; CHECK-NEXT:   RET 0, $eax



More information about the llvm-commits mailing list