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/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"
24 #include "llvm/Support/GenericDomTree.h"
32 extern template class DomTreeNodeBase<BasicBlock>;
33 extern template class DominatorTreeBase<BasicBlock>;
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,
41 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
43 class BasicBlockEdge {
44 const BasicBlock *Start;
45 const BasicBlock *End;
47 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
48 Start(Start_), End(End_) { }
49 const BasicBlock *getStart() const {
52 const BasicBlock *getEnd() const {
55 bool isSingleEdge() const;
58 template <> struct DenseMapInfo<BasicBlockEdge> {
59 static unsigned getHashValue(const BasicBlockEdge *V);
60 typedef DenseMapInfo<const BasicBlock *> BBInfo;
61 static inline BasicBlockEdge getEmptyKey() {
62 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
64 static inline BasicBlockEdge getTombstoneKey() {
65 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
68 static unsigned getHashValue(const BasicBlockEdge &Edge) {
69 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
70 BBInfo::getHashValue(Edge.getEnd()));
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());
78 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
79 /// normal dominator tree.
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.
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.
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> {
98 typedef DominatorTreeBase<BasicBlock> Base;
100 DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
101 explicit DominatorTree(Function &F) : DominatorTreeBase<BasicBlock>(false) {
105 /// \brief Returns *false* if the other dominator tree matches this dominator
107 inline bool compare(const DominatorTree &Other) const {
108 const DomTreeNode *R = getRootNode();
109 const DomTreeNode *OtherR = Other.getRootNode();
111 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
114 if (Base::compare(Other))
120 // Ensure base-class overloads are visible.
121 using Base::dominates;
123 /// \brief Return true if Def dominates a use in User.
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;
133 // Ensure base class overloads are visible.
134 using Base::isReachableFromEntry;
136 /// \brief Provide an overload for a Use.
137 bool isReachableFromEntry(const Use &U) const;
139 /// \brief Verify the correctness of the domtree by re-computing it.
141 /// This should only be used for debugging as it aborts the program if the
142 /// verification fails.
143 void verifyDomTree() const;
146 //===-------------------------------------
147 // DominatorTree GraphTraits specializations so the DominatorTree can be
148 // iterable by generic graph iterators.
150 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
151 typedef Node *NodeRef;
152 typedef ChildIterator ChildIteratorType;
153 typedef df_iterator<Node *, df_iterator_default_set<Node*>> nodes_iterator;
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(); }
159 static nodes_iterator nodes_begin(NodeRef N) {
160 return df_begin(getEntryNode(N));
163 static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
167 struct GraphTraits<DomTreeNode *>
168 : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::iterator> {};
171 struct GraphTraits<const DomTreeNode *>
172 : public DomTreeGraphTraitsBase<const DomTreeNode,
173 DomTreeNode::const_iterator> {};
175 template <> struct GraphTraits<DominatorTree*>
176 : public GraphTraits<DomTreeNode*> {
177 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
179 static nodes_iterator nodes_begin(DominatorTree *N) {
180 return df_begin(getEntryNode(N));
183 static nodes_iterator nodes_end(DominatorTree *N) {
184 return df_end(getEntryNode(N));
188 /// \brief Analysis pass which computes a \c DominatorTree.
189 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
190 friend AnalysisInfoMixin<DominatorTreeAnalysis>;
191 static AnalysisKey Key;
194 /// \brief Provide the result typedef for this analysis pass.
195 typedef DominatorTree Result;
197 /// \brief Run the analysis pass over a function and produce a dominator tree.
198 DominatorTree run(Function &F, FunctionAnalysisManager &);
201 /// \brief Printer pass for the \c DominatorTree.
202 class DominatorTreePrinterPass
203 : public PassInfoMixin<DominatorTreePrinterPass> {
207 explicit DominatorTreePrinterPass(raw_ostream &OS);
208 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
211 /// \brief Verifier pass for the \c DominatorTree.
212 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
213 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
216 /// \brief Legacy analysis pass which computes a \c DominatorTree.
217 class DominatorTreeWrapperPass : public FunctionPass {
223 DominatorTreeWrapperPass() : FunctionPass(ID) {
224 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
227 DominatorTree &getDomTree() { return DT; }
228 const DominatorTree &getDomTree() const { return DT; }
230 bool runOnFunction(Function &F) override;
232 void verifyAnalysis() const override;
234 void getAnalysisUsage(AnalysisUsage &AU) const override {
235 AU.setPreservesAll();
238 void releaseMemory() override { DT.releaseMemory(); }
240 void print(raw_ostream &OS, const Module *M = nullptr) const override;
243 } // End llvm namespace