• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Analysis/LoopInfoImpl.h"
19 #include "llvm/CodeGen/MachineDominators.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Support/Debug.h"
22 using namespace llvm;
23 
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>;
27 
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)
34 
35 char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
36 
runOnMachineFunction(MachineFunction &)37 bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
38   releaseMemory();
39   LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
40   return false;
41 }
42 
getAnalysisUsage(AnalysisUsage & AU) const43 void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
44   AU.setPreservesAll();
45   AU.addRequired<MachineDominatorTree>();
46   MachineFunctionPass::getAnalysisUsage(AU);
47 }
48 
getTopBlock()49 MachineBasicBlock *MachineLoop::getTopBlock() {
50   MachineBasicBlock *TopMBB = getHeader();
51   MachineFunction::iterator Begin = TopMBB->getParent()->begin();
52   if (TopMBB != Begin) {
53     MachineBasicBlock *PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
54     while (contains(PriorMBB)) {
55       TopMBB = PriorMBB;
56       if (TopMBB == Begin) break;
57       PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
58     }
59   }
60   return TopMBB;
61 }
62 
getBottomBlock()63 MachineBasicBlock *MachineLoop::getBottomBlock() {
64   MachineBasicBlock *BotMBB = getHeader();
65   MachineFunction::iterator End = BotMBB->getParent()->end();
66   if (BotMBB != std::prev(End)) {
67     MachineBasicBlock *NextMBB = std::next(MachineFunction::iterator(BotMBB));
68     while (contains(NextMBB)) {
69       BotMBB = NextMBB;
70       if (BotMBB == std::next(MachineFunction::iterator(BotMBB))) break;
71       NextMBB = std::next(MachineFunction::iterator(BotMBB));
72     }
73   }
74   return BotMBB;
75 }
76 
77 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const78 void MachineLoop::dump() const {
79   print(dbgs());
80 }
81 #endif
82