1 //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
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 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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/MachineLoopInfo.h"
18 #include "llvm/Analysis/LoopInfoImpl.h"
19 #include "llvm/CodeGen/MachineDominators.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Support/Debug.h"
24 // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
25 template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
26 template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
28 char MachineLoopInfo::ID = 0;
29 INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
30 "Machine Natural Loop Construction", true, true)
31 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
32 INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
33 "Machine Natural Loop Construction", true, true)
35 char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
37 bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
39 LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
43 void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addRequired<MachineDominatorTree>();
46 MachineFunctionPass::getAnalysisUsage(AU);
49 MachineBasicBlock *MachineLoop::getTopBlock() {
50 MachineBasicBlock *TopMBB = getHeader();
51 MachineFunction::iterator Begin = TopMBB->getParent()->begin();
52 if (TopMBB != Begin) {
53 MachineBasicBlock *PriorMBB = prior(MachineFunction::iterator(TopMBB));
54 while (contains(PriorMBB)) {
56 if (TopMBB == Begin) break;
57 PriorMBB = prior(MachineFunction::iterator(TopMBB));
63 MachineBasicBlock *MachineLoop::getBottomBlock() {
64 MachineBasicBlock *BotMBB = getHeader();
65 MachineFunction::iterator End = BotMBB->getParent()->end();
66 if (BotMBB != prior(End)) {
67 MachineBasicBlock *NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
68 while (contains(NextMBB)) {
70 if (BotMBB == llvm::next(MachineFunction::iterator(BotMBB))) break;
71 NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
77 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
78 void MachineLoop::dump() const {