Solution.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===-- Solution.h ------- PBQP Solution ------------------------*- 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 Solution class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_PBQP_SOLUTION_H
  14. #define LLVM_CODEGEN_PBQP_SOLUTION_H
  15. #include "Graph.h"
  16. #include "Math.h"
  17. #include <map>
  18. namespace llvm {
  19. namespace PBQP {
  20. /// \brief Represents a solution to a PBQP problem.
  21. ///
  22. /// To get the selection for each node in the problem use the getSelection method.
  23. class Solution {
  24. private:
  25. typedef std::map<GraphBase::NodeId, unsigned> SelectionsMap;
  26. SelectionsMap selections;
  27. unsigned r0Reductions, r1Reductions, r2Reductions, rNReductions;
  28. public:
  29. /// \brief Initialise an empty solution.
  30. Solution()
  31. : r0Reductions(0), r1Reductions(0), r2Reductions(0), rNReductions(0) {}
  32. /// \brief Number of nodes for which selections have been made.
  33. /// @return Number of nodes for which selections have been made.
  34. unsigned numNodes() const { return selections.size(); }
  35. /// \brief Records a reduction via the R0 rule. Should be called from the
  36. /// solver only.
  37. void recordR0() { ++r0Reductions; }
  38. /// \brief Returns the number of R0 reductions applied to solve the problem.
  39. unsigned numR0Reductions() const { return r0Reductions; }
  40. /// \brief Records a reduction via the R1 rule. Should be called from the
  41. /// solver only.
  42. void recordR1() { ++r1Reductions; }
  43. /// \brief Returns the number of R1 reductions applied to solve the problem.
  44. unsigned numR1Reductions() const { return r1Reductions; }
  45. /// \brief Records a reduction via the R2 rule. Should be called from the
  46. /// solver only.
  47. void recordR2() { ++r2Reductions; }
  48. /// \brief Returns the number of R2 reductions applied to solve the problem.
  49. unsigned numR2Reductions() const { return r2Reductions; }
  50. /// \brief Records a reduction via the RN rule. Should be called from the
  51. /// solver only.
  52. void recordRN() { ++ rNReductions; }
  53. /// \brief Returns the number of RN reductions applied to solve the problem.
  54. unsigned numRNReductions() const { return rNReductions; }
  55. /// \brief Set the selection for a given node.
  56. /// @param nodeId Node id.
  57. /// @param selection Selection for nodeId.
  58. void setSelection(GraphBase::NodeId nodeId, unsigned selection) {
  59. selections[nodeId] = selection;
  60. }
  61. /// \brief Get a node's selection.
  62. /// @param nodeId Node id.
  63. /// @return The selection for nodeId;
  64. unsigned getSelection(GraphBase::NodeId nodeId) const {
  65. SelectionsMap::const_iterator sItr = selections.find(nodeId);
  66. assert(sItr != selections.end() && "No selection for node.");
  67. return sItr->second;
  68. }
  69. };
  70. } // namespace PBQP
  71. } // namespace llvm
  72. #endif // LLVM_CODEGEN_PBQP_SOLUTION_H