//
// The LLVM Compiler Infrastructure
//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h"
#include <algorithm>
+#include <map>
#include <set>
namespace llvm {
/// inherit from.
///
template <class NodeT>
-class DominatorBase : public FunctionPass {
+class DominatorBase {
protected:
std::vector<NodeT*> Roots;
const bool IsPostDominators;
- inline DominatorBase(intptr_t ID, bool isPostDom) :
- FunctionPass(ID), Roots(), IsPostDominators(isPostDom) {}
+ inline explicit DominatorBase(bool isPostDom) :
+ Roots(), IsPostDominators(isPostDom) {}
public:
/// getRoots - Return the root blocks of the current CFG. This may include
return C;
}
+ size_t getNumChildren() const {
+ return Children.size();
+ }
+
void setIDom(DomTreeNodeBase<NodeT> *NewIDom) {
assert(IDom && "No immediate dominator?");
if (IDom != NewIDom) {
- std::vector<DomTreeNodeBase<BasicBlock>*>::iterator I =
+ typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I =
std::find(IDom->Children.begin(), IDom->Children.end(), this);
assert(I != IDom->Children.end() &&
"Not in immediate dominator children set!");
};
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
+EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
template<class NodeT>
static std::ostream &operator<<(std::ostream &o,
}
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
-typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
//===----------------------------------------------------------------------===//
/// DominatorTree - Calculate the immediate dominator tree for a function.
///
-template<class N, class GraphT>
-void Calculate(DominatorTreeBase<typename GraphT::NodeType>& DT,
- Function& F);
+template<class FuncT, class N>
+void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
+ FuncT& F);
template<class NodeT>
class DominatorTreeBase : public DominatorBase<NodeT> {
unsigned int SlowQueries;
// Information record used during immediate dominators computation.
struct InfoRec {
+ unsigned DFSNum;
unsigned Semi;
unsigned Size;
- NodeT *Label, *Parent, *Child, *Ancestor;
+ NodeT *Label, *Child;
+ unsigned Parent, Ancestor;
std::vector<NodeT*> Bucket;
- InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0) {}
+ InfoRec() : DFSNum(0), Semi(0), Size(0), Label(0), Child(0), Parent(0),
+ Ancestor(0) {}
};
DenseMap<NodeT*, NodeT*> IDoms;
bool NewBBDominatesNewBBSucc = true;
{
typename GraphT::NodeType* OnePred = PredBlocks[0];
- unsigned i = 1, e = PredBlocks.size();
+ size_t i = 1, e = PredBlocks.size();
for (i = 1; !DT.isReachableFromEntry(OnePred); ++i) {
assert(i != e && "Didn't find reachable pred?");
OnePred = PredBlocks[i];
// Find NewBB's immediate dominator and create new dominator tree node for
// NewBB.
- BasicBlock *NewBBIDom = 0;
+ NodeT *NewBBIDom = 0;
unsigned i = 0;
for (i = 0; i < PredBlocks.size(); ++i)
if (DT.isReachableFromEntry(PredBlocks[i])) {
assert(NewBBIDom && "No immediate dominator found??");
// Create the new dominator tree node... and set the idom of NewBB.
- DomTreeNode *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
+ DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
// If NewBB strictly dominates other blocks, then it is now the immediate
// dominator of NewBBSucc. Update the dominator tree as appropriate.
if (NewBBDominatesNewBBSucc) {
- DomTreeNode *NewBBSuccNode = DT.getNode(NewBBSucc);
+ DomTreeNodeBase<NodeT> *NewBBSuccNode = DT.getNode(NewBBSucc);
DT.changeImmediateDominator(NewBBSuccNode, NewBBNode);
}
}
public:
- DominatorTreeBase(intptr_t ID, bool isPostDom)
- : DominatorBase<NodeT>(ID, isPostDom), DFSInfoValid(false), SlowQueries(0) {}
- ~DominatorTreeBase() { reset(); }
+ explicit DominatorTreeBase(bool isPostDom)
+ : DominatorBase<NodeT>(isPostDom), DFSInfoValid(false), SlowQueries(0) {}
+ virtual ~DominatorTreeBase() { reset(); }
// FIXME: Should remove this
virtual bool runOnFunction(Function &F) { return false; }
/// isReachableFromEntry - Return true if A is dominated by the entry
/// block of the function containing it.
- const bool isReachableFromEntry(NodeT* A) {
+ bool isReachableFromEntry(NodeT* A) {
assert (!this->isPostDominator()
&& "This is not implemented for post dominators");
- return dominates(&A->getParent()->getEntryBlock(), A);
+ return dominates(&A->getParent()->front(), A);
}
/// dominates - Returns true iff A dominates B. Note that this is not a
&& "Two blocks are not in same function");
// If either A or B is a entry block then it is nearest common dominator.
- NodeT &Entry = A->getParent()->getEntryBlock();
+ NodeT &Entry = A->getParent()->front();
if (A == &Entry || B == &Entry)
return &Entry;
assert(IDomNode && "Not immediate dominator specified for block!");
DFSInfoValid = false;
return DomTreeNodes[BB] =
- IDomNode->addChild(new DomTreeNode(BB, IDomNode));
+ IDomNode->addChild(new DomTreeNodeBase<NodeT>(BB, IDomNode));
}
/// changeImmediateDominator - This method is used to update the dominator
///
virtual void print(std::ostream &o, const Module* ) const {
o << "=============================--------------------------------\n";
- o << "Inorder Dominator Tree: ";
+ if (this->isPostDominator())
+ o << "Inorder PostDominator Tree: ";
+ else
+ o << "Inorder Dominator Tree: ";
if (this->DFSInfoValid)
o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
o << "\n";
template<class GraphT>
friend void Link(DominatorTreeBase<typename GraphT::NodeType>& DT,
- typename GraphT::NodeType* V,
- typename GraphT::NodeType* W,
+ unsigned DFSNumV, typename GraphT::NodeType* W,
typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo);
template<class GraphT>
typename GraphT::NodeType* V,
unsigned N);
- template<class N, class GraphT>
- friend void Calculate(DominatorTreeBase<typename GraphT::NodeType>& DT,
- Function& F);
+ template<class FuncT, class N>
+ friend void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
+ FuncT& F);
/// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
/// dominator tree in dfs order.
SmallVector<std::pair<DomTreeNodeBase<NodeT>*,
typename DomTreeNodeBase<NodeT>::iterator>, 32> WorkStack;
- for (unsigned i = 0, e = this->Roots.size(); i != e; ++i) {
+ for (unsigned i = 0, e = (unsigned)this->Roots.size(); i != e; ++i) {
DomTreeNodeBase<NodeT> *ThisRoot = getNode(this->Roots[i]);
WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin()));
ThisRoot->DFSNumIn = DFSNum++;
// Haven't calculated this node yet? Get or calculate the node for the
// immediate dominator.
NodeT *IDom = getIDom(BB);
+
+ assert(IDom || this->DomTreeNodes[NULL]);
DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom);
// Add a new tree node for this BasicBlock, and link it as a child of
return I != IDoms.end() ? I->second : 0;
}
+ inline void addRoot(NodeT* BB) {
+ this->Roots.push_back(BB);
+ }
+
public:
/// recalculate - compute a dominator tree for the given function
- void recalculate(Function& F) {
+ template<class FT>
+ void recalculate(FT& F) {
if (!this->IsPostDominators) {
reset();
// Initialize roots
- this->Roots.push_back(&F.getEntryBlock());
- this->IDoms[&F.getEntryBlock()] = 0;
- this->DomTreeNodes[&F.getEntryBlock()] = 0;
+ this->Roots.push_back(&F.front());
+ this->IDoms[&F.front()] = 0;
+ this->DomTreeNodes[&F.front()] = 0;
this->Vertex.push_back(0);
- Calculate<NodeT*, GraphTraits<NodeT*> >(*this, F);
+ Calculate<FT, NodeT*>(*this, F);
updateDFSNumbers();
} else {
reset(); // Reset from the last time we were run...
// Initialize the roots list
- for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
- TerminatorInst *Insn = I->getTerminator();
- if (Insn->getNumSuccessors() == 0) {
- // Unreachable block is not a root node.
- if (!isa<UnreachableInst>(Insn))
- this->Roots.push_back(I);
- }
+ for (typename FT::iterator I = F.begin(), E = F.end(); I != E; ++I) {
+ if (std::distance(GraphTraits<FT*>::child_begin(I),
+ GraphTraits<FT*>::child_end(I)) == 0)
+ addRoot(I);
// Prepopulate maps so that we don't get iterator invalidation issues later.
this->IDoms[I] = 0;
this->Vertex.push_back(0);
- Calculate<Inverse<NodeT*>, GraphTraits<Inverse<NodeT*> > >(*this, F);
+ Calculate<FT, Inverse<NodeT*> >(*this, F);
}
}
};
DominatorTreeBase<BasicBlock>* DT;
DominatorTree() : FunctionPass(intptr_t(&ID)) {
- DT = new DominatorTreeBase<BasicBlock>(intptr_t(&ID), false);
+ DT = new DominatorTreeBase<BasicBlock>(false);
}
~DominatorTree() {
delete DT;
}
+ DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
+
/// getRoots - Return the root blocks of the current CFG. This may include
/// multiple blocks if we are computing post dominators. For forward
/// dominators, this will always be a single block (the entry node).
/// DominanceFrontierBase - Common base class for computing forward and inverse
/// dominance frontiers for a function.
///
-class DominanceFrontierBase : public DominatorBase<BasicBlock> {
+class DominanceFrontierBase : public FunctionPass {
public:
typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
protected:
DomSetMapType Frontiers;
+ std::vector<BasicBlock*> Roots;
+ const bool IsPostDominators;
+
public:
DominanceFrontierBase(intptr_t ID, bool isPostDom)
- : DominatorBase<BasicBlock>(ID, isPostDom) {}
+ : FunctionPass(ID), IsPostDominators(isPostDom) {}
+
+ /// getRoots - Return the root blocks of the current CFG. This may include
+ /// multiple blocks if we are computing post dominators. For forward
+ /// dominators, this will always be a single block (the entry node).
+ ///
+ inline const std::vector<BasicBlock*> &getRoots() const { return Roots; }
+
+ /// isPostDominator - Returns true if analysis based of postdoms
+ ///
+ bool isPostDominator() const { return IsPostDominators; }
virtual void releaseMemory() { Frontiers.clear(); }