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 // print - Convert to human readable form
86 virtual void print(std::ostream &OS) const;
88 // dominates - Return true if A dominates B. This performs the special checks
89 // neccesary if A and B are in the same basic block.
91 bool dominates(Instruction *A, Instruction *B) const;
95 //===-------------------------------------
96 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
97 // compute a normal dominator set.
99 struct DominatorSet : public DominatorSetBase {
100 static AnalysisID ID; // Build dominator set
102 DominatorSet() : DominatorSetBase(false) {}
104 virtual bool runOnFunction(Function &F);
106 // getAnalysisUsage - This simply provides a dominator set
107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.setPreservesAll();
114 //===-------------------------------------
115 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
116 // compute the post-dominator set.
118 struct PostDominatorSet : public DominatorSetBase {
119 static AnalysisID ID; // Build post-dominator set
121 PostDominatorSet() : DominatorSetBase(true) {}
123 virtual bool runOnFunction(Function &F);
125 // getAnalysisUsage - This obviously provides a dominator set, but it also
126 // uses the UnifyFunctionExitNode pass if building post-dominators
128 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
135 //===----------------------------------------------------------------------===//
137 // ImmediateDominators - Calculate the immediate dominator for each node in a
140 class ImmediateDominatorsBase : public DominatorBase {
142 std::map<BasicBlock*, BasicBlock*> IDoms;
143 void calcIDoms(const DominatorSetBase &DS);
145 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
147 virtual void releaseMemory() { IDoms.clear(); }
149 // Accessor interface:
150 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
151 typedef IDomMapType::const_iterator const_iterator;
152 inline const_iterator begin() const { return IDoms.begin(); }
153 inline const_iterator end() const { return IDoms.end(); }
154 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
156 // operator[] - Return the idom for the specified basic block. The start
157 // node returns null, because it does not have an immediate dominator.
159 inline BasicBlock *operator[](BasicBlock *BB) const {
160 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
161 return I != IDoms.end() ? I->second : 0;
164 // print - Convert to human readable form
165 virtual void print(std::ostream &OS) const;
168 //===-------------------------------------
169 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
170 // is used to compute a normal immediate dominator set.
172 struct ImmediateDominators : public ImmediateDominatorsBase {
173 static AnalysisID ID; // Build immediate dominators
175 ImmediateDominators() : ImmediateDominatorsBase(false) {}
177 virtual bool runOnFunction(Function &F) {
178 IDoms.clear(); // Reset from the last time we were run...
179 DominatorSet &DS = getAnalysis<DominatorSet>();
185 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
186 AU.setPreservesAll();
188 AU.addRequired(DominatorSet::ID);
193 //===-------------------------------------
194 // ImmediatePostDominators Class - Concrete subclass of ImmediateDominatorsBase
195 // that is used to compute the immediate post-dominators.
197 struct ImmediatePostDominators : public ImmediateDominatorsBase {
198 static AnalysisID ID; // Build immediate postdominators
200 ImmediatePostDominators() : ImmediateDominatorsBase(true) {}
202 virtual bool runOnFunction(Function &F) {
203 IDoms.clear(); // Reset from the last time we were run...
204 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
210 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
211 AU.setPreservesAll();
212 AU.addRequired(PostDominatorSet::ID);
219 //===----------------------------------------------------------------------===//
221 // DominatorTree - Calculate the immediate dominator tree for a function.
223 class DominatorTreeBase : public DominatorBase {
229 std::map<BasicBlock*, Node*> Nodes;
231 typedef std::map<BasicBlock*, Node*> NodeMapType;
233 class Node2 : public std::vector<Node*> {
234 friend class DominatorTree;
235 friend class PostDominatorTree;
239 inline BasicBlock *getNode() const { return TheNode; }
240 inline Node2 *getIDom() const { return IDom; }
241 inline const std::vector<Node*> &getChildren() const { return *this; }
243 // dominates - Returns true iff this dominates N. Note that this is not a
244 // constant time operation!
245 inline bool dominates(const Node2 *N) const {
247 while ((IDom = N->getIDom()) != 0 && IDom != this)
248 N = IDom; // Walk up the tree
253 inline Node2(BasicBlock *node, Node *iDom)
254 : TheNode(node), IDom(iDom) {}
255 inline Node2 *addChild(Node *C) { push_back(C); return C; }
259 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
260 ~DominatorTreeBase() { reset(); }
262 virtual void releaseMemory() { reset(); }
264 inline Node *operator[](BasicBlock *BB) const {
265 NodeMapType::const_iterator i = Nodes.find(BB);
266 return (i != Nodes.end()) ? i->second : 0;
269 // print - Convert to human readable form
270 virtual void print(std::ostream &OS) const;
274 //===-------------------------------------
275 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
276 // compute a normal dominator tree.
278 struct DominatorTree : public DominatorTreeBase {
279 static AnalysisID ID; // Build dominator tree
281 DominatorTree() : DominatorTreeBase(false) {}
283 virtual bool runOnFunction(Function &F) {
284 reset(); // Reset from the last time we were run...
285 DominatorSet &DS = getAnalysis<DominatorSet>();
291 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
292 AU.setPreservesAll();
294 AU.addRequired(DominatorSet::ID);
297 void calculate(const DominatorSet &DS);
301 //===-------------------------------------
302 // PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
303 // compute the a post-dominator tree.
305 struct PostDominatorTree : public DominatorTreeBase {
306 static AnalysisID ID; // Build immediate postdominators
308 PostDominatorTree() : DominatorTreeBase(true) {}
310 virtual bool runOnFunction(Function &F) {
311 reset(); // Reset from the last time we were run...
312 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
318 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
319 AU.setPreservesAll();
320 AU.addRequired(PostDominatorSet::ID);
324 void calculate(const PostDominatorSet &DS);
328 //===----------------------------------------------------------------------===//
330 // DominanceFrontier - Calculate the dominance frontiers for a function.
332 class DominanceFrontierBase : public DominatorBase {
334 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
335 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
337 DomSetMapType Frontiers;
339 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
341 virtual void releaseMemory() { Frontiers.clear(); }
343 // Accessor interface:
344 typedef DomSetMapType::const_iterator const_iterator;
345 inline const_iterator begin() const { return Frontiers.begin(); }
346 inline const_iterator end() const { return Frontiers.end(); }
347 inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
349 // print - Convert to human readable form
350 virtual void print(std::ostream &OS) const;
354 //===-------------------------------------
355 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
356 // compute a normal dominator tree.
358 struct DominanceFrontier : public DominanceFrontierBase {
359 static AnalysisID ID; // Build dominance frontier
361 DominanceFrontier() : DominanceFrontierBase(false) {}
363 virtual bool runOnFunction(Function &) {
365 DominatorTree &DT = getAnalysis<DominatorTree>();
367 calculate(DT, DT[Root]);
371 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
372 AU.setPreservesAll();
374 AU.addRequired(DominatorTree::ID);
377 const DomSetType &calculate(const DominatorTree &DT,
378 const DominatorTree::Node *Node);
382 //===-------------------------------------
384 // PostDominanceFrontier Class - Concrete subclass of DominanceFrontier that is
385 // used to compute the a post-dominance frontier.
387 struct PostDominanceFrontier : public DominanceFrontierBase {
388 static AnalysisID ID; // Build post dominance frontier
390 PostDominanceFrontier() : DominanceFrontierBase(true) {}
392 virtual bool runOnFunction(Function &) {
394 PostDominatorTree &DT = getAnalysis<PostDominatorTree>();
396 calculate(DT, DT[Root]);
400 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
401 AU.setPreservesAll();
402 AU.addRequired(PostDominatorTree::ID);
406 const DomSetType &calculate(const PostDominatorTree &DT,
407 const DominatorTree::Node *Node);