Rework post dominator information so that we do not have to

unify all exit nodes of a function to compute post-dominance information.
This does not work with functions that have both unwind and return nodes,
because we cannot unify these blocks.  The new implementation is better
anyway. :)

llvm-svn: 8460
This commit is contained in:
Chris Lattner 2003-09-10 20:37:08 +00:00
parent 2dcab00d57
commit 8157505fae
1 changed files with 95 additions and 86 deletions

View File

@ -5,7 +5,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Analysis/PostDominators.h" #include "llvm/Analysis/PostDominators.h"
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" #include "llvm/iTerminators.h"
#include "llvm/Support/CFG.h" #include "llvm/Support/CFG.h"
#include "Support/DepthFirstIterator.h" #include "Support/DepthFirstIterator.h"
#include "Support/SetOperations.h" #include "Support/SetOperations.h"
@ -23,75 +23,77 @@ B("postdomset", "Post-Dominator Set Construction", true);
// //
bool PostDominatorSet::runOnFunction(Function &F) { bool PostDominatorSet::runOnFunction(Function &F) {
Doms.clear(); // Reset from the last time we were run... Doms.clear(); // Reset from the last time we were run...
// Since we require that the unify all exit nodes pass has been run, we know
// that there can be at most one return instruction in the function left.
// Get it.
//
Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
if (Root == 0) { // No exit node for the function? Postdomsets are all empty // Scan the function looking for the root nodes of the post-dominance
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) // relationships. These blocks end with return and unwind instructions.
Doms[FI] = DomSetType(); // While we are iterating over the function, we also initialize all of the
return false; // domsets to empty.
Roots.clear();
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()))
Roots.push_back(I);
} }
// If there are no exit nodes for the function, postdomsets are all empty.
// This can happen if the function just contains an infinite loop, for
// example.
if (Roots.empty()) return false;
// If we have more than one root, we insert an artificial "null" exit, which
// has "virtual edges" to each of the real exit nodes.
if (Roots.size() > 1)
Doms[0].insert(0);
bool Changed; bool Changed;
do { do {
Changed = false; Changed = false;
std::set<const BasicBlock*> Visited; std::set<const BasicBlock*> Visited;
DomSetType WorkingSet; DomSetType WorkingSet;
idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
for ( ; It != End; ++It) {
BasicBlock *BB = *It;
succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
if (PI != PEnd) { // Is there SOME predecessor?
// Loop until we get to a successor that has had it's dom set filled
// in at least once. We are guaranteed to have this because we are
// traversing the graph in DFO and have handled start nodes specially.
//
while (Doms[*PI].size() == 0) ++PI;
WorkingSet = Doms[*PI];
for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets for (unsigned i = 0, e = Roots.size(); i != e; ++i)
DomSetType &PredSet = Doms[*PI]; for (idf_iterator<BasicBlock*> It = idf_begin(Roots[i]),
if (PredSet.size()) E = idf_end(Roots[i]); It != E; ++It) {
set_intersect(WorkingSet, PredSet); BasicBlock *BB = *It;
} succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
} else if (BB != Root) { if (SI != SE) { // Is there SOME successor?
// If this isn't the root basic block and it has no successors, it must // Loop until we get to a successor that has had it's dom set filled
// be an non-returning block. Fib a bit by saying that the root node // in at least once. We are guaranteed to have this because we are
// postdominates this unreachable node. This isn't exactly true, // traversing the graph in DFO and have handled start nodes specially.
// because there is no path from this node to the root node, but it is //
// sorta true because any paths to the exit node would have to go while (Doms[*SI].size() == 0) ++SI;
// through this node. WorkingSet = Doms[*SI];
//
// This allows for postdominator properties to be built for code that for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
// doesn't return in a reasonable manner. DomSetType &SuccSet = Doms[*SI];
// if (SuccSet.size())
WorkingSet = Doms[Root]; set_intersect(WorkingSet, SuccSet);
} }
} else {
// If this node has no successors, it must be one of the root nodes.
// We will already take care of the notion that the node
// post-dominates itself. The only thing we have to add is that if
// there are multiple root nodes, we want to insert a special "null"
// exit node which dominates the roots as well.
if (Roots.size() > 1)
WorkingSet.insert(0);
}
WorkingSet.insert(BB); // A block always dominates itself WorkingSet.insert(BB); // A block always dominates itself
DomSetType &BBSet = Doms[BB]; DomSetType &BBSet = Doms[BB];
if (BBSet != WorkingSet) { if (BBSet != WorkingSet) {
BBSet.swap(WorkingSet); // Constant time operation! BBSet.swap(WorkingSet); // Constant time operation!
Changed = true; // The sets changed. Changed = true; // The sets changed.
}
WorkingSet.clear(); // Clear out the set for next iteration
} }
WorkingSet.clear(); // Clear out the set for next iteration
}
} while (Changed); } while (Changed);
return false; return false;
} }
// getAnalysisUsage - This obviously provides a post-dominator set, but it also
// requires the UnifyFunctionExitNodes pass.
//
void PostDominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<UnifyFunctionExitNodes>();
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// ImmediatePostDominators Implementation // ImmediatePostDominators Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -107,17 +109,25 @@ static RegisterAnalysis<PostDominatorTree>
F("postdomtree", "Post-Dominator Tree Construction", true); F("postdomtree", "Post-Dominator Tree Construction", true);
void PostDominatorTree::calculate(const PostDominatorSet &DS) { void PostDominatorTree::calculate(const PostDominatorSet &DS) {
Nodes[Root] = new Node(Root, 0); // Add a node for the root... if (Roots.empty()) return;
BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
if (Root) { Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
// Iterate over all nodes in depth first order...
for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root); // Iterate over all nodes in depth first order...
I != E; ++I) { 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; BasicBlock *BB = *I;
const DominatorSet::DomSetType &Dominators = DS.getDominators(BB); const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
unsigned DomSetSize = Dominators.size(); unsigned DomSetSize = Dominators.size();
if (DomSetSize == 1) continue; // Root node... IDom = null 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 // Loop over all dominators of this node. This corresponds to looping
// over nodes in the dominator chain, looking for a node whose dominator // 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 // set is equal to the current nodes, except that the current node does
@ -130,28 +140,27 @@ void PostDominatorTree::calculate(const PostDominatorSet &DS) {
DominatorSet::DomSetType::const_iterator I = Dominators.begin(); DominatorSet::DomSetType::const_iterator I = Dominators.begin();
DominatorSet::DomSetType::const_iterator End = Dominators.end(); DominatorSet::DomSetType::const_iterator End = Dominators.end();
for (; I != End; ++I) { // Iterate over dominators... for (; I != End; ++I) { // Iterate over dominators...
// All of our dominators should form a chain, where the number // All of our dominators should form a chain, where the number
// of elements in the dominator set indicates what level the // of elements in the dominator set indicates what level the
// node is at in the chain. We want the node immediately // node is at in the chain. We want the node immediately
// above us, so it will have an identical dominator set, // above us, so it will have an identical dominator set,
// except that BB will not dominate it... therefore it's // except that BB will not dominate it... therefore it's
// dominator set size will be one less than BB's... // dominator set size will be one less than BB's...
// //
if (DS.getDominators(*I).size() == DomSetSize - 1) { 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! // because we are traversing the CFG in depth first order!
// //
Node *IDomNode = Nodes[*I]; Node *IDomNode = Nodes[*I];
assert(IDomNode && "No node for IDOM?"); assert(IDomNode && "No node for IDOM?");
// Add a new tree node for this BasicBlock, and link it as a child of // Add a new tree node for this BasicBlock, and link it as a child of
// IDomNode // IDomNode
Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode)); Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
break; break;
} }
} }
} }
}
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -167,14 +176,14 @@ PostDominanceFrontier::calculate(const PostDominatorTree &DT,
// Loop over CFG successors to calculate DFlocal[Node] // Loop over CFG successors to calculate DFlocal[Node]
BasicBlock *BB = Node->getNode(); BasicBlock *BB = Node->getNode();
DomSetType &S = Frontiers[BB]; // The new set to fill in... DomSetType &S = Frontiers[BB]; // The new set to fill in...
if (!Root) return S; if (getRoots().empty()) return S;
for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); if (BB)
SI != SE; ++SI) { for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
// Does Node immediately dominate this predeccessor? SI != SE; ++SI)
if (DT[*SI]->getIDom() != Node) // Does Node immediately dominate this predeccessor?
S.insert(*SI); if (DT[*SI]->getIDom() != Node)
} S.insert(*SI);
// At this point, S is DFlocal. Now we union in DFup's of our children... // At this point, S is DFlocal. Now we union in DFup's of our children...
// Loop through and visit the nodes that Node immediately dominates (Node's // Loop through and visit the nodes that Node immediately dominates (Node's