LLVM API Documentation
00001 //===---------- CostAllocator.h - PBQP Cost Allocator -----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Defines classes conforming to the PBQP cost value manager concept. 00011 // 00012 // Cost value managers are memory managers for PBQP cost values (vectors and 00013 // matrices). Since PBQP graphs can grow very large (E.g. hundreds of thousands 00014 // of edges on the largest function in SPEC2006). 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H 00019 #define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H 00020 00021 #include <memory> 00022 #include <set> 00023 #include <type_traits> 00024 00025 namespace PBQP { 00026 00027 template <typename CostT, 00028 typename CostKeyTComparator> 00029 class CostPool { 00030 public: 00031 class PoolEntry : public std::enable_shared_from_this<PoolEntry> { 00032 public: 00033 template <typename CostKeyT> 00034 PoolEntry(CostPool &pool, CostKeyT cost) 00035 : pool(pool), cost(std::move(cost)) {} 00036 ~PoolEntry() { pool.removeEntry(this); } 00037 CostT& getCost() { return cost; } 00038 const CostT& getCost() const { return cost; } 00039 private: 00040 CostPool &pool; 00041 CostT cost; 00042 }; 00043 00044 typedef std::shared_ptr<CostT> PoolRef; 00045 00046 private: 00047 class EntryComparator { 00048 public: 00049 template <typename CostKeyT> 00050 typename std::enable_if< 00051 !std::is_same<PoolEntry*, 00052 typename std::remove_const<CostKeyT>::type>::value, 00053 bool>::type 00054 operator()(const PoolEntry* a, const CostKeyT &b) { 00055 return compare(a->getCost(), b); 00056 } 00057 bool operator()(const PoolEntry* a, const PoolEntry* b) { 00058 return compare(a->getCost(), b->getCost()); 00059 } 00060 private: 00061 CostKeyTComparator compare; 00062 }; 00063 00064 typedef std::set<PoolEntry*, EntryComparator> EntrySet; 00065 00066 EntrySet entrySet; 00067 00068 void removeEntry(PoolEntry *p) { entrySet.erase(p); } 00069 00070 public: 00071 template <typename CostKeyT> PoolRef getCost(CostKeyT costKey) { 00072 typename EntrySet::iterator itr = 00073 std::lower_bound(entrySet.begin(), entrySet.end(), costKey, 00074 EntryComparator()); 00075 00076 if (itr != entrySet.end() && costKey == (*itr)->getCost()) 00077 return PoolRef((*itr)->shared_from_this(), &(*itr)->getCost()); 00078 00079 auto p = std::make_shared<PoolEntry>(*this, std::move(costKey)); 00080 entrySet.insert(itr, p.get()); 00081 return PoolRef(std::move(p), &p->getCost()); 00082 } 00083 }; 00084 00085 template <typename VectorT, typename VectorTComparator, 00086 typename MatrixT, typename MatrixTComparator> 00087 class PoolCostAllocator { 00088 private: 00089 typedef CostPool<VectorT, VectorTComparator> VectorCostPool; 00090 typedef CostPool<MatrixT, MatrixTComparator> MatrixCostPool; 00091 public: 00092 typedef VectorT Vector; 00093 typedef MatrixT Matrix; 00094 typedef typename VectorCostPool::PoolRef VectorPtr; 00095 typedef typename MatrixCostPool::PoolRef MatrixPtr; 00096 00097 template <typename VectorKeyT> 00098 VectorPtr getVector(VectorKeyT v) { return vectorPool.getCost(std::move(v)); } 00099 00100 template <typename MatrixKeyT> 00101 MatrixPtr getMatrix(MatrixKeyT m) { return matrixPool.getCost(std::move(m)); } 00102 private: 00103 VectorCostPool vectorPool; 00104 MatrixCostPool matrixPool; 00105 }; 00106 00107 } 00108 00109 #endif