Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:12

0001 //===- llvm/Analysis/DependenceGraphBuilder.h -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines a builder interface that can be used to populate dependence
0010 // graphs such as DDG and PDG.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_ANALYSIS_DEPENDENCEGRAPHBUILDER_H
0015 #define LLVM_ANALYSIS_DEPENDENCEGRAPHBUILDER_H
0016 
0017 #include "llvm/ADT/DenseMap.h"
0018 #include "llvm/ADT/EquivalenceClasses.h"
0019 #include "llvm/ADT/SmallVector.h"
0020 
0021 namespace llvm {
0022 
0023 class BasicBlock;
0024 class DependenceInfo;
0025 class Instruction;
0026 
0027 /// This abstract builder class defines a set of high-level steps for creating
0028 /// DDG-like graphs. The client code is expected to inherit from this class and
0029 /// define concrete implementation for each of the pure virtual functions used
0030 /// in the high-level algorithm.
0031 template <class GraphType> class AbstractDependenceGraphBuilder {
0032 protected:
0033   using BasicBlockListType = SmallVectorImpl<BasicBlock *>;
0034 
0035 private:
0036   using NodeType = typename GraphType::NodeType;
0037   using EdgeType = typename GraphType::EdgeType;
0038 
0039 public:
0040   using ClassesType = EquivalenceClasses<BasicBlock *>;
0041   using NodeListType = SmallVector<NodeType *, 4>;
0042 
0043   AbstractDependenceGraphBuilder(GraphType &G, DependenceInfo &D,
0044                                  const BasicBlockListType &BBs)
0045       : Graph(G), DI(D), BBList(BBs) {}
0046   virtual ~AbstractDependenceGraphBuilder() = default;
0047 
0048   /// The main entry to the graph construction algorithm. It starts by
0049   /// creating nodes in increasing order of granularity and then
0050   /// adds def-use and memory edges. As one of the final stages, it
0051   /// also creates pi-block nodes to facilitate codegen in transformations
0052   /// that use dependence graphs.
0053   ///
0054   /// The algorithmic complexity of this implementation is O(V^2 * I^2), where V
0055   /// is the number of vertecies (nodes) and I is the number of instructions in
0056   /// each node. The total number of instructions, N, is equal to V * I,
0057   /// therefore the worst-case time complexity is O(N^2). The average time
0058   /// complexity is O((N^2)/2).
0059   void populate() {
0060     computeInstructionOrdinals();
0061     createFineGrainedNodes();
0062     createDefUseEdges();
0063     createMemoryDependencyEdges();
0064     simplify();
0065     createAndConnectRootNode();
0066     createPiBlocks();
0067     sortNodesTopologically();
0068   }
0069 
0070   /// Compute ordinal numbers for each instruction and store them in a map for
0071   /// future look up. These ordinals are used to compute node ordinals which are
0072   /// in turn used to order nodes that are part of a cycle.
0073   /// Instruction ordinals are assigned based on lexical program order.
0074   void computeInstructionOrdinals();
0075 
0076   /// Create fine grained nodes. These are typically atomic nodes that
0077   /// consist of a single instruction.
0078   void createFineGrainedNodes();
0079 
0080   /// Analyze the def-use chains and create edges from the nodes containing
0081   /// definitions to the nodes containing the uses.
0082   void createDefUseEdges();
0083 
0084   /// Analyze data dependencies that exist between memory loads or stores,
0085   /// in the graph nodes and create edges between them.
0086   void createMemoryDependencyEdges();
0087 
0088   /// Create a root node and add edges such that each node in the graph is
0089   /// reachable from the root.
0090   void createAndConnectRootNode();
0091 
0092   /// Apply graph abstraction to groups of nodes that belong to a strongly
0093   /// connected component of the graph to create larger compound nodes
0094   /// called pi-blocks. The purpose of this abstraction is to isolate sets of
0095   /// program elements that need to stay together during codegen and turn
0096   /// the dependence graph into an acyclic graph.
0097   void createPiBlocks();
0098 
0099   /// Go through all the nodes in the graph and collapse any two nodes
0100   /// 'a' and 'b' if all of the following are true:
0101   ///   - the only edge from 'a' is a def-use edge to 'b' and
0102   ///   - the only edge to 'b' is a def-use edge from 'a' and
0103   ///   - there is no cyclic edge from 'b' to 'a' and
0104   ///   - all instructions in 'a' and 'b' belong to the same basic block and
0105   ///   - both 'a' and 'b' are simple (single or multi instruction) nodes.
0106   void simplify();
0107 
0108   /// Topologically sort the graph nodes.
0109   void sortNodesTopologically();
0110 
0111 protected:
0112   /// Create the root node of the graph.
0113   virtual NodeType &createRootNode() = 0;
0114 
0115   /// Create an atomic node in the graph given a single instruction.
0116   virtual NodeType &createFineGrainedNode(Instruction &I) = 0;
0117 
0118   /// Create a pi-block node in the graph representing a group of nodes in an
0119   /// SCC of the graph.
0120   virtual NodeType &createPiBlock(const NodeListType &L) = 0;
0121 
0122   /// Create a def-use edge going from \p Src to \p Tgt.
0123   virtual EdgeType &createDefUseEdge(NodeType &Src, NodeType &Tgt) = 0;
0124 
0125   /// Create a memory dependence edge going from \p Src to \p Tgt.
0126   virtual EdgeType &createMemoryEdge(NodeType &Src, NodeType &Tgt) = 0;
0127 
0128   /// Create a rooted edge going from \p Src to \p Tgt .
0129   virtual EdgeType &createRootedEdge(NodeType &Src, NodeType &Tgt) = 0;
0130 
0131   /// Given a pi-block node, return a vector of all the nodes contained within
0132   /// it.
0133   virtual const NodeListType &getNodesInPiBlock(const NodeType &N) = 0;
0134 
0135   /// Deallocate memory of edge \p E.
0136   virtual void destroyEdge(EdgeType &E) { delete &E; }
0137 
0138   /// Deallocate memory of node \p N.
0139   virtual void destroyNode(NodeType &N) { delete &N; }
0140 
0141   /// Return true if creation of pi-blocks are supported and desired,
0142   /// and false otherwise.
0143   virtual bool shouldCreatePiBlocks() const { return true; }
0144 
0145   /// Return true if graph simplification step is requested, and false
0146   /// otherwise.
0147   virtual bool shouldSimplify() const { return true; }
0148 
0149   /// Return true if it's safe to merge the two nodes.
0150   virtual bool areNodesMergeable(const NodeType &A,
0151                                  const NodeType &B) const = 0;
0152 
0153   /// Append the content of node \p B into node \p A and remove \p B and
0154   /// the edge between \p A and \p B from the graph.
0155   virtual void mergeNodes(NodeType &A, NodeType &B) = 0;
0156 
0157   /// Given an instruction \p I return its associated ordinal number.
0158   size_t getOrdinal(Instruction &I) {
0159     assert(InstOrdinalMap.contains(&I) &&
0160            "No ordinal computed for this instruction.");
0161     return InstOrdinalMap[&I];
0162   }
0163 
0164   /// Given a node \p N return its associated ordinal number.
0165   size_t getOrdinal(NodeType &N) {
0166     assert(NodeOrdinalMap.contains(&N) && "No ordinal computed for this node.");
0167     return NodeOrdinalMap[&N];
0168   }
0169 
0170   /// Map types to map instructions to nodes used when populating the graph.
0171   using InstToNodeMap = DenseMap<Instruction *, NodeType *>;
0172 
0173   /// Map Types to map instruction/nodes to an ordinal number.
0174   using InstToOrdinalMap = DenseMap<Instruction *, size_t>;
0175   using NodeToOrdinalMap = DenseMap<NodeType *, size_t>;
0176 
0177   /// Reference to the graph that gets built by a concrete implementation of
0178   /// this builder.
0179   GraphType &Graph;
0180 
0181   /// Dependence information used to create memory dependence edges in the
0182   /// graph.
0183   DependenceInfo &DI;
0184 
0185   /// The list of basic blocks to consider when building the graph.
0186   const BasicBlockListType &BBList;
0187 
0188   /// A mapping from instructions to the corresponding nodes in the graph.
0189   InstToNodeMap IMap;
0190 
0191   /// A mapping from each instruction to an ordinal number. This map is used to
0192   /// populate the \p NodeOrdinalMap.
0193   InstToOrdinalMap InstOrdinalMap;
0194 
0195   /// A mapping from nodes to an ordinal number. This map is used to sort nodes
0196   /// in a pi-block based on program order.
0197   NodeToOrdinalMap NodeOrdinalMap;
0198 };
0199 
0200 } // namespace llvm
0201 
0202 #endif // LLVM_ANALYSIS_DEPENDENCEGRAPHBUILDER_H