LLVM  3.7.0
Dominators.h
Go to the documentation of this file.
1 //===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the DominatorTree class, which provides fast and efficient
11 // dominance queries.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_DOMINATORS_H
16 #define LLVM_IR_DOMINATORS_H
17 
18 #include "llvm/ADT/DenseMap.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"
30 #include <algorithm>
31 
32 namespace llvm {
33 
34 // FIXME: Replace this brittle forward declaration with the include of the new
35 // PassManager.h when doing so doesn't break the PassManagerBuilder.
36 template <typename IRUnitT> class AnalysisManager;
37 class PreservedAnalyses;
38 
39 extern template class DomTreeNodeBase<BasicBlock>;
40 extern template class DominatorTreeBase<BasicBlock>;
41 
42 extern template void Calculate<Function, BasicBlock *>(
44 extern template void Calculate<Function, Inverse<BasicBlock *>>(
45  DominatorTreeBase<GraphTraits<Inverse<BasicBlock *>>::NodeType> &DT,
46  Function &F);
47 
48 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
49 
51  const BasicBlock *Start;
52  const BasicBlock *End;
53 public:
54  BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
55  Start(Start_), End(End_) { }
56  const BasicBlock *getStart() const {
57  return Start;
58  }
59  const BasicBlock *getEnd() const {
60  return End;
61  }
62  bool isSingleEdge() const;
63 };
64 
65 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
66 /// normal dominator tree.
67 class DominatorTree : public DominatorTreeBase<BasicBlock> {
68 public:
70 
72 
74  : Base(std::move(static_cast<Base &>(Arg))) {}
76  Base::operator=(std::move(static_cast<Base &>(RHS)));
77  return *this;
78  }
79 
80  /// \brief Returns *false* if the other dominator tree matches this dominator
81  /// tree.
82  inline bool compare(const DominatorTree &Other) const {
83  const DomTreeNode *R = getRootNode();
84  const DomTreeNode *OtherR = Other.getRootNode();
85 
86  if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
87  return true;
88 
89  if (Base::compare(Other))
90  return true;
91 
92  return false;
93  }
94 
95  // Ensure base-class overloads are visible.
96  using Base::dominates;
97 
98  /// \brief Return true if Def dominates a use in User.
99  ///
100  /// This performs the special checks necessary if Def and User are in the same
101  /// basic block. Note that Def doesn't dominate a use in Def itself!
102  bool dominates(const Instruction *Def, const Use &U) const;
103  bool dominates(const Instruction *Def, const Instruction *User) const;
104  bool dominates(const Instruction *Def, const BasicBlock *BB) const;
105  bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
106  bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
107 
108  // Ensure base class overloads are visible.
110 
111  /// \brief Provide an overload for a Use.
112  bool isReachableFromEntry(const Use &U) const;
113 
114  /// \brief Verify the correctness of the domtree by re-computing it.
115  ///
116  /// This should only be used for debugging as it aborts the program if the
117  /// verification fails.
118  void verifyDomTree() const;
119 };
120 
121 //===-------------------------------------
122 // DominatorTree GraphTraits specializations so the DominatorTree can be
123 // iterable by generic graph iterators.
124 
125 template <> struct GraphTraits<DomTreeNode*> {
128 
129  static NodeType *getEntryNode(NodeType *N) {
130  return N;
131  }
132  static inline ChildIteratorType child_begin(NodeType *N) {
133  return N->begin();
134  }
135  static inline ChildIteratorType child_end(NodeType *N) {
136  return N->end();
137  }
138 
140 
141  static nodes_iterator nodes_begin(DomTreeNode *N) {
142  return df_begin(getEntryNode(N));
143  }
144 
145  static nodes_iterator nodes_end(DomTreeNode *N) {
146  return df_end(getEntryNode(N));
147  }
148 };
149 
150 template <> struct GraphTraits<DominatorTree*>
151  : public GraphTraits<DomTreeNode*> {
153  return DT->getRootNode();
154  }
155 
157  return df_begin(getEntryNode(N));
158  }
159 
161  return df_end(getEntryNode(N));
162  }
163 };
164 
165 /// \brief Analysis pass which computes a \c DominatorTree.
167 public:
168  /// \brief Provide the result typedef for this analysis pass.
170 
171  /// \brief Opaque, unique identifier for this analysis pass.
172  static void *ID() { return (void *)&PassID; }
173 
174  /// \brief Run the analysis pass over a function and produce a dominator tree.
176 
177  /// \brief Provide access to a name for this pass for debugging purposes.
178  static StringRef name() { return "DominatorTreeAnalysis"; }
179 
180 private:
181  static char PassID;
182 };
183 
184 /// \brief Printer pass for the \c DominatorTree.
186  raw_ostream &OS;
187 
188 public:
191 
192  static StringRef name() { return "DominatorTreePrinterPass"; }
193 };
194 
195 /// \brief Verifier pass for the \c DominatorTree.
198 
199  static StringRef name() { return "DominatorTreeVerifierPass"; }
200 };
201 
202 /// \brief Legacy analysis pass which computes a \c DominatorTree.
204  DominatorTree DT;
205 
206 public:
207  static char ID;
208 
211  }
212 
213  DominatorTree &getDomTree() { return DT; }
214  const DominatorTree &getDomTree() const { return DT; }
215 
216  bool runOnFunction(Function &F) override;
217 
218  void verifyAnalysis() const override;
219 
220  void getAnalysisUsage(AnalysisUsage &AU) const override {
221  AU.setPreservesAll();
222  }
223 
224  void releaseMemory() override { DT.releaseMemory(); }
225 
226  void print(raw_ostream &OS, const Module *M = nullptr) const override;
227 };
228 
229 } // End llvm namespace
230 
231 #endif
bool compare(const DominatorTree &Other) const
Returns false if the other dominator tree matches this dominator tree.
Definition: Dominators.h:82
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: GraphTraits.h:27
const BasicBlock * getStart() const
Definition: Dominators.h:56
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Dominators.h:220
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:166
F(f)
const DominatorTree & getDomTree() const
Definition: Dominators.h:214
NodeType::iterator ChildIteratorType
Definition: Dominators.h:127
template void Calculate< Function, BasicBlock * >(DominatorTreeBase< GraphTraits< BasicBlock * >::NodeType > &DT, Function &F)
static StringRef name()
Provide access to a name for this pass for debugging purposes.
Definition: Dominators.h:178
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:39
static nodes_iterator nodes_begin(DominatorTree *N)
Definition: Dominators.h:156
void initializeDominatorTreeWrapperPassPass(PassRegistry &)
DominatorTree Result
Provide the result typedef for this analysis pass.
Definition: Dominators.h:169
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Dominators.cpp:346
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
DominatorTree(DominatorTree &&Arg)
Definition: Dominators.h:73
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
static ChildIteratorType child_begin(NodeType *N)
Definition: Dominators.h:132
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
DominatorTree & getDomTree()
Definition: Dominators.h:213
#define false
Definition: ConvertUTF.c:65
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:591
DominatorTree run(Function &F)
Run the analysis pass over a function and produce a dominator tree.
Definition: Dominators.cpp:303
static nodes_iterator nodes_end(DominatorTree *N)
Definition: Dominators.h:160
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:265
Base class for the actual dominator tree node.
DominatorTreePrinterPass(raw_ostream &OS)
Definition: Dominators.cpp:311
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: Dominators.h:224
DominatorTree & operator=(DominatorTree &&RHS)
Definition: Dominators.h:75
Core dominator tree base class.
Definition: LoopInfo.h:56
PreservedAnalyses run(Function &F, AnalysisManager< Function > *AM)
Definition: Dominators.cpp:313
An abstract set of preserved analyses following a transformation pass run.
Definition: PassManager.h:69
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
static NodeType * getEntryNode(DominatorTree *DT)
Definition: Dominators.h:152
df_iterator< T > df_end(const T &G)
Represent the analysis usage information of a pass.
static ChildIteratorType child_end(NodeType *N)
Definition: Dominators.h:135
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
static NodeType * getEntryNode(NodeType *N)
Definition: Dominators.h:129
Printer pass for the DominatorTree.
Definition: Dominators.h:185
void verifyDomTree() const
Verify the correctness of the domtree by re-computing it.
Definition: Dominators.cpp:280
static nodes_iterator nodes_end(DomTreeNode *N)
Definition: Dominators.h:145
Verifier pass for the DominatorTree.
Definition: Dominators.h:196
const BasicBlock * getEnd() const
Definition: Dominators.h:59
static StringRef name()
Definition: Dominators.h:192
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:214
static void * ID()
Opaque, unique identifier for this analysis pass.
Definition: Dominators.h:172
df_iterator< DomTreeNode * > nodes_iterator
Definition: Dominators.h:139
DominatorTreeBase< BasicBlock > Base
Definition: Dominators.h:69
df_iterator< T > df_begin(const T &G)
void setPreservesAll()
Set by analyses that do not transform their input at all.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: Dominators.cpp:351
NodeT * getBlock() const
#define N
PreservedAnalyses run(Function &F, AnalysisManager< Function > *AM)
Definition: Dominators.cpp:321
DomTreeNodeBase< BasicBlock > DomTreeNode
bool isSingleEdge() const
Definition: Dominators.cpp:42
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition: Dominators.h:54
bool compare(const DominatorTreeBase &Other) const
compare - Return false if the other dominator tree base matches this dominator tree base...
static nodes_iterator nodes_begin(DomTreeNode *N)
Definition: Dominators.h:141
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
This file defines a set of templates that efficiently compute a dominator tree over a generic graph...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
A generic analysis pass manager with lazy running and caching of results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203