//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
// This file implements the post-dominator construction algorithms.
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
Doms[I]; // Initialize to empty
- if (isa<ReturnInst>(I->getTerminator()) ||
- isa<UnwindInst>(I->getTerminator()))
+ if (succ_begin(I) == succ_end(I))
Roots.push_back(I);
}
//
while (Doms[*SI].size() == 0) ++SI;
WorkingSet = Doms[*SI];
-
+
for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
DomSetType &SuccSet = Doms[*SI];
if (SuccSet.size())
if (Roots.size() > 1)
WorkingSet.insert(0);
}
-
+
WorkingSet.insert(BB); // A block always dominates itself
DomSetType &BBSet = Doms[BB];
if (BBSet != WorkingSet) {
// Loop over all of the nodes that have dominators... figuring out the IDOM
// for each node...
//
- for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
+ for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
DI != DEnd; ++DI) {
BasicBlock *BB = DI->first;
const DominatorSet::DomSetType &Dominators = DI->second;
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
+ // 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) {
- IDoms[BB] = *I;
- break;
+ IDoms[BB] = *I;
+ break;
}
}
}
// 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...
+ for (DominatorSet::DomSetType::const_iterator I = Dominators.begin(),
+ E = Dominators.end(); I != E; ++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
// 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,
+ // 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));
}
}
}
+//===----------------------------------------------------------------------===//
+// PostETForest Implementation
+//===----------------------------------------------------------------------===//
+
+static RegisterAnalysis<PostETForest>
+G("postetforest", "Post-ET-Forest Construction", true);
+
+ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
+ ETNode *&BBNode = Nodes[BB];
+ if (BBNode) return BBNode;
+
+ // Haven't calculated this node yet? Get or calculate the node for the
+ // immediate dominator.
+ BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
+
+ // If we are unreachable, we may not have an immediate dominator.
+ if (!IDom)
+ return BBNode = new ETNode(BB);
+ else {
+ ETNode *IDomNode = getNodeForBlock(IDom);
+
+ // Add a new tree node for this BasicBlock, and link it as a child of
+ // IDomNode
+ BBNode = new ETNode(BB);
+ BBNode->setFather(IDomNode);
+ return BBNode;
+ }
+}
+
+void PostETForest::calculate(const ImmediatePostDominators &ID) {
+ for (unsigned i = 0, e = Roots.size(); i != e; ++i)
+ Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
+
+ // Iterate over all nodes in inverse 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;
+ ETNode *&BBNode = Nodes[BB];
+ if (!BBNode) {
+ ETNode *IDomNode = NULL;
+
+ if (ID.get(BB))
+ IDomNode = getNodeForBlock(ID.get(BB));
+
+ // Add a new ETNode for this BasicBlock, and set it's parent
+ // to it's immediate dominator.
+ BBNode = new ETNode(BB);
+ if (IDomNode)
+ BBNode->setFather(IDomNode);
+ }
+ }
+
+ int dfsnum = 0;
+ // 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) {
+ if (!getNodeForBlock(*I)->hasFather())
+ getNodeForBlock(*I)->assignDFSNumber(dfsnum);
+ }
+ DFSInfoValid = true;
+}
//===----------------------------------------------------------------------===//
// PostDominanceFrontier Implementation
H("postdomfrontier", "Post-Dominance Frontier Construction", true);
const DominanceFrontier::DomSetType &
-PostDominanceFrontier::calculate(const PostDominatorTree &DT,
+PostDominanceFrontier::calculate(const PostDominatorTree &DT,
const DominatorTree::Node *Node) {
// Loop over CFG successors to calculate DFlocal[Node]
BasicBlock *BB = Node->getBlock();
DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
for (; CDFI != CDFE; ++CDFI) {
- if (!Node->dominates(DT[*CDFI]))
- S.insert(*CDFI);
+ if (!Node->properlyDominates(DT[*CDFI]))
+ S.insert(*CDFI);
}
}