20 #define DEBUG_TYPE "dgb" 22 STATISTIC(TotalGraphs,
"Number of dependence graphs created.");
23 STATISTIC(TotalDefUseEdges,
"Number of def-use edges created.");
24 STATISTIC(TotalMemoryEdges,
"Number of memory dependence edges created.");
25 STATISTIC(TotalFineGrainedNodes,
"Number of fine-grained nodes created.");
26 STATISTIC(TotalPiBlockNodes,
"Number of pi-block nodes created.");
28 "Number of confused memory dependencies between two nodes.");
30 "Number of times the source and sink of dependence was reversed to " 31 "expose cycles in the graph.");
42 size_t NextOrdinal = 1;
43 for (
auto *BB : BBList)
45 InstOrdinalMap.insert(std::make_pair(&
I, NextOrdinal++));
51 assert(IMap.empty() &&
"Expected empty instruction map at start");
54 auto &NewNode = createFineGrainedNode(
I);
55 IMap.insert(std::make_pair(&
I, &NewNode));
56 NodeOrdinalMap.insert(std::make_pair(&NewNode, getOrdinal(
I)));
57 ++TotalFineGrainedNodes;
78 auto &RootNode = createRootNode();
80 for (
auto *
N : Graph) {
85 createRootedEdge(RootNode, *
N);
90 if (!shouldCreatePiBlocks())
118 <<
" nodes in it.\n");
123 llvm::sort(NL, [&](NodeType *LHS, NodeType *RHS) {
124 return getOrdinal(*LHS) < getOrdinal(*RHS);
127 NodeType &PiNode = createPiBlock(NL);
136 for (NodeType *
N : Graph) {
139 if (*
N == PiNode || NodesInSCC.count(
N))
142 for (NodeType *SCCNode : NL) {
158 using EdgeKind =
typename EdgeType::EdgeKind;
162 auto createEdgeOfKind = [
this](NodeType &Src, NodeType &Dst,
165 case EdgeKind::RegisterDefUse:
166 createDefUseEdge(Src, Dst);
168 case EdgeKind::MemoryDependence:
169 createMemoryEdge(Src, Dst);
171 case EdgeKind::Rooted:
172 createRootedEdge(Src, Dst);
179 auto reconnectEdges = [&](NodeType *Src, NodeType *Dst, NodeType *New,
181 if (!Src->hasEdgeTo(*Dst))
185 << (Dir == Direction::Incoming ?
"incoming)" :
"outgoing)")
186 <<
":\nSrc:" << *Src <<
"\nDst:" << *Dst
187 <<
"\nNew:" << *New <<
"\n");
188 assert((Dir == Direction::Incoming || Dir == Direction::Outgoing) &&
189 "Invalid direction.");
192 Src->findEdgesTo(*Dst, EL);
193 for (EdgeType *OldEdge : EL) {
194 EdgeKind
Kind = OldEdge->getKind();
195 if (!EdgeAlreadyCreated[Dir][Kind]) {
196 if (Dir == Direction::Incoming) {
197 createEdgeOfKind(*Src, *New, Kind);
199 }
else if (Dir == Direction::Outgoing) {
200 createEdgeOfKind(*New, *Dst, Kind);
203 EdgeAlreadyCreated[Dir][
Kind] =
true;
205 Src->removeEdge(*OldEdge);
206 destroyEdge(*OldEdge);
207 LLVM_DEBUG(
dbgs() <<
"removed old edge between Src and Dst.\n\n");
212 reconnectEdges(
N, SCCNode, &PiNode, Direction::Incoming);
215 reconnectEdges(SCCNode,
N, &PiNode, Direction::Outgoing);
221 InstOrdinalMap.clear();
222 NodeOrdinalMap.clear();
228 for (NodeType *
N : Graph) {
230 N->collectInstructions([](
const Instruction *
I) {
return true; }, SrcIList);
242 NodeType *DstNode =
nullptr;
243 if (IMap.find(UI) != IMap.end())
244 DstNode = IMap.find(UI)->second;
253 <<
"skipped def-use edge since the sink" << *UI
254 <<
" is outside the range of instructions being considered.\n");
262 <<
"skipped def-use edge since the sink and the source (" 263 <<
N <<
") are the same.\n");
267 if (VisitedTargets.
insert(DstNode).second) {
268 createDefUseEdge(*
N, *DstNode);
278 using DGIterator =
typename G::iterator;
280 return I->mayReadOrWriteMemory();
282 for (DGIterator SrcIt = Graph.begin(),
E = Graph.end(); SrcIt !=
E; ++SrcIt) {
284 (*SrcIt)->collectInstructions(isMemoryAccess, SrcIList);
285 if (SrcIList.
empty())
288 for (DGIterator DstIt = SrcIt; DstIt !=
E; ++DstIt) {
289 if (**SrcIt == **DstIt)
292 (*DstIt)->collectInstructions(isMemoryAccess, DstIList);
293 if (DstIList.
empty())
295 bool ForwardEdgeCreated =
false;
296 bool BackwardEdgeCreated =
false;
299 auto D = DI.depends(ISrc, IDst,
true);
309 auto createConfusedEdges = [&](NodeType &Src, NodeType &Dst) {
310 if (!ForwardEdgeCreated) {
311 createMemoryEdge(Src, Dst);
314 if (!BackwardEdgeCreated) {
315 createMemoryEdge(Dst, Src);
318 ForwardEdgeCreated = BackwardEdgeCreated =
true;
319 ++TotalConfusedEdges;
322 auto createForwardEdge = [&](NodeType &Src, NodeType &Dst) {
323 if (!ForwardEdgeCreated) {
324 createMemoryEdge(Src, Dst);
327 ForwardEdgeCreated =
true;
330 auto createBackwardEdge = [&](NodeType &Src, NodeType &Dst) {
331 if (!BackwardEdgeCreated) {
332 createMemoryEdge(Dst, Src);
335 BackwardEdgeCreated =
true;
339 createConfusedEdges(**SrcIt, **DstIt);
340 else if (
D->isOrdered() && !
D->isLoopIndependent()) {
341 bool ReversedEdge =
false;
346 createBackwardEdge(**SrcIt, **DstIt);
348 ++TotalEdgeReversals;
353 createConfusedEdges(**SrcIt, **DstIt);
358 createForwardEdge(**SrcIt, **DstIt);
360 createForwardEdge(**SrcIt, **DstIt);
363 if (ForwardEdgeCreated && BackwardEdgeCreated)
370 if (ForwardEdgeCreated && BackwardEdgeCreated)
381 if (!shouldCreatePiBlocks())
387 if (
N->getKind() == NodeKind::PiBlock) {
392 PiBlockMembers.
end());
397 size_t OldSize = Graph.Nodes.size();
399 for (NodeType *
N :
reverse(NodesInPO))
400 Graph.Nodes.push_back(
N);
401 if (Graph.Nodes.size() != OldSize)
403 "Expected the number of nodes to stay the same after the sort");
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
reference emplace_back(ArgTypes &&... Args)
This class represents lattice values for constants.
void push_back(const T &Elt)
STATISTIC(NumFunctions, "Total number of functions")
Direction
An enum for the direction of the loop.
void createFineGrainedNodes()
Create fine grained nodes.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
void sortNodesTopologically()
Topologically sort the graph nodes.
LLVM Basic Block Representation.
scc_iterator< T > scc_end(const T &G)
Construct the end iterator for a deduced graph type T.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
void createDefUseEdges()
Analyze the def-use chains and create edges from the nodes containing definitions to the nodes contai...
iterator_range< po_iterator< T > > post_order(const T &G)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void computeInstructionOrdinals()
Compute ordinal numbers for each instruction and store them in a map for future look up...
void sort(IteratorTy Start, IteratorTy End)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void createPiBlocks()
Apply graph abstraction to groups of nodes that belong to a strongly connected component of the graph...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Encapsulate some common data and functionality needed for different variations of data dependence gra...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
iterator_range< user_iterator > users()
iterator insert(iterator I, T &&Elt)
void createMemoryDependencyEdges()
Analyze data dependencies that exist between memory loads or stores, in the graph nodes and create ed...
LLVM_NODISCARD bool empty() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void createAndConnectRootNode()
Create a root node and add edges such that each node in the graph is reachable from the root...