1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator --------*- C++ -*--=//
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 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
11 #define LLVM_ANALYSIS_LOOP_INFO_H
13 #include "llvm/Pass.h"
20 //===----------------------------------------------------------------------===//
21 // Loop class - Instances of this class are used to represent loops that are
22 // detected in the flow graph
26 std::vector<const BasicBlock *> Blocks; // First entry is the header node
27 std::vector<Loop*> SubLoops; // Loops contained entirely within this one
28 unsigned LoopDepth; // Nesting depth of this loop
30 Loop(const Loop &); // DO NOT IMPLEMENT
31 const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
34 inline unsigned getLoopDepth() const { return LoopDepth; }
35 inline const BasicBlock *getHeader() const { return Blocks.front(); }
37 // contains - Return true of the specified basic block is in this loop
38 bool contains(const BasicBlock *BB) const;
40 // getSubLoops - Return the loops contained entirely within this loop
41 inline const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
42 inline const std::vector<const BasicBlock*> &getBlocks() const {
47 friend class LoopInfo;
48 inline Loop(const BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
50 void setLoopDepth(unsigned Level) {
52 for (unsigned i = 0; i < SubLoops.size(); ++i)
53 SubLoops[i]->setLoopDepth(Level+1);
59 //===----------------------------------------------------------------------===//
60 // LoopInfo - This class builds and contains all of the top level loop
61 // structures in the specified method.
63 class LoopInfo : public MethodPass {
64 // BBMap - Mapping of basic blocks to the inner most loop they occur in
65 std::map<const BasicBlock *, Loop*> BBMap;
66 std::vector<Loop*> TopLevelLoops;
68 static AnalysisID ID; // cfg::LoopInfo Analysis ID
70 // LoopInfo ctor - Calculate the natural loop information for a CFG
71 LoopInfo(AnalysisID id) { assert(id == ID); }
73 const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
75 // getLoopFor - Return the inner most loop that BB lives in. If a basic block
76 // is in no loop (for example the entry node), null is returned.
78 const Loop *getLoopFor(const BasicBlock *BB) const {
79 std::map<const BasicBlock *, Loop*>::const_iterator I = BBMap.find(BB);
80 return I != BBMap.end() ? I->second : 0;
82 inline const Loop *operator[](const BasicBlock *BB) const {
83 return getLoopFor(BB);
86 // getLoopDepth - Return the loop nesting level of the specified block...
87 unsigned getLoopDepth(const BasicBlock *BB) const {
88 const Loop *L = getLoopFor(BB);
89 return L ? L->getLoopDepth() : 0;
93 // isLoopHeader - True if the block is a loop header node
94 bool isLoopHeader(const BasicBlock *BB) const {
95 return getLoopFor(BB)->getHeader() == BB;
97 // isLoopEnd - True if block jumps to loop entry
98 bool isLoopEnd(const BasicBlock *BB) const;
99 // isLoopExit - True if block is the loop exit
100 bool isLoopExit(const BasicBlock *BB) const;
103 // runOnMethod - Pass framework implementation
104 virtual bool runOnMethod(Function *F);
106 // getAnalysisUsageInfo - Provide loop info, require dominator set
108 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
109 Pass::AnalysisSet &Destroyed,
110 Pass::AnalysisSet &Provided);
112 void Calculate(const DominatorSet &DS);
113 Loop *ConsiderForLoop(const BasicBlock *BB, const DominatorSet &DS);
116 } // End namespace cfg