LLVM  8.0.1
Graph.h
Go to the documentation of this file.
1 //===- Graph.h - PBQP Graph -------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // PBQP Graph class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CODEGEN_PBQP_GRAPH_H
15 #define LLVM_CODEGEN_PBQP_GRAPH_H
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include <algorithm>
19 #include <cassert>
20 #include <iterator>
21 #include <limits>
22 #include <vector>
23 
24 namespace llvm {
25 namespace PBQP {
26 
27  class GraphBase {
28  public:
29  using NodeId = unsigned;
30  using EdgeId = unsigned;
31 
32  /// Returns a value representing an invalid (non-existent) node.
33  static NodeId invalidNodeId() {
35  }
36 
37  /// Returns a value representing an invalid (non-existent) edge.
38  static EdgeId invalidEdgeId() {
40  }
41  };
42 
43  /// PBQP Graph class.
44  /// Instances of this class describe PBQP problems.
45  ///
46  template <typename SolverT>
47  class Graph : public GraphBase {
48  private:
49  using CostAllocator = typename SolverT::CostAllocator;
50 
51  public:
52  using RawVector = typename SolverT::RawVector;
53  using RawMatrix = typename SolverT::RawMatrix;
54  using Vector = typename SolverT::Vector;
55  using Matrix = typename SolverT::Matrix;
56  using VectorPtr = typename CostAllocator::VectorPtr;
57  using MatrixPtr = typename CostAllocator::MatrixPtr;
58  using NodeMetadata = typename SolverT::NodeMetadata;
59  using EdgeMetadata = typename SolverT::EdgeMetadata;
60  using GraphMetadata = typename SolverT::GraphMetadata;
61 
62  private:
63  class NodeEntry {
64  public:
65  using AdjEdgeList = std::vector<EdgeId>;
66  using AdjEdgeIdx = AdjEdgeList::size_type;
67  using AdjEdgeItr = AdjEdgeList::const_iterator;
68 
69  NodeEntry(VectorPtr Costs) : Costs(std::move(Costs)) {}
70 
71  static AdjEdgeIdx getInvalidAdjEdgeIdx() {
73  }
74 
75  AdjEdgeIdx addAdjEdgeId(EdgeId EId) {
76  AdjEdgeIdx Idx = AdjEdgeIds.size();
77  AdjEdgeIds.push_back(EId);
78  return Idx;
79  }
80 
81  void removeAdjEdgeId(Graph &G, NodeId ThisNId, AdjEdgeIdx Idx) {
82  // Swap-and-pop for fast removal.
83  // 1) Update the adj index of the edge currently at back().
84  // 2) Move last Edge down to Idx.
85  // 3) pop_back()
86  // If Idx == size() - 1 then the setAdjEdgeIdx and swap are
87  // redundant, but both operations are cheap.
88  G.getEdge(AdjEdgeIds.back()).setAdjEdgeIdx(ThisNId, Idx);
89  AdjEdgeIds[Idx] = AdjEdgeIds.back();
90  AdjEdgeIds.pop_back();
91  }
92 
93  const AdjEdgeList& getAdjEdgeIds() const { return AdjEdgeIds; }
94 
95  VectorPtr Costs;
97 
98  private:
99  AdjEdgeList AdjEdgeIds;
100  };
101 
102  class EdgeEntry {
103  public:
104  EdgeEntry(NodeId N1Id, NodeId N2Id, MatrixPtr Costs)
105  : Costs(std::move(Costs)) {
106  NIds[0] = N1Id;
107  NIds[1] = N2Id;
108  ThisEdgeAdjIdxs[0] = NodeEntry::getInvalidAdjEdgeIdx();
109  ThisEdgeAdjIdxs[1] = NodeEntry::getInvalidAdjEdgeIdx();
110  }
111 
112  void connectToN(Graph &G, EdgeId ThisEdgeId, unsigned NIdx) {
113  assert(ThisEdgeAdjIdxs[NIdx] == NodeEntry::getInvalidAdjEdgeIdx() &&
114  "Edge already connected to NIds[NIdx].");
115  NodeEntry &N = G.getNode(NIds[NIdx]);
116  ThisEdgeAdjIdxs[NIdx] = N.addAdjEdgeId(ThisEdgeId);
117  }
118 
119  void connect(Graph &G, EdgeId ThisEdgeId) {
120  connectToN(G, ThisEdgeId, 0);
121  connectToN(G, ThisEdgeId, 1);
122  }
123 
124  void setAdjEdgeIdx(NodeId NId, typename NodeEntry::AdjEdgeIdx NewIdx) {
125  if (NId == NIds[0])
126  ThisEdgeAdjIdxs[0] = NewIdx;
127  else {
128  assert(NId == NIds[1] && "Edge not connected to NId");
129  ThisEdgeAdjIdxs[1] = NewIdx;
130  }
131  }
132 
133  void disconnectFromN(Graph &G, unsigned NIdx) {
134  assert(ThisEdgeAdjIdxs[NIdx] != NodeEntry::getInvalidAdjEdgeIdx() &&
135  "Edge not connected to NIds[NIdx].");
136  NodeEntry &N = G.getNode(NIds[NIdx]);
137  N.removeAdjEdgeId(G, NIds[NIdx], ThisEdgeAdjIdxs[NIdx]);
138  ThisEdgeAdjIdxs[NIdx] = NodeEntry::getInvalidAdjEdgeIdx();
139  }
140 
141  void disconnectFrom(Graph &G, NodeId NId) {
142  if (NId == NIds[0])
143  disconnectFromN(G, 0);
144  else {
145  assert(NId == NIds[1] && "Edge does not connect NId");
146  disconnectFromN(G, 1);
147  }
148  }
149 
150  NodeId getN1Id() const { return NIds[0]; }
151  NodeId getN2Id() const { return NIds[1]; }
152 
153  MatrixPtr Costs;
155 
156  private:
157  NodeId NIds[2];
158  typename NodeEntry::AdjEdgeIdx ThisEdgeAdjIdxs[2];
159  };
160 
161  // ----- MEMBERS -----
162 
164  CostAllocator CostAlloc;
165  SolverT *Solver = nullptr;
166 
167  using NodeVector = std::vector<NodeEntry>;
168  using FreeNodeVector = std::vector<NodeId>;
169  NodeVector Nodes;
170  FreeNodeVector FreeNodeIds;
171 
172  using EdgeVector = std::vector<EdgeEntry>;
173  using FreeEdgeVector = std::vector<EdgeId>;
174  EdgeVector Edges;
175  FreeEdgeVector FreeEdgeIds;
176 
177  Graph(const Graph &Other) {}
178 
179  // ----- INTERNAL METHODS -----
180 
181  NodeEntry &getNode(NodeId NId) {
182  assert(NId < Nodes.size() && "Out of bound NodeId");
183  return Nodes[NId];
184  }
185  const NodeEntry &getNode(NodeId NId) const {
186  assert(NId < Nodes.size() && "Out of bound NodeId");
187  return Nodes[NId];
188  }
189 
190  EdgeEntry& getEdge(EdgeId EId) { return Edges[EId]; }
191  const EdgeEntry& getEdge(EdgeId EId) const { return Edges[EId]; }
192 
193  NodeId addConstructedNode(NodeEntry N) {
194  NodeId NId = 0;
195  if (!FreeNodeIds.empty()) {
196  NId = FreeNodeIds.back();
197  FreeNodeIds.pop_back();
198  Nodes[NId] = std::move(N);
199  } else {
200  NId = Nodes.size();
201  Nodes.push_back(std::move(N));
202  }
203  return NId;
204  }
205 
206  EdgeId addConstructedEdge(EdgeEntry E) {
207  assert(findEdge(E.getN1Id(), E.getN2Id()) == invalidEdgeId() &&
208  "Attempt to add duplicate edge.");
209  EdgeId EId = 0;
210  if (!FreeEdgeIds.empty()) {
211  EId = FreeEdgeIds.back();
212  FreeEdgeIds.pop_back();
213  Edges[EId] = std::move(E);
214  } else {
215  EId = Edges.size();
216  Edges.push_back(std::move(E));
217  }
218 
219  EdgeEntry &NE = getEdge(EId);
220 
221  // Add the edge to the adjacency sets of its nodes.
222  NE.connect(*this, EId);
223  return EId;
224  }
225 
226  void operator=(const Graph &Other) {}
227 
228  public:
229  using AdjEdgeItr = typename NodeEntry::AdjEdgeItr;
230 
231  class NodeItr {
232  public:
233  using iterator_category = std::forward_iterator_tag;
235  using difference_type = int;
236  using pointer = NodeId *;
237  using reference = NodeId &;
238 
239  NodeItr(NodeId CurNId, const Graph &G)
240  : CurNId(CurNId), EndNId(G.Nodes.size()), FreeNodeIds(G.FreeNodeIds) {
241  this->CurNId = findNextInUse(CurNId); // Move to first in-use node id
242  }
243 
244  bool operator==(const NodeItr &O) const { return CurNId == O.CurNId; }
245  bool operator!=(const NodeItr &O) const { return !(*this == O); }
246  NodeItr& operator++() { CurNId = findNextInUse(++CurNId); return *this; }
247  NodeId operator*() const { return CurNId; }
248 
249  private:
250  NodeId findNextInUse(NodeId NId) const {
251  while (NId < EndNId && is_contained(FreeNodeIds, NId)) {
252  ++NId;
253  }
254  return NId;
255  }
256 
257  NodeId CurNId, EndNId;
258  const FreeNodeVector &FreeNodeIds;
259  };
260 
261  class EdgeItr {
262  public:
263  EdgeItr(EdgeId CurEId, const Graph &G)
264  : CurEId(CurEId), EndEId(G.Edges.size()), FreeEdgeIds(G.FreeEdgeIds) {
265  this->CurEId = findNextInUse(CurEId); // Move to first in-use edge id
266  }
267 
268  bool operator==(const EdgeItr &O) const { return CurEId == O.CurEId; }
269  bool operator!=(const EdgeItr &O) const { return !(*this == O); }
270  EdgeItr& operator++() { CurEId = findNextInUse(++CurEId); return *this; }
271  EdgeId operator*() const { return CurEId; }
272 
273  private:
274  EdgeId findNextInUse(EdgeId EId) const {
275  while (EId < EndEId && is_contained(FreeEdgeIds, EId)) {
276  ++EId;
277  }
278  return EId;
279  }
280 
281  EdgeId CurEId, EndEId;
282  const FreeEdgeVector &FreeEdgeIds;
283  };
284 
285  class NodeIdSet {
286  public:
287  NodeIdSet(const Graph &G) : G(G) {}
288 
289  NodeItr begin() const { return NodeItr(0, G); }
290  NodeItr end() const { return NodeItr(G.Nodes.size(), G); }
291 
292  bool empty() const { return G.Nodes.empty(); }
293 
294  typename NodeVector::size_type size() const {
295  return G.Nodes.size() - G.FreeNodeIds.size();
296  }
297 
298  private:
299  const Graph& G;
300  };
301 
302  class EdgeIdSet {
303  public:
304  EdgeIdSet(const Graph &G) : G(G) {}
305 
306  EdgeItr begin() const { return EdgeItr(0, G); }
307  EdgeItr end() const { return EdgeItr(G.Edges.size(), G); }
308 
309  bool empty() const { return G.Edges.empty(); }
310 
311  typename NodeVector::size_type size() const {
312  return G.Edges.size() - G.FreeEdgeIds.size();
313  }
314 
315  private:
316  const Graph& G;
317  };
318 
319  class AdjEdgeIdSet {
320  public:
321  AdjEdgeIdSet(const NodeEntry &NE) : NE(NE) {}
322 
323  typename NodeEntry::AdjEdgeItr begin() const {
324  return NE.getAdjEdgeIds().begin();
325  }
326 
327  typename NodeEntry::AdjEdgeItr end() const {
328  return NE.getAdjEdgeIds().end();
329  }
330 
331  bool empty() const { return NE.getAdjEdgeIds().empty(); }
332 
333  typename NodeEntry::AdjEdgeList::size_type size() const {
334  return NE.getAdjEdgeIds().size();
335  }
336 
337  private:
338  const NodeEntry &NE;
339  };
340 
341  /// Construct an empty PBQP graph.
342  Graph() = default;
343 
344  /// Construct an empty PBQP graph with the given graph metadata.
345  Graph(GraphMetadata Metadata) : Metadata(std::move(Metadata)) {}
346 
347  /// Get a reference to the graph metadata.
349 
350  /// Get a const-reference to the graph metadata.
351  const GraphMetadata& getMetadata() const { return Metadata; }
352 
353  /// Lock this graph to the given solver instance in preparation
354  /// for running the solver. This method will call solver.handleAddNode for
355  /// each node in the graph, and handleAddEdge for each edge, to give the
356  /// solver an opportunity to set up any requried metadata.
357  void setSolver(SolverT &S) {
358  assert(!Solver && "Solver already set. Call unsetSolver().");
359  Solver = &S;
360  for (auto NId : nodeIds())
361  Solver->handleAddNode(NId);
362  for (auto EId : edgeIds())
363  Solver->handleAddEdge(EId);
364  }
365 
366  /// Release from solver instance.
367  void unsetSolver() {
368  assert(Solver && "Solver not set.");
369  Solver = nullptr;
370  }
371 
372  /// Add a node with the given costs.
373  /// @param Costs Cost vector for the new node.
374  /// @return Node iterator for the added node.
375  template <typename OtherVectorT>
376  NodeId addNode(OtherVectorT Costs) {
377  // Get cost vector from the problem domain
378  VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
379  NodeId NId = addConstructedNode(NodeEntry(AllocatedCosts));
380  if (Solver)
381  Solver->handleAddNode(NId);
382  return NId;
383  }
384 
385  /// Add a node bypassing the cost allocator.
386  /// @param Costs Cost vector ptr for the new node (must be convertible to
387  /// VectorPtr).
388  /// @return Node iterator for the added node.
389  ///
390  /// This method allows for fast addition of a node whose costs don't need
391  /// to be passed through the cost allocator. The most common use case for
392  /// this is when duplicating costs from an existing node (when using a
393  /// pooling allocator). These have already been uniqued, so we can avoid
394  /// re-constructing and re-uniquing them by attaching them directly to the
395  /// new node.
396  template <typename OtherVectorPtrT>
397  NodeId addNodeBypassingCostAllocator(OtherVectorPtrT Costs) {
398  NodeId NId = addConstructedNode(NodeEntry(Costs));
399  if (Solver)
400  Solver->handleAddNode(NId);
401  return NId;
402  }
403 
404  /// Add an edge between the given nodes with the given costs.
405  /// @param N1Id First node.
406  /// @param N2Id Second node.
407  /// @param Costs Cost matrix for new edge.
408  /// @return Edge iterator for the added edge.
409  template <typename OtherVectorT>
410  EdgeId addEdge(NodeId N1Id, NodeId N2Id, OtherVectorT Costs) {
411  assert(getNodeCosts(N1Id).getLength() == Costs.getRows() &&
412  getNodeCosts(N2Id).getLength() == Costs.getCols() &&
413  "Matrix dimensions mismatch.");
414  // Get cost matrix from the problem domain.
415  MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
416  EdgeId EId = addConstructedEdge(EdgeEntry(N1Id, N2Id, AllocatedCosts));
417  if (Solver)
418  Solver->handleAddEdge(EId);
419  return EId;
420  }
421 
422  /// Add an edge bypassing the cost allocator.
423  /// @param N1Id First node.
424  /// @param N2Id Second node.
425  /// @param Costs Cost matrix for new edge.
426  /// @return Edge iterator for the added edge.
427  ///
428  /// This method allows for fast addition of an edge whose costs don't need
429  /// to be passed through the cost allocator. The most common use case for
430  /// this is when duplicating costs from an existing edge (when using a
431  /// pooling allocator). These have already been uniqued, so we can avoid
432  /// re-constructing and re-uniquing them by attaching them directly to the
433  /// new edge.
434  template <typename OtherMatrixPtrT>
436  OtherMatrixPtrT Costs) {
437  assert(getNodeCosts(N1Id).getLength() == Costs->getRows() &&
438  getNodeCosts(N2Id).getLength() == Costs->getCols() &&
439  "Matrix dimensions mismatch.");
440  // Get cost matrix from the problem domain.
441  EdgeId EId = addConstructedEdge(EdgeEntry(N1Id, N2Id, Costs));
442  if (Solver)
443  Solver->handleAddEdge(EId);
444  return EId;
445  }
446 
447  /// Returns true if the graph is empty.
448  bool empty() const { return NodeIdSet(*this).empty(); }
449 
450  NodeIdSet nodeIds() const { return NodeIdSet(*this); }
451  EdgeIdSet edgeIds() const { return EdgeIdSet(*this); }
452 
453  AdjEdgeIdSet adjEdgeIds(NodeId NId) { return AdjEdgeIdSet(getNode(NId)); }
454 
455  /// Get the number of nodes in the graph.
456  /// @return Number of nodes in the graph.
457  unsigned getNumNodes() const { return NodeIdSet(*this).size(); }
458 
459  /// Get the number of edges in the graph.
460  /// @return Number of edges in the graph.
461  unsigned getNumEdges() const { return EdgeIdSet(*this).size(); }
462 
463  /// Set a node's cost vector.
464  /// @param NId Node to update.
465  /// @param Costs New costs to set.
466  template <typename OtherVectorT>
467  void setNodeCosts(NodeId NId, OtherVectorT Costs) {
468  VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
469  if (Solver)
470  Solver->handleSetNodeCosts(NId, *AllocatedCosts);
471  getNode(NId).Costs = AllocatedCosts;
472  }
473 
474  /// Get a VectorPtr to a node's cost vector. Rarely useful - use
475  /// getNodeCosts where possible.
476  /// @param NId Node id.
477  /// @return VectorPtr to node cost vector.
478  ///
479  /// This method is primarily useful for duplicating costs quickly by
480  /// bypassing the cost allocator. See addNodeBypassingCostAllocator. Prefer
481  /// getNodeCosts when dealing with node cost values.
482  const VectorPtr& getNodeCostsPtr(NodeId NId) const {
483  return getNode(NId).Costs;
484  }
485 
486  /// Get a node's cost vector.
487  /// @param NId Node id.
488  /// @return Node cost vector.
489  const Vector& getNodeCosts(NodeId NId) const {
490  return *getNodeCostsPtr(NId);
491  }
492 
494  return getNode(NId).Metadata;
495  }
496 
497  const NodeMetadata& getNodeMetadata(NodeId NId) const {
498  return getNode(NId).Metadata;
499  }
500 
501  typename NodeEntry::AdjEdgeList::size_type getNodeDegree(NodeId NId) const {
502  return getNode(NId).getAdjEdgeIds().size();
503  }
504 
505  /// Update an edge's cost matrix.
506  /// @param EId Edge id.
507  /// @param Costs New cost matrix.
508  template <typename OtherMatrixT>
509  void updateEdgeCosts(EdgeId EId, OtherMatrixT Costs) {
510  MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
511  if (Solver)
512  Solver->handleUpdateCosts(EId, *AllocatedCosts);
513  getEdge(EId).Costs = AllocatedCosts;
514  }
515 
516  /// Get a MatrixPtr to a node's cost matrix. Rarely useful - use
517  /// getEdgeCosts where possible.
518  /// @param EId Edge id.
519  /// @return MatrixPtr to edge cost matrix.
520  ///
521  /// This method is primarily useful for duplicating costs quickly by
522  /// bypassing the cost allocator. See addNodeBypassingCostAllocator. Prefer
523  /// getEdgeCosts when dealing with edge cost values.
524  const MatrixPtr& getEdgeCostsPtr(EdgeId EId) const {
525  return getEdge(EId).Costs;
526  }
527 
528  /// Get an edge's cost matrix.
529  /// @param EId Edge id.
530  /// @return Edge cost matrix.
531  const Matrix& getEdgeCosts(EdgeId EId) const {
532  return *getEdge(EId).Costs;
533  }
534 
536  return getEdge(EId).Metadata;
537  }
538 
539  const EdgeMetadata& getEdgeMetadata(EdgeId EId) const {
540  return getEdge(EId).Metadata;
541  }
542 
543  /// Get the first node connected to this edge.
544  /// @param EId Edge id.
545  /// @return The first node connected to the given edge.
547  return getEdge(EId).getN1Id();
548  }
549 
550  /// Get the second node connected to this edge.
551  /// @param EId Edge id.
552  /// @return The second node connected to the given edge.
554  return getEdge(EId).getN2Id();
555  }
556 
557  /// Get the "other" node connected to this edge.
558  /// @param EId Edge id.
559  /// @param NId Node id for the "given" node.
560  /// @return The iterator for the "other" node connected to this edge.
562  EdgeEntry &E = getEdge(EId);
563  if (E.getN1Id() == NId) {
564  return E.getN2Id();
565  } // else
566  return E.getN1Id();
567  }
568 
569  /// Get the edge connecting two nodes.
570  /// @param N1Id First node id.
571  /// @param N2Id Second node id.
572  /// @return An id for edge (N1Id, N2Id) if such an edge exists,
573  /// otherwise returns an invalid edge id.
574  EdgeId findEdge(NodeId N1Id, NodeId N2Id) {
575  for (auto AEId : adjEdgeIds(N1Id)) {
576  if ((getEdgeNode1Id(AEId) == N2Id) ||
577  (getEdgeNode2Id(AEId) == N2Id)) {
578  return AEId;
579  }
580  }
581  return invalidEdgeId();
582  }
583 
584  /// Remove a node from the graph.
585  /// @param NId Node id.
586  void removeNode(NodeId NId) {
587  if (Solver)
588  Solver->handleRemoveNode(NId);
589  NodeEntry &N = getNode(NId);
590  // TODO: Can this be for-each'd?
591  for (AdjEdgeItr AEItr = N.adjEdgesBegin(),
592  AEEnd = N.adjEdgesEnd();
593  AEItr != AEEnd;) {
594  EdgeId EId = *AEItr;
595  ++AEItr;
596  removeEdge(EId);
597  }
598  FreeNodeIds.push_back(NId);
599  }
600 
601  /// Disconnect an edge from the given node.
602  ///
603  /// Removes the given edge from the adjacency list of the given node.
604  /// This operation leaves the edge in an 'asymmetric' state: It will no
605  /// longer appear in an iteration over the given node's (NId's) edges, but
606  /// will appear in an iteration over the 'other', unnamed node's edges.
607  ///
608  /// This does not correspond to any normal graph operation, but exists to
609  /// support efficient PBQP graph-reduction based solvers. It is used to
610  /// 'effectively' remove the unnamed node from the graph while the solver
611  /// is performing the reduction. The solver will later call reconnectNode
612  /// to restore the edge in the named node's adjacency list.
613  ///
614  /// Since the degree of a node is the number of connected edges,
615  /// disconnecting an edge from a node 'u' will cause the degree of 'u' to
616  /// drop by 1.
617  ///
618  /// A disconnected edge WILL still appear in an iteration over the graph
619  /// edges.
620  ///
621  /// A disconnected edge should not be removed from the graph, it should be
622  /// reconnected first.
623  ///
624  /// A disconnected edge can be reconnected by calling the reconnectEdge
625  /// method.
626  void disconnectEdge(EdgeId EId, NodeId NId) {
627  if (Solver)
628  Solver->handleDisconnectEdge(EId, NId);
629 
630  EdgeEntry &E = getEdge(EId);
631  E.disconnectFrom(*this, NId);
632  }
633 
634  /// Convenience method to disconnect all neighbours from the given
635  /// node.
637  for (auto AEId : adjEdgeIds(NId))
638  disconnectEdge(AEId, getEdgeOtherNodeId(AEId, NId));
639  }
640 
641  /// Re-attach an edge to its nodes.
642  ///
643  /// Adds an edge that had been previously disconnected back into the
644  /// adjacency set of the nodes that the edge connects.
645  void reconnectEdge(EdgeId EId, NodeId NId) {
646  EdgeEntry &E = getEdge(EId);
647  E.connectTo(*this, EId, NId);
648  if (Solver)
649  Solver->handleReconnectEdge(EId, NId);
650  }
651 
652  /// Remove an edge from the graph.
653  /// @param EId Edge id.
654  void removeEdge(EdgeId EId) {
655  if (Solver)
656  Solver->handleRemoveEdge(EId);
657  EdgeEntry &E = getEdge(EId);
658  E.disconnect();
659  FreeEdgeIds.push_back(EId);
660  Edges[EId].invalidate();
661  }
662 
663  /// Remove all nodes and edges from the graph.
664  void clear() {
665  Nodes.clear();
666  FreeNodeIds.clear();
667  Edges.clear();
668  FreeEdgeIds.clear();
669  }
670  };
671 
672 } // end namespace PBQP
673 } // end namespace llvm
674 
675 #endif // LLVM_CODEGEN_PBQP_GRAPH_HPP
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
EdgeIdSet(const Graph &G)
Definition: Graph.h:304
NodeVector::size_type size() const
Definition: Graph.h:294
This class represents lattice values for constants.
Definition: AllocatorList.h:24
NodeEntry::AdjEdgeItr begin() const
Definition: Graph.h:323
EdgeId addEdge(NodeId N1Id, NodeId N2Id, OtherVectorT Costs)
Add an edge between the given nodes with the given costs.
Definition: Graph.h:410
bool empty() const
Returns true if the graph is empty.
Definition: Graph.h:448
typename RegAllocSolverImpl ::EdgeMetadata EdgeMetadata
Definition: Graph.h:59
EdgeId findEdge(NodeId N1Id, NodeId N2Id)
Get the edge connecting two nodes.
Definition: Graph.h:574
NodeVector::size_type size() const
Definition: Graph.h:311
static NodeId invalidNodeId()
Returns a value representing an invalid (non-existent) node.
Definition: Graph.h:33
bool operator==(const NodeItr &O) const
Definition: Graph.h:244
typename CostAllocator::VectorPtr VectorPtr
Definition: Graph.h:56
NodeItr(NodeId CurNId, const Graph &G)
Definition: Graph.h:239
bool operator!=(const NodeItr &O) const
Definition: Graph.h:245
void reconnectEdge(EdgeId EId, NodeId NId)
Re-attach an edge to its nodes.
Definition: Graph.h:645
void updateEdgeCosts(EdgeId EId, OtherMatrixT Costs)
Update an edge&#39;s cost matrix.
Definition: Graph.h:509
NodeId addEdgeBypassingCostAllocator(NodeId N1Id, NodeId N2Id, OtherMatrixPtrT Costs)
Add an edge bypassing the cost allocator.
Definition: Graph.h:435
NodeId getEdgeNode1Id(EdgeId EId) const
Get the first node connected to this edge.
Definition: Graph.h:546
EdgeIdSet edgeIds() const
Definition: Graph.h:451
NodeItr end() const
Definition: Graph.h:290
Live Register Matrix
NodeMetadata & getNodeMetadata(NodeId NId)
Definition: Graph.h:493
Definition: BitVector.h:938
void disconnectEdge(EdgeId EId, NodeId NId)
Disconnect an edge from the given node.
Definition: Graph.h:626
typename RegAllocSolverImpl ::GraphMetadata GraphMetadata
Definition: Graph.h:60
const EdgeMetadata & getEdgeMetadata(EdgeId EId) const
Definition: Graph.h:539
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:784
NodeIdSet nodeIds() const
Definition: Graph.h:450
NodeEntry::AdjEdgeList::size_type size() const
Definition: Graph.h:333
NodeEntry::AdjEdgeItr end() const
Definition: Graph.h:327
unsigned getNumNodes() const
Get the number of nodes in the graph.
Definition: Graph.h:457
NodeId getEdgeOtherNodeId(EdgeId EId, NodeId NId)
Get the "other" node connected to this edge.
Definition: Graph.h:561
typename RegAllocSolverImpl ::Matrix Matrix
Definition: Graph.h:55
NodeId addNodeBypassingCostAllocator(OtherVectorPtrT Costs)
Add a node bypassing the cost allocator.
Definition: Graph.h:397
const Matrix & getEdgeCosts(EdgeId EId) const
Get an edge&#39;s cost matrix.
Definition: Graph.h:531
void removeNode(NodeId NId)
Remove a node from the graph.
Definition: Graph.h:586
AdjEdgeIdSet adjEdgeIds(NodeId NId)
Definition: Graph.h:453
unsigned NodeId
Definition: Graph.h:29
const MatrixPtr & getEdgeCostsPtr(EdgeId EId) const
Get a MatrixPtr to a node&#39;s cost matrix.
Definition: Graph.h:524
NodeId getEdgeNode2Id(EdgeId EId) const
Get the second node connected to this edge.
Definition: Graph.h:553
unsigned getNumEdges() const
Get the number of edges in the graph.
Definition: Graph.h:461
typename NodeEntry::AdjEdgeItr AdjEdgeItr
Definition: Graph.h:229
const Vector & getNodeCosts(NodeId NId) const
Get a node&#39;s cost vector.
Definition: Graph.h:489
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
NodeId operator*() const
Definition: Graph.h:247
NodeItr begin() const
Definition: Graph.h:289
void setSolver(SolverT &S)
Lock this graph to the given solver instance in preparation for running the solver.
Definition: Graph.h:357
NodeItr & operator++()
Definition: Graph.h:246
EdgeItr begin() const
Definition: Graph.h:306
PBQP Graph class.
Definition: Graph.h:47
void clear()
Remove all nodes and edges from the graph.
Definition: Graph.h:664
bool operator==(const EdgeItr &O) const
Definition: Graph.h:268
EdgeItr(EdgeId CurEId, const Graph &G)
Definition: Graph.h:263
GraphMetadata & getMetadata()
Get a reference to the graph metadata.
Definition: Graph.h:348
void removeEdge(EdgeId EId)
Remove an edge from the graph.
Definition: Graph.h:654
AdjEdgeIdSet(const NodeEntry &NE)
Definition: Graph.h:321
void unsetSolver()
Release from solver instance.
Definition: Graph.h:367
EdgeId operator*() const
Definition: Graph.h:271
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1167
const NodeMetadata & getNodeMetadata(NodeId NId) const
Definition: Graph.h:497
const VectorPtr & getNodeCostsPtr(NodeId NId) const
Get a VectorPtr to a node&#39;s cost vector.
Definition: Graph.h:482
Graph(GraphMetadata Metadata)
Construct an empty PBQP graph with the given graph metadata.
Definition: Graph.h:345
const GraphMetadata & getMetadata() const
Get a const-reference to the graph metadata.
Definition: Graph.h:351
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
NodeEntry::AdjEdgeList::size_type getNodeDegree(NodeId NId) const
Definition: Graph.h:501
EdgeItr & operator++()
Definition: Graph.h:270
bool operator!=(const EdgeItr &O) const
Definition: Graph.h:269
typename RegAllocSolverImpl ::RawVector RawVector
Definition: Graph.h:52
static EdgeId invalidEdgeId()
Returns a value representing an invalid (non-existent) edge.
Definition: Graph.h:38
EdgeItr end() const
Definition: Graph.h:307
void disconnectAllNeighborsFromNode(NodeId NId)
Convenience method to disconnect all neighbours from the given node.
Definition: Graph.h:636
#define N
typename RegAllocSolverImpl ::Vector Vector
Definition: Graph.h:54
EdgeMetadata & getEdgeMetadata(EdgeId EId)
Definition: Graph.h:535
typename CostAllocator::MatrixPtr MatrixPtr
Definition: Graph.h:57
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
typename RegAllocSolverImpl ::NodeMetadata NodeMetadata
Definition: Graph.h:58
NodeIdSet(const Graph &G)
Definition: Graph.h:287
std::forward_iterator_tag iterator_category
Definition: Graph.h:233
typename RegAllocSolverImpl ::RawMatrix RawMatrix
Definition: Graph.h:53
NodeId addNode(OtherVectorT Costs)
Add a node with the given costs.
Definition: Graph.h:376
void setNodeCosts(NodeId NId, OtherVectorT Costs)
Set a node&#39;s cost vector.
Definition: Graph.h:467
std::vector< uint32_t > Metadata
PAL metadata represented as a vector.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1245