blob: 08dbccaf2c013315e55bdc506075173676d8b665 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
28namespace llvm {
29
30class Function;
31class Instruction;
32class Module;
33class raw_ostream;
34
35extern template class DomTreeNodeBase<BasicBlock>;
36extern template class DominatorTreeBase<BasicBlock, false>; // DomTree
37extern template class DominatorTreeBase<BasicBlock, true>; // PostDomTree
38
Andrew Scull0372a572018-11-16 15:47:06 +000039extern template class cfg::Update<BasicBlock *>;
40
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041namespace DomTreeBuilder {
42using BBDomTree = DomTreeBase<BasicBlock>;
43using BBPostDomTree = PostDomTreeBase<BasicBlock>;
44
Andrew Scull0372a572018-11-16 15:47:06 +000045using BBUpdates = ArrayRef<llvm::cfg::Update<BasicBlock *>>;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020047using BBDomTreeGraphDiff = GraphDiff<BasicBlock *, false>;
48using BBPostDomTreeGraphDiff = GraphDiff<BasicBlock *, true>;
49
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050extern template void Calculate<BBDomTree>(BBDomTree &DT);
Andrew Scull0372a572018-11-16 15:47:06 +000051extern template void CalculateWithUpdates<BBDomTree>(BBDomTree &DT,
52 BBUpdates U);
53
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054extern template void Calculate<BBPostDomTree>(BBPostDomTree &DT);
55
56extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
57 BasicBlock *To);
58extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT,
59 BasicBlock *From,
60 BasicBlock *To);
61
62extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
63 BasicBlock *To);
64extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT,
65 BasicBlock *From,
66 BasicBlock *To);
67
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020068extern template void ApplyUpdates<BBDomTree>(BBDomTree &DT,
69 BBDomTreeGraphDiff &,
70 BBDomTreeGraphDiff *);
71extern template void ApplyUpdates<BBPostDomTree>(BBPostDomTree &DT,
72 BBPostDomTreeGraphDiff &,
73 BBPostDomTreeGraphDiff *);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074
75extern template bool Verify<BBDomTree>(const BBDomTree &DT,
76 BBDomTree::VerificationLevel VL);
77extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT,
78 BBPostDomTree::VerificationLevel VL);
79} // namespace DomTreeBuilder
80
81using DomTreeNode = DomTreeNodeBase<BasicBlock>;
82
83class BasicBlockEdge {
84 const BasicBlock *Start;
85 const BasicBlock *End;
86
87public:
88 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
89 Start(Start_), End(End_) {}
90
91 BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
92 : Start(Pair.first), End(Pair.second) {}
93
94 BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
95 : Start(Pair.first), End(Pair.second) {}
96
97 const BasicBlock *getStart() const {
98 return Start;
99 }
100
101 const BasicBlock *getEnd() const {
102 return End;
103 }
104
105 /// Check if this is the only edge between Start and End.
106 bool isSingleEdge() const;
107};
108
109template <> struct DenseMapInfo<BasicBlockEdge> {
110 using BBInfo = DenseMapInfo<const BasicBlock *>;
111
112 static unsigned getHashValue(const BasicBlockEdge *V);
113
114 static inline BasicBlockEdge getEmptyKey() {
115 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
116 }
117
118 static inline BasicBlockEdge getTombstoneKey() {
119 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
120 }
121
122 static unsigned getHashValue(const BasicBlockEdge &Edge) {
123 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
124 BBInfo::getHashValue(Edge.getEnd()));
125 }
126
127 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
128 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
129 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
130 }
131};
132
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100133/// Concrete subclass of DominatorTreeBase that is used to compute a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100134/// normal dominator tree.
135///
136/// Definition: A block is said to be forward statically reachable if there is
137/// a path from the entry of the function to the block. A statically reachable
138/// block may become statically unreachable during optimization.
139///
140/// A forward unreachable block may appear in the dominator tree, or it may
141/// not. If it does, dominance queries will return results as if all reachable
142/// blocks dominate it. When asking for a Node corresponding to a potentially
143/// unreachable block, calling code must handle the case where the block was
144/// unreachable and the result of getNode() is nullptr.
145///
146/// Generally, a block known to be unreachable when the dominator tree is
147/// constructed will not be in the tree. One which becomes unreachable after
148/// the dominator tree is initially constructed may still exist in the tree,
149/// even if the tree is properly updated. Calling code should not rely on the
150/// preceding statements; this is stated only to assist human understanding.
151class DominatorTree : public DominatorTreeBase<BasicBlock, false> {
152 public:
153 using Base = DominatorTreeBase<BasicBlock, false>;
154
155 DominatorTree() = default;
156 explicit DominatorTree(Function &F) { recalculate(F); }
Andrew Scull0372a572018-11-16 15:47:06 +0000157 explicit DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U) {
158 recalculate(*DT.Parent, U);
159 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100160
161 /// Handle invalidation explicitly.
162 bool invalidate(Function &F, const PreservedAnalyses &PA,
163 FunctionAnalysisManager::Invalidator &);
164
165 // Ensure base-class overloads are visible.
166 using Base::dominates;
167
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200168 /// Return true if value Def dominates use U, in the sense that Def is
169 /// available at U, and could be substituted as the used value without
170 /// violating the SSA dominance requirement.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100171 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200172 /// In particular, it is worth noting that:
173 /// * Non-instruction Defs dominate everything.
174 /// * Def does not dominate a use in Def itself (outside of degenerate cases
175 /// like unreachable code or trivial phi cycles).
176 /// * Invoke/callbr Defs only dominate uses in their default destination.
177 bool dominates(const Value *Def, const Use &U) const;
178 /// Return true if value Def dominates all possible uses inside instruction
179 /// User. Same comments as for the Use-based API apply.
180 bool dominates(const Value *Def, const Instruction *User) const;
181 // Does not accept Value to avoid ambiguity with dominance checks between
182 // two basic blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100183 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
184
185 /// Return true if an edge dominates a use.
186 ///
187 /// If BBE is not a unique edge between start and end of the edge, it can
188 /// never dominate the use.
189 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
190 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200191 /// Returns true if edge \p BBE1 dominates edge \p BBE2.
192 bool dominates(const BasicBlockEdge &BBE1, const BasicBlockEdge &BBE2) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100193
194 // Ensure base class overloads are visible.
195 using Base::isReachableFromEntry;
196
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100197 /// Provide an overload for a Use.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100198 bool isReachableFromEntry(const Use &U) const;
199
200 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
201 void viewGraph(const Twine &Name, const Twine &Title);
202 void viewGraph();
203};
204
205//===-------------------------------------
206// DominatorTree GraphTraits specializations so the DominatorTree can be
207// iterable by generic graph iterators.
208
209template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
210 using NodeRef = Node *;
211 using ChildIteratorType = ChildIterator;
212 using nodes_iterator = df_iterator<Node *, df_iterator_default_set<Node*>>;
213
214 static NodeRef getEntryNode(NodeRef N) { return N; }
215 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
216 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
217
218 static nodes_iterator nodes_begin(NodeRef N) {
219 return df_begin(getEntryNode(N));
220 }
221
222 static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
223};
224
225template <>
226struct GraphTraits<DomTreeNode *>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200227 : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::const_iterator> {
228};
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229
230template <>
231struct GraphTraits<const DomTreeNode *>
232 : public DomTreeGraphTraitsBase<const DomTreeNode,
233 DomTreeNode::const_iterator> {};
234
235template <> struct GraphTraits<DominatorTree*>
236 : public GraphTraits<DomTreeNode*> {
237 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
238
239 static nodes_iterator nodes_begin(DominatorTree *N) {
240 return df_begin(getEntryNode(N));
241 }
242
243 static nodes_iterator nodes_end(DominatorTree *N) {
244 return df_end(getEntryNode(N));
245 }
246};
247
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100248/// Analysis pass which computes a \c DominatorTree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100249class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
250 friend AnalysisInfoMixin<DominatorTreeAnalysis>;
251 static AnalysisKey Key;
252
253public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100254 /// Provide the result typedef for this analysis pass.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100255 using Result = DominatorTree;
256
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100257 /// Run the analysis pass over a function and produce a dominator tree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100258 DominatorTree run(Function &F, FunctionAnalysisManager &);
259};
260
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100261/// Printer pass for the \c DominatorTree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100262class DominatorTreePrinterPass
263 : public PassInfoMixin<DominatorTreePrinterPass> {
264 raw_ostream &OS;
265
266public:
267 explicit DominatorTreePrinterPass(raw_ostream &OS);
268
269 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
270};
271
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100272/// Verifier pass for the \c DominatorTree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100273struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
274 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
275};
276
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100277/// Legacy analysis pass which computes a \c DominatorTree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100278class DominatorTreeWrapperPass : public FunctionPass {
279 DominatorTree DT;
280
281public:
282 static char ID;
283
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200284 DominatorTreeWrapperPass();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100285
286 DominatorTree &getDomTree() { return DT; }
287 const DominatorTree &getDomTree() const { return DT; }
288
289 bool runOnFunction(Function &F) override;
290
291 void verifyAnalysis() const override;
292
293 void getAnalysisUsage(AnalysisUsage &AU) const override {
294 AU.setPreservesAll();
295 }
296
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200297 void releaseMemory() override { DT.reset(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298
299 void print(raw_ostream &OS, const Module *M = nullptr) const override;
300};
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100301} // end namespace llvm
302
303#endif // LLVM_IR_DOMINATORS_H