summaryrefslogtreecommitdiff
path: root/include/llvm/CodeGen/PBQP/Solution.h
blob: 091805d00f3e28b70df9f31ffe05b75650923552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===-- Solution.h ------- PBQP Solution ------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// PBQP Solution class.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_PBQP_SOLUTION_H
#define LLVM_CODEGEN_PBQP_SOLUTION_H

#include "Graph.h"
#include "Math.h"
#include <map>

namespace PBQP {

  /// \brief Represents a solution to a PBQP problem.
  ///
  /// To get the selection for each node in the problem use the getSelection method.
  class Solution {
  private:

    typedef std::map<Graph::NodeId, unsigned> SelectionsMap;
    SelectionsMap selections;

    unsigned r0Reductions, r1Reductions, r2Reductions, rNReductions;

  public:

    /// \brief Initialise an empty solution.
    Solution()
      : r0Reductions(0), r1Reductions(0), r2Reductions(0), rNReductions(0) {}

    /// \brief Number of nodes for which selections have been made.
    /// @return Number of nodes for which selections have been made.
    unsigned numNodes() const { return selections.size(); }

    /// \brief Records a reduction via the R0 rule. Should be called from the
    ///        solver only.
    void recordR0() { ++r0Reductions; }

    /// \brief Returns the number of R0 reductions applied to solve the problem.
    unsigned numR0Reductions() const { return r0Reductions; }

    /// \brief Records a reduction via the R1 rule. Should be called from the
    ///        solver only.
    void recordR1() { ++r1Reductions; }

    /// \brief Returns the number of R1 reductions applied to solve the problem.
    unsigned numR1Reductions() const { return r1Reductions; }

    /// \brief Records a reduction via the R2 rule. Should be called from the
    ///        solver only.
    void recordR2() { ++r2Reductions; }

    /// \brief Returns the number of R2 reductions applied to solve the problem.
    unsigned numR2Reductions() const { return r2Reductions; }

    /// \brief Records a reduction via the RN rule. Should be called from the
    ///        solver only.
    void recordRN() { ++ rNReductions; }

    /// \brief Returns the number of RN reductions applied to solve the problem.
    unsigned numRNReductions() const { return rNReductions; }

    /// \brief Set the selection for a given node.
    /// @param nodeId Node id.
    /// @param selection Selection for nodeId.
    void setSelection(Graph::NodeId nodeId, unsigned selection) {
      selections[nodeId] = selection;
    }

    /// \brief Get a node's selection.
    /// @param nodeId Node id.
    /// @return The selection for nodeId;
    unsigned getSelection(Graph::NodeId nodeId) const {
      SelectionsMap::const_iterator sItr = selections.find(nodeId);
      assert(sItr != selections.end() && "No selection for node.");
      return sItr->second;
    }

  };

}

#endif // LLVM_CODEGEN_PBQP_SOLUTION_H