1 //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the MachineLoopInfo class that is used to identify natural 11 // loops and determine the loop depth of various nodes of the CFG. Note that 12 // the loops identified may actually be several natural loops that share the 13 // same header node... not just a single natural loop. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/CodeGen/MachineLoopInfo.h" 18 #include "llvm/CodeGen/MachineDominators.h" 19 #include "llvm/CodeGen/Passes.h" 20 #include "llvm/Analysis/LoopInfoImpl.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Support/Debug.h" 23 using namespace llvm; 24 25 // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops. 26 template class llvm::LoopBase<MachineBasicBlock, MachineLoop>; 27 template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>; 28 29 static cl::opt<bool> 30 StableLoopInfo("stable-machine-loops", cl::Hidden, cl::init(false), 31 cl::desc("Compute a stable loop tree.")); 32 33 char MachineLoopInfo::ID = 0; 34 INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops", 35 "Machine Natural Loop Construction", true, true) 36 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 37 INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops", 38 "Machine Natural Loop Construction", true, true) 39 40 char &llvm::MachineLoopInfoID = MachineLoopInfo::ID; 41 42 bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) { 43 releaseMemory(); 44 if (StableLoopInfo) 45 LI.Analyze(getAnalysis<MachineDominatorTree>().getBase()); 46 else 47 LI.Calculate(getAnalysis<MachineDominatorTree>().getBase()); // Update 48 return false; 49 } 50 51 void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { 52 AU.setPreservesAll(); 53 AU.addRequired<MachineDominatorTree>(); 54 MachineFunctionPass::getAnalysisUsage(AU); 55 } 56 57 MachineBasicBlock *MachineLoop::getTopBlock() { 58 MachineBasicBlock *TopMBB = getHeader(); 59 MachineFunction::iterator Begin = TopMBB->getParent()->begin(); 60 if (TopMBB != Begin) { 61 MachineBasicBlock *PriorMBB = prior(MachineFunction::iterator(TopMBB)); 62 while (contains(PriorMBB)) { 63 TopMBB = PriorMBB; 64 if (TopMBB == Begin) break; 65 PriorMBB = prior(MachineFunction::iterator(TopMBB)); 66 } 67 } 68 return TopMBB; 69 } 70 71 MachineBasicBlock *MachineLoop::getBottomBlock() { 72 MachineBasicBlock *BotMBB = getHeader(); 73 MachineFunction::iterator End = BotMBB->getParent()->end(); 74 if (BotMBB != prior(End)) { 75 MachineBasicBlock *NextMBB = llvm::next(MachineFunction::iterator(BotMBB)); 76 while (contains(NextMBB)) { 77 BotMBB = NextMBB; 78 if (BotMBB == llvm::next(MachineFunction::iterator(BotMBB))) break; 79 NextMBB = llvm::next(MachineFunction::iterator(BotMBB)); 80 } 81 } 82 return BotMBB; 83 } 84 85 void MachineLoop::dump() const { 86 print(dbgs()); 87 } 88