LLVM 23.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 densemap::detail::mix(reinterpret_cast<uintptr_t>(PtrVal));
88 }
89
90 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
91};
92
93// Provide DenseMapInfo for chars.
94template<> struct DenseMapInfo<char> {
95 static constexpr char getEmptyKey() { return ~0; }
96 static constexpr char getTombstoneKey() { return ~0 - 1; }
97 static unsigned getHashValue(const char& Val) { return Val * 37U; }
98
99 static bool isEqual(const char &LHS, const char &RHS) {
100 return LHS == RHS;
101 }
102};
103
104// Provide DenseMapInfo for all integral types except char.
105//
106// The "char" case is excluded because it uses ~0 as the empty key despite
107// "char" being a signed type. "std::is_same_v<T, char>" is included below
108// for clarity; technically, we do not need it because the explicit
109// specialization above "wins",
110template <typename T>
112 T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, char>>> {
113 static constexpr T getEmptyKey() { return std::numeric_limits<T>::max(); }
114
115 static constexpr T getTombstoneKey() {
116 if constexpr (std::is_unsigned_v<T> || std::is_same_v<T, long>)
117 return std::numeric_limits<T>::max() - 1;
118 else
119 return std::numeric_limits<T>::min();
120 }
121
122 static unsigned getHashValue(const T &Val) {
123 if constexpr (std::is_unsigned_v<T> && sizeof(T) > sizeof(unsigned))
124 return densemap::detail::mix(Val);
125 else
126 return static_cast<unsigned>(Val *
127 static_cast<std::make_unsigned_t<T>>(37U));
128 }
129
130 static bool isEqual(const T &LHS, const T &RHS) { return LHS == RHS; }
131};
132
133// Provide DenseMapInfo for all pairs whose members have info.
134template<typename T, typename U>
135struct DenseMapInfo<std::pair<T, U>> {
136 using Pair = std::pair<T, U>;
139
140 static constexpr Pair getEmptyKey() {
141 return {FirstInfo::getEmptyKey(), SecondInfo::getEmptyKey()};
142 }
143
144 static constexpr Pair getTombstoneKey() {
145 return {FirstInfo::getTombstoneKey(), SecondInfo::getTombstoneKey()};
146 }
147
148 static unsigned getHashValue(const Pair& PairVal) {
149 return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
150 SecondInfo::getHashValue(PairVal.second));
151 }
152
153 // Expose an additional function intended to be used by other
154 // specializations of DenseMapInfo without needing to know how
155 // to combine hash values manually
156 static unsigned getHashValuePiecewise(const T &First, const U &Second) {
157 return detail::combineHashValue(FirstInfo::getHashValue(First),
158 SecondInfo::getHashValue(Second));
159 }
160
161 static bool isEqual(const Pair &LHS, const Pair &RHS) {
162 return FirstInfo::isEqual(LHS.first, RHS.first) &&
163 SecondInfo::isEqual(LHS.second, RHS.second);
164 }
165};
166
167// Provide DenseMapInfo for all tuples whose members have info.
168template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
169 using Tuple = std::tuple<Ts...>;
170
171 static constexpr Tuple getEmptyKey() {
173 }
174
175 static constexpr Tuple getTombstoneKey() {
177 }
178
179 template <unsigned I> static unsigned getHashValueImpl(const Tuple &values) {
180 if constexpr (I == sizeof...(Ts)) {
181 return 0;
182 } else {
183 using EltType = std::tuple_element_t<I, Tuple>;
185 DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
187 }
188 }
189
190 static unsigned getHashValue(const std::tuple<Ts...> &values) {
191 return getHashValueImpl<0>(values);
192 }
193
194 template <std::size_t... Is>
195 static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs,
196 std::index_sequence<Is...>) {
197 return (DenseMapInfo<std::tuple_element_t<Is, Tuple>>::isEqual(
198 std::get<Is>(lhs), std::get<Is>(rhs)) &&
199 ...);
200 }
201
202 static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
203 return isEqualImpl(lhs, rhs, std::index_sequence_for<Ts...>{});
204 }
205};
206
207// Provide DenseMapInfo for enum classes.
208template <typename Enum>
209struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
210 using UnderlyingType = std::underlying_type_t<Enum>;
212
213 // If an enum does not have a "fixed" underlying type, it may be UB to cast
214 // some values of the underlying type to the enum. We use an "extra" constexpr
215 // local to ensure that such UB would trigger "static assertion expression is
216 // not an integral constant expression", rather than runtime UB.
217 //
218 // If you hit this error, you can fix by switching to `enum class`, or adding
219 // an explicit underlying type (e.g. `enum X : int`) to the enum's definition.
220
221 static constexpr Enum getEmptyKey() {
222 constexpr Enum V = static_cast<Enum>(Info::getEmptyKey());
223 return V;
224 }
225
226 static constexpr Enum getTombstoneKey() {
227 constexpr Enum V = static_cast<Enum>(Info::getTombstoneKey());
228 return V;
229 }
230
231 static unsigned getHashValue(const Enum &Val) {
232 return Info::getHashValue(static_cast<UnderlyingType>(Val));
233 }
234
235 static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
236};
237
238template <typename T> struct DenseMapInfo<std::optional<T>> {
239 using Optional = std::optional<T>;
241
242 static constexpr Optional getEmptyKey() { return {Info::getEmptyKey()}; }
243
244 static constexpr Optional getTombstoneKey() {
245 return {Info::getTombstoneKey()};
246 }
247
248 static unsigned getHashValue(const Optional &OptionalVal) {
250 OptionalVal.has_value(),
251 Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
252 }
253
254 static bool isEqual(const Optional &LHS, const Optional &RHS) {
255 if (LHS && RHS) {
256 return Info::isEqual(LHS.value(), RHS.value());
257 }
258 return !LHS && !RHS;
259 }
260};
261} // end namespace llvm
262
263#endif // LLVM_ADT_DENSEMAPINFO_H
static unsigned getHashValueImpl(SimpleValue Val)
Definition EarlyCSE.cpp:229
static bool isEqualImpl(SimpleValue LHS, SimpleValue RHS)
Definition EarlyCSE.cpp:350
#define I(x, y, z)
Definition MD5.cpp:57
#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:74
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874
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...