-//===----------------------------------------------------------------------===//
-// PostDominatorTree Implementation
-//===----------------------------------------------------------------------===//
-
-static RegisterAnalysis<PostDominatorTree>
-F("postdomtree", "Post-Dominator Tree Construction", true);
-
-void PostDominatorTree::calculate(const PostDominatorSet &DS) {
- if (Roots.empty()) return;
- BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
-
- Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
-
- // Iterate over all nodes in depth first order...
- for (unsigned i = 0, e = Roots.size(); i != e; ++i)
- for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
- E = idf_end(Roots[i]); I != E; ++I) {
- BasicBlock *BB = *I;
- const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
- unsigned DomSetSize = Dominators.size();
- if (DomSetSize == 1) continue; // Root node... IDom = null
-
- // If we have already computed the immediate dominator for this node,
- // don't revisit. This can happen due to nodes reachable from multiple
- // roots, but which the idf_iterator doesn't know about.
- if (Nodes.find(BB) != Nodes.end()) continue;
-
- // Loop over all dominators of this node. This corresponds to looping
- // over nodes in the dominator chain, looking for a node whose dominator
- // set is equal to the current nodes, except that the current node does
- // not exist in it. This means that it is one level higher in the dom
- // chain than the current node, and it is our idom! We know that we have
- // already added a DominatorTree node for our idom, because the idom must
- // be a predecessor in the depth first order that we are iterating through
- // the function.
- //
- DominatorSet::DomSetType::const_iterator I = Dominators.begin();
- DominatorSet::DomSetType::const_iterator End = Dominators.end();
- for (; I != End; ++I) { // Iterate over dominators...
- // All of our dominators should form a chain, where the number
- // of elements in the dominator set indicates what level the
- // node is at in the chain. We want the node immediately
- // above us, so it will have an identical dominator set,
- // except that BB will not dominate it... therefore it's
- // dominator set size will be one less than BB's...
- //
- if (DS.getDominators(*I).size() == DomSetSize - 1) {
- // We know that the immediate dominator should already have a node,
- // because we are traversing the CFG in depth first order!
- //
- Node *IDomNode = Nodes[*I];
- assert(IDomNode && "No node for IDOM?");
-
- // Add a new tree node for this BasicBlock, and link it as a child of
- // IDomNode
- Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
- break;
- }
- }
- }