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, false>; // DomTree
38 extern template class DominatorTreeBase<BasicBlock, true>; // PostDomTree
40 namespace DomTreeBuilder {
41 using BBDomTree = DomTreeBase<BasicBlock>;
42 using BBPostDomTree = PostDomTreeBase<BasicBlock>;
44 extern template void Calculate<BBDomTree, Function>(BBDomTree &DT, Function &F);
45 extern template void Calculate<BBPostDomTree, Function>(BBPostDomTree &DT,
48 extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
50 extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT,
54 extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
56 extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT,
60 extern template bool Verify<BBDomTree>(const BBDomTree &DT);
61 extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT);
62 } // namespace DomTreeBuilder
64 using DomTreeNode = DomTreeNodeBase<BasicBlock>;
66 class BasicBlockEdge {
67 const BasicBlock *Start;
68 const BasicBlock *End;
71 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
72 Start(Start_), End(End_) {}
74 BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
75 : Start(Pair.first), End(Pair.second) {}
77 BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
78 : Start(Pair.first), End(Pair.second) {}
80 const BasicBlock *getStart() const {
84 const BasicBlock *getEnd() const {
88 /// Check if this is the only edge between Start and End.
89 bool isSingleEdge() const;
92 template <> struct DenseMapInfo<BasicBlockEdge> {
93 using BBInfo = DenseMapInfo<const BasicBlock *>;
95 static unsigned getHashValue(const BasicBlockEdge *V);
97 static inline BasicBlockEdge getEmptyKey() {
98 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
101 static inline BasicBlockEdge getTombstoneKey() {
102 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
105 static unsigned getHashValue(const BasicBlockEdge &Edge) {
106 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
107 BBInfo::getHashValue(Edge.getEnd()));
110 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
111 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
112 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
116 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
117 /// normal dominator tree.
119 /// Definition: A block is said to be forward statically reachable if there is
120 /// a path from the entry of the function to the block. A statically reachable
121 /// block may become statically unreachable during optimization.
123 /// A forward unreachable block may appear in the dominator tree, or it may
124 /// not. If it does, dominance queries will return results as if all reachable
125 /// blocks dominate it. When asking for a Node corresponding to a potentially
126 /// unreachable block, calling code must handle the case where the block was
127 /// unreachable and the result of getNode() is nullptr.
129 /// Generally, a block known to be unreachable when the dominator tree is
130 /// constructed will not be in the tree. One which becomes unreachable after
131 /// the dominator tree is initially constructed may still exist in the tree,
132 /// even if the tree is properly updated. Calling code should not rely on the
133 /// preceding statements; this is stated only to assist human understanding.
134 class DominatorTree : public DominatorTreeBase<BasicBlock, false> {
136 using Base = DominatorTreeBase<BasicBlock, false>;
138 DominatorTree() = default;
139 explicit DominatorTree(Function &F) { recalculate(F); }
141 /// Handle invalidation explicitly.
142 bool invalidate(Function &F, const PreservedAnalyses &PA,
143 FunctionAnalysisManager::Invalidator &);
145 /// \brief Returns *false* if the other dominator tree matches this dominator
147 inline bool compare(const DominatorTree &Other) const {
148 const DomTreeNode *R = getRootNode();
149 const DomTreeNode *OtherR = Other.getRootNode();
150 return !R || !OtherR || R->getBlock() != OtherR->getBlock() ||
151 Base::compare(Other);
154 // Ensure base-class overloads are visible.
155 using Base::dominates;
157 /// \brief Return true if Def dominates a use in User.
159 /// This performs the special checks necessary if Def and User are in the same
160 /// basic block. Note that Def doesn't dominate a use in Def itself!
161 bool dominates(const Instruction *Def, const Use &U) const;
162 bool dominates(const Instruction *Def, const Instruction *User) const;
163 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
165 /// Return true if an edge dominates a use.
167 /// If BBE is not a unique edge between start and end of the edge, it can
168 /// never dominate the use.
169 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
170 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
172 // Ensure base class overloads are visible.
173 using Base::isReachableFromEntry;
175 /// \brief Provide an overload for a Use.
176 bool isReachableFromEntry(const Use &U) const;
178 /// \brief Verify the correctness of the domtree by re-computing it.
180 /// This should only be used for debugging as it aborts the program if the
181 /// verification fails.
182 void verifyDomTree() const;
184 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
185 void viewGraph(const Twine &Name, const Twine &Title);
189 //===-------------------------------------
190 // DominatorTree GraphTraits specializations so the DominatorTree can be
191 // iterable by generic graph iterators.
193 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
194 using NodeRef = Node *;
195 using ChildIteratorType = ChildIterator;
196 using nodes_iterator = df_iterator<Node *, df_iterator_default_set<Node*>>;
198 static NodeRef getEntryNode(NodeRef N) { return N; }
199 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
200 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
202 static nodes_iterator nodes_begin(NodeRef N) {
203 return df_begin(getEntryNode(N));
206 static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
210 struct GraphTraits<DomTreeNode *>
211 : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::iterator> {};
214 struct GraphTraits<const DomTreeNode *>
215 : public DomTreeGraphTraitsBase<const DomTreeNode,
216 DomTreeNode::const_iterator> {};
218 template <> struct GraphTraits<DominatorTree*>
219 : public GraphTraits<DomTreeNode*> {
220 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
222 static nodes_iterator nodes_begin(DominatorTree *N) {
223 return df_begin(getEntryNode(N));
226 static nodes_iterator nodes_end(DominatorTree *N) {
227 return df_end(getEntryNode(N));
231 /// \brief Analysis pass which computes a \c DominatorTree.
232 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
233 friend AnalysisInfoMixin<DominatorTreeAnalysis>;
234 static AnalysisKey Key;
237 /// \brief Provide the result typedef for this analysis pass.
238 using Result = DominatorTree;
240 /// \brief Run the analysis pass over a function and produce a dominator tree.
241 DominatorTree run(Function &F, FunctionAnalysisManager &);
244 /// \brief Printer pass for the \c DominatorTree.
245 class DominatorTreePrinterPass
246 : public PassInfoMixin<DominatorTreePrinterPass> {
250 explicit DominatorTreePrinterPass(raw_ostream &OS);
252 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
255 /// \brief Verifier pass for the \c DominatorTree.
256 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
257 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
260 /// \brief Legacy analysis pass which computes a \c DominatorTree.
261 class DominatorTreeWrapperPass : public FunctionPass {
267 DominatorTreeWrapperPass() : FunctionPass(ID) {
268 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
271 DominatorTree &getDomTree() { return DT; }
272 const DominatorTree &getDomTree() const { return DT; }
274 bool runOnFunction(Function &F) override;
276 void verifyAnalysis() const override;
278 void getAnalysisUsage(AnalysisUsage &AU) const override {
279 AU.setPreservesAll();
282 void releaseMemory() override { DT.releaseMemory(); }
284 void print(raw_ostream &OS, const Module *M = nullptr) const override;
287 } // end namespace llvm
289 #endif // LLVM_IR_DOMINATORS_H