1 //===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs loop invariant code motion on machine instructions. We
11 // attempt to remove as much code from the body of a loop as possible.
13 // This pass does not attempt to throttle itself to limit register pressure.
14 // The register allocation phases are expected to perform rematerialization
15 // to recover when register pressure is high.
17 // This pass is not intended to be a replacement or a complete alternative
18 // for the LLVM-IR-level LICM pass. It is only designed to hoist simple
19 // constructs that are not exposed before lowering and instruction selection.
21 //===----------------------------------------------------------------------===//
23 #define DEBUG_TYPE "machine-licm"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/CodeGen/MachineDominators.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineLoopInfo.h"
28 #include "llvm/CodeGen/MachineMemOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/PseudoSourceValue.h"
31 #include "llvm/MC/MCInstrItineraries.h"
32 #include "llvm/Target/TargetLowering.h"
33 #include "llvm/Target/TargetRegisterInfo.h"
34 #include "llvm/Target/TargetInstrInfo.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Analysis/AliasAnalysis.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/SmallSet.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
45 "Number of machine instructions hoisted out of loops");
47 "Number of instructions hoisted in low reg pressure situation");
48 STATISTIC(NumHighLatency,
49 "Number of high latency instructions hoisted");
51 "Number of hoisted machine instructions CSEed");
52 STATISTIC(NumPostRAHoisted,
53 "Number of machine instructions hoisted out of loops post regalloc");
56 class MachineLICM : public MachineFunctionPass {
59 const TargetMachine *TM;
60 const TargetInstrInfo *TII;
61 const TargetLowering *TLI;
62 const TargetRegisterInfo *TRI;
63 const MachineFrameInfo *MFI;
64 MachineRegisterInfo *MRI;
65 const InstrItineraryData *InstrItins;
67 // Various analyses that we use...
68 AliasAnalysis *AA; // Alias analysis info.
69 MachineLoopInfo *MLI; // Current MachineLoopInfo
70 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
72 // State that is updated as we process loops
73 bool Changed; // True if a loop is changed.
74 bool FirstInLoop; // True if it's the first LICM in the loop.
75 MachineLoop *CurLoop; // The current loop we are working on.
76 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
78 BitVector AllocatableSet;
80 // Track 'estimated' register pressure.
81 SmallSet<unsigned, 32> RegSeen;
82 SmallVector<unsigned, 8> RegPressure;
84 // Register pressure "limit" per register class. If the pressure
85 // is higher than the limit, then it's considered high.
86 SmallVector<unsigned, 8> RegLimit;
88 // Register pressure on path leading from loop preheader to current BB.
89 SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
91 // For each opcode, keep a list of potential CSE instructions.
92 DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap;
100 // If a MBB does not dominate loop exiting blocks then it may not safe
101 // to hoist loads from this block.
102 // Tri-state: 0 - false, 1 - true, 2 - unknown
103 unsigned SpeculationState;
106 static char ID; // Pass identification, replacement for typeid
108 MachineFunctionPass(ID), PreRegAlloc(true) {
109 initializeMachineLICMPass(*PassRegistry::getPassRegistry());
112 explicit MachineLICM(bool PreRA) :
113 MachineFunctionPass(ID), PreRegAlloc(PreRA) {
114 initializeMachineLICMPass(*PassRegistry::getPassRegistry());
117 virtual bool runOnMachineFunction(MachineFunction &MF);
119 const char *getPassName() const { return "Machine Instruction LICM"; }
121 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
122 AU.addRequired<MachineLoopInfo>();
123 AU.addRequired<MachineDominatorTree>();
124 AU.addRequired<AliasAnalysis>();
125 AU.addPreserved<MachineLoopInfo>();
126 AU.addPreserved<MachineDominatorTree>();
127 MachineFunctionPass::getAnalysisUsage(AU);
130 virtual void releaseMemory() {
135 for (DenseMap<unsigned,std::vector<const MachineInstr*> >::iterator
136 CI = CSEMap.begin(), CE = CSEMap.end(); CI != CE; ++CI)
142 /// CandidateInfo - Keep track of information about hoisting candidates.
143 struct CandidateInfo {
147 CandidateInfo(MachineInstr *mi, unsigned def, int fi)
148 : MI(mi), Def(def), FI(fi) {}
151 /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
152 /// invariants out to the preheader.
153 void HoistRegionPostRA();
155 /// HoistPostRA - When an instruction is found to only use loop invariant
156 /// operands that is safe to hoist, this instruction is called to do the
158 void HoistPostRA(MachineInstr *MI, unsigned Def);
160 /// ProcessMI - Examine the instruction for potentai LICM candidate. Also
161 /// gather register def and frame object update information.
162 void ProcessMI(MachineInstr *MI, unsigned *PhysRegDefs,
163 SmallSet<int, 32> &StoredFIs,
164 SmallVector<CandidateInfo, 32> &Candidates);
166 /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the
168 void AddToLiveIns(unsigned Reg);
170 /// IsLICMCandidate - Returns true if the instruction may be a suitable
171 /// candidate for LICM. e.g. If the instruction is a call, then it's
172 /// obviously not safe to hoist it.
173 bool IsLICMCandidate(MachineInstr &I);
175 /// IsLoopInvariantInst - Returns true if the instruction is loop
176 /// invariant. I.e., all virtual register operands are defined outside of
177 /// the loop, physical registers aren't accessed (explicitly or implicitly),
178 /// and the instruction is hoistable.
180 bool IsLoopInvariantInst(MachineInstr &I);
182 /// HasAnyPHIUse - Return true if the specified register is used by any
184 bool HasAnyPHIUse(unsigned Reg) const;
186 /// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
187 /// and an use in the current loop, return true if the target considered
189 bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
192 bool IsCheapInstruction(MachineInstr &MI) const;
194 /// CanCauseHighRegPressure - Visit BBs from header to current BB,
195 /// check if hoisting an instruction of the given cost matrix can cause high
196 /// register pressure.
197 bool CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost);
199 /// UpdateBackTraceRegPressure - Traverse the back trace from header to
200 /// the current block and update their register pressures to reflect the
201 /// effect of hoisting MI from the current block to the preheader.
202 void UpdateBackTraceRegPressure(const MachineInstr *MI);
204 /// IsProfitableToHoist - Return true if it is potentially profitable to
205 /// hoist the given loop invariant.
206 bool IsProfitableToHoist(MachineInstr &MI);
208 /// IsGuaranteedToExecute - Check if this mbb is guaranteed to execute.
209 /// If not then a load from this mbb may not be safe to hoist.
210 bool IsGuaranteedToExecute(MachineBasicBlock *BB);
212 /// HoistRegion - Walk the specified region of the CFG (defined by all
213 /// blocks dominated by the specified block, and that are in the current
214 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
215 /// visit definitions before uses, allowing us to hoist a loop body in one
216 /// pass without iteration.
218 void HoistRegion(MachineDomTreeNode *N, bool IsHeader = false);
220 /// getRegisterClassIDAndCost - For a given MI, register, and the operand
221 /// index, return the ID and cost of its representative register class by
223 void getRegisterClassIDAndCost(const MachineInstr *MI,
224 unsigned Reg, unsigned OpIdx,
225 unsigned &RCId, unsigned &RCCost) const;
227 /// InitRegPressure - Find all virtual register references that are liveout
228 /// of the preheader to initialize the starting "register pressure". Note
229 /// this does not count live through (livein but not used) registers.
230 void InitRegPressure(MachineBasicBlock *BB);
232 /// UpdateRegPressure - Update estimate of register pressure after the
233 /// specified instruction.
234 void UpdateRegPressure(const MachineInstr *MI);
236 /// ExtractHoistableLoad - Unfold a load from the given machineinstr if
237 /// the load itself could be hoisted. Return the unfolded and hoistable
238 /// load, or null if the load couldn't be unfolded or if it wouldn't
240 MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
242 /// LookForDuplicate - Find an instruction amount PrevMIs that is a
243 /// duplicate of MI. Return this instruction if it's found.
244 const MachineInstr *LookForDuplicate(const MachineInstr *MI,
245 std::vector<const MachineInstr*> &PrevMIs);
247 /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on
248 /// the preheader that compute the same value. If it's found, do a RAU on
249 /// with the definition of the existing instruction rather than hoisting
250 /// the instruction to the preheader.
251 bool EliminateCSE(MachineInstr *MI,
252 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI);
254 /// MayCSE - Return true if the given instruction will be CSE'd if it's
255 /// hoisted out of the loop.
256 bool MayCSE(MachineInstr *MI);
258 /// Hoist - When an instruction is found to only use loop invariant operands
259 /// that is safe to hoist, this instruction is called to do the dirty work.
260 /// It returns true if the instruction is hoisted.
261 bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
263 /// InitCSEMap - Initialize the CSE map with instructions that are in the
264 /// current loop preheader that may become duplicates of instructions that
265 /// are hoisted out of the loop.
266 void InitCSEMap(MachineBasicBlock *BB);
268 /// getCurPreheader - Get the preheader for the current loop, splitting
269 /// a critical edge if needed.
270 MachineBasicBlock *getCurPreheader();
272 } // end anonymous namespace
274 char MachineLICM::ID = 0;
275 INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm",
276 "Machine Loop Invariant Code Motion", false, false)
277 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
278 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
279 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
280 INITIALIZE_PASS_END(MachineLICM, "machinelicm",
281 "Machine Loop Invariant Code Motion", false, false)
283 FunctionPass *llvm::createMachineLICMPass(bool PreRegAlloc) {
284 return new MachineLICM(PreRegAlloc);
287 /// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most
288 /// loop that has a unique predecessor.
289 static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
290 // Check whether this loop even has a unique predecessor.
291 if (!CurLoop->getLoopPredecessor())
293 // Ok, now check to see if any of its outer loops do.
294 for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
295 if (L->getLoopPredecessor())
297 // None of them did, so this is the outermost with a unique predecessor.
301 bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
303 DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
305 DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
306 DEBUG(dbgs() << MF.getFunction()->getName() << " ********\n");
308 Changed = FirstInLoop = false;
309 TM = &MF.getTarget();
310 TII = TM->getInstrInfo();
311 TLI = TM->getTargetLowering();
312 TRI = TM->getRegisterInfo();
313 MFI = MF.getFrameInfo();
314 MRI = &MF.getRegInfo();
315 InstrItins = TM->getInstrItineraryData();
316 AllocatableSet = TRI->getAllocatableSet(MF);
319 // Estimate register pressure during pre-regalloc pass.
320 unsigned NumRC = TRI->getNumRegClasses();
321 RegPressure.resize(NumRC);
322 std::fill(RegPressure.begin(), RegPressure.end(), 0);
323 RegLimit.resize(NumRC);
324 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
325 E = TRI->regclass_end(); I != E; ++I)
326 RegLimit[(*I)->getID()] = TRI->getRegPressureLimit(*I, MF);
329 // Get our Loop information...
330 MLI = &getAnalysis<MachineLoopInfo>();
331 DT = &getAnalysis<MachineDominatorTree>();
332 AA = &getAnalysis<AliasAnalysis>();
334 SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
335 while (!Worklist.empty()) {
336 CurLoop = Worklist.pop_back_val();
339 // If this is done before regalloc, only visit outer-most preheader-sporting
341 if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
342 Worklist.append(CurLoop->begin(), CurLoop->end());
349 // CSEMap is initialized for loop header when the first instruction is
351 MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
353 HoistRegion(N, true);
361 /// InstructionStoresToFI - Return true if instruction stores to the
363 static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
364 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
365 oe = MI->memoperands_end(); o != oe; ++o) {
366 if (!(*o)->isStore() || !(*o)->getValue())
368 if (const FixedStackPseudoSourceValue *Value =
369 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
370 if (Value->getFrameIndex() == FI)
377 /// ProcessMI - Examine the instruction for potentai LICM candidate. Also
378 /// gather register def and frame object update information.
379 void MachineLICM::ProcessMI(MachineInstr *MI,
380 unsigned *PhysRegDefs,
381 SmallSet<int, 32> &StoredFIs,
382 SmallVector<CandidateInfo, 32> &Candidates) {
383 bool RuledOut = false;
384 bool HasNonInvariantUse = false;
386 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
387 const MachineOperand &MO = MI->getOperand(i);
389 // Remember if the instruction stores to the frame index.
390 int FI = MO.getIndex();
391 if (!StoredFIs.count(FI) &&
392 MFI->isSpillSlotObjectIndex(FI) &&
393 InstructionStoresToFI(MI, FI))
394 StoredFIs.insert(FI);
395 HasNonInvariantUse = true;
401 unsigned Reg = MO.getReg();
404 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
405 "Not expecting virtual register!");
408 if (Reg && PhysRegDefs[Reg])
409 // If it's using a non-loop-invariant register, then it's obviously not
411 HasNonInvariantUse = true;
415 if (MO.isImplicit()) {
417 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
420 // Non-dead implicit def? This cannot be hoisted.
422 // No need to check if a dead implicit def is also defined by
423 // another instruction.
427 // FIXME: For now, avoid instructions with multiple defs, unless
428 // it's a dead implicit def.
434 // If we have already seen another instruction that defines the same
435 // register, then this is not safe.
436 if (++PhysRegDefs[Reg] > 1)
437 // MI defined register is seen defined by another instruction in
438 // the loop, it cannot be a LICM candidate.
440 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
441 if (++PhysRegDefs[*AS] > 1)
445 // Only consider reloads for now and remats which do not have register
446 // operands. FIXME: Consider unfold load folding instructions.
447 if (Def && !RuledOut) {
449 if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
450 (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
451 Candidates.push_back(CandidateInfo(MI, Def, FI));
455 /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
456 /// invariants out to the preheader.
457 void MachineLICM::HoistRegionPostRA() {
458 unsigned NumRegs = TRI->getNumRegs();
459 unsigned *PhysRegDefs = new unsigned[NumRegs];
460 std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0);
462 SmallVector<CandidateInfo, 32> Candidates;
463 SmallSet<int, 32> StoredFIs;
465 // Walk the entire region, count number of defs for each register, and
466 // collect potential LICM candidates.
467 const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks();
468 for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
469 MachineBasicBlock *BB = Blocks[i];
471 // If the header of the loop containing this basic block is a landing pad,
472 // then don't try to hoist instructions out of this loop.
473 const MachineLoop *ML = MLI->getLoopFor(BB);
474 if (ML && ML->getHeader()->isLandingPad()) continue;
476 // Conservatively treat live-in's as an external def.
477 // FIXME: That means a reload that're reused in successor block(s) will not
479 for (MachineBasicBlock::livein_iterator I = BB->livein_begin(),
480 E = BB->livein_end(); I != E; ++I) {
483 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
487 SpeculationState = SpeculateUnknown;
488 for (MachineBasicBlock::iterator
489 MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
490 MachineInstr *MI = &*MII;
491 ProcessMI(MI, PhysRegDefs, StoredFIs, Candidates);
495 // Now evaluate whether the potential candidates qualify.
496 // 1. Check if the candidate defined register is defined by another
497 // instruction in the loop.
498 // 2. If the candidate is a load from stack slot (always true for now),
499 // check if the slot is stored anywhere in the loop.
500 for (unsigned i = 0, e = Candidates.size(); i != e; ++i) {
501 if (Candidates[i].FI != INT_MIN &&
502 StoredFIs.count(Candidates[i].FI))
505 if (PhysRegDefs[Candidates[i].Def] == 1) {
507 MachineInstr *MI = Candidates[i].MI;
508 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
509 const MachineOperand &MO = MI->getOperand(j);
510 if (!MO.isReg() || MO.isDef() || !MO.getReg())
512 if (PhysRegDefs[MO.getReg()]) {
513 // If it's using a non-loop-invariant register, then it's obviously
514 // not safe to hoist.
520 HoistPostRA(MI, Candidates[i].Def);
524 delete[] PhysRegDefs;
527 /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current
528 /// loop, and make sure it is not killed by any instructions in the loop.
529 void MachineLICM::AddToLiveIns(unsigned Reg) {
530 const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks();
531 for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
532 MachineBasicBlock *BB = Blocks[i];
533 if (!BB->isLiveIn(Reg))
535 for (MachineBasicBlock::iterator
536 MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
537 MachineInstr *MI = &*MII;
538 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
539 MachineOperand &MO = MI->getOperand(i);
540 if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
541 if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
548 /// HoistPostRA - When an instruction is found to only use loop invariant
549 /// operands that is safe to hoist, this instruction is called to do the
551 void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) {
552 MachineBasicBlock *Preheader = getCurPreheader();
553 if (!Preheader) return;
555 // Now move the instructions to the predecessor, inserting it before any
556 // terminator instructions.
558 dbgs() << "Hoisting " << *MI;
559 if (Preheader->getBasicBlock())
560 dbgs() << " to MachineBasicBlock "
561 << Preheader->getName();
562 if (MI->getParent()->getBasicBlock())
563 dbgs() << " from MachineBasicBlock "
564 << MI->getParent()->getName();
568 // Splice the instruction to the preheader.
569 MachineBasicBlock *MBB = MI->getParent();
570 Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
572 // Add register to livein list to all the BBs in the current loop since a
573 // loop invariant must be kept live throughout the whole loop. This is
574 // important to ensure later passes do not scavenge the def register.
581 // IsGuaranteedToExecute - Check if this mbb is guaranteed to execute.
582 // If not then a load from this mbb may not be safe to hoist.
583 bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) {
584 if (SpeculationState != SpeculateUnknown)
585 return SpeculationState == SpeculateFalse;
587 if (BB != CurLoop->getHeader()) {
588 // Check loop exiting blocks.
589 SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
590 CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
591 for (unsigned i = 0, e = CurrentLoopExitingBlocks.size(); i != e; ++i)
592 if (!DT->dominates(BB, CurrentLoopExitingBlocks[i])) {
593 SpeculationState = SpeculateTrue;
598 SpeculationState = SpeculateFalse;
602 /// HoistRegion - Walk the specified region of the CFG (defined by all blocks
603 /// dominated by the specified block, and that are in the current loop) in depth
604 /// first order w.r.t the DominatorTree. This allows us to visit definitions
605 /// before uses, allowing us to hoist a loop body in one pass without iteration.
607 void MachineLICM::HoistRegion(MachineDomTreeNode *N, bool IsHeader) {
608 assert(N != 0 && "Null dominator tree node?");
609 MachineBasicBlock *BB = N->getBlock();
611 // If the header of the loop containing this basic block is a landing pad,
612 // then don't try to hoist instructions out of this loop.
613 const MachineLoop *ML = MLI->getLoopFor(BB);
614 if (ML && ML->getHeader()->isLandingPad()) return;
616 // If this subregion is not in the top level loop at all, exit.
617 if (!CurLoop->contains(BB)) return;
619 MachineBasicBlock *Preheader = getCurPreheader();
624 // Compute registers which are livein into the loop headers.
627 InitRegPressure(Preheader);
630 // Remember livein register pressure.
631 BackTrace.push_back(RegPressure);
633 SpeculationState = SpeculateUnknown;
634 for (MachineBasicBlock::iterator
635 MII = BB->begin(), E = BB->end(); MII != E; ) {
636 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
637 MachineInstr *MI = &*MII;
638 if (!Hoist(MI, Preheader))
639 UpdateRegPressure(MI);
643 // Don't hoist things out of a large switch statement. This often causes
644 // code to be hoisted that wasn't going to be executed, and increases
645 // register pressure in a situation where it's likely to matter.
646 if (BB->succ_size() < 25) {
647 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
648 for (unsigned I = 0, E = Children.size(); I != E; ++I)
649 HoistRegion(Children[I]);
652 BackTrace.pop_back();
655 static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
656 return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
659 /// getRegisterClassIDAndCost - For a given MI, register, and the operand
660 /// index, return the ID and cost of its representative register class.
662 MachineLICM::getRegisterClassIDAndCost(const MachineInstr *MI,
663 unsigned Reg, unsigned OpIdx,
664 unsigned &RCId, unsigned &RCCost) const {
665 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
666 EVT VT = *RC->vt_begin();
667 if (VT == MVT::untyped) {
671 RCId = TLI->getRepRegClassFor(VT)->getID();
672 RCCost = TLI->getRepRegClassCostFor(VT);
676 /// InitRegPressure - Find all virtual register references that are liveout of
677 /// the preheader to initialize the starting "register pressure". Note this
678 /// does not count live through (livein but not used) registers.
679 void MachineLICM::InitRegPressure(MachineBasicBlock *BB) {
680 std::fill(RegPressure.begin(), RegPressure.end(), 0);
682 // If the preheader has only a single predecessor and it ends with a
683 // fallthrough or an unconditional branch, then scan its predecessor for live
684 // defs as well. This happens whenever the preheader is created by splitting
685 // the critical edge from the loop predecessor to the loop header.
686 if (BB->pred_size() == 1) {
687 MachineBasicBlock *TBB = 0, *FBB = 0;
688 SmallVector<MachineOperand, 4> Cond;
689 if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
690 InitRegPressure(*BB->pred_begin());
693 for (MachineBasicBlock::iterator MII = BB->begin(), E = BB->end();
695 MachineInstr *MI = &*MII;
696 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
697 const MachineOperand &MO = MI->getOperand(i);
698 if (!MO.isReg() || MO.isImplicit())
700 unsigned Reg = MO.getReg();
701 if (!TargetRegisterInfo::isVirtualRegister(Reg))
704 bool isNew = RegSeen.insert(Reg);
705 unsigned RCId, RCCost;
706 getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost);
708 RegPressure[RCId] += RCCost;
710 bool isKill = isOperandKill(MO, MRI);
711 if (isNew && !isKill)
712 // Haven't seen this, it must be a livein.
713 RegPressure[RCId] += RCCost;
714 else if (!isNew && isKill)
715 RegPressure[RCId] -= RCCost;
721 /// UpdateRegPressure - Update estimate of register pressure after the
722 /// specified instruction.
723 void MachineLICM::UpdateRegPressure(const MachineInstr *MI) {
724 if (MI->isImplicitDef())
727 SmallVector<unsigned, 4> Defs;
728 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
729 const MachineOperand &MO = MI->getOperand(i);
730 if (!MO.isReg() || MO.isImplicit())
732 unsigned Reg = MO.getReg();
733 if (!TargetRegisterInfo::isVirtualRegister(Reg))
736 bool isNew = RegSeen.insert(Reg);
739 else if (!isNew && isOperandKill(MO, MRI)) {
740 unsigned RCId, RCCost;
741 getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost);
742 if (RCCost > RegPressure[RCId])
743 RegPressure[RCId] = 0;
745 RegPressure[RCId] -= RCCost;
750 while (!Defs.empty()) {
751 unsigned Reg = Defs.pop_back_val();
752 unsigned RCId, RCCost;
753 getRegisterClassIDAndCost(MI, Reg, Idx, RCId, RCCost);
754 RegPressure[RCId] += RCCost;
759 /// IsLICMCandidate - Returns true if the instruction may be a suitable
760 /// candidate for LICM. e.g. If the instruction is a call, then it's obviously
761 /// not safe to hoist it.
762 bool MachineLICM::IsLICMCandidate(MachineInstr &I) {
763 // Check if it's safe to move the instruction.
764 bool DontMoveAcrossStore = true;
765 if (!I.isSafeToMove(TII, AA, DontMoveAcrossStore))
768 // If it is load then check if it is guaranteed to execute by making sure that
769 // it dominates all exiting blocks. If it doesn't, then there is a path out of
770 // the loop which does not execute this load, so we can't hoist it.
771 // Stores and side effects are already checked by isSafeToMove.
772 if (I.getDesc().mayLoad() && !IsGuaranteedToExecute(I.getParent()))
778 /// IsLoopInvariantInst - Returns true if the instruction is loop
779 /// invariant. I.e., all virtual register operands are defined outside of the
780 /// loop, physical registers aren't accessed explicitly, and there are no side
781 /// effects that aren't captured by the operands or other flags.
783 bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
784 if (!IsLICMCandidate(I))
787 // The instruction is loop invariant if all of its operands are.
788 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
789 const MachineOperand &MO = I.getOperand(i);
794 unsigned Reg = MO.getReg();
795 if (Reg == 0) continue;
797 // Don't hoist an instruction that uses or defines a physical register.
798 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
800 // If the physreg has no defs anywhere, it's just an ambient register
801 // and we can freely move its uses. Alternatively, if it's allocatable,
802 // it could get allocated to something with a def during allocation.
803 if (!MRI->def_empty(Reg))
805 if (AllocatableSet.test(Reg))
807 // Check for a def among the register's aliases too.
808 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
809 unsigned AliasReg = *Alias;
810 if (!MRI->def_empty(AliasReg))
812 if (AllocatableSet.test(AliasReg))
815 // Otherwise it's safe to move.
817 } else if (!MO.isDead()) {
818 // A def that isn't dead. We can't move it.
820 } else if (CurLoop->getHeader()->isLiveIn(Reg)) {
821 // If the reg is live into the loop, we can't hoist an instruction
822 // which would clobber it.
830 assert(MRI->getVRegDef(Reg) &&
831 "Machine instr not mapped for this vreg?!");
833 // If the loop contains the definition of an operand, then the instruction
834 // isn't loop invariant.
835 if (CurLoop->contains(MRI->getVRegDef(Reg)))
839 // If we got this far, the instruction is loop invariant!
844 /// HasAnyPHIUse - Return true if the specified register is used by any
846 bool MachineLICM::HasAnyPHIUse(unsigned Reg) const {
847 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
848 UE = MRI->use_end(); UI != UE; ++UI) {
849 MachineInstr *UseMI = &*UI;
852 // Look pass copies as well.
853 if (UseMI->isCopy()) {
854 unsigned Def = UseMI->getOperand(0).getReg();
855 if (TargetRegisterInfo::isVirtualRegister(Def) &&
863 /// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
864 /// and an use in the current loop, return true if the target considered
866 bool MachineLICM::HasHighOperandLatency(MachineInstr &MI,
867 unsigned DefIdx, unsigned Reg) const {
868 if (!InstrItins || InstrItins->isEmpty() || MRI->use_nodbg_empty(Reg))
871 for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(Reg),
872 E = MRI->use_nodbg_end(); I != E; ++I) {
873 MachineInstr *UseMI = &*I;
874 if (UseMI->isCopyLike())
876 if (!CurLoop->contains(UseMI->getParent()))
878 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
879 const MachineOperand &MO = UseMI->getOperand(i);
880 if (!MO.isReg() || !MO.isUse())
882 unsigned MOReg = MO.getReg();
886 if (TII->hasHighOperandLatency(InstrItins, MRI, &MI, DefIdx, UseMI, i))
890 // Only look at the first in loop use.
897 /// IsCheapInstruction - Return true if the instruction is marked "cheap" or
898 /// the operand latency between its def and a use is one or less.
899 bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const {
900 if (MI.getDesc().isAsCheapAsAMove() || MI.isCopyLike())
902 if (!InstrItins || InstrItins->isEmpty())
905 bool isCheap = false;
906 unsigned NumDefs = MI.getDesc().getNumDefs();
907 for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
908 MachineOperand &DefMO = MI.getOperand(i);
909 if (!DefMO.isReg() || !DefMO.isDef())
912 unsigned Reg = DefMO.getReg();
913 if (TargetRegisterInfo::isPhysicalRegister(Reg))
916 if (!TII->hasLowDefLatency(InstrItins, &MI, i))
924 /// CanCauseHighRegPressure - Visit BBs from header to current BB, check
925 /// if hoisting an instruction of the given cost matrix can cause high
926 /// register pressure.
927 bool MachineLICM::CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost) {
928 for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end();
933 unsigned RCId = CI->first;
934 for (unsigned i = BackTrace.size(); i != 0; --i) {
935 SmallVector<unsigned, 8> &RP = BackTrace[i-1];
936 if (RP[RCId] + CI->second >= RegLimit[RCId])
944 /// UpdateBackTraceRegPressure - Traverse the back trace from header to the
945 /// current block and update their register pressures to reflect the effect
946 /// of hoisting MI from the current block to the preheader.
947 void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) {
948 if (MI->isImplicitDef())
951 // First compute the 'cost' of the instruction, i.e. its contribution
952 // to register pressure.
953 DenseMap<unsigned, int> Cost;
954 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
955 const MachineOperand &MO = MI->getOperand(i);
956 if (!MO.isReg() || MO.isImplicit())
958 unsigned Reg = MO.getReg();
959 if (!TargetRegisterInfo::isVirtualRegister(Reg))
962 unsigned RCId, RCCost;
963 getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost);
965 DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
966 if (CI != Cost.end())
967 CI->second += RCCost;
969 Cost.insert(std::make_pair(RCId, RCCost));
970 } else if (isOperandKill(MO, MRI)) {
971 DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
972 if (CI != Cost.end())
973 CI->second -= RCCost;
975 Cost.insert(std::make_pair(RCId, -RCCost));
979 // Update register pressure of blocks from loop header to current block.
980 for (unsigned i = 0, e = BackTrace.size(); i != e; ++i) {
981 SmallVector<unsigned, 8> &RP = BackTrace[i];
982 for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end();
984 unsigned RCId = CI->first;
985 RP[RCId] += CI->second;
990 /// IsProfitableToHoist - Return true if it is potentially profitable to hoist
991 /// the given loop invariant.
992 bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
993 if (MI.isImplicitDef())
996 // If the instruction is cheap, only hoist if it is re-materilizable. LICM
997 // will increase register pressure. It's probably not worth it if the
998 // instruction is cheap.
999 // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting
1000 // these tend to help performance in low register pressure situation. The
1001 // trade off is it may cause spill in high pressure situation. It will end up
1002 // adding a store in the loop preheader. But the reload is no more expensive.
1003 // The side benefit is these loads are frequently CSE'ed.
1004 if (IsCheapInstruction(MI)) {
1005 if (!TII->isTriviallyReMaterializable(&MI, AA))
1008 // Estimate register pressure to determine whether to LICM the instruction.
1009 // In low register pressure situation, we can be more aggressive about
1010 // hoisting. Also, favors hoisting long latency instructions even in
1011 // moderately high pressure situation.
1012 // FIXME: If there are long latency loop-invariant instructions inside the
1013 // loop at this point, why didn't the optimizer's LICM hoist them?
1014 DenseMap<unsigned, int> Cost;
1015 for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1016 const MachineOperand &MO = MI.getOperand(i);
1017 if (!MO.isReg() || MO.isImplicit())
1019 unsigned Reg = MO.getReg();
1020 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1023 unsigned RCId, RCCost;
1024 getRegisterClassIDAndCost(&MI, Reg, i, RCId, RCCost);
1026 if (HasHighOperandLatency(MI, i, Reg)) {
1031 DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
1032 if (CI != Cost.end())
1033 CI->second += RCCost;
1035 Cost.insert(std::make_pair(RCId, RCCost));
1036 } else if (isOperandKill(MO, MRI)) {
1037 // Is a virtual register use is a kill, hoisting it out of the loop
1038 // may actually reduce register pressure or be register pressure
1040 DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
1041 if (CI != Cost.end())
1042 CI->second -= RCCost;
1044 Cost.insert(std::make_pair(RCId, -RCCost));
1048 // Visit BBs from header to current BB, if hoisting this doesn't cause
1049 // high register pressure, then it's safe to proceed.
1050 if (!CanCauseHighRegPressure(Cost)) {
1055 // High register pressure situation, only hoist if the instruction is going to
1057 // Also, do not "speculate" in high register pressure situation. If an
1058 // instruction is not guaranteed to be executed in the loop, it's best to be
1060 if ((!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI)) ||
1061 (!TII->isTriviallyReMaterializable(&MI, AA) &&
1062 !MI.isInvariantLoad(AA)))
1066 // If result(s) of this instruction is used by PHIs outside of the loop, then
1067 // don't hoist it if the instruction because it will introduce an extra copy.
1068 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1069 const MachineOperand &MO = MI.getOperand(i);
1070 if (!MO.isReg() || !MO.isDef())
1072 if (HasAnyPHIUse(MO.getReg()))
1079 MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
1080 // Don't unfold simple loads.
1081 if (MI->getDesc().canFoldAsLoad())
1084 // If not, we may be able to unfold a load and hoist that.
1085 // First test whether the instruction is loading from an amenable
1087 if (!MI->isInvariantLoad(AA))
1090 // Next determine the register class for a temporary register.
1091 unsigned LoadRegIndex;
1093 TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1094 /*UnfoldLoad=*/true,
1095 /*UnfoldStore=*/false,
1097 if (NewOpc == 0) return 0;
1098 const MCInstrDesc &MID = TII->get(NewOpc);
1099 if (MID.getNumDefs() != 1) return 0;
1100 const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI);
1101 // Ok, we're unfolding. Create a temporary register and do the unfold.
1102 unsigned Reg = MRI->createVirtualRegister(RC);
1104 MachineFunction &MF = *MI->getParent()->getParent();
1105 SmallVector<MachineInstr *, 2> NewMIs;
1107 TII->unfoldMemoryOperand(MF, MI, Reg,
1108 /*UnfoldLoad=*/true, /*UnfoldStore=*/false,
1112 "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1114 assert(NewMIs.size() == 2 &&
1115 "Unfolded a load into multiple instructions!");
1116 MachineBasicBlock *MBB = MI->getParent();
1117 MBB->insert(MI, NewMIs[0]);
1118 MBB->insert(MI, NewMIs[1]);
1119 // If unfolding produced a load that wasn't loop-invariant or profitable to
1120 // hoist, discard the new instructions and bail.
1121 if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
1122 NewMIs[0]->eraseFromParent();
1123 NewMIs[1]->eraseFromParent();
1127 // Update register pressure for the unfolded instruction.
1128 UpdateRegPressure(NewMIs[1]);
1130 // Otherwise we successfully unfolded a load that we can hoist.
1131 MI->eraseFromParent();
1135 void MachineLICM::InitCSEMap(MachineBasicBlock *BB) {
1136 for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) {
1137 const MachineInstr *MI = &*I;
1138 unsigned Opcode = MI->getOpcode();
1139 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1140 CI = CSEMap.find(Opcode);
1141 if (CI != CSEMap.end())
1142 CI->second.push_back(MI);
1144 std::vector<const MachineInstr*> CSEMIs;
1145 CSEMIs.push_back(MI);
1146 CSEMap.insert(std::make_pair(Opcode, CSEMIs));
1152 MachineLICM::LookForDuplicate(const MachineInstr *MI,
1153 std::vector<const MachineInstr*> &PrevMIs) {
1154 for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
1155 const MachineInstr *PrevMI = PrevMIs[i];
1156 if (TII->produceSameValue(MI, PrevMI, (PreRegAlloc ? MRI : 0)))
1162 bool MachineLICM::EliminateCSE(MachineInstr *MI,
1163 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) {
1164 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1165 // the undef property onto uses.
1166 if (CI == CSEMap.end() || MI->isImplicitDef())
1169 if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
1170 DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
1172 // Replace virtual registers defined by MI by their counterparts defined
1174 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1175 const MachineOperand &MO = MI->getOperand(i);
1177 // Physical registers may not differ here.
1178 assert((!MO.isReg() || MO.getReg() == 0 ||
1179 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1180 MO.getReg() == Dup->getOperand(i).getReg()) &&
1181 "Instructions with different phys regs are not identical!");
1183 if (MO.isReg() && MO.isDef() &&
1184 !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
1185 MRI->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg());
1186 MRI->clearKillFlags(Dup->getOperand(i).getReg());
1189 MI->eraseFromParent();
1196 /// MayCSE - Return true if the given instruction will be CSE'd if it's
1197 /// hoisted out of the loop.
1198 bool MachineLICM::MayCSE(MachineInstr *MI) {
1199 unsigned Opcode = MI->getOpcode();
1200 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1201 CI = CSEMap.find(Opcode);
1202 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1203 // the undef property onto uses.
1204 if (CI == CSEMap.end() || MI->isImplicitDef())
1207 return LookForDuplicate(MI, CI->second) != 0;
1210 /// Hoist - When an instruction is found to use only loop invariant operands
1211 /// that are safe to hoist, this instruction is called to do the dirty work.
1213 bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
1214 // First check whether we should hoist this instruction.
1215 if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
1216 // If not, try unfolding a hoistable load.
1217 MI = ExtractHoistableLoad(MI);
1218 if (!MI) return false;
1221 // Now move the instructions to the predecessor, inserting it before any
1222 // terminator instructions.
1224 dbgs() << "Hoisting " << *MI;
1225 if (Preheader->getBasicBlock())
1226 dbgs() << " to MachineBasicBlock "
1227 << Preheader->getName();
1228 if (MI->getParent()->getBasicBlock())
1229 dbgs() << " from MachineBasicBlock "
1230 << MI->getParent()->getName();
1234 // If this is the first instruction being hoisted to the preheader,
1235 // initialize the CSE map with potential common expressions.
1237 InitCSEMap(Preheader);
1238 FirstInLoop = false;
1241 // Look for opportunity to CSE the hoisted instruction.
1242 unsigned Opcode = MI->getOpcode();
1243 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1244 CI = CSEMap.find(Opcode);
1245 if (!EliminateCSE(MI, CI)) {
1246 // Otherwise, splice the instruction to the preheader.
1247 Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
1249 // Update register pressure for BBs from header to this block.
1250 UpdateBackTraceRegPressure(MI);
1252 // Clear the kill flags of any register this instruction defines,
1253 // since they may need to be live throughout the entire loop
1254 // rather than just live for part of it.
1255 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1256 MachineOperand &MO = MI->getOperand(i);
1257 if (MO.isReg() && MO.isDef() && !MO.isDead())
1258 MRI->clearKillFlags(MO.getReg());
1261 // Add to the CSE map.
1262 if (CI != CSEMap.end())
1263 CI->second.push_back(MI);
1265 std::vector<const MachineInstr*> CSEMIs;
1266 CSEMIs.push_back(MI);
1267 CSEMap.insert(std::make_pair(Opcode, CSEMIs));
1277 MachineBasicBlock *MachineLICM::getCurPreheader() {
1278 // Determine the block to which to hoist instructions. If we can't find a
1279 // suitable loop predecessor, we can't do any hoisting.
1281 // If we've tried to get a preheader and failed, don't try again.
1282 if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1))
1285 if (!CurPreheader) {
1286 CurPreheader = CurLoop->getLoopPreheader();
1287 if (!CurPreheader) {
1288 MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
1290 CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1294 CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this);
1295 if (!CurPreheader) {
1296 CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1301 return CurPreheader;