diff --git a/llvm/lib/CodeGen/MachineLICM.cpp b/llvm/lib/CodeGen/MachineLICM.cpp index adf4e8e1ec174d4..f4e76fa4af24fb4 100644 --- a/llvm/lib/CodeGen/MachineLICM.cpp +++ b/llvm/lib/CodeGen/MachineLICM.cpp @@ -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. @@ -262,7 +263,8 @@ namespace { DenseMap &OpenChildren, const DenseMap &ParentMap); - void HoistOutOfLoop(MachineLoop *CurLoop, MachineBasicBlock *CurPreheader); + void HoistOutOfLoop(MachineDomTreeNode *HeaderN, MachineLoop *CurLoop, + MachineBasicBlock *CurPreheader); void InitRegPressure(MachineBasicBlock *BB); @@ -377,6 +379,9 @@ bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) { MLI = &getAnalysis().getLI(); DT = &getAnalysis().getDomTree(); AA = &getAnalysis().getAAResults(); + MachineDomTreeUpdater Updater(DT, + MachineDomTreeUpdater::UpdateStrategy::Lazy); + MDTU = &Updater; if (HoistConstLoads) InitializeLoadsHoistableLoops(); @@ -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(); } } @@ -732,6 +738,7 @@ bool MachineLICMBase::IsGuaranteedToExecute(MachineBasicBlock *BB, // Check loop exiting blocks. SmallVector CurrentLoopExitingBlocks; CurLoop->getExitingBlocks(CurrentLoopExitingBlocks); + MDTU->flush(); for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks) if (!DT->dominates(BB, CurrentLoopExitingBlock)) { SpeculationState = SpeculateTrue; @@ -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; @@ -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())) { @@ -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())) { @@ -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(-1); return nullptr;