LLVM  4.0.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/DenseMapInfo.h"
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/IR/CFG.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
25 
26 namespace llvm {
27 
28 class Function;
29 class BasicBlock;
30 class raw_ostream;
31 
32 extern template class DomTreeNodeBase<BasicBlock>;
33 extern template class DominatorTreeBase<BasicBlock>;
34 
35 extern template void Calculate<Function, BasicBlock *>(
36  DominatorTreeBaseByGraphTraits<GraphTraits<BasicBlock *>> &DT, Function &F);
37 extern template void Calculate<Function, Inverse<BasicBlock *>>(
38  DominatorTreeBaseByGraphTraits<GraphTraits<Inverse<BasicBlock *>>> &DT,
39  Function &F);
40 
42 
44  const BasicBlock *Start;
45  const BasicBlock *End;
46 public:
47  BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
48  Start(Start_), End(End_) { }
49  const BasicBlock *getStart() const {
50  return Start;
51  }
52  const BasicBlock *getEnd() const {
53  return End;
54  }
55  bool isSingleEdge() const;
56 };
57 
58 template <> struct DenseMapInfo<BasicBlockEdge> {
59  static unsigned getHashValue(const BasicBlockEdge *V);
61  static inline BasicBlockEdge getEmptyKey() {
62  return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
63  }
64  static inline BasicBlockEdge getTombstoneKey() {
65  return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
66  }
67 
68  static unsigned getHashValue(const BasicBlockEdge &Edge) {
69  return hash_combine(BBInfo::getHashValue(Edge.getStart()),
70  BBInfo::getHashValue(Edge.getEnd()));
71  }
72  static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
73  return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
74  BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
75  }
76 };
77 
78 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
79 /// normal dominator tree.
80 ///
81 /// Definition: A block is said to be forward statically reachable if there is
82 /// a path from the entry of the function to the block. A statically reachable
83 /// block may become statically unreachable during optimization.
84 ///
85 /// A forward unreachable block may appear in the dominator tree, or it may
86 /// not. If it does, dominance queries will return results as if all reachable
87 /// blocks dominate it. When asking for a Node corresponding to a potentially
88 /// unreachable block, calling code must handle the case where the block was
89 /// unreachable and the result of getNode() is nullptr.
90 ///
91 /// Generally, a block known to be unreachable when the dominator tree is
92 /// constructed will not be in the tree. One which becomes unreachable after
93 /// the dominator tree is initially constructed may still exist in the tree,
94 /// even if the tree is properly updated. Calling code should not rely on the
95 /// preceding statements; this is stated only to assist human understanding.
96 class DominatorTree : public DominatorTreeBase<BasicBlock> {
97 public:
99 
102  recalculate(F);
103  }
104 
105  /// \brief Returns *false* if the other dominator tree matches this dominator
106  /// tree.
107  inline bool compare(const DominatorTree &Other) const {
108  const DomTreeNode *R = getRootNode();
109  const DomTreeNode *OtherR = Other.getRootNode();
110 
111  if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
112  return true;
113 
114  if (Base::compare(Other))
115  return true;
116 
117  return false;
118  }
119 
120  // Ensure base-class overloads are visible.
121  using Base::dominates;
122 
123  /// \brief Return true if Def dominates a use in User.
124  ///
125  /// This performs the special checks necessary if Def and User are in the same
126  /// basic block. Note that Def doesn't dominate a use in Def itself!
127  bool dominates(const Instruction *Def, const Use &U) const;
128  bool dominates(const Instruction *Def, const Instruction *User) const;
129  bool dominates(const Instruction *Def, const BasicBlock *BB) const;
130  bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
131  bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
132 
133  // Ensure base class overloads are visible.
135 
136  /// \brief Provide an overload for a Use.
137  bool isReachableFromEntry(const Use &U) const;
138 
139  /// \brief Verify the correctness of the domtree by re-computing it.
140  ///
141  /// This should only be used for debugging as it aborts the program if the
142  /// verification fails.
143  void verifyDomTree() const;
144 };
145 
146 //===-------------------------------------
147 // DominatorTree GraphTraits specializations so the DominatorTree can be
148 // iterable by generic graph iterators.
149 
150 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
151  typedef Node *NodeRef;
152  typedef ChildIterator ChildIteratorType;
154 
155  static NodeRef getEntryNode(NodeRef N) { return N; }
156  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
157  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
158 
160  return df_begin(getEntryNode(N));
161  }
162 
164 };
165 
166 template <>
169 
170 template <>
172  : public DomTreeGraphTraitsBase<const DomTreeNode,
174 
175 template <> struct GraphTraits<DominatorTree*>
176  : public GraphTraits<DomTreeNode*> {
177  static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
178 
180  return df_begin(getEntryNode(N));
181  }
182 
184  return df_end(getEntryNode(N));
185  }
186 };
187 
188 /// \brief Analysis pass which computes a \c DominatorTree.
189 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
191  static AnalysisKey Key;
192 
193 public:
194  /// \brief Provide the result typedef for this analysis pass.
196 
197  /// \brief Run the analysis pass over a function and produce a dominator tree.
199 };
200 
201 /// \brief Printer pass for the \c DominatorTree.
203  : public PassInfoMixin<DominatorTreePrinterPass> {
204  raw_ostream &OS;
205 
206 public:
209 };
210 
211 /// \brief Verifier pass for the \c DominatorTree.
212 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
214 };
215 
216 /// \brief Legacy analysis pass which computes a \c DominatorTree.
218  DominatorTree DT;
219 
220 public:
221  static char ID;
222 
225  }
226 
227  DominatorTree &getDomTree() { return DT; }
228  const DominatorTree &getDomTree() const { return DT; }
229 
230  bool runOnFunction(Function &F) override;
231 
232  void verifyAnalysis() const override;
233 
234  void getAnalysisUsage(AnalysisUsage &AU) const override {
235  AU.setPreservesAll();
236  }
237 
238  void releaseMemory() override { DT.releaseMemory(); }
239 
240  void print(raw_ostream &OS, const Module *M = nullptr) const override;
241 };
242 
243 } // End llvm namespace
244 
245 #endif
bool compare(const DominatorTree &Other) const
Returns false if the other dominator tree matches this dominator tree.
Definition: Dominators.h:107
DenseMapInfo< const BasicBlock * > BBInfo
Definition: Dominators.h:60
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Various leaf nodes.
Definition: ISDOpcodes.h:60
DominatorTree(Function &F)
Definition: Dominators.h:101
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition: Dominators.h:72
template void Calculate< Function, BasicBlock * >(DominatorTreeBaseByGraphTraits< GraphTraits< BasicBlock * >> &DT, Function &F)
static NodeRef getEntryNode(NodeRef N)
Definition: Dominators.h:155
const BasicBlock * getStart() const
Definition: Dominators.h:49
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Dominators.h:234
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:189
const DominatorTree & getDomTree() const
Definition: Dominators.h:228
static nodes_iterator nodes_begin(NodeRef N)
Definition: Dominators.h:159
static nodes_iterator nodes_begin(DominatorTree *N)
Definition: Dominators.h:179
void initializeDominatorTreeWrapperPassPass(PassRegistry &)
DominatorTree Result
Provide the result typedef for this analysis pass.
Definition: Dominators.h:195
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Dominators.cpp:351
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
static BasicBlockEdge getEmptyKey()
Definition: Dominators.h:61
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
DominatorTree & getDomTree()
Definition: Dominators.h:227
#define F(x, y, z)
Definition: MD5.cpp:51
static bool isEqual(const Function &Caller, const Function &Callee)
static nodes_iterator nodes_end(DominatorTree *N)
Definition: Dominators.h:183
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:311
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:269
Function Alias Analysis false
Base class for the actual dominator tree node.
DominatorTreePrinterPass(raw_ostream &OS)
Definition: Dominators.cpp:316
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
NodeT * getBlock() const
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:238
static nodes_iterator nodes_end(NodeRef N)
Definition: Dominators.h:163
Core dominator tree base class.
Definition: LoopInfo.h:59
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
df_iterator< Node *, df_iterator_default_set< Node * > > nodes_iterator
Definition: Dominators.h:153
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:51
ChildIterator ChildIteratorType
Definition: Dominators.h:152
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
DominatorTree run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
Definition: Dominators.cpp:307
df_iterator< T > df_end(const T &G)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:328
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Printer pass for the DominatorTree.
Definition: Dominators.h:202
void verifyDomTree() const
Verify the correctness of the domtree by re-computing it.
Definition: Dominators.cpp:284
Verifier pass for the DominatorTree.
Definition: Dominators.h:212
const BasicBlock * getEnd() const
Definition: Dominators.h:52
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:218
static ChildIteratorType child_begin(NodeRef N)
Definition: Dominators.h:156
static ChildIteratorType child_end(NodeRef N)
Definition: Dominators.h:157
std::vector< DomTreeNodeBase< NodeT > * >::const_iterator const_iterator
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:41
DominatorTreeBase< BasicBlock > Base
Definition: Dominators.h:98
df_iterator< T > df_begin(const T &G)
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:602
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:356
#define N
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition: Dominators.h:68
static NodeRef getEntryNode(DominatorTree *DT)
Definition: Dominators.h:177
bool isSingleEdge() const
Definition: Dominators.cpp:40
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition: Dominators.h:47
aarch64 promote const
bool compare(const DominatorTreeBase &Other) const
compare - Return false if the other dominator tree base matches this dominator tree base...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Dominators.cpp:318
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
This file defines a set of templates that efficiently compute a dominator tree over a generic graph...
void recalculate(FT &F)
recalculate - compute a dominator tree for the given function
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:217
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Dominators.cpp:326
static BasicBlockEdge getTombstoneKey()
Definition: Dominators.h:64