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" 13*480093f4SDimitry Andric #include "llvm/ADT/EnumeratedArray.h" 148bcb0991SDimitry Andric #include "llvm/ADT/SCCIterator.h" 158bcb0991SDimitry Andric #include "llvm/ADT/Statistic.h" 168bcb0991SDimitry Andric #include "llvm/Analysis/DDG.h" 178bcb0991SDimitry Andric 188bcb0991SDimitry Andric using namespace llvm; 198bcb0991SDimitry Andric 208bcb0991SDimitry Andric #define DEBUG_TYPE "dgb" 218bcb0991SDimitry Andric 228bcb0991SDimitry Andric STATISTIC(TotalGraphs, "Number of dependence graphs created."); 238bcb0991SDimitry Andric STATISTIC(TotalDefUseEdges, "Number of def-use edges created."); 248bcb0991SDimitry Andric STATISTIC(TotalMemoryEdges, "Number of memory dependence edges created."); 258bcb0991SDimitry Andric STATISTIC(TotalFineGrainedNodes, "Number of fine-grained nodes created."); 26*480093f4SDimitry Andric STATISTIC(TotalPiBlockNodes, "Number of pi-block nodes created."); 278bcb0991SDimitry Andric STATISTIC(TotalConfusedEdges, 288bcb0991SDimitry Andric "Number of confused memory dependencies between two nodes."); 298bcb0991SDimitry Andric STATISTIC(TotalEdgeReversals, 308bcb0991SDimitry Andric "Number of times the source and sink of dependence was reversed to " 318bcb0991SDimitry Andric "expose cycles in the graph."); 328bcb0991SDimitry Andric 338bcb0991SDimitry Andric using InstructionListType = SmallVector<Instruction *, 2>; 348bcb0991SDimitry Andric 358bcb0991SDimitry Andric //===--------------------------------------------------------------------===// 368bcb0991SDimitry Andric // AbstractDependenceGraphBuilder implementation 378bcb0991SDimitry Andric //===--------------------------------------------------------------------===// 388bcb0991SDimitry Andric 398bcb0991SDimitry Andric template <class G> 40*480093f4SDimitry Andric void AbstractDependenceGraphBuilder<G>::computeInstructionOrdinals() { 41*480093f4SDimitry Andric // The BBList is expected to be in program order. 42*480093f4SDimitry Andric size_t NextOrdinal = 1; 43*480093f4SDimitry Andric for (auto *BB : BBList) 44*480093f4SDimitry Andric for (auto &I : *BB) 45*480093f4SDimitry Andric InstOrdinalMap.insert(std::make_pair(&I, NextOrdinal++)); 46*480093f4SDimitry Andric } 47*480093f4SDimitry Andric 48*480093f4SDimitry Andric template <class G> 498bcb0991SDimitry Andric void AbstractDependenceGraphBuilder<G>::createFineGrainedNodes() { 508bcb0991SDimitry Andric ++TotalGraphs; 518bcb0991SDimitry Andric assert(IMap.empty() && "Expected empty instruction map at start"); 528bcb0991SDimitry Andric for (BasicBlock *BB : BBList) 538bcb0991SDimitry Andric for (Instruction &I : *BB) { 548bcb0991SDimitry Andric auto &NewNode = createFineGrainedNode(I); 558bcb0991SDimitry Andric IMap.insert(std::make_pair(&I, &NewNode)); 56*480093f4SDimitry Andric NodeOrdinalMap.insert(std::make_pair(&NewNode, getOrdinal(I))); 578bcb0991SDimitry Andric ++TotalFineGrainedNodes; 588bcb0991SDimitry Andric } 598bcb0991SDimitry Andric } 608bcb0991SDimitry Andric 618bcb0991SDimitry Andric template <class G> 628bcb0991SDimitry Andric void AbstractDependenceGraphBuilder<G>::createAndConnectRootNode() { 638bcb0991SDimitry Andric // Create a root node that connects to every connected component of the graph. 648bcb0991SDimitry Andric // This is done to allow graph iterators to visit all the disjoint components 658bcb0991SDimitry Andric // of the graph, in a single walk. 668bcb0991SDimitry Andric // 678bcb0991SDimitry Andric // This algorithm works by going through each node of the graph and for each 688bcb0991SDimitry Andric // node N, do a DFS starting from N. A rooted edge is established between the 698bcb0991SDimitry Andric // root node and N (if N is not yet visited). All the nodes reachable from N 708bcb0991SDimitry Andric // are marked as visited and are skipped in the DFS of subsequent nodes. 718bcb0991SDimitry Andric // 728bcb0991SDimitry Andric // Note: This algorithm tries to limit the number of edges out of the root 738bcb0991SDimitry Andric // node to some extent, but there may be redundant edges created depending on 748bcb0991SDimitry Andric // the iteration order. For example for a graph {A -> B}, an edge from the 758bcb0991SDimitry Andric // root node is added to both nodes if B is visited before A. While it does 768bcb0991SDimitry Andric // not result in minimal number of edges, this approach saves compile-time 778bcb0991SDimitry Andric // while keeping the number of edges in check. 788bcb0991SDimitry Andric auto &RootNode = createRootNode(); 798bcb0991SDimitry Andric df_iterator_default_set<const NodeType *, 4> Visited; 808bcb0991SDimitry Andric for (auto *N : Graph) { 818bcb0991SDimitry Andric if (*N == RootNode) 828bcb0991SDimitry Andric continue; 838bcb0991SDimitry Andric for (auto I : depth_first_ext(N, Visited)) 848bcb0991SDimitry Andric if (I == N) 858bcb0991SDimitry Andric createRootedEdge(RootNode, *N); 868bcb0991SDimitry Andric } 878bcb0991SDimitry Andric } 888bcb0991SDimitry Andric 89*480093f4SDimitry Andric template <class G> void AbstractDependenceGraphBuilder<G>::createPiBlocks() { 90*480093f4SDimitry Andric if (!shouldCreatePiBlocks()) 91*480093f4SDimitry Andric return; 92*480093f4SDimitry Andric 93*480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "==== Start of Creation of Pi-Blocks ===\n"); 94*480093f4SDimitry Andric 95*480093f4SDimitry Andric // The overall algorithm is as follows: 96*480093f4SDimitry Andric // 1. Identify SCCs and for each SCC create a pi-block node containing all 97*480093f4SDimitry Andric // the nodes in that SCC. 98*480093f4SDimitry Andric // 2. Identify incoming edges incident to the nodes inside of the SCC and 99*480093f4SDimitry Andric // reconnect them to the pi-block node. 100*480093f4SDimitry Andric // 3. Identify outgoing edges from the nodes inside of the SCC to nodes 101*480093f4SDimitry Andric // outside of it and reconnect them so that the edges are coming out of the 102*480093f4SDimitry Andric // SCC node instead. 103*480093f4SDimitry Andric 104*480093f4SDimitry Andric // Adding nodes as we iterate through the SCCs cause the SCC 105*480093f4SDimitry Andric // iterators to get invalidated. To prevent this invalidation, we first 106*480093f4SDimitry Andric // collect a list of nodes that are part of an SCC, and then iterate over 107*480093f4SDimitry Andric // those lists to create the pi-block nodes. Each element of the list is a 108*480093f4SDimitry Andric // list of nodes in an SCC. Note: trivial SCCs containing a single node are 109*480093f4SDimitry Andric // ignored. 110*480093f4SDimitry Andric SmallVector<NodeListType, 4> ListOfSCCs; 111*480093f4SDimitry Andric for (auto &SCC : make_range(scc_begin(&Graph), scc_end(&Graph))) { 112*480093f4SDimitry Andric if (SCC.size() > 1) 113*480093f4SDimitry Andric ListOfSCCs.emplace_back(SCC.begin(), SCC.end()); 114*480093f4SDimitry Andric } 115*480093f4SDimitry Andric 116*480093f4SDimitry Andric for (NodeListType &NL : ListOfSCCs) { 117*480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "Creating pi-block node with " << NL.size() 118*480093f4SDimitry Andric << " nodes in it.\n"); 119*480093f4SDimitry Andric 120*480093f4SDimitry Andric // SCC iterator may put the nodes in an order that's different from the 121*480093f4SDimitry Andric // program order. To preserve original program order, we sort the list of 122*480093f4SDimitry Andric // nodes based on ordinal numbers computed earlier. 123*480093f4SDimitry Andric llvm::sort(NL, [&](NodeType *LHS, NodeType *RHS) { 124*480093f4SDimitry Andric return getOrdinal(*LHS) < getOrdinal(*RHS); 125*480093f4SDimitry Andric }); 126*480093f4SDimitry Andric 127*480093f4SDimitry Andric NodeType &PiNode = createPiBlock(NL); 128*480093f4SDimitry Andric ++TotalPiBlockNodes; 129*480093f4SDimitry Andric 130*480093f4SDimitry Andric // Build a set to speed up the lookup for edges whose targets 131*480093f4SDimitry Andric // are inside the SCC. 132*480093f4SDimitry Andric SmallPtrSet<NodeType *, 4> NodesInSCC(NL.begin(), NL.end()); 133*480093f4SDimitry Andric 134*480093f4SDimitry Andric // We have the set of nodes in the SCC. We go through the set of nodes 135*480093f4SDimitry Andric // that are outside of the SCC and look for edges that cross the two sets. 136*480093f4SDimitry Andric for (NodeType *N : Graph) { 137*480093f4SDimitry Andric 138*480093f4SDimitry Andric // Skip the SCC node and all the nodes inside of it. 139*480093f4SDimitry Andric if (*N == PiNode || NodesInSCC.count(N)) 140*480093f4SDimitry Andric continue; 141*480093f4SDimitry Andric 142*480093f4SDimitry Andric for (NodeType *SCCNode : NL) { 143*480093f4SDimitry Andric 144*480093f4SDimitry Andric enum Direction { 145*480093f4SDimitry Andric Incoming, // Incoming edges to the SCC 146*480093f4SDimitry Andric Outgoing, // Edges going ot of the SCC 147*480093f4SDimitry Andric DirectionCount // To make the enum usable as an array index. 148*480093f4SDimitry Andric }; 149*480093f4SDimitry Andric 150*480093f4SDimitry Andric // Use these flags to help us avoid creating redundant edges. If there 151*480093f4SDimitry Andric // are more than one edges from an outside node to inside nodes, we only 152*480093f4SDimitry Andric // keep one edge from that node to the pi-block node. Similarly, if 153*480093f4SDimitry Andric // there are more than one edges from inside nodes to an outside node, 154*480093f4SDimitry Andric // we only keep one edge from the pi-block node to the outside node. 155*480093f4SDimitry Andric // There is a flag defined for each direction (incoming vs outgoing) and 156*480093f4SDimitry Andric // for each type of edge supported, using a two-dimensional boolean 157*480093f4SDimitry Andric // array. 158*480093f4SDimitry Andric using EdgeKind = typename EdgeType::EdgeKind; 159*480093f4SDimitry Andric EnumeratedArray<bool, EdgeKind> EdgeAlreadyCreated[DirectionCount]{ 160*480093f4SDimitry Andric false, false}; 161*480093f4SDimitry Andric 162*480093f4SDimitry Andric auto createEdgeOfKind = [this](NodeType &Src, NodeType &Dst, 163*480093f4SDimitry Andric const EdgeKind K) { 164*480093f4SDimitry Andric switch (K) { 165*480093f4SDimitry Andric case EdgeKind::RegisterDefUse: 166*480093f4SDimitry Andric createDefUseEdge(Src, Dst); 167*480093f4SDimitry Andric break; 168*480093f4SDimitry Andric case EdgeKind::MemoryDependence: 169*480093f4SDimitry Andric createMemoryEdge(Src, Dst); 170*480093f4SDimitry Andric break; 171*480093f4SDimitry Andric case EdgeKind::Rooted: 172*480093f4SDimitry Andric createRootedEdge(Src, Dst); 173*480093f4SDimitry Andric break; 174*480093f4SDimitry Andric default: 175*480093f4SDimitry Andric llvm_unreachable("Unsupported type of edge."); 176*480093f4SDimitry Andric } 177*480093f4SDimitry Andric }; 178*480093f4SDimitry Andric 179*480093f4SDimitry Andric auto reconnectEdges = [&](NodeType *Src, NodeType *Dst, NodeType *New, 180*480093f4SDimitry Andric const Direction Dir) { 181*480093f4SDimitry Andric if (!Src->hasEdgeTo(*Dst)) 182*480093f4SDimitry Andric return; 183*480093f4SDimitry Andric LLVM_DEBUG(dbgs() 184*480093f4SDimitry Andric << "reconnecting(" 185*480093f4SDimitry Andric << (Dir == Direction::Incoming ? "incoming)" : "outgoing)") 186*480093f4SDimitry Andric << ":\nSrc:" << *Src << "\nDst:" << *Dst 187*480093f4SDimitry Andric << "\nNew:" << *New << "\n"); 188*480093f4SDimitry Andric assert((Dir == Direction::Incoming || Dir == Direction::Outgoing) && 189*480093f4SDimitry Andric "Invalid direction."); 190*480093f4SDimitry Andric 191*480093f4SDimitry Andric SmallVector<EdgeType *, 10> EL; 192*480093f4SDimitry Andric Src->findEdgesTo(*Dst, EL); 193*480093f4SDimitry Andric for (EdgeType *OldEdge : EL) { 194*480093f4SDimitry Andric EdgeKind Kind = OldEdge->getKind(); 195*480093f4SDimitry Andric if (!EdgeAlreadyCreated[Dir][Kind]) { 196*480093f4SDimitry Andric if (Dir == Direction::Incoming) { 197*480093f4SDimitry Andric createEdgeOfKind(*Src, *New, Kind); 198*480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "created edge from Src to New.\n"); 199*480093f4SDimitry Andric } else if (Dir == Direction::Outgoing) { 200*480093f4SDimitry Andric createEdgeOfKind(*New, *Dst, Kind); 201*480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "created edge from New to Dst.\n"); 202*480093f4SDimitry Andric } 203*480093f4SDimitry Andric EdgeAlreadyCreated[Dir][Kind] = true; 204*480093f4SDimitry Andric } 205*480093f4SDimitry Andric Src->removeEdge(*OldEdge); 206*480093f4SDimitry Andric destroyEdge(*OldEdge); 207*480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "removed old edge between Src and Dst.\n\n"); 208*480093f4SDimitry Andric } 209*480093f4SDimitry Andric }; 210*480093f4SDimitry Andric 211*480093f4SDimitry Andric // Process incoming edges incident to the pi-block node. 212*480093f4SDimitry Andric reconnectEdges(N, SCCNode, &PiNode, Direction::Incoming); 213*480093f4SDimitry Andric 214*480093f4SDimitry Andric // Process edges that are coming out of the pi-block node. 215*480093f4SDimitry Andric reconnectEdges(SCCNode, N, &PiNode, Direction::Outgoing); 216*480093f4SDimitry Andric } 217*480093f4SDimitry Andric } 218*480093f4SDimitry Andric } 219*480093f4SDimitry Andric 220*480093f4SDimitry Andric // Ordinal maps are no longer needed. 221*480093f4SDimitry Andric InstOrdinalMap.clear(); 222*480093f4SDimitry Andric NodeOrdinalMap.clear(); 223*480093f4SDimitry Andric 224*480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "==== End of Creation of Pi-Blocks ===\n"); 225*480093f4SDimitry Andric } 226*480093f4SDimitry 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 377*480093f4SDimitry Andric template <class G> 378*480093f4SDimitry Andric void AbstractDependenceGraphBuilder<G>::sortNodesTopologically() { 379*480093f4SDimitry Andric 380*480093f4SDimitry Andric // If we don't create pi-blocks, then we may not have a DAG. 381*480093f4SDimitry Andric if (!shouldCreatePiBlocks()) 382*480093f4SDimitry Andric return; 383*480093f4SDimitry Andric 384*480093f4SDimitry Andric SmallVector<NodeType *, 64> NodesInPO; 385*480093f4SDimitry Andric using NodeKind = typename NodeType::NodeKind; 386*480093f4SDimitry Andric for (NodeType *N : post_order(&Graph)) { 387*480093f4SDimitry Andric if (N->getKind() == NodeKind::PiBlock) { 388*480093f4SDimitry Andric // Put members of the pi-block right after the pi-block itself, for 389*480093f4SDimitry Andric // convenience. 390*480093f4SDimitry Andric const NodeListType &PiBlockMembers = getNodesInPiBlock(*N); 391*480093f4SDimitry Andric NodesInPO.insert(NodesInPO.end(), PiBlockMembers.begin(), 392*480093f4SDimitry Andric PiBlockMembers.end()); 393*480093f4SDimitry Andric } 394*480093f4SDimitry Andric NodesInPO.push_back(N); 395*480093f4SDimitry Andric } 396*480093f4SDimitry Andric 397*480093f4SDimitry Andric size_t OldSize = Graph.Nodes.size(); 398*480093f4SDimitry Andric Graph.Nodes.clear(); 399*480093f4SDimitry Andric for (NodeType *N : reverse(NodesInPO)) 400*480093f4SDimitry Andric Graph.Nodes.push_back(N); 401*480093f4SDimitry Andric if (Graph.Nodes.size() != OldSize) 402*480093f4SDimitry Andric assert(false && 403*480093f4SDimitry Andric "Expected the number of nodes to stay the same after the sort"); 404*480093f4SDimitry Andric } 405*480093f4SDimitry Andric 4068bcb0991SDimitry Andric template class llvm::AbstractDependenceGraphBuilder<DataDependenceGraph>; 4078bcb0991SDimitry Andric template class llvm::DependenceGraphInfo<DDGNode>; 408