2001-11-26 18:41:20 +00:00
|
|
|
//===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator --------*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This file defines the LoopInfo class that is used to identify natural loops
|
|
|
|
// and determine the loop depth of various nodes of the CFG. Note that the
|
|
|
|
// loops identified may actually be several natural loops that share the same
|
|
|
|
// header node... not just a single natural loop.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_LOOP_INFO_H
|
|
|
|
#define LLVM_ANALYSIS_LOOP_INFO_H
|
|
|
|
|
2002-01-30 23:27:55 +00:00
|
|
|
#include "llvm/Pass.h"
|
2001-11-26 18:41:20 +00:00
|
|
|
#include <set>
|
|
|
|
|
2002-04-28 16:19:42 +00:00
|
|
|
class DominatorSet;
|
|
|
|
class LoopInfo;
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Loop class - Instances of this class are used to represent loops that are
|
|
|
|
// detected in the flow graph
|
|
|
|
//
|
|
|
|
class Loop {
|
|
|
|
Loop *ParentLoop;
|
2002-04-28 00:15:57 +00:00
|
|
|
std::vector<BasicBlock *> Blocks; // First entry is the header node
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Loop*> SubLoops; // Loops contained entirely within this one
|
2001-11-26 18:41:20 +00:00
|
|
|
unsigned LoopDepth; // Nesting depth of this loop
|
|
|
|
|
|
|
|
Loop(const Loop &); // DO NOT IMPLEMENT
|
|
|
|
const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
|
|
|
|
public:
|
|
|
|
|
|
|
|
inline unsigned getLoopDepth() const { return LoopDepth; }
|
2002-04-28 00:15:57 +00:00
|
|
|
inline BasicBlock *getHeader() const { return Blocks.front(); }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
// contains - Return true of the specified basic block is in this loop
|
2002-06-03 22:10:48 +00:00
|
|
|
bool contains(const BasicBlock *BB) const;
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
// getSubLoops - Return the loops contained entirely within this loop
|
2002-01-20 22:54:45 +00:00
|
|
|
inline const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
|
2002-04-28 00:15:57 +00:00
|
|
|
inline const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class LoopInfo;
|
2002-04-28 00:15:57 +00:00
|
|
|
inline Loop(BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
|
2002-04-09 05:43:19 +00:00
|
|
|
~Loop() {
|
|
|
|
for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
|
|
|
|
delete SubLoops[i];
|
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
void setLoopDepth(unsigned Level) {
|
|
|
|
LoopDepth = Level;
|
2002-04-09 05:43:19 +00:00
|
|
|
for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
|
2001-11-26 18:41:20 +00:00
|
|
|
SubLoops[i]->setLoopDepth(Level+1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LoopInfo - This class builds and contains all of the top level loop
|
2002-04-27 06:56:12 +00:00
|
|
|
// structures in the specified function.
|
2001-11-26 18:41:20 +00:00
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
class LoopInfo : public FunctionPass {
|
2001-11-26 18:41:20 +00:00
|
|
|
// BBMap - Mapping of basic blocks to the inner most loop they occur in
|
2002-04-28 00:15:57 +00:00
|
|
|
std::map<BasicBlock*, Loop*> BBMap;
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Loop*> TopLevelLoops;
|
2001-11-26 18:41:20 +00:00
|
|
|
public:
|
2002-04-28 16:19:42 +00:00
|
|
|
static AnalysisID ID; // LoopInfo Analysis ID
|
2002-01-30 23:27:55 +00:00
|
|
|
|
2001-11-26 18:41:20 +00:00
|
|
|
// LoopInfo ctor - Calculate the natural loop information for a CFG
|
2002-01-30 23:27:55 +00:00
|
|
|
LoopInfo(AnalysisID id) { assert(id == ID); }
|
2002-04-09 05:43:19 +00:00
|
|
|
~LoopInfo() { releaseMemory(); }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2002-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const { return "Natural Loop Analysis"; }
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
// getLoopFor - Return the inner most loop that BB lives in. If a basic block
|
|
|
|
// is in no loop (for example the entry node), null is returned.
|
|
|
|
//
|
2002-04-28 00:15:57 +00:00
|
|
|
const Loop *getLoopFor(BasicBlock *BB) const {
|
|
|
|
std::map<BasicBlock *, Loop*>::const_iterator I = BBMap.find(BB);
|
2001-11-26 18:41:20 +00:00
|
|
|
return I != BBMap.end() ? I->second : 0;
|
|
|
|
}
|
2002-04-28 00:15:57 +00:00
|
|
|
inline const Loop *operator[](BasicBlock *BB) const {
|
2001-11-26 18:41:20 +00:00
|
|
|
return getLoopFor(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLoopDepth - Return the loop nesting level of the specified block...
|
2002-04-28 00:15:57 +00:00
|
|
|
unsigned getLoopDepth(BasicBlock *BB) const {
|
2001-11-26 18:41:20 +00:00
|
|
|
const Loop *L = getLoopFor(BB);
|
|
|
|
return L ? L->getLoopDepth() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// isLoopHeader - True if the block is a loop header node
|
2002-04-28 00:15:57 +00:00
|
|
|
bool isLoopHeader(BasicBlock *BB) const {
|
2001-11-26 18:41:20 +00:00
|
|
|
return getLoopFor(BB)->getHeader() == BB;
|
|
|
|
}
|
|
|
|
// isLoopEnd - True if block jumps to loop entry
|
2002-04-28 00:15:57 +00:00
|
|
|
bool isLoopEnd(BasicBlock *BB) const;
|
2001-11-26 18:41:20 +00:00
|
|
|
// isLoopExit - True if block is the loop exit
|
2002-04-28 00:15:57 +00:00
|
|
|
bool isLoopExit(BasicBlock *BB) const;
|
2001-11-26 18:41:20 +00:00
|
|
|
#endif
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
// runOnFunction - Pass framework implementation
|
|
|
|
virtual bool runOnFunction(Function *F);
|
2002-01-30 23:27:55 +00:00
|
|
|
|
2002-04-09 05:43:19 +00:00
|
|
|
virtual void releaseMemory();
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
// getAnalysisUsage - Provide loop info, require dominator set
|
2002-01-30 23:27:55 +00:00
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2001-11-26 18:41:20 +00:00
|
|
|
private:
|
2002-01-30 23:27:55 +00:00
|
|
|
void Calculate(const DominatorSet &DS);
|
2002-04-28 00:15:57 +00:00
|
|
|
Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
|
2001-11-26 18:41:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|