@@ -137,6 +137,9 @@ namespace {
137137 bool Changed = false ; // True if a loop is changed.
138138 bool FirstInLoop = false ; // True if it's the first LICM in the loop.
139139
140+ // Holds information about whether it is allowed to move load instructions
141+ // out of the loop
142+ SmallDenseMap<MachineLoop *, bool > AllowedToHoistLoads;
140143
141144 // Exit blocks of each Loop.
142145 DenseMap<MachineLoop *, SmallVector<MachineBasicBlock *, 8 >> ExitBlockMap;
@@ -227,11 +230,9 @@ namespace {
227230
228231 void AddToLiveIns (MCRegister Reg, MachineLoop *CurLoop);
229232
230- bool IsLICMCandidate (MachineInstr &I, MachineLoop *CurLoop,
231- bool SafeToMoveLoad);
233+ bool IsLICMCandidate (MachineInstr &I, MachineLoop *CurLoop);
232234
233- bool IsLoopInvariantInst (MachineInstr &I, MachineLoop *CurLoop,
234- bool SafeToMoveLoad);
235+ bool IsLoopInvariantInst (MachineInstr &I, MachineLoop *CurLoop);
235236
236237 bool HasLoopPHIUse (const MachineInstr *MI, MachineLoop *CurLoop);
237238
@@ -284,7 +285,7 @@ namespace {
284285 bool MayCSE (MachineInstr *MI);
285286
286287 unsigned Hoist (MachineInstr *MI, MachineBasicBlock *Preheader,
287- MachineLoop *CurLoop, bool SafeToMoveLoad );
288+ MachineLoop *CurLoop);
288289
289290 void InitCSEMap (MachineBasicBlock *BB);
290291
@@ -376,6 +377,36 @@ bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) {
376377 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults ();
377378
378379 SmallVector<MachineLoop *, 8 > Worklist (MLI->begin (), MLI->end ());
380+
381+ // Initialize `AllowedToHoistLoads' if needed.
382+ if (HoistConstLoads) {
383+ auto TmpWorklist = Worklist;
384+ // Initialize all loops with true values
385+ while (!TmpWorklist.empty ()) {
386+ auto *L = TmpWorklist.pop_back_val ();
387+ AllowedToHoistLoads[L] = true ;
388+ TmpWorklist.insert (TmpWorklist.end (), L->getSubLoops ().begin (),
389+ L->getSubLoops ().end ());
390+ }
391+ // Go through all the instructions inside top-level loops and, after finding
392+ // one that makes it potentially unsafe to move loads, update load hoisting
393+ // information for each loop containing this instruction.
394+ for (auto *TopLoop : Worklist) {
395+ for (auto *MBB : TopLoop->blocks ()) {
396+ for (auto &MI : *MBB) {
397+ if (!MI.mayStore () && !MI.isCall () &&
398+ !(MI.mayLoad () && MI.hasOrderedMemoryRef ()))
399+ continue ;
400+ for (MachineLoop *L = MLI->getLoopFor (MI.getParent ()); L != TopLoop;
401+ L = L->getParentLoop ())
402+ AllowedToHoistLoads[L] = false ;
403+ AllowedToHoistLoads[TopLoop] = false ;
404+ break ;
405+ }
406+ }
407+ }
408+ }
409+
379410 while (!Worklist.empty ()) {
380411 MachineLoop *CurLoop = Worklist.pop_back_val ();
381412 MachineBasicBlock *CurPreheader = nullptr ;
@@ -501,7 +532,7 @@ void MachineLICMBase::ProcessMI(MachineInstr *MI, BitVector &PhysRegDefs,
501532 // operands. FIXME: Consider unfold load folding instructions.
502533 if (Def && !RuledOut) {
503534 int FI = std::numeric_limits<int >::min ();
504- if ((!HasNonInvariantUse && IsLICMCandidate (*MI, CurLoop, false )) ||
535+ if ((!HasNonInvariantUse && IsLICMCandidate (*MI, CurLoop)) ||
505536 (TII->isLoadFromStackSlot (*MI, FI) && MFI->isSpillSlotObjectIndex (FI)))
506537 Candidates.push_back (CandidateInfo (MI, Def, FI));
507538 }
@@ -779,33 +810,6 @@ void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN,
779810 BackTrace.clear ();
780811 InitRegPressure (Preheader);
781812
782- // Compute information about whether it is allowed to move load instruction
783- // out of the current loop or one of the inner loops
784- SmallDenseMap<MachineLoop *, bool > AllowedToHoistLoads;
785- if (HoistConstLoads) {
786- SmallVector<MachineLoop *, 4 > Worklist{CurLoop};
787-
788- while (!Worklist.empty ()) {
789- auto *L = Worklist.pop_back_val ();
790- AllowedToHoistLoads[L] = true ;
791- Worklist.insert (Worklist.end (), L->getSubLoops ().begin (),
792- L->getSubLoops ().end ());
793- }
794-
795- for (auto *MBB : CurLoop->blocks ()) {
796- for (auto &MI : *MBB) {
797- if (MI.mayStore () || MI.isCall () ||
798- (MI.mayLoad () && MI.hasOrderedMemoryRef ())) {
799- for (MachineLoop *L = MLI->getLoopFor (MI.getParent ()); L != CurLoop;
800- L = L->getParentLoop ())
801- AllowedToHoistLoads[L] = false ;
802- AllowedToHoistLoads[CurLoop] = false ;
803- break ;
804- }
805- }
806- }
807- }
808-
809813 // Now perform LICM.
810814 for (MachineDomTreeNode *Node : Scopes) {
811815 MachineBasicBlock *MBB = Node->getBlock ();
@@ -814,10 +818,9 @@ void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN,
814818
815819 // Process the block
816820 SpeculationState = SpeculateUnknown;
817- bool SafeToMoveLoad = HoistConstLoads && AllowedToHoistLoads[CurLoop];
818821 for (MachineInstr &MI : llvm::make_early_inc_range (*MBB)) {
819822 unsigned HoistRes = HoistResult::NotHoisted;
820- HoistRes = Hoist (&MI, Preheader, CurLoop, SafeToMoveLoad );
823+ HoistRes = Hoist (&MI, Preheader, CurLoop);
821824 if (HoistRes & HoistResult::NotHoisted) {
822825 // We have failed to hoist MI to outermost loop's preheader. If MI is in
823826 // a subloop, try to hoist it to subloop's preheader.
@@ -828,12 +831,9 @@ void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN,
828831
829832 while (!InnerLoopWorkList.empty ()) {
830833 MachineLoop *InnerLoop = InnerLoopWorkList.pop_back_val ();
831- bool SafeToMoveLoadInner =
832- HoistConstLoads && AllowedToHoistLoads[InnerLoop];
833834 MachineBasicBlock *InnerLoopPreheader = InnerLoop->getLoopPreheader ();
834835 if (InnerLoopPreheader) {
835- HoistRes =
836- Hoist (&MI, InnerLoopPreheader, InnerLoop, SafeToMoveLoadInner);
836+ HoistRes = Hoist (&MI, InnerLoopPreheader, InnerLoop);
837837 if (HoistRes & HoistResult::Hoisted)
838838 break ;
839839 }
@@ -1028,10 +1028,9 @@ static bool isCopyFeedingInvariantStore(const MachineInstr &MI,
10281028
10291029// / Returns true if the instruction may be a suitable candidate for LICM.
10301030// / e.g. If the instruction is a call, then it's obviously not safe to hoist it.
1031- bool MachineLICMBase::IsLICMCandidate (MachineInstr &I, MachineLoop *CurLoop,
1032- bool SafeToMoveLoad) {
1031+ bool MachineLICMBase::IsLICMCandidate (MachineInstr &I, MachineLoop *CurLoop) {
10331032 // Check if it's safe to move the instruction.
1034- bool DontMoveAcrossStore = !SafeToMoveLoad ;
1033+ bool DontMoveAcrossStore = !HoistConstLoads || !AllowedToHoistLoads[CurLoop] ;
10351034 if ((!I.isSafeToMove (AA, DontMoveAcrossStore)) &&
10361035 !(HoistConstStores && isInvariantStore (I, TRI, MRI))) {
10371036 LLVM_DEBUG (dbgs () << " LICM: Instruction not safe to move.\n " );
@@ -1064,9 +1063,9 @@ bool MachineLICMBase::IsLICMCandidate(MachineInstr &I, MachineLoop *CurLoop,
10641063}
10651064
10661065// / Returns true if the instruction is loop invariant.
1067- bool MachineLICMBase::IsLoopInvariantInst (MachineInstr &I, MachineLoop *CurLoop,
1068- bool SafeToMoveLoad ) {
1069- if (!IsLICMCandidate (I, CurLoop, SafeToMoveLoad )) {
1066+ bool MachineLICMBase::IsLoopInvariantInst (MachineInstr &I,
1067+ MachineLoop *CurLoop ) {
1068+ if (!IsLICMCandidate (I, CurLoop)) {
10701069 LLVM_DEBUG (dbgs () << " LICM: Instruction not a LICM candidate\n " );
10711070 return false ;
10721071 }
@@ -1344,7 +1343,7 @@ MachineInstr *MachineLICMBase::ExtractHoistableLoad(MachineInstr *MI,
13441343 MBB->insert (Pos, NewMIs[1 ]);
13451344 // If unfolding produced a load that wasn't loop-invariant or profitable to
13461345 // hoist, discard the new instructions and bail.
1347- if (!IsLoopInvariantInst (*NewMIs[0 ], CurLoop, /* SaveToMovLoad= */ false ) ||
1346+ if (!IsLoopInvariantInst (*NewMIs[0 ], CurLoop) ||
13481347 !IsProfitableToHoist (*NewMIs[0 ], CurLoop)) {
13491348 NewMIs[0 ]->eraseFromParent ();
13501349 NewMIs[1 ]->eraseFromParent ();
@@ -1471,7 +1470,7 @@ bool MachineLICMBase::MayCSE(MachineInstr *MI) {
14711470// / that are safe to hoist, this instruction is called to do the dirty work.
14721471// / It returns true if the instruction is hoisted.
14731472unsigned MachineLICMBase::Hoist (MachineInstr *MI, MachineBasicBlock *Preheader,
1474- MachineLoop *CurLoop, bool SafeToMoveLoad ) {
1473+ MachineLoop *CurLoop) {
14751474 MachineBasicBlock *SrcBlock = MI->getParent ();
14761475
14771476 // Disable the instruction hoisting due to block hotness
@@ -1483,7 +1482,7 @@ unsigned MachineLICMBase::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader,
14831482 }
14841483 // First check whether we should hoist this instruction.
14851484 bool HasExtractHoistableLoad = false ;
1486- if (!IsLoopInvariantInst (*MI, CurLoop, SafeToMoveLoad ) ||
1485+ if (!IsLoopInvariantInst (*MI, CurLoop) ||
14871486 !IsProfitableToHoist (*MI, CurLoop)) {
14881487 // If not, try unfolding a hoistable load.
14891488 MI = ExtractHoistableLoad (MI, CurLoop);
0 commit comments