1 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the post-dominator construction algorithms. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/PostDominators.h" 15 #include "llvm/Instructions.h" 16 #include "llvm/Support/CFG.h" 17 #include "llvm/ADT/DepthFirstIterator.h" 18 #include "llvm/ADT/SetOperations.h" 19 using namespace llvm; 20 21 //===----------------------------------------------------------------------===// 22 // PostDominatorSet Implementation 23 //===----------------------------------------------------------------------===// 24 25 static RegisterAnalysis<PostDominatorSet> 26 B("postdomset", "Post-Dominator Set Construction", true); 27 28 // Postdominator set construction. This converts the specified function to only 29 // have a single exit node (return stmt), then calculates the post dominance 30 // sets for the function. 31 // 32 bool PostDominatorSet::runOnFunction(Function &F) { 33 Doms.clear(); // Reset from the last time we were run... 34 35 // Scan the function looking for the root nodes of the post-dominance 36 // relationships. These blocks end with return and unwind instructions. 37 // While we are iterating over the function, we also initialize all of the 38 // domsets to empty. 39 Roots.clear(); 40 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { 41 Doms[I]; // Initialize to empty 42 43 if (succ_begin(I) == succ_end(I)) 44 Roots.push_back(I); 45 } 46 47 // If there are no exit nodes for the function, postdomsets are all empty. 48 // This can happen if the function just contains an infinite loop, for 49 // example. 50 if (Roots.empty()) return false; 51 52 // If we have more than one root, we insert an artificial "null" exit, which 53 // has "virtual edges" to each of the real exit nodes. 54 if (Roots.size() > 1) 55 Doms[0].insert(0); 56 57 bool Changed; 58 do { 59 Changed = false; 60 61 std::set<BasicBlock*> Visited; 62 DomSetType WorkingSet; 63 64 for (unsigned i = 0, e = Roots.size(); i != e; ++i) 65 for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited), 66 E = idf_ext_end(Roots[i], Visited); It != E; ++It) { 67 BasicBlock *BB = *It; 68 succ_iterator SI = succ_begin(BB), SE = succ_end(BB); 69 if (SI != SE) { // Is there SOME successor? 70 // Loop until we get to a successor that has had it's dom set filled 71 // in at least once. We are guaranteed to have this because we are 72 // traversing the graph in DFO and have handled start nodes specially. 73 // 74 while (Doms[*SI].size() == 0) ++SI; 75 WorkingSet = Doms[*SI]; 76 77 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets 78 DomSetType &SuccSet = Doms[*SI]; 79 if (SuccSet.size()) 80 set_intersect(WorkingSet, SuccSet); 81 } 82 } else { 83 // If this node has no successors, it must be one of the root nodes. 84 // We will already take care of the notion that the node 85 // post-dominates itself. The only thing we have to add is that if 86 // there are multiple root nodes, we want to insert a special "null" 87 // exit node which dominates the roots as well. 88 if (Roots.size() > 1) 89 WorkingSet.insert(0); 90 } 91 92 WorkingSet.insert(BB); // A block always dominates itself 93 DomSetType &BBSet = Doms[BB]; 94 if (BBSet != WorkingSet) { 95 BBSet.swap(WorkingSet); // Constant time operation! 96 Changed = true; // The sets changed. 97 } 98 WorkingSet.clear(); // Clear out the set for next iteration 99 } 100 } while (Changed); 101 return false; 102 } 103 104 //===----------------------------------------------------------------------===// 105 // ImmediatePostDominators Implementation 106 //===----------------------------------------------------------------------===// 107 108 static RegisterAnalysis<ImmediatePostDominators> 109 D("postidom", "Immediate Post-Dominators Construction", true); 110 111 112 // calcIDoms - Calculate the immediate dominator mapping, given a set of 113 // dominators for every basic block. 114 void ImmediatePostDominators::calcIDoms(const DominatorSetBase &DS) { 115 // Loop over all of the nodes that have dominators... figuring out the IDOM 116 // for each node... 117 // 118 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end(); 119 DI != DEnd; ++DI) { 120 BasicBlock *BB = DI->first; 121 const DominatorSet::DomSetType &Dominators = DI->second; 122 unsigned DomSetSize = Dominators.size(); 123 if (DomSetSize == 1) continue; // Root node... IDom = null 124 125 // Loop over all dominators of this node. This corresponds to looping over 126 // nodes in the dominator chain, looking for a node whose dominator set is 127 // equal to the current nodes, except that the current node does not exist 128 // in it. This means that it is one level higher in the dom chain than the 129 // current node, and it is our idom! 130 // 131 DominatorSet::DomSetType::const_iterator I = Dominators.begin(); 132 DominatorSet::DomSetType::const_iterator End = Dominators.end(); 133 for (; I != End; ++I) { // Iterate over dominators... 134 // All of our dominators should form a chain, where the number of elements 135 // in the dominator set indicates what level the node is at in the chain. 136 // We want the node immediately above us, so it will have an identical 137 // dominator set, except that BB will not dominate it... therefore it's 138 // dominator set size will be one less than BB's... 139 // 140 if (DS.getDominators(*I).size() == DomSetSize - 1) { 141 IDoms[BB] = *I; 142 break; 143 } 144 } 145 } 146 } 147 148 //===----------------------------------------------------------------------===// 149 // PostDominatorTree Implementation 150 //===----------------------------------------------------------------------===// 151 152 static RegisterAnalysis<PostDominatorTree> 153 F("postdomtree", "Post-Dominator Tree Construction", true); 154 155 void PostDominatorTree::calculate(const PostDominatorSet &DS) { 156 if (Roots.empty()) return; 157 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0; 158 159 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root... 160 161 // Iterate over all nodes in depth first order... 162 for (unsigned i = 0, e = Roots.size(); i != e; ++i) 163 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]), 164 E = idf_end(Roots[i]); I != E; ++I) { 165 BasicBlock *BB = *I; 166 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB); 167 unsigned DomSetSize = Dominators.size(); 168 if (DomSetSize == 1) continue; // Root node... IDom = null 169 170 // If we have already computed the immediate dominator for this node, 171 // don't revisit. This can happen due to nodes reachable from multiple 172 // roots, but which the idf_iterator doesn't know about. 173 if (Nodes.find(BB) != Nodes.end()) continue; 174 175 // Loop over all dominators of this node. This corresponds to looping 176 // over nodes in the dominator chain, looking for a node whose dominator 177 // set is equal to the current nodes, except that the current node does 178 // not exist in it. This means that it is one level higher in the dom 179 // chain than the current node, and it is our idom! We know that we have 180 // already added a DominatorTree node for our idom, because the idom must 181 // be a predecessor in the depth first order that we are iterating through 182 // the function. 183 // 184 for (DominatorSet::DomSetType::const_iterator I = Dominators.begin(), 185 E = Dominators.end(); I != E; ++I) { // Iterate over dominators. 186 // All of our dominators should form a chain, where the number 187 // of elements in the dominator set indicates what level the 188 // node is at in the chain. We want the node immediately 189 // above us, so it will have an identical dominator set, 190 // except that BB will not dominate it... therefore it's 191 // dominator set size will be one less than BB's... 192 // 193 if (DS.getDominators(*I).size() == DomSetSize - 1) { 194 // We know that the immediate dominator should already have a node, 195 // because we are traversing the CFG in depth first order! 196 // 197 Node *IDomNode = Nodes[*I]; 198 assert(IDomNode && "No node for IDOM?"); 199 200 // Add a new tree node for this BasicBlock, and link it as a child of 201 // IDomNode 202 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode)); 203 break; 204 } 205 } 206 } 207 } 208 209 //===----------------------------------------------------------------------===// 210 // PostDominanceFrontier Implementation 211 //===----------------------------------------------------------------------===// 212 213 static RegisterAnalysis<PostDominanceFrontier> 214 H("postdomfrontier", "Post-Dominance Frontier Construction", true); 215 216 const DominanceFrontier::DomSetType & 217 PostDominanceFrontier::calculate(const PostDominatorTree &DT, 218 const DominatorTree::Node *Node) { 219 // Loop over CFG successors to calculate DFlocal[Node] 220 BasicBlock *BB = Node->getBlock(); 221 DomSetType &S = Frontiers[BB]; // The new set to fill in... 222 if (getRoots().empty()) return S; 223 224 if (BB) 225 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); 226 SI != SE; ++SI) 227 // Does Node immediately dominate this predecessor? 228 if (DT[*SI]->getIDom() != Node) 229 S.insert(*SI); 230 231 // At this point, S is DFlocal. Now we union in DFup's of our children... 232 // Loop through and visit the nodes that Node immediately dominates (Node's 233 // children in the IDomTree) 234 // 235 for (PostDominatorTree::Node::const_iterator 236 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) { 237 DominatorTree::Node *IDominee = *NI; 238 const DomSetType &ChildDF = calculate(DT, IDominee); 239 240 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end(); 241 for (; CDFI != CDFE; ++CDFI) { 242 if (!Node->dominates(DT[*CDFI])) 243 S.insert(*CDFI); 244 } 245 } 246 247 return S; 248 } 249 250 // stub - a dummy function to make linking work ok. 251 void PostDominanceFrontier::stub() { 252 } 253 254