<commit_before>//===-- RegAllocPBQP.h ------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the PBQPBuilder interface, for classes which build PBQP
// instances to represent register allocation problems, and the RegAllocPBQP
// interface.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_REGALLOCPBQP_H
#define LLVM_CODEGEN_REGALLOCPBQP_H

#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/PBQPRAConstraint.h"
#include "llvm/CodeGen/PBQP/CostAllocator.h"
#include "llvm/CodeGen/PBQP/ReductionRules.h"
#include "llvm/Support/ErrorHandling.h"

namespace llvm {
namespace PBQP {
namespace RegAlloc {

/// @brief Spill option index.
inline unsigned getSpillOptionIdx() { return 0; }

/// \brief Metadata to speed allocatability test.
///
/// Keeps track of the number of infinities in each row and column.
class MatrixMetadata {
private:
  MatrixMetadata(const MatrixMetadata&);
  void operator=(const MatrixMetadata&);
public:
  MatrixMetadata(const Matrix& M)
    : WorstRow(0), WorstCol(0),
      UnsafeRows(new bool[M.getRows() - 1]()),
      UnsafeCols(new bool[M.getCols() - 1]()) {

    unsigned* ColCounts = new unsigned[M.getCols() - 1]();

    for (unsigned i = 1; i < M.getRows(); ++i) {
      unsigned RowCount = 0;
      for (unsigned j = 1; j < M.getCols(); ++j) {
        if (M[i][j] == std::numeric_limits<PBQPNum>::infinity()) {
          ++RowCount;
          ++ColCounts[j - 1];
          UnsafeRows[i - 1] = true;
          UnsafeCols[j - 1] = true;
        }
      }
      WorstRow = std::max(WorstRow, RowCount);
    }
    unsigned WorstColCountForCurRow =
      *std::max_element(ColCounts, ColCounts + M.getCols() - 1);
    WorstCol = std::max(WorstCol, WorstColCountForCurRow);
    delete[] ColCounts;
  }

  unsigned getWorstRow() const { return WorstRow; }
  unsigned getWorstCol() const { return WorstCol; }
  const bool* getUnsafeRows() const { return UnsafeRows.get(); }
  const bool* getUnsafeCols() const { return UnsafeCols.get(); }

private:
  unsigned WorstRow, WorstCol;
  std::unique_ptr<bool[]> UnsafeRows;
  std::unique_ptr<bool[]> UnsafeCols;
};

/// \brief Holds a vector of the allowed physical regs for a vreg.
class AllowedRegVector {
  friend hash_code hash_value(const AllowedRegVector &);
public:

  AllowedRegVector() : NumOpts(0), Opts(nullptr) {}

  AllowedRegVector(const std::vector<unsigned> &OptVec)
    : NumOpts(OptVec.size()), Opts(new unsigned[NumOpts]) {
    std::copy(OptVec.begin(), OptVec.end(), Opts.get());
  }

  AllowedRegVector(const AllowedRegVector &Other)
    : NumOpts(Other.NumOpts), Opts(new unsigned[NumOpts]) {
    std::copy(Other.Opts.get(), Other.Opts.get() + NumOpts, Opts.get());
  }

  AllowedRegVector(AllowedRegVector &&Other)
    : NumOpts(std::move(Other.NumOpts)), Opts(std::move(Other.Opts)) {}

  AllowedRegVector& operator=(const AllowedRegVector &Other) {
    NumOpts = Other.NumOpts;
    Opts.reset(new unsigned[NumOpts]);
    std::copy(Other.Opts.get(), Other.Opts.get() + NumOpts, Opts.get());
    return *this;
  }

  AllowedRegVector& operator=(AllowedRegVector &&Other) {
    NumOpts = std::move(Other.NumOpts);
    Opts = std::move(Other.Opts);
    return *this;
  }

  unsigned size() const { return NumOpts; }
  unsigned operator[](size_t I) const { return Opts[I]; }

  bool operator==(const AllowedRegVector &Other) const {
    if (NumOpts != Other.NumOpts)
      return false;
    return std::equal(Opts.get(), Opts.get() + NumOpts, Other.Opts.get());
  }

  bool operator!=(const AllowedRegVector &Other) const {
    return !(*this == Other);
  }

private:
  unsigned NumOpts;
  std::unique_ptr<unsigned[]> Opts;
};

inline hash_code hash_value(const AllowedRegVector &OptRegs) {
  unsigned *OStart = OptRegs.Opts.get();
  unsigned *OEnd = OptRegs.Opts.get() + OptRegs.NumOpts;
  return hash_combine(OptRegs.NumOpts,
                      hash_combine_range(OStart, OEnd));
}

/// \brief Holds graph-level metadata relevent to PBQP RA problems.
class GraphMetadata {
private:
  typedef ValuePool<AllowedRegVector> AllowedRegVecPool;
public:

  typedef typename AllowedRegVecPool::PoolRef AllowedRegVecRef;

  GraphMetadata(MachineFunction &MF,
                LiveIntervals &LIS,
                MachineBlockFrequencyInfo &MBFI)
    : MF(MF), LIS(LIS), MBFI(MBFI) {}

  MachineFunction &MF;
  LiveIntervals &LIS;
  MachineBlockFrequencyInfo &MBFI;

  void setNodeIdForVReg(unsigned VReg, GraphBase::NodeId NId) {
    VRegToNodeId[VReg] = NId;
  }

  GraphBase::NodeId getNodeIdForVReg(unsigned VReg) const {
    auto VRegItr = VRegToNodeId.find(VReg);
    if (VRegItr == VRegToNodeId.end())
      return GraphBase::invalidNodeId();
    return VRegItr->second;
  }

  void eraseNodeIdForVReg(unsigned VReg) {
    VRegToNodeId.erase(VReg);
  }<commit_msg>[PBQP] Remove a spurious 'typename' keyword. This was causing an error on MSVC.<commit_after>//===-- RegAllocPBQP.h ------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the PBQPBuilder interface, for classes which build PBQP
// instances to represent register allocation problems, and the RegAllocPBQP
// interface.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_REGALLOCPBQP_H
#define LLVM_CODEGEN_REGALLOCPBQP_H

#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/PBQPRAConstraint.h"
#include "llvm/CodeGen/PBQP/CostAllocator.h"
#include "llvm/CodeGen/PBQP/ReductionRules.h"
#include "llvm/Support/ErrorHandling.h"

namespace llvm {
namespace PBQP {
namespace RegAlloc {

/// @brief Spill option index.
inline unsigned getSpillOptionIdx() { return 0; }

/// \brief Metadata to speed allocatability test.
///
/// Keeps track of the number of infinities in each row and column.
class MatrixMetadata {
private:
  MatrixMetadata(const MatrixMetadata&);
  void operator=(const MatrixMetadata&);
public:
  MatrixMetadata(const Matrix& M)
    : WorstRow(0), WorstCol(0),
      UnsafeRows(new bool[M.getRows() - 1]()),
      UnsafeCols(new bool[M.getCols() - 1]()) {

    unsigned* ColCounts = new unsigned[M.getCols() - 1]();

    for (unsigned i = 1; i < M.getRows(); ++i) {
      unsigned RowCount = 0;
      for (unsigned j = 1; j < M.getCols(); ++j) {
        if (M[i][j] == std::numeric_limits<PBQPNum>::infinity()) {
          ++RowCount;
          ++ColCounts[j - 1];
          UnsafeRows[i - 1] = true;
          UnsafeCols[j - 1] = true;
        }
      }
      WorstRow = std::max(WorstRow, RowCount);
    }
    unsigned WorstColCountForCurRow =
      *std::max_element(ColCounts, ColCounts + M.getCols() - 1);
    WorstCol = std::max(WorstCol, WorstColCountForCurRow);
    delete[] ColCounts;
  }

  unsigned getWorstRow() const { return WorstRow; }
  unsigned getWorstCol() const { return WorstCol; }
  const bool* getUnsafeRows() const { return UnsafeRows.get(); }
  const bool* getUnsafeCols() const { return UnsafeCols.get(); }

private:
  unsigned WorstRow, WorstCol;
  std::unique_ptr<bool[]> UnsafeRows;
  std::unique_ptr<bool[]> UnsafeCols;
};

/// \brief Holds a vector of the allowed physical regs for a vreg.
class AllowedRegVector {
  friend hash_code hash_value(const AllowedRegVector &);
public:

  AllowedRegVector() : NumOpts(0), Opts(nullptr) {}

  AllowedRegVector(const std::vector<unsigned> &OptVec)
    : NumOpts(OptVec.size()), Opts(new unsigned[NumOpts]) {
    std::copy(OptVec.begin(), OptVec.end(), Opts.get());
  }

  AllowedRegVector(const AllowedRegVector &Other)
    : NumOpts(Other.NumOpts), Opts(new unsigned[NumOpts]) {
    std::copy(Other.Opts.get(), Other.Opts.get() + NumOpts, Opts.get());
  }

  AllowedRegVector(AllowedRegVector &&Other)
    : NumOpts(std::move(Other.NumOpts)), Opts(std::move(Other.Opts)) {}

  AllowedRegVector& operator=(const AllowedRegVector &Other) {
    NumOpts = Other.NumOpts;
    Opts.reset(new unsigned[NumOpts]);
    std::copy(Other.Opts.get(), Other.Opts.get() + NumOpts, Opts.get());
    return *this;
  }

  AllowedRegVector& operator=(AllowedRegVector &&Other) {
    NumOpts = std::move(Other.NumOpts);
    Opts = std::move(Other.Opts);
    return *this;
  }

  unsigned size() const { return NumOpts; }
  unsigned operator[](size_t I) const { return Opts[I]; }

  bool operator==(const AllowedRegVector &Other) const {
    if (NumOpts != Other.NumOpts)
      return false;
    return std::equal(Opts.get(), Opts.get() + NumOpts, Other.Opts.get());
  }

  bool operator!=(const AllowedRegVector &Other) const {
    return !(*this == Other);
  }

private:
  unsigned NumOpts;
  std::unique_ptr<unsigned[]> Opts;
};

inline hash_code hash_value(const AllowedRegVector &OptRegs) {
  unsigned *OStart = OptRegs.Opts.get();
  unsigned *OEnd = OptRegs.Opts.get() + OptRegs.NumOpts;
  return hash_combine(OptRegs.NumOpts,
                      hash_combine_range(OStart, OEnd));
}

/// \brief Holds graph-level metadata relevent to PBQP RA problems.
class GraphMetadata {
private:
  typedef ValuePool<AllowedRegVector> AllowedRegVecPool;
public:

  typedef AllowedRegVecPool::PoolRef AllowedRegVecRef;

  GraphMetadata(MachineFunction &MF,
                LiveIntervals &LIS,
                MachineBlockFrequencyInfo &MBFI)
    : MF(MF), LIS(LIS), MBFI(MBFI) {}

  MachineFunction &MF;
  LiveIntervals &LIS;
  MachineBlockFrequencyInfo &MBFI;

  void setNodeIdForVReg(unsigned VReg, GraphBase::NodeId NId) {
    VRegToNodeId[VReg] = NId;
  }

  GraphBase::NodeId getNodeIdForVReg(unsigned VReg) const {
    auto VRegItr = VRegToNodeId.find(VReg);
    if (VRegItr == VRegToNodeId.end())
      return GraphBase::invalidNodeId();
    return VRegItr->second;
  }

  void eraseNodeIdForVReg(unsigned VReg) {
    VRegToNodeId.erase(VReg);
  }
{'fraction_non_alphanumeric': 0.16895110333430544, 'fraction_numerical': 0.0027218819869738505, 'mean_word_length': 3.188925081433225, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '955562', 'n_tokens_mistral': 3440, 'n_tokens_neox': 3394, 'n_words': 755}