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 template <typename ValueKeyT>
51 static unsigned getHashValue(const ValueKeyT &C) {
52 return hash_value(C);
53 }
54
55 static unsigned getHashValue(PoolEntry *P) {
56 return getHashValue(P->getValue());
57 }
58
59 static unsigned getHashValue(const PoolEntry *P) {
60 return getHashValue(P->getValue());
61 }
62
63 template <typename ValueKeyT1, typename ValueKeyT2>
64 static bool isEqual(const ValueKeyT1 &C1, const ValueKeyT2 &C2) {
65 return C1 == C2;
66 }
67
68 template <typename ValueKeyT>
69 static bool isEqual(const ValueKeyT &C, PoolEntry *P) {
70 return isEqual(C, P->getValue());
71 }
72
73 static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
74 return isEqual(P1->getValue(), P2);
75 }
76 };
77
78 using EntrySetT = DenseSet<PoolEntry *, PoolEntryDSInfo>;
79
80 EntrySetT EntrySet;
81
82 void removeEntry(PoolEntry *P) { EntrySet.erase(P); }
83
84public:
85 template <typename ValueKeyT> PoolRef getValue(ValueKeyT ValueKey) {
86 typename EntrySetT::iterator I = EntrySet.find_as(ValueKey);
87
88 if (I != EntrySet.end())
89 return PoolRef((*I)->shared_from_this(), &(*I)->getValue());
90
91 auto P = std::make_shared<PoolEntry>(*this, std::move(ValueKey));
92 EntrySet.insert(P.get());
93 return PoolRef(P, &P->getValue());
94 }
95};
96
97template <typename VectorT, typename MatrixT> class PoolCostAllocator {
98private:
99 using VectorCostPool = ValuePool<VectorT>;
100 using MatrixCostPool = ValuePool<MatrixT>;
101
102public:
103 using Vector = VectorT;
104 using Matrix = MatrixT;
107
108 template <typename VectorKeyT> VectorPtr getVector(VectorKeyT v) {
109 return VectorPool.getValue(std::move(v));
110 }
111
112 template <typename MatrixKeyT> MatrixPtr getMatrix(MatrixKeyT m) {
113 return MatrixPool.getValue(std::move(m));
114 }
115
116private:
117 VectorCostPool VectorPool;
118 MatrixCostPool MatrixPool;
119};
120
121} // end namespace PBQP
122} // end namespace llvm
123
124#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