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
23 #include <type_traits>
27 template <typename CostT,
28 typename CostKeyTComparator>
31 class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
33 template <typename CostKeyT>
34 PoolEntry(CostPool &pool, CostKeyT cost)
35 : pool(pool), cost(std::move(cost)) {}
36 ~PoolEntry() { pool.removeEntry(this); }
37 CostT& getCost() { return cost; }
38 const CostT& getCost() const { return cost; }
44 typedef std::shared_ptr<CostT> PoolRef;
47 class EntryComparator {
49 template <typename CostKeyT>
50 typename std::enable_if<
51 !std::is_same<PoolEntry*,
52 typename std::remove_const<CostKeyT>::type>::value,
54 operator()(const PoolEntry* a, const CostKeyT &b) {
55 return compare(a->getCost(), b);
57 bool operator()(const PoolEntry* a, const PoolEntry* b) {
58 return compare(a->getCost(), b->getCost());
61 CostKeyTComparator compare;
64 typedef std::set<PoolEntry*, EntryComparator> EntrySet;
68 void removeEntry(PoolEntry *p) { entrySet.erase(p); }
71 template <typename CostKeyT> PoolRef getCost(CostKeyT costKey) {
72 typename EntrySet::iterator itr =
73 std::lower_bound(entrySet.begin(), entrySet.end(), costKey,
76 if (itr != entrySet.end() && costKey == (*itr)->getCost())
77 return PoolRef((*itr)->shared_from_this(), &(*itr)->getCost());
79 auto p = std::make_shared<PoolEntry>(*this, std::move(costKey));
80 entrySet.insert(itr, p.get());
81 return PoolRef(std::move(p), &p->getCost());
85 template <typename VectorT, typename VectorTComparator,
86 typename MatrixT, typename MatrixTComparator>
87 class PoolCostAllocator {
89 typedef CostPool<VectorT, VectorTComparator> VectorCostPool;
90 typedef CostPool<MatrixT, MatrixTComparator> MatrixCostPool;
92 typedef VectorT Vector;
93 typedef MatrixT Matrix;
94 typedef typename VectorCostPool::PoolRef VectorPtr;
95 typedef typename MatrixCostPool::PoolRef MatrixPtr;
97 template <typename VectorKeyT>
98 VectorPtr getVector(VectorKeyT v) { return vectorPool.getCost(std::move(v)); }
100 template <typename MatrixKeyT>
101 MatrixPtr getMatrix(MatrixKeyT m) { return matrixPool.getCost(std::move(m)); }
103 VectorCostPool vectorPool;
104 MatrixCostPool matrixPool;