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/DenseMapInfo.h"
19 #include "llvm/ADT/DepthFirstIterator.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"
26 #include "llvm/Support/GenericDomTree.h"
36 extern template class DomTreeNodeBase<BasicBlock>;
37 extern template class DominatorTreeBase<BasicBlock>;
39 namespace DomTreeBuilder {
40 extern template void Calculate<Function, BasicBlock *>(
41 DominatorTreeBaseByGraphTraits<GraphTraits<BasicBlock *>> &DT, Function &F);
43 extern template void Calculate<Function, Inverse<BasicBlock *>>(
44 DominatorTreeBaseByGraphTraits<GraphTraits<Inverse<BasicBlock *>>> &DT,
47 extern template bool Verify<BasicBlock *>(
48 const DominatorTreeBaseByGraphTraits<GraphTraits<BasicBlock *>> &DT);
50 extern template bool Verify<Inverse<BasicBlock *>>(
51 const DominatorTreeBaseByGraphTraits<GraphTraits<Inverse<BasicBlock *>>>
53 } // namespace DomTreeBuilder
55 using DomTreeNode = DomTreeNodeBase<BasicBlock>;
57 class BasicBlockEdge {
58 const BasicBlock *Start;
59 const BasicBlock *End;
62 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
63 Start(Start_), End(End_) {}
65 BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
66 : Start(Pair.first), End(Pair.second) {}
68 BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
69 : Start(Pair.first), End(Pair.second) {}
71 const BasicBlock *getStart() const {
75 const BasicBlock *getEnd() const {
79 /// Check if this is the only edge between Start and End.
80 bool isSingleEdge() const;
83 template <> struct DenseMapInfo<BasicBlockEdge> {
84 using BBInfo = DenseMapInfo<const BasicBlock *>;
86 static unsigned getHashValue(const BasicBlockEdge *V);
88 static inline BasicBlockEdge getEmptyKey() {
89 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
92 static inline BasicBlockEdge getTombstoneKey() {
93 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
96 static unsigned getHashValue(const BasicBlockEdge &Edge) {
97 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
98 BBInfo::getHashValue(Edge.getEnd()));
101 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
102 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
103 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
107 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
108 /// normal dominator tree.
110 /// Definition: A block is said to be forward statically reachable if there is
111 /// a path from the entry of the function to the block. A statically reachable
112 /// block may become statically unreachable during optimization.
114 /// A forward unreachable block may appear in the dominator tree, or it may
115 /// not. If it does, dominance queries will return results as if all reachable
116 /// blocks dominate it. When asking for a Node corresponding to a potentially
117 /// unreachable block, calling code must handle the case where the block was
118 /// unreachable and the result of getNode() is nullptr.
120 /// Generally, a block known to be unreachable when the dominator tree is
121 /// constructed will not be in the tree. One which becomes unreachable after
122 /// the dominator tree is initially constructed may still exist in the tree,
123 /// even if the tree is properly updated. Calling code should not rely on the
124 /// preceding statements; this is stated only to assist human understanding.
125 class DominatorTree : public DominatorTreeBase<BasicBlock> {
127 using Base = DominatorTreeBase<BasicBlock>;
129 DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
130 explicit DominatorTree(Function &F) : DominatorTreeBase<BasicBlock>(false) {
134 /// Handle invalidation explicitly.
135 bool invalidate(Function &F, const PreservedAnalyses &PA,
136 FunctionAnalysisManager::Invalidator &);
138 /// \brief Returns *false* if the other dominator tree matches this dominator
140 inline bool compare(const DominatorTree &Other) const {
141 const DomTreeNode *R = getRootNode();
142 const DomTreeNode *OtherR = Other.getRootNode();
143 return !R || !OtherR || R->getBlock() != OtherR->getBlock() ||
144 Base::compare(Other);
147 // Ensure base-class overloads are visible.
148 using Base::dominates;
150 /// \brief Return true if Def dominates a use in User.
152 /// This performs the special checks necessary if Def and User are in the same
153 /// basic block. Note that Def doesn't dominate a use in Def itself!
154 bool dominates(const Instruction *Def, const Use &U) const;
155 bool dominates(const Instruction *Def, const Instruction *User) const;
156 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
158 /// Return true if an edge dominates a use.
160 /// If BBE is not a unique edge between start and end of the edge, it can
161 /// never dominate the use.
162 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
163 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
165 // Ensure base class overloads are visible.
166 using Base::isReachableFromEntry;
168 /// \brief Provide an overload for a Use.
169 bool isReachableFromEntry(const Use &U) const;
171 /// \brief Verify the correctness of the domtree by re-computing it.
173 /// This should only be used for debugging as it aborts the program if the
174 /// verification fails.
175 void verifyDomTree() const;
177 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
178 void viewGraph(const Twine &Name, const Twine &Title);
182 //===-------------------------------------
183 // DominatorTree GraphTraits specializations so the DominatorTree can be
184 // iterable by generic graph iterators.
186 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
187 using NodeRef = Node *;
188 using ChildIteratorType = ChildIterator;
189 using nodes_iterator = df_iterator<Node *, df_iterator_default_set<Node*>>;
191 static NodeRef getEntryNode(NodeRef N) { return N; }
192 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
193 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
195 static nodes_iterator nodes_begin(NodeRef N) {
196 return df_begin(getEntryNode(N));
199 static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
203 struct GraphTraits<DomTreeNode *>
204 : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::iterator> {};
207 struct GraphTraits<const DomTreeNode *>
208 : public DomTreeGraphTraitsBase<const DomTreeNode,
209 DomTreeNode::const_iterator> {};
211 template <> struct GraphTraits<DominatorTree*>
212 : public GraphTraits<DomTreeNode*> {
213 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
215 static nodes_iterator nodes_begin(DominatorTree *N) {
216 return df_begin(getEntryNode(N));
219 static nodes_iterator nodes_end(DominatorTree *N) {
220 return df_end(getEntryNode(N));
224 /// \brief Analysis pass which computes a \c DominatorTree.
225 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
226 friend AnalysisInfoMixin<DominatorTreeAnalysis>;
227 static AnalysisKey Key;
230 /// \brief Provide the result typedef for this analysis pass.
231 using Result = DominatorTree;
233 /// \brief Run the analysis pass over a function and produce a dominator tree.
234 DominatorTree run(Function &F, FunctionAnalysisManager &);
237 /// \brief Printer pass for the \c DominatorTree.
238 class DominatorTreePrinterPass
239 : public PassInfoMixin<DominatorTreePrinterPass> {
243 explicit DominatorTreePrinterPass(raw_ostream &OS);
245 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
248 /// \brief Verifier pass for the \c DominatorTree.
249 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
250 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
253 /// \brief Legacy analysis pass which computes a \c DominatorTree.
254 class DominatorTreeWrapperPass : public FunctionPass {
260 DominatorTreeWrapperPass() : FunctionPass(ID) {
261 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
264 DominatorTree &getDomTree() { return DT; }
265 const DominatorTree &getDomTree() const { return DT; }
267 bool runOnFunction(Function &F) override;
269 void verifyAnalysis() const override;
271 void getAnalysisUsage(AnalysisUsage &AU) const override {
272 AU.setPreservesAll();
275 void releaseMemory() override { DT.releaseMemory(); }
277 void print(raw_ostream &OS, const Module *M = nullptr) const override;
280 } // end namespace llvm
282 #endif // LLVM_IR_DOMINATORS_H