2001-11-26 19:41:20 +01: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-31 00:27:55 +01:00
|
|
|
#include "llvm/Pass.h"
|
2001-11-26 19:41:20 +01:00
|
|
|
#include <set>
|
|
|
|
|
2002-04-28 18:19:42 +02:00
|
|
|
class DominatorSet;
|
|
|
|
class LoopInfo;
|
2001-11-26 19:41:20 +01: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 02:15:57 +02:00
|
|
|
std::vector<BasicBlock *> Blocks; // First entry is the header node
|
2002-01-20 23:54:45 +01:00
|
|
|
std::vector<Loop*> SubLoops; // Loops contained entirely within this one
|
2001-11-26 19:41:20 +01: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 02:15:57 +02:00
|
|
|
inline BasicBlock *getHeader() const { return Blocks.front(); }
|
2001-11-26 19:41:20 +01:00
|
|
|
|
|
|
|
// contains - Return true of the specified basic block is in this loop
|
2002-06-04 00:10:48 +02:00
|
|
|
bool contains(const BasicBlock *BB) const;
|
2001-11-26 19:41:20 +01:00
|
|
|
|
|
|
|
// getSubLoops - Return the loops contained entirely within this loop
|
2002-01-20 23:54:45 +01:00
|
|
|
inline const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
|
2002-04-28 02:15:57 +02:00
|
|
|
inline const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
|
2001-11-26 19:41:20 +01:00
|
|
|
|
2002-07-27 03:12:15 +02:00
|
|
|
void print(std::ostream &O) const;
|
2001-11-26 19:41:20 +01:00
|
|
|
private:
|
|
|
|
friend class LoopInfo;
|
2002-04-28 02:15:57 +02:00
|
|
|
inline Loop(BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
|
2002-04-09 07:43:19 +02:00
|
|
|
~Loop() {
|
|
|
|
for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
|
|
|
|
delete SubLoops[i];
|
|
|
|
}
|
2001-11-26 19:41:20 +01:00
|
|
|
|
|
|
|
void setLoopDepth(unsigned Level) {
|
|
|
|
LoopDepth = Level;
|
2002-04-09 07:43:19 +02:00
|
|
|
for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
|
2001-11-26 19:41:20 +01:00
|
|
|
SubLoops[i]->setLoopDepth(Level+1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LoopInfo - This class builds and contains all of the top level loop
|
2002-04-27 08:56:12 +02:00
|
|
|
// structures in the specified function.
|
2001-11-26 19:41:20 +01:00
|
|
|
//
|
2002-04-27 08:56:12 +02:00
|
|
|
class LoopInfo : public FunctionPass {
|
2001-11-26 19:41:20 +01:00
|
|
|
// BBMap - Mapping of basic blocks to the inner most loop they occur in
|
2002-04-28 02:15:57 +02:00
|
|
|
std::map<BasicBlock*, Loop*> BBMap;
|
2002-01-20 23:54:45 +01:00
|
|
|
std::vector<Loop*> TopLevelLoops;
|
2001-11-26 19:41:20 +01:00
|
|
|
public:
|
2002-04-28 18:19:42 +02:00
|
|
|
static AnalysisID ID; // LoopInfo Analysis ID
|
2002-01-31 00:27:55 +01:00
|
|
|
|
2001-11-26 19:41:20 +01:00
|
|
|
// LoopInfo ctor - Calculate the natural loop information for a CFG
|
2002-04-09 07:43:19 +02:00
|
|
|
~LoopInfo() { releaseMemory(); }
|
2001-11-26 19:41:20 +01:00
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
|
2001-11-26 19:41:20 +01: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-06-25 18:13:24 +02:00
|
|
|
const Loop *getLoopFor(const BasicBlock *BB) const {
|
|
|
|
std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
|
2001-11-26 19:41:20 +01:00
|
|
|
return I != BBMap.end() ? I->second : 0;
|
|
|
|
}
|
2002-06-25 18:13:24 +02:00
|
|
|
inline const Loop *operator[](const BasicBlock *BB) const {
|
2001-11-26 19:41:20 +01:00
|
|
|
return getLoopFor(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLoopDepth - Return the loop nesting level of the specified block...
|
2002-06-25 18:13:24 +02:00
|
|
|
unsigned getLoopDepth(const BasicBlock *BB) const {
|
2001-11-26 19:41:20 +01: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 02:15:57 +02:00
|
|
|
bool isLoopHeader(BasicBlock *BB) const {
|
2001-11-26 19:41:20 +01:00
|
|
|
return getLoopFor(BB)->getHeader() == BB;
|
|
|
|
}
|
|
|
|
// isLoopEnd - True if block jumps to loop entry
|
2002-04-28 02:15:57 +02:00
|
|
|
bool isLoopEnd(BasicBlock *BB) const;
|
2001-11-26 19:41:20 +01:00
|
|
|
// isLoopExit - True if block is the loop exit
|
2002-04-28 02:15:57 +02:00
|
|
|
bool isLoopExit(BasicBlock *BB) const;
|
2001-11-26 19:41:20 +01:00
|
|
|
#endif
|
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
// runOnFunction - Pass framework implementation
|
2002-06-25 18:13:24 +02:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-01-31 00:27:55 +01:00
|
|
|
|
2002-04-09 07:43:19 +02:00
|
|
|
virtual void releaseMemory();
|
2002-07-27 03:12:15 +02:00
|
|
|
void print(std::ostream &O) const;
|
2002-04-09 07:43:19 +02:00
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
// getAnalysisUsage - Provide loop info, require dominator set
|
2002-01-31 00:27:55 +01:00
|
|
|
//
|
2002-04-27 08:56:12 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2001-11-26 19:41:20 +01:00
|
|
|
private:
|
2002-01-31 00:27:55 +01:00
|
|
|
void Calculate(const DominatorSet &DS);
|
2002-04-28 02:15:57 +02:00
|
|
|
Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
|
2001-11-26 19:41:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|