1 //===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the DominatorTree class, which provides fast and efficient 10 // dominance queries. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_IR_DOMINATORS_H 15 #define LLVM_IR_DOMINATORS_H 16 17 #include "llvm/ADT/DenseMapInfo.h" 18 #include "llvm/ADT/DepthFirstIterator.h" 19 #include "llvm/ADT/GraphTraits.h" 20 #include "llvm/ADT/Hashing.h" 21 #include "llvm/IR/BasicBlock.h" 22 #include "llvm/IR/CFG.h" 23 #include "llvm/IR/PassManager.h" 24 #include "llvm/Pass.h" 25 #include "llvm/Support/GenericDomTree.h" 26 #include <utility> 27 28 namespace llvm { 29 30 class Function; 31 class Instruction; 32 class Module; 33 class raw_ostream; 34 35 extern template class DomTreeNodeBase<BasicBlock>; 36 extern template class DominatorTreeBase<BasicBlock, false>; // DomTree 37 extern template class DominatorTreeBase<BasicBlock, true>; // PostDomTree 38 39 extern template class cfg::Update<BasicBlock *>; 40 41 namespace DomTreeBuilder { 42 using BBDomTree = DomTreeBase<BasicBlock>; 43 using BBPostDomTree = PostDomTreeBase<BasicBlock>; 44 45 using BBUpdates = ArrayRef<llvm::cfg::Update<BasicBlock *>>; 46 47 extern template void Calculate<BBDomTree>(BBDomTree &DT); 48 extern template void CalculateWithUpdates<BBDomTree>(BBDomTree &DT, 49 BBUpdates U); 50 51 extern template void Calculate<BBPostDomTree>(BBPostDomTree &DT); 52 53 extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From, 54 BasicBlock *To); 55 extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT, 56 BasicBlock *From, 57 BasicBlock *To); 58 59 extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From, 60 BasicBlock *To); 61 extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT, 62 BasicBlock *From, 63 BasicBlock *To); 64 65 extern template void ApplyUpdates<BBDomTree>(BBDomTree &DT, BBUpdates); 66 extern template void ApplyUpdates<BBPostDomTree>(BBPostDomTree &DT, BBUpdates); 67 68 extern template bool Verify<BBDomTree>(const BBDomTree &DT, 69 BBDomTree::VerificationLevel VL); 70 extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT, 71 BBPostDomTree::VerificationLevel VL); 72 } // namespace DomTreeBuilder 73 74 using DomTreeNode = DomTreeNodeBase<BasicBlock>; 75 76 class BasicBlockEdge { 77 const BasicBlock *Start; 78 const BasicBlock *End; 79 80 public: 81 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) : 82 Start(Start_), End(End_) {} 83 84 BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair) 85 : Start(Pair.first), End(Pair.second) {} 86 87 BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair) 88 : Start(Pair.first), End(Pair.second) {} 89 90 const BasicBlock *getStart() const { 91 return Start; 92 } 93 94 const BasicBlock *getEnd() const { 95 return End; 96 } 97 98 /// Check if this is the only edge between Start and End. 99 bool isSingleEdge() const; 100 }; 101 102 template <> struct DenseMapInfo<BasicBlockEdge> { 103 using BBInfo = DenseMapInfo<const BasicBlock *>; 104 105 static unsigned getHashValue(const BasicBlockEdge *V); 106 107 static inline BasicBlockEdge getEmptyKey() { 108 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey()); 109 } 110 111 static inline BasicBlockEdge getTombstoneKey() { 112 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey()); 113 } 114 115 static unsigned getHashValue(const BasicBlockEdge &Edge) { 116 return hash_combine(BBInfo::getHashValue(Edge.getStart()), 117 BBInfo::getHashValue(Edge.getEnd())); 118 } 119 120 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) { 121 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) && 122 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd()); 123 } 124 }; 125 126 /// Concrete subclass of DominatorTreeBase that is used to compute a 127 /// normal dominator tree. 128 /// 129 /// Definition: A block is said to be forward statically reachable if there is 130 /// a path from the entry of the function to the block. A statically reachable 131 /// block may become statically unreachable during optimization. 132 /// 133 /// A forward unreachable block may appear in the dominator tree, or it may 134 /// not. If it does, dominance queries will return results as if all reachable 135 /// blocks dominate it. When asking for a Node corresponding to a potentially 136 /// unreachable block, calling code must handle the case where the block was 137 /// unreachable and the result of getNode() is nullptr. 138 /// 139 /// Generally, a block known to be unreachable when the dominator tree is 140 /// constructed will not be in the tree. One which becomes unreachable after 141 /// the dominator tree is initially constructed may still exist in the tree, 142 /// even if the tree is properly updated. Calling code should not rely on the 143 /// preceding statements; this is stated only to assist human understanding. 144 class DominatorTree : public DominatorTreeBase<BasicBlock, false> { 145 public: 146 using Base = DominatorTreeBase<BasicBlock, false>; 147 148 DominatorTree() = default; 149 explicit DominatorTree(Function &F) { recalculate(F); } 150 explicit DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U) { 151 recalculate(*DT.Parent, U); 152 } 153 154 /// Handle invalidation explicitly. 155 bool invalidate(Function &F, const PreservedAnalyses &PA, 156 FunctionAnalysisManager::Invalidator &); 157 158 // Ensure base-class overloads are visible. 159 using Base::dominates; 160 161 /// Return true if Def dominates a use in User. 162 /// 163 /// This performs the special checks necessary if Def and User are in the same 164 /// basic block. Note that Def doesn't dominate a use in Def itself! 165 bool dominates(const Instruction *Def, const Use &U) const; 166 bool dominates(const Instruction *Def, const Instruction *User) const; 167 bool dominates(const Instruction *Def, const BasicBlock *BB) const; 168 169 /// Return true if an edge dominates a use. 170 /// 171 /// If BBE is not a unique edge between start and end of the edge, it can 172 /// never dominate the use. 173 bool dominates(const BasicBlockEdge &BBE, const Use &U) const; 174 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const; 175 /// Returns true if edge \p BBE1 dominates edge \p BBE2. 176 bool dominates(const BasicBlockEdge &BBE1, const BasicBlockEdge &BBE2) const; 177 178 // Ensure base class overloads are visible. 179 using Base::isReachableFromEntry; 180 181 /// Provide an overload for a Use. 182 bool isReachableFromEntry(const Use &U) const; 183 184 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`. 185 void viewGraph(const Twine &Name, const Twine &Title); 186 void viewGraph(); 187 }; 188 189 //===------------------------------------- 190 // DominatorTree GraphTraits specializations so the DominatorTree can be 191 // iterable by generic graph iterators. 192 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*>>; 197 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(); } 201 202 static nodes_iterator nodes_begin(NodeRef N) { 203 return df_begin(getEntryNode(N)); 204 } 205 206 static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); } 207 }; 208 209 template <> 210 struct GraphTraits<DomTreeNode *> 211 : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::const_iterator> { 212 }; 213 214 template <> 215 struct GraphTraits<const DomTreeNode *> 216 : public DomTreeGraphTraitsBase<const DomTreeNode, 217 DomTreeNode::const_iterator> {}; 218 219 template <> struct GraphTraits<DominatorTree*> 220 : public GraphTraits<DomTreeNode*> { 221 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); } 222 223 static nodes_iterator nodes_begin(DominatorTree *N) { 224 return df_begin(getEntryNode(N)); 225 } 226 227 static nodes_iterator nodes_end(DominatorTree *N) { 228 return df_end(getEntryNode(N)); 229 } 230 }; 231 232 /// Analysis pass which computes a \c DominatorTree. 233 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> { 234 friend AnalysisInfoMixin<DominatorTreeAnalysis>; 235 static AnalysisKey Key; 236 237 public: 238 /// Provide the result typedef for this analysis pass. 239 using Result = DominatorTree; 240 241 /// Run the analysis pass over a function and produce a dominator tree. 242 DominatorTree run(Function &F, FunctionAnalysisManager &); 243 }; 244 245 /// Printer pass for the \c DominatorTree. 246 class DominatorTreePrinterPass 247 : public PassInfoMixin<DominatorTreePrinterPass> { 248 raw_ostream &OS; 249 250 public: 251 explicit DominatorTreePrinterPass(raw_ostream &OS); 252 253 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 254 }; 255 256 /// Verifier pass for the \c DominatorTree. 257 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> { 258 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 259 }; 260 261 /// Legacy analysis pass which computes a \c DominatorTree. 262 class DominatorTreeWrapperPass : public FunctionPass { 263 DominatorTree DT; 264 265 public: 266 static char ID; 267 268 DominatorTreeWrapperPass(); 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.reset(); } 282 283 void print(raw_ostream &OS, const Module *M = nullptr) const override; 284 }; 285 } // end namespace llvm 286 287 #endif // LLVM_IR_DOMINATORS_H 288