ImmutableGraph.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //==========-- ImmutableGraph.h - A fast DAG implementation ---------=========//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// Description: ImmutableGraph is a fast DAG implementation that cannot be
  10. /// modified, except by creating a new ImmutableGraph. ImmutableGraph is
  11. /// implemented as two arrays: one containing nodes, and one containing edges.
  12. /// The advantages to this implementation are two-fold:
  13. /// 1. Iteration and traversal operations benefit from cache locality.
  14. /// 2. Operations on sets of nodes/edges are efficient, and representations of
  15. /// those sets in memory are compact. For instance, a set of edges is
  16. /// implemented as a bit vector, wherein each bit corresponds to one edge in
  17. /// the edge array. This implies a lower bound of 64x spatial improvement
  18. /// over, e.g., an llvm::DenseSet or llvm::SmallSet. It also means that
  19. /// insert/erase/contains operations complete in negligible constant time:
  20. /// insert and erase require one load and one store, and contains requires
  21. /// just one load.
  22. ///
  23. //===----------------------------------------------------------------------===//
  24. #ifndef LLVM_LIB_TARGET_X86_IMMUTABLEGRAPH_H
  25. #define LLVM_LIB_TARGET_X86_IMMUTABLEGRAPH_H
  26. #include "llvm/ADT/BitVector.h"
  27. #include "llvm/ADT/GraphTraits.h"
  28. #include "llvm/ADT/STLExtras.h"
  29. #include <algorithm>
  30. #include <iterator>
  31. #include <utility>
  32. #include <vector>
  33. namespace llvm {
  34. template <typename NodeValueT, typename EdgeValueT> class ImmutableGraph {
  35. using Traits = GraphTraits<ImmutableGraph<NodeValueT, EdgeValueT> *>;
  36. template <typename> friend class ImmutableGraphBuilder;
  37. public:
  38. using node_value_type = NodeValueT;
  39. using edge_value_type = EdgeValueT;
  40. using size_type = int;
  41. class Node;
  42. class Edge {
  43. friend class ImmutableGraph;
  44. template <typename> friend class ImmutableGraphBuilder;
  45. const Node *Dest;
  46. edge_value_type Value;
  47. public:
  48. const Node *getDest() const { return Dest; };
  49. const edge_value_type &getValue() const { return Value; }
  50. };
  51. class Node {
  52. friend class ImmutableGraph;
  53. template <typename> friend class ImmutableGraphBuilder;
  54. const Edge *Edges;
  55. node_value_type Value;
  56. public:
  57. const node_value_type &getValue() const { return Value; }
  58. const Edge *edges_begin() const { return Edges; }
  59. // Nodes are allocated sequentially. Edges for a node are stored together.
  60. // The end of this Node's edges is the beginning of the next node's edges.
  61. // An extra node was allocated to hold the end pointer for the last real
  62. // node.
  63. const Edge *edges_end() const { return (this + 1)->Edges; }
  64. ArrayRef<Edge> edges() const {
  65. return ArrayRef(edges_begin(), edges_end());
  66. }
  67. };
  68. protected:
  69. ImmutableGraph(std::unique_ptr<Node[]> Nodes, std::unique_ptr<Edge[]> Edges,
  70. size_type NodesSize, size_type EdgesSize)
  71. : Nodes(std::move(Nodes)), Edges(std::move(Edges)), NodesSize(NodesSize),
  72. EdgesSize(EdgesSize) {}
  73. ImmutableGraph(const ImmutableGraph &) = delete;
  74. ImmutableGraph(ImmutableGraph &&) = delete;
  75. ImmutableGraph &operator=(const ImmutableGraph &) = delete;
  76. ImmutableGraph &operator=(ImmutableGraph &&) = delete;
  77. public:
  78. ArrayRef<Node> nodes() const { return ArrayRef(Nodes.get(), NodesSize); }
  79. const Node *nodes_begin() const { return nodes().begin(); }
  80. const Node *nodes_end() const { return nodes().end(); }
  81. ArrayRef<Edge> edges() const { return ArrayRef(Edges.get(), EdgesSize); }
  82. const Edge *edges_begin() const { return edges().begin(); }
  83. const Edge *edges_end() const { return edges().end(); }
  84. size_type nodes_size() const { return NodesSize; }
  85. size_type edges_size() const { return EdgesSize; }
  86. // Node N must belong to this ImmutableGraph.
  87. size_type getNodeIndex(const Node &N) const {
  88. return std::distance(nodes_begin(), &N);
  89. }
  90. // Edge E must belong to this ImmutableGraph.
  91. size_type getEdgeIndex(const Edge &E) const {
  92. return std::distance(edges_begin(), &E);
  93. }
  94. // FIXME: Could NodeSet and EdgeSet be templated to share code?
  95. class NodeSet {
  96. const ImmutableGraph &G;
  97. BitVector V;
  98. public:
  99. NodeSet(const ImmutableGraph &G, bool ContainsAll = false)
  100. : G{G}, V{static_cast<unsigned>(G.nodes_size()), ContainsAll} {}
  101. bool insert(const Node &N) {
  102. size_type Idx = G.getNodeIndex(N);
  103. bool AlreadyExists = V.test(Idx);
  104. V.set(Idx);
  105. return !AlreadyExists;
  106. }
  107. void erase(const Node &N) {
  108. size_type Idx = G.getNodeIndex(N);
  109. V.reset(Idx);
  110. }
  111. bool contains(const Node &N) const {
  112. size_type Idx = G.getNodeIndex(N);
  113. return V.test(Idx);
  114. }
  115. void clear() { V.reset(); }
  116. size_type empty() const { return V.none(); }
  117. /// Return the number of elements in the set
  118. size_type count() const { return V.count(); }
  119. /// Return the size of the set's domain
  120. size_type size() const { return V.size(); }
  121. /// Set union
  122. NodeSet &operator|=(const NodeSet &RHS) {
  123. assert(&this->G == &RHS.G);
  124. V |= RHS.V;
  125. return *this;
  126. }
  127. /// Set intersection
  128. NodeSet &operator&=(const NodeSet &RHS) {
  129. assert(&this->G == &RHS.G);
  130. V &= RHS.V;
  131. return *this;
  132. }
  133. /// Set disjoint union
  134. NodeSet &operator^=(const NodeSet &RHS) {
  135. assert(&this->G == &RHS.G);
  136. V ^= RHS.V;
  137. return *this;
  138. }
  139. using index_iterator = typename BitVector::const_set_bits_iterator;
  140. index_iterator index_begin() const { return V.set_bits_begin(); }
  141. index_iterator index_end() const { return V.set_bits_end(); }
  142. void set(size_type Idx) { V.set(Idx); }
  143. void reset(size_type Idx) { V.reset(Idx); }
  144. class iterator {
  145. const NodeSet &Set;
  146. size_type Current;
  147. void advance() {
  148. assert(Current != -1);
  149. Current = Set.V.find_next(Current);
  150. }
  151. public:
  152. iterator(const NodeSet &Set, size_type Begin)
  153. : Set{Set}, Current{Begin} {}
  154. iterator operator++(int) {
  155. iterator Tmp = *this;
  156. advance();
  157. return Tmp;
  158. }
  159. iterator &operator++() {
  160. advance();
  161. return *this;
  162. }
  163. Node *operator*() const {
  164. assert(Current != -1);
  165. return Set.G.nodes_begin() + Current;
  166. }
  167. bool operator==(const iterator &other) const {
  168. assert(&this->Set == &other.Set);
  169. return this->Current == other.Current;
  170. }
  171. bool operator!=(const iterator &other) const { return !(*this == other); }
  172. };
  173. iterator begin() const { return iterator{*this, V.find_first()}; }
  174. iterator end() const { return iterator{*this, -1}; }
  175. };
  176. class EdgeSet {
  177. const ImmutableGraph &G;
  178. BitVector V;
  179. public:
  180. EdgeSet(const ImmutableGraph &G, bool ContainsAll = false)
  181. : G{G}, V{static_cast<unsigned>(G.edges_size()), ContainsAll} {}
  182. bool insert(const Edge &E) {
  183. size_type Idx = G.getEdgeIndex(E);
  184. bool AlreadyExists = V.test(Idx);
  185. V.set(Idx);
  186. return !AlreadyExists;
  187. }
  188. void erase(const Edge &E) {
  189. size_type Idx = G.getEdgeIndex(E);
  190. V.reset(Idx);
  191. }
  192. bool contains(const Edge &E) const {
  193. size_type Idx = G.getEdgeIndex(E);
  194. return V.test(Idx);
  195. }
  196. void clear() { V.reset(); }
  197. bool empty() const { return V.none(); }
  198. /// Return the number of elements in the set
  199. size_type count() const { return V.count(); }
  200. /// Return the size of the set's domain
  201. size_type size() const { return V.size(); }
  202. /// Set union
  203. EdgeSet &operator|=(const EdgeSet &RHS) {
  204. assert(&this->G == &RHS.G);
  205. V |= RHS.V;
  206. return *this;
  207. }
  208. /// Set intersection
  209. EdgeSet &operator&=(const EdgeSet &RHS) {
  210. assert(&this->G == &RHS.G);
  211. V &= RHS.V;
  212. return *this;
  213. }
  214. /// Set disjoint union
  215. EdgeSet &operator^=(const EdgeSet &RHS) {
  216. assert(&this->G == &RHS.G);
  217. V ^= RHS.V;
  218. return *this;
  219. }
  220. using index_iterator = typename BitVector::const_set_bits_iterator;
  221. index_iterator index_begin() const { return V.set_bits_begin(); }
  222. index_iterator index_end() const { return V.set_bits_end(); }
  223. void set(size_type Idx) { V.set(Idx); }
  224. void reset(size_type Idx) { V.reset(Idx); }
  225. class iterator {
  226. const EdgeSet &Set;
  227. size_type Current;
  228. void advance() {
  229. assert(Current != -1);
  230. Current = Set.V.find_next(Current);
  231. }
  232. public:
  233. iterator(const EdgeSet &Set, size_type Begin)
  234. : Set{Set}, Current{Begin} {}
  235. iterator operator++(int) {
  236. iterator Tmp = *this;
  237. advance();
  238. return Tmp;
  239. }
  240. iterator &operator++() {
  241. advance();
  242. return *this;
  243. }
  244. Edge *operator*() const {
  245. assert(Current != -1);
  246. return Set.G.edges_begin() + Current;
  247. }
  248. bool operator==(const iterator &other) const {
  249. assert(&this->Set == &other.Set);
  250. return this->Current == other.Current;
  251. }
  252. bool operator!=(const iterator &other) const { return !(*this == other); }
  253. };
  254. iterator begin() const { return iterator{*this, V.find_first()}; }
  255. iterator end() const { return iterator{*this, -1}; }
  256. };
  257. private:
  258. std::unique_ptr<Node[]> Nodes;
  259. std::unique_ptr<Edge[]> Edges;
  260. size_type NodesSize;
  261. size_type EdgesSize;
  262. };
  263. template <typename GraphT> class ImmutableGraphBuilder {
  264. using node_value_type = typename GraphT::node_value_type;
  265. using edge_value_type = typename GraphT::edge_value_type;
  266. static_assert(
  267. std::is_base_of<ImmutableGraph<node_value_type, edge_value_type>,
  268. GraphT>::value,
  269. "Template argument to ImmutableGraphBuilder must derive from "
  270. "ImmutableGraph<>");
  271. using size_type = typename GraphT::size_type;
  272. using NodeSet = typename GraphT::NodeSet;
  273. using Node = typename GraphT::Node;
  274. using EdgeSet = typename GraphT::EdgeSet;
  275. using Edge = typename GraphT::Edge;
  276. using BuilderEdge = std::pair<edge_value_type, size_type>;
  277. using EdgeList = std::vector<BuilderEdge>;
  278. using BuilderVertex = std::pair<node_value_type, EdgeList>;
  279. using VertexVec = std::vector<BuilderVertex>;
  280. public:
  281. using BuilderNodeRef = size_type;
  282. BuilderNodeRef addVertex(const node_value_type &V) {
  283. auto I = AdjList.emplace(AdjList.end(), V, EdgeList{});
  284. return std::distance(AdjList.begin(), I);
  285. }
  286. void addEdge(const edge_value_type &E, BuilderNodeRef From,
  287. BuilderNodeRef To) {
  288. AdjList[From].second.emplace_back(E, To);
  289. }
  290. bool empty() const { return AdjList.empty(); }
  291. template <typename... ArgT> std::unique_ptr<GraphT> get(ArgT &&... Args) {
  292. size_type VertexSize = AdjList.size(), EdgeSize = 0;
  293. for (const auto &V : AdjList) {
  294. EdgeSize += V.second.size();
  295. }
  296. auto VertexArray =
  297. std::make_unique<Node[]>(VertexSize + 1 /* terminator node */);
  298. auto EdgeArray = std::make_unique<Edge[]>(EdgeSize);
  299. size_type VI = 0, EI = 0;
  300. for (; VI < VertexSize; ++VI) {
  301. VertexArray[VI].Value = std::move(AdjList[VI].first);
  302. VertexArray[VI].Edges = &EdgeArray[EI];
  303. auto NumEdges = static_cast<size_type>(AdjList[VI].second.size());
  304. for (size_type VEI = 0; VEI < NumEdges; ++VEI, ++EI) {
  305. auto &E = AdjList[VI].second[VEI];
  306. EdgeArray[EI].Value = std::move(E.first);
  307. EdgeArray[EI].Dest = &VertexArray[E.second];
  308. }
  309. }
  310. assert(VI == VertexSize && EI == EdgeSize && "ImmutableGraph malformed");
  311. VertexArray[VI].Edges = &EdgeArray[EdgeSize]; // terminator node
  312. return std::make_unique<GraphT>(std::move(VertexArray),
  313. std::move(EdgeArray), VertexSize, EdgeSize,
  314. std::forward<ArgT>(Args)...);
  315. }
  316. template <typename... ArgT>
  317. static std::unique_ptr<GraphT> trim(const GraphT &G, const NodeSet &TrimNodes,
  318. const EdgeSet &TrimEdges,
  319. ArgT &&... Args) {
  320. size_type NewVertexSize = G.nodes_size() - TrimNodes.count();
  321. size_type NewEdgeSize = G.edges_size() - TrimEdges.count();
  322. auto NewVertexArray =
  323. std::make_unique<Node[]>(NewVertexSize + 1 /* terminator node */);
  324. auto NewEdgeArray = std::make_unique<Edge[]>(NewEdgeSize);
  325. // Walk the nodes and determine the new index for each node.
  326. size_type NewNodeIndex = 0;
  327. std::vector<size_type> RemappedNodeIndex(G.nodes_size());
  328. for (const Node &N : G.nodes()) {
  329. if (TrimNodes.contains(N))
  330. continue;
  331. RemappedNodeIndex[G.getNodeIndex(N)] = NewNodeIndex++;
  332. }
  333. assert(NewNodeIndex == NewVertexSize &&
  334. "Should have assigned NewVertexSize indices");
  335. size_type VertexI = 0, EdgeI = 0;
  336. for (const Node &N : G.nodes()) {
  337. if (TrimNodes.contains(N))
  338. continue;
  339. NewVertexArray[VertexI].Value = N.getValue();
  340. NewVertexArray[VertexI].Edges = &NewEdgeArray[EdgeI];
  341. for (const Edge &E : N.edges()) {
  342. if (TrimEdges.contains(E))
  343. continue;
  344. NewEdgeArray[EdgeI].Value = E.getValue();
  345. size_type DestIdx = G.getNodeIndex(*E.getDest());
  346. size_type NewIdx = RemappedNodeIndex[DestIdx];
  347. assert(NewIdx < NewVertexSize);
  348. NewEdgeArray[EdgeI].Dest = &NewVertexArray[NewIdx];
  349. ++EdgeI;
  350. }
  351. ++VertexI;
  352. }
  353. assert(VertexI == NewVertexSize && EdgeI == NewEdgeSize &&
  354. "Gadget graph malformed");
  355. NewVertexArray[VertexI].Edges = &NewEdgeArray[NewEdgeSize]; // terminator
  356. return std::make_unique<GraphT>(std::move(NewVertexArray),
  357. std::move(NewEdgeArray), NewVertexSize,
  358. NewEdgeSize, std::forward<ArgT>(Args)...);
  359. }
  360. private:
  361. VertexVec AdjList;
  362. };
  363. template <typename NodeValueT, typename EdgeValueT>
  364. struct GraphTraits<ImmutableGraph<NodeValueT, EdgeValueT> *> {
  365. using GraphT = ImmutableGraph<NodeValueT, EdgeValueT>;
  366. using NodeRef = typename GraphT::Node const *;
  367. using EdgeRef = typename GraphT::Edge const &;
  368. static NodeRef edge_dest(EdgeRef E) { return E.getDest(); }
  369. using ChildIteratorType =
  370. mapped_iterator<typename GraphT::Edge const *, decltype(&edge_dest)>;
  371. static NodeRef getEntryNode(GraphT *G) { return G->nodes_begin(); }
  372. static ChildIteratorType child_begin(NodeRef N) {
  373. return {N->edges_begin(), &edge_dest};
  374. }
  375. static ChildIteratorType child_end(NodeRef N) {
  376. return {N->edges_end(), &edge_dest};
  377. }
  378. static NodeRef getNode(typename GraphT::Node const &N) { return NodeRef{&N}; }
  379. using nodes_iterator =
  380. mapped_iterator<typename GraphT::Node const *, decltype(&getNode)>;
  381. static nodes_iterator nodes_begin(GraphT *G) {
  382. return {G->nodes_begin(), &getNode};
  383. }
  384. static nodes_iterator nodes_end(GraphT *G) {
  385. return {G->nodes_end(), &getNode};
  386. }
  387. using ChildEdgeIteratorType = typename GraphT::Edge const *;
  388. static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
  389. return N->edges_begin();
  390. }
  391. static ChildEdgeIteratorType child_edge_end(NodeRef N) {
  392. return N->edges_end();
  393. }
  394. static typename GraphT::size_type size(GraphT *G) { return G->nodes_size(); }
  395. };
  396. } // end namespace llvm
  397. #endif // LLVM_LIB_TARGET_X86_IMMUTABLEGRAPH_H