llvm.org GIT mirror llvm / ba43963
Completely purge DomSet. This is the (hopefully) final patch for PR1171. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35731 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 12 years ago
8 changed file(s) with 16 addition(s) and 329 deletion(s). Raw diff Collapse all Expand all
99 // This file defines the following classes:
1010 // 1. ImmediateDominators: Calculates and holds a mapping between BasicBlocks
1111 // and their immediate dominator.
12 // 2. DominatorSet: Calculates the [reverse] dominator set for a function
13 // 3. DominatorTree: Represent the ImmediateDominator as an explicit tree
12 // 2. DominatorTree: Represent the ImmediateDominator as an explicit tree
1413 // structure.
1514 // 4. ETForest: Efficient data structure for dominance comparisons and
1615 // nearest-common-ancestor queries.
173172 BasicBlock *Eval(BasicBlock *v);
174173 void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo);
175174 };
176
177
178
179 //===----------------------------------------------------------------------===//
180 /// DominatorSet - Maintain a set for every basic block in a
181 /// function, that represents the blocks that dominate the block. If the block
182 /// is unreachable in this function, the set will be empty. This cannot happen
183 /// for reachable code, because every block dominates at least itself.
184 ///
185 class DominatorSetBase : public DominatorBase {
186 public:
187 typedef std::set DomSetType; // Dom set for a bb
188 // Map of dom sets
189 typedef std::map DomSetMapType;
190 protected:
191 DomSetMapType Doms;
192 public:
193 DominatorSetBase(bool isPostDom) : DominatorBase(isPostDom) {}
194
195 virtual void releaseMemory() { Doms.clear(); }
196
197 // Accessor interface:
198 typedef DomSetMapType::const_iterator const_iterator;
199 typedef DomSetMapType::iterator iterator;
200 inline const_iterator begin() const { return Doms.begin(); }
201 inline iterator begin() { return Doms.begin(); }
202 inline const_iterator end() const { return Doms.end(); }
203 inline iterator end() { return Doms.end(); }
204 inline const_iterator find(BasicBlock* B) const { return Doms.find(B); }
205 inline iterator find(BasicBlock* B) { return Doms.find(B); }
206
207
208 /// getDominators - Return the set of basic blocks that dominate the specified
209 /// block.
210 ///
211 inline const DomSetType &getDominators(BasicBlock *BB) const {
212 const_iterator I = find(BB);
213 assert(I != end() && "BB not in function!");
214 return I->second;
215 }
216
217 /// isReachable - Return true if the specified basicblock is reachable. If
218 /// the block is reachable, we have dominator set information for it.
219 ///
220 bool isReachable(BasicBlock *BB) const {
221 return !getDominators(BB).empty();
222 }
223
224 /// dominates - Return true if A dominates B.
225 ///
226 inline bool dominates(BasicBlock *A, BasicBlock *B) const {
227 return getDominators(B).count(A) != 0;
228 }
229
230 /// properlyDominates - Return true if A dominates B and A != B.
231 ///
232 bool properlyDominates(BasicBlock *A, BasicBlock *B) const {
233 return dominates(A, B) && A != B;
234 }
235
236 /// print - Convert to human readable form
237 ///
238 virtual void print(std::ostream &OS, const Module* = 0) const;
239 void print(std::ostream *OS, const Module* M = 0) const {
240 if (OS) print(*OS, M);
241 }
242
243 /// dominates - Return true if A dominates B. This performs the special
244 /// checks necessary if A and B are in the same basic block.
245 ///
246 bool dominates(Instruction *A, Instruction *B) const;
247
248 //===--------------------------------------------------------------------===//
249 // API to update (Post)DominatorSet information based on modifications to
250 // the CFG...
251
252 /// addBasicBlock - Call to update the dominator set with information about a
253 /// new block that was inserted into the function.
254 ///
255 void addBasicBlock(BasicBlock *BB, const DomSetType &Dominators) {
256 assert(find(BB) == end() && "Block already in DominatorSet!");
257 Doms.insert(std::make_pair(BB, Dominators));
258 }
259
260 /// addDominator - If a new block is inserted into the CFG, then method may be
261 /// called to notify the blocks it dominates that it is in their set.
262 ///
263 void addDominator(BasicBlock *BB, BasicBlock *NewDominator) {
264 iterator I = find(BB);
265 assert(I != end() && "BB is not in DominatorSet!");
266 I->second.insert(NewDominator);
267 }
268 };
269
270
271 //===-------------------------------------
272 /// DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
273 /// compute a normal dominator set.
274 ///
275 class DominatorSet : public DominatorSetBase {
276 public:
277 DominatorSet() : DominatorSetBase(false) {}
278
279 virtual bool runOnFunction(Function &F);
280
281 BasicBlock *getRoot() const {
282 assert(Roots.size() == 1 && "Should always have entry node!");
283 return Roots[0];
284 }
285
286 /// getAnalysisUsage - This simply provides a dominator set
287 ///
288 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
289 AU.addRequired();
290 AU.setPreservesAll();
291 }
292
293 // stub - dummy function, just ignore it
294 static int stub;
295 };
296
297175
298176 //===----------------------------------------------------------------------===//
299177 /// DominatorTree - Calculate the immediate dominator tree for a function.
690568
691569 } // End llvm namespace
692570
693 // Make sure that any clients of this file link in Dominators.cpp
694 FORCE_DEFINING_FILE_TO_BE_LINKED(DominatorSet)
695
696571 #endif
3535 void Compress(BasicBlock *V, InfoRec &VInfo);
3636 BasicBlock *Eval(BasicBlock *v);
3737 void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo);
38 };
39
40 /// PostDominatorSet Class - Concrete subclass of DominatorSetBase that is used
41 /// to compute the post-dominator set. Because there can be multiple exit nodes
42 /// in an LLVM function, we calculate post dominators with a special null block
43 /// which is the virtual exit node that the real exit nodes all virtually branch
44 /// to. Clients should be prepared to see an entry in the dominator sets with a
45 /// null BasicBlock*.
46 ///
47 struct PostDominatorSet : public DominatorSetBase {
48 PostDominatorSet() : DominatorSetBase(true) {}
49
50 virtual bool runOnFunction(Function &F);
51
52 /// getAnalysisUsage - This simply provides a dominator set
53 ///
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.addRequired();
56 AU.setPreservesAll();
57 }
58
59 // stub - dummy function, just ignore it
60 static void stub();
6138 };
6239
6340 /// PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
113113
114114 (void)new llvm::IntervalPartition();
115115 (void)new llvm::ImmediateDominators();
116 (void)new llvm::PostDominatorSet();
117116 (void)new llvm::FindUsedTypes();
118117 (void)new llvm::ScalarEvolution();
119118 ((llvm::Function*)0)->viewCFGOnly();
6060 bool AllowIdenticalEdges = false);
6161
6262 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
63 /// split the critical edge. This will update DominatorSet, ImmediateDominator,
63 /// split the critical edge. This will update ETForest, ImmediateDominator,
6464 /// DominatorTree, and DominatorFrontier information if it is available, thus
6565 /// calling this pass will not invalidate either of them. This returns true if
6666 /// the edge was split, false otherwise. If MergeIdenticalEdges is true (the
1818
1919 namespace llvm {
2020 class BasicBlock;
21 class DominatorSet;
2221 class Function;
2322 class Loop;
2423
166166 }
167167
168168 //===----------------------------------------------------------------------===//
169 // PostDominatorSet Implementation
170 //===----------------------------------------------------------------------===//
171
172 static RegisterPass
173 B("postdomset", "Post-Dominator Set Construction", true);
174
175 // Postdominator set construction. This converts the specified function to only
176 // have a single exit node (return stmt), then calculates the post dominance
177 // sets for the function.
178 //
179 bool PostDominatorSet::runOnFunction(Function &F) {
180 // Scan the function looking for the root nodes of the post-dominance
181 // relationships. These blocks end with return and unwind instructions.
182 // While we are iterating over the function, we also initialize all of the
183 // domsets to empty.
184 Roots.clear();
185 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
186 if (succ_begin(I) == succ_end(I))
187 Roots.push_back(I);
188
189 // If there are no exit nodes for the function, postdomsets are all empty.
190 // This can happen if the function just contains an infinite loop, for
191 // example.
192 ImmediatePostDominators &IPD = getAnalysis();
193 Doms.clear(); // Reset from the last time we were run...
194 if (Roots.empty()) return false;
195
196 // If we have more than one root, we insert an artificial "null" exit, which
197 // has "virtual edges" to each of the real exit nodes.
198 //if (Roots.size() > 1)
199 // Doms[0].insert(0);
200
201 // Root nodes only dominate themselves.
202 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
203 Doms[Roots[i]].insert(Roots[i]);
204
205 // Loop over all of the blocks in the function, calculating dominator sets for
206 // each function.
207 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
208 if (BasicBlock *IPDom = IPD[I]) { // Get idom if block is reachable
209 DomSetType &DS = Doms[I];
210 assert(DS.empty() && "PostDomset already filled in for this block?");
211 DS.insert(I); // Blocks always dominate themselves
212
213 // Insert all dominators into the set...
214 while (IPDom) {
215 // If we have already computed the dominator sets for our immediate post
216 // dominator, just use it instead of walking all the way up to the root.
217 DomSetType &IPDS = Doms[IPDom];
218 if (!IPDS.empty()) {
219 DS.insert(IPDS.begin(), IPDS.end());
220 break;
221 } else {
222 DS.insert(IPDom);
223 IPDom = IPD[IPDom];
224 }
225 }
226 } else {
227 // Ensure that every basic block has at least an empty set of nodes. This
228 // is important for the case when there is unreachable blocks.
229 Doms[I];
230 }
231
232 return false;
233 }
234
235 //===----------------------------------------------------------------------===//
236169 // PostDominatorTree Implementation
237170 //===----------------------------------------------------------------------===//
238171
153153 // many analyses if they are around.
154154 AU.addPreservedID(LoopSimplifyID);
155155 AU.addPreserved();
156 AU.addPreserved();
157156 AU.addPreserved();
158157 AU.addPreserved();
159158 AU.addPreserved();
250250 o << "\n";
251251 }
252252
253
254
255 //===----------------------------------------------------------------------===//
256 // DominatorSet Implementation
257 //===----------------------------------------------------------------------===//
258
259 static RegisterPass
260 B("domset", "Dominator Set Construction", true);
261
262 // dominates - Return true if A dominates B. This performs the special checks
263 // necessary if A and B are in the same basic block.
264 //
265 bool DominatorSetBase::dominates(Instruction *A, Instruction *B) const {
266 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
267 if (BBA != BBB) return dominates(BBA, BBB);
268
269 // It is not possible to determine dominance between two PHI nodes
270 // based on their ordering.
271 if (isa(A) && isa(B))
272 return false;
273
274 // Loop through the basic block until we find A or B.
275 BasicBlock::iterator I = BBA->begin();
276 for (; &*I != A && &*I != B; ++I) /*empty*/;
277
278 if(!IsPostDominators) {
279 // A dominates B if it is found first in the basic block.
280 return &*I == A;
281 } else {
282 // A post-dominates B if B is found first in the basic block.
283 return &*I == B;
284 }
285 }
286
287
288 // runOnFunction - This method calculates the forward dominator sets for the
289 // specified function.
290 //
291 bool DominatorSet::runOnFunction(Function &F) {
292 BasicBlock *Root = &F.getEntryBlock();
293 Roots.clear();
294 Roots.push_back(Root);
295 assert(pred_begin(Root) == pred_end(Root) &&
296 "Root node has predecessors in function!");
297
298 ImmediateDominators &ID = getAnalysis();
299 Doms.clear();
300 if (Roots.empty()) return false;
301
302 // Root nodes only dominate themselves.
303 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
304 Doms[Roots[i]].insert(Roots[i]);
305
306 // Loop over all of the blocks in the function, calculating dominator sets for
307 // each function.
308 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
309 if (BasicBlock *IDom = ID[I]) { // Get idom if block is reachable
310 DomSetType &DS = Doms[I];
311 assert(DS.empty() && "Domset already filled in for this block?");
312 DS.insert(I); // Blocks always dominate themselves
313
314 // Insert all dominators into the set...
315 while (IDom) {
316 // If we have already computed the dominator sets for our immediate
317 // dominator, just use it instead of walking all the way up to the root.
318 DomSetType &IDS = Doms[IDom];
319 if (!IDS.empty()) {
320 DS.insert(IDS.begin(), IDS.end());
321 break;
322 } else {
323 DS.insert(IDom);
324 IDom = ID[IDom];
325 }
326 }
327 } else {
328 // Ensure that every basic block has at least an empty set of nodes. This
329 // is important for the case when there is unreachable blocks.
330 Doms[I];
331 }
332
333 return false;
334 }
335
336 namespace llvm {
337 static std::ostream &operator<<(std::ostream &o,
338 const std::set &BBs) {
339 for (std::set::const_iterator I = BBs.begin(), E = BBs.end();
340 I != E; ++I)
341 if (*I)
342 WriteAsOperand(o, *I, false);
343 else
344 o << " <>";
345 return o;
346 }
347 }
348
349 void DominatorSetBase::print(std::ostream &o, const Module* ) const {
350 for (const_iterator I = begin(), E = end(); I != E; ++I) {
351 o << " DomSet For BB: ";
352 if (I->first)
353 WriteAsOperand(o, I->first, false);
354 else
355 o << " <>";
356 o << " is:\t" << I->second << "\n";
357 }
358 }
359
360253 //===----------------------------------------------------------------------===//
361254 // DominatorTree Implementation
362255 //===----------------------------------------------------------------------===//
526419
527420 return *Result;
528421 }
422
423 namespace llvm {
424 static std::ostream &operator<<(std::ostream &o,
425 const std::set &BBs) {
426 for (std::set::const_iterator I = BBs.begin(), E = BBs.end();
427 I != E; ++I)
428 if (*I)
429 WriteAsOperand(o, *I, false);
430 else
431 o << " <>";
432 return o;
433 }
434 }
435
529436
530437 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
531438 for (const_iterator I = begin(), E = end(); I != E; ++I) {
1070977 }
1071978 o << "\n";
1072979 }
1073
1074 DEFINING_FILE_FOR(DominatorSet)