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 const char *getPassName() const {
67 if (isPostDominator()) return "Post-Dominator Set Construction";
68 else return "Dominator Set Construction";
71 virtual bool runOnFunction(Function *F);
73 // Accessor interface:
74 typedef DomSetMapType::const_iterator const_iterator;
75 typedef DomSetMapType::iterator iterator;
76 inline const_iterator begin() const { return Doms.begin(); }
77 inline iterator begin() { return Doms.begin(); }
78 inline const_iterator end() const { return Doms.end(); }
79 inline iterator end() { return Doms.end(); }
80 inline const_iterator find(BasicBlock* B) const { return Doms.find(B); }
81 inline iterator find(BasicBlock* B) { return Doms.find(B); }
83 // getDominators - Return the set of basic blocks that dominate the specified
86 inline const DomSetType &getDominators(BasicBlock *BB) const {
87 const_iterator I = find(BB);
88 assert(I != end() && "BB not in function!");
92 // dominates - Return true if A dominates B.
94 inline bool dominates(BasicBlock *A, BasicBlock *B) const {
95 return getDominators(B).count(A) != 0;
98 // getAnalysisUsage - This obviously provides a dominator set, but it also
99 // uses the UnifyFunctionExitNode pass if building post-dominators
101 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
105 //===----------------------------------------------------------------------===//
107 // ImmediateDominators - Calculate the immediate dominator for each node in a
110 class ImmediateDominators : public DominatorBase {
111 std::map<BasicBlock*, BasicBlock*> IDoms;
112 void calcIDoms(const DominatorSet &DS);
115 // ImmediateDominators ctor - Calculate the idom or post-idom mapping,
118 static AnalysisID ID; // Build immediate dominators
119 static AnalysisID PostDomID; // Build immediate postdominators
121 ImmediateDominators(AnalysisID id) : DominatorBase(id == PostDomID) {}
123 virtual const char *getPassName() const {
124 if (isPostDominator()) return "Immediate Post-Dominators Construction";
125 else return "Immediate Dominators Construction";
128 virtual bool runOnFunction(Function *F) {
129 IDoms.clear(); // Reset from the last time we were run...
131 if (isPostDominator())
132 DS = &getAnalysis<DominatorSet>(DominatorSet::PostDomID);
134 DS = &getAnalysis<DominatorSet>();
136 Root = DS->getRoot();
137 calcIDoms(*DS); // Can be used to make rev-idoms
141 // Accessor interface:
142 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
143 typedef IDomMapType::const_iterator const_iterator;
144 inline const_iterator begin() const { return IDoms.begin(); }
145 inline const_iterator end() const { return IDoms.end(); }
146 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
148 // operator[] - Return the idom for the specified basic block. The start
149 // node returns null, because it does not have an immediate dominator.
151 inline BasicBlock *operator[](BasicBlock *BB) const {
152 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
153 return I != IDoms.end() ? I->second : 0;
156 // getAnalysisUsage - This obviously provides a dominator tree, but it
157 // can only do so with the input of dominator sets
159 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
160 AU.setPreservesAll();
161 if (isPostDominator()) {
162 AU.addRequired(DominatorSet::PostDomID);
163 AU.addProvided(PostDomID);
165 AU.addRequired(DominatorSet::ID);
172 //===----------------------------------------------------------------------===//
174 // DominatorTree - Calculate the immediate dominator tree for a function.
176 class DominatorTree : public DominatorBase {
181 std::map<BasicBlock*, Node*> Nodes;
182 void calculate(const DominatorSet &DS);
184 typedef std::map<BasicBlock*, Node*> NodeMapType;
186 class Node2 : public std::vector<Node*> {
187 friend class DominatorTree;
191 inline BasicBlock *getNode() const { return TheNode; }
192 inline Node2 *getIDom() const { return IDom; }
193 inline const std::vector<Node*> &getChildren() const { return *this; }
195 // dominates - Returns true iff this dominates N. Note that this is not a
196 // constant time operation!
197 inline bool dominates(const Node2 *N) const {
199 while ((IDom = N->getIDom()) != 0 && IDom != this)
200 N = IDom; // Walk up the tree
205 inline Node2(BasicBlock *node, Node *iDom)
206 : TheNode(node), IDom(iDom) {}
207 inline Node2 *addChild(Node *C) { push_back(C); return C; }
211 // DominatorTree ctor - Compute a dominator tree, given various amounts of
212 // previous knowledge...
213 static AnalysisID ID; // Build dominator tree
214 static AnalysisID PostDomID; // Build postdominator tree
216 DominatorTree(AnalysisID id) : DominatorBase(id == PostDomID) {}
217 ~DominatorTree() { reset(); }
219 virtual const char *getPassName() const {
220 if (isPostDominator()) return "Post-Dominator Tree Construction";
221 else return "Dominator Tree Construction";
224 virtual bool runOnFunction(Function *F) {
227 if (isPostDominator())
228 DS = &getAnalysis<DominatorSet>(DominatorSet::PostDomID);
230 DS = &getAnalysis<DominatorSet>();
231 Root = DS->getRoot();
232 calculate(*DS); // Can be used to make rev-idoms
236 inline Node *operator[](BasicBlock *BB) const {
237 NodeMapType::const_iterator i = Nodes.find(BB);
238 return (i != Nodes.end()) ? i->second : 0;
241 // getAnalysisUsage - This obviously provides a dominator tree, but it
242 // uses dominator sets
244 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
245 AU.setPreservesAll();
246 if (isPostDominator()) {
247 AU.addRequired(DominatorSet::PostDomID);
248 AU.addProvided(PostDomID);
250 AU.addRequired(DominatorSet::ID);
257 //===----------------------------------------------------------------------===//
259 // DominanceFrontier - Calculate the dominance frontiers for a function.
261 class DominanceFrontier : public DominatorBase {
263 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
264 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
266 DomSetMapType Frontiers;
267 const DomSetType &calcDomFrontier(const DominatorTree &DT,
268 const DominatorTree::Node *Node);
269 const DomSetType &calcPostDomFrontier(const DominatorTree &DT,
270 const DominatorTree::Node *Node);
273 // DominatorFrontier ctor - Compute dominator frontiers for a function
275 static AnalysisID ID; // Build dominator frontier
276 static AnalysisID PostDomID; // Build postdominator frontier
278 DominanceFrontier(AnalysisID id) : DominatorBase(id == PostDomID) {}
280 virtual const char *getPassName() const {
281 if (isPostDominator()) return "Post-Dominance Frontier Construction";
282 else return "Dominance Frontier Construction";
285 virtual bool runOnFunction(Function *) {
288 if (isPostDominator())
289 DT = &getAnalysis<DominatorTree>(DominatorTree::PostDomID);
291 DT = &getAnalysis<DominatorTree>();
292 Root = DT->getRoot();
294 if (isPostDominator())
295 calcPostDomFrontier(*DT, (*DT)[Root]);
297 calcDomFrontier(*DT, (*DT)[Root]);
301 // Accessor interface:
302 typedef DomSetMapType::const_iterator const_iterator;
303 inline const_iterator begin() const { return Frontiers.begin(); }
304 inline const_iterator end() const { return Frontiers.end(); }
305 inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
307 // getAnalysisUsage - This obviously provides the dominance frontier, but it
308 // uses dominator sets
310 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
311 AU.setPreservesAll();
312 if (isPostDominator()) {
313 AU.addRequired(DominatorTree::PostDomID);
314 AU.addProvided(PostDomID);
316 AU.addRequired(DominatorTree::ID);