CostAllocator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===---------- CostAllocator.h - PBQP Cost Allocator -----------*- 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. // Defines classes conforming to the PBQP cost value manager concept.
  11. //
  12. // Cost value managers are memory managers for PBQP cost values (vectors and
  13. // matrices). Since PBQP graphs can grow very large (E.g. hundreds of thousands
  14. // of edges on the largest function in SPEC2006).
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
  18. #define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
  19. #include "llvm/ADT/DenseSet.h"
  20. #include <memory>
  21. #include <type_traits>
  22. namespace llvm {
  23. namespace PBQP {
  24. template <typename ValueT>
  25. class ValuePool {
  26. public:
  27. typedef std::shared_ptr<const ValueT> PoolRef;
  28. private:
  29. class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
  30. public:
  31. template <typename ValueKeyT>
  32. PoolEntry(ValuePool &Pool, ValueKeyT Value)
  33. : Pool(Pool), Value(std::move(Value)) {}
  34. ~PoolEntry() { Pool.removeEntry(this); }
  35. const ValueT& getValue() const { return Value; }
  36. private:
  37. ValuePool &Pool;
  38. ValueT Value;
  39. };
  40. class PoolEntryDSInfo {
  41. public:
  42. static inline PoolEntry* getEmptyKey() { return nullptr; }
  43. static inline PoolEntry* getTombstoneKey() {
  44. return reinterpret_cast<PoolEntry*>(static_cast<uintptr_t>(1));
  45. }
  46. template <typename ValueKeyT>
  47. static unsigned getHashValue(const ValueKeyT &C) {
  48. return hash_value(C);
  49. }
  50. static unsigned getHashValue(PoolEntry *P) {
  51. return getHashValue(P->getValue());
  52. }
  53. static unsigned getHashValue(const PoolEntry *P) {
  54. return getHashValue(P->getValue());
  55. }
  56. template <typename ValueKeyT1, typename ValueKeyT2>
  57. static
  58. bool isEqual(const ValueKeyT1 &C1, const ValueKeyT2 &C2) {
  59. return C1 == C2;
  60. }
  61. template <typename ValueKeyT>
  62. static bool isEqual(const ValueKeyT &C, PoolEntry *P) {
  63. if (P == getEmptyKey() || P == getTombstoneKey())
  64. return false;
  65. return isEqual(C, P->getValue());
  66. }
  67. static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
  68. if (P1 == getEmptyKey() || P1 == getTombstoneKey())
  69. return P1 == P2;
  70. return isEqual(P1->getValue(), P2);
  71. }
  72. };
  73. typedef DenseSet<PoolEntry*, PoolEntryDSInfo> EntrySetT;
  74. EntrySetT EntrySet;
  75. void removeEntry(PoolEntry *P) { EntrySet.erase(P); }
  76. public:
  77. template <typename ValueKeyT> PoolRef getValue(ValueKeyT ValueKey) {
  78. typename EntrySetT::iterator I = EntrySet.find_as(ValueKey);
  79. if (I != EntrySet.end())
  80. return PoolRef((*I)->shared_from_this(), &(*I)->getValue());
  81. auto P = std::make_shared<PoolEntry>(*this, std::move(ValueKey));
  82. EntrySet.insert(P.get());
  83. return PoolRef(std::move(P), &P->getValue());
  84. }
  85. };
  86. template <typename VectorT, typename MatrixT>
  87. class PoolCostAllocator {
  88. private:
  89. typedef ValuePool<VectorT> VectorCostPool;
  90. typedef ValuePool<MatrixT> MatrixCostPool;
  91. public:
  92. typedef VectorT Vector;
  93. typedef MatrixT Matrix;
  94. typedef typename VectorCostPool::PoolRef VectorPtr;
  95. typedef typename MatrixCostPool::PoolRef MatrixPtr;
  96. template <typename VectorKeyT>
  97. VectorPtr getVector(VectorKeyT v) { return VectorPool.getValue(std::move(v)); }
  98. template <typename MatrixKeyT>
  99. MatrixPtr getMatrix(MatrixKeyT m) { return MatrixPool.getValue(std::move(m)); }
  100. private:
  101. VectorCostPool VectorPool;
  102. MatrixCostPool MatrixPool;
  103. };
  104. } // namespace PBQP
  105. } // namespace llvm
  106. #endif