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
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 unsigned getHashValue(const T &Val);
57 // static bool isEqual(const T &LHS, const T &RHS);
58};
59
60// Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
61// that are aligned to alignof(T) bytes, but try to avoid requiring T to be
62// complete. This allows clients to instantiate DenseMap<T*, ...> with forward
63// declared key types. Assume that no pointer key type requires more than 4096
64// bytes of alignment.
65template<typename T>
66struct DenseMapInfo<T*> {
67 // The following should hold, but it would require T to be complete:
68 // static_assert(alignof(T) <= (1 << Log2MaxAlign),
69 // "DenseMap does not support pointer keys requiring more than "
70 // "Log2MaxAlign bits of alignment");
71 static constexpr uintptr_t Log2MaxAlign = 12;
72
73 static constexpr T *getEmptyKey() {
74 uintptr_t Val = static_cast<uintptr_t>(-1);
75 Val <<= Log2MaxAlign;
76 return reinterpret_cast<T*>(Val);
77 }
78
79 static unsigned getHashValue(const T *PtrVal) {
80 return densemap::detail::mix(reinterpret_cast<uintptr_t>(PtrVal));
81 }
82
83 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
84};
85
86// Provide DenseMapInfo for chars.
87template<> struct DenseMapInfo<char> {
88 static constexpr char getEmptyKey() { return ~0; }
89 static unsigned getHashValue(const char& Val) { return Val * 37U; }
90
91 static bool isEqual(const char &LHS, const char &RHS) {
92 return LHS == RHS;
93 }
94};
95
96// Provide DenseMapInfo for all integral types except char.
97//
98// The "char" case is excluded because it uses ~0 as the empty key despite
99// "char" being a signed type. "std::is_same_v<T, char>" is included below
100// for clarity; technically, we do not need it because the explicit
101// specialization above "wins",
102template <typename T>
104 T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, char>>> {
105 static constexpr T getEmptyKey() { return std::numeric_limits<T>::max(); }
106
107 static unsigned getHashValue(const T &Val) {
108 if constexpr (std::is_unsigned_v<T> && sizeof(T) > sizeof(unsigned))
109 return densemap::detail::mix(Val);
110 else
111 return static_cast<unsigned>(Val *
112 static_cast<std::make_unsigned_t<T>>(37U));
113 }
114
115 static bool isEqual(const T &LHS, const T &RHS) { return LHS == RHS; }
116};
117
118// Provide DenseMapInfo for all pairs whose members have info.
119template<typename T, typename U>
120struct DenseMapInfo<std::pair<T, U>> {
121 using Pair = std::pair<T, U>;
124
125 static constexpr Pair getEmptyKey() {
126 return {FirstInfo::getEmptyKey(), SecondInfo::getEmptyKey()};
127 }
128
129 static unsigned getHashValue(const Pair& PairVal) {
130 return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
131 SecondInfo::getHashValue(PairVal.second));
132 }
133
134 // Expose an additional function intended to be used by other
135 // specializations of DenseMapInfo without needing to know how
136 // to combine hash values manually
137 static unsigned getHashValuePiecewise(const T &First, const U &Second) {
138 return detail::combineHashValue(FirstInfo::getHashValue(First),
139 SecondInfo::getHashValue(Second));
140 }
141
142 static bool isEqual(const Pair &LHS, const Pair &RHS) {
143 return FirstInfo::isEqual(LHS.first, RHS.first) &&
144 SecondInfo::isEqual(LHS.second, RHS.second);
145 }
146};
147
148// Provide DenseMapInfo for all tuples whose members have info.
149template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
150 using Tuple = std::tuple<Ts...>;
151
152 static constexpr Tuple getEmptyKey() {
154 }
155
156 template <unsigned I> static unsigned getHashValueImpl(const Tuple &values) {
157 if constexpr (I == sizeof...(Ts)) {
158 return 0;
159 } else {
160 using EltType = std::tuple_element_t<I, Tuple>;
162 DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
164 }
165 }
166
167 static unsigned getHashValue(const std::tuple<Ts...> &values) {
168 return getHashValueImpl<0>(values);
169 }
170
171 template <std::size_t... Is>
172 static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs,
173 std::index_sequence<Is...>) {
174 return (DenseMapInfo<std::tuple_element_t<Is, Tuple>>::isEqual(
175 std::get<Is>(lhs), std::get<Is>(rhs)) &&
176 ...);
177 }
178
179 static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
180 return isEqualImpl(lhs, rhs, std::index_sequence_for<Ts...>{});
181 }
182};
183
184// Provide DenseMapInfo for enum classes.
185template <typename Enum>
186struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
187 using UnderlyingType = std::underlying_type_t<Enum>;
189
190 // If an enum does not have a "fixed" underlying type, it may be UB to cast
191 // some values of the underlying type to the enum. We use an "extra" constexpr
192 // local to ensure that such UB would trigger "static assertion expression is
193 // not an integral constant expression", rather than runtime UB.
194 //
195 // If you hit this error, you can fix by switching to `enum class`, or adding
196 // an explicit underlying type (e.g. `enum X : int`) to the enum's definition.
197
198 static constexpr Enum getEmptyKey() {
199 constexpr Enum V = static_cast<Enum>(Info::getEmptyKey());
200 return V;
201 }
202
203 static unsigned getHashValue(const Enum &Val) {
204 return Info::getHashValue(static_cast<UnderlyingType>(Val));
205 }
206
207 static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
208};
209
210template <typename T> struct DenseMapInfo<std::optional<T>> {
211 using Optional = std::optional<T>;
213
214 static constexpr Optional getEmptyKey() { return {Info::getEmptyKey()}; }
215
216 static unsigned getHashValue(const Optional &OptionalVal) {
218 OptionalVal.has_value(),
219 Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
220 }
221
222 static bool isEqual(const Optional &LHS, const Optional &RHS) {
223 if (LHS && RHS) {
224 return Info::isEqual(LHS.value(), RHS.value());
225 }
226 return !LHS && !RHS;
227 }
228};
229} // end namespace llvm
230
231#endif // LLVM_ADT_DENSEMAPINFO_H
static unsigned getHashValueImpl(SimpleValue Val)
Definition EarlyCSE.cpp:224
static bool isEqualImpl(SimpleValue LHS, SimpleValue RHS)
Definition EarlyCSE.cpp:345
#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:861
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 Optional getEmptyKey()
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 bool isEqual(const Pair &LHS, const Pair &RHS)
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...