From 706e61ead9b7913098ff3fbf729263a36e01f1b9 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 10 Sep 2003 20:37:08 +0000 Subject: [PATCH] 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. :) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8460 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/PostDominators.cpp | 181 +++++++++++++++++--------------- 1 file changed, 95 insertions(+), 86 deletions(-) diff --git a/lib/Analysis/PostDominators.cpp b/lib/Analysis/PostDominators.cpp index ddf8d7fb5a0..6be8f3dd231 100644 --- a/lib/Analysis/PostDominators.cpp +++ b/lib/Analysis/PostDominators.cpp @@ -5,7 +5,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/PostDominators.h" -#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" +#include "llvm/iTerminators.h" #include "llvm/Support/CFG.h" #include "Support/DepthFirstIterator.h" #include "Support/SetOperations.h" @@ -23,75 +23,77 @@ B("postdomset", "Post-Dominator Set Construction", true); // bool PostDominatorSet::runOnFunction(Function &F) { 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().getExitNode(); - if (Root == 0) { // No exit node for the function? Postdomsets are all empty - for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) - Doms[FI] = DomSetType(); - return false; + // Scan the function looking for the root nodes of the post-dominance + // relationships. These blocks end with return and unwind instructions. + // While we are iterating over the function, we also initialize all of the + // domsets to empty. + Roots.clear(); + for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { + Doms[I]; // Initialize to empty + + if (isa(I->getTerminator()) || + isa(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; do { Changed = false; std::set Visited; DomSetType WorkingSet; - idf_iterator 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 - DomSetType &PredSet = Doms[*PI]; - if (PredSet.size()) - set_intersect(WorkingSet, PredSet); - } - } else if (BB != Root) { - // If this isn't the root basic block and it has no successors, it must - // be an non-returning block. Fib a bit by saying that the root node - // postdominates this unreachable node. This isn't exactly true, - // 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 - // through this node. - // - // This allows for postdominator properties to be built for code that - // doesn't return in a reasonable manner. - // - WorkingSet = Doms[Root]; - } + for (unsigned i = 0, e = Roots.size(); i != e; ++i) + for (idf_iterator It = idf_begin(Roots[i]), + E = idf_end(Roots[i]); It != E; ++It) { + BasicBlock *BB = *It; + succ_iterator SI = succ_begin(BB), SE = succ_end(BB); + if (SI != SE) { // Is there SOME successor? + // 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[*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()) + 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 - DomSetType &BBSet = Doms[BB]; - if (BBSet != WorkingSet) { - BBSet.swap(WorkingSet); // Constant time operation! - Changed = true; // The sets changed. + WorkingSet.insert(BB); // A block always dominates itself + DomSetType &BBSet = Doms[BB]; + if (BBSet != WorkingSet) { + BBSet.swap(WorkingSet); // Constant time operation! + 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); 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(); -} - //===----------------------------------------------------------------------===// // ImmediatePostDominators Implementation //===----------------------------------------------------------------------===// @@ -107,17 +109,25 @@ static RegisterAnalysis F("postdomtree", "Post-Dominator Tree Construction", true); 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) { - // Iterate over all nodes in depth first order... - for (idf_iterator I = idf_begin(Root), E = idf_end(Root); - I != E; ++I) { + 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 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 @@ -130,28 +140,27 @@ void PostDominatorTree::calculate(const PostDominatorSet &DS) { 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?"); + // 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; - } + // 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; + } } } - } } //===----------------------------------------------------------------------===// @@ -167,14 +176,14 @@ PostDominanceFrontier::calculate(const PostDominatorTree &DT, // Loop over CFG successors to calculate DFlocal[Node] BasicBlock *BB = Node->getNode(); 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); - SI != SE; ++SI) { - // Does Node immediately dominate this predeccessor? - if (DT[*SI]->getIDom() != Node) - S.insert(*SI); - } + if (BB) + for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); + SI != SE; ++SI) + // Does Node immediately dominate this predeccessor? + if (DT[*SI]->getIDom() != Node) + S.insert(*SI); // 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