18bcb0991SDimitry Andric //===- DependenceGraphBuilder.cpp ------------------------------------------==//
28bcb0991SDimitry Andric //
38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68bcb0991SDimitry Andric //
78bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
88bcb0991SDimitry Andric // This file implements common steps of the build algorithm for construction
98bcb0991SDimitry Andric // of dependence graphs such as DDG and PDG.
108bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
118bcb0991SDimitry Andric
128bcb0991SDimitry Andric #include "llvm/Analysis/DependenceGraphBuilder.h"
135ffd83dbSDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
14480093f4SDimitry Andric #include "llvm/ADT/EnumeratedArray.h"
15*81ad6265SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
168bcb0991SDimitry Andric #include "llvm/ADT/SCCIterator.h"
178bcb0991SDimitry Andric #include "llvm/ADT/Statistic.h"
188bcb0991SDimitry Andric #include "llvm/Analysis/DDG.h"
198bcb0991SDimitry Andric
208bcb0991SDimitry Andric using namespace llvm;
218bcb0991SDimitry Andric
228bcb0991SDimitry Andric #define DEBUG_TYPE "dgb"
238bcb0991SDimitry Andric
248bcb0991SDimitry Andric STATISTIC(TotalGraphs, "Number of dependence graphs created.");
258bcb0991SDimitry Andric STATISTIC(TotalDefUseEdges, "Number of def-use edges created.");
268bcb0991SDimitry Andric STATISTIC(TotalMemoryEdges, "Number of memory dependence edges created.");
278bcb0991SDimitry Andric STATISTIC(TotalFineGrainedNodes, "Number of fine-grained nodes created.");
28480093f4SDimitry Andric STATISTIC(TotalPiBlockNodes, "Number of pi-block nodes created.");
298bcb0991SDimitry Andric STATISTIC(TotalConfusedEdges,
308bcb0991SDimitry Andric "Number of confused memory dependencies between two nodes.");
318bcb0991SDimitry Andric STATISTIC(TotalEdgeReversals,
328bcb0991SDimitry Andric "Number of times the source and sink of dependence was reversed to "
338bcb0991SDimitry Andric "expose cycles in the graph.");
348bcb0991SDimitry Andric
358bcb0991SDimitry Andric using InstructionListType = SmallVector<Instruction *, 2>;
368bcb0991SDimitry Andric
378bcb0991SDimitry Andric //===--------------------------------------------------------------------===//
388bcb0991SDimitry Andric // AbstractDependenceGraphBuilder implementation
398bcb0991SDimitry Andric //===--------------------------------------------------------------------===//
408bcb0991SDimitry Andric
418bcb0991SDimitry Andric template <class G>
computeInstructionOrdinals()42480093f4SDimitry Andric void AbstractDependenceGraphBuilder<G>::computeInstructionOrdinals() {
43480093f4SDimitry Andric // The BBList is expected to be in program order.
44480093f4SDimitry Andric size_t NextOrdinal = 1;
45480093f4SDimitry Andric for (auto *BB : BBList)
46480093f4SDimitry Andric for (auto &I : *BB)
47480093f4SDimitry Andric InstOrdinalMap.insert(std::make_pair(&I, NextOrdinal++));
48480093f4SDimitry Andric }
49480093f4SDimitry Andric
50480093f4SDimitry Andric template <class G>
createFineGrainedNodes()518bcb0991SDimitry Andric void AbstractDependenceGraphBuilder<G>::createFineGrainedNodes() {
528bcb0991SDimitry Andric ++TotalGraphs;
538bcb0991SDimitry Andric assert(IMap.empty() && "Expected empty instruction map at start");
548bcb0991SDimitry Andric for (BasicBlock *BB : BBList)
558bcb0991SDimitry Andric for (Instruction &I : *BB) {
568bcb0991SDimitry Andric auto &NewNode = createFineGrainedNode(I);
578bcb0991SDimitry Andric IMap.insert(std::make_pair(&I, &NewNode));
58480093f4SDimitry Andric NodeOrdinalMap.insert(std::make_pair(&NewNode, getOrdinal(I)));
598bcb0991SDimitry Andric ++TotalFineGrainedNodes;
608bcb0991SDimitry Andric }
618bcb0991SDimitry Andric }
628bcb0991SDimitry Andric
638bcb0991SDimitry Andric template <class G>
createAndConnectRootNode()648bcb0991SDimitry Andric void AbstractDependenceGraphBuilder<G>::createAndConnectRootNode() {
658bcb0991SDimitry Andric // Create a root node that connects to every connected component of the graph.
668bcb0991SDimitry Andric // This is done to allow graph iterators to visit all the disjoint components
678bcb0991SDimitry Andric // of the graph, in a single walk.
688bcb0991SDimitry Andric //
698bcb0991SDimitry Andric // This algorithm works by going through each node of the graph and for each
708bcb0991SDimitry Andric // node N, do a DFS starting from N. A rooted edge is established between the
718bcb0991SDimitry Andric // root node and N (if N is not yet visited). All the nodes reachable from N
728bcb0991SDimitry Andric // are marked as visited and are skipped in the DFS of subsequent nodes.
738bcb0991SDimitry Andric //
748bcb0991SDimitry Andric // Note: This algorithm tries to limit the number of edges out of the root
758bcb0991SDimitry Andric // node to some extent, but there may be redundant edges created depending on
768bcb0991SDimitry Andric // the iteration order. For example for a graph {A -> B}, an edge from the
778bcb0991SDimitry Andric // root node is added to both nodes if B is visited before A. While it does
788bcb0991SDimitry Andric // not result in minimal number of edges, this approach saves compile-time
798bcb0991SDimitry Andric // while keeping the number of edges in check.
808bcb0991SDimitry Andric auto &RootNode = createRootNode();
818bcb0991SDimitry Andric df_iterator_default_set<const NodeType *, 4> Visited;
828bcb0991SDimitry Andric for (auto *N : Graph) {
838bcb0991SDimitry Andric if (*N == RootNode)
848bcb0991SDimitry Andric continue;
858bcb0991SDimitry Andric for (auto I : depth_first_ext(N, Visited))
868bcb0991SDimitry Andric if (I == N)
878bcb0991SDimitry Andric createRootedEdge(RootNode, *N);
888bcb0991SDimitry Andric }
898bcb0991SDimitry Andric }
908bcb0991SDimitry Andric
createPiBlocks()91480093f4SDimitry Andric template <class G> void AbstractDependenceGraphBuilder<G>::createPiBlocks() {
92480093f4SDimitry Andric if (!shouldCreatePiBlocks())
93480093f4SDimitry Andric return;
94480093f4SDimitry Andric
95480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "==== Start of Creation of Pi-Blocks ===\n");
96480093f4SDimitry Andric
97480093f4SDimitry Andric // The overall algorithm is as follows:
98480093f4SDimitry Andric // 1. Identify SCCs and for each SCC create a pi-block node containing all
99480093f4SDimitry Andric // the nodes in that SCC.
100480093f4SDimitry Andric // 2. Identify incoming edges incident to the nodes inside of the SCC and
101480093f4SDimitry Andric // reconnect them to the pi-block node.
102480093f4SDimitry Andric // 3. Identify outgoing edges from the nodes inside of the SCC to nodes
103480093f4SDimitry Andric // outside of it and reconnect them so that the edges are coming out of the
104480093f4SDimitry Andric // SCC node instead.
105480093f4SDimitry Andric
106480093f4SDimitry Andric // Adding nodes as we iterate through the SCCs cause the SCC
107480093f4SDimitry Andric // iterators to get invalidated. To prevent this invalidation, we first
108480093f4SDimitry Andric // collect a list of nodes that are part of an SCC, and then iterate over
109480093f4SDimitry Andric // those lists to create the pi-block nodes. Each element of the list is a
110480093f4SDimitry Andric // list of nodes in an SCC. Note: trivial SCCs containing a single node are
111480093f4SDimitry Andric // ignored.
112480093f4SDimitry Andric SmallVector<NodeListType, 4> ListOfSCCs;
113480093f4SDimitry Andric for (auto &SCC : make_range(scc_begin(&Graph), scc_end(&Graph))) {
114480093f4SDimitry Andric if (SCC.size() > 1)
115480093f4SDimitry Andric ListOfSCCs.emplace_back(SCC.begin(), SCC.end());
116480093f4SDimitry Andric }
117480093f4SDimitry Andric
118480093f4SDimitry Andric for (NodeListType &NL : ListOfSCCs) {
119480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "Creating pi-block node with " << NL.size()
120480093f4SDimitry Andric << " nodes in it.\n");
121480093f4SDimitry Andric
122480093f4SDimitry Andric // SCC iterator may put the nodes in an order that's different from the
123480093f4SDimitry Andric // program order. To preserve original program order, we sort the list of
124480093f4SDimitry Andric // nodes based on ordinal numbers computed earlier.
125480093f4SDimitry Andric llvm::sort(NL, [&](NodeType *LHS, NodeType *RHS) {
126480093f4SDimitry Andric return getOrdinal(*LHS) < getOrdinal(*RHS);
127480093f4SDimitry Andric });
128480093f4SDimitry Andric
129480093f4SDimitry Andric NodeType &PiNode = createPiBlock(NL);
130480093f4SDimitry Andric ++TotalPiBlockNodes;
131480093f4SDimitry Andric
132480093f4SDimitry Andric // Build a set to speed up the lookup for edges whose targets
133480093f4SDimitry Andric // are inside the SCC.
134480093f4SDimitry Andric SmallPtrSet<NodeType *, 4> NodesInSCC(NL.begin(), NL.end());
135480093f4SDimitry Andric
136480093f4SDimitry Andric // We have the set of nodes in the SCC. We go through the set of nodes
137480093f4SDimitry Andric // that are outside of the SCC and look for edges that cross the two sets.
138480093f4SDimitry Andric for (NodeType *N : Graph) {
139480093f4SDimitry Andric
140480093f4SDimitry Andric // Skip the SCC node and all the nodes inside of it.
141480093f4SDimitry Andric if (*N == PiNode || NodesInSCC.count(N))
142480093f4SDimitry Andric continue;
143480093f4SDimitry Andric
144480093f4SDimitry Andric enum Direction {
145480093f4SDimitry Andric Incoming, // Incoming edges to the SCC
146480093f4SDimitry Andric Outgoing, // Edges going ot of the SCC
147480093f4SDimitry Andric DirectionCount // To make the enum usable as an array index.
148480093f4SDimitry Andric };
149480093f4SDimitry Andric
150480093f4SDimitry Andric // Use these flags to help us avoid creating redundant edges. If there
151480093f4SDimitry Andric // are more than one edges from an outside node to inside nodes, we only
152480093f4SDimitry Andric // keep one edge from that node to the pi-block node. Similarly, if
153480093f4SDimitry Andric // there are more than one edges from inside nodes to an outside node,
154480093f4SDimitry Andric // we only keep one edge from the pi-block node to the outside node.
155480093f4SDimitry Andric // There is a flag defined for each direction (incoming vs outgoing) and
156480093f4SDimitry Andric // for each type of edge supported, using a two-dimensional boolean
157480093f4SDimitry Andric // array.
158480093f4SDimitry Andric using EdgeKind = typename EdgeType::EdgeKind;
159e8d8bef9SDimitry Andric EnumeratedArray<bool, EdgeKind> EdgeAlreadyCreated[DirectionCount]{false,
160e8d8bef9SDimitry Andric false};
161480093f4SDimitry Andric
162480093f4SDimitry Andric auto createEdgeOfKind = [this](NodeType &Src, NodeType &Dst,
163480093f4SDimitry Andric const EdgeKind K) {
164480093f4SDimitry Andric switch (K) {
165480093f4SDimitry Andric case EdgeKind::RegisterDefUse:
166480093f4SDimitry Andric createDefUseEdge(Src, Dst);
167480093f4SDimitry Andric break;
168480093f4SDimitry Andric case EdgeKind::MemoryDependence:
169480093f4SDimitry Andric createMemoryEdge(Src, Dst);
170480093f4SDimitry Andric break;
171480093f4SDimitry Andric case EdgeKind::Rooted:
172480093f4SDimitry Andric createRootedEdge(Src, Dst);
173480093f4SDimitry Andric break;
174480093f4SDimitry Andric default:
175480093f4SDimitry Andric llvm_unreachable("Unsupported type of edge.");
176480093f4SDimitry Andric }
177480093f4SDimitry Andric };
178480093f4SDimitry Andric
179480093f4SDimitry Andric auto reconnectEdges = [&](NodeType *Src, NodeType *Dst, NodeType *New,
180480093f4SDimitry Andric const Direction Dir) {
181480093f4SDimitry Andric if (!Src->hasEdgeTo(*Dst))
182480093f4SDimitry Andric return;
183e8d8bef9SDimitry Andric LLVM_DEBUG(
184e8d8bef9SDimitry Andric dbgs() << "reconnecting("
185480093f4SDimitry Andric << (Dir == Direction::Incoming ? "incoming)" : "outgoing)")
186e8d8bef9SDimitry Andric << ":\nSrc:" << *Src << "\nDst:" << *Dst << "\nNew:" << *New
187e8d8bef9SDimitry Andric << "\n");
188480093f4SDimitry Andric assert((Dir == Direction::Incoming || Dir == Direction::Outgoing) &&
189480093f4SDimitry Andric "Invalid direction.");
190480093f4SDimitry Andric
191480093f4SDimitry Andric SmallVector<EdgeType *, 10> EL;
192480093f4SDimitry Andric Src->findEdgesTo(*Dst, EL);
193480093f4SDimitry Andric for (EdgeType *OldEdge : EL) {
194480093f4SDimitry Andric EdgeKind Kind = OldEdge->getKind();
195480093f4SDimitry Andric if (!EdgeAlreadyCreated[Dir][Kind]) {
196480093f4SDimitry Andric if (Dir == Direction::Incoming) {
197480093f4SDimitry Andric createEdgeOfKind(*Src, *New, Kind);
198480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "created edge from Src to New.\n");
199480093f4SDimitry Andric } else if (Dir == Direction::Outgoing) {
200480093f4SDimitry Andric createEdgeOfKind(*New, *Dst, Kind);
201480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "created edge from New to Dst.\n");
202480093f4SDimitry Andric }
203480093f4SDimitry Andric EdgeAlreadyCreated[Dir][Kind] = true;
204480093f4SDimitry Andric }
205480093f4SDimitry Andric Src->removeEdge(*OldEdge);
206480093f4SDimitry Andric destroyEdge(*OldEdge);
207480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "removed old edge between Src and Dst.\n\n");
208480093f4SDimitry Andric }
209480093f4SDimitry Andric };
210480093f4SDimitry Andric
211e8d8bef9SDimitry Andric for (NodeType *SCCNode : NL) {
212480093f4SDimitry Andric // Process incoming edges incident to the pi-block node.
213480093f4SDimitry Andric reconnectEdges(N, SCCNode, &PiNode, Direction::Incoming);
214480093f4SDimitry Andric
215480093f4SDimitry Andric // Process edges that are coming out of the pi-block node.
216480093f4SDimitry Andric reconnectEdges(SCCNode, N, &PiNode, Direction::Outgoing);
217480093f4SDimitry Andric }
218480093f4SDimitry Andric }
219480093f4SDimitry Andric }
220480093f4SDimitry Andric
221480093f4SDimitry Andric // Ordinal maps are no longer needed.
222480093f4SDimitry Andric InstOrdinalMap.clear();
223480093f4SDimitry Andric NodeOrdinalMap.clear();
224480093f4SDimitry Andric
225480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "==== End of Creation of Pi-Blocks ===\n");
226480093f4SDimitry Andric }
227480093f4SDimitry Andric
createDefUseEdges()2288bcb0991SDimitry Andric template <class G> void AbstractDependenceGraphBuilder<G>::createDefUseEdges() {
2298bcb0991SDimitry Andric for (NodeType *N : Graph) {
2308bcb0991SDimitry Andric InstructionListType SrcIList;
2318bcb0991SDimitry Andric N->collectInstructions([](const Instruction *I) { return true; }, SrcIList);
2328bcb0991SDimitry Andric
2338bcb0991SDimitry Andric // Use a set to mark the targets that we link to N, so we don't add
2348bcb0991SDimitry Andric // duplicate def-use edges when more than one instruction in a target node
2358bcb0991SDimitry Andric // use results of instructions that are contained in N.
2368bcb0991SDimitry Andric SmallPtrSet<NodeType *, 4> VisitedTargets;
2378bcb0991SDimitry Andric
2388bcb0991SDimitry Andric for (Instruction *II : SrcIList) {
2398bcb0991SDimitry Andric for (User *U : II->users()) {
2408bcb0991SDimitry Andric Instruction *UI = dyn_cast<Instruction>(U);
2418bcb0991SDimitry Andric if (!UI)
2428bcb0991SDimitry Andric continue;
2438bcb0991SDimitry Andric NodeType *DstNode = nullptr;
2448bcb0991SDimitry Andric if (IMap.find(UI) != IMap.end())
2458bcb0991SDimitry Andric DstNode = IMap.find(UI)->second;
2468bcb0991SDimitry Andric
2478bcb0991SDimitry Andric // In the case of loops, the scope of the subgraph is all the
2488bcb0991SDimitry Andric // basic blocks (and instructions within them) belonging to the loop. We
2498bcb0991SDimitry Andric // simply ignore all the edges coming from (or going into) instructions
2508bcb0991SDimitry Andric // or basic blocks outside of this range.
2518bcb0991SDimitry Andric if (!DstNode) {
2528bcb0991SDimitry Andric LLVM_DEBUG(
2538bcb0991SDimitry Andric dbgs()
2548bcb0991SDimitry Andric << "skipped def-use edge since the sink" << *UI
2558bcb0991SDimitry Andric << " is outside the range of instructions being considered.\n");
2568bcb0991SDimitry Andric continue;
2578bcb0991SDimitry Andric }
2588bcb0991SDimitry Andric
2598bcb0991SDimitry Andric // Self dependencies are ignored because they are redundant and
2608bcb0991SDimitry Andric // uninteresting.
2618bcb0991SDimitry Andric if (DstNode == N) {
2628bcb0991SDimitry Andric LLVM_DEBUG(dbgs()
2638bcb0991SDimitry Andric << "skipped def-use edge since the sink and the source ("
2648bcb0991SDimitry Andric << N << ") are the same.\n");
2658bcb0991SDimitry Andric continue;
2668bcb0991SDimitry Andric }
2678bcb0991SDimitry Andric
2688bcb0991SDimitry Andric if (VisitedTargets.insert(DstNode).second) {
2698bcb0991SDimitry Andric createDefUseEdge(*N, *DstNode);
2708bcb0991SDimitry Andric ++TotalDefUseEdges;
2718bcb0991SDimitry Andric }
2728bcb0991SDimitry Andric }
2738bcb0991SDimitry Andric }
2748bcb0991SDimitry Andric }
2758bcb0991SDimitry Andric }
2768bcb0991SDimitry Andric
2778bcb0991SDimitry Andric template <class G>
createMemoryDependencyEdges()2788bcb0991SDimitry Andric void AbstractDependenceGraphBuilder<G>::createMemoryDependencyEdges() {
2798bcb0991SDimitry Andric using DGIterator = typename G::iterator;
2808bcb0991SDimitry Andric auto isMemoryAccess = [](const Instruction *I) {
2818bcb0991SDimitry Andric return I->mayReadOrWriteMemory();
2828bcb0991SDimitry Andric };
2838bcb0991SDimitry Andric for (DGIterator SrcIt = Graph.begin(), E = Graph.end(); SrcIt != E; ++SrcIt) {
2848bcb0991SDimitry Andric InstructionListType SrcIList;
2858bcb0991SDimitry Andric (*SrcIt)->collectInstructions(isMemoryAccess, SrcIList);
2868bcb0991SDimitry Andric if (SrcIList.empty())
2878bcb0991SDimitry Andric continue;
2888bcb0991SDimitry Andric
2898bcb0991SDimitry Andric for (DGIterator DstIt = SrcIt; DstIt != E; ++DstIt) {
2908bcb0991SDimitry Andric if (**SrcIt == **DstIt)
2918bcb0991SDimitry Andric continue;
2928bcb0991SDimitry Andric InstructionListType DstIList;
2938bcb0991SDimitry Andric (*DstIt)->collectInstructions(isMemoryAccess, DstIList);
2948bcb0991SDimitry Andric if (DstIList.empty())
2958bcb0991SDimitry Andric continue;
2968bcb0991SDimitry Andric bool ForwardEdgeCreated = false;
2978bcb0991SDimitry Andric bool BackwardEdgeCreated = false;
2988bcb0991SDimitry Andric for (Instruction *ISrc : SrcIList) {
2998bcb0991SDimitry Andric for (Instruction *IDst : DstIList) {
3008bcb0991SDimitry Andric auto D = DI.depends(ISrc, IDst, true);
3018bcb0991SDimitry Andric if (!D)
3028bcb0991SDimitry Andric continue;
3038bcb0991SDimitry Andric
3048bcb0991SDimitry Andric // If we have a dependence with its left-most non-'=' direction
3058bcb0991SDimitry Andric // being '>' we need to reverse the direction of the edge, because
3068bcb0991SDimitry Andric // the source of the dependence cannot occur after the sink. For
3078bcb0991SDimitry Andric // confused dependencies, we will create edges in both directions to
3088bcb0991SDimitry Andric // represent the possibility of a cycle.
3098bcb0991SDimitry Andric
3108bcb0991SDimitry Andric auto createConfusedEdges = [&](NodeType &Src, NodeType &Dst) {
3118bcb0991SDimitry Andric if (!ForwardEdgeCreated) {
3128bcb0991SDimitry Andric createMemoryEdge(Src, Dst);
3138bcb0991SDimitry Andric ++TotalMemoryEdges;
3148bcb0991SDimitry Andric }
3158bcb0991SDimitry Andric if (!BackwardEdgeCreated) {
3168bcb0991SDimitry Andric createMemoryEdge(Dst, Src);
3178bcb0991SDimitry Andric ++TotalMemoryEdges;
3188bcb0991SDimitry Andric }
3198bcb0991SDimitry Andric ForwardEdgeCreated = BackwardEdgeCreated = true;
3208bcb0991SDimitry Andric ++TotalConfusedEdges;
3218bcb0991SDimitry Andric };
3228bcb0991SDimitry Andric
3238bcb0991SDimitry Andric auto createForwardEdge = [&](NodeType &Src, NodeType &Dst) {
3248bcb0991SDimitry Andric if (!ForwardEdgeCreated) {
3258bcb0991SDimitry Andric createMemoryEdge(Src, Dst);
3268bcb0991SDimitry Andric ++TotalMemoryEdges;
3278bcb0991SDimitry Andric }
3288bcb0991SDimitry Andric ForwardEdgeCreated = true;
3298bcb0991SDimitry Andric };
3308bcb0991SDimitry Andric
3318bcb0991SDimitry Andric auto createBackwardEdge = [&](NodeType &Src, NodeType &Dst) {
3328bcb0991SDimitry Andric if (!BackwardEdgeCreated) {
3338bcb0991SDimitry Andric createMemoryEdge(Dst, Src);
3348bcb0991SDimitry Andric ++TotalMemoryEdges;
3358bcb0991SDimitry Andric }
3368bcb0991SDimitry Andric BackwardEdgeCreated = true;
3378bcb0991SDimitry Andric };
3388bcb0991SDimitry Andric
3398bcb0991SDimitry Andric if (D->isConfused())
3408bcb0991SDimitry Andric createConfusedEdges(**SrcIt, **DstIt);
3418bcb0991SDimitry Andric else if (D->isOrdered() && !D->isLoopIndependent()) {
3428bcb0991SDimitry Andric bool ReversedEdge = false;
3438bcb0991SDimitry Andric for (unsigned Level = 1; Level <= D->getLevels(); ++Level) {
3448bcb0991SDimitry Andric if (D->getDirection(Level) == Dependence::DVEntry::EQ)
3458bcb0991SDimitry Andric continue;
3468bcb0991SDimitry Andric else if (D->getDirection(Level) == Dependence::DVEntry::GT) {
3478bcb0991SDimitry Andric createBackwardEdge(**SrcIt, **DstIt);
3488bcb0991SDimitry Andric ReversedEdge = true;
3498bcb0991SDimitry Andric ++TotalEdgeReversals;
3508bcb0991SDimitry Andric break;
3518bcb0991SDimitry Andric } else if (D->getDirection(Level) == Dependence::DVEntry::LT)
3528bcb0991SDimitry Andric break;
3538bcb0991SDimitry Andric else {
3548bcb0991SDimitry Andric createConfusedEdges(**SrcIt, **DstIt);
3558bcb0991SDimitry Andric break;
3568bcb0991SDimitry Andric }
3578bcb0991SDimitry Andric }
3588bcb0991SDimitry Andric if (!ReversedEdge)
3598bcb0991SDimitry Andric createForwardEdge(**SrcIt, **DstIt);
3608bcb0991SDimitry Andric } else
3618bcb0991SDimitry Andric createForwardEdge(**SrcIt, **DstIt);
3628bcb0991SDimitry Andric
3638bcb0991SDimitry Andric // Avoid creating duplicate edges.
3648bcb0991SDimitry Andric if (ForwardEdgeCreated && BackwardEdgeCreated)
3658bcb0991SDimitry Andric break;
3668bcb0991SDimitry Andric }
3678bcb0991SDimitry Andric
3688bcb0991SDimitry Andric // If we've created edges in both directions, there is no more
3698bcb0991SDimitry Andric // unique edge that we can create between these two nodes, so we
3708bcb0991SDimitry Andric // can exit early.
3718bcb0991SDimitry Andric if (ForwardEdgeCreated && BackwardEdgeCreated)
3728bcb0991SDimitry Andric break;
3738bcb0991SDimitry Andric }
3748bcb0991SDimitry Andric }
3758bcb0991SDimitry Andric }
3768bcb0991SDimitry Andric }
3778bcb0991SDimitry Andric
simplify()3785ffd83dbSDimitry Andric template <class G> void AbstractDependenceGraphBuilder<G>::simplify() {
3795ffd83dbSDimitry Andric if (!shouldSimplify())
3805ffd83dbSDimitry Andric return;
3815ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "==== Start of Graph Simplification ===\n");
3825ffd83dbSDimitry Andric
3835ffd83dbSDimitry Andric // This algorithm works by first collecting a set of candidate nodes that have
3845ffd83dbSDimitry Andric // an out-degree of one (in terms of def-use edges), and then ignoring those
3855ffd83dbSDimitry Andric // whose targets have an in-degree more than one. Each node in the resulting
3865ffd83dbSDimitry Andric // set can then be merged with its corresponding target and put back into the
3875ffd83dbSDimitry Andric // worklist until no further merge candidates are available.
3885ffd83dbSDimitry Andric SmallPtrSet<NodeType *, 32> CandidateSourceNodes;
3895ffd83dbSDimitry Andric
3905ffd83dbSDimitry Andric // A mapping between nodes and their in-degree. To save space, this map
3915ffd83dbSDimitry Andric // only contains nodes that are targets of nodes in the CandidateSourceNodes.
3925ffd83dbSDimitry Andric DenseMap<NodeType *, unsigned> TargetInDegreeMap;
3935ffd83dbSDimitry Andric
3945ffd83dbSDimitry Andric for (NodeType *N : Graph) {
3955ffd83dbSDimitry Andric if (N->getEdges().size() != 1)
3965ffd83dbSDimitry Andric continue;
3975ffd83dbSDimitry Andric EdgeType &Edge = N->back();
3985ffd83dbSDimitry Andric if (!Edge.isDefUse())
3995ffd83dbSDimitry Andric continue;
4005ffd83dbSDimitry Andric CandidateSourceNodes.insert(N);
4015ffd83dbSDimitry Andric
4025ffd83dbSDimitry Andric // Insert an element into the in-degree map and initialize to zero. The
4035ffd83dbSDimitry Andric // count will get updated in the next step.
4045ffd83dbSDimitry Andric TargetInDegreeMap.insert({&Edge.getTargetNode(), 0});
4055ffd83dbSDimitry Andric }
4065ffd83dbSDimitry Andric
4075ffd83dbSDimitry Andric LLVM_DEBUG({
4085ffd83dbSDimitry Andric dbgs() << "Size of candidate src node list:" << CandidateSourceNodes.size()
4095ffd83dbSDimitry Andric << "\nNode with single outgoing def-use edge:\n";
4105ffd83dbSDimitry Andric for (NodeType *N : CandidateSourceNodes) {
4115ffd83dbSDimitry Andric dbgs() << N << "\n";
4125ffd83dbSDimitry Andric }
4135ffd83dbSDimitry Andric });
4145ffd83dbSDimitry Andric
4155ffd83dbSDimitry Andric for (NodeType *N : Graph) {
4165ffd83dbSDimitry Andric for (EdgeType *E : *N) {
4175ffd83dbSDimitry Andric NodeType *Tgt = &E->getTargetNode();
4185ffd83dbSDimitry Andric auto TgtIT = TargetInDegreeMap.find(Tgt);
4195ffd83dbSDimitry Andric if (TgtIT != TargetInDegreeMap.end())
4205ffd83dbSDimitry Andric ++(TgtIT->second);
4215ffd83dbSDimitry Andric }
4225ffd83dbSDimitry Andric }
4235ffd83dbSDimitry Andric
4245ffd83dbSDimitry Andric LLVM_DEBUG({
4255ffd83dbSDimitry Andric dbgs() << "Size of target in-degree map:" << TargetInDegreeMap.size()
4265ffd83dbSDimitry Andric << "\nContent of in-degree map:\n";
4275ffd83dbSDimitry Andric for (auto &I : TargetInDegreeMap) {
4285ffd83dbSDimitry Andric dbgs() << I.first << " --> " << I.second << "\n";
4295ffd83dbSDimitry Andric }
4305ffd83dbSDimitry Andric });
4315ffd83dbSDimitry Andric
4325ffd83dbSDimitry Andric SmallVector<NodeType *, 32> Worklist(CandidateSourceNodes.begin(),
4335ffd83dbSDimitry Andric CandidateSourceNodes.end());
4345ffd83dbSDimitry Andric while (!Worklist.empty()) {
4355ffd83dbSDimitry Andric NodeType &Src = *Worklist.pop_back_val();
4365ffd83dbSDimitry Andric // As nodes get merged, we need to skip any node that has been removed from
4375ffd83dbSDimitry Andric // the candidate set (see below).
4385ffd83dbSDimitry Andric if (!CandidateSourceNodes.erase(&Src))
4395ffd83dbSDimitry Andric continue;
4405ffd83dbSDimitry Andric
4415ffd83dbSDimitry Andric assert(Src.getEdges().size() == 1 &&
4425ffd83dbSDimitry Andric "Expected a single edge from the candidate src node.");
4435ffd83dbSDimitry Andric NodeType &Tgt = Src.back().getTargetNode();
4445ffd83dbSDimitry Andric assert(TargetInDegreeMap.find(&Tgt) != TargetInDegreeMap.end() &&
4455ffd83dbSDimitry Andric "Expected target to be in the in-degree map.");
4465ffd83dbSDimitry Andric
4475ffd83dbSDimitry Andric if (TargetInDegreeMap[&Tgt] != 1)
4485ffd83dbSDimitry Andric continue;
4495ffd83dbSDimitry Andric
4505ffd83dbSDimitry Andric if (!areNodesMergeable(Src, Tgt))
4515ffd83dbSDimitry Andric continue;
4525ffd83dbSDimitry Andric
4535ffd83dbSDimitry Andric // Do not merge if there is also an edge from target to src (immediate
4545ffd83dbSDimitry Andric // cycle).
4555ffd83dbSDimitry Andric if (Tgt.hasEdgeTo(Src))
4565ffd83dbSDimitry Andric continue;
4575ffd83dbSDimitry Andric
4585ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Merging:" << Src << "\nWith:" << Tgt << "\n");
4595ffd83dbSDimitry Andric
4605ffd83dbSDimitry Andric mergeNodes(Src, Tgt);
4615ffd83dbSDimitry Andric
4625ffd83dbSDimitry Andric // If the target node is in the candidate set itself, we need to put the
4635ffd83dbSDimitry Andric // src node back into the worklist again so it gives the target a chance
4645ffd83dbSDimitry Andric // to get merged into it. For example if we have:
4655ffd83dbSDimitry Andric // {(a)->(b), (b)->(c), (c)->(d), ...} and the worklist is initially {b, a},
4665ffd83dbSDimitry Andric // then after merging (a) and (b) together, we need to put (a,b) back in
4675ffd83dbSDimitry Andric // the worklist so that (c) can get merged in as well resulting in
4685ffd83dbSDimitry Andric // {(a,b,c) -> d}
4695ffd83dbSDimitry Andric // We also need to remove the old target (b), from the worklist. We first
4705ffd83dbSDimitry Andric // remove it from the candidate set here, and skip any item from the
4715ffd83dbSDimitry Andric // worklist that is not in the set.
4725ffd83dbSDimitry Andric if (CandidateSourceNodes.erase(&Tgt)) {
4735ffd83dbSDimitry Andric Worklist.push_back(&Src);
4745ffd83dbSDimitry Andric CandidateSourceNodes.insert(&Src);
4755ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Putting " << &Src << " back in the worklist.\n");
4765ffd83dbSDimitry Andric }
4775ffd83dbSDimitry Andric }
4785ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "=== End of Graph Simplification ===\n");
4795ffd83dbSDimitry Andric }
4805ffd83dbSDimitry Andric
481480093f4SDimitry Andric template <class G>
sortNodesTopologically()482480093f4SDimitry Andric void AbstractDependenceGraphBuilder<G>::sortNodesTopologically() {
483480093f4SDimitry Andric
484480093f4SDimitry Andric // If we don't create pi-blocks, then we may not have a DAG.
485480093f4SDimitry Andric if (!shouldCreatePiBlocks())
486480093f4SDimitry Andric return;
487480093f4SDimitry Andric
488480093f4SDimitry Andric SmallVector<NodeType *, 64> NodesInPO;
489480093f4SDimitry Andric using NodeKind = typename NodeType::NodeKind;
490480093f4SDimitry Andric for (NodeType *N : post_order(&Graph)) {
491480093f4SDimitry Andric if (N->getKind() == NodeKind::PiBlock) {
492480093f4SDimitry Andric // Put members of the pi-block right after the pi-block itself, for
493480093f4SDimitry Andric // convenience.
494480093f4SDimitry Andric const NodeListType &PiBlockMembers = getNodesInPiBlock(*N);
495e8d8bef9SDimitry Andric llvm::append_range(NodesInPO, PiBlockMembers);
496480093f4SDimitry Andric }
497480093f4SDimitry Andric NodesInPO.push_back(N);
498480093f4SDimitry Andric }
499480093f4SDimitry Andric
500480093f4SDimitry Andric size_t OldSize = Graph.Nodes.size();
501480093f4SDimitry Andric Graph.Nodes.clear();
502e8d8bef9SDimitry Andric append_range(Graph.Nodes, reverse(NodesInPO));
503480093f4SDimitry Andric if (Graph.Nodes.size() != OldSize)
504480093f4SDimitry Andric assert(false &&
505480093f4SDimitry Andric "Expected the number of nodes to stay the same after the sort");
506480093f4SDimitry Andric }
507480093f4SDimitry Andric
5088bcb0991SDimitry Andric template class llvm::AbstractDependenceGraphBuilder<DataDependenceGraph>;
5098bcb0991SDimitry Andric template class llvm::DependenceGraphInfo<DDGNode>;
510