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
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-26 05:32:43 +00:00
|
|
|
/// Loop class - Instances of this class are used to represent loops that are
|
|
|
|
/// detected in the flow graph
|
|
|
|
///
|
2001-11-26 18:41:20 +00:00
|
|
|
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(); }
|
2002-09-26 05:32:43 +00:00
|
|
|
inline Loop *getParentLoop() const { return ParentLoop; }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2002-09-26 05:32:43 +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
|
|
|
|
2002-09-26 05:32:43 +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
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// isLoopExit - True if terminator in the block can branch to another block
|
|
|
|
/// that is outside of the current loop.
|
2002-10-11 05:31:10 +00:00
|
|
|
bool isLoopExit(const BasicBlock *BB) const;
|
|
|
|
|
|
|
|
/// Find number of back edges
|
|
|
|
unsigned getNumBackEdges() const;
|
2002-09-25 19:10:06 +00:00
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// getLoopPreheader - If there is a preheader for this loop, return it. A
|
|
|
|
/// loop has a preheader if there is only one edge to the header of the loop
|
|
|
|
/// from outside of the loop. If this is the case, the block branching to the
|
|
|
|
/// header of the loop is the preheader node. The "preheaders" pass can be
|
|
|
|
/// "Required" to ensure that there is always a preheader node for every loop.
|
|
|
|
///
|
|
|
|
/// This method returns null if there is no preheader for the loop (either
|
|
|
|
/// because the loop is dead or because multiple blocks branch to the header
|
|
|
|
/// node of this loop).
|
|
|
|
///
|
|
|
|
BasicBlock *getLoopPreheader() const;
|
|
|
|
|
|
|
|
/// addBasicBlockToLoop - This function is used by other analyses to update
|
|
|
|
/// loop information. NewBB is set to be a new member of the current loop.
|
|
|
|
/// Because of this, it is added as a member of all parent loops, and is added
|
|
|
|
/// to the specified LoopInfo object as being in the current basic block. It
|
|
|
|
/// is not valid to replace the loop header with this method.
|
|
|
|
///
|
|
|
|
void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
|
|
|
|
|
2002-07-27 01:12:15 +00:00
|
|
|
void print(std::ostream &O) const;
|
2001-11-26 18:41:20 +00:00
|
|
|
private:
|
|
|
|
friend class LoopInfo;
|
2002-09-26 16:15:19 +00:00
|
|
|
inline Loop(BasicBlock *BB) : ParentLoop(0) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-26 05:32:43 +00:00
|
|
|
/// LoopInfo - This class builds and contains all of the top level loop
|
|
|
|
/// structures in the specified function.
|
|
|
|
///
|
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;
|
2002-09-26 05:32:43 +00:00
|
|
|
friend class Loop;
|
2001-11-26 18:41:20 +00:00
|
|
|
public:
|
2002-04-09 05:43:19 +00:00
|
|
|
~LoopInfo() { releaseMemory(); }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2002-09-26 05:32:43 +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-06-25 16:13:24 +00:00
|
|
|
const Loop *getLoopFor(const BasicBlock *BB) const {
|
|
|
|
std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
|
2001-11-26 18:41:20 +00:00
|
|
|
return I != BBMap.end() ? I->second : 0;
|
|
|
|
}
|
2002-09-26 05:32:43 +00:00
|
|
|
|
|
|
|
/// operator[] - same as getLoopFor...
|
|
|
|
///
|
2002-06-25 16:13:24 +00:00
|
|
|
inline const Loop *operator[](const BasicBlock *BB) const {
|
2001-11-26 18:41:20 +00:00
|
|
|
return getLoopFor(BB);
|
|
|
|
}
|
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// getLoopDepth - Return the loop nesting level of the specified block...
|
|
|
|
///
|
2002-06-25 16:13:24 +00:00
|
|
|
unsigned getLoopDepth(const 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
|
|
|
#endif
|
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// runOnFunction - Calculate the natural loop information.
|
|
|
|
///
|
2002-06-25 16:13:24 +00:00
|
|
|
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-07-27 01:12:15 +00:00
|
|
|
void print(std::ostream &O) const;
|
2002-04-09 05:43:19 +00:00
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// getAnalysisUsage - Requires dominator sets
|
|
|
|
///
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2002-08-26 16:45:19 +00:00
|
|
|
static void stub(); // Noop
|
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
|
|
|
};
|
|
|
|
|
2002-08-26 16:45:19 +00:00
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
// Make sure that any clients of this file link in LoopInfo.cpp
|
2002-08-26 16:45:19 +00:00
|
|
|
static IncludeFile
|
|
|
|
LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);
|
|
|
|
|
2001-11-26 18:41:20 +00:00
|
|
|
#endif
|