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"
24 //===----------------------------------------------------------------------===//
26 // DominatorBase - Base class that other, more interesting dominator analyses
29 class DominatorBase : public FunctionPass {
32 const bool IsPostDominators;
34 inline DominatorBase(bool isPostDom) : Root(0), IsPostDominators(isPostDom) {}
36 inline BasicBlock *getRoot() const { return Root; }
38 // Returns true if analysis based of postdoms
39 bool isPostDominator() const { return IsPostDominators; }
42 //===----------------------------------------------------------------------===//
44 // DominatorSet - Maintain a set<BasicBlock*> for every basic block in a
45 // function, that represents the blocks that dominate the block.
47 class DominatorSet : public DominatorBase {
49 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
51 typedef std::map<BasicBlock*, DomSetType> DomSetMapType;
55 void calcForwardDominatorSet(Function *F);
56 void calcPostDominatorSet(Function *F);
58 // DominatorSet ctor - Build either the dominator set or the post-dominator
59 // set for a function...
61 static AnalysisID ID; // Build dominator set
62 static AnalysisID PostDomID; // Build postdominator set
64 DominatorSet(AnalysisID id) : DominatorBase(id == PostDomID) {}
66 virtual bool runOnFunction(Function *F);
68 // Accessor interface:
69 typedef DomSetMapType::const_iterator const_iterator;
70 typedef DomSetMapType::iterator iterator;
71 inline const_iterator begin() const { return Doms.begin(); }
72 inline iterator begin() { return Doms.begin(); }
73 inline const_iterator end() const { return Doms.end(); }
74 inline iterator end() { return Doms.end(); }
75 inline const_iterator find(BasicBlock* B) const { return Doms.find(B); }
76 inline iterator find(BasicBlock* B) { return Doms.find(B); }
78 // getDominators - Return the set of basic blocks that dominate the specified
81 inline const DomSetType &getDominators(BasicBlock *BB) const {
82 const_iterator I = find(BB);
83 assert(I != end() && "BB not in function!");
87 // dominates - Return true if A dominates B.
89 inline bool dominates(BasicBlock *A, BasicBlock *B) const {
90 return getDominators(B).count(A) != 0;
93 // getAnalysisUsage - This obviously provides a dominator set, but it also
94 // uses the UnifyFunctionExitNode pass if building post-dominators
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
100 //===----------------------------------------------------------------------===//
102 // ImmediateDominators - Calculate the immediate dominator for each node in a
105 class ImmediateDominators : public DominatorBase {
106 std::map<BasicBlock*, BasicBlock*> IDoms;
107 void calcIDoms(const DominatorSet &DS);
110 // ImmediateDominators ctor - Calculate the idom or post-idom mapping,
113 static AnalysisID ID; // Build immediate dominators
114 static AnalysisID PostDomID; // Build immediate postdominators
116 ImmediateDominators(AnalysisID id) : DominatorBase(id == PostDomID) {}
118 virtual bool runOnFunction(Function *F) {
119 IDoms.clear(); // Reset from the last time we were run...
121 if (isPostDominator())
122 DS = &getAnalysis<DominatorSet>(DominatorSet::PostDomID);
124 DS = &getAnalysis<DominatorSet>();
126 Root = DS->getRoot();
127 calcIDoms(*DS); // Can be used to make rev-idoms
131 // Accessor interface:
132 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
133 typedef IDomMapType::const_iterator const_iterator;
134 inline const_iterator begin() const { return IDoms.begin(); }
135 inline const_iterator end() const { return IDoms.end(); }
136 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
138 // operator[] - Return the idom for the specified basic block. The start
139 // node returns null, because it does not have an immediate dominator.
141 inline BasicBlock *operator[](BasicBlock *BB) const {
142 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
143 return I != IDoms.end() ? I->second : 0;
146 // getAnalysisUsage - This obviously provides a dominator tree, but it
147 // can only do so with the input of dominator sets
149 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
150 AU.setPreservesAll();
151 if (isPostDominator()) {
152 AU.addRequired(DominatorSet::PostDomID);
153 AU.addProvided(PostDomID);
155 AU.addRequired(DominatorSet::ID);
162 //===----------------------------------------------------------------------===//
164 // DominatorTree - Calculate the immediate dominator tree for a function.
166 class DominatorTree : public DominatorBase {
171 std::map<BasicBlock*, Node*> Nodes;
172 void calculate(const DominatorSet &DS);
174 typedef std::map<BasicBlock*, Node*> NodeMapType;
176 class Node2 : public std::vector<Node*> {
177 friend class DominatorTree;
181 inline BasicBlock *getNode() const { return TheNode; }
182 inline Node2 *getIDom() const { return IDom; }
183 inline const std::vector<Node*> &getChildren() const { return *this; }
185 // dominates - Returns true iff this dominates N. Note that this is not a
186 // constant time operation!
187 inline bool dominates(const Node2 *N) const {
189 while ((IDom = N->getIDom()) != 0 && IDom != this)
190 N = IDom; // Walk up the tree
195 inline Node2(BasicBlock *node, Node *iDom)
196 : TheNode(node), IDom(iDom) {}
197 inline Node2 *addChild(Node *C) { push_back(C); return C; }
201 // DominatorTree ctor - Compute a dominator tree, given various amounts of
202 // previous knowledge...
203 static AnalysisID ID; // Build dominator tree
204 static AnalysisID PostDomID; // Build postdominator tree
206 DominatorTree(AnalysisID id) : DominatorBase(id == PostDomID) {}
207 ~DominatorTree() { reset(); }
209 virtual bool runOnFunction(Function *F) {
212 if (isPostDominator())
213 DS = &getAnalysis<DominatorSet>(DominatorSet::PostDomID);
215 DS = &getAnalysis<DominatorSet>();
216 Root = DS->getRoot();
217 calculate(*DS); // Can be used to make rev-idoms
221 inline Node *operator[](BasicBlock *BB) const {
222 NodeMapType::const_iterator i = Nodes.find(BB);
223 return (i != Nodes.end()) ? i->second : 0;
226 // getAnalysisUsage - This obviously provides a dominator tree, but it
227 // uses dominator sets
229 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
230 AU.setPreservesAll();
231 if (isPostDominator()) {
232 AU.addRequired(DominatorSet::PostDomID);
233 AU.addProvided(PostDomID);
235 AU.addRequired(DominatorSet::ID);
242 //===----------------------------------------------------------------------===//
244 // DominanceFrontier - Calculate the dominance frontiers for a function.
246 class DominanceFrontier : public DominatorBase {
248 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
249 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
251 DomSetMapType Frontiers;
252 const DomSetType &calcDomFrontier(const DominatorTree &DT,
253 const DominatorTree::Node *Node);
254 const DomSetType &calcPostDomFrontier(const DominatorTree &DT,
255 const DominatorTree::Node *Node);
258 // DominatorFrontier ctor - Compute dominator frontiers for a function
260 static AnalysisID ID; // Build dominator frontier
261 static AnalysisID PostDomID; // Build postdominator frontier
263 DominanceFrontier(AnalysisID id) : DominatorBase(id == PostDomID) {}
265 virtual bool runOnFunction(Function *) {
268 if (isPostDominator())
269 DT = &getAnalysis<DominatorTree>(DominatorTree::PostDomID);
271 DT = &getAnalysis<DominatorTree>();
272 Root = DT->getRoot();
274 if (isPostDominator())
275 calcPostDomFrontier(*DT, (*DT)[Root]);
277 calcDomFrontier(*DT, (*DT)[Root]);
281 // Accessor interface:
282 typedef DomSetMapType::const_iterator const_iterator;
283 inline const_iterator begin() const { return Frontiers.begin(); }
284 inline const_iterator end() const { return Frontiers.end(); }
285 inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
287 // getAnalysisUsage - This obviously provides the dominance frontier, but it
288 // uses dominator sets
290 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
291 AU.setPreservesAll();
292 if (isPostDominator()) {
293 AU.addRequired(DominatorTree::PostDomID);
294 AU.addProvided(PostDomID);
296 AU.addRequired(DominatorTree::ID);