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