1 //===- LoopInfo.cpp - Natural Loop Calculator -------------------------------=//
3 // This file defines the LoopInfo class that is used to identify natural loops
4 // and determine the loop depth of various nodes of the CFG. Note that the
5 // loops identified may actually be several natural loops that share the same
6 // header node... not just a single natural loop.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/LoopInfo.h"
11 #include "llvm/Analysis/Dominators.h"
12 #include "llvm/Support/CFG.h"
13 #include "llvm/Assembly/Writer.h"
14 #include "Support/DepthFirstIterator.h"
17 static RegisterAnalysis<LoopInfo>
18 X("loops", "Natural Loop Construction", true);
20 //===----------------------------------------------------------------------===//
21 // Loop implementation
23 bool Loop::contains(const BasicBlock *BB) const {
24 return find(Blocks.begin(), Blocks.end(), BB) != Blocks.end();
27 void Loop::print(std::ostream &OS) const {
28 OS << std::string(getLoopDepth()*2, ' ') << "Loop Containing: ";
30 for (unsigned i = 0; i < getBlocks().size(); ++i) {
32 WriteAsOperand(OS, (const Value*)getBlocks()[i]);
36 std::copy(getSubLoops().begin(), getSubLoops().end(),
37 std::ostream_iterator<const Loop*>(OS, "\n"));
40 //===----------------------------------------------------------------------===//
41 // LoopInfo implementation
44 bool LoopInfo::runOnFunction(Function &) {
46 Calculate(getAnalysis<DominatorSet>()); // Update
50 void LoopInfo::releaseMemory() {
51 for (std::vector<Loop*>::iterator I = TopLevelLoops.begin(),
52 E = TopLevelLoops.end(); I != E; ++I)
53 delete *I; // Delete all of the loops...
55 BBMap.clear(); // Reset internal state of analysis
56 TopLevelLoops.clear();
60 void LoopInfo::Calculate(const DominatorSet &DS) {
61 BasicBlock *RootNode = DS.getRoot();
63 for (df_iterator<BasicBlock*> NI = df_begin(RootNode),
64 NE = df_end(RootNode); NI != NE; ++NI)
65 if (Loop *L = ConsiderForLoop(*NI, DS))
66 TopLevelLoops.push_back(L);
68 for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
69 TopLevelLoops[i]->setLoopDepth(1);
72 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.addRequired<DominatorSet>();
77 void LoopInfo::print(std::ostream &OS) const {
78 std::copy(getTopLevelLoops().begin(), getTopLevelLoops().end(),
79 std::ostream_iterator<const Loop*>(OS, "\n"));
82 Loop *LoopInfo::ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS) {
83 if (BBMap.find(BB) != BBMap.end()) return 0; // Havn't processed this node?
85 std::vector<BasicBlock *> TodoStack;
87 // Scan the predecessors of BB, checking to see if BB dominates any of
89 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I)
90 if (DS.dominates(BB, *I)) // If BB dominates it's predecessor...
91 TodoStack.push_back(*I);
93 if (TodoStack.empty()) return 0; // Doesn't dominate any predecessors...
95 // Create a new loop to represent this basic block...
96 Loop *L = new Loop(BB);
99 while (!TodoStack.empty()) { // Process all the nodes in the loop
100 BasicBlock *X = TodoStack.back();
101 TodoStack.pop_back();
103 if (!L->contains(X)) { // As of yet unprocessed??
104 L->Blocks.push_back(X);
106 // Add all of the predecessors of X to the end of the work stack...
107 TodoStack.insert(TodoStack.end(), pred_begin(X), pred_end(X));
111 // Add the basic blocks that comprise this loop to the BBMap so that this
112 // loop can be found for them. Also check subsidary basic blocks to see if
113 // they start subloops of their own.
115 for (std::vector<BasicBlock*>::reverse_iterator I = L->Blocks.rbegin(),
116 E = L->Blocks.rend(); I != E; ++I) {
118 // Check to see if this block starts a new loop
119 if (Loop *NewLoop = ConsiderForLoop(*I, DS)) {
120 L->SubLoops.push_back(NewLoop);
121 NewLoop->ParentLoop = L;
124 if (BBMap.find(*I) == BBMap.end())
125 BBMap.insert(std::make_pair(*I, L));