Skip to content

Commit

Permalink
Use lazy strategy in MachineLICM and flush updater when it is necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
paperchalice committed Jul 23, 2024
1 parent 080f51f commit 23d2e5d
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions llvm/lib/CodeGen/MachineLICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ namespace {
MachineBlockFrequencyInfo *MBFI = nullptr; // Machine block frequncy info
MachineLoopInfo *MLI = nullptr; // Current MachineLoopInfo
MachineDominatorTree *DT = nullptr; // Machine dominator tree for the cur loop
MachineDomTreeUpdater *MDTU = nullptr;

// State that is updated as we process loops
bool Changed = false; // True if a loop is changed.
Expand Down Expand Up @@ -262,7 +263,8 @@ namespace {
DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
const DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap);

void HoistOutOfLoop(MachineLoop *CurLoop, MachineBasicBlock *CurPreheader);
void HoistOutOfLoop(MachineDomTreeNode *HeaderN, MachineLoop *CurLoop,
MachineBasicBlock *CurPreheader);

void InitRegPressure(MachineBasicBlock *BB);

Expand Down Expand Up @@ -377,6 +379,9 @@ bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) {
MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
DT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
MachineDomTreeUpdater Updater(DT,
MachineDomTreeUpdater::UpdateStrategy::Lazy);
MDTU = &Updater;

if (HoistConstLoads)
InitializeLoadsHoistableLoops();
Expand All @@ -391,8 +396,9 @@ bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) {
else {
// CSEMap is initialized for loop header when the first instruction is
// being hoisted.
MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
FirstInLoop = true;
HoistOutOfLoop(CurLoop, CurPreheader);
HoistOutOfLoop(N, CurLoop, CurPreheader);
CSEMap.clear();
}
}
Expand Down Expand Up @@ -732,6 +738,7 @@ bool MachineLICMBase::IsGuaranteedToExecute(MachineBasicBlock *BB,
// Check loop exiting blocks.
SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
MDTU->flush();
for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
if (!DT->dominates(BB, CurrentLoopExitingBlock)) {
SpeculationState = SpeculateTrue;
Expand Down Expand Up @@ -795,10 +802,10 @@ void MachineLICMBase::ExitScopeIfDone(MachineDomTreeNode *Node,
/// specified header block, and that are in the current loop) in depth first
/// order w.r.t the DominatorTree. This allows us to visit definitions before
/// uses, allowing us to hoist a loop body in one pass without iteration.
void MachineLICMBase::HoistOutOfLoop(MachineLoop *CurLoop,
void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN,
MachineLoop *CurLoop,
MachineBasicBlock *CurPreheader) {
MachineBasicBlock *Preheader = getCurPreheader(CurLoop, CurPreheader);
MachineDomTreeNode *HeaderN = DT->getNode(CurLoop->getHeader());
if (!Preheader)
return;

Expand Down Expand Up @@ -1570,6 +1577,8 @@ bool MachineLICMBase::MayCSE(MachineInstr *MI) {
return false;

unsigned Opcode = MI->getOpcode();
if (!CSEMap.empty())
MDTU->flush();
for (auto &Map : CSEMap) {
// Check this CSEMap's preheader dominates MI's basic block.
if (DT->dominates(Map.first, MI->getParent())) {
Expand Down Expand Up @@ -1638,6 +1647,8 @@ unsigned MachineLICMBase::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader,
// Look for opportunity to CSE the hoisted instruction.
unsigned Opcode = MI->getOpcode();
bool HasCSEDone = false;
if (!CSEMap.empty())
MDTU->flush();
for (auto &Map : CSEMap) {
// Check this CSEMap's preheader dominates MI's basic block.
if (DT->dominates(Map.first, MI->getParent())) {
Expand Down Expand Up @@ -1703,10 +1714,8 @@ MachineLICMBase::getCurPreheader(MachineLoop *CurLoop,
return nullptr;
}

MachineDomTreeUpdater MDTU(DT,
MachineDomTreeUpdater::UpdateStrategy::Eager);
CurPreheader =
Pred->SplitCriticalEdge(CurLoop->getHeader(), *this, nullptr, &MDTU);
CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), *this,
/*LiveInSets=*/nullptr, MDTU);
if (!CurPreheader) {
CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
return nullptr;
Expand Down

0 comments on commit 23d2e5d

Please sign in to comment.