[llvm] [RISCV] Update V0Defs after moving Src in peepholes (PR #107359)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 5 08:03:37 PDT 2024
https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/107359
>From 42fac4be0734125518d6759eafc552732dd4a7f7 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Thu, 5 Sep 2024 13:26:04 +0800
Subject: [PATCH 1/2] [RISCV] Update V0Defs after moving Src in peepholes
If we move a pseudo in tryReduceVL or foldVMV_V_V via ensureDominates,
its V0 definition may have changed so we need to update V0Defs.
This shouldn't have any functional change today since any pseudo which
uses V0 won't be able to move past a new definition.
However this will matter if we add a peephole to convert unmasked
pseudos to masked pseudos and add a use of V0.
---
llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
index 026e0a365b8dcb..e6ea8c41269ca3 100644
--- a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
@@ -61,7 +61,7 @@ class RISCVVectorPeephole : public MachineFunctionPass {
}
private:
- bool tryToReduceVL(MachineInstr &MI) const;
+ bool tryToReduceVL(MachineInstr &MI);
bool convertToVLMAX(MachineInstr &MI) const;
bool convertToWholeRegister(MachineInstr &MI) const;
bool convertToUnmasked(MachineInstr &MI) const;
@@ -71,7 +71,7 @@ class RISCVVectorPeephole : public MachineFunctionPass {
bool isAllOnesMask(const MachineInstr *MaskDef) const;
std::optional<unsigned> getConstant(const MachineOperand &VL) const;
- bool ensureDominates(const MachineOperand &Use, MachineInstr &Src) const;
+ bool ensureDominates(const MachineOperand &Use, MachineInstr &Src);
/// Maps uses of V0 to the corresponding def of V0.
DenseMap<const MachineInstr *, const MachineInstr *> V0Defs;
@@ -107,7 +107,7 @@ static unsigned getSEWLMULRatio(const MachineInstr &MI) {
// Attempt to reduce the VL of an instruction whose sole use is feeding a
// instruction with a narrower VL. This currently works backwards from the
// user instruction (which might have a smaller VL).
-bool RISCVVectorPeephole::tryToReduceVL(MachineInstr &MI) const {
+bool RISCVVectorPeephole::tryToReduceVL(MachineInstr &MI) {
// Note that the goal here is a bit multifaceted.
// 1) For store's reducing the VL of the value being stored may help to
// reduce VL toggles. This is somewhat of an artifact of the fact we
@@ -457,7 +457,7 @@ static bool dominates(MachineBasicBlock::const_iterator A,
/// does. Returns false if doesn't dominate and we can't move. \p MO must be in
/// the same basic block as \Src.
bool RISCVVectorPeephole::ensureDominates(const MachineOperand &MO,
- MachineInstr &Src) const {
+ MachineInstr &Src) {
assert(MO.getParent()->getParent() == Src.getParent());
if (!MO.isReg() || MO.getReg() == RISCV::NoRegister)
return true;
@@ -466,7 +466,7 @@ bool RISCVVectorPeephole::ensureDominates(const MachineOperand &MO,
if (Def->getParent() == Src.getParent() && !dominates(Def, Src)) {
if (!isSafeToMove(Src, *Def->getNextNode()))
return false;
- // FIXME: Update V0Defs
+ V0Defs[&Src] = V0Defs[Def];
Src.moveBefore(Def->getNextNode());
}
>From 1b0da2919e1681bacfbf7cef319502dc4b81c2f4 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Thu, 5 Sep 2024 23:02:44 +0800
Subject: [PATCH 2/2] Use V0Def of node being inserted before
---
llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
index e6ea8c41269ca3..48c06a7e0d57b6 100644
--- a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
@@ -464,10 +464,11 @@ bool RISCVVectorPeephole::ensureDominates(const MachineOperand &MO,
MachineInstr *Def = MRI->getVRegDef(MO.getReg());
if (Def->getParent() == Src.getParent() && !dominates(Def, Src)) {
- if (!isSafeToMove(Src, *Def->getNextNode()))
+ MachineInstr *AfterDef = Def->getNextNode();
+ if (!isSafeToMove(Src, *AfterDef))
return false;
- V0Defs[&Src] = V0Defs[Def];
- Src.moveBefore(Def->getNextNode());
+ V0Defs[&Src] = V0Defs[AfterDef];
+ Src.moveBefore(AfterDef);
}
return true;
More information about the llvm-commits
mailing list