From: Devang Patel Date: Tue, 12 Jun 2007 00:14:41 +0000 (+0000) Subject: Maintain DFS number in DomTreeNode itself. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=3726b82a55a1864c2f9af86beaaeb2e1f3fbc99b;p=oota-llvm.git Maintain DFS number in DomTreeNode itself. This means now ETNodes are not useful anymore. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37546 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Analysis/Dominators.h b/include/llvm/Analysis/Dominators.h index 755407b40d9..f2d6da53e1e 100644 --- a/include/llvm/Analysis/Dominators.h +++ b/include/llvm/Analysis/Dominators.h @@ -65,6 +65,8 @@ class DomTreeNode { DomTreeNode *IDom; ETNode *ETN; std::vector Children; + int DFSNumIn, DFSNumOut; + public: typedef std::vector::iterator iterator; typedef std::vector::const_iterator const_iterator; @@ -80,12 +82,22 @@ public: inline const std::vector &getChildren() const { return Children; } inline DomTreeNode(BasicBlock *BB, DomTreeNode *iDom, ETNode *E) - : TheBB(BB), IDom(iDom), ETN(E) { + : TheBB(BB), IDom(iDom), ETN(E), DFSNumIn(-1), DFSNumOut(-1) { if (IDom) ETN->setFather(IDom->getETNode()); } inline DomTreeNode *addChild(DomTreeNode *C) { Children.push_back(C); return C; } void setIDom(DomTreeNode *NewIDom); + + // Return true if this node is dominated by other. Use this only if DFS info is valid. + bool DominatedBy(const DomTreeNode *other) const { + return this->DFSNumIn >= other->DFSNumIn && + this->DFSNumOut <= other->DFSNumOut; + } + + /// assignDFSNumber - Assign In and Out numbers while walking dominator tree + /// in dfs order. + void assignDFSNumber(int num); }; //===----------------------------------------------------------------------===// @@ -214,14 +226,16 @@ protected: ETNode *NodeB = B->getETNode(); if (DFSInfoValid) - return NodeB->DominatedBy(NodeA); + return B->DominatedBy(A); + //return NodeB->DominatedBy(NodeA); // If we end up with too many slow queries, just update the // DFS numbers on the theory that we are going to keep querying. SlowQueries++; if (SlowQueries > 32) { updateDFSNumbers(); - return NodeB->DominatedBy(NodeA); + return B->DominatedBy(A); + //return NodeB->DominatedBy(NodeA); } //return NodeB->DominatedBySlow(NodeA); return dominatedBySlowTreeWalk(A, B); diff --git a/lib/VMCore/Dominators.cpp b/lib/VMCore/Dominators.cpp index 8b2c1a45e22..225d8d200ae 100644 --- a/lib/VMCore/Dominators.cpp +++ b/lib/VMCore/Dominators.cpp @@ -319,9 +319,11 @@ void DominatorTreeBase::updateDFSNumbers() BasicBlock *BB = *I; DomTreeNode *BBNode = getNode(BB); if (BBNode) { - ETNode *ETN = BBNode->getETNode(); - if (ETN && !ETN->hasFather()) - ETN->assignDFSNumber(dfsnum); + if (!BBNode->getIDom()) + BBNode->assignDFSNumber(dfsnum); + //ETNode *ETN = BBNode->getETNode(); + //if (ETN && !ETN->hasFather()) + // ETN->assignDFSNumber(dfsnum); } } SlowQueries = 0; @@ -414,6 +416,38 @@ BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A, BasicBl return NULL; } +/// assignDFSNumber - Assign In and Out numbers while walking dominator tree +/// in dfs order. +void DomTreeNode::assignDFSNumber(int num) { + std::vector workStack; + std::set visitedNodes; + + workStack.push_back(this); + visitedNodes.insert(this); + this->DFSNumIn = num++; + + while (!workStack.empty()) { + DomTreeNode *Node = workStack.back(); + + bool visitChild = false; + for (std::vector::iterator DI = Node->begin(), + E = Node->end(); DI != E && !visitChild; ++DI) { + DomTreeNode *Child = *DI; + if (visitedNodes.count(Child) == 0) { + visitChild = true; + Child->DFSNumIn = num++; + workStack.push_back(Child); + visitedNodes.insert(Child); + } + } + if (!visitChild) { + // If we reach here means all children are visited + Node->DFSNumOut = num++; + workStack.pop_back(); + } + } +} + void DomTreeNode::setIDom(DomTreeNode *NewIDom) { assert(IDom && "No immediate dominator?"); if (IDom != NewIDom) {