1 //===- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass performs loop invariant code motion on machine instructions. We
10 // attempt to remove as much code from the body of a loop as possible.
11 //
12 // This pass is not intended to be a replacement or a complete alternative
13 // for the LLVM-IR-level LICM pass. It is only designed to hoist simple
14 // constructs that are not exposed before lowering and instruction selection.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
27 #include "llvm/CodeGen/MachineDominators.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/CodeGen/MachineLoopInfo.h"
33 #include "llvm/CodeGen/MachineMemOperand.h"
34 #include "llvm/CodeGen/MachineOperand.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/PseudoSourceValue.h"
37 #include "llvm/CodeGen/TargetInstrInfo.h"
38 #include "llvm/CodeGen/TargetLowering.h"
39 #include "llvm/CodeGen/TargetRegisterInfo.h"
40 #include "llvm/CodeGen/TargetSchedule.h"
41 #include "llvm/CodeGen/TargetSubtargetInfo.h"
42 #include "llvm/IR/DebugLoc.h"
43 #include "llvm/InitializePasses.h"
44 #include "llvm/MC/MCInstrDesc.h"
45 #include "llvm/MC/MCRegister.h"
46 #include "llvm/MC/MCRegisterInfo.h"
47 #include "llvm/Pass.h"
48 #include "llvm/Support/Casting.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Debug.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <limits>
55 #include <vector>
56 
57 using namespace llvm;
58 
59 #define DEBUG_TYPE "machinelicm"
60 
61 static cl::opt<bool>
62 AvoidSpeculation("avoid-speculation",
63                  cl::desc("MachineLICM should avoid speculation"),
64                  cl::init(true), cl::Hidden);
65 
66 static cl::opt<bool>
67 HoistCheapInsts("hoist-cheap-insts",
68                 cl::desc("MachineLICM should hoist even cheap instructions"),
69                 cl::init(false), cl::Hidden);
70 
71 static cl::opt<bool>
72 HoistConstStores("hoist-const-stores",
73                  cl::desc("Hoist invariant stores"),
74                  cl::init(true), cl::Hidden);
75 // The default threshold of 100 (i.e. if target block is 100 times hotter)
76 // is based on empirical data on a single target and is subject to tuning.
77 static cl::opt<unsigned>
78 BlockFrequencyRatioThreshold("block-freq-ratio-threshold",
79                              cl::desc("Do not hoist instructions if target"
80                              "block is N times hotter than the source."),
81                              cl::init(100), cl::Hidden);
82 
83 enum class UseBFI { None, PGO, All };
84 
85 static cl::opt<UseBFI>
86 DisableHoistingToHotterBlocks("disable-hoisting-to-hotter-blocks",
87                               cl::desc("Disable hoisting instructions to"
88                               " hotter blocks"),
89                               cl::init(UseBFI::PGO), cl::Hidden,
90                               cl::values(clEnumValN(UseBFI::None, "none",
91                               "disable the feature"),
92                               clEnumValN(UseBFI::PGO, "pgo",
93                               "enable the feature when using profile data"),
94                               clEnumValN(UseBFI::All, "all",
95                               "enable the feature with/wo profile data")));
96 
97 STATISTIC(NumHoisted,
98           "Number of machine instructions hoisted out of loops");
99 STATISTIC(NumLowRP,
100           "Number of instructions hoisted in low reg pressure situation");
101 STATISTIC(NumHighLatency,
102           "Number of high latency instructions hoisted");
103 STATISTIC(NumCSEed,
104           "Number of hoisted machine instructions CSEed");
105 STATISTIC(NumPostRAHoisted,
106           "Number of machine instructions hoisted out of loops post regalloc");
107 STATISTIC(NumStoreConst,
108           "Number of stores of const phys reg hoisted out of loops");
109 STATISTIC(NumNotHoistedDueToHotness,
110           "Number of instructions not hoisted due to block frequency");
111 
112 namespace {
113 
114   class MachineLICMBase : public MachineFunctionPass {
115     const TargetInstrInfo *TII;
116     const TargetLoweringBase *TLI;
117     const TargetRegisterInfo *TRI;
118     const MachineFrameInfo *MFI;
119     MachineRegisterInfo *MRI;
120     TargetSchedModel SchedModel;
121     bool PreRegAlloc;
122     bool HasProfileData;
123 
124     // Various analyses that we use...
125     AliasAnalysis        *AA;      // Alias analysis info.
126     MachineBlockFrequencyInfo *MBFI; // Machine block frequncy info
127     MachineLoopInfo      *MLI;     // Current MachineLoopInfo
128     MachineDominatorTree *DT;      // Machine dominator tree for the cur loop
129 
130     // State that is updated as we process loops
131     bool         Changed;          // True if a loop is changed.
132     bool         FirstInLoop;      // True if it's the first LICM in the loop.
133     MachineLoop *CurLoop;          // The current loop we are working on.
134     MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
135 
136     // Exit blocks for CurLoop.
137     SmallVector<MachineBasicBlock *, 8> ExitBlocks;
138 
139     bool isExitBlock(const MachineBasicBlock *MBB) const {
140       return is_contained(ExitBlocks, MBB);
141     }
142 
143     // Track 'estimated' register pressure.
144     SmallSet<Register, 32> RegSeen;
145     SmallVector<unsigned, 8> RegPressure;
146 
147     // Register pressure "limit" per register pressure set. If the pressure
148     // is higher than the limit, then it's considered high.
149     SmallVector<unsigned, 8> RegLimit;
150 
151     // Register pressure on path leading from loop preheader to current BB.
152     SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
153 
154     // For each opcode, keep a list of potential CSE instructions.
155     DenseMap<unsigned, std::vector<MachineInstr *>> CSEMap;
156 
157     enum {
158       SpeculateFalse   = 0,
159       SpeculateTrue    = 1,
160       SpeculateUnknown = 2
161     };
162 
163     // If a MBB does not dominate loop exiting blocks then it may not safe
164     // to hoist loads from this block.
165     // Tri-state: 0 - false, 1 - true, 2 - unknown
166     unsigned SpeculationState;
167 
168   public:
169     MachineLICMBase(char &PassID, bool PreRegAlloc)
170         : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
171 
172     bool runOnMachineFunction(MachineFunction &MF) override;
173 
174     void getAnalysisUsage(AnalysisUsage &AU) const override {
175       AU.addRequired<MachineLoopInfo>();
176       if (DisableHoistingToHotterBlocks != UseBFI::None)
177         AU.addRequired<MachineBlockFrequencyInfo>();
178       AU.addRequired<MachineDominatorTree>();
179       AU.addRequired<AAResultsWrapperPass>();
180       AU.addPreserved<MachineLoopInfo>();
181       MachineFunctionPass::getAnalysisUsage(AU);
182     }
183 
184     void releaseMemory() override {
185       RegSeen.clear();
186       RegPressure.clear();
187       RegLimit.clear();
188       BackTrace.clear();
189       CSEMap.clear();
190     }
191 
192   private:
193     /// Keep track of information about hoisting candidates.
194     struct CandidateInfo {
195       MachineInstr *MI;
196       unsigned      Def;
197       int           FI;
198 
199       CandidateInfo(MachineInstr *mi, unsigned def, int fi)
200         : MI(mi), Def(def), FI(fi) {}
201     };
202 
203     void HoistRegionPostRA();
204 
205     void HoistPostRA(MachineInstr *MI, unsigned Def);
206 
207     void ProcessMI(MachineInstr *MI, BitVector &PhysRegDefs,
208                    BitVector &PhysRegClobbers, SmallSet<int, 32> &StoredFIs,
209                    SmallVectorImpl<CandidateInfo> &Candidates);
210 
211     void AddToLiveIns(MCRegister Reg);
212 
213     bool IsLICMCandidate(MachineInstr &I);
214 
215     bool IsLoopInvariantInst(MachineInstr &I);
216 
217     bool HasLoopPHIUse(const MachineInstr *MI) const;
218 
219     bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
220                                Register Reg) const;
221 
222     bool IsCheapInstruction(MachineInstr &MI) const;
223 
224     bool CanCauseHighRegPressure(const DenseMap<unsigned, int> &Cost,
225                                  bool Cheap);
226 
227     void UpdateBackTraceRegPressure(const MachineInstr *MI);
228 
229     bool IsProfitableToHoist(MachineInstr &MI);
230 
231     bool IsGuaranteedToExecute(MachineBasicBlock *BB);
232 
233     bool isTriviallyReMaterializable(const MachineInstr &MI,
234                                      AAResults *AA) const;
235 
236     void EnterScope(MachineBasicBlock *MBB);
237 
238     void ExitScope(MachineBasicBlock *MBB);
239 
240     void ExitScopeIfDone(
241         MachineDomTreeNode *Node,
242         DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
243         DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap);
244 
245     void HoistOutOfLoop(MachineDomTreeNode *HeaderN);
246 
247     void InitRegPressure(MachineBasicBlock *BB);
248 
249     DenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI,
250                                              bool ConsiderSeen,
251                                              bool ConsiderUnseenAsDef);
252 
253     void UpdateRegPressure(const MachineInstr *MI,
254                            bool ConsiderUnseenAsDef = false);
255 
256     MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
257 
258     MachineInstr *LookForDuplicate(const MachineInstr *MI,
259                                    std::vector<MachineInstr *> &PrevMIs);
260 
261     bool
262     EliminateCSE(MachineInstr *MI,
263                  DenseMap<unsigned, std::vector<MachineInstr *>>::iterator &CI);
264 
265     bool MayCSE(MachineInstr *MI);
266 
267     bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
268 
269     void InitCSEMap(MachineBasicBlock *BB);
270 
271     bool isTgtHotterThanSrc(MachineBasicBlock *SrcBlock,
272                             MachineBasicBlock *TgtBlock);
273     MachineBasicBlock *getCurPreheader();
274   };
275 
276   class MachineLICM : public MachineLICMBase {
277   public:
278     static char ID;
279     MachineLICM() : MachineLICMBase(ID, false) {
280       initializeMachineLICMPass(*PassRegistry::getPassRegistry());
281     }
282   };
283 
284   class EarlyMachineLICM : public MachineLICMBase {
285   public:
286     static char ID;
287     EarlyMachineLICM() : MachineLICMBase(ID, true) {
288       initializeEarlyMachineLICMPass(*PassRegistry::getPassRegistry());
289     }
290   };
291 
292 } // end anonymous namespace
293 
294 char MachineLICM::ID;
295 char EarlyMachineLICM::ID;
296 
297 char &llvm::MachineLICMID = MachineLICM::ID;
298 char &llvm::EarlyMachineLICMID = EarlyMachineLICM::ID;
299 
300 INITIALIZE_PASS_BEGIN(MachineLICM, DEBUG_TYPE,
301                       "Machine Loop Invariant Code Motion", false, false)
302 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
303 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
304 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
305 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
306 INITIALIZE_PASS_END(MachineLICM, DEBUG_TYPE,
307                     "Machine Loop Invariant Code Motion", false, false)
308 
309 INITIALIZE_PASS_BEGIN(EarlyMachineLICM, "early-machinelicm",
310                       "Early Machine Loop Invariant Code Motion", false, false)
311 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
312 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
313 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
314 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
315 INITIALIZE_PASS_END(EarlyMachineLICM, "early-machinelicm",
316                     "Early Machine Loop Invariant Code Motion", false, false)
317 
318 /// Test if the given loop is the outer-most loop that has a unique predecessor.
319 static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
320   // Check whether this loop even has a unique predecessor.
321   if (!CurLoop->getLoopPredecessor())
322     return false;
323   // Ok, now check to see if any of its outer loops do.
324   for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
325     if (L->getLoopPredecessor())
326       return false;
327   // None of them did, so this is the outermost with a unique predecessor.
328   return true;
329 }
330 
331 bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) {
332   if (skipFunction(MF.getFunction()))
333     return false;
334 
335   Changed = FirstInLoop = false;
336   const TargetSubtargetInfo &ST = MF.getSubtarget();
337   TII = ST.getInstrInfo();
338   TLI = ST.getTargetLowering();
339   TRI = ST.getRegisterInfo();
340   MFI = &MF.getFrameInfo();
341   MRI = &MF.getRegInfo();
342   SchedModel.init(&ST);
343 
344   PreRegAlloc = MRI->isSSA();
345   HasProfileData = MF.getFunction().hasProfileData();
346 
347   if (PreRegAlloc)
348     LLVM_DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
349   else
350     LLVM_DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
351   LLVM_DEBUG(dbgs() << MF.getName() << " ********\n");
352 
353   if (PreRegAlloc) {
354     // Estimate register pressure during pre-regalloc pass.
355     unsigned NumRPS = TRI->getNumRegPressureSets();
356     RegPressure.resize(NumRPS);
357     std::fill(RegPressure.begin(), RegPressure.end(), 0);
358     RegLimit.resize(NumRPS);
359     for (unsigned i = 0, e = NumRPS; i != e; ++i)
360       RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
361   }
362 
363   // Get our Loop information...
364   if (DisableHoistingToHotterBlocks != UseBFI::None)
365     MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
366   MLI = &getAnalysis<MachineLoopInfo>();
367   DT  = &getAnalysis<MachineDominatorTree>();
368   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
369 
370   SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
371   while (!Worklist.empty()) {
372     CurLoop = Worklist.pop_back_val();
373     CurPreheader = nullptr;
374     ExitBlocks.clear();
375 
376     // If this is done before regalloc, only visit outer-most preheader-sporting
377     // loops.
378     if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
379       Worklist.append(CurLoop->begin(), CurLoop->end());
380       continue;
381     }
382 
383     CurLoop->getExitBlocks(ExitBlocks);
384 
385     if (!PreRegAlloc)
386       HoistRegionPostRA();
387     else {
388       // CSEMap is initialized for loop header when the first instruction is
389       // being hoisted.
390       MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
391       FirstInLoop = true;
392       HoistOutOfLoop(N);
393       CSEMap.clear();
394     }
395   }
396 
397   return Changed;
398 }
399 
400 /// Return true if instruction stores to the specified frame.
401 static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
402   // Check mayStore before memory operands so that e.g. DBG_VALUEs will return
403   // true since they have no memory operands.
404   if (!MI->mayStore())
405      return false;
406   // If we lost memory operands, conservatively assume that the instruction
407   // writes to all slots.
408   if (MI->memoperands_empty())
409     return true;
410   for (const MachineMemOperand *MemOp : MI->memoperands()) {
411     if (!MemOp->isStore() || !MemOp->getPseudoValue())
412       continue;
413     if (const FixedStackPseudoSourceValue *Value =
414         dyn_cast<FixedStackPseudoSourceValue>(MemOp->getPseudoValue())) {
415       if (Value->getFrameIndex() == FI)
416         return true;
417     }
418   }
419   return false;
420 }
421 
422 /// Examine the instruction for potentai LICM candidate. Also
423 /// gather register def and frame object update information.
424 void MachineLICMBase::ProcessMI(MachineInstr *MI,
425                                 BitVector &PhysRegDefs,
426                                 BitVector &PhysRegClobbers,
427                                 SmallSet<int, 32> &StoredFIs,
428                                 SmallVectorImpl<CandidateInfo> &Candidates) {
429   bool RuledOut = false;
430   bool HasNonInvariantUse = false;
431   unsigned Def = 0;
432   for (const MachineOperand &MO : MI->operands()) {
433     if (MO.isFI()) {
434       // Remember if the instruction stores to the frame index.
435       int FI = MO.getIndex();
436       if (!StoredFIs.count(FI) &&
437           MFI->isSpillSlotObjectIndex(FI) &&
438           InstructionStoresToFI(MI, FI))
439         StoredFIs.insert(FI);
440       HasNonInvariantUse = true;
441       continue;
442     }
443 
444     // We can't hoist an instruction defining a physreg that is clobbered in
445     // the loop.
446     if (MO.isRegMask()) {
447       PhysRegClobbers.setBitsNotInMask(MO.getRegMask());
448       continue;
449     }
450 
451     if (!MO.isReg())
452       continue;
453     Register Reg = MO.getReg();
454     if (!Reg)
455       continue;
456     assert(Register::isPhysicalRegister(Reg) &&
457            "Not expecting virtual register!");
458 
459     if (!MO.isDef()) {
460       if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg)))
461         // If it's using a non-loop-invariant register, then it's obviously not
462         // safe to hoist.
463         HasNonInvariantUse = true;
464       continue;
465     }
466 
467     if (MO.isImplicit()) {
468       for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
469         PhysRegClobbers.set(*AI);
470       if (!MO.isDead())
471         // Non-dead implicit def? This cannot be hoisted.
472         RuledOut = true;
473       // No need to check if a dead implicit def is also defined by
474       // another instruction.
475       continue;
476     }
477 
478     // FIXME: For now, avoid instructions with multiple defs, unless
479     // it's a dead implicit def.
480     if (Def)
481       RuledOut = true;
482     else
483       Def = Reg;
484 
485     // If we have already seen another instruction that defines the same
486     // register, then this is not safe.  Two defs is indicated by setting a
487     // PhysRegClobbers bit.
488     for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) {
489       if (PhysRegDefs.test(*AS))
490         PhysRegClobbers.set(*AS);
491     }
492     // Need a second loop because MCRegAliasIterator can visit the same
493     // register twice.
494     for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS)
495       PhysRegDefs.set(*AS);
496 
497     if (PhysRegClobbers.test(Reg))
498       // MI defined register is seen defined by another instruction in
499       // the loop, it cannot be a LICM candidate.
500       RuledOut = true;
501   }
502 
503   // Only consider reloads for now and remats which do not have register
504   // operands. FIXME: Consider unfold load folding instructions.
505   if (Def && !RuledOut) {
506     int FI = std::numeric_limits<int>::min();
507     if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
508         (TII->isLoadFromStackSlot(*MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
509       Candidates.push_back(CandidateInfo(MI, Def, FI));
510   }
511 }
512 
513 /// Walk the specified region of the CFG and hoist loop invariants out to the
514 /// preheader.
515 void MachineLICMBase::HoistRegionPostRA() {
516   MachineBasicBlock *Preheader = getCurPreheader();
517   if (!Preheader)
518     return;
519 
520   unsigned NumRegs = TRI->getNumRegs();
521   BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop.
522   BitVector PhysRegClobbers(NumRegs); // Regs defined more than once.
523 
524   SmallVector<CandidateInfo, 32> Candidates;
525   SmallSet<int, 32> StoredFIs;
526 
527   // Walk the entire region, count number of defs for each register, and
528   // collect potential LICM candidates.
529   for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
530     // If the header of the loop containing this basic block is a landing pad,
531     // then don't try to hoist instructions out of this loop.
532     const MachineLoop *ML = MLI->getLoopFor(BB);
533     if (ML && ML->getHeader()->isEHPad()) continue;
534 
535     // Conservatively treat live-in's as an external def.
536     // FIXME: That means a reload that're reused in successor block(s) will not
537     // be LICM'ed.
538     for (const auto &LI : BB->liveins()) {
539       for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI)
540         PhysRegDefs.set(*AI);
541     }
542 
543     SpeculationState = SpeculateUnknown;
544     for (MachineInstr &MI : *BB)
545       ProcessMI(&MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
546   }
547 
548   // Gather the registers read / clobbered by the terminator.
549   BitVector TermRegs(NumRegs);
550   MachineBasicBlock::iterator TI = Preheader->getFirstTerminator();
551   if (TI != Preheader->end()) {
552     for (const MachineOperand &MO : TI->operands()) {
553       if (!MO.isReg())
554         continue;
555       Register Reg = MO.getReg();
556       if (!Reg)
557         continue;
558       for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
559         TermRegs.set(*AI);
560     }
561   }
562 
563   // Now evaluate whether the potential candidates qualify.
564   // 1. Check if the candidate defined register is defined by another
565   //    instruction in the loop.
566   // 2. If the candidate is a load from stack slot (always true for now),
567   //    check if the slot is stored anywhere in the loop.
568   // 3. Make sure candidate def should not clobber
569   //    registers read by the terminator. Similarly its def should not be
570   //    clobbered by the terminator.
571   for (CandidateInfo &Candidate : Candidates) {
572     if (Candidate.FI != std::numeric_limits<int>::min() &&
573         StoredFIs.count(Candidate.FI))
574       continue;
575 
576     unsigned Def = Candidate.Def;
577     if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) {
578       bool Safe = true;
579       MachineInstr *MI = Candidate.MI;
580       for (const MachineOperand &MO : MI->operands()) {
581         if (!MO.isReg() || MO.isDef() || !MO.getReg())
582           continue;
583         Register Reg = MO.getReg();
584         if (PhysRegDefs.test(Reg) ||
585             PhysRegClobbers.test(Reg)) {
586           // If it's using a non-loop-invariant register, then it's obviously
587           // not safe to hoist.
588           Safe = false;
589           break;
590         }
591       }
592       if (Safe)
593         HoistPostRA(MI, Candidate.Def);
594     }
595   }
596 }
597 
598 /// Add register 'Reg' to the livein sets of BBs in the current loop, and make
599 /// sure it is not killed by any instructions in the loop.
600 void MachineLICMBase::AddToLiveIns(MCRegister Reg) {
601   for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
602     if (!BB->isLiveIn(Reg))
603       BB->addLiveIn(Reg);
604     for (MachineInstr &MI : *BB) {
605       for (MachineOperand &MO : MI.operands()) {
606         if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
607         if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
608           MO.setIsKill(false);
609       }
610     }
611   }
612 }
613 
614 /// When an instruction is found to only use loop invariant operands that is
615 /// safe to hoist, this instruction is called to do the dirty work.
616 void MachineLICMBase::HoistPostRA(MachineInstr *MI, unsigned Def) {
617   MachineBasicBlock *Preheader = getCurPreheader();
618 
619   // Now move the instructions to the predecessor, inserting it before any
620   // terminator instructions.
621   LLVM_DEBUG(dbgs() << "Hoisting to " << printMBBReference(*Preheader)
622                     << " from " << printMBBReference(*MI->getParent()) << ": "
623                     << *MI);
624 
625   // Splice the instruction to the preheader.
626   MachineBasicBlock *MBB = MI->getParent();
627   Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
628 
629   // Since we are moving the instruction out of its basic block, we do not
630   // retain its debug location. Doing so would degrade the debugging
631   // experience and adversely affect the accuracy of profiling information.
632   assert(!MI->isDebugInstr() && "Should not hoist debug inst");
633   MI->setDebugLoc(DebugLoc());
634 
635   // Add register to livein list to all the BBs in the current loop since a
636   // loop invariant must be kept live throughout the whole loop. This is
637   // important to ensure later passes do not scavenge the def register.
638   AddToLiveIns(Def);
639 
640   ++NumPostRAHoisted;
641   Changed = true;
642 }
643 
644 /// Check if this mbb is guaranteed to execute. If not then a load from this mbb
645 /// may not be safe to hoist.
646 bool MachineLICMBase::IsGuaranteedToExecute(MachineBasicBlock *BB) {
647   if (SpeculationState != SpeculateUnknown)
648     return SpeculationState == SpeculateFalse;
649 
650   if (BB != CurLoop->getHeader()) {
651     // Check loop exiting blocks.
652     SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
653     CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
654     for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
655       if (!DT->dominates(BB, CurrentLoopExitingBlock)) {
656         SpeculationState = SpeculateTrue;
657         return false;
658       }
659   }
660 
661   SpeculationState = SpeculateFalse;
662   return true;
663 }
664 
665 /// Check if \p MI is trivially remateralizable and if it does not have any
666 /// virtual register uses. Even though rematerializable RA might not actually
667 /// rematerialize it in this scenario. In that case we do not want to hoist such
668 /// instruction out of the loop in a belief RA will sink it back if needed.
669 bool MachineLICMBase::isTriviallyReMaterializable(const MachineInstr &MI,
670                                                   AAResults *AA) const {
671   if (!TII->isTriviallyReMaterializable(MI, AA))
672     return false;
673 
674   for (const MachineOperand &MO : MI.operands()) {
675     if (MO.isReg() && MO.isUse() && MO.getReg().isVirtual())
676       return false;
677   }
678 
679   return true;
680 }
681 
682 void MachineLICMBase::EnterScope(MachineBasicBlock *MBB) {
683   LLVM_DEBUG(dbgs() << "Entering " << printMBBReference(*MBB) << '\n');
684 
685   // Remember livein register pressure.
686   BackTrace.push_back(RegPressure);
687 }
688 
689 void MachineLICMBase::ExitScope(MachineBasicBlock *MBB) {
690   LLVM_DEBUG(dbgs() << "Exiting " << printMBBReference(*MBB) << '\n');
691   BackTrace.pop_back();
692 }
693 
694 /// Destroy scope for the MBB that corresponds to the given dominator tree node
695 /// if its a leaf or all of its children are done. Walk up the dominator tree to
696 /// destroy ancestors which are now done.
697 void MachineLICMBase::ExitScopeIfDone(MachineDomTreeNode *Node,
698     DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
699     DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) {
700   if (OpenChildren[Node])
701     return;
702 
703   // Pop scope.
704   ExitScope(Node->getBlock());
705 
706   // Now traverse upwards to pop ancestors whose offsprings are all done.
707   while (MachineDomTreeNode *Parent = ParentMap[Node]) {
708     unsigned Left = --OpenChildren[Parent];
709     if (Left != 0)
710       break;
711     ExitScope(Parent->getBlock());
712     Node = Parent;
713   }
714 }
715 
716 /// Walk the specified loop in the CFG (defined by all blocks dominated by the
717 /// specified header block, and that are in the current loop) in depth first
718 /// order w.r.t the DominatorTree. This allows us to visit definitions before
719 /// uses, allowing us to hoist a loop body in one pass without iteration.
720 void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
721   MachineBasicBlock *Preheader = getCurPreheader();
722   if (!Preheader)
723     return;
724 
725   SmallVector<MachineDomTreeNode*, 32> Scopes;
726   SmallVector<MachineDomTreeNode*, 8> WorkList;
727   DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
728   DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
729 
730   // Perform a DFS walk to determine the order of visit.
731   WorkList.push_back(HeaderN);
732   while (!WorkList.empty()) {
733     MachineDomTreeNode *Node = WorkList.pop_back_val();
734     assert(Node && "Null dominator tree node?");
735     MachineBasicBlock *BB = Node->getBlock();
736 
737     // If the header of the loop containing this basic block is a landing pad,
738     // then don't try to hoist instructions out of this loop.
739     const MachineLoop *ML = MLI->getLoopFor(BB);
740     if (ML && ML->getHeader()->isEHPad())
741       continue;
742 
743     // If this subregion is not in the top level loop at all, exit.
744     if (!CurLoop->contains(BB))
745       continue;
746 
747     Scopes.push_back(Node);
748     unsigned NumChildren = Node->getNumChildren();
749 
750     // Don't hoist things out of a large switch statement.  This often causes
751     // code to be hoisted that wasn't going to be executed, and increases
752     // register pressure in a situation where it's likely to matter.
753     if (BB->succ_size() >= 25)
754       NumChildren = 0;
755 
756     OpenChildren[Node] = NumChildren;
757     if (NumChildren) {
758       // Add children in reverse order as then the next popped worklist node is
759       // the first child of this node.  This means we ultimately traverse the
760       // DOM tree in exactly the same order as if we'd recursed.
761       for (MachineDomTreeNode *Child : reverse(Node->children())) {
762         ParentMap[Child] = Node;
763         WorkList.push_back(Child);
764       }
765     }
766   }
767 
768   if (Scopes.size() == 0)
769     return;
770 
771   // Compute registers which are livein into the loop headers.
772   RegSeen.clear();
773   BackTrace.clear();
774   InitRegPressure(Preheader);
775 
776   // Now perform LICM.
777   for (MachineDomTreeNode *Node : Scopes) {
778     MachineBasicBlock *MBB = Node->getBlock();
779 
780     EnterScope(MBB);
781 
782     // Process the block
783     SpeculationState = SpeculateUnknown;
784     for (MachineInstr &MI : llvm::make_early_inc_range(*MBB)) {
785       if (!Hoist(&MI, Preheader))
786         UpdateRegPressure(&MI);
787       // If we have hoisted an instruction that may store, it can only be a
788       // constant store.
789     }
790 
791     // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
792     ExitScopeIfDone(Node, OpenChildren, ParentMap);
793   }
794 }
795 
796 static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
797   return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
798 }
799 
800 /// Find all virtual register references that are liveout of the preheader to
801 /// initialize the starting "register pressure". Note this does not count live
802 /// through (livein but not used) registers.
803 void MachineLICMBase::InitRegPressure(MachineBasicBlock *BB) {
804   std::fill(RegPressure.begin(), RegPressure.end(), 0);
805 
806   // If the preheader has only a single predecessor and it ends with a
807   // fallthrough or an unconditional branch, then scan its predecessor for live
808   // defs as well. This happens whenever the preheader is created by splitting
809   // the critical edge from the loop predecessor to the loop header.
810   if (BB->pred_size() == 1) {
811     MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
812     SmallVector<MachineOperand, 4> Cond;
813     if (!TII->analyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
814       InitRegPressure(*BB->pred_begin());
815   }
816 
817   for (const MachineInstr &MI : *BB)
818     UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
819 }
820 
821 /// Update estimate of register pressure after the specified instruction.
822 void MachineLICMBase::UpdateRegPressure(const MachineInstr *MI,
823                                         bool ConsiderUnseenAsDef) {
824   auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
825   for (const auto &RPIdAndCost : Cost) {
826     unsigned Class = RPIdAndCost.first;
827     if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second)
828       RegPressure[Class] = 0;
829     else
830       RegPressure[Class] += RPIdAndCost.second;
831   }
832 }
833 
834 /// Calculate the additional register pressure that the registers used in MI
835 /// cause.
836 ///
837 /// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
838 /// figure out which usages are live-ins.
839 /// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
840 DenseMap<unsigned, int>
841 MachineLICMBase::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
842                                   bool ConsiderUnseenAsDef) {
843   DenseMap<unsigned, int> Cost;
844   if (MI->isImplicitDef())
845     return Cost;
846   for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
847     const MachineOperand &MO = MI->getOperand(i);
848     if (!MO.isReg() || MO.isImplicit())
849       continue;
850     Register Reg = MO.getReg();
851     if (!Register::isVirtualRegister(Reg))
852       continue;
853 
854     // FIXME: It seems bad to use RegSeen only for some of these calculations.
855     bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
856     const TargetRegisterClass *RC = MRI->getRegClass(Reg);
857 
858     RegClassWeight W = TRI->getRegClassWeight(RC);
859     int RCCost = 0;
860     if (MO.isDef())
861       RCCost = W.RegWeight;
862     else {
863       bool isKill = isOperandKill(MO, MRI);
864       if (isNew && !isKill && ConsiderUnseenAsDef)
865         // Haven't seen this, it must be a livein.
866         RCCost = W.RegWeight;
867       else if (!isNew && isKill)
868         RCCost = -W.RegWeight;
869     }
870     if (RCCost == 0)
871       continue;
872     const int *PS = TRI->getRegClassPressureSets(RC);
873     for (; *PS != -1; ++PS) {
874       if (Cost.find(*PS) == Cost.end())
875         Cost[*PS] = RCCost;
876       else
877         Cost[*PS] += RCCost;
878     }
879   }
880   return Cost;
881 }
882 
883 /// Return true if this machine instruction loads from global offset table or
884 /// constant pool.
885 static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
886   assert(MI.mayLoad() && "Expected MI that loads!");
887 
888   // If we lost memory operands, conservatively assume that the instruction
889   // reads from everything..
890   if (MI.memoperands_empty())
891     return true;
892 
893   for (MachineMemOperand *MemOp : MI.memoperands())
894     if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
895       if (PSV->isGOT() || PSV->isConstantPool())
896         return true;
897 
898   return false;
899 }
900 
901 // This function iterates through all the operands of the input store MI and
902 // checks that each register operand statisfies isCallerPreservedPhysReg.
903 // This means, the value being stored and the address where it is being stored
904 // is constant throughout the body of the function (not including prologue and
905 // epilogue). When called with an MI that isn't a store, it returns false.
906 // A future improvement can be to check if the store registers are constant
907 // throughout the loop rather than throughout the funtion.
908 static bool isInvariantStore(const MachineInstr &MI,
909                              const TargetRegisterInfo *TRI,
910                              const MachineRegisterInfo *MRI) {
911 
912   bool FoundCallerPresReg = false;
913   if (!MI.mayStore() || MI.hasUnmodeledSideEffects() ||
914       (MI.getNumOperands() == 0))
915     return false;
916 
917   // Check that all register operands are caller-preserved physical registers.
918   for (const MachineOperand &MO : MI.operands()) {
919     if (MO.isReg()) {
920       Register Reg = MO.getReg();
921       // If operand is a virtual register, check if it comes from a copy of a
922       // physical register.
923       if (Register::isVirtualRegister(Reg))
924         Reg = TRI->lookThruCopyLike(MO.getReg(), MRI);
925       if (Register::isVirtualRegister(Reg))
926         return false;
927       if (!TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *MI.getMF()))
928         return false;
929       else
930         FoundCallerPresReg = true;
931     } else if (!MO.isImm()) {
932         return false;
933     }
934   }
935   return FoundCallerPresReg;
936 }
937 
938 // Return true if the input MI is a copy instruction that feeds an invariant
939 // store instruction. This means that the src of the copy has to satisfy
940 // isCallerPreservedPhysReg and atleast one of it's users should satisfy
941 // isInvariantStore.
942 static bool isCopyFeedingInvariantStore(const MachineInstr &MI,
943                                         const MachineRegisterInfo *MRI,
944                                         const TargetRegisterInfo *TRI) {
945 
946   // FIXME: If targets would like to look through instructions that aren't
947   // pure copies, this can be updated to a query.
948   if (!MI.isCopy())
949     return false;
950 
951   const MachineFunction *MF = MI.getMF();
952   // Check that we are copying a constant physical register.
953   Register CopySrcReg = MI.getOperand(1).getReg();
954   if (Register::isVirtualRegister(CopySrcReg))
955     return false;
956 
957   if (!TRI->isCallerPreservedPhysReg(CopySrcReg.asMCReg(), *MF))
958     return false;
959 
960   Register CopyDstReg = MI.getOperand(0).getReg();
961   // Check if any of the uses of the copy are invariant stores.
962   assert(Register::isVirtualRegister(CopyDstReg) &&
963          "copy dst is not a virtual reg");
964 
965   for (MachineInstr &UseMI : MRI->use_instructions(CopyDstReg)) {
966     if (UseMI.mayStore() && isInvariantStore(UseMI, TRI, MRI))
967       return true;
968   }
969   return false;
970 }
971 
972 /// Returns true if the instruction may be a suitable candidate for LICM.
973 /// e.g. If the instruction is a call, then it's obviously not safe to hoist it.
974 bool MachineLICMBase::IsLICMCandidate(MachineInstr &I) {
975   // Check if it's safe to move the instruction.
976   bool DontMoveAcrossStore = true;
977   if ((!I.isSafeToMove(AA, DontMoveAcrossStore)) &&
978       !(HoistConstStores && isInvariantStore(I, TRI, MRI))) {
979     LLVM_DEBUG(dbgs() << "LICM: Instruction not safe to move.\n");
980     return false;
981   }
982 
983   // If it is a load then check if it is guaranteed to execute by making sure
984   // that it dominates all exiting blocks. If it doesn't, then there is a path
985   // out of the loop which does not execute this load, so we can't hoist it.
986   // Loads from constant memory are safe to speculate, for example indexed load
987   // from a jump table.
988   // Stores and side effects are already checked by isSafeToMove.
989   if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
990       !IsGuaranteedToExecute(I.getParent())) {
991     LLVM_DEBUG(dbgs() << "LICM: Load not guaranteed to execute.\n");
992     return false;
993   }
994 
995   // Convergent attribute has been used on operations that involve inter-thread
996   // communication which results are implicitly affected by the enclosing
997   // control flows. It is not safe to hoist or sink such operations across
998   // control flow.
999   if (I.isConvergent())
1000     return false;
1001 
1002   if (!TII->shouldHoist(I, CurLoop))
1003     return false;
1004 
1005   return true;
1006 }
1007 
1008 /// Returns true if the instruction is loop invariant.
1009 bool MachineLICMBase::IsLoopInvariantInst(MachineInstr &I) {
1010   if (!IsLICMCandidate(I)) {
1011     LLVM_DEBUG(dbgs() << "LICM: Instruction not a LICM candidate\n");
1012     return false;
1013   }
1014   return CurLoop->isLoopInvariant(I);
1015 }
1016 
1017 /// Return true if the specified instruction is used by a phi node and hoisting
1018 /// it could cause a copy to be inserted.
1019 bool MachineLICMBase::HasLoopPHIUse(const MachineInstr *MI) const {
1020   SmallVector<const MachineInstr*, 8> Work(1, MI);
1021   do {
1022     MI = Work.pop_back_val();
1023     for (const MachineOperand &MO : MI->operands()) {
1024       if (!MO.isReg() || !MO.isDef())
1025         continue;
1026       Register Reg = MO.getReg();
1027       if (!Register::isVirtualRegister(Reg))
1028         continue;
1029       for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
1030         // A PHI may cause a copy to be inserted.
1031         if (UseMI.isPHI()) {
1032           // A PHI inside the loop causes a copy because the live range of Reg is
1033           // extended across the PHI.
1034           if (CurLoop->contains(&UseMI))
1035             return true;
1036           // A PHI in an exit block can cause a copy to be inserted if the PHI
1037           // has multiple predecessors in the loop with different values.
1038           // For now, approximate by rejecting all exit blocks.
1039           if (isExitBlock(UseMI.getParent()))
1040             return true;
1041           continue;
1042         }
1043         // Look past copies as well.
1044         if (UseMI.isCopy() && CurLoop->contains(&UseMI))
1045           Work.push_back(&UseMI);
1046       }
1047     }
1048   } while (!Work.empty());
1049   return false;
1050 }
1051 
1052 /// Compute operand latency between a def of 'Reg' and an use in the current
1053 /// loop, return true if the target considered it high.
1054 bool MachineLICMBase::HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
1055                                             Register Reg) const {
1056   if (MRI->use_nodbg_empty(Reg))
1057     return false;
1058 
1059   for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
1060     if (UseMI.isCopyLike())
1061       continue;
1062     if (!CurLoop->contains(UseMI.getParent()))
1063       continue;
1064     for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
1065       const MachineOperand &MO = UseMI.getOperand(i);
1066       if (!MO.isReg() || !MO.isUse())
1067         continue;
1068       Register MOReg = MO.getReg();
1069       if (MOReg != Reg)
1070         continue;
1071 
1072       if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i))
1073         return true;
1074     }
1075 
1076     // Only look at the first in loop use.
1077     break;
1078   }
1079 
1080   return false;
1081 }
1082 
1083 /// Return true if the instruction is marked "cheap" or the operand latency
1084 /// between its def and a use is one or less.
1085 bool MachineLICMBase::IsCheapInstruction(MachineInstr &MI) const {
1086   if (TII->isAsCheapAsAMove(MI) || MI.isCopyLike())
1087     return true;
1088 
1089   bool isCheap = false;
1090   unsigned NumDefs = MI.getDesc().getNumDefs();
1091   for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
1092     MachineOperand &DefMO = MI.getOperand(i);
1093     if (!DefMO.isReg() || !DefMO.isDef())
1094       continue;
1095     --NumDefs;
1096     Register Reg = DefMO.getReg();
1097     if (Register::isPhysicalRegister(Reg))
1098       continue;
1099 
1100     if (!TII->hasLowDefLatency(SchedModel, MI, i))
1101       return false;
1102     isCheap = true;
1103   }
1104 
1105   return isCheap;
1106 }
1107 
1108 /// Visit BBs from header to current BB, check if hoisting an instruction of the
1109 /// given cost matrix can cause high register pressure.
1110 bool
1111 MachineLICMBase::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost,
1112                                          bool CheapInstr) {
1113   for (const auto &RPIdAndCost : Cost) {
1114     if (RPIdAndCost.second <= 0)
1115       continue;
1116 
1117     unsigned Class = RPIdAndCost.first;
1118     int Limit = RegLimit[Class];
1119 
1120     // Don't hoist cheap instructions if they would increase register pressure,
1121     // even if we're under the limit.
1122     if (CheapInstr && !HoistCheapInsts)
1123       return true;
1124 
1125     for (const auto &RP : BackTrace)
1126       if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit)
1127         return true;
1128   }
1129 
1130   return false;
1131 }
1132 
1133 /// Traverse the back trace from header to the current block and update their
1134 /// register pressures to reflect the effect of hoisting MI from the current
1135 /// block to the preheader.
1136 void MachineLICMBase::UpdateBackTraceRegPressure(const MachineInstr *MI) {
1137   // First compute the 'cost' of the instruction, i.e. its contribution
1138   // to register pressure.
1139   auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
1140                                /*ConsiderUnseenAsDef=*/false);
1141 
1142   // Update register pressure of blocks from loop header to current block.
1143   for (auto &RP : BackTrace)
1144     for (const auto &RPIdAndCost : Cost)
1145       RP[RPIdAndCost.first] += RPIdAndCost.second;
1146 }
1147 
1148 /// Return true if it is potentially profitable to hoist the given loop
1149 /// invariant.
1150 bool MachineLICMBase::IsProfitableToHoist(MachineInstr &MI) {
1151   if (MI.isImplicitDef())
1152     return true;
1153 
1154   // Besides removing computation from the loop, hoisting an instruction has
1155   // these effects:
1156   //
1157   // - The value defined by the instruction becomes live across the entire
1158   //   loop. This increases register pressure in the loop.
1159   //
1160   // - If the value is used by a PHI in the loop, a copy will be required for
1161   //   lowering the PHI after extending the live range.
1162   //
1163   // - When hoisting the last use of a value in the loop, that value no longer
1164   //   needs to be live in the loop. This lowers register pressure in the loop.
1165 
1166   if (HoistConstStores &&  isCopyFeedingInvariantStore(MI, MRI, TRI))
1167     return true;
1168 
1169   bool CheapInstr = IsCheapInstruction(MI);
1170   bool CreatesCopy = HasLoopPHIUse(&MI);
1171 
1172   // Don't hoist a cheap instruction if it would create a copy in the loop.
1173   if (CheapInstr && CreatesCopy) {
1174     LLVM_DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
1175     return false;
1176   }
1177 
1178   // Rematerializable instructions should always be hoisted providing the
1179   // register allocator can just pull them down again when needed.
1180   if (isTriviallyReMaterializable(MI, AA))
1181     return true;
1182 
1183   // FIXME: If there are long latency loop-invariant instructions inside the
1184   // loop at this point, why didn't the optimizer's LICM hoist them?
1185   for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1186     const MachineOperand &MO = MI.getOperand(i);
1187     if (!MO.isReg() || MO.isImplicit())
1188       continue;
1189     Register Reg = MO.getReg();
1190     if (!Register::isVirtualRegister(Reg))
1191       continue;
1192     if (MO.isDef() && HasHighOperandLatency(MI, i, Reg)) {
1193       LLVM_DEBUG(dbgs() << "Hoist High Latency: " << MI);
1194       ++NumHighLatency;
1195       return true;
1196     }
1197   }
1198 
1199   // Estimate register pressure to determine whether to LICM the instruction.
1200   // In low register pressure situation, we can be more aggressive about
1201   // hoisting. Also, favors hoisting long latency instructions even in
1202   // moderately high pressure situation.
1203   // Cheap instructions will only be hoisted if they don't increase register
1204   // pressure at all.
1205   auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
1206                                /*ConsiderUnseenAsDef=*/false);
1207 
1208   // Visit BBs from header to current BB, if hoisting this doesn't cause
1209   // high register pressure, then it's safe to proceed.
1210   if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
1211     LLVM_DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
1212     ++NumLowRP;
1213     return true;
1214   }
1215 
1216   // Don't risk increasing register pressure if it would create copies.
1217   if (CreatesCopy) {
1218     LLVM_DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
1219     return false;
1220   }
1221 
1222   // Do not "speculate" in high register pressure situation. If an
1223   // instruction is not guaranteed to be executed in the loop, it's best to be
1224   // conservative.
1225   if (AvoidSpeculation &&
1226       (!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) {
1227     LLVM_DEBUG(dbgs() << "Won't speculate: " << MI);
1228     return false;
1229   }
1230 
1231   // High register pressure situation, only hoist if the instruction is going
1232   // to be remat'ed.
1233   if (!isTriviallyReMaterializable(MI, AA) &&
1234       !MI.isDereferenceableInvariantLoad(AA)) {
1235     LLVM_DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
1236     return false;
1237   }
1238 
1239   return true;
1240 }
1241 
1242 /// Unfold a load from the given machineinstr if the load itself could be
1243 /// hoisted. Return the unfolded and hoistable load, or null if the load
1244 /// couldn't be unfolded or if it wouldn't be hoistable.
1245 MachineInstr *MachineLICMBase::ExtractHoistableLoad(MachineInstr *MI) {
1246   // Don't unfold simple loads.
1247   if (MI->canFoldAsLoad())
1248     return nullptr;
1249 
1250   // If not, we may be able to unfold a load and hoist that.
1251   // First test whether the instruction is loading from an amenable
1252   // memory location.
1253   if (!MI->isDereferenceableInvariantLoad(AA))
1254     return nullptr;
1255 
1256   // Next determine the register class for a temporary register.
1257   unsigned LoadRegIndex;
1258   unsigned NewOpc =
1259     TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1260                                     /*UnfoldLoad=*/true,
1261                                     /*UnfoldStore=*/false,
1262                                     &LoadRegIndex);
1263   if (NewOpc == 0) return nullptr;
1264   const MCInstrDesc &MID = TII->get(NewOpc);
1265   MachineFunction &MF = *MI->getMF();
1266   const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI, MF);
1267   // Ok, we're unfolding. Create a temporary register and do the unfold.
1268   Register Reg = MRI->createVirtualRegister(RC);
1269 
1270   SmallVector<MachineInstr *, 2> NewMIs;
1271   bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg,
1272                                           /*UnfoldLoad=*/true,
1273                                           /*UnfoldStore=*/false, NewMIs);
1274   (void)Success;
1275   assert(Success &&
1276          "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1277          "succeeded!");
1278   assert(NewMIs.size() == 2 &&
1279          "Unfolded a load into multiple instructions!");
1280   MachineBasicBlock *MBB = MI->getParent();
1281   MachineBasicBlock::iterator Pos = MI;
1282   MBB->insert(Pos, NewMIs[0]);
1283   MBB->insert(Pos, NewMIs[1]);
1284   // If unfolding produced a load that wasn't loop-invariant or profitable to
1285   // hoist, discard the new instructions and bail.
1286   if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
1287     NewMIs[0]->eraseFromParent();
1288     NewMIs[1]->eraseFromParent();
1289     return nullptr;
1290   }
1291 
1292   // Update register pressure for the unfolded instruction.
1293   UpdateRegPressure(NewMIs[1]);
1294 
1295   // Otherwise we successfully unfolded a load that we can hoist.
1296 
1297   // Update the call site info.
1298   if (MI->shouldUpdateCallSiteInfo())
1299     MF.eraseCallSiteInfo(MI);
1300 
1301   MI->eraseFromParent();
1302   return NewMIs[0];
1303 }
1304 
1305 /// Initialize the CSE map with instructions that are in the current loop
1306 /// preheader that may become duplicates of instructions that are hoisted
1307 /// out of the loop.
1308 void MachineLICMBase::InitCSEMap(MachineBasicBlock *BB) {
1309   for (MachineInstr &MI : *BB)
1310     CSEMap[MI.getOpcode()].push_back(&MI);
1311 }
1312 
1313 /// Find an instruction amount PrevMIs that is a duplicate of MI.
1314 /// Return this instruction if it's found.
1315 MachineInstr *
1316 MachineLICMBase::LookForDuplicate(const MachineInstr *MI,
1317                                   std::vector<MachineInstr *> &PrevMIs) {
1318   for (MachineInstr *PrevMI : PrevMIs)
1319     if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr)))
1320       return PrevMI;
1321 
1322   return nullptr;
1323 }
1324 
1325 /// Given a LICM'ed instruction, look for an instruction on the preheader that
1326 /// computes the same value. If it's found, do a RAU on with the definition of
1327 /// the existing instruction rather than hoisting the instruction to the
1328 /// preheader.
1329 bool MachineLICMBase::EliminateCSE(
1330     MachineInstr *MI,
1331     DenseMap<unsigned, std::vector<MachineInstr *>>::iterator &CI) {
1332   // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1333   // the undef property onto uses.
1334   if (CI == CSEMap.end() || MI->isImplicitDef())
1335     return false;
1336 
1337   if (MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
1338     LLVM_DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
1339 
1340     // Replace virtual registers defined by MI by their counterparts defined
1341     // by Dup.
1342     SmallVector<unsigned, 2> Defs;
1343     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1344       const MachineOperand &MO = MI->getOperand(i);
1345 
1346       // Physical registers may not differ here.
1347       assert((!MO.isReg() || MO.getReg() == 0 ||
1348               !Register::isPhysicalRegister(MO.getReg()) ||
1349               MO.getReg() == Dup->getOperand(i).getReg()) &&
1350              "Instructions with different phys regs are not identical!");
1351 
1352       if (MO.isReg() && MO.isDef() &&
1353           !Register::isPhysicalRegister(MO.getReg()))
1354         Defs.push_back(i);
1355     }
1356 
1357     SmallVector<const TargetRegisterClass*, 2> OrigRCs;
1358     for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1359       unsigned Idx = Defs[i];
1360       Register Reg = MI->getOperand(Idx).getReg();
1361       Register DupReg = Dup->getOperand(Idx).getReg();
1362       OrigRCs.push_back(MRI->getRegClass(DupReg));
1363 
1364       if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
1365         // Restore old RCs if more than one defs.
1366         for (unsigned j = 0; j != i; ++j)
1367           MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
1368         return false;
1369       }
1370     }
1371 
1372     for (unsigned Idx : Defs) {
1373       Register Reg = MI->getOperand(Idx).getReg();
1374       Register DupReg = Dup->getOperand(Idx).getReg();
1375       MRI->replaceRegWith(Reg, DupReg);
1376       MRI->clearKillFlags(DupReg);
1377       // Clear Dup dead flag if any, we reuse it for Reg.
1378       if (!MRI->use_nodbg_empty(DupReg))
1379         Dup->getOperand(Idx).setIsDead(false);
1380     }
1381 
1382     MI->eraseFromParent();
1383     ++NumCSEed;
1384     return true;
1385   }
1386   return false;
1387 }
1388 
1389 /// Return true if the given instruction will be CSE'd if it's hoisted out of
1390 /// the loop.
1391 bool MachineLICMBase::MayCSE(MachineInstr *MI) {
1392   unsigned Opcode = MI->getOpcode();
1393   DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1394       CSEMap.find(Opcode);
1395   // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1396   // the undef property onto uses.
1397   if (CI == CSEMap.end() || MI->isImplicitDef())
1398     return false;
1399 
1400   return LookForDuplicate(MI, CI->second) != nullptr;
1401 }
1402 
1403 /// When an instruction is found to use only loop invariant operands
1404 /// that are safe to hoist, this instruction is called to do the dirty work.
1405 /// It returns true if the instruction is hoisted.
1406 bool MachineLICMBase::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
1407   MachineBasicBlock *SrcBlock = MI->getParent();
1408 
1409   // Disable the instruction hoisting due to block hotness
1410   if ((DisableHoistingToHotterBlocks == UseBFI::All ||
1411       (DisableHoistingToHotterBlocks == UseBFI::PGO && HasProfileData)) &&
1412       isTgtHotterThanSrc(SrcBlock, Preheader)) {
1413     ++NumNotHoistedDueToHotness;
1414     return false;
1415   }
1416   // First check whether we should hoist this instruction.
1417   if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
1418     // If not, try unfolding a hoistable load.
1419     MI = ExtractHoistableLoad(MI);
1420     if (!MI) return false;
1421   }
1422 
1423   // If we have hoisted an instruction that may store, it can only be a constant
1424   // store.
1425   if (MI->mayStore())
1426     NumStoreConst++;
1427 
1428   // Now move the instructions to the predecessor, inserting it before any
1429   // terminator instructions.
1430   LLVM_DEBUG({
1431     dbgs() << "Hoisting " << *MI;
1432     if (MI->getParent()->getBasicBlock())
1433       dbgs() << " from " << printMBBReference(*MI->getParent());
1434     if (Preheader->getBasicBlock())
1435       dbgs() << " to " << printMBBReference(*Preheader);
1436     dbgs() << "\n";
1437   });
1438 
1439   // If this is the first instruction being hoisted to the preheader,
1440   // initialize the CSE map with potential common expressions.
1441   if (FirstInLoop) {
1442     InitCSEMap(Preheader);
1443     FirstInLoop = false;
1444   }
1445 
1446   // Look for opportunity to CSE the hoisted instruction.
1447   unsigned Opcode = MI->getOpcode();
1448   DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1449       CSEMap.find(Opcode);
1450   if (!EliminateCSE(MI, CI)) {
1451     // Otherwise, splice the instruction to the preheader.
1452     Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
1453 
1454     // Since we are moving the instruction out of its basic block, we do not
1455     // retain its debug location. Doing so would degrade the debugging
1456     // experience and adversely affect the accuracy of profiling information.
1457     assert(!MI->isDebugInstr() && "Should not hoist debug inst");
1458     MI->setDebugLoc(DebugLoc());
1459 
1460     // Update register pressure for BBs from header to this block.
1461     UpdateBackTraceRegPressure(MI);
1462 
1463     // Clear the kill flags of any register this instruction defines,
1464     // since they may need to be live throughout the entire loop
1465     // rather than just live for part of it.
1466     for (MachineOperand &MO : MI->operands())
1467       if (MO.isReg() && MO.isDef() && !MO.isDead())
1468         MRI->clearKillFlags(MO.getReg());
1469 
1470     // Add to the CSE map.
1471     if (CI != CSEMap.end())
1472       CI->second.push_back(MI);
1473     else
1474       CSEMap[Opcode].push_back(MI);
1475   }
1476 
1477   ++NumHoisted;
1478   Changed = true;
1479 
1480   return true;
1481 }
1482 
1483 /// Get the preheader for the current loop, splitting a critical edge if needed.
1484 MachineBasicBlock *MachineLICMBase::getCurPreheader() {
1485   // Determine the block to which to hoist instructions. If we can't find a
1486   // suitable loop predecessor, we can't do any hoisting.
1487 
1488   // If we've tried to get a preheader and failed, don't try again.
1489   if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1))
1490     return nullptr;
1491 
1492   if (!CurPreheader) {
1493     CurPreheader = CurLoop->getLoopPreheader();
1494     if (!CurPreheader) {
1495       MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
1496       if (!Pred) {
1497         CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1498         return nullptr;
1499       }
1500 
1501       CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), *this);
1502       if (!CurPreheader) {
1503         CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1504         return nullptr;
1505       }
1506     }
1507   }
1508   return CurPreheader;
1509 }
1510 
1511 /// Is the target basic block at least "BlockFrequencyRatioThreshold"
1512 /// times hotter than the source basic block.
1513 bool MachineLICMBase::isTgtHotterThanSrc(MachineBasicBlock *SrcBlock,
1514                                          MachineBasicBlock *TgtBlock) {
1515   // Parse source and target basic block frequency from MBFI
1516   uint64_t SrcBF = MBFI->getBlockFreq(SrcBlock).getFrequency();
1517   uint64_t DstBF = MBFI->getBlockFreq(TgtBlock).getFrequency();
1518 
1519   // Disable the hoisting if source block frequency is zero
1520   if (!SrcBF)
1521     return true;
1522 
1523   double Ratio = (double)DstBF / SrcBF;
1524 
1525   // Compare the block frequency ratio with the threshold
1526   return Ratio > BlockFrequencyRatioThreshold;
1527 }
1528