Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/DependenceGraphBuilder.cpp
35233 views
//===- DependenceGraphBuilder.cpp ------------------------------------------==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// This file implements common steps of the build algorithm for construction8// of dependence graphs such as DDG and PDG.9//===----------------------------------------------------------------------===//1011#include "llvm/Analysis/DependenceGraphBuilder.h"12#include "llvm/ADT/DepthFirstIterator.h"13#include "llvm/ADT/EnumeratedArray.h"14#include "llvm/ADT/PostOrderIterator.h"15#include "llvm/ADT/SCCIterator.h"16#include "llvm/ADT/Statistic.h"17#include "llvm/Analysis/DDG.h"1819using namespace llvm;2021#define DEBUG_TYPE "dgb"2223STATISTIC(TotalGraphs, "Number of dependence graphs created.");24STATISTIC(TotalDefUseEdges, "Number of def-use edges created.");25STATISTIC(TotalMemoryEdges, "Number of memory dependence edges created.");26STATISTIC(TotalFineGrainedNodes, "Number of fine-grained nodes created.");27STATISTIC(TotalPiBlockNodes, "Number of pi-block nodes created.");28STATISTIC(TotalConfusedEdges,29"Number of confused memory dependencies between two nodes.");30STATISTIC(TotalEdgeReversals,31"Number of times the source and sink of dependence was reversed to "32"expose cycles in the graph.");3334using InstructionListType = SmallVector<Instruction *, 2>;3536//===--------------------------------------------------------------------===//37// AbstractDependenceGraphBuilder implementation38//===--------------------------------------------------------------------===//3940template <class G>41void AbstractDependenceGraphBuilder<G>::computeInstructionOrdinals() {42// The BBList is expected to be in program order.43size_t NextOrdinal = 1;44for (auto *BB : BBList)45for (auto &I : *BB)46InstOrdinalMap.insert(std::make_pair(&I, NextOrdinal++));47}4849template <class G>50void AbstractDependenceGraphBuilder<G>::createFineGrainedNodes() {51++TotalGraphs;52assert(IMap.empty() && "Expected empty instruction map at start");53for (BasicBlock *BB : BBList)54for (Instruction &I : *BB) {55auto &NewNode = createFineGrainedNode(I);56IMap.insert(std::make_pair(&I, &NewNode));57NodeOrdinalMap.insert(std::make_pair(&NewNode, getOrdinal(I)));58++TotalFineGrainedNodes;59}60}6162template <class G>63void AbstractDependenceGraphBuilder<G>::createAndConnectRootNode() {64// Create a root node that connects to every connected component of the graph.65// This is done to allow graph iterators to visit all the disjoint components66// of the graph, in a single walk.67//68// This algorithm works by going through each node of the graph and for each69// node N, do a DFS starting from N. A rooted edge is established between the70// root node and N (if N is not yet visited). All the nodes reachable from N71// are marked as visited and are skipped in the DFS of subsequent nodes.72//73// Note: This algorithm tries to limit the number of edges out of the root74// node to some extent, but there may be redundant edges created depending on75// the iteration order. For example for a graph {A -> B}, an edge from the76// root node is added to both nodes if B is visited before A. While it does77// not result in minimal number of edges, this approach saves compile-time78// while keeping the number of edges in check.79auto &RootNode = createRootNode();80df_iterator_default_set<const NodeType *, 4> Visited;81for (auto *N : Graph) {82if (*N == RootNode)83continue;84for (auto I : depth_first_ext(N, Visited))85if (I == N)86createRootedEdge(RootNode, *N);87}88}8990template <class G> void AbstractDependenceGraphBuilder<G>::createPiBlocks() {91if (!shouldCreatePiBlocks())92return;9394LLVM_DEBUG(dbgs() << "==== Start of Creation of Pi-Blocks ===\n");9596// The overall algorithm is as follows:97// 1. Identify SCCs and for each SCC create a pi-block node containing all98// the nodes in that SCC.99// 2. Identify incoming edges incident to the nodes inside of the SCC and100// reconnect them to the pi-block node.101// 3. Identify outgoing edges from the nodes inside of the SCC to nodes102// outside of it and reconnect them so that the edges are coming out of the103// SCC node instead.104105// Adding nodes as we iterate through the SCCs cause the SCC106// iterators to get invalidated. To prevent this invalidation, we first107// collect a list of nodes that are part of an SCC, and then iterate over108// those lists to create the pi-block nodes. Each element of the list is a109// list of nodes in an SCC. Note: trivial SCCs containing a single node are110// ignored.111SmallVector<NodeListType, 4> ListOfSCCs;112for (auto &SCC : make_range(scc_begin(&Graph), scc_end(&Graph))) {113if (SCC.size() > 1)114ListOfSCCs.emplace_back(SCC.begin(), SCC.end());115}116117for (NodeListType &NL : ListOfSCCs) {118LLVM_DEBUG(dbgs() << "Creating pi-block node with " << NL.size()119<< " nodes in it.\n");120121// SCC iterator may put the nodes in an order that's different from the122// program order. To preserve original program order, we sort the list of123// nodes based on ordinal numbers computed earlier.124llvm::sort(NL, [&](NodeType *LHS, NodeType *RHS) {125return getOrdinal(*LHS) < getOrdinal(*RHS);126});127128NodeType &PiNode = createPiBlock(NL);129++TotalPiBlockNodes;130131// Build a set to speed up the lookup for edges whose targets132// are inside the SCC.133SmallPtrSet<NodeType *, 4> NodesInSCC(NL.begin(), NL.end());134135// We have the set of nodes in the SCC. We go through the set of nodes136// that are outside of the SCC and look for edges that cross the two sets.137for (NodeType *N : Graph) {138139// Skip the SCC node and all the nodes inside of it.140if (*N == PiNode || NodesInSCC.count(N))141continue;142143enum Direction {144Incoming, // Incoming edges to the SCC145Outgoing, // Edges going ot of the SCC146DirectionCount // To make the enum usable as an array index.147};148149// Use these flags to help us avoid creating redundant edges. If there150// are more than one edges from an outside node to inside nodes, we only151// keep one edge from that node to the pi-block node. Similarly, if152// there are more than one edges from inside nodes to an outside node,153// we only keep one edge from the pi-block node to the outside node.154// There is a flag defined for each direction (incoming vs outgoing) and155// for each type of edge supported, using a two-dimensional boolean156// array.157using EdgeKind = typename EdgeType::EdgeKind;158EnumeratedArray<bool, EdgeKind> EdgeAlreadyCreated[DirectionCount]{false,159false};160161auto createEdgeOfKind = [this](NodeType &Src, NodeType &Dst,162const EdgeKind K) {163switch (K) {164case EdgeKind::RegisterDefUse:165createDefUseEdge(Src, Dst);166break;167case EdgeKind::MemoryDependence:168createMemoryEdge(Src, Dst);169break;170case EdgeKind::Rooted:171createRootedEdge(Src, Dst);172break;173default:174llvm_unreachable("Unsupported type of edge.");175}176};177178auto reconnectEdges = [&](NodeType *Src, NodeType *Dst, NodeType *New,179const Direction Dir) {180if (!Src->hasEdgeTo(*Dst))181return;182LLVM_DEBUG(183dbgs() << "reconnecting("184<< (Dir == Direction::Incoming ? "incoming)" : "outgoing)")185<< ":\nSrc:" << *Src << "\nDst:" << *Dst << "\nNew:" << *New186<< "\n");187assert((Dir == Direction::Incoming || Dir == Direction::Outgoing) &&188"Invalid direction.");189190SmallVector<EdgeType *, 10> EL;191Src->findEdgesTo(*Dst, EL);192for (EdgeType *OldEdge : EL) {193EdgeKind Kind = OldEdge->getKind();194if (!EdgeAlreadyCreated[Dir][Kind]) {195if (Dir == Direction::Incoming) {196createEdgeOfKind(*Src, *New, Kind);197LLVM_DEBUG(dbgs() << "created edge from Src to New.\n");198} else if (Dir == Direction::Outgoing) {199createEdgeOfKind(*New, *Dst, Kind);200LLVM_DEBUG(dbgs() << "created edge from New to Dst.\n");201}202EdgeAlreadyCreated[Dir][Kind] = true;203}204Src->removeEdge(*OldEdge);205destroyEdge(*OldEdge);206LLVM_DEBUG(dbgs() << "removed old edge between Src and Dst.\n\n");207}208};209210for (NodeType *SCCNode : NL) {211// Process incoming edges incident to the pi-block node.212reconnectEdges(N, SCCNode, &PiNode, Direction::Incoming);213214// Process edges that are coming out of the pi-block node.215reconnectEdges(SCCNode, N, &PiNode, Direction::Outgoing);216}217}218}219220// Ordinal maps are no longer needed.221InstOrdinalMap.clear();222NodeOrdinalMap.clear();223224LLVM_DEBUG(dbgs() << "==== End of Creation of Pi-Blocks ===\n");225}226227template <class G> void AbstractDependenceGraphBuilder<G>::createDefUseEdges() {228for (NodeType *N : Graph) {229InstructionListType SrcIList;230N->collectInstructions([](const Instruction *I) { return true; }, SrcIList);231232// Use a set to mark the targets that we link to N, so we don't add233// duplicate def-use edges when more than one instruction in a target node234// use results of instructions that are contained in N.235SmallPtrSet<NodeType *, 4> VisitedTargets;236237for (Instruction *II : SrcIList) {238for (User *U : II->users()) {239Instruction *UI = dyn_cast<Instruction>(U);240if (!UI)241continue;242NodeType *DstNode = nullptr;243if (IMap.find(UI) != IMap.end())244DstNode = IMap.find(UI)->second;245246// In the case of loops, the scope of the subgraph is all the247// basic blocks (and instructions within them) belonging to the loop. We248// simply ignore all the edges coming from (or going into) instructions249// or basic blocks outside of this range.250if (!DstNode) {251LLVM_DEBUG(252dbgs()253<< "skipped def-use edge since the sink" << *UI254<< " is outside the range of instructions being considered.\n");255continue;256}257258// Self dependencies are ignored because they are redundant and259// uninteresting.260if (DstNode == N) {261LLVM_DEBUG(dbgs()262<< "skipped def-use edge since the sink and the source ("263<< N << ") are the same.\n");264continue;265}266267if (VisitedTargets.insert(DstNode).second) {268createDefUseEdge(*N, *DstNode);269++TotalDefUseEdges;270}271}272}273}274}275276template <class G>277void AbstractDependenceGraphBuilder<G>::createMemoryDependencyEdges() {278using DGIterator = typename G::iterator;279auto isMemoryAccess = [](const Instruction *I) {280return I->mayReadOrWriteMemory();281};282for (DGIterator SrcIt = Graph.begin(), E = Graph.end(); SrcIt != E; ++SrcIt) {283InstructionListType SrcIList;284(*SrcIt)->collectInstructions(isMemoryAccess, SrcIList);285if (SrcIList.empty())286continue;287288for (DGIterator DstIt = SrcIt; DstIt != E; ++DstIt) {289if (**SrcIt == **DstIt)290continue;291InstructionListType DstIList;292(*DstIt)->collectInstructions(isMemoryAccess, DstIList);293if (DstIList.empty())294continue;295bool ForwardEdgeCreated = false;296bool BackwardEdgeCreated = false;297for (Instruction *ISrc : SrcIList) {298for (Instruction *IDst : DstIList) {299auto D = DI.depends(ISrc, IDst, true);300if (!D)301continue;302303// If we have a dependence with its left-most non-'=' direction304// being '>' we need to reverse the direction of the edge, because305// the source of the dependence cannot occur after the sink. For306// confused dependencies, we will create edges in both directions to307// represent the possibility of a cycle.308309auto createConfusedEdges = [&](NodeType &Src, NodeType &Dst) {310if (!ForwardEdgeCreated) {311createMemoryEdge(Src, Dst);312++TotalMemoryEdges;313}314if (!BackwardEdgeCreated) {315createMemoryEdge(Dst, Src);316++TotalMemoryEdges;317}318ForwardEdgeCreated = BackwardEdgeCreated = true;319++TotalConfusedEdges;320};321322auto createForwardEdge = [&](NodeType &Src, NodeType &Dst) {323if (!ForwardEdgeCreated) {324createMemoryEdge(Src, Dst);325++TotalMemoryEdges;326}327ForwardEdgeCreated = true;328};329330auto createBackwardEdge = [&](NodeType &Src, NodeType &Dst) {331if (!BackwardEdgeCreated) {332createMemoryEdge(Dst, Src);333++TotalMemoryEdges;334}335BackwardEdgeCreated = true;336};337338if (D->isConfused())339createConfusedEdges(**SrcIt, **DstIt);340else if (D->isOrdered() && !D->isLoopIndependent()) {341bool ReversedEdge = false;342for (unsigned Level = 1; Level <= D->getLevels(); ++Level) {343if (D->getDirection(Level) == Dependence::DVEntry::EQ)344continue;345else if (D->getDirection(Level) == Dependence::DVEntry::GT) {346createBackwardEdge(**SrcIt, **DstIt);347ReversedEdge = true;348++TotalEdgeReversals;349break;350} else if (D->getDirection(Level) == Dependence::DVEntry::LT)351break;352else {353createConfusedEdges(**SrcIt, **DstIt);354break;355}356}357if (!ReversedEdge)358createForwardEdge(**SrcIt, **DstIt);359} else360createForwardEdge(**SrcIt, **DstIt);361362// Avoid creating duplicate edges.363if (ForwardEdgeCreated && BackwardEdgeCreated)364break;365}366367// If we've created edges in both directions, there is no more368// unique edge that we can create between these two nodes, so we369// can exit early.370if (ForwardEdgeCreated && BackwardEdgeCreated)371break;372}373}374}375}376377template <class G> void AbstractDependenceGraphBuilder<G>::simplify() {378if (!shouldSimplify())379return;380LLVM_DEBUG(dbgs() << "==== Start of Graph Simplification ===\n");381382// This algorithm works by first collecting a set of candidate nodes that have383// an out-degree of one (in terms of def-use edges), and then ignoring those384// whose targets have an in-degree more than one. Each node in the resulting385// set can then be merged with its corresponding target and put back into the386// worklist until no further merge candidates are available.387SmallPtrSet<NodeType *, 32> CandidateSourceNodes;388389// A mapping between nodes and their in-degree. To save space, this map390// only contains nodes that are targets of nodes in the CandidateSourceNodes.391DenseMap<NodeType *, unsigned> TargetInDegreeMap;392393for (NodeType *N : Graph) {394if (N->getEdges().size() != 1)395continue;396EdgeType &Edge = N->back();397if (!Edge.isDefUse())398continue;399CandidateSourceNodes.insert(N);400401// Insert an element into the in-degree map and initialize to zero. The402// count will get updated in the next step.403TargetInDegreeMap.insert({&Edge.getTargetNode(), 0});404}405406LLVM_DEBUG({407dbgs() << "Size of candidate src node list:" << CandidateSourceNodes.size()408<< "\nNode with single outgoing def-use edge:\n";409for (NodeType *N : CandidateSourceNodes) {410dbgs() << N << "\n";411}412});413414for (NodeType *N : Graph) {415for (EdgeType *E : *N) {416NodeType *Tgt = &E->getTargetNode();417auto TgtIT = TargetInDegreeMap.find(Tgt);418if (TgtIT != TargetInDegreeMap.end())419++(TgtIT->second);420}421}422423LLVM_DEBUG({424dbgs() << "Size of target in-degree map:" << TargetInDegreeMap.size()425<< "\nContent of in-degree map:\n";426for (auto &I : TargetInDegreeMap) {427dbgs() << I.first << " --> " << I.second << "\n";428}429});430431SmallVector<NodeType *, 32> Worklist(CandidateSourceNodes.begin(),432CandidateSourceNodes.end());433while (!Worklist.empty()) {434NodeType &Src = *Worklist.pop_back_val();435// As nodes get merged, we need to skip any node that has been removed from436// the candidate set (see below).437if (!CandidateSourceNodes.erase(&Src))438continue;439440assert(Src.getEdges().size() == 1 &&441"Expected a single edge from the candidate src node.");442NodeType &Tgt = Src.back().getTargetNode();443assert(TargetInDegreeMap.find(&Tgt) != TargetInDegreeMap.end() &&444"Expected target to be in the in-degree map.");445446if (TargetInDegreeMap[&Tgt] != 1)447continue;448449if (!areNodesMergeable(Src, Tgt))450continue;451452// Do not merge if there is also an edge from target to src (immediate453// cycle).454if (Tgt.hasEdgeTo(Src))455continue;456457LLVM_DEBUG(dbgs() << "Merging:" << Src << "\nWith:" << Tgt << "\n");458459mergeNodes(Src, Tgt);460461// If the target node is in the candidate set itself, we need to put the462// src node back into the worklist again so it gives the target a chance463// to get merged into it. For example if we have:464// {(a)->(b), (b)->(c), (c)->(d), ...} and the worklist is initially {b, a},465// then after merging (a) and (b) together, we need to put (a,b) back in466// the worklist so that (c) can get merged in as well resulting in467// {(a,b,c) -> d}468// We also need to remove the old target (b), from the worklist. We first469// remove it from the candidate set here, and skip any item from the470// worklist that is not in the set.471if (CandidateSourceNodes.erase(&Tgt)) {472Worklist.push_back(&Src);473CandidateSourceNodes.insert(&Src);474LLVM_DEBUG(dbgs() << "Putting " << &Src << " back in the worklist.\n");475}476}477LLVM_DEBUG(dbgs() << "=== End of Graph Simplification ===\n");478}479480template <class G>481void AbstractDependenceGraphBuilder<G>::sortNodesTopologically() {482483// If we don't create pi-blocks, then we may not have a DAG.484if (!shouldCreatePiBlocks())485return;486487SmallVector<NodeType *, 64> NodesInPO;488using NodeKind = typename NodeType::NodeKind;489for (NodeType *N : post_order(&Graph)) {490if (N->getKind() == NodeKind::PiBlock) {491// Put members of the pi-block right after the pi-block itself, for492// convenience.493const NodeListType &PiBlockMembers = getNodesInPiBlock(*N);494llvm::append_range(NodesInPO, PiBlockMembers);495}496NodesInPO.push_back(N);497}498499size_t OldSize = Graph.Nodes.size();500Graph.Nodes.clear();501append_range(Graph.Nodes, reverse(NodesInPO));502if (Graph.Nodes.size() != OldSize)503assert(false &&504"Expected the number of nodes to stay the same after the sort");505}506507template class llvm::AbstractDependenceGraphBuilder<DataDependenceGraph>;508template class llvm::DependenceGraphInfo<DDGNode>;509510511