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
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> static unsigned getHashValueImpl(const Tuple &values) {
183 if constexpr (I == sizeof...(Ts)) {
184 return 0;
185 } else {
186 using EltType = std::tuple_element_t<I, Tuple>;
188 DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
190 }
191 }
192
193 static unsigned getHashValue(const std::tuple<Ts...> &values) {
194 return getHashValueImpl<0>(values);
195 }
196
197 template <std::size_t... Is>
198 static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs,
199 std::index_sequence<Is...>) {
200 return (DenseMapInfo<std::tuple_element_t<Is, Tuple>>::isEqual(
201 std::get<Is>(lhs), std::get<Is>(rhs)) &&
202 ...);
203 }
204
205 static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
206 return isEqualImpl(lhs, rhs, std::index_sequence_for<Ts...>{});
207 }
208};
209
210// Provide DenseMapInfo for enum classes.
211template <typename Enum>
212struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
213 using UnderlyingType = std::underlying_type_t<Enum>;
215
216 // If an enum does not have a "fixed" underlying type, it may be UB to cast
217 // some values of the underlying type to the enum. We use an "extra" constexpr
218 // local to ensure that such UB would trigger "static assertion expression is
219 // not an integral constant expression", rather than runtime UB.
220 //
221 // If you hit this error, you can fix by switching to `enum class`, or adding
222 // an explicit underlying type (e.g. `enum X : int`) to the enum's definition.
223
224 static constexpr Enum getEmptyKey() {
225 constexpr Enum V = static_cast<Enum>(Info::getEmptyKey());
226 return V;
227 }
228
229 static constexpr Enum getTombstoneKey() {
230 constexpr Enum V = static_cast<Enum>(Info::getTombstoneKey());
231 return V;
232 }
233
234 static unsigned getHashValue(const Enum &Val) {
235 return Info::getHashValue(static_cast<UnderlyingType>(Val));
236 }
237
238 static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
239};
240
241template <typename T> struct DenseMapInfo<std::optional<T>> {
242 using Optional = std::optional<T>;
244
245 static constexpr Optional getEmptyKey() { return {Info::getEmptyKey()}; }
246
247 static constexpr Optional getTombstoneKey() {
248 return {Info::getTombstoneKey()};
249 }
250
251 static unsigned getHashValue(const Optional &OptionalVal) {
253 OptionalVal.has_value(),
254 Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
255 }
256
257 static bool isEqual(const Optional &LHS, const Optional &RHS) {
258 if (LHS && RHS) {
259 return Info::isEqual(LHS.value(), RHS.value());
260 }
261 return !LHS && !RHS;
262 }
263};
264} // end namespace llvm
265
266#endif // LLVM_ADT_DENSEMAPINFO_H
static unsigned getHashValueImpl(SimpleValue Val)
Definition EarlyCSE.cpp:233
static bool isEqualImpl(SimpleValue LHS, SimpleValue RHS)
Definition EarlyCSE.cpp:354
#define I(x, y, z)
Definition MD5.cpp:58
#define T
Value * RHS
Value * LHS
uint64_t mix(uint64_t x)
unsigned combineHashValue(unsigned a, unsigned b)
Simplistic combination of 32-bit hash values into 32-bit hash values.
This is an optimization pass for GlobalISel generic memory operations.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
static constexpr T * getTombstoneKey()
static constexpr uintptr_t Log2MaxAlign
static constexpr T * getEmptyKey()
static unsigned getHashValue(const T *PtrVal)
static bool isEqual(const T *LHS, const T *RHS)
static bool isEqual(const char &LHS, const char &RHS)
static constexpr char getEmptyKey()
static unsigned getHashValue(const char &Val)
static constexpr char getTombstoneKey()
static constexpr Optional getEmptyKey()
static constexpr Optional getTombstoneKey()
static unsigned getHashValue(const Optional &OptionalVal)
static bool isEqual(const Optional &LHS, const Optional &RHS)
static unsigned getHashValuePiecewise(const T &First, const U &Second)
static unsigned getHashValue(const Pair &PairVal)
static constexpr Pair getTombstoneKey()
static bool isEqual(const Pair &LHS, const Pair &RHS)
static constexpr Tuple getTombstoneKey()
static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::index_sequence< Is... >)
static unsigned getHashValue(const std::tuple< Ts... > &values)
static bool isEqual(const Tuple &lhs, const Tuple &rhs)
static unsigned getHashValueImpl(const Tuple &values)
An information struct used to provide DenseMap with the various necessary components for a given valu...