#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 {
protected:
std::vector<NodeT*> Roots;
const bool IsPostDominators;
- inline DominatorBase(bool isPostDom) :
+ inline explicit DominatorBase(bool isPostDom) :
Roots(), IsPostDominators(isPostDom) {}
public:
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) {
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;
PE = GraphTraits<Inverse<N> >::child_end(NewBB); PI != PE; ++PI)
PredBlocks.push_back(*PI);
- assert(!PredBlocks.empty() && "No predblocks??");
-
- // The newly inserted basic block will dominate existing basic blocks iff the
- // PredBlocks dominate all of the non-pred blocks. If all predblocks dominate
- // the non-pred blocks, then they all must be the same block!
- //
- bool NewBBDominatesNewBBSucc = true;
- {
- typename GraphT::NodeType* OnePred = PredBlocks[0];
- unsigned i = 1, e = PredBlocks.size();
- for (i = 1; !DT.isReachableFromEntry(OnePred); ++i) {
- assert(i != e && "Didn't find reachable pred?");
- OnePred = PredBlocks[i];
- }
+ assert(!PredBlocks.empty() && "No predblocks??");
- for (; i != e; ++i)
- if (PredBlocks[i] != OnePred && DT.isReachableFromEntry(OnePred)) {
- NewBBDominatesNewBBSucc = false;
- break;
- }
-
- if (NewBBDominatesNewBBSucc)
- for (typename GraphTraits<Inverse<N> >::ChildIteratorType PI =
- GraphTraits<Inverse<N> >::child_begin(NewBBSucc),
- E = GraphTraits<Inverse<N> >::child_end(NewBBSucc); PI != E; ++PI)
- if (*PI != NewBB && !DT.dominates(NewBBSucc, *PI)) {
- NewBBDominatesNewBBSucc = false;
- break;
- }
- }
-
- // The other scenario where the new block can dominate its successors are when
- // all predecessors of NewBBSucc that are not NewBB are dominated by NewBBSucc
- // already.
- if (!NewBBDominatesNewBBSucc) {
- NewBBDominatesNewBBSucc = true;
- for (typename GraphTraits<Inverse<N> >::ChildIteratorType PI =
- GraphTraits<Inverse<N> >::child_begin(NewBBSucc),
- E = GraphTraits<Inverse<N> >::child_end(NewBBSucc); PI != E; ++PI)
- if (*PI != NewBB && !DT.dominates(NewBBSucc, *PI)) {
- NewBBDominatesNewBBSucc = false;
- break;
- }
- }
+ bool NewBBDominatesNewBBSucc = true;
+ for (typename GraphTraits<Inverse<N> >::ChildIteratorType PI =
+ GraphTraits<Inverse<N> >::child_begin(NewBBSucc),
+ E = GraphTraits<Inverse<N> >::child_end(NewBBSucc); PI != E; ++PI)
+ if (*PI != NewBB && !DT.dominates(NewBBSucc, *PI) &&
+ DT.isReachableFromEntry(*PI)) {
+ NewBBDominatesNewBBSucc = false;
+ break;
+ }
// Find NewBB's immediate dominator and create new dominator tree node for
// NewBB.
NewBBIDom = PredBlocks[i];
break;
}
- assert(i != PredBlocks.size() && "No reachable preds?");
+
+ // It's possible that none of the predecessors of NewBB are reachable;
+ // in that case, NewBB itself is unreachable, so nothing needs to be
+ // changed.
+ if (!NewBBIDom)
+ return;
+
for (i = i + 1; i < PredBlocks.size(); ++i) {
if (DT.isReachableFromEntry(PredBlocks[i]))
NewBBIDom = DT.findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
}
- assert(NewBBIDom && "No immediate dominator found??");
// Create the new dominator tree node... and set the idom of NewBB.
DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
}
public:
- DominatorTreeBase(bool isPostDom)
+ 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
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>
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
}
inline void addRoot(NodeT* BB) {
- // Unreachable block is not a root node.
- if (!isa<UnreachableInst>(&BB->back()))
- this->Roots.push_back(BB);
+ this->Roots.push_back(BB);
}
public:
static char ID; // Pass ID, replacement for typeid
DominatorTreeBase<BasicBlock>* DT;
- DominatorTree() : FunctionPass(intptr_t(&ID)) {
+ DominatorTree() : FunctionPass(&ID) {
DT = new DominatorTreeBase<BasicBlock>(false);
}
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();
typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
protected:
DomSetMapType Frontiers;
- std::vector<BasicBlock*> Roots;
- const bool IsPostDominators;
+ std::vector<BasicBlock*> Roots;
+ const bool IsPostDominators;
public:
- DominanceFrontierBase(intptr_t ID, bool isPostDom)
+ DominanceFrontierBase(void *ID, bool isPostDom)
: FunctionPass(ID), IsPostDominators(isPostDom) {}
/// getRoots - Return the root blocks of the current CFG. This may include
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; ) {
+ 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; ) {
+ BasicBlock *Node = I->first;
+ const_iterator DFI = find(Node);
+ if (DFI == end())
+ return true;
+
+ if (compareDomSet(I->second, DFI->second))
+ return true;
+
+ ++I;
+ 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);
};