DependenceGraphBuilder.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Analysis/DependenceGraphBuilder.h -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines a builder interface that can be used to populate dependence
  15. // graphs such as DDG and PDG.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_DEPENDENCE_GRAPH_BUILDER_H
  19. #define LLVM_ANALYSIS_DEPENDENCE_GRAPH_BUILDER_H
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/EquivalenceClasses.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. namespace llvm {
  24. class BasicBlock;
  25. class DependenceInfo;
  26. class Instruction;
  27. /// This abstract builder class defines a set of high-level steps for creating
  28. /// DDG-like graphs. The client code is expected to inherit from this class and
  29. /// define concrete implementation for each of the pure virtual functions used
  30. /// in the high-level algorithm.
  31. template <class GraphType> class AbstractDependenceGraphBuilder {
  32. protected:
  33. using BasicBlockListType = SmallVectorImpl<BasicBlock *>;
  34. private:
  35. using NodeType = typename GraphType::NodeType;
  36. using EdgeType = typename GraphType::EdgeType;
  37. public:
  38. using ClassesType = EquivalenceClasses<BasicBlock *>;
  39. using NodeListType = SmallVector<NodeType *, 4>;
  40. AbstractDependenceGraphBuilder(GraphType &G, DependenceInfo &D,
  41. const BasicBlockListType &BBs)
  42. : Graph(G), DI(D), BBList(BBs) {}
  43. virtual ~AbstractDependenceGraphBuilder() {}
  44. /// The main entry to the graph construction algorithm. It starts by
  45. /// creating nodes in increasing order of granularity and then
  46. /// adds def-use and memory edges. As one of the final stages, it
  47. /// also creates pi-block nodes to facilitate codegen in transformations
  48. /// that use dependence graphs.
  49. ///
  50. /// The algorithmic complexity of this implementation is O(V^2 * I^2), where V
  51. /// is the number of vertecies (nodes) and I is the number of instructions in
  52. /// each node. The total number of instructions, N, is equal to V * I,
  53. /// therefore the worst-case time complexity is O(N^2). The average time
  54. /// complexity is O((N^2)/2).
  55. void populate() {
  56. computeInstructionOrdinals();
  57. createFineGrainedNodes();
  58. createDefUseEdges();
  59. createMemoryDependencyEdges();
  60. simplify();
  61. createAndConnectRootNode();
  62. createPiBlocks();
  63. sortNodesTopologically();
  64. }
  65. /// Compute ordinal numbers for each instruction and store them in a map for
  66. /// future look up. These ordinals are used to compute node ordinals which are
  67. /// in turn used to order nodes that are part of a cycle.
  68. /// Instruction ordinals are assigned based on lexical program order.
  69. void computeInstructionOrdinals();
  70. /// Create fine grained nodes. These are typically atomic nodes that
  71. /// consist of a single instruction.
  72. void createFineGrainedNodes();
  73. /// Analyze the def-use chains and create edges from the nodes containing
  74. /// definitions to the nodes containing the uses.
  75. void createDefUseEdges();
  76. /// Analyze data dependencies that exist between memory loads or stores,
  77. /// in the graph nodes and create edges between them.
  78. void createMemoryDependencyEdges();
  79. /// Create a root node and add edges such that each node in the graph is
  80. /// reachable from the root.
  81. void createAndConnectRootNode();
  82. /// Apply graph abstraction to groups of nodes that belong to a strongly
  83. /// connected component of the graph to create larger compound nodes
  84. /// called pi-blocks. The purpose of this abstraction is to isolate sets of
  85. /// program elements that need to stay together during codegen and turn
  86. /// the dependence graph into an acyclic graph.
  87. void createPiBlocks();
  88. /// Go through all the nodes in the graph and collapse any two nodes
  89. /// 'a' and 'b' if all of the following are true:
  90. /// - the only edge from 'a' is a def-use edge to 'b' and
  91. /// - the only edge to 'b' is a def-use edge from 'a' and
  92. /// - there is no cyclic edge from 'b' to 'a' and
  93. /// - all instructions in 'a' and 'b' belong to the same basic block and
  94. /// - both 'a' and 'b' are simple (single or multi instruction) nodes.
  95. void simplify();
  96. /// Topologically sort the graph nodes.
  97. void sortNodesTopologically();
  98. protected:
  99. /// Create the root node of the graph.
  100. virtual NodeType &createRootNode() = 0;
  101. /// Create an atomic node in the graph given a single instruction.
  102. virtual NodeType &createFineGrainedNode(Instruction &I) = 0;
  103. /// Create a pi-block node in the graph representing a group of nodes in an
  104. /// SCC of the graph.
  105. virtual NodeType &createPiBlock(const NodeListType &L) = 0;
  106. /// Create a def-use edge going from \p Src to \p Tgt.
  107. virtual EdgeType &createDefUseEdge(NodeType &Src, NodeType &Tgt) = 0;
  108. /// Create a memory dependence edge going from \p Src to \p Tgt.
  109. virtual EdgeType &createMemoryEdge(NodeType &Src, NodeType &Tgt) = 0;
  110. /// Create a rooted edge going from \p Src to \p Tgt .
  111. virtual EdgeType &createRootedEdge(NodeType &Src, NodeType &Tgt) = 0;
  112. /// Given a pi-block node, return a vector of all the nodes contained within
  113. /// it.
  114. virtual const NodeListType &getNodesInPiBlock(const NodeType &N) = 0;
  115. /// Deallocate memory of edge \p E.
  116. virtual void destroyEdge(EdgeType &E) { delete &E; }
  117. /// Deallocate memory of node \p N.
  118. virtual void destroyNode(NodeType &N) { delete &N; }
  119. /// Return true if creation of pi-blocks are supported and desired,
  120. /// and false otherwise.
  121. virtual bool shouldCreatePiBlocks() const { return true; }
  122. /// Return true if graph simplification step is requested, and false
  123. /// otherwise.
  124. virtual bool shouldSimplify() const { return true; }
  125. /// Return true if it's safe to merge the two nodes.
  126. virtual bool areNodesMergeable(const NodeType &A,
  127. const NodeType &B) const = 0;
  128. /// Append the content of node \p B into node \p A and remove \p B and
  129. /// the edge between \p A and \p B from the graph.
  130. virtual void mergeNodes(NodeType &A, NodeType &B) = 0;
  131. /// Given an instruction \p I return its associated ordinal number.
  132. size_t getOrdinal(Instruction &I) {
  133. assert(InstOrdinalMap.find(&I) != InstOrdinalMap.end() &&
  134. "No ordinal computed for this instruction.");
  135. return InstOrdinalMap[&I];
  136. }
  137. /// Given a node \p N return its associated ordinal number.
  138. size_t getOrdinal(NodeType &N) {
  139. assert(NodeOrdinalMap.find(&N) != NodeOrdinalMap.end() &&
  140. "No ordinal computed for this node.");
  141. return NodeOrdinalMap[&N];
  142. }
  143. /// Map types to map instructions to nodes used when populating the graph.
  144. using InstToNodeMap = DenseMap<Instruction *, NodeType *>;
  145. /// Map Types to map instruction/nodes to an ordinal number.
  146. using InstToOrdinalMap = DenseMap<Instruction *, size_t>;
  147. using NodeToOrdinalMap = DenseMap<NodeType *, size_t>;
  148. /// Reference to the graph that gets built by a concrete implementation of
  149. /// this builder.
  150. GraphType &Graph;
  151. /// Dependence information used to create memory dependence edges in the
  152. /// graph.
  153. DependenceInfo &DI;
  154. /// The list of basic blocks to consider when building the graph.
  155. const BasicBlockListType &BBList;
  156. /// A mapping from instructions to the corresponding nodes in the graph.
  157. InstToNodeMap IMap;
  158. /// A mapping from each instruction to an ordinal number. This map is used to
  159. /// populate the \p NodeOrdinalMap.
  160. InstToOrdinalMap InstOrdinalMap;
  161. /// A mapping from nodes to an ordinal number. This map is used to sort nodes
  162. /// in a pi-block based on program order.
  163. NodeToOrdinalMap NodeOrdinalMap;
  164. };
  165. } // namespace llvm
  166. #endif // LLVM_ANALYSIS_DEPENDENCE_GRAPH_BUILDER_H
  167. #ifdef __GNUC__
  168. #pragma GCC diagnostic pop
  169. #endif