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/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"
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>;
30 StableLoopInfo("stable-machine-loops", cl::Hidden, cl::init(false),
31 cl::desc("Compute a stable loop tree."));
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)
40 char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
42 bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
45 LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
47 LI.Calculate(getAnalysis<MachineDominatorTree>().getBase()); // Update
51 void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.addRequired<MachineDominatorTree>();
54 MachineFunctionPass::getAnalysisUsage(AU);
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)) {
64 if (TopMBB == Begin) break;
65 PriorMBB = prior(MachineFunction::iterator(TopMBB));
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)) {
78 if (BotMBB == llvm::next(MachineFunction::iterator(BotMBB))) break;
79 NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
85 void MachineLoop::dump() const {