[llvm] [WebAssembly] Avoid same-block dominance walks in RegStackify (NFCI) (PR #218369)
Frank Denis via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 24 03:32:24 PDT 2026
https://github.com/jedisct1 created https://github.com/llvm/llvm-project/pull/218369
RegStackify asks whether one use dominates the others.
But when both uses are in the same block, MachineDominatorTree answers that by walking the block from the start.
Use the SlotIndexes already available here instead.
This avoids repeatedly walking over DBG_VALUEs and other instructions in large blocks.
>From 96e4560d162a54f36d5730d164fc78453f840f03 Mon Sep 17 00:00:00 2001
From: Frank Denis <github at pureftpd.org>
Date: Mon, 24 Aug 2026 12:22:31 +0200
Subject: [PATCH] [WebAssembly] Avoid same-block dominance walks in RegStackify
(NFCI)
RegStackify asks whether one use dominates the others.
But when both uses are in the same block, MachineDominatorTree
answers that by walking the block from the start.
Use the SlotIndexes already available here instead.
This avoids repeatedly walking over DBG_VALUEs and other
instructions in large blocks.
---
.../WebAssembly/WebAssemblyRegStackify.cpp | 13 +++++++--
llvm/test/CodeGen/WebAssembly/reg-stackify.ll | 27 +++++++++++++++++++
2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
index ec8623a114388..44dd6168487d3 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
@@ -463,7 +463,16 @@ static bool oneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse,
const LiveInterval &LI = LIS.getInterval(Reg);
const MachineInstr *OneUseInst = OneUse.getParent();
- VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst));
+ SlotIndex OneUseIdx = LIS.getInstructionIndex(*OneUseInst);
+ VNInfo *OneUseVNI = LI.getVNInfoBefore(OneUseIdx);
+
+ auto OneUseDominates = [&](const MachineInstr *UseI) {
+ if (OneUseInst->getParent() != UseI->getParent())
+ return MDT.dominates(OneUseInst->getParent(), UseI->getParent());
+ if (OneUseInst == UseI)
+ return true;
+ return SlotIndex::isEarlierInstr(OneUseIdx, LIS.getInstructionIndex(*UseI));
+ };
for (const MachineOperand &Use : MRI.use_nodbg_operands(Reg)) {
if (&Use == &OneUse)
@@ -482,7 +491,7 @@ static bool oneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse,
return false;
} else {
// Test that the use is dominated by the one selected use.
- while (!MDT.dominates(OneUseInst, UseInst)) {
+ while (!OneUseDominates(UseInst)) {
// Actually, dominating is over-conservative. Test that the use would
// happen after the one selected use in the stack evaluation order.
//
diff --git a/llvm/test/CodeGen/WebAssembly/reg-stackify.ll b/llvm/test/CodeGen/WebAssembly/reg-stackify.ll
index f9c233d1be3a3..6c97f41d858b3 100644
--- a/llvm/test/CodeGen/WebAssembly/reg-stackify.ll
+++ b/llvm/test/CodeGen/WebAssembly/reg-stackify.ll
@@ -345,6 +345,33 @@ define void @simple_multiple_use(i32 %x, i32 %y) {
ret void
}
+; Multiple uses in the same block with a debug value between them.
+
+; CHECK-LABEL: same_block_use_with_dbg_value:
+; CHECK: .functype same_block_use_with_dbg_value (i32, i32) -> (){{$}}
+; CHECK-NEXT: i32.mul $push[[NUM0:[0-9]+]]=, $1, $0{{$}}
+; CHECK-NEXT: local.tee $push[[NUM1:[0-9]+]]=, $[[NUM2:[0-9]+]]=, $pop[[NUM0]]{{$}}
+; CHECK-NEXT: call use_a, $pop[[NUM1]]{{$}}
+; CHECK-NEXT: call use_b, $[[NUM2]]{{$}}
+; CHECK-NEXT: return{{$}}
+; NOREGS-LABEL: same_block_use_with_dbg_value:
+; NOREGS: .functype same_block_use_with_dbg_value (i32, i32) -> (){{$}}
+; NOREGS-NEXT: local.get 1{{$}}
+; NOREGS-NEXT: local.get 0{{$}}
+; NOREGS-NEXT: i32.mul
+; NOREGS-NEXT: local.tee 0{{$}}
+; NOREGS-NEXT: call use_a{{$}}
+; NOREGS-NEXT: local.get 0{{$}}
+; NOREGS-NEXT: call use_b{{$}}
+; NOREGS-NEXT: return{{$}}
+define void @same_block_use_with_dbg_value(i32 %x, i32 %y) {
+ %mul = mul i32 %y, %x
+ call void @use_a(i32 %mul)
+ call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !7, metadata !9), !dbg !10
+ call void @use_b(i32 %mul)
+ ret void
+}
+
; Multiple uses of the same value in one instruction.
; CHECK-LABEL: multiple_uses_in_same_insn:
More information about the llvm-commits
mailing list