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);
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.
352template <typename T> auto get_hashable_data(const T &value) {
353 if constexpr (is_hashable_data<T>::value) {
354 // This variant is enabled when the type itself can be used.
355 return value;
356 } else {
357 // This variant is enabled when we must first call hash_value and use the
358 // result as our data.
359 using ::llvm::hash_value;
360 return static_cast<size_t>(hash_value(value));
361 }
362}
363
364/// Helper to store data from a value into a buffer and advance the
365/// pointer into that buffer.
366///
367/// This routine first checks whether there is enough space in the provided
368/// buffer, and if not immediately returns false. If there is space, it
369/// copies the underlying bytes of value into the buffer, advances the
370/// buffer_ptr past the copied bytes, and returns true.
371template <typename T>
372bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
373 size_t offset = 0) {
374 size_t store_size = sizeof(value) - offset;
375 if (buffer_ptr + store_size > buffer_end)
376 return false;
377 const char *value_data = reinterpret_cast<const char *>(&value);
378 std::memcpy(buffer_ptr, value_data + offset, store_size);
379 buffer_ptr += store_size;
380 return true;
381}
382
383/// Implement the combining of integral values into a hash_code.
384///
385/// This overload is selected when the value type of the iterator is
386/// integral. Rather than computing a hash_code for each object and then
387/// combining them, this (as an optimization) directly combines the integers.
388template <typename InputIteratorT>
389hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
390 const uint64_t seed = get_execution_seed();
391 char buffer[64], *buffer_ptr = buffer;
392 char *const buffer_end = std::end(buffer);
393 while (first != last && store_and_advance(buffer_ptr, buffer_end,
394 get_hashable_data(*first)))
395 ++first;
396 if (first == last)
397 return hash_short(buffer, buffer_ptr - buffer, seed);
398 assert(buffer_ptr == buffer_end);
399
400 hash_state state = state.create(buffer, seed);
401 size_t length = 64;
402 while (first != last) {
403 // Fill up the buffer. We don't clear it, which re-mixes the last round
404 // when only a partial 64-byte chunk is left.
405 buffer_ptr = buffer;
406 while (first != last && store_and_advance(buffer_ptr, buffer_end,
407 get_hashable_data(*first)))
408 ++first;
409
410 // Rotate the buffer if we did a partial fill in order to simulate doing
411 // a mix of the last 64-bytes. That is how the algorithm works when we
412 // have a contiguous byte sequence, and we want to emulate that here.
413 std::rotate(buffer, buffer_ptr, buffer_end);
414
415 // Mix this chunk into the current state.
416 state.mix(buffer);
417 length += buffer_ptr - buffer;
418 };
419
420 return state.finalize(length);
421}
422
423/// Implement the combining of integral values into a hash_code.
424///
425/// This overload is selected when the value type of the iterator is integral
426/// and when the input iterator is actually a pointer. Rather than computing
427/// a hash_code for each object and then combining them, this (as an
428/// optimization) directly combines the integers. Also, because the integers
429/// are stored in contiguous memory, this routine avoids copying each value
430/// and directly reads from the underlying memory.
431template <typename ValueT>
432std::enable_if_t<is_hashable_data<ValueT>::value, hash_code>
434 const uint64_t seed = get_execution_seed();
435 const char *s_begin = reinterpret_cast<const char *>(first);
436 const char *s_end = reinterpret_cast<const char *>(last);
437 const size_t length = std::distance(s_begin, s_end);
438 if (length <= 64)
439 return hash_short(s_begin, length, seed);
440
441 const char *s_aligned_end = s_begin + (length & ~63);
442 hash_state state = state.create(s_begin, seed);
443 s_begin += 64;
444 while (s_begin != s_aligned_end) {
445 state.mix(s_begin);
446 s_begin += 64;
447 }
448 if (length & 63)
449 state.mix(s_end - 64);
450
451 return state.finalize(length);
452}
453
454} // namespace detail
455} // namespace hashing
456
457
458/// Compute a hash_code for a sequence of values.
459///
460/// This hashes a sequence of values. It produces the same hash_code as
461/// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
462/// and is significantly faster given pointers and types which can be hashed as
463/// a sequence of bytes.
464template <typename InputIteratorT>
465hash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
466 return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
467}
468
469// A wrapper for hash_combine_range above.
470template <typename RangeT> hash_code hash_combine_range(RangeT &&R) {
471 return hash_combine_range(adl_begin(R), adl_end(R));
472}
473
474// Implementation details for hash_combine.
475namespace hashing {
476namespace detail {
477
478/// Helper class to manage the recursive combining of hash_combine
479/// arguments.
480///
481/// This class exists to manage the state and various calls involved in the
482/// recursive combining of arguments used in hash_combine. It is particularly
483/// useful at minimizing the code in the recursive calls to ease the pain
484/// caused by a lack of variadic functions.
486 char buffer[64] = {};
489
490public:
491 /// Construct a recursive hash combining helper.
492 ///
493 /// This sets up the state for a recursive hash combine, including getting
494 /// the seed and buffer setup.
497
498 /// Combine one chunk of data into the current in-flight hash.
499 ///
500 /// This merges one chunk of data into the hash. First it tries to buffer
501 /// the data. If the buffer is full, it hashes the buffer into its
502 /// hash_state, empties it, and then merges the new chunk in. This also
503 /// handles cases where the data straddles the end of the buffer.
504 template <typename T>
505 char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
506 if (!store_and_advance(buffer_ptr, buffer_end, data)) {
507 // Check for skew which prevents the buffer from being packed, and do
508 // a partial store into the buffer to fill it. This is only a concern
509 // with the variadic combine because that formation can have varying
510 // argument types.
511 size_t partial_store_size = buffer_end - buffer_ptr;
512 std::memcpy(buffer_ptr, &data, partial_store_size);
513
514 // If the store fails, our buffer is full and ready to hash. We have to
515 // either initialize the hash state (on the first full buffer) or mix
516 // this buffer into the existing hash state. Length tracks the *hashed*
517 // length, not the buffered length.
518 if (length == 0) {
519 state = state.create(buffer, seed);
520 length = 64;
521 } else {
522 // Mix this chunk into the current state and bump length up by 64.
523 state.mix(buffer);
524 length += 64;
525 }
526 // Reset the buffer_ptr to the head of the buffer for the next chunk of
527 // data.
528 buffer_ptr = buffer;
529
530 // Try again to store into the buffer -- this cannot fail as we only
531 // store types smaller than the buffer.
532 if (!store_and_advance(buffer_ptr, buffer_end, data,
533 partial_store_size))
534 llvm_unreachable("buffer smaller than stored type");
535 }
536 return buffer_ptr;
537 }
538
539 /// Recursive, variadic combining method.
540 ///
541 /// This function recurses through each argument, combining that argument
542 /// into a single hash.
543 template <typename T, typename ...Ts>
544 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
545 const T &arg, const Ts &...args) {
546 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
547
548 // Recurse to the next argument.
549 return combine(length, buffer_ptr, buffer_end, args...);
550 }
551
552 /// Base case for recursive, variadic combining.
553 ///
554 /// The base case when combining arguments recursively is reached when all
555 /// arguments have been handled. It flushes the remaining buffer and
556 /// constructs a hash_code.
557 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
558 // Check whether the entire set of values fit in the buffer. If so, we'll
559 // use the optimized short hashing routine and skip state entirely.
560 if (length == 0)
561 return hash_short(buffer, buffer_ptr - buffer, seed);
562
563 // Mix the final buffer, rotating it if we did a partial fill in order to
564 // simulate doing a mix of the last 64-bytes. That is how the algorithm
565 // works when we have a contiguous byte sequence, and we want to emulate
566 // that here.
567 std::rotate(buffer, buffer_ptr, buffer_end);
568
569 // Mix this chunk into the current state.
570 state.mix(buffer);
571 length += buffer_ptr - buffer;
572
573 return state.finalize(length);
574 }
575};
576
577} // namespace detail
578} // namespace hashing
579
580/// Combine values into a single hash_code.
581///
582/// This routine accepts a varying number of arguments of any type. It will
583/// attempt to combine them into a single hash_code. For user-defined types it
584/// attempts to call a \see hash_value overload (via ADL) for the type. For
585/// integer and pointer types it directly combines their data into the
586/// resulting hash_code.
587///
588/// The result is suitable for returning from a user's hash_value
589/// *implementation* for their user-defined type. Consumers of a type should
590/// *not* call this routine, they should instead call 'hash_value'.
591template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
592 // Recursively hash each argument using a helper class.
594 return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
595}
596
597// Implementation details for implementations of hash_value overloads provided
598// here.
599namespace hashing {
600namespace detail {
601
602/// Helper to hash the value of a single integer.
603///
604/// Overloads for smaller integer types are not provided to ensure consistent
605/// behavior in the presence of integral promotions. Essentially,
606/// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
608 // Similar to hash_4to8_bytes but using a seed instead of length.
609 const uint64_t seed = get_execution_seed();
610 const char *s = reinterpret_cast<const char *>(&value);
611 const uint64_t a = fetch32(s);
612 return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
613}
614
615} // namespace detail
616} // namespace hashing
617
618// Declared and documented above, but defined here so that any of the hashing
619// infrastructure is available.
620template <typename T>
621std::enable_if_t<is_integral_or_enum<T>::value, hash_code> hash_value(T value) {
622 return ::llvm::hashing::detail::hash_integer_value(
623 static_cast<uint64_t>(value));
624}
625
626// Declared and documented above, but defined here so that any of the hashing
627// infrastructure is available.
628template <typename T> hash_code hash_value(const T *ptr) {
629 return ::llvm::hashing::detail::hash_integer_value(
630 reinterpret_cast<uintptr_t>(ptr));
631}
632
633// Declared and documented above, but defined here so that any of the hashing
634// infrastructure is available.
635template <typename T, typename U>
636hash_code hash_value(const std::pair<T, U> &arg) {
637 return hash_combine(arg.first, arg.second);
638}
639
640template <typename... Ts> hash_code hash_value(const std::tuple<Ts...> &arg) {
641 return std::apply([](const auto &...xs) { return hash_combine(xs...); }, arg);
642}
643
644// Declared and documented above, but defined here so that any of the hashing
645// infrastructure is available.
646template <typename T>
647hash_code hash_value(const std::basic_string<T> &arg) {
648 return hash_combine_range(arg);
649}
650
651template <typename T> hash_code hash_value(const std::optional<T> &arg) {
652 return arg ? hash_combine(true, *arg) : hash_value(false);
653}
654
655template <> struct DenseMapInfo<hash_code, void> {
656 static inline hash_code getEmptyKey() { return hash_code(-1); }
657 static inline hash_code getTombstoneKey() { return hash_code(-2); }
658 static unsigned getHashValue(hash_code val) {
659 return static_cast<unsigned>(size_t(val));
660 }
661 static bool isEqual(hash_code LHS, hash_code RHS) { return LHS == RHS; }
662};
663
664} // namespace llvm
665
666/// Implement std::hash so that hash_code can be used in STL containers.
667namespace std {
668
669template<>
670struct hash<llvm::hash_code> {
671 size_t operator()(llvm::hash_code const& Val) const {
672 return Val;
673 }
674};
675
676} // namespace std;
677
678#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#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:372
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:389
hash_code hash_integer_value(uint64_t value)
Helper to hash the value of a single integer.
Definition Hashing.h:607
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
auto get_hashable_data(const T &value)
Helper to get the hashable data representation for a type.
Definition Hashing.h:352
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
void swapByteOrder(T &Value)
This is an optimization pass for GlobalISel generic memory operations.
constexpr T rotr(T V, int R)
Definition bit.h:350
hash_code hash_value(const FixedPointSemantics &Val)
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:591
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:465
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
static bool isEqual(hash_code LHS, hash_code RHS)
Definition Hashing.h:661
static unsigned getHashValue(hash_code val)
Definition Hashing.h:658
static hash_code getTombstoneKey()
Definition Hashing.h:657
An information struct used to provide DenseMap with the various necessary components for a given valu...
Helper class to manage the recursive combining of hash_combine arguments.
Definition Hashing.h:485
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end)
Base case for recursive, variadic combining.
Definition Hashing.h:557
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:505
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:544
hash_combine_recursive_helper()
Construct a recursive hash combining helper.
Definition Hashing.h:495
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:671