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 bool runOnFunction(Function &F);
103 // getAnalysisUsage - This simply provides a dominator set
104 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
105 AU.setPreservesAll();
111 //===-------------------------------------
112 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
113 // compute the post-dominator set.
115 struct PostDominatorSet : public DominatorSetBase {
116 static AnalysisID ID; // Build post-dominator set
118 PostDominatorSet() : DominatorSetBase(true) {}
120 virtual bool runOnFunction(Function &F);
122 // getAnalysisUsage - This obviously provides a dominator set, but it also
123 // uses the UnifyFunctionExitNode pass if building post-dominators
125 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
132 //===----------------------------------------------------------------------===//
134 // ImmediateDominators - Calculate the immediate dominator for each node in a
137 class ImmediateDominatorsBase : public DominatorBase {
139 std::map<BasicBlock*, BasicBlock*> IDoms;
140 void calcIDoms(const DominatorSetBase &DS);
142 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
144 virtual void releaseMemory() { IDoms.clear(); }
146 // Accessor interface:
147 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
148 typedef IDomMapType::const_iterator const_iterator;
149 inline const_iterator begin() const { return IDoms.begin(); }
150 inline const_iterator end() const { return IDoms.end(); }
151 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
153 // operator[] - Return the idom for the specified basic block. The start
154 // node returns null, because it does not have an immediate dominator.
156 inline BasicBlock *operator[](BasicBlock *BB) const {
157 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
158 return I != IDoms.end() ? I->second : 0;
162 //===-------------------------------------
163 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
164 // is used to compute a normal immediate dominator set.
166 struct ImmediateDominators : public ImmediateDominatorsBase {
167 static AnalysisID ID; // Build immediate dominators
169 ImmediateDominators() : ImmediateDominatorsBase(false) {}
171 virtual bool runOnFunction(Function &F) {
172 IDoms.clear(); // Reset from the last time we were run...
173 DominatorSet &DS = getAnalysis<DominatorSet>();
179 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
180 AU.setPreservesAll();
182 AU.addRequired(DominatorSet::ID);
187 //===-------------------------------------
188 // ImmediatePostDominators Class - Concrete subclass of ImmediateDominatorsBase
189 // that is used to compute the immediate post-dominators.
191 struct ImmediatePostDominators : public ImmediateDominatorsBase {
192 static AnalysisID ID; // Build immediate postdominators
194 ImmediatePostDominators() : ImmediateDominatorsBase(true) {}
196 virtual bool runOnFunction(Function &F) {
197 IDoms.clear(); // Reset from the last time we were run...
198 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
204 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
205 AU.setPreservesAll();
206 AU.addRequired(PostDominatorSet::ID);
213 //===----------------------------------------------------------------------===//
215 // DominatorTree - Calculate the immediate dominator tree for a function.
217 class DominatorTreeBase : public DominatorBase {
223 std::map<BasicBlock*, Node*> Nodes;
225 typedef std::map<BasicBlock*, Node*> NodeMapType;
227 class Node2 : public std::vector<Node*> {
228 friend class DominatorTree;
229 friend class PostDominatorTree;
233 inline BasicBlock *getNode() const { return TheNode; }
234 inline Node2 *getIDom() const { return IDom; }
235 inline const std::vector<Node*> &getChildren() const { return *this; }
237 // dominates - Returns true iff this dominates N. Note that this is not a
238 // constant time operation!
239 inline bool dominates(const Node2 *N) const {
241 while ((IDom = N->getIDom()) != 0 && IDom != this)
242 N = IDom; // Walk up the tree
247 inline Node2(BasicBlock *node, Node *iDom)
248 : TheNode(node), IDom(iDom) {}
249 inline Node2 *addChild(Node *C) { push_back(C); return C; }
253 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
254 ~DominatorTreeBase() { reset(); }
256 virtual void releaseMemory() { reset(); }
258 inline Node *operator[](BasicBlock *BB) const {
259 NodeMapType::const_iterator i = Nodes.find(BB);
260 return (i != Nodes.end()) ? i->second : 0;
265 //===-------------------------------------
266 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
267 // compute a normal dominator tree.
269 struct DominatorTree : public DominatorTreeBase {
270 static AnalysisID ID; // Build dominator tree
272 DominatorTree() : DominatorTreeBase(false) {}
274 virtual bool runOnFunction(Function &F) {
275 reset(); // Reset from the last time we were run...
276 DominatorSet &DS = getAnalysis<DominatorSet>();
282 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
283 AU.setPreservesAll();
285 AU.addRequired(DominatorSet::ID);
288 void calculate(const DominatorSet &DS);
292 //===-------------------------------------
293 // PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
294 // compute the a post-dominator tree.
296 struct PostDominatorTree : public DominatorTreeBase {
297 static AnalysisID ID; // Build immediate postdominators
299 PostDominatorTree() : DominatorTreeBase(true) {}
301 virtual bool runOnFunction(Function &F) {
302 reset(); // Reset from the last time we were run...
303 PostDominatorSet &DS = getAnalysis<PostDominatorSet>();
309 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
310 AU.setPreservesAll();
311 AU.addRequired(PostDominatorSet::ID);
315 void calculate(const PostDominatorSet &DS);
319 //===----------------------------------------------------------------------===//
321 // DominanceFrontier - Calculate the dominance frontiers for a function.
323 class DominanceFrontierBase : public DominatorBase {
325 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
326 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
328 DomSetMapType Frontiers;
330 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
332 virtual void releaseMemory() { Frontiers.clear(); }
334 // Accessor interface:
335 typedef DomSetMapType::const_iterator const_iterator;
336 inline const_iterator begin() const { return Frontiers.begin(); }
337 inline const_iterator end() const { return Frontiers.end(); }
338 inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
342 //===-------------------------------------
343 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
344 // compute a normal dominator tree.
346 struct DominanceFrontier : public DominanceFrontierBase {
347 static AnalysisID ID; // Build dominance frontier
349 DominanceFrontier() : DominanceFrontierBase(false) {}
351 virtual bool runOnFunction(Function &) {
353 DominatorTree &DT = getAnalysis<DominatorTree>();
355 calculate(DT, DT[Root]);
359 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
360 AU.setPreservesAll();
362 AU.addRequired(DominatorTree::ID);
365 const DomSetType &calculate(const DominatorTree &DT,
366 const DominatorTree::Node *Node);
370 //===-------------------------------------
372 // PostDominanceFrontier Class - Concrete subclass of DominanceFrontier that is
373 // used to compute the a post-dominance frontier.
375 struct PostDominanceFrontier : public DominanceFrontierBase {
376 static AnalysisID ID; // Build post dominance frontier
378 PostDominanceFrontier() : DominanceFrontierBase(true) {}
380 virtual bool runOnFunction(Function &) {
382 PostDominatorTree &DT = getAnalysis<PostDominatorTree>();
384 calculate(DT, DT[Root]);
388 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
389 AU.setPreservesAll();
390 AU.addRequired(PostDominatorTree::ID);
394 const DomSetType &calculate(const PostDominatorTree &DT,
395 const DominatorTree::Node *Node);