Skip to content

Commit 5f1cfb6

Browse files
mshockwavelukel97
andcommitted
fixup! Visit the blocks in post-order instead
Co-Authored-By: Luke Lau <[email protected]>
1 parent e74254a commit 5f1cfb6

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "RISCV.h"
2828
#include "RISCVSubtarget.h"
29-
#include "llvm/ADT/SetVector.h"
29+
#include "llvm/ADT/PostOrderIterator.h"
3030
#include "llvm/ADT/Statistic.h"
3131
#include "llvm/CodeGen/LiveDebugVariables.h"
3232
#include "llvm/CodeGen/LiveIntervals.h"
@@ -896,8 +896,7 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
896896

897897
bool canMutatePriorConfig(const MachineInstr &PrevMI, const MachineInstr &MI,
898898
const DemandedFields &Used) const;
899-
void coalesceVSETVLIs(SetVector<MachineBasicBlock *> &Worklist,
900-
MachineBasicBlock &MBB) const;
899+
void coalesceVSETVLIs(MachineBasicBlock &MBB) const;
901900

902901
VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) const;
903902
VSETVLIInfo computeInfoForInstr(const MachineInstr &MI) const;
@@ -1644,8 +1643,7 @@ bool RISCVInsertVSETVLI::canMutatePriorConfig(
16441643
return areCompatibleVTYPEs(PriorVType, VType, Used);
16451644
}
16461645

1647-
void RISCVInsertVSETVLI::coalesceVSETVLIs(
1648-
SetVector<MachineBasicBlock *> &Worklist, MachineBasicBlock &MBB) const {
1646+
void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
16491647
MachineInstr *NextMI = nullptr;
16501648
// We can have arbitrary code in successors, so VL and VTYPE
16511649
// must be considered demanded.
@@ -1664,18 +1662,9 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(
16641662
LIS->shrinkToUses(&LIS->getInterval(OldVLReg));
16651663

16661664
MachineInstr *VLOpDef = MRI->getUniqueVRegDef(OldVLReg);
1667-
if (VLOpDef && MRI->use_nodbg_empty(OldVLReg)) {
1668-
if (TII->isAddImmediate(*VLOpDef, OldVLReg))
1669-
ToDelete.push_back(VLOpDef);
1670-
// If the destination register of a vset* instruction becomes dead because
1671-
// of this, there might be a chance to eliminate it. Put into the worklist
1672-
// so that we can revisit it.
1673-
// Note that since this is a virtual register, the definition instruction
1674-
// is always placed earlier in the program order. Thus, we avoid
1675-
// enqueuing blocks in cycle and therefore guarantee to terminate.
1676-
if (RISCVInstrInfo::isVectorConfigInstr(*VLOpDef))
1677-
Worklist.insert(VLOpDef->getParent());
1678-
}
1665+
if (VLOpDef && TII->isAddImmediate(*VLOpDef, OldVLReg) &&
1666+
MRI->use_nodbg_empty(OldVLReg))
1667+
ToDelete.push_back(VLOpDef);
16791668
};
16801669

16811670
for (MachineInstr &MI : make_early_inc_range(reverse(MBB))) {
@@ -1852,14 +1841,11 @@ bool RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
18521841
// any cross block analysis within the dataflow. We can't have both
18531842
// demanded fields based mutation and non-local analysis in the
18541843
// dataflow at the same time without introducing inconsistencies.
1855-
using BBPtrIterator = pointer_iterator<MachineFunction::iterator>;
1856-
SetVector<MachineBasicBlock *> Worklist(BBPtrIterator(MF.begin()),
1857-
BBPtrIterator(MF.end()));
1858-
while (!Worklist.empty()) {
1859-
MachineBasicBlock *MBB = Worklist.front();
1860-
Worklist.erase(Worklist.begin());
1861-
coalesceVSETVLIs(Worklist, *MBB);
1862-
}
1844+
// We're visiting blocks from the bottom up because a VSETVLI in the
1845+
// earlier block might become dead when its uses in later blocks are
1846+
// optimized away.
1847+
for (MachineBasicBlock *MBB : post_order(&MF))
1848+
coalesceVSETVLIs(*MBB);
18631849

18641850
// Insert PseudoReadVL after VLEFF/VLSEGFF and replace it with the vl output
18651851
// of VLEFF/VLSEGFF.

0 commit comments

Comments
 (0)