LLVM  6.0.0svn
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"
20 #include "llvm/ADT/GraphTraits.h"
21 #include "llvm/ADT/Hashing.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/Pass.h"
27 #include <utility>
28 
29 namespace llvm {
30 
31 class Function;
32 class Instruction;
33 class Module;
34 class raw_ostream;
35 
36 extern template class DomTreeNodeBase<BasicBlock>;
37 extern template class DominatorTreeBase<BasicBlock, false>; // DomTree
38 extern template class DominatorTreeBase<BasicBlock, true>; // PostDomTree
39 
40 namespace DomTreeBuilder {
43 
44 extern template void Calculate<BBDomTree>(BBDomTree &DT);
45 extern template void Calculate<BBPostDomTree>(BBPostDomTree &DT);
46 
47 extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
48  BasicBlock *To);
49 extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT,
50  BasicBlock *From,
51  BasicBlock *To);
52 
53 extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
54  BasicBlock *To);
55 extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT,
56  BasicBlock *From,
57  BasicBlock *To);
58 
59 extern template bool Verify<BBDomTree>(const BBDomTree &DT);
60 extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT);
61 } // namespace DomTreeBuilder
62 
64 
66  const BasicBlock *Start;
67  const BasicBlock *End;
68 
69 public:
70  BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
71  Start(Start_), End(End_) {}
72 
73  BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
74  : Start(Pair.first), End(Pair.second) {}
75 
76  BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
77  : Start(Pair.first), End(Pair.second) {}
78 
79  const BasicBlock *getStart() const {
80  return Start;
81  }
82 
83  const BasicBlock *getEnd() const {
84  return End;
85  }
86 
87  /// Check if this is the only edge between Start and End.
88  bool isSingleEdge() const;
89 };
90 
91 template <> struct DenseMapInfo<BasicBlockEdge> {
93 
94  static unsigned getHashValue(const BasicBlockEdge *V);
95 
96  static inline BasicBlockEdge getEmptyKey() {
97  return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
98  }
99 
100  static inline BasicBlockEdge getTombstoneKey() {
101  return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
102  }
103 
104  static unsigned getHashValue(const BasicBlockEdge &Edge) {
105  return hash_combine(BBInfo::getHashValue(Edge.getStart()),
106  BBInfo::getHashValue(Edge.getEnd()));
107  }
108 
109  static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
110  return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
111  BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
112  }
113 };
114 
115 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
116 /// normal dominator tree.
117 ///
118 /// Definition: A block is said to be forward statically reachable if there is
119 /// a path from the entry of the function to the block. A statically reachable
120 /// block may become statically unreachable during optimization.
121 ///
122 /// A forward unreachable block may appear in the dominator tree, or it may
123 /// not. If it does, dominance queries will return results as if all reachable
124 /// blocks dominate it. When asking for a Node corresponding to a potentially
125 /// unreachable block, calling code must handle the case where the block was
126 /// unreachable and the result of getNode() is nullptr.
127 ///
128 /// Generally, a block known to be unreachable when the dominator tree is
129 /// constructed will not be in the tree. One which becomes unreachable after
130 /// the dominator tree is initially constructed may still exist in the tree,
131 /// even if the tree is properly updated. Calling code should not rely on the
132 /// preceding statements; this is stated only to assist human understanding.
134  public:
136 
137  DominatorTree() = default;
138  explicit DominatorTree(Function &F) { recalculate(F); }
139 
140  /// Handle invalidation explicitly.
141  bool invalidate(Function &F, const PreservedAnalyses &PA,
143 
144  /// \brief Returns *false* if the other dominator tree matches this dominator
145  /// tree.
146  inline bool compare(const DominatorTree &Other) const {
147  const DomTreeNode *R = getRootNode();
148  const DomTreeNode *OtherR = Other.getRootNode();
149  return !R || !OtherR || R->getBlock() != OtherR->getBlock() ||
150  Base::compare(Other);
151  }
152 
153  // Ensure base-class overloads are visible.
154  using Base::dominates;
155 
156  /// \brief Return true if Def dominates a use in User.
157  ///
158  /// This performs the special checks necessary if Def and User are in the same
159  /// basic block. Note that Def doesn't dominate a use in Def itself!
160  bool dominates(const Instruction *Def, const Use &U) const;
161  bool dominates(const Instruction *Def, const Instruction *User) const;
162  bool dominates(const Instruction *Def, const BasicBlock *BB) const;
163 
164  /// Return true if an edge dominates a use.
165  ///
166  /// If BBE is not a unique edge between start and end of the edge, it can
167  /// never dominate the use.
168  bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
169  bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
170 
171  // Ensure base class overloads are visible.
172  using Base::isReachableFromEntry;
173 
174  /// \brief Provide an overload for a Use.
175  bool isReachableFromEntry(const Use &U) const;
176 
177  /// \brief Verify the correctness of the domtree by re-computing it.
178  ///
179  /// This should only be used for debugging as it aborts the program if the
180  /// verification fails.
181  void verifyDomTree() const;
182 
183  // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
184  void viewGraph(const Twine &Name, const Twine &Title);
185  void viewGraph();
186 };
187 
188 //===-------------------------------------
189 // DominatorTree GraphTraits specializations so the DominatorTree can be
190 // iterable by generic graph iterators.
191 
192 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
193  using NodeRef = Node *;
194  using ChildIteratorType = ChildIterator;
196 
197  static NodeRef getEntryNode(NodeRef N) { return N; }
198  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
199  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
200 
202  return df_begin(getEntryNode(N));
203  }
204 
205  static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
206 };
207 
208 template <>
211 
212 template <>
214  : public DomTreeGraphTraitsBase<const DomTreeNode,
216 
217 template <> struct GraphTraits<DominatorTree*>
218  : public GraphTraits<DomTreeNode*> {
219  static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
220 
221  static nodes_iterator nodes_begin(DominatorTree *N) {
222  return df_begin(getEntryNode(N));
223  }
224 
225  static nodes_iterator nodes_end(DominatorTree *N) {
226  return df_end(getEntryNode(N));
227  }
228 };
229 
230 /// \brief Analysis pass which computes a \c DominatorTree.
231 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
233  static AnalysisKey Key;
234 
235 public:
236  /// \brief Provide the result typedef for this analysis pass.
238 
239  /// \brief Run the analysis pass over a function and produce a dominator tree.
241 };
242 
243 /// \brief Printer pass for the \c DominatorTree.
245  : public PassInfoMixin<DominatorTreePrinterPass> {
246  raw_ostream &OS;
247 
248 public:
250 
252 };
253 
254 /// \brief Verifier pass for the \c DominatorTree.
255 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
257 };
258 
259 /// \brief Legacy analysis pass which computes a \c DominatorTree.
261  DominatorTree DT;
262 
263 public:
264  static char ID;
265 
268  }
269 
270  DominatorTree &getDomTree() { return DT; }
271  const DominatorTree &getDomTree() const { return DT; }
272 
273  bool runOnFunction(Function &F) override;
274 
275  void verifyAnalysis() const override;
276 
277  void getAnalysisUsage(AnalysisUsage &AU) const override {
278  AU.setPreservesAll();
279  }
280 
281  void releaseMemory() override { DT.releaseMemory(); }
282 
283  void print(raw_ostream &OS, const Module *M = nullptr) const override;
284 };
285 
286 } // end namespace llvm
287 
288 #endif // LLVM_IR_DOMINATORS_H
typename std::vector< DomTreeNodeBase *>::const_iterator const_iterator
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
DominatorTree(Function &F)
Definition: Dominators.h:138
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition: Dominators.h:109
template void DeleteEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
static NodeRef getEntryNode(NodeRef N)
Definition: Dominators.h:197
template bool Verify< BBDomTree >(const BBDomTree &DT)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Dominators.h:277
unsigned second
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:231
const BasicBlock * getEnd() const
Definition: Dominators.h:83
static nodes_iterator nodes_begin(NodeRef N)
Definition: Dominators.h:201
static nodes_iterator nodes_begin(DominatorTree *N)
Definition: Dominators.h:221
void initializeDominatorTreeWrapperPassPass(PassRegistry &)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
BasicBlockEdge(const std::pair< BasicBlock *, BasicBlock *> &Pair)
Definition: Dominators.h:73
static BasicBlockEdge getEmptyKey()
Definition: Dominators.h:96
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
DominatorTree & getDomTree()
Definition: Dominators.h:270
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:695
#define F(x, y, z)
Definition: MD5.cpp:55
static bool isEqual(const Function &Caller, const Function &Callee)
static nodes_iterator nodes_end(DominatorTree *N)
Definition: Dominators.h:225
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:133
const char * Name
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:281
static nodes_iterator nodes_end(NodeRef N)
Definition: Dominators.h:205
Core dominator tree base class.
Definition: LoopInfo.h:60
NodeT * getBlock() const
typename DomTreeNode *::UnknownGraphTypeError NodeRef
Definition: GraphTraits.h:59
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
DominatorTree run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
Definition: Dominators.cpp:328
df_iterator< T > df_end(const T &G)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
Represent the analysis usage information of a pass.
static const unsigned End
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Printer pass for the DominatorTree.
Definition: Dominators.h:244
template void Calculate< BBDomTree >(BBDomTree &DT)
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
Verifier pass for the DominatorTree.
Definition: Dominators.h:255
unsigned first
static ChildIteratorType child_begin(NodeRef N)
Definition: Dominators.h:198
static ChildIteratorType child_end(NodeRef N)
Definition: Dominators.h:199
template void InsertEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
const size_t N
const DominatorTree & getDomTree() const
Definition: Dominators.h:271
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.
template void DeleteEdge< BBDomTree >(BBDomTree &DT, BasicBlock *From, BasicBlock *To)
bool compare(const DominatorTree &Other) const
Returns false if the other dominator tree matches this dominator tree.
Definition: Dominators.h:146
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition: Dominators.h:104
static NodeRef getEntryNode(DominatorTree *DT)
Definition: Dominators.h:219
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:559
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
Definition: ScaledNumber.h:252
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition: Dominators.h:70
const BasicBlock * getStart() const
Definition: Dominators.h:79
aarch64 promote const
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:63
template void Calculate< BBPostDomTree >(BBPostDomTree &DT)
template bool Verify< BBPostDomTree >(const BBPostDomTree &DT)
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...
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:260
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:70
BasicBlockEdge(const std::pair< const BasicBlock *, const BasicBlock *> &Pair)
Definition: Dominators.h:76
template void InsertEdge< BBDomTree >(BBDomTree &DT, BasicBlock *From, BasicBlock *To)
static BasicBlockEdge getTombstoneKey()
Definition: Dominators.h:100