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"
26 template <typename GraphType> struct GraphTraits;
28 //===----------------------------------------------------------------------===//
30 // DominatorBase - Base class that other, more interesting dominator analyses
33 class DominatorBase : public FunctionPass {
36 const bool IsPostDominators;
38 inline DominatorBase(bool isPostDom) : Root(0), IsPostDominators(isPostDom) {}
40 inline BasicBlock *getRoot() const { return Root; }
42 // Returns true if analysis based of postdoms
43 bool isPostDominator() const { return IsPostDominators; }
46 //===----------------------------------------------------------------------===//
48 // DominatorSet - Maintain a set<BasicBlock*> for every basic block in a
49 // function, that represents the blocks that dominate the block.
51 class DominatorSetBase : public DominatorBase {
53 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
55 typedef std::map<BasicBlock*, DomSetType> DomSetMapType;
59 DominatorSetBase(bool isPostDom) : DominatorBase(isPostDom) {}
61 virtual void releaseMemory() { Doms.clear(); }
63 // Accessor interface:
64 typedef DomSetMapType::const_iterator const_iterator;
65 typedef DomSetMapType::iterator iterator;
66 inline const_iterator begin() const { return Doms.begin(); }
67 inline iterator begin() { return Doms.begin(); }
68 inline const_iterator end() const { return Doms.end(); }
69 inline iterator end() { return Doms.end(); }
70 inline const_iterator find(BasicBlock* B) const { return Doms.find(B); }
71 inline iterator find(BasicBlock* B) { return Doms.find(B); }
74 /// getDominators - Return the set of basic blocks that dominate the specified
77 inline const DomSetType &getDominators(BasicBlock *BB) const {
78 const_iterator I = find(BB);
79 assert(I != end() && "BB not in function!");
83 /// dominates - Return true if A dominates B.
85 inline bool dominates(BasicBlock *A, BasicBlock *B) const {
86 return getDominators(B).count(A) != 0;
89 /// properlyDominates - Return true if A dominates B and A != B.
91 bool properlyDominates(BasicBlock *A, BasicBlock *B) const {
92 return dominates(A, B) && A != B;
95 /// print - Convert to human readable form
96 virtual void print(std::ostream &OS) const;
98 /// dominates - Return true if A dominates B. This performs the special
99 /// checks neccesary if A and B are in the same basic block.
101 bool dominates(Instruction *A, Instruction *B) const;
103 //===--------------------------------------------------------------------===//
104 // API to update (Post)DominatorSet information based on modifications to
107 /// addBasicBlock - Call to update the dominator set with information about a
108 /// new block that was inserted into the function.
109 void addBasicBlock(BasicBlock *BB, const DomSetType &Dominators) {
110 assert(find(BB) == end() && "Block already in DominatorSet!");
111 Doms.insert(std::make_pair(BB, Dominators));
114 // addDominator - If a new block is inserted into the CFG, then method may be
115 // called to notify the blocks it dominates that it is in their set.
117 void addDominator(BasicBlock *BB, BasicBlock *NewDominator) {
118 iterator I = find(BB);
119 assert(I != end() && "BB is not in DominatorSet!");
120 I->second.insert(NewDominator);
125 //===-------------------------------------
126 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
127 // compute a normal dominator set.
129 struct DominatorSet : public DominatorSetBase {
130 DominatorSet() : DominatorSetBase(false) {}
132 virtual bool runOnFunction(Function &F);
134 /// recalculate - This method may be called by external passes that modify the
135 /// CFG and then need dominator information recalculated. This method is
136 /// obviously really slow, so it should be avoided if at all possible.
139 // getAnalysisUsage - This simply provides a dominator set
140 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
141 AU.setPreservesAll();
144 void calculateDominatorsFromBlock(BasicBlock *BB);
148 //===----------------------------------------------------------------------===//
150 // ImmediateDominators - Calculate the immediate dominator for each node in a
153 class ImmediateDominatorsBase : public DominatorBase {
155 std::map<BasicBlock*, BasicBlock*> IDoms;
156 void calcIDoms(const DominatorSetBase &DS);
158 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
160 virtual void releaseMemory() { IDoms.clear(); }
162 // Accessor interface:
163 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
164 typedef IDomMapType::const_iterator const_iterator;
165 inline const_iterator begin() const { return IDoms.begin(); }
166 inline const_iterator end() const { return IDoms.end(); }
167 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
169 // operator[] - Return the idom for the specified basic block. The start
170 // node returns null, because it does not have an immediate dominator.
172 inline BasicBlock *operator[](BasicBlock *BB) const {
176 // get() - Synonym for operator[].
177 inline BasicBlock *get(BasicBlock *BB) const {
178 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
179 return I != IDoms.end() ? I->second : 0;
182 //===--------------------------------------------------------------------===//
183 // API to update Immediate(Post)Dominators information based on modifications
186 /// addNewBlock - Add a new block to the CFG, with the specified immediate
189 void addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
190 assert(get(BB) == 0 && "BasicBlock already in idom info!");
194 /// setImmediateDominator - Update the immediate dominator information to
195 /// change the current immediate dominator for the specified block to another
196 /// block. This method requires that BB already have an IDom, otherwise just
198 void setImmediateDominator(BasicBlock *BB, BasicBlock *NewIDom) {
199 assert(IDoms.find(BB) != IDoms.end() && "BB doesn't have idom yet!");
203 // print - Convert to human readable form
204 virtual void print(std::ostream &OS) const;
207 //===-------------------------------------
208 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
209 // is used to compute a normal immediate dominator set.
211 struct ImmediateDominators : public ImmediateDominatorsBase {
212 ImmediateDominators() : ImmediateDominatorsBase(false) {}
214 virtual bool runOnFunction(Function &F) {
215 IDoms.clear(); // Reset from the last time we were run...
216 DominatorSet &DS = getAnalysis<DominatorSet>();
222 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
223 AU.setPreservesAll();
224 AU.addRequired<DominatorSet>();
229 //===----------------------------------------------------------------------===//
231 // DominatorTree - Calculate the immediate dominator tree for a function.
233 class DominatorTreeBase : public DominatorBase {
239 std::map<BasicBlock*, Node*> Nodes;
241 typedef std::map<BasicBlock*, Node*> NodeMapType;
244 friend class DominatorTree;
245 friend class PostDominatorTree;
246 friend class DominatorTreeBase;
249 std::vector<Node*> Children;
251 typedef std::vector<Node*>::iterator iterator;
252 typedef std::vector<Node*>::const_iterator const_iterator;
254 iterator begin() { return Children.begin(); }
255 iterator end() { return Children.end(); }
256 const_iterator begin() const { return Children.begin(); }
257 const_iterator end() const { return Children.end(); }
259 inline BasicBlock *getNode() const { return TheNode; }
260 inline Node2 *getIDom() const { return IDom; }
261 inline const std::vector<Node*> &getChildren() const { return Children; }
263 // dominates - Returns true iff this dominates N. Note that this is not a
264 // constant time operation!
265 inline bool dominates(const Node2 *N) const {
267 while ((IDom = N->getIDom()) != 0 && IDom != this)
268 N = IDom; // Walk up the tree
273 inline Node2(BasicBlock *node, Node *iDom)
274 : TheNode(node), IDom(iDom) {}
275 inline Node2 *addChild(Node *C) { Children.push_back(C); return C; }
277 void setIDom(Node2 *NewIDom);
281 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
282 ~DominatorTreeBase() { reset(); }
284 virtual void releaseMemory() { reset(); }
286 /// getNode - return the (Post)DominatorTree node for the specified basic
287 /// block. This is the same as using operator[] on this class.
289 inline Node *getNode(BasicBlock *BB) const {
290 NodeMapType::const_iterator i = Nodes.find(BB);
291 return (i != Nodes.end()) ? i->second : 0;
294 inline Node *operator[](BasicBlock *BB) const {
298 //===--------------------------------------------------------------------===// // API to update (Post)DominatorTree information based on modifications to
301 /// createNewNode - Add a new node to the dominator tree information. This
302 /// creates a new node as a child of IDomNode, linking it into the children
303 /// list of the immediate dominator.
305 Node *createNewNode(BasicBlock *BB, Node *IDomNode) {
306 assert(getNode(BB) == 0 && "Block already in dominator tree!");
307 assert(IDomNode && "Not immediate dominator specified for block!");
308 return Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
311 /// changeImmediateDominator - This method is used to update the dominator
312 /// tree information when a node's immediate dominator changes.
314 void changeImmediateDominator(Node *Node, Node *NewIDom) {
315 assert(Node && NewIDom && "Cannot change null node pointers!");
316 Node->setIDom(NewIDom);
319 /// print - Convert to human readable form
320 virtual void print(std::ostream &OS) const;
324 //===-------------------------------------
325 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
326 // compute a normal dominator tree.
328 struct DominatorTree : public DominatorTreeBase {
329 DominatorTree() : DominatorTreeBase(false) {}
331 virtual bool runOnFunction(Function &F) {
332 reset(); // Reset from the last time we were run...
333 DominatorSet &DS = getAnalysis<DominatorSet>();
339 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
340 AU.setPreservesAll();
341 AU.addRequired<DominatorSet>();
344 void calculate(const DominatorSet &DS);
347 //===-------------------------------------
348 // DominatorTree GraphTraits specialization so the DominatorTree can be
349 // iterable by generic graph iterators.
351 template <> struct GraphTraits<DominatorTree::Node*> {
352 typedef DominatorTree::Node NodeType;
353 typedef NodeType::iterator ChildIteratorType;
355 static NodeType *getEntryNode(NodeType *N) {
358 static inline ChildIteratorType child_begin(NodeType* N) {
361 static inline ChildIteratorType child_end(NodeType* N) {
366 template <> struct GraphTraits<DominatorTree*>
367 : public GraphTraits<DominatorTree::Node*> {
368 static NodeType *getEntryNode(DominatorTree *DT) {
369 return DT->getNode(DT->getRoot());
373 //===----------------------------------------------------------------------===//
375 // DominanceFrontier - Calculate the dominance frontiers for a function.
377 class DominanceFrontierBase : public DominatorBase {
379 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
380 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
382 DomSetMapType Frontiers;
384 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
386 virtual void releaseMemory() { Frontiers.clear(); }
388 // Accessor interface:
389 typedef DomSetMapType::iterator iterator;
390 typedef DomSetMapType::const_iterator const_iterator;
391 iterator begin() { return Frontiers.begin(); }
392 const_iterator begin() const { return Frontiers.begin(); }
393 iterator end() { return Frontiers.end(); }
394 const_iterator end() const { return Frontiers.end(); }
395 iterator find(BasicBlock *B) { return Frontiers.find(B); }
396 const_iterator find(BasicBlock *B) const { return Frontiers.find(B); }
398 void addBasicBlock(BasicBlock *BB, const DomSetType &frontier) {
399 assert(find(BB) == end() && "Block already in DominanceFrontier!");
400 Frontiers.insert(std::make_pair(BB, frontier));
403 void addToFrontier(iterator I, BasicBlock *Node) {
404 assert(I != end() && "BB is not in DominanceFrontier!");
405 I->second.insert(Node);
408 void removeFromFrontier(iterator I, BasicBlock *Node) {
409 assert(I != end() && "BB is not in DominanceFrontier!");
410 assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
411 I->second.erase(Node);
414 // print - Convert to human readable form
415 virtual void print(std::ostream &OS) const;
419 //===-------------------------------------
420 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
421 // compute a normal dominator tree.
423 struct DominanceFrontier : public DominanceFrontierBase {
424 DominanceFrontier() : DominanceFrontierBase(false) {}
426 virtual bool runOnFunction(Function &) {
428 DominatorTree &DT = getAnalysis<DominatorTree>();
430 calculate(DT, DT[Root]);
434 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
435 AU.setPreservesAll();
436 AU.addRequired<DominatorTree>();
439 const DomSetType &calculate(const DominatorTree &DT,
440 const DominatorTree::Node *Node);