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