LLVM 22.0.0git
Hashing.h
Go to the documentation of this file.
1//===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- 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// This file implements the newly proposed standard C++ interfaces for hashing
10// arbitrary data and building hash functions for user-defined types. This
11// interface was originally proposed in N3333[1] and is currently under review
12// for inclusion in a future TR and/or standard.
13//
14// The primary interfaces provide are comprised of one type and three functions:
15//
16// -- 'hash_code' class is an opaque type representing the hash code for some
17// data. It is the intended product of hashing, and can be used to implement
18// hash tables, checksumming, and other common uses of hashes. It is not an
19// integer type (although it can be converted to one) because it is risky
20// to assume much about the internals of a hash_code. In particular, each
21// execution of the program has a high probability of producing a different
22// hash_code for a given input. Thus their values are not stable to save or
23// persist, and should only be used during the execution for the
24// construction of hashing datastructures.
25//
26// -- 'hash_value' is a function designed to be overloaded for each
27// user-defined type which wishes to be used within a hashing context. It
28// should be overloaded within the user-defined type's namespace and found
29// via ADL. Overloads for primitive types are provided by this library.
30//
31// -- 'hash_combine' and 'hash_combine_range' are functions designed to aid
32// programmers in easily and intuitively combining a set of data into
33// a single hash_code for their object. They should only logically be used
34// within the implementation of a 'hash_value' routine or similar context.
35//
36// Note that 'hash_combine_range' contains very special logic for hashing
37// a contiguous array of integers or pointers. This logic is *extremely* fast,
38// on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were
39// benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys
40// under 32-bytes.
41//
42//===----------------------------------------------------------------------===//
43
44#ifndef LLVM_ADT_HASHING_H
45#define LLVM_ADT_HASHING_H
46
47#include "llvm/ADT/ADL.h"
48#include "llvm/Config/abi-breaking.h"
53#include <algorithm>
54#include <cassert>
55#include <cstring>
56#include <optional>
57#include <string>
58#include <tuple>
59#include <utility>
60
61namespace llvm {
62template <typename T, typename Enable> struct DenseMapInfo;
63
64/// An opaque object representing a hash code.
65///
66/// This object represents the result of hashing some entity. It is intended to
67/// be used to implement hashtables or other hashing-based data structures.
68/// While it wraps and exposes a numeric value, this value should not be
69/// trusted to be stable or predictable across processes or executions.
70///
71/// In order to obtain the hash_code for an object 'x':
72/// \code
73/// using llvm::hash_value;
74/// llvm::hash_code code = hash_value(x);
75/// \endcode
76class hash_code {
77 size_t value;
78
79public:
80 /// Default construct a hash_code.
81 /// Note that this leaves the value uninitialized.
82 hash_code() = default;
83
84 /// Form a hash code directly from a numerical value.
85 hash_code(size_t value) : value(value) {}
86
87 /// Convert the hash code to its numerical value for use.
88 /*explicit*/ operator size_t() const { return value; }
89
90 friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
91 return lhs.value == rhs.value;
92 }
93 friend bool operator!=(const hash_code &lhs, const hash_code &rhs) {
94 return lhs.value != rhs.value;
95 }
96
97 /// Allow a hash_code to be directly run through hash_value.
98 friend size_t hash_value(const hash_code &code) { return code.value; }
99};
100
101/// Compute a hash_code for any integer value.
102///
103/// Note that this function is intended to compute the same hash_code for
104/// a particular value without regard to the pre-promotion type. This is in
105/// contrast to hash_combine which may produce different hash_codes for
106/// differing argument types even if they would implicit promote to a common
107/// type without changing the value.
108template <typename T>
109std::enable_if_t<is_integral_or_enum<T>::value, hash_code> hash_value(T value);
110
111/// Compute a hash_code for a pointer's address.
112///
113/// N.B.: This hashes the *address*. Not the value and not the type.
114template <typename T> hash_code hash_value(const T *ptr);
115
116/// Compute a hash_code for a pair of objects.
117template <typename T, typename U>
118hash_code hash_value(const std::pair<T, U> &arg);
119
120/// Compute a hash_code for a tuple.
121template <typename... Ts>
122hash_code hash_value(const std::tuple<Ts...> &arg);
123
124/// Compute a hash_code for a standard string.
125template <typename T>
126hash_code hash_value(const std::basic_string<T> &arg);
127
128/// Compute a hash_code for a standard string.
129template <typename T> hash_code hash_value(const std::optional<T> &arg);
130
131// All of the implementation details of actually computing the various hash
132// code values are held within this namespace. These routines are included in
133// the header file mainly to allow inlining and constant propagation.
134namespace hashing {
135namespace detail {
136
137inline uint64_t fetch64(const char *p) {
138 uint64_t result;
139 std::memcpy(&result, p, sizeof(result));
141 sys::swapByteOrder(result);
142 return result;
143}
144
145inline uint32_t fetch32(const char *p) {
146 uint32_t result;
147 std::memcpy(&result, p, sizeof(result));
149 sys::swapByteOrder(result);
150 return result;
151}
152
153/// Some primes between 2^63 and 2^64 for various uses.
154static constexpr uint64_t k0 = 0xc3a5c85c97cb3127ULL;
155static constexpr uint64_t k1 = 0xb492b66fbe98f273ULL;
156static constexpr uint64_t k2 = 0x9ae16a3b2f90404fULL;
157static constexpr uint64_t k3 = 0xc949d7c7509e6557ULL;
158
159/// Bitwise right rotate.
160/// Normally this will compile to a single instruction, especially if the
161/// shift is a manifest constant.
162inline uint64_t rotate(uint64_t val, size_t shift) {
163 // Avoid shifting by 64: doing so yields an undefined result.
164 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
165}
166
168 return val ^ (val >> 47);
169}
170
172 // Murmur-inspired hashing.
173 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
174 uint64_t a = (low ^ high) * kMul;
175 a ^= (a >> 47);
176 uint64_t b = (high ^ a) * kMul;
177 b ^= (b >> 47);
178 b *= kMul;
179 return b;
180}
181
182inline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) {
183 uint8_t a = s[0];
184 uint8_t b = s[len >> 1];
185 uint8_t c = s[len - 1];
186 uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
187 uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
188 return shift_mix(y * k2 ^ z * k3 ^ seed) * k2;
189}
190
191inline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) {
192 uint64_t a = fetch32(s);
193 return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4));
194}
195
196inline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) {
197 uint64_t a = fetch64(s);
198 uint64_t b = fetch64(s + len - 8);
199 return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
200}
201
202inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
203 uint64_t a = fetch64(s) * k1;
204 uint64_t b = fetch64(s + 8);
205 uint64_t c = fetch64(s + len - 8) * k2;
206 uint64_t d = fetch64(s + len - 16) * k0;
207 return hash_16_bytes(llvm::rotr<uint64_t>(a - b, 43) +
208 llvm::rotr<uint64_t>(c ^ seed, 30) + d,
209 a + llvm::rotr<uint64_t>(b ^ k3, 20) - c + len + seed);
210}
211
212inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
213 uint64_t z = fetch64(s + 24);
214 uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
215 uint64_t b = llvm::rotr<uint64_t>(a + z, 52);
216 uint64_t c = llvm::rotr<uint64_t>(a, 37);
217 a += fetch64(s + 8);
218 c += llvm::rotr<uint64_t>(a, 7);
219 a += fetch64(s + 16);
220 uint64_t vf = a + z;
221 uint64_t vs = b + llvm::rotr<uint64_t>(a, 31) + c;
222 a = fetch64(s + 16) + fetch64(s + len - 32);
223 z = fetch64(s + len - 8);
224 b = llvm::rotr<uint64_t>(a + z, 52);
225 c = llvm::rotr<uint64_t>(a, 37);
226 a += fetch64(s + len - 24);
227 c += llvm::rotr<uint64_t>(a, 7);
228 a += fetch64(s + len - 16);
229 uint64_t wf = a + z;
230 uint64_t ws = b + llvm::rotr<uint64_t>(a, 31) + c;
231 uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
232 return shift_mix((seed ^ (r * k0)) + vs) * k2;
233}
234
235inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
236 if (length >= 4 && length <= 8)
237 return hash_4to8_bytes(s, length, seed);
238 if (length > 8 && length <= 16)
239 return hash_9to16_bytes(s, length, seed);
240 if (length > 16 && length <= 32)
241 return hash_17to32_bytes(s, length, seed);
242 if (length > 32)
243 return hash_33to64_bytes(s, length, seed);
244 if (length != 0)
245 return hash_1to3_bytes(s, length, seed);
246
247 return k2 ^ seed;
248}
249
250/// The intermediate state used during hashing.
251/// Currently, the algorithm for computing hash codes is based on CityHash and
252/// keeps 56 bytes of arbitrary state.
254 uint64_t h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0;
255
256 /// Create a new hash_state structure and initialize it based on the
257 /// seed and the first 64-byte chunk.
258 /// This effectively performs the initial mix.
259 static hash_state create(const char *s, uint64_t seed) {
260 hash_state state = {0,
261 seed,
262 hash_16_bytes(seed, k1),
263 llvm::rotr<uint64_t>(seed ^ k1, 49),
264 seed * k1,
265 shift_mix(seed),
266 0};
267 state.h6 = hash_16_bytes(state.h4, state.h5);
268 state.mix(s);
269 return state;
270 }
271
272 /// Mix 32-bytes from the input sequence into the 16-bytes of 'a'
273 /// and 'b', including whatever is already in 'a' and 'b'.
274 static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
275 a += fetch64(s);
276 uint64_t c = fetch64(s + 24);
277 b = llvm::rotr<uint64_t>(b + a + c, 21);
278 uint64_t d = a;
279 a += fetch64(s + 8) + fetch64(s + 16);
280 b += llvm::rotr<uint64_t>(a, 44) + d;
281 a += c;
282 }
283
284 /// Mix in a 64-byte buffer of data.
285 /// We mix all 64 bytes even when the chunk length is smaller, but we
286 /// record the actual length.
287 void mix(const char *s) {
288 h0 = llvm::rotr<uint64_t>(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
289 h1 = llvm::rotr<uint64_t>(h1 + h4 + fetch64(s + 48), 42) * k1;
290 h0 ^= h6;
291 h1 += h3 + fetch64(s + 40);
292 h2 = llvm::rotr<uint64_t>(h2 + h5, 33) * k1;
293 h3 = h4 * k1;
294 h4 = h0 + h5;
295 mix_32_bytes(s, h3, h4);
296 h5 = h2 + h6;
297 h6 = h1 + fetch64(s + 16);
298 mix_32_bytes(s + 32, h5, h6);
299 std::swap(h2, h0);
300 }
301
302 /// Compute the final 64-bit hash code value based on the current
303 /// state and the length of bytes hashed.
304 uint64_t finalize(size_t length) {
306 hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
307 }
308};
309
310/// In LLVM_ENABLE_ABI_BREAKING_CHECKS builds, the seed is non-deterministic
311/// per process (address of a function in LLVMSupport) to prevent having users
312/// depend on the particular hash values. On platforms without ASLR, this is
313/// still likely non-deterministic per build.
315#if LLVM_ENABLE_ABI_BREAKING_CHECKS
316 return static_cast<uint64_t>(
317 reinterpret_cast<uintptr_t>(&install_fatal_error_handler));
318#else
319 return 0xff51afd7ed558ccdULL;
320#endif
321}
322
323
324/// Trait to indicate whether a type's bits can be hashed directly.
325///
326/// A type trait which is true if we want to combine values for hashing by
327/// reading the underlying data. It is false if values of this type must
328/// first be passed to hash_value, and the resulting hash_codes combined.
329//
330// FIXME: We want to replace is_integral_or_enum and is_pointer here with
331// a predicate which asserts that comparing the underlying storage of two
332// values of the type for equality is equivalent to comparing the two values
333// for equality. For all the platforms we care about, this holds for integers
334// and pointers, but there are platforms where it doesn't and we would like to
335// support user-defined types which happen to satisfy this property.
336template <typename T> struct is_hashable_data
337 : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
338 std::is_pointer<T>::value) &&
339 64 % sizeof(T) == 0)> {};
340
341// Special case std::pair to detect when both types are viable and when there
342// is no alignment-derived padding in the pair. This is a bit of a lie because
343// std::pair isn't truly POD, but it's close enough in all reasonable
344// implementations for our use case of hashing the underlying data.
345template <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
346 : std::integral_constant<bool, (is_hashable_data<T>::value &&
347 is_hashable_data<U>::value &&
348 (sizeof(T) + sizeof(U)) ==
349 sizeof(std::pair<T, U>))> {};
350
351/// Helper to get the hashable data representation for a type.
352/// This variant is enabled when the type itself can be used.
353template <typename T>
354std::enable_if_t<is_hashable_data<T>::value, T>
356 return value;
357}
358/// Helper to get the hashable data representation for a type.
359/// This variant is enabled when we must first call hash_value and use the
360/// result as our data.
361template <typename T>
362std::enable_if_t<!is_hashable_data<T>::value, size_t>
364 using ::llvm::hash_value;
365 return hash_value(value);
366}
367
368/// Helper to store data from a value into a buffer and advance the
369/// pointer into that buffer.
370///
371/// This routine first checks whether there is enough space in the provided
372/// buffer, and if not immediately returns false. If there is space, it
373/// copies the underlying bytes of value into the buffer, advances the
374/// buffer_ptr past the copied bytes, and returns true.
375template <typename T>
376bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
377 size_t offset = 0) {
378 size_t store_size = sizeof(value) - offset;
379 if (buffer_ptr + store_size > buffer_end)
380 return false;
381 const char *value_data = reinterpret_cast<const char *>(&value);
382 std::memcpy(buffer_ptr, value_data + offset, store_size);
383 buffer_ptr += store_size;
384 return true;
385}
386
387/// Implement the combining of integral values into a hash_code.
388///
389/// This overload is selected when the value type of the iterator is
390/// integral. Rather than computing a hash_code for each object and then
391/// combining them, this (as an optimization) directly combines the integers.
392template <typename InputIteratorT>
393hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
394 const uint64_t seed = get_execution_seed();
395 char buffer[64], *buffer_ptr = buffer;
396 char *const buffer_end = std::end(buffer);
397 while (first != last && store_and_advance(buffer_ptr, buffer_end,
398 get_hashable_data(*first)))
399 ++first;
400 if (first == last)
401 return hash_short(buffer, buffer_ptr - buffer, seed);
402 assert(buffer_ptr == buffer_end);
403
404 hash_state state = state.create(buffer, seed);
405 size_t length = 64;
406 while (first != last) {
407 // Fill up the buffer. We don't clear it, which re-mixes the last round
408 // when only a partial 64-byte chunk is left.
409 buffer_ptr = buffer;
410 while (first != last && store_and_advance(buffer_ptr, buffer_end,
411 get_hashable_data(*first)))
412 ++first;
413
414 // Rotate the buffer if we did a partial fill in order to simulate doing
415 // a mix of the last 64-bytes. That is how the algorithm works when we
416 // have a contiguous byte sequence, and we want to emulate that here.
417 std::rotate(buffer, buffer_ptr, buffer_end);
418
419 // Mix this chunk into the current state.
420 state.mix(buffer);
421 length += buffer_ptr - buffer;
422 };
423
424 return state.finalize(length);
425}
426
427/// Implement the combining of integral values into a hash_code.
428///
429/// This overload is selected when the value type of the iterator is integral
430/// and when the input iterator is actually a pointer. Rather than computing
431/// a hash_code for each object and then combining them, this (as an
432/// optimization) directly combines the integers. Also, because the integers
433/// are stored in contiguous memory, this routine avoids copying each value
434/// and directly reads from the underlying memory.
435template <typename ValueT>
436std::enable_if_t<is_hashable_data<ValueT>::value, hash_code>
438 const uint64_t seed = get_execution_seed();
439 const char *s_begin = reinterpret_cast<const char *>(first);
440 const char *s_end = reinterpret_cast<const char *>(last);
441 const size_t length = std::distance(s_begin, s_end);
442 if (length <= 64)
443 return hash_short(s_begin, length, seed);
444
445 const char *s_aligned_end = s_begin + (length & ~63);
446 hash_state state = state.create(s_begin, seed);
447 s_begin += 64;
448 while (s_begin != s_aligned_end) {
449 state.mix(s_begin);
450 s_begin += 64;
451 }
452 if (length & 63)
453 state.mix(s_end - 64);
454
455 return state.finalize(length);
456}
457
458} // namespace detail
459} // namespace hashing
460
461
462/// Compute a hash_code for a sequence of values.
463///
464/// This hashes a sequence of values. It produces the same hash_code as
465/// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
466/// and is significantly faster given pointers and types which can be hashed as
467/// a sequence of bytes.
468template <typename InputIteratorT>
469hash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
470 return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
471}
472
473// A wrapper for hash_combine_range above.
474template <typename RangeT> hash_code hash_combine_range(RangeT &&R) {
475 return hash_combine_range(adl_begin(R), adl_end(R));
476}
477
478// Implementation details for hash_combine.
479namespace hashing {
480namespace detail {
481
482/// Helper class to manage the recursive combining of hash_combine
483/// arguments.
484///
485/// This class exists to manage the state and various calls involved in the
486/// recursive combining of arguments used in hash_combine. It is particularly
487/// useful at minimizing the code in the recursive calls to ease the pain
488/// caused by a lack of variadic functions.
490 char buffer[64] = {};
493
494public:
495 /// Construct a recursive hash combining helper.
496 ///
497 /// This sets up the state for a recursive hash combine, including getting
498 /// the seed and buffer setup.
501
502 /// Combine one chunk of data into the current in-flight hash.
503 ///
504 /// This merges one chunk of data into the hash. First it tries to buffer
505 /// the data. If the buffer is full, it hashes the buffer into its
506 /// hash_state, empties it, and then merges the new chunk in. This also
507 /// handles cases where the data straddles the end of the buffer.
508 template <typename T>
509 char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
510 if (!store_and_advance(buffer_ptr, buffer_end, data)) {
511 // Check for skew which prevents the buffer from being packed, and do
512 // a partial store into the buffer to fill it. This is only a concern
513 // with the variadic combine because that formation can have varying
514 // argument types.
515 size_t partial_store_size = buffer_end - buffer_ptr;
516 std::memcpy(buffer_ptr, &data, partial_store_size);
517
518 // If the store fails, our buffer is full and ready to hash. We have to
519 // either initialize the hash state (on the first full buffer) or mix
520 // this buffer into the existing hash state. Length tracks the *hashed*
521 // length, not the buffered length.
522 if (length == 0) {
524 length = 64;
525 } else {
526 // Mix this chunk into the current state and bump length up by 64.
528 length += 64;
529 }
530 // Reset the buffer_ptr to the head of the buffer for the next chunk of
531 // data.
532 buffer_ptr = buffer;
533
534 // Try again to store into the buffer -- this cannot fail as we only
535 // store types smaller than the buffer.
536 if (!store_and_advance(buffer_ptr, buffer_end, data,
537 partial_store_size))
538 llvm_unreachable("buffer smaller than stored type");
539 }
540 return buffer_ptr;
541 }
542
543 /// Recursive, variadic combining method.
544 ///
545 /// This function recurses through each argument, combining that argument
546 /// into a single hash.
547 template <typename T, typename ...Ts>
548 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
549 const T &arg, const Ts &...args) {
550 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
551
552 // Recurse to the next argument.
553 return combine(length, buffer_ptr, buffer_end, args...);
554 }
555
556 /// Base case for recursive, variadic combining.
557 ///
558 /// The base case when combining arguments recursively is reached when all
559 /// arguments have been handled. It flushes the remaining buffer and
560 /// constructs a hash_code.
561 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
562 // Check whether the entire set of values fit in the buffer. If so, we'll
563 // use the optimized short hashing routine and skip state entirely.
564 if (length == 0)
565 return hash_short(buffer, buffer_ptr - buffer, seed);
566
567 // Mix the final buffer, rotating it if we did a partial fill in order to
568 // simulate doing a mix of the last 64-bytes. That is how the algorithm
569 // works when we have a contiguous byte sequence, and we want to emulate
570 // that here.
571 std::rotate(buffer, buffer_ptr, buffer_end);
572
573 // Mix this chunk into the current state.
575 length += buffer_ptr - buffer;
576
577 return state.finalize(length);
578 }
579};
580
581} // namespace detail
582} // namespace hashing
583
584/// Combine values into a single hash_code.
585///
586/// This routine accepts a varying number of arguments of any type. It will
587/// attempt to combine them into a single hash_code. For user-defined types it
588/// attempts to call a \see hash_value overload (via ADL) for the type. For
589/// integer and pointer types it directly combines their data into the
590/// resulting hash_code.
591///
592/// The result is suitable for returning from a user's hash_value
593/// *implementation* for their user-defined type. Consumers of a type should
594/// *not* call this routine, they should instead call 'hash_value'.
595template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
596 // Recursively hash each argument using a helper class.
598 return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
599}
600
601// Implementation details for implementations of hash_value overloads provided
602// here.
603namespace hashing {
604namespace detail {
605
606/// Helper to hash the value of a single integer.
607///
608/// Overloads for smaller integer types are not provided to ensure consistent
609/// behavior in the presence of integral promotions. Essentially,
610/// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
612 // Similar to hash_4to8_bytes but using a seed instead of length.
613 const uint64_t seed = get_execution_seed();
614 const char *s = reinterpret_cast<const char *>(&value);
615 const uint64_t a = fetch32(s);
616 return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
617}
618
619} // namespace detail
620} // namespace hashing
621
622// Declared and documented above, but defined here so that any of the hashing
623// infrastructure is available.
624template <typename T>
625std::enable_if_t<is_integral_or_enum<T>::value, hash_code> hash_value(T value) {
626 return ::llvm::hashing::detail::hash_integer_value(
627 static_cast<uint64_t>(value));
628}
629
630// Declared and documented above, but defined here so that any of the hashing
631// infrastructure is available.
632template <typename T> hash_code hash_value(const T *ptr) {
633 return ::llvm::hashing::detail::hash_integer_value(
634 reinterpret_cast<uintptr_t>(ptr));
635}
636
637// Declared and documented above, but defined here so that any of the hashing
638// infrastructure is available.
639template <typename T, typename U>
640hash_code hash_value(const std::pair<T, U> &arg) {
641 return hash_combine(arg.first, arg.second);
642}
643
644template <typename... Ts> hash_code hash_value(const std::tuple<Ts...> &arg) {
645 return std::apply([](const auto &...xs) { return hash_combine(xs...); }, arg);
646}
647
648// Declared and documented above, but defined here so that any of the hashing
649// infrastructure is available.
650template <typename T>
651hash_code hash_value(const std::basic_string<T> &arg) {
652 return hash_combine_range(arg);
653}
654
655template <typename T> hash_code hash_value(const std::optional<T> &arg) {
656 return arg ? hash_combine(true, *arg) : hash_value(false);
657}
658
659template <> struct DenseMapInfo<hash_code, void> {
660 static inline hash_code getEmptyKey() { return hash_code(-1); }
661 static inline hash_code getTombstoneKey() { return hash_code(-2); }
662 static unsigned getHashValue(hash_code val) {
663 return static_cast<unsigned>(size_t(val));
664 }
665 static bool isEqual(hash_code LHS, hash_code RHS) { return LHS == RHS; }
666};
667
668} // namespace llvm
669
670/// Implement std::hash so that hash_code can be used in STL containers.
671namespace std {
672
673template<>
674struct hash<llvm::hash_code> {
675 size_t operator()(llvm::hash_code const& Val) const {
676 return Val;
677 }
678};
679
680} // namespace std;
681
682#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Given that RA is a live value
#define T
nvptx lower args
static Split data
Value * RHS
Value * LHS
An opaque object representing a hash code.
Definition: Hashing.h:76
friend size_t hash_value(const hash_code &code)
Allow a hash_code to be directly run through hash_value.
Definition: Hashing.h:98
friend bool operator==(const hash_code &lhs, const hash_code &rhs)
Definition: Hashing.h:90
friend bool operator!=(const hash_code &lhs, const hash_code &rhs)
Definition: Hashing.h:93
hash_code(size_t value)
Form a hash code directly from a numerical value.
Definition: Hashing.h:85
hash_code()=default
Default construct a hash_code.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:182
bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T &value, size_t offset=0)
Helper to store data from a value into a buffer and advance the pointer into that buffer.
Definition: Hashing.h:376
uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:196
uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:191
hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last)
Implement the combining of integral values into a hash_code.
Definition: Hashing.h:393
std::enable_if_t< is_hashable_data< T >::value, T > get_hashable_data(const T &value)
Helper to get the hashable data representation for a type.
Definition: Hashing.h:355
hash_code hash_integer_value(uint64_t value)
Helper to hash the value of a single integer.
Definition: Hashing.h:611
uint64_t rotate(uint64_t val, size_t shift)
Bitwise right rotate.
Definition: Hashing.h:162
static constexpr uint64_t k2
Definition: Hashing.h:156
uint64_t fetch64(const char *p)
Definition: Hashing.h:137
uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:202
static constexpr uint64_t k1
Definition: Hashing.h:155
uint64_t get_execution_seed()
In LLVM_ENABLE_ABI_BREAKING_CHECKS builds, the seed is non-deterministic per process (address of a fu...
Definition: Hashing.h:314
uint32_t fetch32(const char *p)
Definition: Hashing.h:145
static constexpr uint64_t k3
Definition: Hashing.h:157
uint64_t hash_short(const char *s, size_t length, uint64_t seed)
Definition: Hashing.h:235
static constexpr uint64_t k0
Some primes between 2^63 and 2^64 for various uses.
Definition: Hashing.h:154
uint64_t shift_mix(uint64_t val)
Definition: Hashing.h:167
uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed)
Definition: Hashing.h:212
uint64_t hash_16_bytes(uint64_t low, uint64_t high)
Definition: Hashing.h:171
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:26
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:61
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:137
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
Definition: ADL.h:78
LLVM_ABI void install_fatal_error_handler(fatal_error_handler_t handler, void *user_data=nullptr)
install_fatal_error_handler - Installs a new error handler to be used whenever a serious (non-recover...
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
Definition: ADL.h:86
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:595
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:469
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858
static bool isEqual(hash_code LHS, hash_code RHS)
Definition: Hashing.h:665
static hash_code getEmptyKey()
Definition: Hashing.h:660
static unsigned getHashValue(hash_code val)
Definition: Hashing.h:662
static hash_code getTombstoneKey()
Definition: Hashing.h:661
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54
Helper class to manage the recursive combining of hash_combine arguments.
Definition: Hashing.h:489
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end)
Base case for recursive, variadic combining.
Definition: Hashing.h:561
char * combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data)
Combine one chunk of data into the current in-flight hash.
Definition: Hashing.h:509
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, const T &arg, const Ts &...args)
Recursive, variadic combining method.
Definition: Hashing.h:548
hash_combine_recursive_helper()
Construct a recursive hash combining helper.
Definition: Hashing.h:499
The intermediate state used during hashing.
Definition: Hashing.h:253
static hash_state create(const char *s, uint64_t seed)
Create a new hash_state structure and initialize it based on the seed and the first 64-byte chunk.
Definition: Hashing.h:259
uint64_t finalize(size_t length)
Compute the final 64-bit hash code value based on the current state and the length of bytes hashed.
Definition: Hashing.h:304
static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b)
Mix 32-bytes from the input sequence into the 16-bytes of 'a' and 'b', including whatever is already ...
Definition: Hashing.h:274
void mix(const char *s)
Mix in a 64-byte buffer of data.
Definition: Hashing.h:287
Trait to indicate whether a type's bits can be hashed directly.
Definition: Hashing.h:339
size_t operator()(llvm::hash_code const &Val) const
Definition: Hashing.h:675