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 {FirstInfo::getEmptyKey(), SecondInfo::getEmptyKey()};
143 }
144
145 static constexpr Pair getTombstoneKey() {
146 return {FirstInfo::getTombstoneKey(), SecondInfo::getTombstoneKey()};
147 }
148
149 static unsigned getHashValue(const Pair& PairVal) {
150 return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
151 SecondInfo::getHashValue(PairVal.second));
152 }
153
154 // Expose an additional function intended to be used by other
155 // specializations of DenseMapInfo without needing to know how
156 // to combine hash values manually
157 static unsigned getHashValuePiecewise(const T &First, const U &Second) {
158 return detail::combineHashValue(FirstInfo::getHashValue(First),
159 SecondInfo::getHashValue(Second));
160 }
161
162 static bool isEqual(const Pair &LHS, const Pair &RHS) {
163 return FirstInfo::isEqual(LHS.first, RHS.first) &&
164 SecondInfo::isEqual(LHS.second, RHS.second);
165 }
166};
167
168// Provide DenseMapInfo for all tuples whose members have info.
169template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
170 using Tuple = std::tuple<Ts...>;
171
172 static constexpr Tuple getEmptyKey() {
174 }
175
176 static constexpr Tuple getTombstoneKey() {
178 }
179
180 template <unsigned I> static unsigned getHashValueImpl(const Tuple &values) {
181 if constexpr (I == sizeof...(Ts)) {
182 return 0;
183 } else {
184 using EltType = std::tuple_element_t<I, Tuple>;
186 DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
188 }
189 }
190
191 static unsigned getHashValue(const std::tuple<Ts...> &values) {
192 return getHashValueImpl<0>(values);
193 }
194
195 template <std::size_t... Is>
196 static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs,
197 std::index_sequence<Is...>) {
198 return (DenseMapInfo<std::tuple_element_t<Is, Tuple>>::isEqual(
199 std::get<Is>(lhs), std::get<Is>(rhs)) &&
200 ...);
201 }
202
203 static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
204 return isEqualImpl(lhs, rhs, std::index_sequence_for<Ts...>{});
205 }
206};
207
208// Provide DenseMapInfo for enum classes.
209template <typename Enum>
210struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
211 using UnderlyingType = std::underlying_type_t<Enum>;
213
214 // If an enum does not have a "fixed" underlying type, it may be UB to cast
215 // some values of the underlying type to the enum. We use an "extra" constexpr
216 // local to ensure that such UB would trigger "static assertion expression is
217 // not an integral constant expression", rather than runtime UB.
218 //
219 // If you hit this error, you can fix by switching to `enum class`, or adding
220 // an explicit underlying type (e.g. `enum X : int`) to the enum's definition.
221
222 static constexpr Enum getEmptyKey() {
223 constexpr Enum V = static_cast<Enum>(Info::getEmptyKey());
224 return V;
225 }
226
227 static constexpr Enum getTombstoneKey() {
228 constexpr Enum V = static_cast<Enum>(Info::getTombstoneKey());
229 return V;
230 }
231
232 static unsigned getHashValue(const Enum &Val) {
233 return Info::getHashValue(static_cast<UnderlyingType>(Val));
234 }
235
236 static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
237};
238
239template <typename T> struct DenseMapInfo<std::optional<T>> {
240 using Optional = std::optional<T>;
242
243 static constexpr Optional getEmptyKey() { return {Info::getEmptyKey()}; }
244
245 static constexpr Optional getTombstoneKey() {
246 return {Info::getTombstoneKey()};
247 }
248
249 static unsigned getHashValue(const Optional &OptionalVal) {
251 OptionalVal.has_value(),
252 Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
253 }
254
255 static bool isEqual(const Optional &LHS, const Optional &RHS) {
256 if (LHS && RHS) {
257 return Info::isEqual(LHS.value(), RHS.value());
258 }
259 return !LHS && !RHS;
260 }
261};
262} // end namespace llvm
263
264#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...