[llvm-branch-commits] [llvm] d82b356 - [CodeGen] Ensure `undef` propagation when an undef copy is eliminated if used as a subregister def (#204039)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jul 29 22:52:39 PDT 2026


Author: Ryan Buchner
Date: 2026-07-29T18:02:38Z
New Revision: d82b356e26bf6fc175b01b949fd3982084b1c572

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

LOG: [CodeGen] Ensure `undef` propagation when an undef copy is eliminated if used as a subregister def (#204039)

If we eliminate an `undef` copy and there is a use of the copy that
`isDef` on just a sub-register, mark it as `undef`.

Fixes #204036.

(cherry picked from commit c85d1c54c5fba6344b117e369328a4b9b5a1386b)

Added: 
    llvm/test/CodeGen/ARM/coalesce-copy-undef.ll
    llvm/test/CodeGen/ARM/coalesce-copy-undef.mir

Modified: 
    llvm/lib/CodeGen/RegisterCoalescer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index 4b4ba2144f964..fa1616e57bb19 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -1816,11 +1816,13 @@ MachineInstr *RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI) {
 
   // Mark uses as undef.
   for (MachineOperand &MO : MRI->reg_nodbg_operands(DstReg)) {
-    if (MO.isDef() /*|| MO.isUndef()*/)
+    if (MO.isDef() && !MO.getSubReg())
       continue;
     const MachineInstr &MI = *MO.getParent();
     SlotIndex UseIdx = LIS->getInstructionIndex(MI);
     LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(MO.getSubReg());
+    if (MO.isDef())
+      UseMask = ~UseMask;
     bool isLive;
     if (!UseMask.all() && DstLI.hasSubRanges()) {
       isLive = false;

diff  --git a/llvm/test/CodeGen/ARM/coalesce-copy-undef.ll b/llvm/test/CodeGen/ARM/coalesce-copy-undef.ll
new file mode 100644
index 0000000000000..438be5cabf28b
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/coalesce-copy-undef.ll
@@ -0,0 +1,33 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=armv7-- < %s | FileCheck %s
+
+define void @copy_undef_propogate(i32 %in, ptr %out) {
+; CHECK-LABEL: copy_undef_propogate:
+; CHECK:       @ %bb.0: @ %entry
+; CHECK-NEXT:    vmov.32 d17[0], r0
+; CHECK-NEXT:    @ implicit-def: $r0
+; CHECK-NEXT:  .LBB0_1: @ %for.cond
+; CHECK-NEXT:    @ =>This Inner Loop Header: Depth=1
+; CHECK-NEXT:    vmov.32 d16[0], r0
+; CHECK-NEXT:    mov r2, r1
+; CHECK-NEXT:    vorr d18, d16, d16
+; CHECK-NEXT:    vorr q10, q8, q8
+; CHECK-NEXT:    vst1.32 {d16, d17}, [r2:128]!
+; CHECK-NEXT:    vmov.32 d18[1], r0
+; CHECK-NEXT:    add r0, r0, #1
+; CHECK-NEXT:    vorr d20, d18, d18
+; CHECK-NEXT:    vst1.64 {d20, d21}, [r2:128]
+; CHECK-NEXT:    b .LBB0_1
+entry:
+  %1 = insertelement <8 x i32> <i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>, i32 %in, i64 6
+  br label %for.cond
+
+for.cond:
+  %i.0 = phi i32 [ poison, %entry ], [ %inc, %for.cond ]
+  %2 = insertelement <8 x i32> %1, i32 %i.0, i64 4
+  %3 = insertelement <8 x i32> %2, i32 %i.0, i64 5
+  store <8 x i32> %3, ptr %out
+  %inc = add nsw i32 %i.0, 1
+  br label %for.cond
+}
+

diff  --git a/llvm/test/CodeGen/ARM/coalesce-copy-undef.mir b/llvm/test/CodeGen/ARM/coalesce-copy-undef.mir
new file mode 100644
index 0000000000000..224f7e47e20bc
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/coalesce-copy-undef.mir
@@ -0,0 +1,22 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=armv7-- -run-pass=register-coalescer -verify-coalescing -o - %s | FileCheck %s
+
+# Make sure we don't crash on this input
+
+---
+name: copy_undef_propogate
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $r0
+
+    ; CHECK-LABEL: name: copy_undef_propogate
+    ; CHECK: liveins: $r0
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: KILL $r0
+    %0:gpr = COPY killed $r0
+    %1:dpr = VSETLNi32 undef %1, killed %0, 0, 14 /* CC::al */, $noreg
+    undef %2.dsub_0:qpr = COPY undef %3:dpr
+    %2.dsub_1:qpr = COPY killed %1
+    %4:dpr = COPY %2.dsub_0
+...


        


More information about the llvm-branch-commits mailing list