1 //===---------- CostAllocator.h - PBQP Cost Allocator -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Defines classes conforming to the PBQP cost value manager concept.
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).
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
19 #define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
21 #include "llvm/ADT/DenseSet.h"
23 #include <type_traits>
28 template <typename ValueT>
31 typedef std::shared_ptr<ValueT> PoolRef;
35 class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
37 template <typename ValueKeyT>
38 PoolEntry(ValuePool &Pool, ValueKeyT Value)
39 : Pool(Pool), Value(std::move(Value)) {}
40 ~PoolEntry() { Pool.removeEntry(this); }
41 ValueT& getValue() { return Value; }
42 const ValueT& getValue() const { return Value; }
48 class PoolEntryDSInfo {
50 static inline PoolEntry* getEmptyKey() { return nullptr; }
52 static inline PoolEntry* getTombstoneKey() {
53 return reinterpret_cast<PoolEntry*>(static_cast<uintptr_t>(1));
56 template <typename ValueKeyT>
57 static unsigned getHashValue(const ValueKeyT &C) {
61 static unsigned getHashValue(PoolEntry *P) {
62 return getHashValue(P->getValue());
65 static unsigned getHashValue(const PoolEntry *P) {
66 return getHashValue(P->getValue());
69 template <typename ValueKeyT1, typename ValueKeyT2>
71 bool isEqual(const ValueKeyT1 &C1, const ValueKeyT2 &C2) {
75 template <typename ValueKeyT>
76 static bool isEqual(const ValueKeyT &C, PoolEntry *P) {
77 if (P == getEmptyKey() || P == getTombstoneKey())
79 return isEqual(C, P->getValue());
82 static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
83 if (P1 == getEmptyKey() || P1 == getTombstoneKey())
85 return isEqual(P1->getValue(), P2);
90 typedef DenseSet<PoolEntry*, PoolEntryDSInfo> EntrySetT;
94 void removeEntry(PoolEntry *P) { EntrySet.erase(P); }
97 template <typename ValueKeyT> PoolRef getValue(ValueKeyT ValueKey) {
98 typename EntrySetT::iterator I = EntrySet.find_as(ValueKey);
100 if (I != EntrySet.end())
101 return PoolRef((*I)->shared_from_this(), &(*I)->getValue());
103 auto P = std::make_shared<PoolEntry>(*this, std::move(ValueKey));
104 EntrySet.insert(P.get());
105 return PoolRef(std::move(P), &P->getValue());
109 template <typename VectorT, typename MatrixT>
110 class PoolCostAllocator {
112 typedef ValuePool<VectorT> VectorCostPool;
113 typedef ValuePool<MatrixT> MatrixCostPool;
115 typedef VectorT Vector;
116 typedef MatrixT Matrix;
117 typedef typename VectorCostPool::PoolRef VectorPtr;
118 typedef typename MatrixCostPool::PoolRef MatrixPtr;
120 template <typename VectorKeyT>
121 VectorPtr getVector(VectorKeyT v) { return VectorPool.getValue(std::move(v)); }
123 template <typename MatrixKeyT>
124 MatrixPtr getMatrix(MatrixKeyT m) { return MatrixPool.getValue(std::move(m)); }
126 VectorCostPool VectorPool;
127 MatrixCostPool MatrixPool;