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();
113 //===-------------------------------------
114 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
115 // compute the post-dominator set.
117 struct PostDominatorSet : public DominatorSetBase {
118 static AnalysisID ID; // Build post-dominator set
120 PostDominatorSet() : DominatorSetBase(true) {}
122 virtual bool runOnFunction(Function &F);
124 // getAnalysisUsage - This obviously provides a dominator set, but it also
125 // uses the UnifyFunctionExitNode pass if building post-dominators
127 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
134 //===----------------------------------------------------------------------===//
136 // ImmediateDominators - Calculate the immediate dominator for each node in a
139 class ImmediateDominatorsBase : public DominatorBase {
141 std::map<BasicBlock*, BasicBlock*> IDoms;
142 void calcIDoms(const DominatorSetBase &DS);
144 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
146 virtual void releaseMemory() { IDoms.clear(); }
148 // Accessor interface:
149 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
150 typedef IDomMapType::const_iterator const_iterator;
151 inline const_iterator begin() const { return IDoms.begin(); }
152 inline const_iterator end() const { return IDoms.end(); }
153 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
155 // operator[] - Return the idom for the specified basic block. The start
156 // node returns null, because it does not have an immediate dominator.
158 inline BasicBlock *operator[](BasicBlock *BB) const {
159 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
160 return I != IDoms.end() ? I->second : 0;
163 // print - Convert to human readable form
164 virtual void print(std::ostream &OS) const;
167 //===-------------------------------------
168 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
169 // is used to compute a normal immediate dominator set.
171 struct ImmediateDominators : public ImmediateDominatorsBase {
172 static AnalysisID ID; // Build immediate dominators
174 ImmediateDominators() : ImmediateDominatorsBase(false) {}
176 virtual bool runOnFunction(Function &F) {
177 IDoms.clear(); // Reset from the last time we were run...
178 DominatorSet &DS = getAnalysis<DominatorSet>();
184 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
185 AU.setPreservesAll();
186 AU.addRequired(DominatorSet::ID);
191 //===-------------------------------------
192 // ImmediatePostDominators Class - Concrete subclass of ImmediateDominatorsBase
193 // that is used to compute the immediate post-dominators.
195 struct ImmediatePostDominators : public ImmediateDominatorsBase {
196 static AnalysisID ID; // Build immediate postdominators
198 ImmediatePostDominators() : ImmediateDominatorsBase(true) {}
200 virtual bool runOnFunction(Function &F) {
201 IDoms.clear(); // Reset from the last time we were run...
202 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
208 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.setPreservesAll();
210 AU.addRequired(PostDominatorSet::ID);
216 //===----------------------------------------------------------------------===//
218 // DominatorTree - Calculate the immediate dominator tree for a function.
220 class DominatorTreeBase : public DominatorBase {
226 std::map<BasicBlock*, Node*> Nodes;
228 typedef std::map<BasicBlock*, Node*> NodeMapType;
230 class Node2 : public std::vector<Node*> {
231 friend class DominatorTree;
232 friend class PostDominatorTree;
236 inline BasicBlock *getNode() const { return TheNode; }
237 inline Node2 *getIDom() const { return IDom; }
238 inline const std::vector<Node*> &getChildren() const { return *this; }
240 // dominates - Returns true iff this dominates N. Note that this is not a
241 // constant time operation!
242 inline bool dominates(const Node2 *N) const {
244 while ((IDom = N->getIDom()) != 0 && IDom != this)
245 N = IDom; // Walk up the tree
250 inline Node2(BasicBlock *node, Node *iDom)
251 : TheNode(node), IDom(iDom) {}
252 inline Node2 *addChild(Node *C) { push_back(C); return C; }
256 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
257 ~DominatorTreeBase() { reset(); }
259 virtual void releaseMemory() { reset(); }
261 inline Node *operator[](BasicBlock *BB) const {
262 NodeMapType::const_iterator i = Nodes.find(BB);
263 return (i != Nodes.end()) ? i->second : 0;
266 // print - Convert to human readable form
267 virtual void print(std::ostream &OS) const;
271 //===-------------------------------------
272 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
273 // compute a normal dominator tree.
275 struct DominatorTree : public DominatorTreeBase {
276 static AnalysisID ID; // Build dominator tree
278 DominatorTree() : DominatorTreeBase(false) {}
280 virtual bool runOnFunction(Function &F) {
281 reset(); // Reset from the last time we were run...
282 DominatorSet &DS = getAnalysis<DominatorSet>();
288 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
289 AU.setPreservesAll();
290 AU.addRequired(DominatorSet::ID);
293 void calculate(const DominatorSet &DS);
297 //===-------------------------------------
298 // PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
299 // compute the a post-dominator tree.
301 struct PostDominatorTree : public DominatorTreeBase {
302 static AnalysisID ID; // Build immediate postdominators
304 PostDominatorTree() : DominatorTreeBase(true) {}
306 virtual bool runOnFunction(Function &F) {
307 reset(); // Reset from the last time we were run...
308 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
314 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
315 AU.setPreservesAll();
316 AU.addRequired(PostDominatorSet::ID);
319 void calculate(const PostDominatorSet &DS);
323 //===----------------------------------------------------------------------===//
325 // DominanceFrontier - Calculate the dominance frontiers for a function.
327 class DominanceFrontierBase : public DominatorBase {
329 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
330 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
332 DomSetMapType Frontiers;
334 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
336 virtual void releaseMemory() { Frontiers.clear(); }
338 // Accessor interface:
339 typedef DomSetMapType::const_iterator const_iterator;
340 inline const_iterator begin() const { return Frontiers.begin(); }
341 inline const_iterator end() const { return Frontiers.end(); }
342 inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
344 // print - Convert to human readable form
345 virtual void print(std::ostream &OS) const;
349 //===-------------------------------------
350 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
351 // compute a normal dominator tree.
353 struct DominanceFrontier : public DominanceFrontierBase {
354 static AnalysisID ID; // Build dominance frontier
356 DominanceFrontier() : DominanceFrontierBase(false) {}
358 virtual bool runOnFunction(Function &) {
360 DominatorTree &DT = getAnalysis<DominatorTree>();
362 calculate(DT, DT[Root]);
366 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
367 AU.setPreservesAll();
368 AU.addRequired(DominatorTree::ID);
371 const DomSetType &calculate(const DominatorTree &DT,
372 const DominatorTree::Node *Node);
376 //===-------------------------------------
378 // PostDominanceFrontier Class - Concrete subclass of DominanceFrontier that is
379 // used to compute the a post-dominance frontier.
381 struct PostDominanceFrontier : public DominanceFrontierBase {
382 static AnalysisID ID; // Build post dominance frontier
384 PostDominanceFrontier() : DominanceFrontierBase(true) {}
386 virtual bool runOnFunction(Function &) {
388 PostDominatorTree &DT = getAnalysis<PostDominatorTree>();
390 calculate(DT, DT[Root]);
394 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
395 AU.setPreservesAll();
396 AU.addRequired(PostDominatorTree::ID);
399 const DomSetType &calculate(const PostDominatorTree &DT,
400 const DominatorTree::Node *Node);