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>
28 template <typename CostT,
29 typename CostKeyTComparator>
32 class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
34 template <typename CostKeyT>
35 PoolEntry(CostPool &pool, CostKeyT cost)
36 : pool(pool), cost(std::move(cost)) {}
37 ~PoolEntry() { pool.removeEntry(this); }
38 CostT& getCost() { return cost; }
39 const CostT& getCost() const { return cost; }
45 typedef std::shared_ptr<CostT> PoolRef;
48 class EntryComparator {
50 template <typename CostKeyT>
51 typename std::enable_if<
52 !std::is_same<PoolEntry*,
53 typename std::remove_const<CostKeyT>::type>::value,
55 operator()(const PoolEntry* a, const CostKeyT &b) {
56 return compare(a->getCost(), b);
58 bool operator()(const PoolEntry* a, const PoolEntry* b) {
59 return compare(a->getCost(), b->getCost());
62 CostKeyTComparator compare;
65 typedef std::set<PoolEntry*, EntryComparator> EntrySet;
69 void removeEntry(PoolEntry *p) { entrySet.erase(p); }
72 template <typename CostKeyT> PoolRef getCost(CostKeyT costKey) {
73 typename EntrySet::iterator itr =
74 std::lower_bound(entrySet.begin(), entrySet.end(), costKey,
77 if (itr != entrySet.end() && costKey == (*itr)->getCost())
78 return PoolRef((*itr)->shared_from_this(), &(*itr)->getCost());
80 auto p = std::make_shared<PoolEntry>(*this, std::move(costKey));
81 entrySet.insert(itr, p.get());
82 return PoolRef(std::move(p), &p->getCost());
86 template <typename VectorT, typename VectorTComparator,
87 typename MatrixT, typename MatrixTComparator>
88 class PoolCostAllocator {
90 typedef CostPool<VectorT, VectorTComparator> VectorCostPool;
91 typedef CostPool<MatrixT, MatrixTComparator> MatrixCostPool;
93 typedef VectorT Vector;
94 typedef MatrixT Matrix;
95 typedef typename VectorCostPool::PoolRef VectorPtr;
96 typedef typename MatrixCostPool::PoolRef MatrixPtr;
98 template <typename VectorKeyT>
99 VectorPtr getVector(VectorKeyT v) { return vectorPool.getCost(std::move(v)); }
101 template <typename MatrixKeyT>
102 MatrixPtr getMatrix(MatrixKeyT m) { return matrixPool.getCost(std::move(m)); }
104 VectorCostPool vectorPool;
105 MatrixCostPool matrixPool;