LLVM API Documentation

MachineLoopInfo.cpp
Go to the documentation of this file.
00001 //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MachineLoopInfo class that is used to identify natural
00011 // loops and determine the loop depth of various nodes of the CFG.  Note that
00012 // the loops identified may actually be several natural loops that share the
00013 // same header node... not just a single natural loop.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/CodeGen/MachineLoopInfo.h"
00018 #include "llvm/Analysis/LoopInfoImpl.h"
00019 #include "llvm/CodeGen/MachineDominators.h"
00020 #include "llvm/CodeGen/Passes.h"
00021 #include "llvm/Support/Debug.h"
00022 using namespace llvm;
00023 
00024 // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
00025 template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
00026 template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
00027 
00028 char MachineLoopInfo::ID = 0;
00029 INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
00030                 "Machine Natural Loop Construction", true, true)
00031 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
00032 INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
00033                 "Machine Natural Loop Construction", true, true)
00034 
00035 char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
00036 
00037 bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
00038   releaseMemory();
00039   LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
00040   return false;
00041 }
00042 
00043 void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
00044   AU.setPreservesAll();
00045   AU.addRequired<MachineDominatorTree>();
00046   MachineFunctionPass::getAnalysisUsage(AU);
00047 }
00048 
00049 MachineBasicBlock *MachineLoop::getTopBlock() {
00050   MachineBasicBlock *TopMBB = getHeader();
00051   MachineFunction::iterator Begin = TopMBB->getParent()->begin();
00052   if (TopMBB != Begin) {
00053     MachineBasicBlock *PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
00054     while (contains(PriorMBB)) {
00055       TopMBB = PriorMBB;
00056       if (TopMBB == Begin) break;
00057       PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
00058     }
00059   }
00060   return TopMBB;
00061 }
00062 
00063 MachineBasicBlock *MachineLoop::getBottomBlock() {
00064   MachineBasicBlock *BotMBB = getHeader();
00065   MachineFunction::iterator End = BotMBB->getParent()->end();
00066   if (BotMBB != std::prev(End)) {
00067     MachineBasicBlock *NextMBB = std::next(MachineFunction::iterator(BotMBB));
00068     while (contains(NextMBB)) {
00069       BotMBB = NextMBB;
00070       if (BotMBB == std::next(MachineFunction::iterator(BotMBB))) break;
00071       NextMBB = std::next(MachineFunction::iterator(BotMBB));
00072     }
00073   }
00074   return BotMBB;
00075 }
00076 
00077 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00078 void MachineLoop::dump() const {
00079   print(dbgs());
00080 }
00081 #endif