1 //===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DominatorTree class, which provides fast and efficient
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_DOMINATORS_H
16 #define LLVM_IR_DOMINATORS_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/GraphTraits.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/GenericDomTree.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/raw_ostream.h"
34 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
35 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
37 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
39 class BasicBlockEdge {
40 const BasicBlock *Start;
41 const BasicBlock *End;
43 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
44 Start(Start_), End(End_) { }
45 const BasicBlock *getStart() const {
48 const BasicBlock *getEnd() const {
51 bool isSingleEdge() const;
54 //===-------------------------------------
55 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
56 /// compute a normal dominator tree.
58 class DominatorTree : public FunctionPass {
60 static char ID; // Pass ID, replacement for typeid
61 DominatorTreeBase<BasicBlock>* DT;
63 DominatorTree() : FunctionPass(ID) {
64 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
65 DT = new DominatorTreeBase<BasicBlock>(false);
72 DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
74 /// getRoots - Return the root blocks of the current CFG. This may include
75 /// multiple blocks if we are computing post dominators. For forward
76 /// dominators, this will always be a single block (the entry node).
78 inline const std::vector<BasicBlock*> &getRoots() const {
79 return DT->getRoots();
82 inline BasicBlock *getRoot() const {
86 inline DomTreeNode *getRootNode() const {
87 return DT->getRootNode();
90 /// Get all nodes dominated by R, including R itself.
91 void getDescendants(BasicBlock *R,
92 SmallVectorImpl<BasicBlock *> &Result) const {
93 DT->getDescendants(R, Result);
96 /// compare - Return false if the other dominator tree matches this
97 /// dominator tree. Otherwise return true.
98 inline bool compare(DominatorTree &Other) const {
99 DomTreeNode *R = getRootNode();
100 DomTreeNode *OtherR = Other.getRootNode();
102 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
105 if (DT->compare(Other.getBase()))
111 virtual bool runOnFunction(Function &F);
113 virtual void verifyAnalysis() const;
115 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
116 AU.setPreservesAll();
119 inline bool dominates(const DomTreeNode* A, const DomTreeNode* B) const {
120 return DT->dominates(A, B);
123 inline bool dominates(const BasicBlock* A, const BasicBlock* B) const {
124 return DT->dominates(A, B);
127 // dominates - Return true if Def dominates a use in User. This performs
128 // the special checks necessary if Def and User are in the same basic block.
129 // Note that Def doesn't dominate a use in Def itself!
130 bool dominates(const Instruction *Def, const Use &U) const;
131 bool dominates(const Instruction *Def, const Instruction *User) const;
132 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
133 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
134 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
136 bool properlyDominates(const DomTreeNode *A, const DomTreeNode *B) const {
137 return DT->properlyDominates(A, B);
140 bool properlyDominates(const BasicBlock *A, const BasicBlock *B) const {
141 return DT->properlyDominates(A, B);
144 /// findNearestCommonDominator - Find nearest common dominator basic block
145 /// for basic block A and B. If there is no such block then return NULL.
146 inline BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B) {
147 return DT->findNearestCommonDominator(A, B);
150 inline const BasicBlock *findNearestCommonDominator(const BasicBlock *A,
151 const BasicBlock *B) {
152 return DT->findNearestCommonDominator(A, B);
155 inline DomTreeNode *operator[](BasicBlock *BB) const {
156 return DT->getNode(BB);
159 /// getNode - return the (Post)DominatorTree node for the specified basic
160 /// block. This is the same as using operator[] on this class.
162 inline DomTreeNode *getNode(BasicBlock *BB) const {
163 return DT->getNode(BB);
166 /// addNewBlock - Add a new node to the dominator tree information. This
167 /// creates a new node as a child of DomBB dominator node,linking it into
168 /// the children list of the immediate dominator.
169 inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
170 return DT->addNewBlock(BB, DomBB);
173 /// changeImmediateDominator - This method is used to update the dominator
174 /// tree information when a node's immediate dominator changes.
176 inline void changeImmediateDominator(BasicBlock *N, BasicBlock* NewIDom) {
177 DT->changeImmediateDominator(N, NewIDom);
180 inline void changeImmediateDominator(DomTreeNode *N, DomTreeNode* NewIDom) {
181 DT->changeImmediateDominator(N, NewIDom);
184 /// eraseNode - Removes a node from the dominator tree. Block must not
185 /// dominate any other blocks. Removes node from its immediate dominator's
186 /// children list. Deletes dominator node associated with basic block BB.
187 inline void eraseNode(BasicBlock *BB) {
191 /// splitBlock - BB is split and now it has one successor. Update dominator
192 /// tree to reflect this change.
193 inline void splitBlock(BasicBlock* NewBB) {
194 DT->splitBlock(NewBB);
197 bool isReachableFromEntry(const BasicBlock* A) const {
198 return DT->isReachableFromEntry(A);
201 bool isReachableFromEntry(const Use &U) const;
204 virtual void releaseMemory() {
208 virtual void print(raw_ostream &OS, const Module* M= 0) const;
211 //===-------------------------------------
212 /// DominatorTree GraphTraits specialization so the DominatorTree can be
213 /// iterable by generic graph iterators.
215 template <> struct GraphTraits<DomTreeNode*> {
216 typedef DomTreeNode NodeType;
217 typedef NodeType::iterator ChildIteratorType;
219 static NodeType *getEntryNode(NodeType *N) {
222 static inline ChildIteratorType child_begin(NodeType *N) {
225 static inline ChildIteratorType child_end(NodeType *N) {
229 typedef df_iterator<DomTreeNode*> nodes_iterator;
231 static nodes_iterator nodes_begin(DomTreeNode *N) {
232 return df_begin(getEntryNode(N));
235 static nodes_iterator nodes_end(DomTreeNode *N) {
236 return df_end(getEntryNode(N));
240 template <> struct GraphTraits<DominatorTree*>
241 : public GraphTraits<DomTreeNode*> {
242 static NodeType *getEntryNode(DominatorTree *DT) {
243 return DT->getRootNode();
246 static nodes_iterator nodes_begin(DominatorTree *N) {
247 return df_begin(getEntryNode(N));
250 static nodes_iterator nodes_end(DominatorTree *N) {
251 return df_end(getEntryNode(N));
256 } // End llvm namespace