llvm.org GIT mirror llvm / 96fc0d2
[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 Lang Hames 5 years ago
3 changed file(s) with 76 addition(s) and 67 deletion(s). Raw diff Collapse all Expand all
1717 #ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
1818 #define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
1919
20 #include "llvm/ADT/DenseSet.h"
2021 #include
21 #include
2222 #include
2323
2424 namespace llvm {
2525 namespace PBQP {
2626
27 template
28 typename CostKeyTComparator>
27 template >
2928 class CostPool {
3029 public:
30 typedef std::shared_ptr PoolRef;
31
32 private:
33
3134 class PoolEntry : public std::enable_shared_from_this {
3235 public:
3336 template
4144 CostT cost;
4245 };
4346
44 typedef std::shared_ptr PoolRef;
47 class PoolEntryDSInfo {
48 public:
49 static inline PoolEntry* getEmptyKey() { return nullptr; }
4550
46 private:
47 class EntryComparator {
48 public:
51 static inline PoolEntry* getTombstoneKey() {
52 return reinterpret_cast(static_cast(1));
53 }
54
4955 template
50 typename std::enable_if<
51 !std::is_same
52 typename std::remove_const::type>::value,
53 bool>::type
54 operator()(const PoolEntry* a, const CostKeyT &b) {
55 return compare(a->getCost(), b);
56 static unsigned getHashValue(const CostKeyT &C) {
57 return hash_value(C);
5658 }
57 bool operator()(const PoolEntry* a, const PoolEntry* b) {
58 return compare(a->getCost(), b->getCost());
59
60 static unsigned getHashValue(PoolEntry *P) {
61 return getHashValue(P->getCost());
5962 }
60 private:
61 CostKeyTComparator compare;
63
64 static unsigned getHashValue(const PoolEntry *P) {
65 return getHashValue(P->getCost());
66 }
67
68 template
69 static
70 bool isEqual(const CostKeyT1 &C1, const CostKeyT2 &C2) {
71 return C1 == C2;
72 }
73
74 template
75 static bool isEqual(const CostKeyT &C, PoolEntry *P) {
76 if (P == getEmptyKey() || P == getTombstoneKey())
77 return false;
78 return isEqual(C, P->getCost());
79 }
80
81 static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
82 if (P1 == getEmptyKey() || P1 == getTombstoneKey())
83 return P1 == P2;
84 return isEqual(P1->getCost(), P2);
85 }
86
6287 };
6388
64 typedef std::set> EntrySet;
89 typedef DenseSet> EntrySet;
6590
6691 EntrySet entrySet;
6792
6994
7095 public:
7196 template PoolRef getCost(CostKeyT costKey) {
72 typename EntrySet::iterator itr =
73 std::lower_bound(entrySet.begin(), entrySet.end(), costKey,
74 EntryComparator());
97 typename EntrySet::iterator itr = entrySet.find_as(costKey);
7598
76 if (itr != entrySet.end() && costKey == (*itr)->getCost())
99 if (itr != entrySet.end())
77100 return PoolRef((*itr)->shared_from_this(), &(*itr)->getCost());
78101
79102 auto p = std::make_shared(*this, std::move(costKey));
80 entrySet.insert(itr, p.get());
103 entrySet.insert(p.get());
81104 return PoolRef(std::move(p), &p->getCost());
82105 }
83106 };
84107
85 template
86 typename MatrixT, typename MatrixTComparator>
108 template >
87109 class PoolCostAllocator {
88110 private:
89 typedef CostPool VectorCostPool;
90 typedef CostPool<MatrixT, MatrixTComparator> MatrixCostPool;
111 typedef CostPool<VectorT> VectorCostPool;
112 typedef CostPool MatrixCostPool;
91113 public:
92114 typedef VectorT Vector;
93115 typedef MatrixT Matrix;
99 #ifndef LLVM_CODEGEN_PBQP_MATH_H
1010 #define LLVM_CODEGEN_PBQP_MATH_H
1111
12 #include "llvm/ADT/Hashing.h"
1213 #include
1314 #include
1415 #include
2021
2122 /// \brief PBQP Vector class.
2223 class Vector {
23 friend class VectorComparator;
24 friend hash_code hash_value(const Vector &);
2425 public:
2526
2627 /// \brief Construct a PBQP vector of the given size.
136137 PBQPNum *Data;
137138 };
138139
139 class VectorComparator {
140 public:
141 bool operator()(const Vector &A, const Vector &B) {
142 if (A.Length < B.Length)
143 return true;
144 if (B.Length < A.Length)
145 return false;
146 char *AData = reinterpret_cast(A.Data);
147 char *BData = reinterpret_cast(B.Data);
148 return std::lexicographical_compare(AData,
149 AData + A.Length * sizeof(PBQPNum),
150 BData,
151 BData + A.Length * sizeof(PBQPNum));
152 }
153 };
140 /// \brief Return a hash_value for the given vector.
141 inline hash_code hash_value(const Vector &V) {
142 unsigned *VBegin = reinterpret_cast(V.Data);
143 unsigned *VEnd = reinterpret_cast(V.Data + V.Length);
144 return hash_combine(V.Length, hash_combine_range(VBegin, VEnd));
145 }
154146
155147 /// \brief Output a textual representation of the given vector on the given
156148 /// output stream.
166158 return OS;
167159 }
168160
169
170161 /// \brief PBQP Matrix class
171162 class Matrix {
172163 private:
173 friend class MatrixComparator;
164 friend hash_code hash_value(const Matrix &);
174165 public:
175166
176167 /// \brief Construct a PBQP Matrix with the given dimensions.
384375 PBQPNum *Data;
385376 };
386377
387 class MatrixComparator {
388 public:
389 bool operator()(const Matrix &A, const Matrix &B) {
390 if (A.Rows < B.Rows)
391 return true;
392 if (B.Rows < A.Rows)
393 return false;
394 if (A.Cols < B.Cols)
395 return true;
396 if (B.Cols < A.Cols)
397 return false;
398 char *AData = reinterpret_cast(A.Data);
399 char *BData = reinterpret_cast(B.Data);
400 return std::lexicographical_compare(
401 AData, AData + (A.Rows * A.Cols * sizeof(PBQPNum)),
402 BData, BData + (A.Rows * A.Cols * sizeof(PBQPNum)));
403 }
404 };
378 /// \brief Return a hash_code for the given matrix.
379 inline hash_code hash_value(const Matrix &M) {
380 unsigned *MBegin = reinterpret_cast(M.Data);
381 unsigned *MEnd = reinterpret_cast(M.Data + (M.Rows * M.Cols));
382 return hash_combine(M.Rows, M.Cols, hash_combine_range(MBegin, MEnd));
383 }
405384
406385 /// \brief Output a textual representation of the given matrix on the given
407386 /// output stream.
409388 OStream& operator<<(OStream &OS, const Matrix &M) {
410389 assert((M.getRows() != 0) && "Zero-row matrix badness.");
411390 for (unsigned i = 0; i < M.getRows(); ++i)
412 OS << M.getRowAsVector(i);
391 OS << M.getRowAsVector(i) << "\n";
413392 return OS;
414393 }
415394
424403 };
425404
426405 template
406 inline hash_code hash_value(const MDVector &V) {
407 return hash_value(static_cast(V));
408 }
409
410 template
427411 class MDMatrix : public Matrix {
428412 public:
429413 MDMatrix(const Matrix &m) : Matrix(m), md(*this) { }
433417 Metadata md;
434418 };
435419
420 template
421 inline hash_code hash_value(const MDMatrix &M) {
422 return hash_value(static_cast(M));
423 }
424
436425 } // namespace PBQP
437426 } // namespace llvm
438427
144144 typedef PBQP::Matrix RawMatrix;
145145 typedef PBQP::Vector Vector;
146146 typedef RAMatrix Matrix;
147 typedef PBQP::PoolCostAllocator<
148 Vector, PBQP::VectorComparator,
149 Matrix, PBQP::MatrixComparator> CostAllocator;
147 typedef PBQP::PoolCostAllocator> CostAllocator;
150148
151149 typedef GraphBase::NodeId NodeId;
152150 typedef GraphBase::EdgeId EdgeId;