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_ANALYSIS_DOMINATORS_H
19 #define LLVM_ANALYSIS_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 DominatorSet() : DominatorSetBase(false) {}
102 virtual bool runOnFunction(Function &F);
104 // getAnalysisUsage - This simply provides a dominator set
105 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
106 AU.setPreservesAll();
109 void calculateDominatorsFromBlock(BasicBlock *BB);
113 //===----------------------------------------------------------------------===//
115 // ImmediateDominators - Calculate the immediate dominator for each node in a
118 class ImmediateDominatorsBase : public DominatorBase {
120 std::map<BasicBlock*, BasicBlock*> IDoms;
121 void calcIDoms(const DominatorSetBase &DS);
123 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
125 virtual void releaseMemory() { IDoms.clear(); }
127 // Accessor interface:
128 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
129 typedef IDomMapType::const_iterator const_iterator;
130 inline const_iterator begin() const { return IDoms.begin(); }
131 inline const_iterator end() const { return IDoms.end(); }
132 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
134 // operator[] - Return the idom for the specified basic block. The start
135 // node returns null, because it does not have an immediate dominator.
137 inline BasicBlock *operator[](BasicBlock *BB) const {
138 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
139 return I != IDoms.end() ? I->second : 0;
142 // print - Convert to human readable form
143 virtual void print(std::ostream &OS) const;
146 //===-------------------------------------
147 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
148 // is used to compute a normal immediate dominator set.
150 struct ImmediateDominators : public ImmediateDominatorsBase {
151 ImmediateDominators() : ImmediateDominatorsBase(false) {}
153 virtual bool runOnFunction(Function &F) {
154 IDoms.clear(); // Reset from the last time we were run...
155 DominatorSet &DS = getAnalysis<DominatorSet>();
161 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
162 AU.setPreservesAll();
163 AU.addRequired<DominatorSet>();
168 //===----------------------------------------------------------------------===//
170 // DominatorTree - Calculate the immediate dominator tree for a function.
172 class DominatorTreeBase : public DominatorBase {
178 std::map<BasicBlock*, Node*> Nodes;
180 typedef std::map<BasicBlock*, Node*> NodeMapType;
182 class Node2 : public std::vector<Node*> {
183 friend class DominatorTree;
184 friend class PostDominatorTree;
188 inline BasicBlock *getNode() const { return TheNode; }
189 inline Node2 *getIDom() const { return IDom; }
190 inline const std::vector<Node*> &getChildren() const { return *this; }
192 // dominates - Returns true iff this dominates N. Note that this is not a
193 // constant time operation!
194 inline bool dominates(const Node2 *N) const {
196 while ((IDom = N->getIDom()) != 0 && IDom != this)
197 N = IDom; // Walk up the tree
202 inline Node2(BasicBlock *node, Node *iDom)
203 : TheNode(node), IDom(iDom) {}
204 inline Node2 *addChild(Node *C) { push_back(C); return C; }
208 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
209 ~DominatorTreeBase() { reset(); }
211 virtual void releaseMemory() { reset(); }
213 inline Node *operator[](BasicBlock *BB) const {
214 NodeMapType::const_iterator i = Nodes.find(BB);
215 return (i != Nodes.end()) ? i->second : 0;
218 // print - Convert to human readable form
219 virtual void print(std::ostream &OS) const;
223 //===-------------------------------------
224 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
225 // compute a normal dominator tree.
227 struct DominatorTree : public DominatorTreeBase {
228 DominatorTree() : DominatorTreeBase(false) {}
230 virtual bool runOnFunction(Function &F) {
231 reset(); // Reset from the last time we were run...
232 DominatorSet &DS = getAnalysis<DominatorSet>();
238 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
239 AU.setPreservesAll();
240 AU.addRequired<DominatorSet>();
243 void calculate(const DominatorSet &DS);
247 //===----------------------------------------------------------------------===//
249 // DominanceFrontier - Calculate the dominance frontiers for a function.
251 class DominanceFrontierBase : public DominatorBase {
253 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
254 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
256 DomSetMapType Frontiers;
258 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
260 virtual void releaseMemory() { Frontiers.clear(); }
262 // Accessor interface:
263 typedef DomSetMapType::const_iterator const_iterator;
264 inline const_iterator begin() const { return Frontiers.begin(); }
265 inline const_iterator end() const { return Frontiers.end(); }
266 inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
268 // print - Convert to human readable form
269 virtual void print(std::ostream &OS) const;
273 //===-------------------------------------
274 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
275 // compute a normal dominator tree.
277 struct DominanceFrontier : public DominanceFrontierBase {
278 DominanceFrontier() : DominanceFrontierBase(false) {}
280 virtual bool runOnFunction(Function &) {
282 DominatorTree &DT = getAnalysis<DominatorTree>();
284 calculate(DT, DT[Root]);
288 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
289 AU.setPreservesAll();
290 AU.addRequired<DominatorTree>();
293 const DomSetType &calculate(const DominatorTree &DT,
294 const DominatorTree::Node *Node);