File indexing completed on 2026-05-10 08:43:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
0018 #define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
0019
0020 #include "llvm/ADT/DenseSet.h"
0021 #include <algorithm>
0022 #include <cstdint>
0023 #include <memory>
0024
0025 namespace llvm {
0026 namespace PBQP {
0027
0028 template <typename ValueT> class ValuePool {
0029 public:
0030 using PoolRef = std::shared_ptr<const ValueT>;
0031
0032 private:
0033 class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
0034 public:
0035 template <typename ValueKeyT>
0036 PoolEntry(ValuePool &Pool, ValueKeyT Value)
0037 : Pool(Pool), Value(std::move(Value)) {}
0038
0039 ~PoolEntry() { Pool.removeEntry(this); }
0040
0041 const ValueT &getValue() const { return Value; }
0042
0043 private:
0044 ValuePool &Pool;
0045 ValueT Value;
0046 };
0047
0048 class PoolEntryDSInfo {
0049 public:
0050 static inline PoolEntry *getEmptyKey() { return nullptr; }
0051
0052 static inline PoolEntry *getTombstoneKey() {
0053 return reinterpret_cast<PoolEntry *>(static_cast<uintptr_t>(1));
0054 }
0055
0056 template <typename ValueKeyT>
0057 static unsigned getHashValue(const ValueKeyT &C) {
0058 return hash_value(C);
0059 }
0060
0061 static unsigned getHashValue(PoolEntry *P) {
0062 return getHashValue(P->getValue());
0063 }
0064
0065 static unsigned getHashValue(const PoolEntry *P) {
0066 return getHashValue(P->getValue());
0067 }
0068
0069 template <typename ValueKeyT1, typename ValueKeyT2>
0070 static bool isEqual(const ValueKeyT1 &C1, const ValueKeyT2 &C2) {
0071 return C1 == C2;
0072 }
0073
0074 template <typename ValueKeyT>
0075 static bool isEqual(const ValueKeyT &C, PoolEntry *P) {
0076 if (P == getEmptyKey() || P == getTombstoneKey())
0077 return false;
0078 return isEqual(C, P->getValue());
0079 }
0080
0081 static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
0082 if (P1 == getEmptyKey() || P1 == getTombstoneKey())
0083 return P1 == P2;
0084 return isEqual(P1->getValue(), P2);
0085 }
0086 };
0087
0088 using EntrySetT = DenseSet<PoolEntry *, PoolEntryDSInfo>;
0089
0090 EntrySetT EntrySet;
0091
0092 void removeEntry(PoolEntry *P) { EntrySet.erase(P); }
0093
0094 public:
0095 template <typename ValueKeyT> PoolRef getValue(ValueKeyT ValueKey) {
0096 typename EntrySetT::iterator I = EntrySet.find_as(ValueKey);
0097
0098 if (I != EntrySet.end())
0099 return PoolRef((*I)->shared_from_this(), &(*I)->getValue());
0100
0101 auto P = std::make_shared<PoolEntry>(*this, std::move(ValueKey));
0102 EntrySet.insert(P.get());
0103 return PoolRef(P, &P->getValue());
0104 }
0105 };
0106
0107 template <typename VectorT, typename MatrixT> class PoolCostAllocator {
0108 private:
0109 using VectorCostPool = ValuePool<VectorT>;
0110 using MatrixCostPool = ValuePool<MatrixT>;
0111
0112 public:
0113 using Vector = VectorT;
0114 using Matrix = MatrixT;
0115 using VectorPtr = typename VectorCostPool::PoolRef;
0116 using MatrixPtr = typename MatrixCostPool::PoolRef;
0117
0118 template <typename VectorKeyT> VectorPtr getVector(VectorKeyT v) {
0119 return VectorPool.getValue(std::move(v));
0120 }
0121
0122 template <typename MatrixKeyT> MatrixPtr getMatrix(MatrixKeyT m) {
0123 return MatrixPool.getValue(std::move(m));
0124 }
0125
0126 private:
0127 VectorCostPool VectorPool;
0128 MatrixCostPool MatrixPool;
0129 };
0130
0131 }
0132 }
0133
0134 #endif