[llvm] [RISCV] Ignore debug uses in vector peephole folds (PR #230319)
Yongqiang Tian via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 04:44:29 PDT 2026
https://github.com/yqtian-se updated https://github.com/llvm/llvm-project/pull/230319
>From 3d21218e5d37658d237073dd966975612a006550 Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Fri, 9 Oct 2026 03:29:10 +0000
Subject: [PATCH 1/5] [RISCV] Precommit debug-use vector peephole tests
Add MIR coverage for five folds affected by debug-only uses and a
negative case with two non-debug uses. Record the current behavior
before changing the pass.
Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.
---
.../RISCV/rvv/vector-peephole-debug.mir | 188 ++++++++++++++++++
1 file changed, 188 insertions(+)
create mode 100644 llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
diff --git a/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
new file mode 100644
index 0000000000000..5391b70ea2aa5
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
@@ -0,0 +1,188 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=riscv64 -mattr=+v -run-pass=riscv-vector-peephole \
+# RUN: -verify-machineinstrs %s -o - | FileCheck %s
+
+--- |
+ define void @fold_vmv_debug_use() !dbg !4 { ret void }
+ define void @fold_vmerge_debug_use() !dbg !11 { ret void }
+ define void @fold_vmand_debug_use() !dbg !15 { ret void }
+ define void @fold_undef_passthru_debug_use() !dbg !19 { ret void }
+ define void @convert_same_mask_debug_use() !dbg !23 { ret void }
+ define void @dont_fold_two_nondebug_uses() !dbg !27 { ret void }
+
+ !llvm.dbg.cu = !{!0}
+ !llvm.module.flags = !{!2, !3}
+
+ !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+ !1 = !DIFile(filename: "vector-peephole-debug.mir", directory: "")
+ !2 = !{i32 7, !"Dwarf Version", i32 5}
+ !3 = !{i32 2, !"Debug Info Version", i32 3}
+ !4 = distinct !DISubprogram(name: "fold_vmv_debug_use", scope: !1, file: !1, line: 1, type: !5, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !7)
+ !5 = !DISubroutineType(types: !6)
+ !6 = !{}
+ !7 = !{!8}
+ !8 = !DILocalVariable(name: "vmv", scope: !4, file: !1, line: 1, type: !9)
+ !9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+ !10 = !DILocation(line: 1, column: 1, scope: !4)
+ !11 = distinct !DISubprogram(name: "fold_vmerge_debug_use", scope: !1, file: !1, line: 2, type: !5, scopeLine: 2, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
+ !12 = !{!13}
+ !13 = !DILocalVariable(name: "vmerge", scope: !11, file: !1, line: 2, type: !9)
+ !14 = !DILocation(line: 2, column: 1, scope: !11)
+ !15 = distinct !DISubprogram(name: "fold_vmand_debug_use", scope: !1, file: !1, line: 3, type: !5, scopeLine: 3, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !16)
+ !16 = !{!17}
+ !17 = !DILocalVariable(name: "vmand", scope: !15, file: !1, line: 3, type: !9)
+ !18 = !DILocation(line: 3, column: 1, scope: !15)
+ !19 = distinct !DISubprogram(name: "fold_undef_passthru_debug_use", scope: !1, file: !1, line: 4, type: !5, scopeLine: 4, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !20)
+ !20 = !{!21}
+ !21 = !DILocalVariable(name: "undef_passthru", scope: !19, file: !1, line: 4, type: !9)
+ !22 = !DILocation(line: 4, column: 1, scope: !19)
+ !23 = distinct !DISubprogram(name: "convert_same_mask_debug_use", scope: !1, file: !1, line: 5, type: !5, scopeLine: 5, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !24)
+ !24 = !{!25}
+ !25 = !DILocalVariable(name: "same_mask", scope: !23, file: !1, line: 5, type: !9)
+ !26 = !DILocation(line: 5, column: 1, scope: !23)
+ !27 = distinct !DISubprogram(name: "dont_fold_two_nondebug_uses", scope: !1, file: !1, line: 6, type: !5, scopeLine: 6, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !28)
+ !28 = !{!29}
+ !29 = !DILocalVariable(name: "two_uses", scope: !27, file: !1, line: 6, type: !9)
+ !30 = !DILocation(line: 6, column: 1, scope: !27)
+...
+---
+name: fold_vmv_debug_use
+body: |
+ bb.0:
+ liveins: $v8
+ ; CHECK-LABEL: name: fold_vmv_debug_use
+ ; CHECK: liveins: $v8
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %passthru:vr = COPY $v8
+ ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
+ ; CHECK-NEXT: %y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
+ ; CHECK-NEXT: DBG_VALUE %y, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
+ %passthru:vr = COPY $v8
+ %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
+ DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !10
+ %y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5, 1
+ DBG_VALUE %y, $noreg, !8, !DIExpression(), debug-location !10
+...
+---
+name: fold_vmerge_debug_use
+body: |
+ bb.0:
+ liveins: $x8, $v0, $v8
+ ; CHECK-LABEL: name: fold_vmerge_debug_use
+ ; CHECK: liveins: $x8, $v0, $v8
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %avl:gprnox0 = COPY $x8
+ ; CHECK-NEXT: %passthru:vrnov0 = COPY $v8
+ ; CHECK-NEXT: %x:vrnov0 = PseudoVLE32_V_M1 $noreg, $noreg, %avl /* vl */, 5 /* e32 */, 2 /* tu, ma */ :: (load unknown-size, align 1)
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ ; CHECK-NEXT: %xcopy:vrnov0 = COPY %x
+ ; CHECK-NEXT: DBG_VALUE %xcopy, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ ; CHECK-NEXT: %mask:vmv0 = COPY $v0
+ ; CHECK-NEXT: %y:vrnov0 = PseudoVMERGE_VVM_M1 %passthru, %passthru, %xcopy, %mask, %avl /* vl */, 5 /* e32 */
+ ; CHECK-NEXT: DBG_VALUE %y, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ %avl:gprnox0 = COPY $x8
+ %passthru:vrnov0 = COPY $v8
+ %x:vrnov0 = PseudoVLE32_V_M1 $noreg, $noreg, %avl, 5, 2 :: (load unknown-size)
+ DBG_VALUE %x, $noreg, !13, !DIExpression(), debug-location !14
+ %xcopy:vrnov0 = COPY %x
+ DBG_VALUE %xcopy, $noreg, !13, !DIExpression(), debug-location !14
+ %mask:vmv0 = COPY $v0
+ %y:vrnov0 = PseudoVMERGE_VVM_M1 %passthru, %passthru, %xcopy, %mask, %avl, 5
+ DBG_VALUE %y, $noreg, !13, !DIExpression(), debug-location !14
+...
+---
+name: fold_vmand_debug_use
+body: |
+ bb.0:
+ liveins: $x10, $v8, $v9, $v10
+ ; CHECK-LABEL: name: fold_vmand_debug_use
+ ; CHECK: liveins: $x10, $v8, $v9, $v10
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %avl:gprnox0 = COPY $x10
+ ; CHECK-NEXT: %mask:vr = COPY $v10
+ ; CHECK-NEXT: %b:vr = COPY $v9
+ ; CHECK-NEXT: %a:vr = COPY $v8
+ ; CHECK-NEXT: %cmp:vr = PseudoVMSLT_VV_M1 %a, %b, %avl /* vl */, 5 /* e32 */
+ ; CHECK-NEXT: DBG_VALUE %cmp, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
+ ; CHECK-NEXT: %cmpcopy:vr = COPY %cmp
+ ; CHECK-NEXT: DBG_VALUE %cmpcopy, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
+ ; CHECK-NEXT: %and:vr = PseudoVMAND_MM_B32 killed %cmpcopy, killed %mask, %avl /* vl */, 0 /* e8 */
+ ; CHECK-NEXT: DBG_VALUE %and, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
+ ; CHECK-NEXT: $v0 = COPY %and
+ ; CHECK-NEXT: PseudoRET implicit $v0
+ %avl:gprnox0 = COPY $x10
+ %mask:vr = COPY $v10
+ %b:vr = COPY $v9
+ %a:vr = COPY $v8
+ %cmp:vr = PseudoVMSLT_VV_M1 %a, %b, %avl, 5
+ DBG_VALUE %cmp, $noreg, !17, !DIExpression(), debug-location !18
+ %cmpcopy:vr = COPY %cmp
+ DBG_VALUE %cmpcopy, $noreg, !17, !DIExpression(), debug-location !18
+ %and:vr = PseudoVMAND_MM_B32 killed %cmpcopy, killed %mask, %avl, 0
+ DBG_VALUE %and, $noreg, !17, !DIExpression(), debug-location !18
+ $v0 = COPY %and
+ PseudoRET implicit $v0
+...
+---
+name: fold_undef_passthru_debug_use
+body: |
+ bb.0:
+ liveins: $v8
+ ; CHECK-LABEL: name: fold_undef_passthru_debug_use
+ ; CHECK: liveins: $v8
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %passthru:vr = COPY $v8
+ ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
+ %passthru:vr = COPY $v8
+ %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
+ DBG_VALUE %x, $noreg, !21, !DIExpression(), debug-location !22
+ %y:vr = PseudoVMV_V_V_M1 $noreg, %x, 4, 5, 1
+ DBG_VALUE %y, $noreg, !21, !DIExpression(), debug-location !22
+...
+---
+name: convert_same_mask_debug_use
+body: |
+ bb.0:
+ liveins: $v8, $v0
+ ; CHECK-LABEL: name: convert_same_mask_debug_use
+ ; CHECK: liveins: $v8, $v0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %mask:vmv0 = COPY $v0
+ ; CHECK-NEXT: %true:vrnov0 = PseudoVADD_VV_M1_MASK $noreg, $noreg, $noreg, %mask, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
+ ; CHECK-NEXT: DBG_VALUE %true, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
+ ; CHECK-NEXT: %false:vrnov0 = COPY $v8
+ ; CHECK-NEXT: %x:vrnov0 = PseudoVMERGE_VVM_M1 $noreg, %false, %true, %mask, 4 /* vl */, 5 /* e32 */
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
+ ; CHECK-NEXT: $v8 = COPY %x
+ %mask:vmv0 = COPY $v0
+ %true:vrnov0 = PseudoVADD_VV_M1_MASK $noreg, $noreg, $noreg, %mask, 4, 5, 0
+ DBG_VALUE %true, $noreg, !25, !DIExpression(), debug-location !26
+ %false:vrnov0 = COPY $v8
+ %x:vrnov0 = PseudoVMERGE_VVM_M1 $noreg, %false, %true, %mask, 4, 5
+ DBG_VALUE %x, $noreg, !25, !DIExpression(), debug-location !26
+ $v8 = COPY %x
+...
+---
+name: dont_fold_two_nondebug_uses
+body: |
+ bb.0:
+ liveins: $v8
+ ; CHECK-LABEL: name: dont_fold_two_nondebug_uses
+ ; CHECK: liveins: $v8
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %passthru:vr = COPY $v8
+ ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !24, !DIExpression(), debug-location !DILocation(line: 6, column: 1, scope: !22)
+ ; CHECK-NEXT: %y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
+ ; CHECK-NEXT: %z:vr = COPY %x
+ ; CHECK-NEXT: DBG_VALUE %y, $noreg, !24, !DIExpression(), debug-location !DILocation(line: 6, column: 1, scope: !22)
+ %passthru:vr = COPY $v8
+ %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
+ DBG_VALUE %x, $noreg, !29, !DIExpression(), debug-location !30
+ %y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5, 1
+ %z:vr = COPY %x
+ DBG_VALUE %y, $noreg, !29, !DIExpression(), debug-location !30
+...
>From 0c1b821dc7c178780b71bbea0012f2a0a8b9c5c3 Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Fri, 9 Oct 2026 03:30:56 +0000
Subject: [PATCH 2/5] [RISCV] Ignore debug uses in vector peephole folds
DBG_VALUE users can make vector peephole folds skip transformations
that should be independent of debug information. Use
hasOneNonDBGUse() in the affected checks, and invalidate debug
locations when their definitions are moved, rewritten, or erased.
Update the precommitted MIR checks for the corrected behavior.
Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.
---
llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp | 54 +++++++++++++------
.../RISCV/rvv/vector-peephole-debug.mir | 40 +++++++-------
2 files changed, 57 insertions(+), 37 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
index 6c70c703db2c5..c8701a4984241 100644
--- a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
@@ -68,6 +68,7 @@ class RISCVVectorPeepholeImpl {
Register
lookThruCopies(Register Reg, bool OneUseOnly = false,
SmallVectorImpl<MachineInstr *> *Copies = nullptr) const;
+ void eraseInstrAndUndefDebugUses(MachineInstr &MI) const;
};
class RISCVVectorPeepholeLegacy : public MachineFunctionPass {
@@ -326,6 +327,14 @@ Register RISCVVectorPeepholeImpl::lookThruCopies(
return Reg;
}
+/// Erase an instruction whose result does not survive, undefining its debug
+/// uses before removing the definition.
+void RISCVVectorPeepholeImpl::eraseInstrAndUndefDebugUses(
+ MachineInstr &MI) const {
+ MRI->markUsesInDebugValueAsUndef(MI.getOperand(0).getReg());
+ MI.eraseFromParent();
+}
+
/// If a PseudoVMERGE_VVM's true operand is a masked pseudo and both have the
/// same mask, and the masked pseudo's passthru is the same as the false
/// operand, we can convert the PseudoVMERGE_VVM to a PseudoVMV_V_V.
@@ -380,9 +389,12 @@ bool RISCVVectorPeepholeImpl::convertSameMaskVMergeToVMv(MachineInstr &MI) {
if (TruePassthruReg != FalseReg) {
// If True's passthru is undef see if we can change it to False
if (TruePassthruReg.isValid() ||
- !MRI->hasOneUse(MI.getOperand(3).getReg()) ||
+ !MRI->hasOneNonDBGUse(MI.getOperand(3).getReg()) ||
!ensureDominates(&MI.getOperand(2), *True))
return false;
+ // True may have moved below its debug users and its passthru is about to
+ // change. The old debug location no longer describes the rewritten value.
+ MRI->markUsesInDebugValueAsUndef(True->getOperand(0).getReg());
True->getOperand(1).setReg(MI.getOperand(2).getReg());
// If True is masked then its passthru needs to be in VRNoV0.
MRI->constrainRegClass(True->getOperand(1).getReg(),
@@ -520,7 +532,7 @@ bool RISCVVectorPeepholeImpl::foldUndefPassthruVMV_V_V(MachineInstr &MI) {
// agnostic policy if MI's undef tail subsumes the input's.
MachineInstr *Src = MRI->getVRegDef(MI.getOperand(2).getReg());
if (Src && !Src->hasUnmodeledSideEffects() &&
- MRI->hasOneUse(MI.getOperand(2).getReg()) &&
+ MRI->hasOneNonDBGUse(MI.getOperand(2).getReg()) &&
RISCVII::hasVLOp(Src->getDesc().TSFlags) &&
RISCVII::hasVecPolicyOp(Src->getDesc().TSFlags) && hasSameEEW(MI, *Src)) {
const MachineOperand &MIVL = MI.getOperand(3);
@@ -530,8 +542,11 @@ bool RISCVVectorPeepholeImpl::foldUndefPassthruVMV_V_V(MachineInstr &MI) {
MachineOperand &SrcPolicy =
Src->getOperand(RISCVII::getVecPolicyOpNum(Src->getDesc()));
- if (RISCV::isVLKnownLE(*MRI, MIVL, SrcVL))
+ if (RISCV::isVLKnownLE(*MRI, MIVL, SrcVL)) {
+ // Tail elements are about to become undefined.
+ MRI->markUsesInDebugValueAsUndef(Src->getOperand(0).getReg());
SrcPolicy.setImm(SrcPolicy.getImm() | RISCVVType::TAIL_AGNOSTIC);
+ }
}
MRI->constrainRegClass(MI.getOperand(2).getReg(),
@@ -542,8 +557,8 @@ bool RISCVVectorPeepholeImpl::foldUndefPassthruVMV_V_V(MachineInstr &MI) {
return true;
}
-/// If a PseudoVMV_V_V is the only user of its input, fold its passthru and VL
-/// into it.
+/// If a PseudoVMV_V_V is the only non-debug user of its input, fold its
+/// passthru and VL into it.
///
/// %x = PseudoVADD_V_V_M1 %passthru, %a, %b, %vl1, sew, policy
/// %y = PseudoVMV_V_V_M1 %passthru, %x, %vl2, sew, policy
@@ -558,7 +573,7 @@ bool RISCVVectorPeepholeImpl::foldVMV_V_V(MachineInstr &MI) {
MachineOperand &Passthru = MI.getOperand(1);
- if (!MRI->hasOneUse(MI.getOperand(2).getReg()))
+ if (!MRI->hasOneNonDBGUse(MI.getOperand(2).getReg()))
return false;
MachineInstr *Src = MRI->getVRegDef(MI.getOperand(2).getReg());
@@ -601,6 +616,9 @@ bool RISCVVectorPeepholeImpl::foldVMV_V_V(MachineInstr &MI) {
if (!ensureDominates(&Passthru, *Src))
return false;
+ // Src is about to be changed to compute MI's value. Invalidate its existing
+ // debug users before replaceRegWith redirects MI's debug users to Src.
+ MRI->markUsesInDebugValueAsUndef(Src->getOperand(0).getReg());
if (NeedsCommute) {
auto [OpIdx1, OpIdx2] = *NeedsCommute;
[[maybe_unused]] bool Commuted =
@@ -663,7 +681,7 @@ bool RISCVVectorPeepholeImpl::foldVMergeToMask(MachineInstr &MI) const {
Register FalseReg = lookThruCopies(FalseOp.getReg());
Register TrueReg = lookThruCopies(MI.getOperand(3).getReg(),
/*OneUseOnly=*/true, &TrueCopies);
- if (!TrueReg.isVirtual() || !MRI->hasOneUse(TrueReg))
+ if (!TrueReg.isVirtual() || !MRI->hasOneNonDBGUse(TrueReg))
return false;
MachineInstr *TrueDef = MRI->getVRegDef(TrueReg);
if (!TrueDef)
@@ -757,6 +775,10 @@ bool RISCVVectorPeepholeImpl::foldVMergeToMask(MachineInstr &MI) const {
if (!ensureDominates({&MaskOp, &FalseOp, &MinVL}, True))
return false;
+ // True is about to be rewritten to compute MI's value under MI's register.
+ // Undef True's own debug users first; after replaceRegWith they would
+ // describe MI's value instead.
+ MRI->markUsesInDebugValueAsUndef(TrueReg);
if (NeedsCommute) {
auto [OpIdx1, OpIdx2] = *NeedsCommute;
[[maybe_unused]] bool Commuted =
@@ -795,7 +817,7 @@ bool RISCVVectorPeepholeImpl::foldVMergeToMask(MachineInstr &MI) const {
// Cleanup all the COPYs on True's value. We have to manually do this because
// sometimes sinking True causes these COPY to be invalid (use before define).
for (MachineInstr *TrueCopy : TrueCopies)
- TrueCopy->eraseFromParent();
+ eraseInstrAndUndefDebugUses(*TrueCopy);
return true;
}
@@ -824,8 +846,9 @@ bool RISCVVectorPeepholeImpl::foldVMANDToMaskedCompare(MachineInstr &MI) const {
// the original vmand did not require. If the vmand's result has more than one
// use then it is an interior mask value rather than a final result feeding
// v0, and introducing the v0 requirement tends to add vmv1r.v moves. Only
- // fold single-use results, where the value coalesces onto v0 for free.
- if (!MRI->hasOneUse(MI.getOperand(0).getReg()))
+ // fold results with a single non-debug use, where the value coalesces onto
+ // v0 for free.
+ if (!MRI->hasOneNonDBGUse(MI.getOperand(0).getReg()))
return false;
// Try each operand as the comparison to be masked; the other becomes the
@@ -833,12 +856,12 @@ bool RISCVVectorPeepholeImpl::foldVMANDToMaskedCompare(MachineInstr &MI) const {
for (unsigned CmpIdx : {1, 2}) {
unsigned MaskIdx = CmpIdx == 1 ? 2 : 1;
- // The comparison must be single use so that folding it into MI doesn't
- // leave an extra unmasked comparison behind.
+ // The comparison must have a single non-debug use so that folding it into
+ // MI doesn't leave an extra unmasked comparison behind.
SmallVector<MachineInstr *, 4> CmpCopies;
Register CmpReg = lookThruCopies(MI.getOperand(CmpIdx).getReg(),
/*OneUseOnly=*/true, &CmpCopies);
- if (!CmpReg.isVirtual() || !MRI->hasOneUse(CmpReg))
+ if (!CmpReg.isVirtual() || !MRI->hasOneNonDBGUse(CmpReg))
continue;
MachineInstr &Cmp = *MRI->getUniqueVRegDef(CmpReg);
if (Cmp.getParent() != MI.getParent())
@@ -939,9 +962,10 @@ bool RISCVVectorPeepholeImpl::foldVMANDToMaskedCompare(MachineInstr &MI) const {
}
MRI->clearKillFlags(MaskReg);
MI.eraseFromParent();
- Cmp.eraseFromParent();
+ // The comparison is about to be erased.
+ eraseInstrAndUndefDebugUses(Cmp);
for (MachineInstr *CmpCopy : CmpCopies)
- CmpCopy->eraseFromParent();
+ eraseInstrAndUndefDebugUses(*CmpCopy);
return true;
}
diff --git a/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
index 5391b70ea2aa5..bd745834d08e2 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
@@ -54,10 +54,9 @@ body: |
; CHECK: liveins: $v8
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %passthru:vr = COPY $v8
- ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
+ ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
+ ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
; CHECK-NEXT: DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
- ; CHECK-NEXT: %y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE %y, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
%passthru:vr = COPY $v8
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !10
@@ -74,12 +73,10 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %avl:gprnox0 = COPY $x8
; CHECK-NEXT: %passthru:vrnov0 = COPY $v8
- ; CHECK-NEXT: %x:vrnov0 = PseudoVLE32_V_M1 $noreg, $noreg, %avl /* vl */, 5 /* e32 */, 2 /* tu, ma */ :: (load unknown-size, align 1)
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
- ; CHECK-NEXT: %xcopy:vrnov0 = COPY %x
- ; CHECK-NEXT: DBG_VALUE %xcopy, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
; CHECK-NEXT: %mask:vmv0 = COPY $v0
- ; CHECK-NEXT: %y:vrnov0 = PseudoVMERGE_VVM_M1 %passthru, %passthru, %xcopy, %mask, %avl /* vl */, 5 /* e32 */
+ ; CHECK-NEXT: %y:vrnov0 = PseudoVLE32_V_M1_MASK %passthru, $noreg, %mask, %avl /* vl */, 5 /* e32 */, 0 /* tu, mu */ :: (load unknown-size, align 1)
; CHECK-NEXT: DBG_VALUE %y, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
%avl:gprnox0 = COPY $x8
%passthru:vrnov0 = COPY $v8
@@ -101,13 +98,13 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %avl:gprnox0 = COPY $x10
; CHECK-NEXT: %mask:vr = COPY $v10
- ; CHECK-NEXT: %b:vr = COPY $v9
- ; CHECK-NEXT: %a:vr = COPY $v8
- ; CHECK-NEXT: %cmp:vr = PseudoVMSLT_VV_M1 %a, %b, %avl /* vl */, 5 /* e32 */
- ; CHECK-NEXT: DBG_VALUE %cmp, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
- ; CHECK-NEXT: %cmpcopy:vr = COPY %cmp
- ; CHECK-NEXT: DBG_VALUE %cmpcopy, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
- ; CHECK-NEXT: %and:vr = PseudoVMAND_MM_B32 killed %cmpcopy, killed %mask, %avl /* vl */, 0 /* e8 */
+ ; CHECK-NEXT: %b:vrnov0 = COPY $v9
+ ; CHECK-NEXT: %a:vrnov0 = COPY $v8
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:vmv0 = COPY %mask
+ ; CHECK-NEXT: [[COPY1:%[0-9]+]]:vr = COPY %mask
+ ; CHECK-NEXT: %and:vr = PseudoVMSLT_VV_M1_MASK [[COPY1]], %a, %b, [[COPY]], %avl /* vl */, 5 /* e32 */, 1 /* ta, mu */
+ ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
+ ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
; CHECK-NEXT: DBG_VALUE %and, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
; CHECK-NEXT: $v0 = COPY %and
; CHECK-NEXT: PseudoRET implicit $v0
@@ -133,8 +130,8 @@ body: |
; CHECK: liveins: $v8
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %passthru:vr = COPY $v8
- ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
+ ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
+ ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
%passthru:vr = COPY $v8
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
@@ -151,12 +148,11 @@ body: |
; CHECK: liveins: $v8, $v0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %mask:vmv0 = COPY $v0
- ; CHECK-NEXT: %true:vrnov0 = PseudoVADD_VV_M1_MASK $noreg, $noreg, $noreg, %mask, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
- ; CHECK-NEXT: DBG_VALUE %true, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
+ ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
; CHECK-NEXT: %false:vrnov0 = COPY $v8
- ; CHECK-NEXT: %x:vrnov0 = PseudoVMERGE_VVM_M1 $noreg, %false, %true, %mask, 4 /* vl */, 5 /* e32 */
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
- ; CHECK-NEXT: $v8 = COPY %x
+ ; CHECK-NEXT: %true:vrnov0 = PseudoVADD_VV_M1_MASK %false, $noreg, $noreg, %mask, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
+ ; CHECK-NEXT: DBG_VALUE %true, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
+ ; CHECK-NEXT: $v8 = COPY %true
%mask:vmv0 = COPY $v0
%true:vrnov0 = PseudoVADD_VV_M1_MASK $noreg, $noreg, $noreg, %mask, 4, 5, 0
DBG_VALUE %true, $noreg, !25, !DIExpression(), debug-location !26
>From 907df5e27962c83fa47f421626d462d8fb07f670 Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Fri, 9 Oct 2026 11:33:26 +0000
Subject: [PATCH 3/5] [RISCV] Pass MRI explicitly to vector peephole cleanup
helper
Make eraseInstrAndUndefDebugUses static and pass MachineRegisterInfo
explicitly. This makes the helper's dependency clear without changing
its behavior.
Assisted-by: OpenAI Codex 5.6 Sol
---
llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
index c8701a4984241..845bfcd3a51f9 100644
--- a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
@@ -68,7 +68,8 @@ class RISCVVectorPeepholeImpl {
Register
lookThruCopies(Register Reg, bool OneUseOnly = false,
SmallVectorImpl<MachineInstr *> *Copies = nullptr) const;
- void eraseInstrAndUndefDebugUses(MachineInstr &MI) const;
+ static void eraseInstrAndUndefDebugUses(MachineInstr &MI,
+ MachineRegisterInfo *MRI);
};
class RISCVVectorPeepholeLegacy : public MachineFunctionPass {
@@ -330,7 +331,7 @@ Register RISCVVectorPeepholeImpl::lookThruCopies(
/// Erase an instruction whose result does not survive, undefining its debug
/// uses before removing the definition.
void RISCVVectorPeepholeImpl::eraseInstrAndUndefDebugUses(
- MachineInstr &MI) const {
+ MachineInstr &MI, MachineRegisterInfo *MRI) {
MRI->markUsesInDebugValueAsUndef(MI.getOperand(0).getReg());
MI.eraseFromParent();
}
@@ -817,7 +818,7 @@ bool RISCVVectorPeepholeImpl::foldVMergeToMask(MachineInstr &MI) const {
// Cleanup all the COPYs on True's value. We have to manually do this because
// sometimes sinking True causes these COPY to be invalid (use before define).
for (MachineInstr *TrueCopy : TrueCopies)
- eraseInstrAndUndefDebugUses(*TrueCopy);
+ eraseInstrAndUndefDebugUses(*TrueCopy, MRI);
return true;
}
@@ -963,9 +964,9 @@ bool RISCVVectorPeepholeImpl::foldVMANDToMaskedCompare(MachineInstr &MI) const {
MRI->clearKillFlags(MaskReg);
MI.eraseFromParent();
// The comparison is about to be erased.
- eraseInstrAndUndefDebugUses(Cmp);
+ eraseInstrAndUndefDebugUses(Cmp, MRI);
for (MachineInstr *CmpCopy : CmpCopies)
- eraseInstrAndUndefDebugUses(*CmpCopy);
+ eraseInstrAndUndefDebugUses(*CmpCopy, MRI);
return true;
}
>From 589ebde845e572beebe8fa77f12cf0c52165e988 Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Sat, 10 Oct 2026 01:40:52 +0000
Subject: [PATCH 4/5] [RISCV] Leave debug-use cleanup to LiveDebugVariables
Keep the non-debug use-count checks that allow vector peephole folds
despite DBG_VALUE users, but remove the eager invalidation of those
users. LiveDebugVariables handles debug locations left by rewritten
or erased definitions.
Regenerate the MIR checks for the pass output after this change.
Assisted-by: OpenAI Codex
---
llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp | 32 +++----------------
.../RISCV/rvv/vector-peephole-debug.mir | 14 ++++----
2 files changed, 11 insertions(+), 35 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
index 845bfcd3a51f9..7a1234011635b 100644
--- a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
@@ -68,8 +68,6 @@ class RISCVVectorPeepholeImpl {
Register
lookThruCopies(Register Reg, bool OneUseOnly = false,
SmallVectorImpl<MachineInstr *> *Copies = nullptr) const;
- static void eraseInstrAndUndefDebugUses(MachineInstr &MI,
- MachineRegisterInfo *MRI);
};
class RISCVVectorPeepholeLegacy : public MachineFunctionPass {
@@ -328,14 +326,6 @@ Register RISCVVectorPeepholeImpl::lookThruCopies(
return Reg;
}
-/// Erase an instruction whose result does not survive, undefining its debug
-/// uses before removing the definition.
-void RISCVVectorPeepholeImpl::eraseInstrAndUndefDebugUses(
- MachineInstr &MI, MachineRegisterInfo *MRI) {
- MRI->markUsesInDebugValueAsUndef(MI.getOperand(0).getReg());
- MI.eraseFromParent();
-}
-
/// If a PseudoVMERGE_VVM's true operand is a masked pseudo and both have the
/// same mask, and the masked pseudo's passthru is the same as the false
/// operand, we can convert the PseudoVMERGE_VVM to a PseudoVMV_V_V.
@@ -393,9 +383,6 @@ bool RISCVVectorPeepholeImpl::convertSameMaskVMergeToVMv(MachineInstr &MI) {
!MRI->hasOneNonDBGUse(MI.getOperand(3).getReg()) ||
!ensureDominates(&MI.getOperand(2), *True))
return false;
- // True may have moved below its debug users and its passthru is about to
- // change. The old debug location no longer describes the rewritten value.
- MRI->markUsesInDebugValueAsUndef(True->getOperand(0).getReg());
True->getOperand(1).setReg(MI.getOperand(2).getReg());
// If True is masked then its passthru needs to be in VRNoV0.
MRI->constrainRegClass(True->getOperand(1).getReg(),
@@ -543,11 +530,8 @@ bool RISCVVectorPeepholeImpl::foldUndefPassthruVMV_V_V(MachineInstr &MI) {
MachineOperand &SrcPolicy =
Src->getOperand(RISCVII::getVecPolicyOpNum(Src->getDesc()));
- if (RISCV::isVLKnownLE(*MRI, MIVL, SrcVL)) {
- // Tail elements are about to become undefined.
- MRI->markUsesInDebugValueAsUndef(Src->getOperand(0).getReg());
+ if (RISCV::isVLKnownLE(*MRI, MIVL, SrcVL))
SrcPolicy.setImm(SrcPolicy.getImm() | RISCVVType::TAIL_AGNOSTIC);
- }
}
MRI->constrainRegClass(MI.getOperand(2).getReg(),
@@ -617,9 +601,6 @@ bool RISCVVectorPeepholeImpl::foldVMV_V_V(MachineInstr &MI) {
if (!ensureDominates(&Passthru, *Src))
return false;
- // Src is about to be changed to compute MI's value. Invalidate its existing
- // debug users before replaceRegWith redirects MI's debug users to Src.
- MRI->markUsesInDebugValueAsUndef(Src->getOperand(0).getReg());
if (NeedsCommute) {
auto [OpIdx1, OpIdx2] = *NeedsCommute;
[[maybe_unused]] bool Commuted =
@@ -776,10 +757,6 @@ bool RISCVVectorPeepholeImpl::foldVMergeToMask(MachineInstr &MI) const {
if (!ensureDominates({&MaskOp, &FalseOp, &MinVL}, True))
return false;
- // True is about to be rewritten to compute MI's value under MI's register.
- // Undef True's own debug users first; after replaceRegWith they would
- // describe MI's value instead.
- MRI->markUsesInDebugValueAsUndef(TrueReg);
if (NeedsCommute) {
auto [OpIdx1, OpIdx2] = *NeedsCommute;
[[maybe_unused]] bool Commuted =
@@ -818,7 +795,7 @@ bool RISCVVectorPeepholeImpl::foldVMergeToMask(MachineInstr &MI) const {
// Cleanup all the COPYs on True's value. We have to manually do this because
// sometimes sinking True causes these COPY to be invalid (use before define).
for (MachineInstr *TrueCopy : TrueCopies)
- eraseInstrAndUndefDebugUses(*TrueCopy, MRI);
+ TrueCopy->eraseFromParent();
return true;
}
@@ -963,10 +940,9 @@ bool RISCVVectorPeepholeImpl::foldVMANDToMaskedCompare(MachineInstr &MI) const {
}
MRI->clearKillFlags(MaskReg);
MI.eraseFromParent();
- // The comparison is about to be erased.
- eraseInstrAndUndefDebugUses(Cmp, MRI);
+ Cmp.eraseFromParent();
for (MachineInstr *CmpCopy : CmpCopies)
- eraseInstrAndUndefDebugUses(*CmpCopy, MRI);
+ CmpCopy->eraseFromParent();
return true;
}
diff --git a/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
index bd745834d08e2..e6257f9f3eb32 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
@@ -55,7 +55,7 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %passthru:vr = COPY $v8
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
; CHECK-NEXT: DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
%passthru:vr = COPY $v8
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
@@ -73,8 +73,8 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %avl:gprnox0 = COPY $x8
; CHECK-NEXT: %passthru:vrnov0 = COPY $v8
- ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
- ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ ; CHECK-NEXT: DBG_VALUE %y, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ ; CHECK-NEXT: DBG_VALUE %xcopy:vrnov0, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
; CHECK-NEXT: %mask:vmv0 = COPY $v0
; CHECK-NEXT: %y:vrnov0 = PseudoVLE32_V_M1_MASK %passthru, $noreg, %mask, %avl /* vl */, 5 /* e32 */, 0 /* tu, mu */ :: (load unknown-size, align 1)
; CHECK-NEXT: DBG_VALUE %y, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
@@ -103,8 +103,8 @@ body: |
; CHECK-NEXT: [[COPY:%[0-9]+]]:vmv0 = COPY %mask
; CHECK-NEXT: [[COPY1:%[0-9]+]]:vr = COPY %mask
; CHECK-NEXT: %and:vr = PseudoVMSLT_VV_M1_MASK [[COPY1]], %a, %b, [[COPY]], %avl /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
- ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
+ ; CHECK-NEXT: DBG_VALUE %cmp:vr, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
+ ; CHECK-NEXT: DBG_VALUE %cmpcopy:vr, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
; CHECK-NEXT: DBG_VALUE %and, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
; CHECK-NEXT: $v0 = COPY %and
; CHECK-NEXT: PseudoRET implicit $v0
@@ -131,7 +131,7 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %passthru:vr = COPY $v8
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
%passthru:vr = COPY $v8
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
@@ -148,7 +148,7 @@ body: |
; CHECK: liveins: $v8, $v0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %mask:vmv0 = COPY $v0
- ; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
+ ; CHECK-NEXT: DBG_VALUE %true, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
; CHECK-NEXT: %false:vrnov0 = COPY $v8
; CHECK-NEXT: %true:vrnov0 = PseudoVADD_VV_M1_MASK %false, $noreg, $noreg, %mask, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
; CHECK-NEXT: DBG_VALUE %true, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
>From 7331cd78198aff9e2ce00d9266459ac7a8075650 Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Sat, 10 Oct 2026 11:39:44 +0000
Subject: [PATCH 5/5] [RISCV] Simplify vector peephole debug-use tests
Keep one debug use for each affected use-count check, including both
the comparison and result checks in the masked-compare fold. Remove
redundant debug records and simplify the surrounding metadata.
Regenerate the MIR checks for the reduced test.
Assisted-by: OpenAI Codex
Assisted-by: Claude (review)
---
.../RISCV/rvv/vector-peephole-debug.mir | 80 +++++++------------
1 file changed, 29 insertions(+), 51 deletions(-)
diff --git a/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
index e6257f9f3eb32..c2c645d3226fd 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/vector-peephole-debug.mir
@@ -11,39 +11,31 @@
define void @dont_fold_two_nondebug_uses() !dbg !27 { ret void }
!llvm.dbg.cu = !{!0}
- !llvm.module.flags = !{!2, !3}
+ !llvm.module.flags = !{!2}
- !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+ !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, emissionKind: FullDebug)
!1 = !DIFile(filename: "vector-peephole-debug.mir", directory: "")
- !2 = !{i32 7, !"Dwarf Version", i32 5}
- !3 = !{i32 2, !"Debug Info Version", i32 3}
- !4 = distinct !DISubprogram(name: "fold_vmv_debug_use", scope: !1, file: !1, line: 1, type: !5, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !7)
+ !2 = !{i32 2, !"Debug Info Version", i32 3}
+ !4 = distinct !DISubprogram(name: "fold_vmv_debug_use", scope: !1, file: !1, type: !5, spFlags: DISPFlagDefinition, unit: !0)
!5 = !DISubroutineType(types: !6)
- !6 = !{}
- !7 = !{!8}
- !8 = !DILocalVariable(name: "vmv", scope: !4, file: !1, line: 1, type: !9)
- !9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
- !10 = !DILocation(line: 1, column: 1, scope: !4)
- !11 = distinct !DISubprogram(name: "fold_vmerge_debug_use", scope: !1, file: !1, line: 2, type: !5, scopeLine: 2, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
- !12 = !{!13}
- !13 = !DILocalVariable(name: "vmerge", scope: !11, file: !1, line: 2, type: !9)
- !14 = !DILocation(line: 2, column: 1, scope: !11)
- !15 = distinct !DISubprogram(name: "fold_vmand_debug_use", scope: !1, file: !1, line: 3, type: !5, scopeLine: 3, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !16)
- !16 = !{!17}
- !17 = !DILocalVariable(name: "vmand", scope: !15, file: !1, line: 3, type: !9)
- !18 = !DILocation(line: 3, column: 1, scope: !15)
- !19 = distinct !DISubprogram(name: "fold_undef_passthru_debug_use", scope: !1, file: !1, line: 4, type: !5, scopeLine: 4, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !20)
- !20 = !{!21}
- !21 = !DILocalVariable(name: "undef_passthru", scope: !19, file: !1, line: 4, type: !9)
- !22 = !DILocation(line: 4, column: 1, scope: !19)
- !23 = distinct !DISubprogram(name: "convert_same_mask_debug_use", scope: !1, file: !1, line: 5, type: !5, scopeLine: 5, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !24)
- !24 = !{!25}
- !25 = !DILocalVariable(name: "same_mask", scope: !23, file: !1, line: 5, type: !9)
- !26 = !DILocation(line: 5, column: 1, scope: !23)
- !27 = distinct !DISubprogram(name: "dont_fold_two_nondebug_uses", scope: !1, file: !1, line: 6, type: !5, scopeLine: 6, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !28)
- !28 = !{!29}
- !29 = !DILocalVariable(name: "two_uses", scope: !27, file: !1, line: 6, type: !9)
- !30 = !DILocation(line: 6, column: 1, scope: !27)
+ !6 = !{null}
+ !8 = !DILocalVariable(name: "vmv", scope: !4)
+ !10 = !DILocation(line: 0, scope: !4)
+ !11 = distinct !DISubprogram(name: "fold_vmerge_debug_use", scope: !1, file: !1, type: !5, spFlags: DISPFlagDefinition, unit: !0)
+ !13 = !DILocalVariable(name: "vmerge", scope: !11)
+ !14 = !DILocation(line: 0, scope: !11)
+ !15 = distinct !DISubprogram(name: "fold_vmand_debug_use", scope: !1, file: !1, type: !5, spFlags: DISPFlagDefinition, unit: !0)
+ !17 = !DILocalVariable(name: "vmand", scope: !15)
+ !18 = !DILocation(line: 0, scope: !15)
+ !19 = distinct !DISubprogram(name: "fold_undef_passthru_debug_use", scope: !1, file: !1, type: !5, spFlags: DISPFlagDefinition, unit: !0)
+ !21 = !DILocalVariable(name: "undef_passthru", scope: !19)
+ !22 = !DILocation(line: 0, scope: !19)
+ !23 = distinct !DISubprogram(name: "convert_same_mask_debug_use", scope: !1, file: !1, type: !5, spFlags: DISPFlagDefinition, unit: !0)
+ !25 = !DILocalVariable(name: "same_mask", scope: !23)
+ !26 = !DILocation(line: 0, scope: !23)
+ !27 = distinct !DISubprogram(name: "dont_fold_two_nondebug_uses", scope: !1, file: !1, type: !5, spFlags: DISPFlagDefinition, unit: !0)
+ !29 = !DILocalVariable(name: "two_uses", scope: !27)
+ !30 = !DILocation(line: 0, scope: !27)
...
---
name: fold_vmv_debug_use
@@ -55,13 +47,11 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %passthru:vr = COPY $v8
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !4)
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !3)
%passthru:vr = COPY $v8
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
DBG_VALUE %x, $noreg, !8, !DIExpression(), debug-location !10
%y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5, 1
- DBG_VALUE %y, $noreg, !8, !DIExpression(), debug-location !10
...
---
name: fold_vmerge_debug_use
@@ -73,20 +63,16 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %avl:gprnox0 = COPY $x8
; CHECK-NEXT: %passthru:vrnov0 = COPY $v8
- ; CHECK-NEXT: DBG_VALUE %y, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
- ; CHECK-NEXT: DBG_VALUE %xcopy:vrnov0, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
+ ; CHECK-NEXT: DBG_VALUE %y, $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !6)
; CHECK-NEXT: %mask:vmv0 = COPY $v0
; CHECK-NEXT: %y:vrnov0 = PseudoVLE32_V_M1_MASK %passthru, $noreg, %mask, %avl /* vl */, 5 /* e32 */, 0 /* tu, mu */ :: (load unknown-size, align 1)
- ; CHECK-NEXT: DBG_VALUE %y, $noreg, !12, !DIExpression(), debug-location !DILocation(line: 2, column: 1, scope: !10)
%avl:gprnox0 = COPY $x8
%passthru:vrnov0 = COPY $v8
%x:vrnov0 = PseudoVLE32_V_M1 $noreg, $noreg, %avl, 5, 2 :: (load unknown-size)
DBG_VALUE %x, $noreg, !13, !DIExpression(), debug-location !14
%xcopy:vrnov0 = COPY %x
- DBG_VALUE %xcopy, $noreg, !13, !DIExpression(), debug-location !14
%mask:vmv0 = COPY $v0
%y:vrnov0 = PseudoVMERGE_VVM_M1 %passthru, %passthru, %xcopy, %mask, %avl, 5
- DBG_VALUE %y, $noreg, !13, !DIExpression(), debug-location !14
...
---
name: fold_vmand_debug_use
@@ -103,9 +89,8 @@ body: |
; CHECK-NEXT: [[COPY:%[0-9]+]]:vmv0 = COPY %mask
; CHECK-NEXT: [[COPY1:%[0-9]+]]:vr = COPY %mask
; CHECK-NEXT: %and:vr = PseudoVMSLT_VV_M1_MASK [[COPY1]], %a, %b, [[COPY]], %avl /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE %cmp:vr, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
- ; CHECK-NEXT: DBG_VALUE %cmpcopy:vr, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
- ; CHECK-NEXT: DBG_VALUE %and, $noreg, !15, !DIExpression(), debug-location !DILocation(line: 3, column: 1, scope: !13)
+ ; CHECK-NEXT: DBG_VALUE %cmp:vr, $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !7)
+ ; CHECK-NEXT: DBG_VALUE %and, $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !7)
; CHECK-NEXT: $v0 = COPY %and
; CHECK-NEXT: PseudoRET implicit $v0
%avl:gprnox0 = COPY $x10
@@ -115,7 +100,6 @@ body: |
%cmp:vr = PseudoVMSLT_VV_M1 %a, %b, %avl, 5
DBG_VALUE %cmp, $noreg, !17, !DIExpression(), debug-location !18
%cmpcopy:vr = COPY %cmp
- DBG_VALUE %cmpcopy, $noreg, !17, !DIExpression(), debug-location !18
%and:vr = PseudoVMAND_MM_B32 killed %cmpcopy, killed %mask, %avl, 0
DBG_VALUE %and, $noreg, !17, !DIExpression(), debug-location !18
$v0 = COPY %and
@@ -131,13 +115,11 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %passthru:vr = COPY $v8
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !18, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !16)
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !8)
%passthru:vr = COPY $v8
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
DBG_VALUE %x, $noreg, !21, !DIExpression(), debug-location !22
%y:vr = PseudoVMV_V_V_M1 $noreg, %x, 4, 5, 1
- DBG_VALUE %y, $noreg, !21, !DIExpression(), debug-location !22
...
---
name: convert_same_mask_debug_use
@@ -148,17 +130,15 @@ body: |
; CHECK: liveins: $v8, $v0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %mask:vmv0 = COPY $v0
- ; CHECK-NEXT: DBG_VALUE %true, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
+ ; CHECK-NEXT: DBG_VALUE %true, $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !9)
; CHECK-NEXT: %false:vrnov0 = COPY $v8
; CHECK-NEXT: %true:vrnov0 = PseudoVADD_VV_M1_MASK %false, $noreg, $noreg, %mask, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
- ; CHECK-NEXT: DBG_VALUE %true, $noreg, !21, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !19)
; CHECK-NEXT: $v8 = COPY %true
%mask:vmv0 = COPY $v0
%true:vrnov0 = PseudoVADD_VV_M1_MASK $noreg, $noreg, $noreg, %mask, 4, 5, 0
DBG_VALUE %true, $noreg, !25, !DIExpression(), debug-location !26
%false:vrnov0 = COPY $v8
%x:vrnov0 = PseudoVMERGE_VVM_M1 $noreg, %false, %true, %mask, 4, 5
- DBG_VALUE %x, $noreg, !25, !DIExpression(), debug-location !26
$v8 = COPY %x
...
---
@@ -171,14 +151,12 @@ body: |
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %passthru:vr = COPY $v8
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4 /* vl */, 5 /* e32 */, 0 /* tu, mu */
- ; CHECK-NEXT: DBG_VALUE %x, $noreg, !24, !DIExpression(), debug-location !DILocation(line: 6, column: 1, scope: !22)
+ ; CHECK-NEXT: DBG_VALUE %x, $noreg, !11, !DIExpression(), debug-location !DILocation(line: 0, scope: !10)
; CHECK-NEXT: %y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4 /* vl */, 5 /* e32 */, 1 /* ta, mu */
; CHECK-NEXT: %z:vr = COPY %x
- ; CHECK-NEXT: DBG_VALUE %y, $noreg, !24, !DIExpression(), debug-location !DILocation(line: 6, column: 1, scope: !22)
%passthru:vr = COPY $v8
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5, 0
DBG_VALUE %x, $noreg, !29, !DIExpression(), debug-location !30
%y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5, 1
%z:vr = COPY %x
- DBG_VALUE %y, $noreg, !29, !DIExpression(), debug-location !30
...
More information about the llvm-commits
mailing list