-void DominatorTree::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
-#if !BALANCE_IDOM_TREE
- // Higher-complexity but faster implementation
- WInfo.Ancestor = V;
-#else
- // Lower-complexity but slower implementation
- BasicBlock *WLabel = WInfo.Label;
- unsigned WLabelSemi = Info[WLabel].Semi;
- BasicBlock *S = W;
- InfoRec *SInfo = &Info[S];
-
- BasicBlock *SChild = SInfo->Child;
- InfoRec *SChildInfo = &Info[SChild];
-
- while (WLabelSemi < Info[SChildInfo->Label].Semi) {
- BasicBlock *SChildChild = SChildInfo->Child;
- if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
- SChildInfo->Ancestor = S;
- SInfo->Child = SChild = SChildChild;
- SChildInfo = &Info[SChild];
- } else {
- SChildInfo->Size = SInfo->Size;
- S = SInfo->Ancestor = SChild;
- SInfo = SChildInfo;
- SChild = SChildChild;
- SChildInfo = &Info[SChild];
- }
- }
-
- InfoRec &VInfo = Info[V];
- SInfo->Label = WLabel;
-
- assert(V != W && "The optimization here will not work in this case!");
- unsigned WSize = WInfo.Size;
- unsigned VSize = (VInfo.Size += WSize);
-
- if (VSize < 2*WSize)
- std::swap(S, VInfo.Child);
-
- while (S) {
- SInfo = &Info[S];
- SInfo->Ancestor = V;
- S = SInfo->Child;
- }
-#endif
-}
-
-void DominatorTree::calculate(Function& F) {
- BasicBlock* Root = Roots[0];
-
- // Add a node for the root...
- DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
-
- Vertex.push_back(0);
-
- // Step #1: Number blocks in depth-first order and initialize variables used
- // in later stages of the algorithm.
- unsigned N = 0;
- for (unsigned i = 0, e = Roots.size(); i != e; ++i)
- N = DFSPass(Roots[i], Info[Roots[i]], 0);
-
- for (unsigned i = N; i >= 2; --i) {
- BasicBlock *W = Vertex[i];
- InfoRec &WInfo = Info[W];
-
- // Step #2: Calculate the semidominators of all vertices
- for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
- if (Info.count(*PI)) { // Only if this predecessor is reachable!
- unsigned SemiU = Info[Eval(*PI)].Semi;
- if (SemiU < WInfo.Semi)
- WInfo.Semi = SemiU;
- }
-
- Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
-
- 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) continue; // 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
- DomTreeNode *C = new DomTreeNode(I, IDomNode);
- DomTreeNodes[I] = 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) {
- if (!BBNode->getIDom())
- BBNode->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) {
- assert (!isPostDominator()
- && "This is not implemented for post dominators");
- return dominates(&A->getParent()->getEntryBlock(), A);
-}
-
-// dominates - Return true if A dominates B. THis performs the