2014-03-03 18:50:05 +00:00
|
|
|
//===---------- CostAllocator.h - PBQP Cost Allocator -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Defines classes conforming to the PBQP cost value manager concept.
|
|
|
|
//
|
|
|
|
// Cost value managers are memory managers for PBQP cost values (vectors and
|
|
|
|
// matrices). Since PBQP graphs can grow very large (E.g. hundreds of thousands
|
|
|
|
// of edges on the largest function in SPEC2006).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
|
|
|
|
#define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
|
2014-03-03 18:50:05 +00:00
|
|
|
|
2014-10-20 04:26:23 +00:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2014-09-10 23:54:45 +00:00
|
|
|
#include <memory>
|
2014-03-03 18:50:05 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
namespace llvm {
|
2014-03-03 18:50:05 +00:00
|
|
|
namespace PBQP {
|
|
|
|
|
2014-10-20 04:26:23 +00:00
|
|
|
template <typename CostT>
|
2014-03-03 18:50:05 +00:00
|
|
|
class CostPool {
|
|
|
|
public:
|
2014-10-20 04:26:23 +00:00
|
|
|
typedef std::shared_ptr<CostT> PoolRef;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-09-10 23:54:45 +00:00
|
|
|
class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
|
2014-03-03 18:50:05 +00:00
|
|
|
public:
|
|
|
|
template <typename CostKeyT>
|
|
|
|
PoolEntry(CostPool &pool, CostKeyT cost)
|
2014-09-10 23:54:45 +00:00
|
|
|
: pool(pool), cost(std::move(cost)) {}
|
2014-03-03 18:50:05 +00:00
|
|
|
~PoolEntry() { pool.removeEntry(this); }
|
|
|
|
CostT& getCost() { return cost; }
|
|
|
|
const CostT& getCost() const { return cost; }
|
|
|
|
private:
|
|
|
|
CostPool &pool;
|
|
|
|
CostT cost;
|
|
|
|
};
|
|
|
|
|
2014-10-20 04:26:23 +00:00
|
|
|
class PoolEntryDSInfo {
|
2014-03-03 18:50:05 +00:00
|
|
|
public:
|
2014-10-20 04:26:23 +00:00
|
|
|
static inline PoolEntry* getEmptyKey() { return nullptr; }
|
|
|
|
|
|
|
|
static inline PoolEntry* getTombstoneKey() {
|
|
|
|
return reinterpret_cast<PoolEntry*>(static_cast<uintptr_t>(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CostKeyT>
|
|
|
|
static unsigned getHashValue(const CostKeyT &C) {
|
|
|
|
return hash_value(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(PoolEntry *P) {
|
|
|
|
return getHashValue(P->getCost());
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const PoolEntry *P) {
|
|
|
|
return getHashValue(P->getCost());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CostKeyT1, typename CostKeyT2>
|
|
|
|
static
|
|
|
|
bool isEqual(const CostKeyT1 &C1, const CostKeyT2 &C2) {
|
|
|
|
return C1 == C2;
|
|
|
|
}
|
|
|
|
|
2014-03-03 18:50:05 +00:00
|
|
|
template <typename CostKeyT>
|
2014-10-20 04:26:23 +00:00
|
|
|
static bool isEqual(const CostKeyT &C, PoolEntry *P) {
|
|
|
|
if (P == getEmptyKey() || P == getTombstoneKey())
|
|
|
|
return false;
|
|
|
|
return isEqual(C, P->getCost());
|
2014-03-03 18:50:05 +00:00
|
|
|
}
|
2014-10-20 04:26:23 +00:00
|
|
|
|
|
|
|
static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
|
|
|
|
if (P1 == getEmptyKey() || P1 == getTombstoneKey())
|
|
|
|
return P1 == P2;
|
|
|
|
return isEqual(P1->getCost(), P2);
|
2014-03-03 18:50:05 +00:00
|
|
|
}
|
2014-10-20 04:26:23 +00:00
|
|
|
|
2014-03-03 18:50:05 +00:00
|
|
|
};
|
|
|
|
|
2014-10-20 04:26:23 +00:00
|
|
|
typedef DenseSet<PoolEntry*, PoolEntryDSInfo> EntrySet;
|
2014-03-03 18:50:05 +00:00
|
|
|
|
|
|
|
EntrySet entrySet;
|
|
|
|
|
|
|
|
void removeEntry(PoolEntry *p) { entrySet.erase(p); }
|
|
|
|
|
|
|
|
public:
|
2014-09-11 00:08:54 +00:00
|
|
|
template <typename CostKeyT> PoolRef getCost(CostKeyT costKey) {
|
2014-10-20 04:26:23 +00:00
|
|
|
typename EntrySet::iterator itr = entrySet.find_as(costKey);
|
2014-03-03 18:50:05 +00:00
|
|
|
|
2014-10-20 04:26:23 +00:00
|
|
|
if (itr != entrySet.end())
|
2014-09-11 00:08:54 +00:00
|
|
|
return PoolRef((*itr)->shared_from_this(), &(*itr)->getCost());
|
2014-03-03 18:50:05 +00:00
|
|
|
|
2014-09-10 23:54:45 +00:00
|
|
|
auto p = std::make_shared<PoolEntry>(*this, std::move(costKey));
|
2014-10-20 04:26:23 +00:00
|
|
|
entrySet.insert(p.get());
|
2014-09-11 00:08:54 +00:00
|
|
|
return PoolRef(std::move(p), &p->getCost());
|
2014-03-03 18:50:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-20 04:26:23 +00:00
|
|
|
template <typename VectorT, typename MatrixT>
|
2014-03-03 18:50:05 +00:00
|
|
|
class PoolCostAllocator {
|
|
|
|
private:
|
2014-10-20 04:26:23 +00:00
|
|
|
typedef CostPool<VectorT> VectorCostPool;
|
|
|
|
typedef CostPool<MatrixT> MatrixCostPool;
|
2014-03-03 18:50:05 +00:00
|
|
|
public:
|
|
|
|
typedef VectorT Vector;
|
|
|
|
typedef MatrixT Matrix;
|
|
|
|
typedef typename VectorCostPool::PoolRef VectorPtr;
|
|
|
|
typedef typename MatrixCostPool::PoolRef MatrixPtr;
|
|
|
|
|
|
|
|
template <typename VectorKeyT>
|
|
|
|
VectorPtr getVector(VectorKeyT v) { return vectorPool.getCost(std::move(v)); }
|
|
|
|
|
|
|
|
template <typename MatrixKeyT>
|
|
|
|
MatrixPtr getMatrix(MatrixKeyT m) { return matrixPool.getCost(std::move(m)); }
|
|
|
|
private:
|
|
|
|
VectorCostPool vectorPool;
|
|
|
|
MatrixCostPool matrixPool;
|
|
|
|
};
|
|
|
|
|
2014-10-09 18:20:51 +00:00
|
|
|
} // namespace PBQP
|
|
|
|
} // namespace llvm
|
2014-03-03 18:50:05 +00:00
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#endif
|