//
// 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/Pass.h"
#include "llvm/BasicBlock.h"
#include "llvm/Function.h"
-#include "llvm/Instruction.h"
#include "llvm/Instructions.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/GraphTraits.h"
#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
const std::vector<DomTreeNodeBase<NodeT>*> &getChildren() const {
return Children;
}
-
+
DomTreeNodeBase(NodeT *BB, DomTreeNodeBase<NodeT> *iDom)
: TheBB(BB), IDom(iDom), DFSNumIn(-1), DFSNumOut(-1) { }
Children.push_back(C);
return C;
}
+
+ size_t getNumChildren() const {
+ return Children.size();
+ }
+
+ void clearAllChildren() {
+ Children.clear();
+ }
+ bool compare(DomTreeNodeBase<NodeT> *Other) {
+ if (getNumChildren() != Other->getNumChildren())
+ return true;
+
+ SmallPtrSet<NodeT *, 4> OtherChildren;
+ for(iterator I = Other->begin(), E = Other->end(); I != E; ++I) {
+ NodeT *Nd = (*I)->getBlock();
+ OtherChildren.insert(Nd);
+ }
+
+ for(iterator I = begin(), E = end(); I != E; ++I) {
+ NodeT *N = (*I)->getBlock();
+ if (OtherChildren.count(N) == 0)
+ return true;
+ }
+ return false;
+ }
+
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; }
+ /// compare - Return false if the other dominator tree base matches this
+ /// dominator tree base. Otherwise return true.
+ bool compare(DominatorTreeBase &Other) const {
+
+ const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes;
+ if (DomTreeNodes.size() != OtherDomTreeNodes.size())
+ return true;
+
+ SmallPtrSet<const NodeT *,4> MyBBs;
+ for (typename DomTreeNodeMapType::const_iterator
+ I = this->DomTreeNodes.begin(),
+ E = this->DomTreeNodes.end(); I != E; ++I) {
+ NodeT *BB = I->first;
+ typename DomTreeNodeMapType::const_iterator OI = OtherDomTreeNodes.find(BB);
+ if (OI == OtherDomTreeNodes.end())
+ return true;
+
+ DomTreeNodeBase<NodeT>* MyNd = I->second;
+ DomTreeNodeBase<NodeT>* OtherNd = OI->second;
+
+ if (MyNd->compare(OtherNd))
+ return true;
+ }
+
+ return false;
+ }
+
virtual void releaseMemory() { reset(); }
/// getNode - return the (Post)DominatorTree node for the specified basic
/// 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);
}
}
};
static char ID; // Pass ID, replacement for typeid
DominatorTreeBase<BasicBlock>* DT;
- DominatorTree() : FunctionPass(intptr_t(&ID)) {
- DT = new DominatorTreeBase<BasicBlock>(intptr_t(&ID), false);
+ DominatorTree() : FunctionPass(&ID) {
+ 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).
inline DomTreeNode *getRootNode() const {
return DT->getRootNode();
}
-
+
+ /// compare - Return false if the other dominator tree matches this
+ /// dominator tree. Otherwise return true.
+ inline bool compare(DominatorTree &Other) const {
+ DomTreeNode *R = getRootNode();
+ DomTreeNode *OtherR = Other.getRootNode();
+
+ if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
+ return true;
+
+ if (DT->compare(Other.getBase()))
+ return true;
+
+ return false;
+ }
+
virtual bool runOnFunction(Function &F);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
DT->splitBlock(NewBB);
}
+ bool isReachableFromEntry(BasicBlock* A) {
+ return DT->isReachableFromEntry(A);
+ }
+
virtual void releaseMemory() {
DT->releaseMemory();
/// 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) {}
+ DominanceFrontierBase(void *ID, bool 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(); }
I->second.erase(Node);
}
+ /// compareDomSet - Return false if two domsets match. Otherwise
+ /// return true;
+ bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const {
+ std::set<BasicBlock *> tmpSet;
+ for (DomSetType::const_iterator I = DS2.begin(),
+ E = DS2.end(); I != E; ++I)
+ tmpSet.insert(*I);
+
+ for (DomSetType::const_iterator I = DS1.begin(),
+ E = DS1.end(); I != E; ++I) {
+ BasicBlock *Node = *I;
+
+ if (tmpSet.erase(Node) == 0)
+ // Node is in DS1 but not in DS2.
+ return true;
+ }
+
+ if(!tmpSet.empty())
+ // There are nodes that are in DS2 but not in DS1.
+ return true;
+
+ // DS1 and DS2 matches.
+ return false;
+ }
+
+ /// compare - Return true if the other dominance frontier base matches
+ /// this dominance frontier base. Otherwise return false.
+ bool compare(DominanceFrontierBase &Other) const {
+ DomSetMapType tmpFrontiers;
+ for (DomSetMapType::const_iterator I = Other.begin(),
+ E = Other.end(); I != E; ++I)
+ tmpFrontiers.insert(std::make_pair(I->first, I->second));
+
+ for (DomSetMapType::iterator I = tmpFrontiers.begin(),
+ E = tmpFrontiers.end(); I != E; ++I) {
+ BasicBlock *Node = I->first;
+ const_iterator DFI = find(Node);
+ if (DFI == end())
+ return true;
+
+ if (compareDomSet(I->second, DFI->second))
+ return true;
+
+ tmpFrontiers.erase(Node);
+ }
+
+ if (!tmpFrontiers.empty())
+ return true;
+
+ return false;
+ }
+
/// print - Convert to human readable form
///
virtual void print(std::ostream &OS, const Module* = 0) const;
public:
static char ID; // Pass ID, replacement for typeid
DominanceFrontier() :
- DominanceFrontierBase(intptr_t(&ID), false) {}
+ DominanceFrontierBase(&ID, false) {}
BasicBlock *getRoot() const {
assert(Roots.size() == 1 && "Should always have entry node!");
// itself is not member of NewBB's dominance frontier.
DominanceFrontier::iterator NewDFI = find(NewBB);
DominanceFrontier::iterator DFI = find(BB);
+ // If BB was an entry block then its frontier is empty.
+ if (DFI == end())
+ return;
DominanceFrontier::DomSetType BBSet = DFI->second;
for (DominanceFrontier::DomSetType::iterator BBSetI = BBSet.begin(),
BBSetE = BBSet.end(); BBSetI != BBSetE; ++BBSetI) {
NewDFI->second.erase(BB);
}
-private:
const DomSetType &calculate(const DominatorTree &DT,
const DomTreeNode *Node);
};