From: Lang Hames Date: Mon, 20 Oct 2014 04:26:23 +0000 (+0000) Subject: [PBQP] Use DenseSet rather than std::set for PBQP's PoolCostAllocator X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=96fc0d298c2ae67f8d68de6ffcf068f2c7162346;hp=35c4e071bef11d3e6d193a1fa337788a99bbce3d [PBQP] Use DenseSet rather than std::set for PBQP's PoolCostAllocator implementation. This is good for a ~6% reduction in total compile time on the nightly test suite when running with -regalloc=pbqp. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220183 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/CodeGen/PBQP/CostAllocator.h b/include/llvm/CodeGen/PBQP/CostAllocator.h index 281ddfcb94f..8c86a700cf6 100644 --- a/include/llvm/CodeGen/PBQP/CostAllocator.h +++ b/include/llvm/CodeGen/PBQP/CostAllocator.h @@ -18,17 +18,20 @@ #ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H #define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H +#include "llvm/ADT/DenseSet.h" #include -#include #include namespace llvm { namespace PBQP { -template +template class CostPool { public: + typedef std::shared_ptr PoolRef; + +private: + class PoolEntry : public std::enable_shared_from_this { public: template @@ -42,27 +45,49 @@ public: CostT cost; }; - typedef std::shared_ptr PoolRef; - -private: - class EntryComparator { + class PoolEntryDSInfo { public: + static inline PoolEntry* getEmptyKey() { return nullptr; } + + static inline PoolEntry* getTombstoneKey() { + return reinterpret_cast(static_cast(1)); + } + + template + 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 + static + bool isEqual(const CostKeyT1 &C1, const CostKeyT2 &C2) { + return C1 == C2; + } + template - typename std::enable_if< - !std::is_same::type>::value, - bool>::type - operator()(const PoolEntry* a, const CostKeyT &b) { - return compare(a->getCost(), b); + static bool isEqual(const CostKeyT &C, PoolEntry *P) { + if (P == getEmptyKey() || P == getTombstoneKey()) + return false; + return isEqual(C, P->getCost()); } - bool operator()(const PoolEntry* a, const PoolEntry* b) { - return compare(a->getCost(), b->getCost()); + + static bool isEqual(PoolEntry *P1, PoolEntry *P2) { + if (P1 == getEmptyKey() || P1 == getTombstoneKey()) + return P1 == P2; + return isEqual(P1->getCost(), P2); } - private: - CostKeyTComparator compare; + }; - typedef std::set EntrySet; + typedef DenseSet EntrySet; EntrySet entrySet; @@ -70,25 +95,22 @@ private: public: template PoolRef getCost(CostKeyT costKey) { - typename EntrySet::iterator itr = - std::lower_bound(entrySet.begin(), entrySet.end(), costKey, - EntryComparator()); + typename EntrySet::iterator itr = entrySet.find_as(costKey); - if (itr != entrySet.end() && costKey == (*itr)->getCost()) + if (itr != entrySet.end()) return PoolRef((*itr)->shared_from_this(), &(*itr)->getCost()); auto p = std::make_shared(*this, std::move(costKey)); - entrySet.insert(itr, p.get()); + entrySet.insert(p.get()); return PoolRef(std::move(p), &p->getCost()); } }; -template +template class PoolCostAllocator { private: - typedef CostPool VectorCostPool; - typedef CostPool MatrixCostPool; + typedef CostPool VectorCostPool; + typedef CostPool MatrixCostPool; public: typedef VectorT Vector; typedef MatrixT Matrix; diff --git a/include/llvm/CodeGen/PBQP/Math.h b/include/llvm/CodeGen/PBQP/Math.h index 60ad19b7360..2792608e29c 100644 --- a/include/llvm/CodeGen/PBQP/Math.h +++ b/include/llvm/CodeGen/PBQP/Math.h @@ -10,6 +10,7 @@ #ifndef LLVM_CODEGEN_PBQP_MATH_H #define LLVM_CODEGEN_PBQP_MATH_H +#include "llvm/ADT/Hashing.h" #include #include #include @@ -21,7 +22,7 @@ typedef float PBQPNum; /// \brief PBQP Vector class. class Vector { - friend class VectorComparator; + friend hash_code hash_value(const Vector &); public: /// \brief Construct a PBQP vector of the given size. @@ -137,21 +138,12 @@ private: PBQPNum *Data; }; -class VectorComparator { -public: - bool operator()(const Vector &A, const Vector &B) { - if (A.Length < B.Length) - return true; - if (B.Length < A.Length) - return false; - char *AData = reinterpret_cast(A.Data); - char *BData = reinterpret_cast(B.Data); - return std::lexicographical_compare(AData, - AData + A.Length * sizeof(PBQPNum), - BData, - BData + A.Length * sizeof(PBQPNum)); - } -}; +/// \brief Return a hash_value for the given vector. +inline hash_code hash_value(const Vector &V) { + unsigned *VBegin = reinterpret_cast(V.Data); + unsigned *VEnd = reinterpret_cast(V.Data + V.Length); + return hash_combine(V.Length, hash_combine_range(VBegin, VEnd)); +} /// \brief Output a textual representation of the given vector on the given /// output stream. @@ -167,11 +159,10 @@ OStream& operator<<(OStream &OS, const Vector &V) { return OS; } - /// \brief PBQP Matrix class class Matrix { private: - friend class MatrixComparator; + friend hash_code hash_value(const Matrix &); public: /// \brief Construct a PBQP Matrix with the given dimensions. @@ -385,24 +376,12 @@ private: PBQPNum *Data; }; -class MatrixComparator { -public: - bool operator()(const Matrix &A, const Matrix &B) { - if (A.Rows < B.Rows) - return true; - if (B.Rows < A.Rows) - return false; - if (A.Cols < B.Cols) - return true; - if (B.Cols < A.Cols) - return false; - char *AData = reinterpret_cast(A.Data); - char *BData = reinterpret_cast(B.Data); - return std::lexicographical_compare( - AData, AData + (A.Rows * A.Cols * sizeof(PBQPNum)), - BData, BData + (A.Rows * A.Cols * sizeof(PBQPNum))); - } -}; +/// \brief Return a hash_code for the given matrix. +inline hash_code hash_value(const Matrix &M) { + unsigned *MBegin = reinterpret_cast(M.Data); + unsigned *MEnd = reinterpret_cast(M.Data + (M.Rows * M.Cols)); + return hash_combine(M.Rows, M.Cols, hash_combine_range(MBegin, MEnd)); +} /// \brief Output a textual representation of the given matrix on the given /// output stream. @@ -410,7 +389,7 @@ template OStream& operator<<(OStream &OS, const Matrix &M) { assert((M.getRows() != 0) && "Zero-row matrix badness."); for (unsigned i = 0; i < M.getRows(); ++i) - OS << M.getRowAsVector(i); + OS << M.getRowAsVector(i) << "\n"; return OS; } @@ -424,6 +403,11 @@ private: Metadata md; }; +template +inline hash_code hash_value(const MDVector &V) { + return hash_value(static_cast(V)); +} + template class MDMatrix : public Matrix { public: @@ -434,6 +418,11 @@ private: Metadata md; }; +template +inline hash_code hash_value(const MDMatrix &M) { + return hash_value(static_cast(M)); +} + } // namespace PBQP } // namespace llvm diff --git a/include/llvm/CodeGen/RegAllocPBQP.h b/include/llvm/CodeGen/RegAllocPBQP.h index 6ab9bccf28e..3d242f1bebe 100644 --- a/include/llvm/CodeGen/RegAllocPBQP.h +++ b/include/llvm/CodeGen/RegAllocPBQP.h @@ -145,9 +145,7 @@ public: typedef PBQP::Matrix RawMatrix; typedef PBQP::Vector Vector; typedef RAMatrix Matrix; - typedef PBQP::PoolCostAllocator< - Vector, PBQP::VectorComparator, - Matrix, PBQP::MatrixComparator> CostAllocator; + typedef PBQP::PoolCostAllocator CostAllocator; typedef GraphBase::NodeId NodeId; typedef GraphBase::EdgeId EdgeId;