Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 1 | //===- DominatorSet.cpp - Dominator Set Calculation --------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file provides a simple class to calculate the dominator set of a method. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 8 | #include "llvm/Analysis/SimplifyCFG.h" // To get cfg::UnifyAllExitNodes |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 9 | #include "llvm/CFG.h" |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 10 | #include "llvm/Support/STLExtras.h" |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | // Helper Template |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | // set_intersect - Identical to set_intersection, except that it works on |
| 18 | // set<>'s and is nicer to use. Functionally, this iterates through S1, |
| 19 | // removing elements that are not contained in S2. |
| 20 | // |
| 21 | template <class Ty, class Ty2> |
| 22 | void set_intersect(set<Ty> &S1, const set<Ty2> &S2) { |
| 23 | for (typename set<Ty>::iterator I = S1.begin(); I != S1.end();) { |
| 24 | const Ty &E = *I; |
| 25 | ++I; |
| 26 | if (!S2.count(E)) S1.erase(E); // Erase element if not in S2 |
| 27 | } |
| 28 | } |
| 29 | |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // DominatorBase Implementation |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | bool cfg::DominatorBase::isPostDominator() const { |
Chris Lattner | 384e5b1 | 2001-08-23 17:07:19 +0000 | [diff] [blame] | 35 | // Root can be null if there is no exit node from the CFG and is postdom set |
| 36 | return Root == 0 || Root != Root->getParent()->front(); |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // DominatorSet Implementation |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | // DominatorSet ctor - Build either the dominator set or the post-dominator |
| 45 | // set for a method... |
| 46 | // |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 47 | cfg::DominatorSet::DominatorSet(const Method *M) : DominatorBase(M->front()) { |
| 48 | calcForwardDominatorSet(M); |
| 49 | } |
| 50 | |
| 51 | // calcForwardDominatorSet - This method calculates the forward dominator sets |
| 52 | // for the specified method. |
| 53 | // |
| 54 | void cfg::DominatorSet::calcForwardDominatorSet(const Method *M) { |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 55 | assert(Root && M && "Can't build dominator set of null method!"); |
Chris Lattner | 1b5499b | 2001-08-24 14:56:34 +0000 | [diff] [blame^] | 56 | assert(Root->use_size() == 0 && "Root node has predecessors in method!"); |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 57 | bool Changed; |
| 58 | do { |
| 59 | Changed = false; |
| 60 | |
| 61 | DomSetType WorkingSet; |
| 62 | df_const_iterator It = df_begin(M), End = df_end(M); |
| 63 | for ( ; It != End; ++It) { |
| 64 | const BasicBlock *BB = *It; |
| 65 | pred_const_iterator PI = pred_begin(BB), PEnd = pred_end(BB); |
| 66 | if (PI != PEnd) { // Is there SOME predecessor? |
| 67 | // Loop until we get to a predecessor that has had it's dom set filled |
| 68 | // in at least once. We are guaranteed to have this because we are |
| 69 | // traversing the graph in DFO and have handled start nodes specially. |
| 70 | // |
| 71 | while (Doms[*PI].size() == 0) ++PI; |
| 72 | WorkingSet = Doms[*PI]; |
| 73 | |
| 74 | for (++PI; PI != PEnd; ++PI) { // Intersect all of the predecessor sets |
| 75 | DomSetType &PredSet = Doms[*PI]; |
| 76 | if (PredSet.size()) |
| 77 | set_intersect(WorkingSet, PredSet); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | WorkingSet.insert(BB); // A block always dominates itself |
| 82 | DomSetType &BBSet = Doms[BB]; |
| 83 | if (BBSet != WorkingSet) { |
| 84 | BBSet.swap(WorkingSet); // Constant time operation! |
| 85 | Changed = true; // The sets changed. |
| 86 | } |
| 87 | WorkingSet.clear(); // Clear out the set for next iteration |
| 88 | } |
| 89 | } while (Changed); |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 92 | // Postdominator set constructor. This ctor converts the specified method to |
| 93 | // only have a single exit node (return stmt), then calculates the post |
| 94 | // dominance sets for the method. |
| 95 | // |
| 96 | cfg::DominatorSet::DominatorSet(Method *M, bool PostDomSet) |
| 97 | : DominatorBase(M->front()) { |
| 98 | if (!PostDomSet) { calcForwardDominatorSet(M); return; } |
| 99 | |
| 100 | Root = cfg::UnifyAllExitNodes(M); |
Chris Lattner | 384e5b1 | 2001-08-23 17:07:19 +0000 | [diff] [blame] | 101 | if (Root == 0) { // No exit node for the method? Postdomsets are all empty |
| 102 | for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) |
| 103 | Doms[*MI] = DomSetType(); |
| 104 | return; |
| 105 | } |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 106 | |
| 107 | bool Changed; |
| 108 | do { |
| 109 | Changed = false; |
| 110 | |
| 111 | set<const BasicBlock*> Visited; |
| 112 | DomSetType WorkingSet; |
| 113 | idf_const_iterator It = idf_begin(Root), End = idf_end(Root); |
| 114 | for ( ; It != End; ++It) { |
| 115 | const BasicBlock *BB = *It; |
| 116 | succ_const_iterator PI = succ_begin(BB), PEnd = succ_end(BB); |
| 117 | if (PI != PEnd) { // Is there SOME predecessor? |
| 118 | // Loop until we get to a successor that has had it's dom set filled |
| 119 | // in at least once. We are guaranteed to have this because we are |
| 120 | // traversing the graph in DFO and have handled start nodes specially. |
| 121 | // |
| 122 | while (Doms[*PI].size() == 0) ++PI; |
| 123 | WorkingSet = Doms[*PI]; |
| 124 | |
| 125 | for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets |
| 126 | DomSetType &PredSet = Doms[*PI]; |
| 127 | if (PredSet.size()) |
| 128 | set_intersect(WorkingSet, PredSet); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | WorkingSet.insert(BB); // A block always dominates itself |
| 133 | DomSetType &BBSet = Doms[BB]; |
| 134 | if (BBSet != WorkingSet) { |
| 135 | BBSet.swap(WorkingSet); // Constant time operation! |
| 136 | Changed = true; // The sets changed. |
| 137 | } |
| 138 | WorkingSet.clear(); // Clear out the set for next iteration |
| 139 | } |
| 140 | } while (Changed); |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | // ImmediateDominators Implementation |
| 146 | //===----------------------------------------------------------------------===// |
| 147 | |
| 148 | // calcIDoms - Calculate the immediate dominator mapping, given a set of |
| 149 | // dominators for every basic block. |
| 150 | void cfg::ImmediateDominators::calcIDoms(const DominatorSet &DS) { |
| 151 | // Loop over all of the nodes that have dominators... figuring out the IDOM |
| 152 | // for each node... |
| 153 | // |
| 154 | for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end(); |
| 155 | DI != DEnd; ++DI) { |
| 156 | const BasicBlock *BB = DI->first; |
| 157 | const DominatorSet::DomSetType &Dominators = DI->second; |
| 158 | unsigned DomSetSize = Dominators.size(); |
| 159 | if (DomSetSize == 1) continue; // Root node... IDom = null |
| 160 | |
| 161 | // Loop over all dominators of this node. This corresponds to looping over |
| 162 | // nodes in the dominator chain, looking for a node whose dominator set is |
| 163 | // equal to the current nodes, except that the current node does not exist |
| 164 | // in it. This means that it is one level higher in the dom chain than the |
| 165 | // current node, and it is our idom! |
| 166 | // |
| 167 | DominatorSet::DomSetType::const_iterator I = Dominators.begin(); |
| 168 | DominatorSet::DomSetType::const_iterator End = Dominators.end(); |
| 169 | for (; I != End; ++I) { // Iterate over dominators... |
| 170 | // All of our dominators should form a chain, where the number of elements |
| 171 | // in the dominator set indicates what level the node is at in the chain. |
| 172 | // We want the node immediately above us, so it will have an identical |
| 173 | // dominator set, except that BB will not dominate it... therefore it's |
| 174 | // dominator set size will be one less than BB's... |
| 175 | // |
| 176 | if (DS.getDominators(*I).size() == DomSetSize - 1) { |
| 177 | IDoms[BB] = *I; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | // DominatorTree Implementation |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | |
| 189 | // DominatorTree dtor - Free all of the tree node memory. |
| 190 | // |
| 191 | cfg::DominatorTree::~DominatorTree() { |
| 192 | for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) |
| 193 | delete I->second; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | cfg::DominatorTree::DominatorTree(const ImmediateDominators &IDoms) |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 198 | : DominatorBase(IDoms.getRoot()) { |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 199 | const Method *M = Root->getParent(); |
| 200 | |
| 201 | Nodes[Root] = new Node(Root, 0); // Add a node for the root... |
| 202 | |
| 203 | // Iterate over all nodes in depth first order... |
| 204 | for (df_const_iterator I = df_begin(M), E = df_end(M); I != E; ++I) { |
| 205 | const BasicBlock *BB = *I, *IDom = IDoms[*I]; |
| 206 | |
| 207 | if (IDom != 0) { // Ignore the root node and other nasty nodes |
| 208 | // We know that the immediate dominator should already have a node, |
| 209 | // because we are traversing the CFG in depth first order! |
| 210 | // |
| 211 | assert(Nodes[IDom] && "No node for IDOM?"); |
| 212 | Node *IDomNode = Nodes[IDom]; |
| 213 | |
| 214 | // Add a new tree node for this BasicBlock, and link it as a child of |
| 215 | // IDomNode |
| 216 | Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode)); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void cfg::DominatorTree::calculate(const DominatorSet &DS) { |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 222 | Nodes[Root] = new Node(Root, 0); // Add a node for the root... |
| 223 | |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 224 | if (!isPostDominator()) { |
| 225 | // Iterate over all nodes in depth first order... |
| 226 | for (df_const_iterator I = df_begin(Root), E = df_end(Root); I != E; ++I) { |
| 227 | const BasicBlock *BB = *I; |
| 228 | const DominatorSet::DomSetType &Dominators = DS.getDominators(BB); |
| 229 | unsigned DomSetSize = Dominators.size(); |
| 230 | if (DomSetSize == 1) continue; // Root node... IDom = null |
| 231 | |
| 232 | // Loop over all dominators of this node. This corresponds to looping over |
| 233 | // nodes in the dominator chain, looking for a node whose dominator set is |
| 234 | // equal to the current nodes, except that the current node does not exist |
| 235 | // in it. This means that it is one level higher in the dom chain than the |
| 236 | // current node, and it is our idom! We know that we have already added |
| 237 | // a DominatorTree node for our idom, because the idom must be a |
| 238 | // predecessor in the depth first order that we are iterating through the |
| 239 | // method. |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 240 | // |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 241 | DominatorSet::DomSetType::const_iterator I = Dominators.begin(); |
| 242 | DominatorSet::DomSetType::const_iterator End = Dominators.end(); |
| 243 | for (; I != End; ++I) { // Iterate over dominators... |
| 244 | // All of our dominators should form a chain, where the number of elements |
| 245 | // in the dominator set indicates what level the node is at in the chain. |
| 246 | // We want the node immediately above us, so it will have an identical |
| 247 | // dominator set, except that BB will not dominate it... therefore it's |
| 248 | // dominator set size will be one less than BB's... |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 249 | // |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 250 | if (DS.getDominators(*I).size() == DomSetSize - 1) { |
| 251 | // We know that the immediate dominator should already have a node, |
| 252 | // because we are traversing the CFG in depth first order! |
| 253 | // |
| 254 | Node *IDomNode = Nodes[*I]; |
| 255 | assert(IDomNode && "No node for IDOM?"); |
| 256 | |
| 257 | // Add a new tree node for this BasicBlock, and link it as a child of |
| 258 | // IDomNode |
| 259 | Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode)); |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | } |
Chris Lattner | 384e5b1 | 2001-08-23 17:07:19 +0000 | [diff] [blame] | 264 | } else if (Root) { |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 265 | // Iterate over all nodes in depth first order... |
| 266 | for (idf_const_iterator I = idf_begin(Root), E = idf_end(Root); I != E; ++I) { |
| 267 | const BasicBlock *BB = *I; |
| 268 | const DominatorSet::DomSetType &Dominators = DS.getDominators(BB); |
| 269 | unsigned DomSetSize = Dominators.size(); |
| 270 | if (DomSetSize == 1) continue; // Root node... IDom = null |
| 271 | |
| 272 | // Loop over all dominators of this node. This corresponds to looping over |
| 273 | // nodes in the dominator chain, looking for a node whose dominator set is |
| 274 | // equal to the current nodes, except that the current node does not exist |
| 275 | // in it. This means that it is one level higher in the dom chain than the |
| 276 | // current node, and it is our idom! We know that we have already added |
| 277 | // a DominatorTree node for our idom, because the idom must be a |
| 278 | // predecessor in the depth first order that we are iterating through the |
| 279 | // method. |
| 280 | // |
| 281 | DominatorSet::DomSetType::const_iterator I = Dominators.begin(); |
| 282 | DominatorSet::DomSetType::const_iterator End = Dominators.end(); |
| 283 | for (; I != End; ++I) { // Iterate over dominators... |
| 284 | // All of our dominators should form a chain, where the number of elements |
| 285 | // in the dominator set indicates what level the node is at in the chain. |
| 286 | // We want the node immediately above us, so it will have an identical |
| 287 | // dominator set, except that BB will not dominate it... therefore it's |
| 288 | // dominator set size will be one less than BB's... |
| 289 | // |
| 290 | if (DS.getDominators(*I).size() == DomSetSize - 1) { |
| 291 | // We know that the immediate dominator should already have a node, |
| 292 | // because we are traversing the CFG in depth first order! |
| 293 | // |
| 294 | Node *IDomNode = Nodes[*I]; |
| 295 | assert(IDomNode && "No node for IDOM?"); |
| 296 | |
| 297 | // Add a new tree node for this BasicBlock, and link it as a child of |
| 298 | // IDomNode |
| 299 | Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode)); |
| 300 | break; |
| 301 | } |
Chris Lattner | 1715229 | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | // DominanceFrontier Implementation |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | |
| 313 | const cfg::DominanceFrontier::DomSetType & |
| 314 | cfg::DominanceFrontier::calcDomFrontier(const DominatorTree &DT, |
| 315 | const DominatorTree::Node *Node) { |
| 316 | // Loop over CFG successors to calculate DFlocal[Node] |
| 317 | const BasicBlock *BB = Node->getNode(); |
| 318 | DomSetType &S = Frontiers[BB]; // The new set to fill in... |
| 319 | |
| 320 | for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 321 | SI != SE; ++SI) { |
| 322 | // Does Node immediately dominate this successor? |
| 323 | if (DT[*SI]->getIDom() != Node) |
| 324 | S.insert(*SI); |
| 325 | } |
| 326 | |
| 327 | // At this point, S is DFlocal. Now we union in DFup's of our children... |
| 328 | // Loop through and visit the nodes that Node immediately dominates (Node's |
| 329 | // children in the IDomTree) |
| 330 | // |
| 331 | for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end(); |
| 332 | NI != NE; ++NI) { |
| 333 | DominatorTree::Node *IDominee = *NI; |
| 334 | const DomSetType &ChildDF = calcDomFrontier(DT, IDominee); |
| 335 | |
| 336 | DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end(); |
| 337 | for (; CDFI != CDFE; ++CDFI) { |
| 338 | if (!Node->dominates(DT[*CDFI])) |
| 339 | S.insert(*CDFI); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return S; |
| 344 | } |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 345 | |
| 346 | const cfg::DominanceFrontier::DomSetType & |
| 347 | cfg::DominanceFrontier::calcPostDomFrontier(const DominatorTree &DT, |
| 348 | const DominatorTree::Node *Node) { |
| 349 | // Loop over CFG successors to calculate DFlocal[Node] |
| 350 | const BasicBlock *BB = Node->getNode(); |
| 351 | DomSetType &S = Frontiers[BB]; // The new set to fill in... |
Chris Lattner | 384e5b1 | 2001-08-23 17:07:19 +0000 | [diff] [blame] | 352 | if (!Root) return S; |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 353 | |
| 354 | for (pred_const_iterator SI = pred_begin(BB), SE = pred_end(BB); |
| 355 | SI != SE; ++SI) { |
| 356 | // Does Node immediately dominate this predeccessor? |
| 357 | if (DT[*SI]->getIDom() != Node) |
| 358 | S.insert(*SI); |
| 359 | } |
| 360 | |
| 361 | // At this point, S is DFlocal. Now we union in DFup's of our children... |
| 362 | // Loop through and visit the nodes that Node immediately dominates (Node's |
| 363 | // children in the IDomTree) |
| 364 | // |
| 365 | for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end(); |
| 366 | NI != NE; ++NI) { |
| 367 | DominatorTree::Node *IDominee = *NI; |
Chris Lattner | 3590830 | 2001-07-08 05:54:09 +0000 | [diff] [blame] | 368 | const DomSetType &ChildDF = calcPostDomFrontier(DT, IDominee); |
Chris Lattner | 94108ab | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 369 | |
| 370 | DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end(); |
| 371 | for (; CDFI != CDFE; ++CDFI) { |
| 372 | if (!Node->dominates(DT[*CDFI])) |
| 373 | S.insert(*CDFI); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return S; |
| 378 | } |