LLVM API Documentation
00001 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the post-dominator construction algorithms. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Analysis/PostDominators.h" 00015 #include "llvm/Instructions.h" 00016 #include "llvm/Support/CFG.h" 00017 #include "llvm/ADT/DepthFirstIterator.h" 00018 #include "llvm/ADT/SetOperations.h" 00019 using namespace llvm; 00020 00021 //===----------------------------------------------------------------------===// 00022 // PostDominatorSet Implementation 00023 //===----------------------------------------------------------------------===// 00024 00025 static RegisterAnalysis<PostDominatorSet> 00026 B("postdomset", "Post-Dominator Set Construction", true); 00027 00028 // Postdominator set construction. This converts the specified function to only 00029 // have a single exit node (return stmt), then calculates the post dominance 00030 // sets for the function. 00031 // 00032 bool PostDominatorSet::runOnFunction(Function &F) { 00033 Doms.clear(); // Reset from the last time we were run... 00034 00035 // Scan the function looking for the root nodes of the post-dominance 00036 // relationships. These blocks end with return and unwind instructions. 00037 // While we are iterating over the function, we also initialize all of the 00038 // domsets to empty. 00039 Roots.clear(); 00040 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { 00041 Doms[I]; // Initialize to empty 00042 00043 if (succ_begin(I) == succ_end(I)) 00044 Roots.push_back(I); 00045 } 00046 00047 // If there are no exit nodes for the function, postdomsets are all empty. 00048 // This can happen if the function just contains an infinite loop, for 00049 // example. 00050 if (Roots.empty()) return false; 00051 00052 // If we have more than one root, we insert an artificial "null" exit, which 00053 // has "virtual edges" to each of the real exit nodes. 00054 if (Roots.size() > 1) 00055 Doms[0].insert(0); 00056 00057 bool Changed; 00058 do { 00059 Changed = false; 00060 00061 std::set<BasicBlock*> Visited; 00062 DomSetType WorkingSet; 00063 00064 for (unsigned i = 0, e = Roots.size(); i != e; ++i) 00065 for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited), 00066 E = idf_ext_end(Roots[i], Visited); It != E; ++It) { 00067 BasicBlock *BB = *It; 00068 succ_iterator SI = succ_begin(BB), SE = succ_end(BB); 00069 if (SI != SE) { // Is there SOME successor? 00070 // Loop until we get to a successor that has had it's dom set filled 00071 // in at least once. We are guaranteed to have this because we are 00072 // traversing the graph in DFO and have handled start nodes specially. 00073 // 00074 while (Doms[*SI].size() == 0) ++SI; 00075 WorkingSet = Doms[*SI]; 00076 00077 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets 00078 DomSetType &SuccSet = Doms[*SI]; 00079 if (SuccSet.size()) 00080 set_intersect(WorkingSet, SuccSet); 00081 } 00082 } else { 00083 // If this node has no successors, it must be one of the root nodes. 00084 // We will already take care of the notion that the node 00085 // post-dominates itself. The only thing we have to add is that if 00086 // there are multiple root nodes, we want to insert a special "null" 00087 // exit node which dominates the roots as well. 00088 if (Roots.size() > 1) 00089 WorkingSet.insert(0); 00090 } 00091 00092 WorkingSet.insert(BB); // A block always dominates itself 00093 DomSetType &BBSet = Doms[BB]; 00094 if (BBSet != WorkingSet) { 00095 BBSet.swap(WorkingSet); // Constant time operation! 00096 Changed = true; // The sets changed. 00097 } 00098 WorkingSet.clear(); // Clear out the set for next iteration 00099 } 00100 } while (Changed); 00101 return false; 00102 } 00103 00104 //===----------------------------------------------------------------------===// 00105 // ImmediatePostDominators Implementation 00106 //===----------------------------------------------------------------------===// 00107 00108 static RegisterAnalysis<ImmediatePostDominators> 00109 D("postidom", "Immediate Post-Dominators Construction", true); 00110 00111 00112 // calcIDoms - Calculate the immediate dominator mapping, given a set of 00113 // dominators for every basic block. 00114 void ImmediatePostDominators::calcIDoms(const DominatorSetBase &DS) { 00115 // Loop over all of the nodes that have dominators... figuring out the IDOM 00116 // for each node... 00117 // 00118 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end(); 00119 DI != DEnd; ++DI) { 00120 BasicBlock *BB = DI->first; 00121 const DominatorSet::DomSetType &Dominators = DI->second; 00122 unsigned DomSetSize = Dominators.size(); 00123 if (DomSetSize == 1) continue; // Root node... IDom = null 00124 00125 // Loop over all dominators of this node. This corresponds to looping over 00126 // nodes in the dominator chain, looking for a node whose dominator set is 00127 // equal to the current nodes, except that the current node does not exist 00128 // in it. This means that it is one level higher in the dom chain than the 00129 // current node, and it is our idom! 00130 // 00131 DominatorSet::DomSetType::const_iterator I = Dominators.begin(); 00132 DominatorSet::DomSetType::const_iterator End = Dominators.end(); 00133 for (; I != End; ++I) { // Iterate over dominators... 00134 // All of our dominators should form a chain, where the number of elements 00135 // in the dominator set indicates what level the node is at in the chain. 00136 // We want the node immediately above us, so it will have an identical 00137 // dominator set, except that BB will not dominate it... therefore it's 00138 // dominator set size will be one less than BB's... 00139 // 00140 if (DS.getDominators(*I).size() == DomSetSize - 1) { 00141 IDoms[BB] = *I; 00142 break; 00143 } 00144 } 00145 } 00146 } 00147 00148 //===----------------------------------------------------------------------===// 00149 // PostDominatorTree Implementation 00150 //===----------------------------------------------------------------------===// 00151 00152 static RegisterAnalysis<PostDominatorTree> 00153 F("postdomtree", "Post-Dominator Tree Construction", true); 00154 00155 void PostDominatorTree::calculate(const PostDominatorSet &DS) { 00156 if (Roots.empty()) return; 00157 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0; 00158 00159 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root... 00160 00161 // Iterate over all nodes in depth first order... 00162 for (unsigned i = 0, e = Roots.size(); i != e; ++i) 00163 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]), 00164 E = idf_end(Roots[i]); I != E; ++I) { 00165 BasicBlock *BB = *I; 00166 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB); 00167 unsigned DomSetSize = Dominators.size(); 00168 if (DomSetSize == 1) continue; // Root node... IDom = null 00169 00170 // If we have already computed the immediate dominator for this node, 00171 // don't revisit. This can happen due to nodes reachable from multiple 00172 // roots, but which the idf_iterator doesn't know about. 00173 if (Nodes.find(BB) != Nodes.end()) continue; 00174 00175 // Loop over all dominators of this node. This corresponds to looping 00176 // over nodes in the dominator chain, looking for a node whose dominator 00177 // set is equal to the current nodes, except that the current node does 00178 // not exist in it. This means that it is one level higher in the dom 00179 // chain than the current node, and it is our idom! We know that we have 00180 // already added a DominatorTree node for our idom, because the idom must 00181 // be a predecessor in the depth first order that we are iterating through 00182 // the function. 00183 // 00184 for (DominatorSet::DomSetType::const_iterator I = Dominators.begin(), 00185 E = Dominators.end(); I != E; ++I) { // Iterate over dominators. 00186 // All of our dominators should form a chain, where the number 00187 // of elements in the dominator set indicates what level the 00188 // node is at in the chain. We want the node immediately 00189 // above us, so it will have an identical dominator set, 00190 // except that BB will not dominate it... therefore it's 00191 // dominator set size will be one less than BB's... 00192 // 00193 if (DS.getDominators(*I).size() == DomSetSize - 1) { 00194 // We know that the immediate dominator should already have a node, 00195 // because we are traversing the CFG in depth first order! 00196 // 00197 Node *IDomNode = Nodes[*I]; 00198 assert(IDomNode && "No node for IDOM?"); 00199 00200 // Add a new tree node for this BasicBlock, and link it as a child of 00201 // IDomNode 00202 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode)); 00203 break; 00204 } 00205 } 00206 } 00207 } 00208 00209 //===----------------------------------------------------------------------===// 00210 // PostDominanceFrontier Implementation 00211 //===----------------------------------------------------------------------===// 00212 00213 static RegisterAnalysis<PostDominanceFrontier> 00214 H("postdomfrontier", "Post-Dominance Frontier Construction", true); 00215 00216 const DominanceFrontier::DomSetType & 00217 PostDominanceFrontier::calculate(const PostDominatorTree &DT, 00218 const DominatorTree::Node *Node) { 00219 // Loop over CFG successors to calculate DFlocal[Node] 00220 BasicBlock *BB = Node->getBlock(); 00221 DomSetType &S = Frontiers[BB]; // The new set to fill in... 00222 if (getRoots().empty()) return S; 00223 00224 if (BB) 00225 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); 00226 SI != SE; ++SI) 00227 // Does Node immediately dominate this predecessor? 00228 if (DT[*SI]->getIDom() != Node) 00229 S.insert(*SI); 00230 00231 // At this point, S is DFlocal. Now we union in DFup's of our children... 00232 // Loop through and visit the nodes that Node immediately dominates (Node's 00233 // children in the IDomTree) 00234 // 00235 for (PostDominatorTree::Node::const_iterator 00236 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) { 00237 DominatorTree::Node *IDominee = *NI; 00238 const DomSetType &ChildDF = calculate(DT, IDominee); 00239 00240 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end(); 00241 for (; CDFI != CDFE; ++CDFI) { 00242 if (!Node->dominates(DT[*CDFI])) 00243 S.insert(*CDFI); 00244 } 00245 } 00246 00247 return S; 00248 } 00249 00250 // stub - a dummy function to make linking work ok. 00251 void PostDominanceFrontier::stub() { 00252 } 00253