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 DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT, Function &F);
37 extern template void Calculate<Function, Inverse<BasicBlock *>>(
38 DominatorTreeBase<GraphTraits<Inverse<BasicBlock *>>::NodeType> &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 DominatorTree(DominatorTree &&Arg)
106 : Base(std::move(static_cast<Base &>(Arg))) {}
107 DominatorTree &operator=(DominatorTree &&RHS) {
108 Base::operator=(std::move(static_cast<Base &>(RHS)));
112 /// \brief Returns *false* if the other dominator tree matches this dominator
114 inline bool compare(const DominatorTree &Other) const {
115 const DomTreeNode *R = getRootNode();
116 const DomTreeNode *OtherR = Other.getRootNode();
118 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
121 if (Base::compare(Other))
127 // Ensure base-class overloads are visible.
128 using Base::dominates;
130 /// \brief Return true if Def dominates a use in User.
132 /// This performs the special checks necessary if Def and User are in the same
133 /// basic block. Note that Def doesn't dominate a use in Def itself!
134 bool dominates(const Instruction *Def, const Use &U) const;
135 bool dominates(const Instruction *Def, const Instruction *User) const;
136 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
137 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
138 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
140 // Ensure base class overloads are visible.
141 using Base::isReachableFromEntry;
143 /// \brief Provide an overload for a Use.
144 bool isReachableFromEntry(const Use &U) const;
146 /// \brief Verify the correctness of the domtree by re-computing it.
148 /// This should only be used for debugging as it aborts the program if the
149 /// verification fails.
150 void verifyDomTree() const;
153 //===-------------------------------------
154 // DominatorTree GraphTraits specializations so the DominatorTree can be
155 // iterable by generic graph iterators.
157 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
158 typedef Node NodeType;
159 typedef ChildIterator ChildIteratorType;
160 typedef df_iterator<Node *, SmallPtrSet<NodeType *, 8>> nodes_iterator;
162 static NodeType *getEntryNode(NodeType *N) { return N; }
163 static inline ChildIteratorType child_begin(NodeType *N) {
166 static inline ChildIteratorType child_end(NodeType *N) { return N->end(); }
168 static nodes_iterator nodes_begin(NodeType *N) {
169 return df_begin(getEntryNode(N));
172 static nodes_iterator nodes_end(NodeType *N) {
173 return df_end(getEntryNode(N));
178 struct GraphTraits<DomTreeNode *>
179 : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::iterator> {};
182 struct GraphTraits<const DomTreeNode *>
183 : public DomTreeGraphTraitsBase<const DomTreeNode,
184 DomTreeNode::const_iterator> {};
186 template <> struct GraphTraits<DominatorTree*>
187 : public GraphTraits<DomTreeNode*> {
188 static NodeType *getEntryNode(DominatorTree *DT) {
189 return DT->getRootNode();
192 static nodes_iterator nodes_begin(DominatorTree *N) {
193 return df_begin(getEntryNode(N));
196 static nodes_iterator nodes_end(DominatorTree *N) {
197 return df_end(getEntryNode(N));
201 /// \brief Analysis pass which computes a \c DominatorTree.
202 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
203 friend AnalysisInfoMixin<DominatorTreeAnalysis>;
207 /// \brief Provide the result typedef for this analysis pass.
208 typedef DominatorTree Result;
210 /// \brief Run the analysis pass over a function and produce a dominator tree.
211 DominatorTree run(Function &F, AnalysisManager<Function> &);
214 /// \brief Printer pass for the \c DominatorTree.
215 class DominatorTreePrinterPass
216 : public PassInfoMixin<DominatorTreePrinterPass> {
220 explicit DominatorTreePrinterPass(raw_ostream &OS);
221 PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
224 /// \brief Verifier pass for the \c DominatorTree.
225 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
226 PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
229 /// \brief Legacy analysis pass which computes a \c DominatorTree.
230 class DominatorTreeWrapperPass : public FunctionPass {
236 DominatorTreeWrapperPass() : FunctionPass(ID) {
237 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
240 DominatorTree &getDomTree() { return DT; }
241 const DominatorTree &getDomTree() const { return DT; }
243 bool runOnFunction(Function &F) override;
245 void verifyAnalysis() const override;
247 void getAnalysisUsage(AnalysisUsage &AU) const override {
248 AU.setPreservesAll();
251 void releaseMemory() override { DT.releaseMemory(); }
253 void print(raw_ostream &OS, const Module *M = nullptr) const override;
256 } // End llvm namespace