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 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
55 /// normal dominator tree.
56 class DominatorTree : public FunctionPass {
58 static char ID; // Pass ID, replacement for typeid
59 DominatorTreeBase<BasicBlock>* DT;
61 DominatorTree() : FunctionPass(ID) {
62 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
63 DT = new DominatorTreeBase<BasicBlock>(false);
70 DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
72 /// \brief Returns the root blocks of the current CFG.
74 /// This may include multiple blocks if we are computing post dominators.
75 /// For forward dominators, this will always be a single block (the entry
77 inline const std::vector<BasicBlock*> &getRoots() const {
78 return DT->getRoots();
81 inline BasicBlock *getRoot() const {
85 inline DomTreeNode *getRootNode() const {
86 return DT->getRootNode();
89 /// Get all nodes dominated by R, including R itself.
90 void getDescendants(BasicBlock *R,
91 SmallVectorImpl<BasicBlock *> &Result) const {
92 DT->getDescendants(R, Result);
95 /// \brief Returns *false* if the other dominator tree matches this dominator
97 inline bool compare(DominatorTree &Other) const {
98 DomTreeNode *R = getRootNode();
99 DomTreeNode *OtherR = Other.getRootNode();
101 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
104 if (DT->compare(Other.getBase()))
110 virtual bool runOnFunction(Function &F);
112 virtual void verifyAnalysis() const;
114 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
115 AU.setPreservesAll();
118 inline bool dominates(const DomTreeNode* A, const DomTreeNode* B) const {
119 return DT->dominates(A, B);
122 inline bool dominates(const BasicBlock* A, const BasicBlock* B) const {
123 return DT->dominates(A, B);
126 // \brief Return true if Def dominates a use in User.
128 // This performs the special checks necessary if Def and User are in the same
129 // basic block. 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 /// \brief Find nearest common dominator basic block for basic block A and B.
146 /// If there is no such block then return NULL.
147 inline BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B) {
148 return DT->findNearestCommonDominator(A, B);
151 inline const BasicBlock *findNearestCommonDominator(const BasicBlock *A,
152 const BasicBlock *B) {
153 return DT->findNearestCommonDominator(A, B);
156 inline DomTreeNode *operator[](BasicBlock *BB) const {
157 return DT->getNode(BB);
160 /// \brief Returns the DominatorTree node for the specified basic block.
162 /// This is the same as using operator[] on this class.
163 inline DomTreeNode *getNode(BasicBlock *BB) const {
164 return DT->getNode(BB);
167 /// \brief Add a new node to the dominator tree information.
169 /// This creates a new node as a child of DomBB dominator node, linking it
170 /// into the children list of the immediate dominator.
171 inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
172 return DT->addNewBlock(BB, DomBB);
175 /// \brief Updates the dominator tree information when a node's immediate
176 /// dominator changes.
177 inline void changeImmediateDominator(BasicBlock *N, BasicBlock* NewIDom) {
178 DT->changeImmediateDominator(N, NewIDom);
181 inline void changeImmediateDominator(DomTreeNode *N, DomTreeNode* NewIDom) {
182 DT->changeImmediateDominator(N, NewIDom);
185 /// \brief Removes a node from the dominator tree.
187 /// The block must not dominate any other blocks. Removes node from its
188 /// immediate dominator's children list. Deletes dominator node associated
189 /// with basic block BB.
190 inline void eraseNode(BasicBlock *BB) {
194 /// \brief BB is split and now it has one successor; update dominator tree to
195 /// reflect this change.
196 inline void splitBlock(BasicBlock* NewBB) {
197 DT->splitBlock(NewBB);
200 bool isReachableFromEntry(const BasicBlock* A) const {
201 return DT->isReachableFromEntry(A);
204 bool isReachableFromEntry(const Use &U) const;
207 virtual void releaseMemory() {
211 virtual void print(raw_ostream &OS, const Module* M= 0) const;
214 //===-------------------------------------
215 // DominatorTree GraphTraits specializations so the DominatorTree can be
216 // iterable by generic graph iterators.
218 template <> struct GraphTraits<DomTreeNode*> {
219 typedef DomTreeNode NodeType;
220 typedef NodeType::iterator ChildIteratorType;
222 static NodeType *getEntryNode(NodeType *N) {
225 static inline ChildIteratorType child_begin(NodeType *N) {
228 static inline ChildIteratorType child_end(NodeType *N) {
232 typedef df_iterator<DomTreeNode*> nodes_iterator;
234 static nodes_iterator nodes_begin(DomTreeNode *N) {
235 return df_begin(getEntryNode(N));
238 static nodes_iterator nodes_end(DomTreeNode *N) {
239 return df_end(getEntryNode(N));
243 template <> struct GraphTraits<DominatorTree*>
244 : public GraphTraits<DomTreeNode*> {
245 static NodeType *getEntryNode(DominatorTree *DT) {
246 return DT->getRootNode();
249 static nodes_iterator nodes_begin(DominatorTree *N) {
250 return df_begin(getEntryNode(N));
253 static nodes_iterator nodes_end(DominatorTree *N) {
254 return df_end(getEntryNode(N));
258 } // End llvm namespace