1 //===- llvm/Analysis/Dominators.h - Dominator Info Calculation ---*- C++ -*--=//
3 // This file defines the following classes:
4 // 1. DominatorSet: Calculates the [reverse] dominator set for a function
5 // 2. ImmediateDominators: Calculates and holds a mapping between BasicBlocks
6 // and their immediate dominator.
7 // 3. DominatorTree: Represent the ImmediateDominator as an explicit tree
9 // 4. DominanceFrontier: Calculate and hold the dominance frontier for a
12 // These data structures are listed in increasing order of complexity. It
13 // takes longer to calculate the dominator frontier, for example, than the
14 // ImmediateDominator mapping.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_DOMINATORS_H
19 #define LLVM_DOMINATORS_H
21 #include "llvm/Pass.h"
25 //===----------------------------------------------------------------------===//
27 // DominatorBase - Base class that other, more interesting dominator analyses
30 class DominatorBase : public FunctionPass {
33 const bool IsPostDominators;
35 inline DominatorBase(bool isPostDom) : Root(0), IsPostDominators(isPostDom) {}
37 inline BasicBlock *getRoot() const { return Root; }
39 // Returns true if analysis based of postdoms
40 bool isPostDominator() const { return IsPostDominators; }
43 //===----------------------------------------------------------------------===//
45 // DominatorSet - Maintain a set<BasicBlock*> for every basic block in a
46 // function, that represents the blocks that dominate the block.
48 class DominatorSetBase : public DominatorBase {
50 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
52 typedef std::map<BasicBlock*, DomSetType> DomSetMapType;
56 DominatorSetBase(bool isPostDom) : DominatorBase(isPostDom) {}
58 virtual void releaseMemory() { Doms.clear(); }
60 // Accessor interface:
61 typedef DomSetMapType::const_iterator const_iterator;
62 typedef DomSetMapType::iterator iterator;
63 inline const_iterator begin() const { return Doms.begin(); }
64 inline iterator begin() { return Doms.begin(); }
65 inline const_iterator end() const { return Doms.end(); }
66 inline iterator end() { return Doms.end(); }
67 inline const_iterator find(BasicBlock* B) const { return Doms.find(B); }
68 inline iterator find(BasicBlock* B) { return Doms.find(B); }
70 // getDominators - Return the set of basic blocks that dominate the specified
73 inline const DomSetType &getDominators(BasicBlock *BB) const {
74 const_iterator I = find(BB);
75 assert(I != end() && "BB not in function!");
79 // dominates - Return true if A dominates B.
81 inline bool dominates(BasicBlock *A, BasicBlock *B) const {
82 return getDominators(B).count(A) != 0;
85 // dominates - Return true if A dominates B. This performs the special checks
86 // neccesary if A and B are in the same basic block.
88 bool dominates(Instruction *A, Instruction *B) const;
92 //===-------------------------------------
93 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
94 // compute a normal dominator set.
96 struct DominatorSet : public DominatorSetBase {
97 static AnalysisID ID; // Build dominator set
99 DominatorSet() : DominatorSetBase(false) {}
101 virtual const char *getPassName() const {
102 return "Dominator Set Construction";
105 virtual bool runOnFunction(Function &F);
107 // getAnalysisUsage - This simply provides a dominator set
108 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
109 AU.setPreservesAll();
115 //===-------------------------------------
116 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
117 // compute the post-dominator set.
119 struct PostDominatorSet : public DominatorSetBase {
120 static AnalysisID ID; // Build post-dominator set
122 PostDominatorSet() : DominatorSetBase(true) {}
124 virtual const char *getPassName() const {
125 return "Post-Dominator Set Construction";
128 virtual bool runOnFunction(Function &F);
130 // getAnalysisUsage - This obviously provides a dominator set, but it also
131 // uses the UnifyFunctionExitNode pass if building post-dominators
133 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
140 //===----------------------------------------------------------------------===//
142 // ImmediateDominators - Calculate the immediate dominator for each node in a
145 class ImmediateDominatorsBase : public DominatorBase {
147 std::map<BasicBlock*, BasicBlock*> IDoms;
148 void calcIDoms(const DominatorSetBase &DS);
150 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
152 virtual void releaseMemory() { IDoms.clear(); }
154 // Accessor interface:
155 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
156 typedef IDomMapType::const_iterator const_iterator;
157 inline const_iterator begin() const { return IDoms.begin(); }
158 inline const_iterator end() const { return IDoms.end(); }
159 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
161 // operator[] - Return the idom for the specified basic block. The start
162 // node returns null, because it does not have an immediate dominator.
164 inline BasicBlock *operator[](BasicBlock *BB) const {
165 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
166 return I != IDoms.end() ? I->second : 0;
170 //===-------------------------------------
171 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
172 // is used to compute a normal immediate dominator set.
174 struct ImmediateDominators : public ImmediateDominatorsBase {
175 static AnalysisID ID; // Build immediate dominators
177 ImmediateDominators() : ImmediateDominatorsBase(false) {}
179 virtual const char *getPassName() const {
180 return "Immediate Dominators Construction";
183 virtual bool runOnFunction(Function &F) {
184 IDoms.clear(); // Reset from the last time we were run...
185 DominatorSet &DS = getAnalysis<DominatorSet>();
191 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
192 AU.setPreservesAll();
194 AU.addRequired(DominatorSet::ID);
199 //===-------------------------------------
200 // ImmediatePostDominators Class - Concrete subclass of ImmediateDominatorsBase
201 // that is used to compute the immediate post-dominators.
203 struct ImmediatePostDominators : public ImmediateDominatorsBase {
204 static AnalysisID ID; // Build immediate postdominators
206 ImmediatePostDominators() : ImmediateDominatorsBase(true) {}
208 virtual const char *getPassName() const {
209 return "Immediate Post-Dominators Construction";
212 virtual bool runOnFunction(Function &F) {
213 IDoms.clear(); // Reset from the last time we were run...
214 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
220 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
221 AU.setPreservesAll();
222 AU.addRequired(PostDominatorSet::ID);
229 //===----------------------------------------------------------------------===//
231 // DominatorTree - Calculate the immediate dominator tree for a function.
233 class DominatorTreeBase : public DominatorBase {
239 std::map<BasicBlock*, Node*> Nodes;
241 typedef std::map<BasicBlock*, Node*> NodeMapType;
243 class Node2 : public std::vector<Node*> {
244 friend class DominatorTree;
245 friend class PostDominatorTree;
249 inline BasicBlock *getNode() const { return TheNode; }
250 inline Node2 *getIDom() const { return IDom; }
251 inline const std::vector<Node*> &getChildren() const { return *this; }
253 // dominates - Returns true iff this dominates N. Note that this is not a
254 // constant time operation!
255 inline bool dominates(const Node2 *N) const {
257 while ((IDom = N->getIDom()) != 0 && IDom != this)
258 N = IDom; // Walk up the tree
263 inline Node2(BasicBlock *node, Node *iDom)
264 : TheNode(node), IDom(iDom) {}
265 inline Node2 *addChild(Node *C) { push_back(C); return C; }
269 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
270 ~DominatorTreeBase() { reset(); }
272 virtual void releaseMemory() { reset(); }
274 inline Node *operator[](BasicBlock *BB) const {
275 NodeMapType::const_iterator i = Nodes.find(BB);
276 return (i != Nodes.end()) ? i->second : 0;
281 //===-------------------------------------
282 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
283 // compute a normal dominator tree.
285 struct DominatorTree : public DominatorTreeBase {
286 static AnalysisID ID; // Build dominator tree
288 DominatorTree() : DominatorTreeBase(false) {}
290 virtual const char *getPassName() const {
291 return "Dominator Tree Construction";
294 virtual bool runOnFunction(Function &F) {
295 reset(); // Reset from the last time we were run...
296 DominatorSet &DS = getAnalysis<DominatorSet>();
302 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
303 AU.setPreservesAll();
305 AU.addRequired(DominatorSet::ID);
308 void calculate(const DominatorSet &DS);
312 //===-------------------------------------
313 // PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
314 // compute the a post-dominator tree.
316 struct PostDominatorTree : public DominatorTreeBase {
317 static AnalysisID ID; // Build immediate postdominators
319 PostDominatorTree() : DominatorTreeBase(true) {}
321 virtual const char *getPassName() const {
322 return "Post-Dominator Tree Construction";
325 virtual bool runOnFunction(Function &F) {
326 reset(); // Reset from the last time we were run...
327 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
333 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
334 AU.setPreservesAll();
335 AU.addRequired(PostDominatorSet::ID);
339 void calculate(const PostDominatorSet &DS);
343 //===----------------------------------------------------------------------===//
345 // DominanceFrontier - Calculate the dominance frontiers for a function.
347 class DominanceFrontierBase : public DominatorBase {
349 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
350 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
352 DomSetMapType Frontiers;
354 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
356 virtual void releaseMemory() { Frontiers.clear(); }
358 // Accessor interface:
359 typedef DomSetMapType::const_iterator const_iterator;
360 inline const_iterator begin() const { return Frontiers.begin(); }
361 inline const_iterator end() const { return Frontiers.end(); }
362 inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
366 //===-------------------------------------
367 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
368 // compute a normal dominator tree.
370 struct DominanceFrontier : public DominanceFrontierBase {
371 static AnalysisID ID; // Build dominance frontier
373 DominanceFrontier() : DominanceFrontierBase(false) {}
375 virtual const char *getPassName() const {
376 return "Dominance Frontier Construction";
379 virtual bool runOnFunction(Function &) {
381 DominatorTree &DT = getAnalysis<DominatorTree>();
383 calculate(DT, DT[Root]);
387 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
388 AU.setPreservesAll();
390 AU.addRequired(DominatorTree::ID);
393 const DomSetType &calculate(const DominatorTree &DT,
394 const DominatorTree::Node *Node);
398 //===-------------------------------------
400 // PostDominanceFrontier Class - Concrete subclass of DominanceFrontier that is
401 // used to compute the a post-dominance frontier.
403 struct PostDominanceFrontier : public DominanceFrontierBase {
404 static AnalysisID ID; // Build post dominance frontier
406 PostDominanceFrontier() : DominanceFrontierBase(true) {}
408 virtual const char *getPassName() const {
409 return "Post-Dominance Frontier Construction";
412 virtual bool runOnFunction(Function &) {
414 PostDominatorTree &DT = getAnalysis<PostDominatorTree>();
416 calculate(DT, DT[Root]);
420 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
421 AU.setPreservesAll();
422 AU.addRequired(PostDominatorTree::ID);
426 const DomSetType &calculate(const PostDominatorTree &DT,
427 const DominatorTree::Node *Node);