LLVM 23.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 <vector>
28
29namespace llvm {
30
31template <class BlockT>
33public:
35
36 DFCalculateWorkObject(BlockT *B, BlockT *P, const DomTreeNodeT *N,
37 const DomTreeNodeT *PN)
38 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
39
40 BlockT *currentBB;
41 BlockT *parentBB;
44};
45
46template <class BlockT, bool IsPostDom>
48 for (const_iterator I = begin(), E = end(); I != E; ++I) {
49 OS << " DomFrontier for BB ";
50 if (I->first)
51 I->first->printAsOperand(OS, false);
52 else
53 OS << " <<exit node>>";
54 OS << " is:\t";
55
56 const SetVector<BlockT *> &BBs = I->second;
57
58 for (const BlockT *BB : BBs) {
59 OS << ' ';
60 if (BB)
61 BB->printAsOperand(OS, false);
62 else
63 OS << "<<exit node>>";
64 }
65 OS << '\n';
66 }
67}
68
69#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
70template <class BlockT, bool IsPostDom>
74#endif
75
76template <class BlockT, bool IsPostDom>
78 // NOTE: RootNode might be virtual for `IsPostDom == true`.
79 const DomTreeNodeT *RootNode = DT.getRootNode();
80 assert(IsPostDom ||
81 (DT.root_size() == 1 && RootNode->getBlock() == *DT.root_begin()) &&
82 "Multiple roots for forward dominators?");
83 BlockT *BB = RootNode->getBlock();
85 std::vector<DFCalculateWorkObject<BlockT>> workList;
87
88 workList.push_back(
89 DFCalculateWorkObject<BlockT>(BB, nullptr, RootNode, nullptr));
90 do {
91 DFCalculateWorkObject<BlockT> *currentW = &workList.back();
92 assert(currentW && "Missing work object.");
93
94 BlockT *currentBB = currentW->currentBB;
95 BlockT *parentBB = currentW->parentBB;
96 const DomTreeNodeT *currentNode = currentW->Node;
97 const DomTreeNodeT *parentNode = currentW->parentNode;
98 assert(currentNode && "Invalid work object. Missing current Node");
99 assert((currentBB || DT.isVirtualRoot(currentNode)) &&
100 "Invalid work object. Missing current Basic Block");
101
102 // Note that for `IsPostDom == true`, the virtual root node (null currentBB)
103 // is an immediate post-dominator for all the exit nodes (which are
104 // virtual node's CFG successors).
105
106 // Visit each block only once.
107 if (currentBB && visited.insert(currentBB).second) {
108 // Loop over CFG successors to calculate DFlocal[currentNode].
109 DomSetType &S = this->Frontiers[currentBB];
110 for (const auto Child : children<GraphTy>(currentBB)) {
111 // Does Node immediately dominate this successor?
112 if (DT[Child]->getIDom() != currentNode)
113 S.insert(Child);
114 }
115 }
116
117 // At this point, S is DFlocal. Now we union in DFup's of our children...
118 // Loop through and visit the nodes that Node immediately dominates (Node's
119 // children in the IDomTree)
120 bool visitChild = false;
121 for (typename DomTreeNodeT::const_iterator NI = currentNode->begin(),
122 NE = currentNode->end();
123 NI != NE; ++NI) {
124 DomTreeNodeT *IDominee = *NI;
125 BlockT *childBB = IDominee->getBlock();
126 if (visited.count(childBB) == 0) {
127 workList.push_back(DFCalculateWorkObject<BlockT>(
128 childBB, currentBB, IDominee, currentNode));
129 visitChild = true;
130 }
131 }
132
133 // If all children are visited or there is any child then pop this block
134 // from the workList.
135 if (!visitChild) {
136 if (RootNode == currentNode) {
137 break;
138 }
139
140 workList.pop_back();
141 if (!parentBB) {
142 assert(IsPostDom && "For forward frontiers only root node (processed "
143 "above) might not have a parent.");
144 // Processing below isn't necessary for the virtual root node in case
145 // of post-dominance frontier.
146 continue;
147 }
148
149 DomSetType &S = this->Frontiers[currentBB];
150 typename DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
151 DomSetType &parentSet = this->Frontiers[parentBB];
152 for (; CDFI != CDFE; ++CDFI) {
153 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
154 parentSet.insert(*CDFI);
155 }
156 }
157
158 } while (!workList.empty());
159}
160
161} // end namespace llvm
162
163#endif // LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-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:57
#define P(N)
This file defines the SmallPtrSet class.
DFCalculateWorkObject(BlockT *B, BlockT *P, const DomTreeNodeT *N, const DomTreeNodeT *PN)
DomTreeNodeBase< BlockT > DomTreeNodeT
Base class for the actual dominator tree node.
iterator begin() const
NodeT * getBlock() const
iterator end() const
void analyze(const DomTreeT &DT)
DominatorTreeBase< BlockT, IsPostDom > DomTreeT
void print(raw_ostream &OS) const
print - Convert to human readable form
void dump() const
dump - Dump the dominance frontier to dbgs().
DomTreeNodeBase< BlockT > DomTreeNodeT
typename DomSetMapType::const_iterator const_iterator
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
root_iterator root_begin()
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
bool isVirtualRoot(const DomTreeNodeBase< NodeT > *A) const
A vector that has set insertion semantics.
Definition SetVector.h:57
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:106
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
#define N