LLVM 23.0.0git
CostAllocator.h
Go to the documentation of this file.
1//===- CostAllocator.h - PBQP Cost Allocator --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines classes conforming to the PBQP cost value manager concept.
10//
11// Cost value managers are memory managers for PBQP cost values (vectors and
12// matrices). Since PBQP graphs can grow very large (E.g. hundreds of thousands
13// of edges on the largest function in SPEC2006).
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
18#define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
19
20#include "llvm/ADT/DenseSet.h"
21#include <algorithm>
22#include <cstdint>
23#include <memory>
24
25namespace llvm {
26namespace PBQP {
27
28template <typename ValueT> class ValuePool {
29public:
30 using PoolRef = std::shared_ptr<const ValueT>;
31
32private:
33 class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
34 public:
35 template <typename ValueKeyT>
36 PoolEntry(ValuePool &Pool, ValueKeyT Value)
37 : Pool(Pool), Value(std::move(Value)) {}
38
39 ~PoolEntry() { Pool.removeEntry(this); }
40
41 const ValueT &getValue() const { return Value; }
42
43 private:
44 ValuePool &Pool;
45 ValueT Value;
46 };
47
48 class PoolEntryDSInfo {
49 public:
50 static inline PoolEntry *getEmptyKey() { return nullptr; }
51
52 template <typename ValueKeyT>
53 static unsigned getHashValue(const ValueKeyT &C) {
54 return hash_value(C);
55 }
56
57 static unsigned getHashValue(PoolEntry *P) {
58 return getHashValue(P->getValue());
59 }
60
61 static unsigned getHashValue(const PoolEntry *P) {
62 return getHashValue(P->getValue());
63 }
64
65 template <typename ValueKeyT1, typename ValueKeyT2>
66 static bool isEqual(const ValueKeyT1 &C1, const ValueKeyT2 &C2) {
67 return C1 == C2;
68 }
69
70 template <typename ValueKeyT>
71 static bool isEqual(const ValueKeyT &C, PoolEntry *P) {
72 if (P == getEmptyKey())
73 return false;
74 return isEqual(C, P->getValue());
75 }
76
77 static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
78 if (P1 == getEmptyKey())
79 return P1 == P2;
80 return isEqual(P1->getValue(), P2);
81 }
82 };
83
84 using EntrySetT = DenseSet<PoolEntry *, PoolEntryDSInfo>;
85
86 EntrySetT EntrySet;
87
88 void removeEntry(PoolEntry *P) { EntrySet.erase(P); }
89
90public:
91 template <typename ValueKeyT> PoolRef getValue(ValueKeyT ValueKey) {
92 typename EntrySetT::iterator I = EntrySet.find_as(ValueKey);
93
94 if (I != EntrySet.end())
95 return PoolRef((*I)->shared_from_this(), &(*I)->getValue());
96
97 auto P = std::make_shared<PoolEntry>(*this, std::move(ValueKey));
98 EntrySet.insert(P.get());
99 return PoolRef(P, &P->getValue());
100 }
101};
102
103template <typename VectorT, typename MatrixT> class PoolCostAllocator {
104private:
105 using VectorCostPool = ValuePool<VectorT>;
106 using MatrixCostPool = ValuePool<MatrixT>;
107
108public:
109 using Vector = VectorT;
110 using Matrix = MatrixT;
113
114 template <typename VectorKeyT> VectorPtr getVector(VectorKeyT v) {
115 return VectorPool.getValue(std::move(v));
116 }
117
118 template <typename MatrixKeyT> MatrixPtr getMatrix(MatrixKeyT m) {
119 return MatrixPool.getValue(std::move(m));
120 }
121
122private:
123 VectorCostPool VectorPool;
124 MatrixCostPool MatrixPool;
125};
126
127} // end namespace PBQP
128} // end namespace llvm
129
130#endif // LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
This file defines the DenseSet and SmallDenseSet classes.
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
VectorPtr getVector(VectorKeyT v)
typename MatrixCostPool::PoolRef MatrixPtr
MatrixPtr getMatrix(MatrixKeyT m)
typename VectorCostPool::PoolRef VectorPtr
PoolRef getValue(ValueKeyT ValueKey)
std::shared_ptr< const ValueT > PoolRef
LLVM Value Representation.
Definition Value.h:75
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
hash_code hash_value(const Vector &V)
Return a hash_value for the given vector.
Definition Math.h:95
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137