[llvm] [CodeGen] TwoAddress: only keep undef REG_SEQUENCE COPY when tracking subreg liveness (PR #202599)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 17 03:45:23 PDT 2026
https://github.com/HelloWorldU updated https://github.com/llvm/llvm-project/pull/202599
>From 00727f54b5ba995c9e870e94104acc5ba5ff9f3c Mon Sep 17 00:00:00 2001
From: HelloWorldU <asd001liu at gmail.com>
Date: Wed, 17 Jun 2026 18:44:41 +0800
Subject: [PATCH] [CodeGen] RegisterCoalescer: keep partial redefs valid when
eliminating undef copies
eliminateUndefCopy deletes a COPY of an undef value and marks the explicit
uses of the removed value as undef. A partial subregister def that implicitly
reads the lanes it does not write was skipped, so after the value was removed
it kept reading a value that no longer exists. On targets that do not track
subregister liveness (e.g. armv8a without MVE) RegisterCoalescer then asserted
with "Instruction is reading nonexistent value". Turn such exposed partial
redefs into read-undef defs.
Fixes #202263.
---
llvm/lib/CodeGen/RegisterCoalescer.cpp | 33 ++++++++++++++++---
.../regcoalesce-undef-copy-subreg-redef.ll | 26 +++++++++++++++
2 files changed, 55 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/CodeGen/ARM/regcoalesce-undef-copy-subreg-redef.ll
diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index eb43eddfa081e..117d2cfa657e7 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -1816,10 +1816,35 @@ MachineInstr *RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI) {
// Mark uses as undef.
for (MachineOperand &MO : MRI->reg_nodbg_operands(DstReg)) {
- if (MO.isDef() /*|| MO.isUndef()*/)
+ if (MO.isUndef())
continue;
- const MachineInstr &MI = *MO.getParent();
- SlotIndex UseIdx = LIS->getInstructionIndex(MI);
+
+ MachineInstr *MI = MO.getParent();
+
+ // A partial subreg def also reads the lanes it does not write. Once the
+ // undef value it read has been removed, that read has no incoming value,
+ // so turn it into a read-undef def.
+ if (MO.isDef()) {
+ if (MI == CopyMI)
+ continue;
+
+ unsigned SubReg = MO.getSubReg();
+ if (SubReg == 0 || !MO.readsReg())
+ continue;
+
+ SlotIndex DefIdx =
+ LIS->getInstructionIndex(*MI).getRegSlot(MO.isEarlyClobber());
+ if (DstLI.hasSubRanges()) {
+ addUndefFlag(DstLI, DefIdx, MO, SubReg);
+ } else if (!DstLI.Query(DefIdx).valueIn()) {
+ MO.setIsUndef(true);
+ LLVM_DEBUG(dbgs() << "\tnew read-undef: " << DefIdx << '\t' << *MI);
+ }
+ continue;
+ }
+
+ // Regular uses read at the instruction's use slot.
+ SlotIndex UseIdx = LIS->getInstructionIndex(*MI);
LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(MO.getSubReg());
bool isLive;
if (!UseMask.all() && DstLI.hasSubRanges()) {
@@ -1837,7 +1862,7 @@ MachineInstr *RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI) {
if (isLive)
continue;
MO.setIsUndef(true);
- LLVM_DEBUG(dbgs() << "\tnew undef: " << UseIdx << '\t' << MI);
+ LLVM_DEBUG(dbgs() << "\tnew undef: " << UseIdx << '\t' << *MI);
}
// A def of a subregister may be a use of the other subregisters, so
diff --git a/llvm/test/CodeGen/ARM/regcoalesce-undef-copy-subreg-redef.ll b/llvm/test/CodeGen/ARM/regcoalesce-undef-copy-subreg-redef.ll
new file mode 100644
index 0000000000000..34e34cfc63ec6
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/regcoalesce-undef-copy-subreg-redef.ll
@@ -0,0 +1,26 @@
+; REQUIRES: arm-registered-target
+; RUN: llc -mtriple=armv8a-unknown-linux -verify-machineinstrs < %s -o /dev/null
+
+; Regression test for issue #202263. Lowering the REG_SEQUENCE that builds the
+; <2 x i64> keeps a COPY of an undef value which feeds a later partial
+; subregister redef. When RegisterCoalescer eliminates that undef copy, the
+; redef must be turned into a read-undef def. Otherwise the coalescer asserts
+; with "Instruction is reading nonexistent value" on targets that do not track
+; subregister liveness (armv8a A-profile has no MVE).
+
+target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
+target triple = "armv8a-unknown-linux"
+
+define void @init(i64 %x, i1 %c, ptr %p) {
+entry:
+ %v0 = insertelement <2 x i64> poison, i64 %x, i64 1
+ br i1 %c, label %exit, label %body
+
+body:
+ %v1 = insertelement <2 x i64> %v0, i64 1, i64 0
+ store <2 x i64> %v1, ptr %p, align 8
+ br label %exit
+
+exit:
+ ret void
+}
More information about the llvm-commits
mailing list