1 //===- llvm/Analysis/DominatorSet.h - Dominator Set Calculation --*- C++ -*--=//
3 // This file defines the following classes:
4 // 1. DominatorSet: Calculates the [reverse] dominator set for a method
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
29 //===----------------------------------------------------------------------===//
31 // DominatorBase - Base class that other, more interesting dominator analyses
36 const BasicBlock *Root;
37 inline DominatorBase(const BasicBlock *root = 0) : Root(root) {}
39 inline const BasicBlock *getRoot() const { return Root; }
40 bool isPostDominator() const; // Returns true if analysis based of postdoms
43 //===----------------------------------------------------------------------===//
45 // DominatorSet - Maintain a set<const BasicBlock*> for every basic block in a
46 // method, that represents the blocks that dominate the block.
48 class DominatorSet : public DominatorBase {
50 typedef set<const BasicBlock*> DomSetType; // Dom set for a bb
51 typedef map<const BasicBlock *, DomSetType> DomSetMapType; // Map of dom sets
55 void calcForwardDominatorSet(const Method *M);
57 // DominatorSet ctor - Build either the dominator set or the post-dominator
58 // set for a method... Building the postdominator set may require the analysis
59 // routine to modify the method so that there is only a single return in the
62 DominatorSet(const Method *M);
63 DominatorSet( Method *M, bool PostDomSet);
65 // Accessor interface:
66 typedef DomSetMapType::const_iterator const_iterator;
67 inline const_iterator begin() const { return Doms.begin(); }
68 inline const_iterator end() const { return Doms.end(); }
69 inline const_iterator find(const BasicBlock* B) const { return Doms.find(B); }
71 // getDominators - Return the set of basic blocks that dominate the specified
74 inline const DomSetType &getDominators(const BasicBlock *BB) const {
75 const_iterator I = find(BB);
76 assert(I != end() && "BB not in method!");
80 // dominates - Return true if A dominates B.
82 inline bool dominates(const BasicBlock *A, const BasicBlock *B) const {
83 return getDominators(B).count(A) != 0;
88 //===----------------------------------------------------------------------===//
90 // ImmediateDominators - Calculate the immediate dominator for each node in a
93 class ImmediateDominators : public DominatorBase {
94 map<const BasicBlock*, const BasicBlock*> IDoms;
95 void calcIDoms(const DominatorSet &DS);
98 // ImmediateDominators ctor - Calculate the idom mapping, for a method, or
99 // from a dominator set calculated for something else...
101 inline ImmediateDominators(const DominatorSet &DS)
102 : DominatorBase(DS.getRoot()) {
103 calcIDoms(DS); // Can be used to make rev-idoms
106 // Accessor interface:
107 typedef map<const BasicBlock*, const BasicBlock*> IDomMapType;
108 typedef IDomMapType::const_iterator const_iterator;
109 inline const_iterator begin() const { return IDoms.begin(); }
110 inline const_iterator end() const { return IDoms.end(); }
111 inline const_iterator find(const BasicBlock* B) const { return IDoms.find(B);}
113 // operator[] - Return the idom for the specified basic block. The start
114 // node returns null, because it does not have an immediate dominator.
116 inline const BasicBlock *operator[](const BasicBlock *BB) const {
117 map<const BasicBlock*, const BasicBlock*>::const_iterator I =
119 return I != IDoms.end() ? I->second : 0;
124 //===----------------------------------------------------------------------===//
126 // DominatorTree - Calculate the immediate dominator tree for a method.
128 class DominatorTree : public DominatorBase {
130 map<const BasicBlock*, Node*> Nodes;
131 void calculate(const DominatorSet &DS);
132 typedef map<const BasicBlock*, Node*> NodeMapType;
134 class Node : public vector<Node*> {
135 friend class DominatorTree;
136 const BasicBlock *TheNode;
139 inline const BasicBlock *getNode() const { return TheNode; }
140 inline Node *getIDom() const { return IDom; }
141 inline const vector<Node*> &getChildren() const { return *this; }
143 // dominates - Returns true iff this dominates N. Note that this is not a
144 // constant time operation!
145 inline bool dominates(const Node *N) const {
147 while ((IDom = N->getIDom()) != 0 && IDom != this)
148 N = IDom; // Walk up the tree
153 inline Node(const BasicBlock *node, Node *iDom)
154 : TheNode(node), IDom(iDom) {}
155 inline Node *addChild(Node *C) { push_back(C); return C; }
159 // DominatorTree ctors - Compute a dominator tree, given various amounts of
160 // previous knowledge...
161 inline DominatorTree(const DominatorSet &DS) : DominatorBase(DS.getRoot()) {
165 DominatorTree(const ImmediateDominators &IDoms);
168 inline const Node *operator[](const BasicBlock *BB) const {
169 NodeMapType::const_iterator i = Nodes.find(BB);
170 return (i != Nodes.end()) ? i->second : 0;
175 //===----------------------------------------------------------------------===//
177 // DominanceFrontier - Calculate the dominance frontiers for a method.
179 class DominanceFrontier : public DominatorBase {
180 typedef set<const BasicBlock*> DomSetType; // Dom set for a bb
181 typedef map<const BasicBlock *, DomSetType> DomSetMapType; // Map of dom sets
183 DomSetMapType Frontiers;
184 const DomSetType &calcDomFrontier(const DominatorTree &DT,
185 const DominatorTree::Node *Node);
186 const DomSetType &calcPostDomFrontier(const DominatorTree &DT,
187 const DominatorTree::Node *Node);
189 DominanceFrontier(const DominatorSet &DS) : DominatorBase(DS.getRoot()) {
190 const DominatorTree DT(DS);
191 if (isPostDominator())
192 calcPostDomFrontier(DT, DT[Root]);
194 calcDomFrontier(DT, DT[Root]);
196 DominanceFrontier(const ImmediateDominators &ID)
197 : DominatorBase(ID.getRoot()) {
198 const DominatorTree DT(ID);
199 if (isPostDominator())
200 calcPostDomFrontier(DT, DT[Root]);
202 calcDomFrontier(DT, DT[Root]);
204 DominanceFrontier(const DominatorTree &DT) : DominatorBase(DT.getRoot()) {
205 if (isPostDominator())
206 calcPostDomFrontier(DT, DT[Root]);
208 calcDomFrontier(DT, DT[Root]);
211 // Accessor interface:
212 typedef DomSetMapType::const_iterator const_iterator;
213 inline const_iterator begin() const { return Frontiers.begin(); }
214 inline const_iterator end() const { return Frontiers.end(); }
215 inline const_iterator find(const BasicBlock* B) const { return Frontiers.find(B);}
218 } // End namespace cfg