LLVM 22.0.0git
DenseMapInfo.h
Go to the documentation of this file.
1//===- llvm/ADT/DenseMapInfo.h - Type traits for DenseMap -------*- 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/// \file
10/// This file defines DenseMapInfo traits for DenseMap.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_DENSEMAPINFO_H
15#define LLVM_ADT_DENSEMAPINFO_H
16
17#include <cassert>
18#include <cstddef>
19#include <cstdint>
20#include <limits>
21#include <optional>
22#include <tuple>
23#include <type_traits>
24#include <utility>
25
26namespace llvm {
27
28namespace densemap::detail {
29// A bit mixer with very low latency using one multiplications and one
30// xor-shift. The constant is from splitmix64.
32 x *= 0xbf58476d1ce4e5b9u;
33 x ^= x >> 31;
34 return x;
35}
36} // namespace densemap::detail
37
38namespace detail {
39
40/// Simplistic combination of 32-bit hash values into 32-bit hash values.
41inline unsigned combineHashValue(unsigned a, unsigned b) {
42 uint64_t x = (uint64_t)a << 32 | (uint64_t)b;
43 return (unsigned)densemap::detail::mix(x);
44}
45
46} // end namespace detail
47
48/// An information struct used to provide DenseMap with the various necessary
49/// components for a given value type `T`. `Enable` is an optional additional
50/// parameter that is used to support SFINAE (generally using std::enable_if_t)
51/// in derived DenseMapInfo specializations; in non-SFINAE use cases this should
52/// just be `void`.
53template<typename T, typename Enable = void>
55 // static constexpr T getEmptyKey();
56 // static constexpr T getTombstoneKey();
57 // static unsigned getHashValue(const T &Val);
58 // static bool isEqual(const T &LHS, const T &RHS);
59};
60
61// Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
62// that are aligned to alignof(T) bytes, but try to avoid requiring T to be
63// complete. This allows clients to instantiate DenseMap<T*, ...> with forward
64// declared key types. Assume that no pointer key type requires more than 4096
65// bytes of alignment.
66template<typename T>
67struct DenseMapInfo<T*> {
68 // The following should hold, but it would require T to be complete:
69 // static_assert(alignof(T) <= (1 << Log2MaxAlign),
70 // "DenseMap does not support pointer keys requiring more than "
71 // "Log2MaxAlign bits of alignment");
72 static constexpr uintptr_t Log2MaxAlign = 12;
73
74 static constexpr T *getEmptyKey() {
75 uintptr_t Val = static_cast<uintptr_t>(-1);
76 Val <<= Log2MaxAlign;
77 return reinterpret_cast<T*>(Val);
78 }
79
80 static constexpr T *getTombstoneKey() {
81 uintptr_t Val = static_cast<uintptr_t>(-2);
82 Val <<= Log2MaxAlign;
83 return reinterpret_cast<T*>(Val);
84 }
85
86 static unsigned getHashValue(const T *PtrVal) {
87 return (unsigned((uintptr_t)PtrVal) >> 4) ^
88 (unsigned((uintptr_t)PtrVal) >> 9);
89 }
90
91 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
92};
93
94// Provide DenseMapInfo for chars.
95template<> struct DenseMapInfo<char> {
96 static constexpr char getEmptyKey() { return ~0; }
97 static constexpr char getTombstoneKey() { return ~0 - 1; }
98 static unsigned getHashValue(const char& Val) { return Val * 37U; }
99
100 static bool isEqual(const char &LHS, const char &RHS) {
101 return LHS == RHS;
102 }
103};
104
105// Provide DenseMapInfo for all integral types except char.
106//
107// The "char" case is excluded because it uses ~0 as the empty key despite
108// "char" being a signed type. "std::is_same_v<T, char>" is included below
109// for clarity; technically, we do not need it because the explicit
110// specialization above "wins",
111template <typename T>
113 T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, char>>> {
114 static constexpr T getEmptyKey() { return std::numeric_limits<T>::max(); }
115
116 static constexpr T getTombstoneKey() {
117 if constexpr (std::is_unsigned_v<T> || std::is_same_v<T, long>)
118 return std::numeric_limits<T>::max() - 1;
119 else
120 return std::numeric_limits<T>::min();
121 }
122
123 static unsigned getHashValue(const T &Val) {
124 if constexpr (std::is_unsigned_v<T> && sizeof(T) > sizeof(unsigned))
125 return densemap::detail::mix(Val);
126 else
127 return static_cast<unsigned>(Val *
128 static_cast<std::make_unsigned_t<T>>(37U));
129 }
130
131 static bool isEqual(const T &LHS, const T &RHS) { return LHS == RHS; }
132};
133
134// Provide DenseMapInfo for all pairs whose members have info.
135template<typename T, typename U>
136struct DenseMapInfo<std::pair<T, U>> {
137 using Pair = std::pair<T, U>;
140
141 static constexpr Pair getEmptyKey() {
142 return std::make_pair(FirstInfo::getEmptyKey(),
143 SecondInfo::getEmptyKey());
144 }
145
146 static constexpr Pair getTombstoneKey() {
147 return std::make_pair(FirstInfo::getTombstoneKey(),
148 SecondInfo::getTombstoneKey());
149 }
150
151 static unsigned getHashValue(const Pair& PairVal) {
152 return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
153 SecondInfo::getHashValue(PairVal.second));
154 }
155
156 // Expose an additional function intended to be used by other
157 // specializations of DenseMapInfo without needing to know how
158 // to combine hash values manually
159 static unsigned getHashValuePiecewise(const T &First, const U &Second) {
160 return detail::combineHashValue(FirstInfo::getHashValue(First),
161 SecondInfo::getHashValue(Second));
162 }
163
164 static bool isEqual(const Pair &LHS, const Pair &RHS) {
165 return FirstInfo::isEqual(LHS.first, RHS.first) &&
166 SecondInfo::isEqual(LHS.second, RHS.second);
167 }
168};
169
170// Provide DenseMapInfo for all tuples whose members have info.
171template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
172 using Tuple = std::tuple<Ts...>;
173
174 static constexpr Tuple getEmptyKey() {
176 }
177
178 static constexpr Tuple getTombstoneKey() {
180 }
181
182 template <unsigned I>
183 static unsigned getHashValueImpl(const Tuple &values, std::false_type) {
184 using EltType = std::tuple_element_t<I, Tuple>;
185 std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
188 getHashValueImpl<I + 1>(values, atEnd));
189 }
190
191 template <unsigned I>
192 static unsigned getHashValueImpl(const Tuple &, std::true_type) {
193 return 0;
194 }
195
196 static unsigned getHashValue(const std::tuple<Ts...> &values) {
197 std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
198 return getHashValueImpl<0>(values, atEnd);
199 }
200
201 template <unsigned I>
202 static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type) {
203 using EltType = std::tuple_element_t<I, Tuple>;
204 std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
205 return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs), std::get<I>(rhs)) &&
206 isEqualImpl<I + 1>(lhs, rhs, atEnd);
207 }
208
209 template <unsigned I>
210 static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type) {
211 return true;
212 }
213
214 static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
215 std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
216 return isEqualImpl<0>(lhs, rhs, atEnd);
217 }
218};
219
220// Provide DenseMapInfo for enum classes.
221template <typename Enum>
222struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
223 using UnderlyingType = std::underlying_type_t<Enum>;
225
226 // If an enum does not have a "fixed" underlying type, it may be UB to cast
227 // some values of the underlying type to the enum. We use an "extra" constexpr
228 // local to ensure that such UB would trigger "static assertion expression is
229 // not an integral constant expression", rather than runtime UB.
230 //
231 // If you hit this error, you can fix by switching to `enum class`, or adding
232 // an explicit underlying type (e.g. `enum X : int`) to the enum's definition.
233
234 static constexpr Enum getEmptyKey() {
235 constexpr Enum V = static_cast<Enum>(Info::getEmptyKey());
236 return V;
237 }
238
239 static constexpr Enum getTombstoneKey() {
240 constexpr Enum V = static_cast<Enum>(Info::getTombstoneKey());
241 return V;
242 }
243
244 static unsigned getHashValue(const Enum &Val) {
245 return Info::getHashValue(static_cast<UnderlyingType>(Val));
246 }
247
248 static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
249};
250
251template <typename T> struct DenseMapInfo<std::optional<T>> {
252 using Optional = std::optional<T>;
254
255 static constexpr Optional getEmptyKey() { return {Info::getEmptyKey()}; }
256
257 static constexpr Optional getTombstoneKey() {
258 return {Info::getTombstoneKey()};
259 }
260
261 static unsigned getHashValue(const Optional &OptionalVal) {
263 OptionalVal.has_value(),
264 Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
265 }
266
267 static bool isEqual(const Optional &LHS, const Optional &RHS) {
268 if (LHS && RHS) {
269 return Info::isEqual(LHS.value(), RHS.value());
270 }
271 return !LHS && !RHS;
272 }
273};
274} // end namespace llvm
275
276#endif // LLVM_ADT_DENSEMAPINFO_H
Mark the given Function as meaning that it cannot be changed in any way mark any values that are used as this function s parameters or by its return values(according to Uses) live as well. void DeadArgumentEliminationPass
#define I(x, y, z)
Definition: MD5.cpp:58
#define T
Value * RHS
Value * LHS
uint64_t mix(uint64_t x)
Definition: DenseMapInfo.h:31
unsigned combineHashValue(unsigned a, unsigned b)
Simplistic combination of 32-bit hash values into 32-bit hash values.
Definition: DenseMapInfo.h:41
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
static bool isEqual(const Enum &LHS, const Enum &RHS)
Definition: DenseMapInfo.h:248
static constexpr T * getTombstoneKey()
Definition: DenseMapInfo.h:80
static constexpr T * getEmptyKey()
Definition: DenseMapInfo.h:74
static unsigned getHashValue(const T *PtrVal)
Definition: DenseMapInfo.h:86
static bool isEqual(const T *LHS, const T *RHS)
Definition: DenseMapInfo.h:91
static bool isEqual(const char &LHS, const char &RHS)
Definition: DenseMapInfo.h:100
static constexpr char getEmptyKey()
Definition: DenseMapInfo.h:96
static unsigned getHashValue(const char &Val)
Definition: DenseMapInfo.h:98
static constexpr char getTombstoneKey()
Definition: DenseMapInfo.h:97
static constexpr Optional getEmptyKey()
Definition: DenseMapInfo.h:255
static constexpr Optional getTombstoneKey()
Definition: DenseMapInfo.h:257
static unsigned getHashValue(const Optional &OptionalVal)
Definition: DenseMapInfo.h:261
static bool isEqual(const Optional &LHS, const Optional &RHS)
Definition: DenseMapInfo.h:267
static unsigned getHashValuePiecewise(const T &First, const U &Second)
Definition: DenseMapInfo.h:159
static constexpr Pair getEmptyKey()
Definition: DenseMapInfo.h:141
static unsigned getHashValue(const Pair &PairVal)
Definition: DenseMapInfo.h:151
static constexpr Pair getTombstoneKey()
Definition: DenseMapInfo.h:146
static bool isEqual(const Pair &LHS, const Pair &RHS)
Definition: DenseMapInfo.h:164
static unsigned getHashValueImpl(const Tuple &values, std::false_type)
Definition: DenseMapInfo.h:183
static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type)
Definition: DenseMapInfo.h:202
static constexpr Tuple getTombstoneKey()
Definition: DenseMapInfo.h:178
static unsigned getHashValue(const std::tuple< Ts... > &values)
Definition: DenseMapInfo.h:196
static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type)
Definition: DenseMapInfo.h:210
static bool isEqual(const Tuple &lhs, const Tuple &rhs)
Definition: DenseMapInfo.h:214
static constexpr Tuple getEmptyKey()
Definition: DenseMapInfo.h:174
static unsigned getHashValueImpl(const Tuple &, std::true_type)
Definition: DenseMapInfo.h:192
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54