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/CFG.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/GenericDomTree.h"
29 #include "llvm/Support/raw_ostream.h"
34 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
35 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
38 EXTERN_TEMPLATE_INSTANTIATION(void Calculate<Function LLVM_COMMA BasicBlock *>(
39 DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT LLVM_COMMA
41 EXTERN_TEMPLATE_INSTANTIATION(
42 void Calculate<Function LLVM_COMMA Inverse<BasicBlock *> >(
43 DominatorTreeBase<GraphTraits<Inverse<BasicBlock *> >::NodeType> &DT
44 LLVM_COMMA Function &F));
47 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
49 class BasicBlockEdge {
50 const BasicBlock *Start;
51 const BasicBlock *End;
53 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
54 Start(Start_), End(End_) { }
55 const BasicBlock *getStart() const {
58 const BasicBlock *getEnd() const {
61 bool isSingleEdge() const;
64 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
65 /// normal dominator tree.
66 class DominatorTree : public DominatorTreeBase<BasicBlock> {
68 typedef DominatorTreeBase<BasicBlock> Base;
70 DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
72 // FIXME: This is no longer needed and should be removed when its uses are
74 Base& getBase() { return *this; }
76 /// \brief Returns *false* if the other dominator tree matches this dominator
78 inline bool compare(const DominatorTree &Other) const {
79 const DomTreeNode *R = getRootNode();
80 const DomTreeNode *OtherR = Other.getRootNode();
82 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
85 if (Base::compare(Other))
91 // Ensure base-class overloads are visible.
92 using Base::dominates;
94 /// \brief Return true if Def dominates a use in User.
96 /// This performs the special checks necessary if Def and User are in the same
97 /// basic block. Note that Def doesn't dominate a use in Def itself!
98 bool dominates(const Instruction *Def, const Use &U) const;
99 bool dominates(const Instruction *Def, const Instruction *User) const;
100 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
101 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
102 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
104 inline DomTreeNode *operator[](BasicBlock *BB) const {
108 // Ensure base class overloads are visible.
109 using Base::isReachableFromEntry;
111 /// \brief Provide an overload for a Use.
112 bool isReachableFromEntry(const Use &U) const;
114 /// \brief Verify the correctness of the domtree by re-computing it.
116 /// This should only be used for debugging as it aborts the program if the
117 /// verification fails.
118 void verifyDomTree() const;
121 //===-------------------------------------
122 // DominatorTree GraphTraits specializations so the DominatorTree can be
123 // iterable by generic graph iterators.
125 template <> struct GraphTraits<DomTreeNode*> {
126 typedef DomTreeNode NodeType;
127 typedef NodeType::iterator ChildIteratorType;
129 static NodeType *getEntryNode(NodeType *N) {
132 static inline ChildIteratorType child_begin(NodeType *N) {
135 static inline ChildIteratorType child_end(NodeType *N) {
139 typedef df_iterator<DomTreeNode*> nodes_iterator;
141 static nodes_iterator nodes_begin(DomTreeNode *N) {
142 return df_begin(getEntryNode(N));
145 static nodes_iterator nodes_end(DomTreeNode *N) {
146 return df_end(getEntryNode(N));
150 template <> struct GraphTraits<DominatorTree*>
151 : public GraphTraits<DomTreeNode*> {
152 static NodeType *getEntryNode(DominatorTree *DT) {
153 return DT->getRootNode();
156 static nodes_iterator nodes_begin(DominatorTree *N) {
157 return df_begin(getEntryNode(N));
160 static nodes_iterator nodes_end(DominatorTree *N) {
161 return df_end(getEntryNode(N));
165 /// \brief Analysis pass which computes a \c DominatorTree.
166 class DominatorTreeWrapperPass : public FunctionPass {
172 DominatorTreeWrapperPass() : FunctionPass(ID) {
173 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
176 DominatorTree &getDomTree() { return DT; }
177 const DominatorTree &getDomTree() const { return DT; }
179 virtual bool runOnFunction(Function &F);
181 virtual void verifyAnalysis() const;
183 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
184 AU.setPreservesAll();
187 virtual void releaseMemory() { DT.releaseMemory(); }
189 virtual void print(raw_ostream &OS, const Module *M = 0) const;
192 } // End llvm namespace