- BasicBlock *WParent = WInfo.Parent;
- Link(WParent, W, WInfo);
-
- // Step #3: Implicitly define the immediate dominator of vertices
- std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
- while (!WParentBucket.empty()) {
- BasicBlock *V = WParentBucket.back();
- WParentBucket.pop_back();
- BasicBlock *U = Eval(V);
- IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
- }
- }
-
- // Step #4: Explicitly define the immediate dominator of each vertex
- for (unsigned i = 2; i <= N; ++i) {
- BasicBlock *W = Vertex[i];
- BasicBlock *&WIDom = IDoms[W];
- if (WIDom != Vertex[Info[W].Semi])
- WIDom = IDoms[WIDom];
- }
-
- // Loop over all of the reachable blocks in the function...
- for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
- if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block.
- DomTreeNode *&BBNode = DomTreeNodes[I];
- if (!BBNode) { // Haven't calculated this node yet?
- // Get or calculate the node for the immediate dominator
- DomTreeNode *IDomNode = getNodeForBlock(ImmDom);
-
- // Add a new tree node for this BasicBlock, and link it as a child of
- // IDomNode
- ETNode *ET = new ETNode(I);
- ETNodes[I] = ET;
- DomTreeNode *C = new DomTreeNode(I, IDomNode, ET);
- DomTreeNodes[I] = C;
- BBNode = IDomNode->addChild(C);
- }
- }
-
- // Free temporary memory used to construct idom's
- Info.clear();
- IDoms.clear();
- std::vector<BasicBlock*>().swap(Vertex);
-
- updateDFSNumbers();
-}
-
-void DominatorTreeBase::updateDFSNumbers()
-{
- int dfsnum = 0;
- // Iterate over all nodes in depth first order.
- for (unsigned i = 0, e = Roots.size(); i != e; ++i)
- for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
- E = df_end(Roots[i]); I != E; ++I) {
- BasicBlock *BB = *I;
- DomTreeNode *BBNode = getNode(BB);
- if (BBNode) {
- ETNode *ETN = BBNode->getETNode();
- if (ETN && !ETN->hasFather())
- ETN->assignDFSNumber(dfsnum);
- }
- }
- SlowQueries = 0;
- DFSInfoValid = true;
-}
-
-/// isReachableFromEntry - Return true if A is dominated by the entry
-/// block of the function containing it.
-const bool DominatorTreeBase::isReachableFromEntry(BasicBlock* A) {
- return dominates(&A->getParent()->getEntryBlock(), A);
-}
-
-// dominates - Return true if A dominates B. THis performs the
-// special checks necessary if A and B are in the same basic block.
-bool DominatorTreeBase::dominates(Instruction *A, Instruction *B) {
- BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
- if (BBA != BBB) return dominates(BBA, BBB);
-
- // It is not possible to determine dominance between two PHI nodes
- // based on their ordering.
- if (isa<PHINode>(A) && isa<PHINode>(B))
- return false;
-
- // Loop through the basic block until we find A or B.
- BasicBlock::iterator I = BBA->begin();
- for (; &*I != A && &*I != B; ++I) /*empty*/;
-
- if(!IsPostDominators) {
- // A dominates B if it is found first in the basic block.
- return &*I == A;
- } else {
- // A post-dominates B if B is found first in the basic block.
- return &*I == B;
- }
-}
-
-// DominatorTreeBase::reset - Free all of the tree node memory.
-//
-void DominatorTreeBase::reset() {
- for (DomTreeNodeMapType::iterator I = DomTreeNodes.begin(), E = DomTreeNodes.end(); I != E; ++I)
- delete I->second;
- DomTreeNodes.clear();
- IDoms.clear();
- Roots.clear();
- Vertex.clear();
- RootNode = 0;
-}
-
-/// findNearestCommonDominator - Find nearest common dominator basic block
-/// for basic block A and B. If there is no such block then return NULL.
-BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A, BasicBlock *B) {
-
- assert (!isPostDominator() && "This is not implemented for post dominators");
- assert (A->getParent() == B->getParent() && "Two blocks are not in same function");
-
- // If either A or B is a entry block then it is nearest common dominator.
- BasicBlock &Entry = A->getParent()->getEntryBlock();
- if (A == &Entry || B == &Entry)
- return &Entry;
-
- // If A and B are same then A is nearest common dominator.
- DomTreeNode *NodeA = getNode(A);
- if (A != 0 && A == B)
- return A;
-
- DomTreeNode *NodeB = getNode(B);
-
- // Collect NodeA dominators set.
- std::set<DomTreeNode *> NodeADoms;
- NodeADoms.insert(NodeA);
- DomTreeNode *IDomA = NodeA->getIDom();
- while(IDomA) {
- NodeADoms.insert(IDomA);
- IDomA = IDomA->getIDom();
- }
-
- // If B dominates A then B is nearest common dominator.
- if (NodeADoms.count(NodeB) != 0)
- return B;
-
- // Walk NodeB immediate dominators chain and find common dominator node.
- DomTreeNode *IDomB = NodeB->getIDom();
- while(IDomB) {
- if (NodeADoms.count(IDomB) != 0)
- return IDomB->getBlock();
-
- IDomB = IDomB->getIDom();
- }
-
- return NULL;
-}
-
-void DomTreeNode::setIDom(DomTreeNode *NewIDom) {
- assert(IDom && "No immediate dominator?");
- if (IDom != NewIDom) {
- std::vector<DomTreeNode*>::iterator I =
- std::find(IDom->Children.begin(), IDom->Children.end(), this);
- assert(I != IDom->Children.end() &&
- "Not in immediate dominator children set!");
- // I am no longer your child...
- IDom->Children.erase(I);
-
- // Switch to new dominator
- IDom = NewIDom;
- IDom->Children.push_back(this);
-
- if (!ETN->hasFather())
- ETN->setFather(IDom->getETNode());
- else if (ETN->getFather()->getData<BasicBlock>() != IDom->getBlock()) {
- ETN->Split();
- ETN->setFather(IDom->getETNode());
- }
- }
-}
-
-DomTreeNode *DominatorTree::getNodeForBlock(BasicBlock *BB) {
- DomTreeNode *&BBNode = DomTreeNodes[BB];
- if (BBNode) return BBNode;
-
- // Haven't calculated this node yet? Get or calculate the node for the
- // immediate dominator.
- BasicBlock *IDom = getIDom(BB);
- DomTreeNode *IDomNode = getNodeForBlock(IDom);
-
- // Add a new tree node for this BasicBlock, and link it as a child of
- // IDomNode
- ETNode *ET = new ETNode(BB);
- ETNodes[BB] = ET;
- DomTreeNode *C = new DomTreeNode(BB, IDomNode, ET);
- DomTreeNodes[BB] = C;
- return BBNode = IDomNode->addChild(C);
-}
-
-static std::ostream &operator<<(std::ostream &o,
- const DomTreeNode *Node) {
- if (Node->getBlock())
- WriteAsOperand(o, Node->getBlock(), false);
- else
- o << " <<exit node>>";
- return o << "\n";
-}
-
-static void PrintDomTree(const DomTreeNode *N, std::ostream &o,
- unsigned Lev) {
- o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
- for (DomTreeNode::const_iterator I = N->begin(), E = N->end();
- I != E; ++I)
- PrintDomTree(*I, o, Lev+1);
-}
-
-void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
- o << "=============================--------------------------------\n"
- << "Inorder Dominator Tree:\n";
- PrintDomTree(getRootNode(), o, 1);
-}
-
-void DominatorTreeBase::dump() {
- print (llvm::cerr);
-}