LLVM 19.0.0git
DominanceFrontierImpl.h
Go to the documentation of this file.
1//===- llvm/Analysis/DominanceFrontier.h - Dominator Frontiers --*- 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 is the generic implementation of the DominanceFrontier class, which
10// calculate and holds the dominance frontier for a function for.
11//
12// This should be considered deprecated, don't add any more uses of this data
13// structure.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
18#define LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
19
22#include "llvm/Config/llvm-config.h"
23#include "llvm/Support/Debug.h"
26#include <cassert>
27#include <set>
28#include <utility>
29#include <vector>
30
31namespace llvm {
32
33template <class BlockT>
35public:
37
38 DFCalculateWorkObject(BlockT *B, BlockT *P, const DomTreeNodeT *N,
39 const DomTreeNodeT *PN)
40 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
41
42 BlockT *currentBB;
43 BlockT *parentBB;
46};
47
48template <class BlockT, bool IsPostDom>
50 assert(find(BB) != end() && "Block is not in DominanceFrontier!");
51 for (iterator I = begin(), E = end(); I != E; ++I)
52 I->second.remove(BB);
53 Frontiers.erase(BB);
54}
55
56template <class BlockT, bool IsPostDom>
58 BlockT *Node) {
59 assert(I != end() && "BB is not in DominanceFrontier!");
60 I->second.insert(Node);
61}
62
63template <class BlockT, bool IsPostDom>
65 iterator I, BlockT *Node) {
66 assert(I != end() && "BB is not in DominanceFrontier!");
67 assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
68 I->second.remove(Node);
69}
70
71template <class BlockT, bool IsPostDom>
73 DomSetType &DS1, const DomSetType &DS2) const {
74 std::set<BlockT *> tmpSet;
75 for (BlockT *BB : DS2)
76 tmpSet.insert(BB);
77
78 for (typename DomSetType::const_iterator I = DS1.begin(), E = DS1.end();
79 I != E;) {
80 BlockT *Node = *I++;
81
82 if (tmpSet.erase(Node) == 0)
83 // Node is in DS1 but tnot in DS2.
84 return true;
85 }
86
87 if (!tmpSet.empty()) {
88 // There are nodes that are in DS2 but not in DS1.
89 return true;
90 }
91
92 // DS1 and DS2 matches.
93 return false;
95
96template <class BlockT, bool IsPostDom>
99 DomSetMapType tmpFrontiers;
100 for (typename DomSetMapType::const_iterator I = Other.begin(),
101 E = Other.end();
102 I != E; ++I)
103 tmpFrontiers.insert(std::make_pair(I->first, I->second));
104
105 for (typename DomSetMapType::iterator I = tmpFrontiers.begin(),
106 E = tmpFrontiers.end();
107 I != E;) {
108 BlockT *Node = I->first;
109 const_iterator DFI = find(Node);
110 if (DFI == end())
111 return true;
112
113 if (compareDomSet(I->second, DFI->second))
114 return true;
115
116 ++I;
117 tmpFrontiers.erase(Node);
118 }
119
120 if (!tmpFrontiers.empty())
121 return true;
122
123 return false;
124}
125
126template <class BlockT, bool IsPostDom>
128 for (const_iterator I = begin(), E = end(); I != E; ++I) {
129 OS << " DomFrontier for BB ";
130 if (I->first)
131 I->first->printAsOperand(OS, false);
132 else
133 OS << " <<exit node>>";
134 OS << " is:\t";
135
136 const SetVector<BlockT *> &BBs = I->second;
137
138 for (const BlockT *BB : BBs) {
139 OS << ' ';
140 if (BB)
141 BB->printAsOperand(OS, false);
142 else
143 OS << "<<exit node>>";
144 }
145 OS << '\n';
146 }
147}
148
149#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
150template <class BlockT, bool IsPostDom>
152 print(dbgs());
153}
154#endif
155
156template <class BlockT>
159 const DomTreeNodeT *Node) {
160 BlockT *BB = Node->getBlock();
161 DomSetType *Result = nullptr;
162
163 std::vector<DFCalculateWorkObject<BlockT>> workList;
165
166 workList.push_back(DFCalculateWorkObject<BlockT>(BB, nullptr, Node, nullptr));
167 do {
168 DFCalculateWorkObject<BlockT> *currentW = &workList.back();
169 assert(currentW && "Missing work object.");
170
171 BlockT *currentBB = currentW->currentBB;
172 BlockT *parentBB = currentW->parentBB;
173 const DomTreeNodeT *currentNode = currentW->Node;
174 const DomTreeNodeT *parentNode = currentW->parentNode;
175 assert(currentBB && "Invalid work object. Missing current Basic Block");
176 assert(currentNode && "Invalid work object. Missing current Node");
177 DomSetType &S = this->Frontiers[currentBB];
178
179 // Visit each block only once.
180 if (visited.insert(currentBB).second) {
181 // Loop over CFG successors to calculate DFlocal[currentNode]
182 for (const auto Succ : children<BlockT *>(currentBB)) {
183 // Does Node immediately dominate this successor?
184 if (DT[Succ]->getIDom() != currentNode)
185 S.insert(Succ);
186 }
187 }
188
189 // At this point, S is DFlocal. Now we union in DFup's of our children...
190 // Loop through and visit the nodes that Node immediately dominates (Node's
191 // children in the IDomTree)
192 bool visitChild = false;
193 for (typename DomTreeNodeT::const_iterator NI = currentNode->begin(),
194 NE = currentNode->end();
195 NI != NE; ++NI) {
196 DomTreeNodeT *IDominee = *NI;
197 BlockT *childBB = IDominee->getBlock();
198 if (visited.count(childBB) == 0) {
199 workList.push_back(DFCalculateWorkObject<BlockT>(
200 childBB, currentBB, IDominee, currentNode));
201 visitChild = true;
202 }
203 }
204
205 // If all children are visited or there is any child then pop this block
206 // from the workList.
207 if (!visitChild) {
208 if (!parentBB) {
209 Result = &S;
210 break;
211 }
212
213 typename DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
214 DomSetType &parentSet = this->Frontiers[parentBB];
215 for (; CDFI != CDFE; ++CDFI) {
216 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
217 parentSet.insert(*CDFI);
218 }
219 workList.pop_back();
220 }
221
222 } while (!workList.empty());
223
224 return *Result;
225}
226
227} // end namespace llvm
228
229#endif // LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
DFCalculateWorkObject(BlockT *B, BlockT *P, const DomTreeNodeT *N, const DomTreeNodeT *PN)
bool erase(const KeyT &Val)
Definition: DenseMap.h:329
bool empty() const
Definition: DenseMap.h:98
iterator begin()
Definition: DenseMap.h:75
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Base class for the actual dominator tree node.
NodeT * getBlock() const
typename SmallVector< DomTreeNodeBase *, 4 >::const_iterator const_iterator
DominanceFrontierBase - Common base class for computing forward and inverse dominance frontiers for a...
bool compare(DominanceFrontierBase &Other) const
compare - Return true if the other dominance frontier base matches this dominance frontier base.
void print(raw_ostream &OS) const
print - Convert to human readable form
typename DomSetMapType::iterator iterator
bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const
compareDomSet - Return false if two domsets match.
void removeFromFrontier(iterator I, BlockT *Node)
void dump() const
dump - Dump the dominance frontier to dbgs().
typename DomSetMapType::const_iterator const_iterator
void addToFrontier(iterator I, BlockT *Node)
void removeBlock(BlockT *BB)
removeBlock - Remove basic block BB's frontier.
Core dominator tree base class.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
const DomSetType & calculate(const DomTreeT &DT, const DomTreeNodeT *Node)
typename DominanceFrontierBase< BlockT, false >::DomSetType DomSetType
A vector that has set insertion semantics.
Definition: SetVector.h:57
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:113
typename vector_type::const_iterator const_iterator
Definition: SetVector.h:70
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:103
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:360
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.
#define N