Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:05

0001 //===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements the newly proposed standard C++ interfaces for hashing
0010 // arbitrary data and building hash functions for user-defined types. This
0011 // interface was originally proposed in N3333[1] and is currently under review
0012 // for inclusion in a future TR and/or standard.
0013 //
0014 // The primary interfaces provide are comprised of one type and three functions:
0015 //
0016 //  -- 'hash_code' class is an opaque type representing the hash code for some
0017 //     data. It is the intended product of hashing, and can be used to implement
0018 //     hash tables, checksumming, and other common uses of hashes. It is not an
0019 //     integer type (although it can be converted to one) because it is risky
0020 //     to assume much about the internals of a hash_code. In particular, each
0021 //     execution of the program has a high probability of producing a different
0022 //     hash_code for a given input. Thus their values are not stable to save or
0023 //     persist, and should only be used during the execution for the
0024 //     construction of hashing datastructures.
0025 //
0026 //  -- 'hash_value' is a function designed to be overloaded for each
0027 //     user-defined type which wishes to be used within a hashing context. It
0028 //     should be overloaded within the user-defined type's namespace and found
0029 //     via ADL. Overloads for primitive types are provided by this library.
0030 //
0031 //  -- 'hash_combine' and 'hash_combine_range' are functions designed to aid
0032 //      programmers in easily and intuitively combining a set of data into
0033 //      a single hash_code for their object. They should only logically be used
0034 //      within the implementation of a 'hash_value' routine or similar context.
0035 //
0036 // Note that 'hash_combine_range' contains very special logic for hashing
0037 // a contiguous array of integers or pointers. This logic is *extremely* fast,
0038 // on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were
0039 // benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys
0040 // under 32-bytes.
0041 //
0042 //===----------------------------------------------------------------------===//
0043 
0044 #ifndef LLVM_ADT_HASHING_H
0045 #define LLVM_ADT_HASHING_H
0046 
0047 #include "llvm/Config/abi-breaking.h"
0048 #include "llvm/Support/DataTypes.h"
0049 #include "llvm/Support/ErrorHandling.h"
0050 #include "llvm/Support/SwapByteOrder.h"
0051 #include "llvm/Support/type_traits.h"
0052 #include <algorithm>
0053 #include <cassert>
0054 #include <cstring>
0055 #include <optional>
0056 #include <string>
0057 #include <tuple>
0058 #include <utility>
0059 
0060 namespace llvm {
0061 template <typename T, typename Enable> struct DenseMapInfo;
0062 
0063 /// An opaque object representing a hash code.
0064 ///
0065 /// This object represents the result of hashing some entity. It is intended to
0066 /// be used to implement hashtables or other hashing-based data structures.
0067 /// While it wraps and exposes a numeric value, this value should not be
0068 /// trusted to be stable or predictable across processes or executions.
0069 ///
0070 /// In order to obtain the hash_code for an object 'x':
0071 /// \code
0072 ///   using llvm::hash_value;
0073 ///   llvm::hash_code code = hash_value(x);
0074 /// \endcode
0075 class hash_code {
0076   size_t value;
0077 
0078 public:
0079   /// Default construct a hash_code.
0080   /// Note that this leaves the value uninitialized.
0081   hash_code() = default;
0082 
0083   /// Form a hash code directly from a numerical value.
0084   hash_code(size_t value) : value(value) {}
0085 
0086   /// Convert the hash code to its numerical value for use.
0087   /*explicit*/ operator size_t() const { return value; }
0088 
0089   friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
0090     return lhs.value == rhs.value;
0091   }
0092   friend bool operator!=(const hash_code &lhs, const hash_code &rhs) {
0093     return lhs.value != rhs.value;
0094   }
0095 
0096   /// Allow a hash_code to be directly run through hash_value.
0097   friend size_t hash_value(const hash_code &code) { return code.value; }
0098 };
0099 
0100 /// Compute a hash_code for any integer value.
0101 ///
0102 /// Note that this function is intended to compute the same hash_code for
0103 /// a particular value without regard to the pre-promotion type. This is in
0104 /// contrast to hash_combine which may produce different hash_codes for
0105 /// differing argument types even if they would implicit promote to a common
0106 /// type without changing the value.
0107 template <typename T>
0108 std::enable_if_t<is_integral_or_enum<T>::value, hash_code> hash_value(T value);
0109 
0110 /// Compute a hash_code for a pointer's address.
0111 ///
0112 /// N.B.: This hashes the *address*. Not the value and not the type.
0113 template <typename T> hash_code hash_value(const T *ptr);
0114 
0115 /// Compute a hash_code for a pair of objects.
0116 template <typename T, typename U>
0117 hash_code hash_value(const std::pair<T, U> &arg);
0118 
0119 /// Compute a hash_code for a tuple.
0120 template <typename... Ts>
0121 hash_code hash_value(const std::tuple<Ts...> &arg);
0122 
0123 /// Compute a hash_code for a standard string.
0124 template <typename T>
0125 hash_code hash_value(const std::basic_string<T> &arg);
0126 
0127 /// Compute a hash_code for a standard string.
0128 template <typename T> hash_code hash_value(const std::optional<T> &arg);
0129 
0130 // All of the implementation details of actually computing the various hash
0131 // code values are held within this namespace. These routines are included in
0132 // the header file mainly to allow inlining and constant propagation.
0133 namespace hashing {
0134 namespace detail {
0135 
0136 inline uint64_t fetch64(const char *p) {
0137   uint64_t result;
0138   memcpy(&result, p, sizeof(result));
0139   if (sys::IsBigEndianHost)
0140     sys::swapByteOrder(result);
0141   return result;
0142 }
0143 
0144 inline uint32_t fetch32(const char *p) {
0145   uint32_t result;
0146   memcpy(&result, p, sizeof(result));
0147   if (sys::IsBigEndianHost)
0148     sys::swapByteOrder(result);
0149   return result;
0150 }
0151 
0152 /// Some primes between 2^63 and 2^64 for various uses.
0153 static constexpr uint64_t k0 = 0xc3a5c85c97cb3127ULL;
0154 static constexpr uint64_t k1 = 0xb492b66fbe98f273ULL;
0155 static constexpr uint64_t k2 = 0x9ae16a3b2f90404fULL;
0156 static constexpr uint64_t k3 = 0xc949d7c7509e6557ULL;
0157 
0158 /// Bitwise right rotate.
0159 /// Normally this will compile to a single instruction, especially if the
0160 /// shift is a manifest constant.
0161 inline uint64_t rotate(uint64_t val, size_t shift) {
0162   // Avoid shifting by 64: doing so yields an undefined result.
0163   return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
0164 }
0165 
0166 inline uint64_t shift_mix(uint64_t val) {
0167   return val ^ (val >> 47);
0168 }
0169 
0170 inline uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
0171   // Murmur-inspired hashing.
0172   const uint64_t kMul = 0x9ddfea08eb382d69ULL;
0173   uint64_t a = (low ^ high) * kMul;
0174   a ^= (a >> 47);
0175   uint64_t b = (high ^ a) * kMul;
0176   b ^= (b >> 47);
0177   b *= kMul;
0178   return b;
0179 }
0180 
0181 inline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) {
0182   uint8_t a = s[0];
0183   uint8_t b = s[len >> 1];
0184   uint8_t c = s[len - 1];
0185   uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
0186   uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
0187   return shift_mix(y * k2 ^ z * k3 ^ seed) * k2;
0188 }
0189 
0190 inline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) {
0191   uint64_t a = fetch32(s);
0192   return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4));
0193 }
0194 
0195 inline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) {
0196   uint64_t a = fetch64(s);
0197   uint64_t b = fetch64(s + len - 8);
0198   return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
0199 }
0200 
0201 inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
0202   uint64_t a = fetch64(s) * k1;
0203   uint64_t b = fetch64(s + 8);
0204   uint64_t c = fetch64(s + len - 8) * k2;
0205   uint64_t d = fetch64(s + len - 16) * k0;
0206   return hash_16_bytes(llvm::rotr<uint64_t>(a - b, 43) +
0207                            llvm::rotr<uint64_t>(c ^ seed, 30) + d,
0208                        a + llvm::rotr<uint64_t>(b ^ k3, 20) - c + len + seed);
0209 }
0210 
0211 inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
0212   uint64_t z = fetch64(s + 24);
0213   uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
0214   uint64_t b = llvm::rotr<uint64_t>(a + z, 52);
0215   uint64_t c = llvm::rotr<uint64_t>(a, 37);
0216   a += fetch64(s + 8);
0217   c += llvm::rotr<uint64_t>(a, 7);
0218   a += fetch64(s + 16);
0219   uint64_t vf = a + z;
0220   uint64_t vs = b + llvm::rotr<uint64_t>(a, 31) + c;
0221   a = fetch64(s + 16) + fetch64(s + len - 32);
0222   z = fetch64(s + len - 8);
0223   b = llvm::rotr<uint64_t>(a + z, 52);
0224   c = llvm::rotr<uint64_t>(a, 37);
0225   a += fetch64(s + len - 24);
0226   c += llvm::rotr<uint64_t>(a, 7);
0227   a += fetch64(s + len - 16);
0228   uint64_t wf = a + z;
0229   uint64_t ws = b + llvm::rotr<uint64_t>(a, 31) + c;
0230   uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
0231   return shift_mix((seed ^ (r * k0)) + vs) * k2;
0232 }
0233 
0234 inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
0235   if (length >= 4 && length <= 8)
0236     return hash_4to8_bytes(s, length, seed);
0237   if (length > 8 && length <= 16)
0238     return hash_9to16_bytes(s, length, seed);
0239   if (length > 16 && length <= 32)
0240     return hash_17to32_bytes(s, length, seed);
0241   if (length > 32)
0242     return hash_33to64_bytes(s, length, seed);
0243   if (length != 0)
0244     return hash_1to3_bytes(s, length, seed);
0245 
0246   return k2 ^ seed;
0247 }
0248 
0249 /// The intermediate state used during hashing.
0250 /// Currently, the algorithm for computing hash codes is based on CityHash and
0251 /// keeps 56 bytes of arbitrary state.
0252 struct hash_state {
0253   uint64_t h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0;
0254 
0255   /// Create a new hash_state structure and initialize it based on the
0256   /// seed and the first 64-byte chunk.
0257   /// This effectively performs the initial mix.
0258   static hash_state create(const char *s, uint64_t seed) {
0259     hash_state state = {0,
0260                         seed,
0261                         hash_16_bytes(seed, k1),
0262                         llvm::rotr<uint64_t>(seed ^ k1, 49),
0263                         seed * k1,
0264                         shift_mix(seed),
0265                         0};
0266     state.h6 = hash_16_bytes(state.h4, state.h5);
0267     state.mix(s);
0268     return state;
0269   }
0270 
0271   /// Mix 32-bytes from the input sequence into the 16-bytes of 'a'
0272   /// and 'b', including whatever is already in 'a' and 'b'.
0273   static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
0274     a += fetch64(s);
0275     uint64_t c = fetch64(s + 24);
0276     b = llvm::rotr<uint64_t>(b + a + c, 21);
0277     uint64_t d = a;
0278     a += fetch64(s + 8) + fetch64(s + 16);
0279     b += llvm::rotr<uint64_t>(a, 44) + d;
0280     a += c;
0281   }
0282 
0283   /// Mix in a 64-byte buffer of data.
0284   /// We mix all 64 bytes even when the chunk length is smaller, but we
0285   /// record the actual length.
0286   void mix(const char *s) {
0287     h0 = llvm::rotr<uint64_t>(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
0288     h1 = llvm::rotr<uint64_t>(h1 + h4 + fetch64(s + 48), 42) * k1;
0289     h0 ^= h6;
0290     h1 += h3 + fetch64(s + 40);
0291     h2 = llvm::rotr<uint64_t>(h2 + h5, 33) * k1;
0292     h3 = h4 * k1;
0293     h4 = h0 + h5;
0294     mix_32_bytes(s, h3, h4);
0295     h5 = h2 + h6;
0296     h6 = h1 + fetch64(s + 16);
0297     mix_32_bytes(s + 32, h5, h6);
0298     std::swap(h2, h0);
0299   }
0300 
0301   /// Compute the final 64-bit hash code value based on the current
0302   /// state and the length of bytes hashed.
0303   uint64_t finalize(size_t length) {
0304     return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
0305                          hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
0306   }
0307 };
0308 
0309 /// In LLVM_ENABLE_ABI_BREAKING_CHECKS builds, the seed is non-deterministic
0310 /// per process (address of a function in LLVMSupport) to prevent having users
0311 /// depend on the particular hash values. On platforms without ASLR, this is
0312 /// still likely non-deterministic per build.
0313 inline uint64_t get_execution_seed() {
0314 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
0315   return static_cast<uint64_t>(
0316       reinterpret_cast<uintptr_t>(&install_fatal_error_handler));
0317 #else
0318   return 0xff51afd7ed558ccdULL;
0319 #endif
0320 }
0321 
0322 
0323 /// Trait to indicate whether a type's bits can be hashed directly.
0324 ///
0325 /// A type trait which is true if we want to combine values for hashing by
0326 /// reading the underlying data. It is false if values of this type must
0327 /// first be passed to hash_value, and the resulting hash_codes combined.
0328 //
0329 // FIXME: We want to replace is_integral_or_enum and is_pointer here with
0330 // a predicate which asserts that comparing the underlying storage of two
0331 // values of the type for equality is equivalent to comparing the two values
0332 // for equality. For all the platforms we care about, this holds for integers
0333 // and pointers, but there are platforms where it doesn't and we would like to
0334 // support user-defined types which happen to satisfy this property.
0335 template <typename T> struct is_hashable_data
0336   : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
0337                                    std::is_pointer<T>::value) &&
0338                                   64 % sizeof(T) == 0)> {};
0339 
0340 // Special case std::pair to detect when both types are viable and when there
0341 // is no alignment-derived padding in the pair. This is a bit of a lie because
0342 // std::pair isn't truly POD, but it's close enough in all reasonable
0343 // implementations for our use case of hashing the underlying data.
0344 template <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
0345   : std::integral_constant<bool, (is_hashable_data<T>::value &&
0346                                   is_hashable_data<U>::value &&
0347                                   (sizeof(T) + sizeof(U)) ==
0348                                    sizeof(std::pair<T, U>))> {};
0349 
0350 /// Helper to get the hashable data representation for a type.
0351 /// This variant is enabled when the type itself can be used.
0352 template <typename T>
0353 std::enable_if_t<is_hashable_data<T>::value, T>
0354 get_hashable_data(const T &value) {
0355   return value;
0356 }
0357 /// Helper to get the hashable data representation for a type.
0358 /// This variant is enabled when we must first call hash_value and use the
0359 /// result as our data.
0360 template <typename T>
0361 std::enable_if_t<!is_hashable_data<T>::value, size_t>
0362 get_hashable_data(const T &value) {
0363   using ::llvm::hash_value;
0364   return hash_value(value);
0365 }
0366 
0367 /// Helper to store data from a value into a buffer and advance the
0368 /// pointer into that buffer.
0369 ///
0370 /// This routine first checks whether there is enough space in the provided
0371 /// buffer, and if not immediately returns false. If there is space, it
0372 /// copies the underlying bytes of value into the buffer, advances the
0373 /// buffer_ptr past the copied bytes, and returns true.
0374 template <typename T>
0375 bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
0376                        size_t offset = 0) {
0377   size_t store_size = sizeof(value) - offset;
0378   if (buffer_ptr + store_size > buffer_end)
0379     return false;
0380   const char *value_data = reinterpret_cast<const char *>(&value);
0381   memcpy(buffer_ptr, value_data + offset, store_size);
0382   buffer_ptr += store_size;
0383   return true;
0384 }
0385 
0386 /// Implement the combining of integral values into a hash_code.
0387 ///
0388 /// This overload is selected when the value type of the iterator is
0389 /// integral. Rather than computing a hash_code for each object and then
0390 /// combining them, this (as an optimization) directly combines the integers.
0391 template <typename InputIteratorT>
0392 hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
0393   const uint64_t seed = get_execution_seed();
0394   char buffer[64], *buffer_ptr = buffer;
0395   char *const buffer_end = std::end(buffer);
0396   while (first != last && store_and_advance(buffer_ptr, buffer_end,
0397                                             get_hashable_data(*first)))
0398     ++first;
0399   if (first == last)
0400     return hash_short(buffer, buffer_ptr - buffer, seed);
0401   assert(buffer_ptr == buffer_end);
0402 
0403   hash_state state = state.create(buffer, seed);
0404   size_t length = 64;
0405   while (first != last) {
0406     // Fill up the buffer. We don't clear it, which re-mixes the last round
0407     // when only a partial 64-byte chunk is left.
0408     buffer_ptr = buffer;
0409     while (first != last && store_and_advance(buffer_ptr, buffer_end,
0410                                               get_hashable_data(*first)))
0411       ++first;
0412 
0413     // Rotate the buffer if we did a partial fill in order to simulate doing
0414     // a mix of the last 64-bytes. That is how the algorithm works when we
0415     // have a contiguous byte sequence, and we want to emulate that here.
0416     std::rotate(buffer, buffer_ptr, buffer_end);
0417 
0418     // Mix this chunk into the current state.
0419     state.mix(buffer);
0420     length += buffer_ptr - buffer;
0421   };
0422 
0423   return state.finalize(length);
0424 }
0425 
0426 /// Implement the combining of integral values into a hash_code.
0427 ///
0428 /// This overload is selected when the value type of the iterator is integral
0429 /// and when the input iterator is actually a pointer. Rather than computing
0430 /// a hash_code for each object and then combining them, this (as an
0431 /// optimization) directly combines the integers. Also, because the integers
0432 /// are stored in contiguous memory, this routine avoids copying each value
0433 /// and directly reads from the underlying memory.
0434 template <typename ValueT>
0435 std::enable_if_t<is_hashable_data<ValueT>::value, hash_code>
0436 hash_combine_range_impl(ValueT *first, ValueT *last) {
0437   const uint64_t seed = get_execution_seed();
0438   const char *s_begin = reinterpret_cast<const char *>(first);
0439   const char *s_end = reinterpret_cast<const char *>(last);
0440   const size_t length = std::distance(s_begin, s_end);
0441   if (length <= 64)
0442     return hash_short(s_begin, length, seed);
0443 
0444   const char *s_aligned_end = s_begin + (length & ~63);
0445   hash_state state = state.create(s_begin, seed);
0446   s_begin += 64;
0447   while (s_begin != s_aligned_end) {
0448     state.mix(s_begin);
0449     s_begin += 64;
0450   }
0451   if (length & 63)
0452     state.mix(s_end - 64);
0453 
0454   return state.finalize(length);
0455 }
0456 
0457 } // namespace detail
0458 } // namespace hashing
0459 
0460 
0461 /// Compute a hash_code for a sequence of values.
0462 ///
0463 /// This hashes a sequence of values. It produces the same hash_code as
0464 /// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
0465 /// and is significantly faster given pointers and types which can be hashed as
0466 /// a sequence of bytes.
0467 template <typename InputIteratorT>
0468 hash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
0469   return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
0470 }
0471 
0472 
0473 // Implementation details for hash_combine.
0474 namespace hashing {
0475 namespace detail {
0476 
0477 /// Helper class to manage the recursive combining of hash_combine
0478 /// arguments.
0479 ///
0480 /// This class exists to manage the state and various calls involved in the
0481 /// recursive combining of arguments used in hash_combine. It is particularly
0482 /// useful at minimizing the code in the recursive calls to ease the pain
0483 /// caused by a lack of variadic functions.
0484 struct hash_combine_recursive_helper {
0485   char buffer[64] = {};
0486   hash_state state;
0487   const uint64_t seed;
0488 
0489 public:
0490   /// Construct a recursive hash combining helper.
0491   ///
0492   /// This sets up the state for a recursive hash combine, including getting
0493   /// the seed and buffer setup.
0494   hash_combine_recursive_helper()
0495     : seed(get_execution_seed()) {}
0496 
0497   /// Combine one chunk of data into the current in-flight hash.
0498   ///
0499   /// This merges one chunk of data into the hash. First it tries to buffer
0500   /// the data. If the buffer is full, it hashes the buffer into its
0501   /// hash_state, empties it, and then merges the new chunk in. This also
0502   /// handles cases where the data straddles the end of the buffer.
0503   template <typename T>
0504   char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
0505     if (!store_and_advance(buffer_ptr, buffer_end, data)) {
0506       // Check for skew which prevents the buffer from being packed, and do
0507       // a partial store into the buffer to fill it. This is only a concern
0508       // with the variadic combine because that formation can have varying
0509       // argument types.
0510       size_t partial_store_size = buffer_end - buffer_ptr;
0511       memcpy(buffer_ptr, &data, partial_store_size);
0512 
0513       // If the store fails, our buffer is full and ready to hash. We have to
0514       // either initialize the hash state (on the first full buffer) or mix
0515       // this buffer into the existing hash state. Length tracks the *hashed*
0516       // length, not the buffered length.
0517       if (length == 0) {
0518         state = state.create(buffer, seed);
0519         length = 64;
0520       } else {
0521         // Mix this chunk into the current state and bump length up by 64.
0522         state.mix(buffer);
0523         length += 64;
0524       }
0525       // Reset the buffer_ptr to the head of the buffer for the next chunk of
0526       // data.
0527       buffer_ptr = buffer;
0528 
0529       // Try again to store into the buffer -- this cannot fail as we only
0530       // store types smaller than the buffer.
0531       if (!store_and_advance(buffer_ptr, buffer_end, data,
0532                              partial_store_size))
0533         llvm_unreachable("buffer smaller than stored type");
0534     }
0535     return buffer_ptr;
0536   }
0537 
0538   /// Recursive, variadic combining method.
0539   ///
0540   /// This function recurses through each argument, combining that argument
0541   /// into a single hash.
0542   template <typename T, typename ...Ts>
0543   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
0544                     const T &arg, const Ts &...args) {
0545     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
0546 
0547     // Recurse to the next argument.
0548     return combine(length, buffer_ptr, buffer_end, args...);
0549   }
0550 
0551   /// Base case for recursive, variadic combining.
0552   ///
0553   /// The base case when combining arguments recursively is reached when all
0554   /// arguments have been handled. It flushes the remaining buffer and
0555   /// constructs a hash_code.
0556   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
0557     // Check whether the entire set of values fit in the buffer. If so, we'll
0558     // use the optimized short hashing routine and skip state entirely.
0559     if (length == 0)
0560       return hash_short(buffer, buffer_ptr - buffer, seed);
0561 
0562     // Mix the final buffer, rotating it if we did a partial fill in order to
0563     // simulate doing a mix of the last 64-bytes. That is how the algorithm
0564     // works when we have a contiguous byte sequence, and we want to emulate
0565     // that here.
0566     std::rotate(buffer, buffer_ptr, buffer_end);
0567 
0568     // Mix this chunk into the current state.
0569     state.mix(buffer);
0570     length += buffer_ptr - buffer;
0571 
0572     return state.finalize(length);
0573   }
0574 };
0575 
0576 } // namespace detail
0577 } // namespace hashing
0578 
0579 /// Combine values into a single hash_code.
0580 ///
0581 /// This routine accepts a varying number of arguments of any type. It will
0582 /// attempt to combine them into a single hash_code. For user-defined types it
0583 /// attempts to call a \see hash_value overload (via ADL) for the type. For
0584 /// integer and pointer types it directly combines their data into the
0585 /// resulting hash_code.
0586 ///
0587 /// The result is suitable for returning from a user's hash_value
0588 /// *implementation* for their user-defined type. Consumers of a type should
0589 /// *not* call this routine, they should instead call 'hash_value'.
0590 template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
0591   // Recursively hash each argument using a helper class.
0592   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
0593   return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
0594 }
0595 
0596 // Implementation details for implementations of hash_value overloads provided
0597 // here.
0598 namespace hashing {
0599 namespace detail {
0600 
0601 /// Helper to hash the value of a single integer.
0602 ///
0603 /// Overloads for smaller integer types are not provided to ensure consistent
0604 /// behavior in the presence of integral promotions. Essentially,
0605 /// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
0606 inline hash_code hash_integer_value(uint64_t value) {
0607   // Similar to hash_4to8_bytes but using a seed instead of length.
0608   const uint64_t seed = get_execution_seed();
0609   const char *s = reinterpret_cast<const char *>(&value);
0610   const uint64_t a = fetch32(s);
0611   return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
0612 }
0613 
0614 } // namespace detail
0615 } // namespace hashing
0616 
0617 // Declared and documented above, but defined here so that any of the hashing
0618 // infrastructure is available.
0619 template <typename T>
0620 std::enable_if_t<is_integral_or_enum<T>::value, hash_code> hash_value(T value) {
0621   return ::llvm::hashing::detail::hash_integer_value(
0622       static_cast<uint64_t>(value));
0623 }
0624 
0625 // Declared and documented above, but defined here so that any of the hashing
0626 // infrastructure is available.
0627 template <typename T> hash_code hash_value(const T *ptr) {
0628   return ::llvm::hashing::detail::hash_integer_value(
0629     reinterpret_cast<uintptr_t>(ptr));
0630 }
0631 
0632 // Declared and documented above, but defined here so that any of the hashing
0633 // infrastructure is available.
0634 template <typename T, typename U>
0635 hash_code hash_value(const std::pair<T, U> &arg) {
0636   return hash_combine(arg.first, arg.second);
0637 }
0638 
0639 template <typename... Ts> hash_code hash_value(const std::tuple<Ts...> &arg) {
0640   return std::apply([](const auto &...xs) { return hash_combine(xs...); }, arg);
0641 }
0642 
0643 // Declared and documented above, but defined here so that any of the hashing
0644 // infrastructure is available.
0645 template <typename T>
0646 hash_code hash_value(const std::basic_string<T> &arg) {
0647   return hash_combine_range(arg.begin(), arg.end());
0648 }
0649 
0650 template <typename T> hash_code hash_value(const std::optional<T> &arg) {
0651   return arg ? hash_combine(true, *arg) : hash_value(false);
0652 }
0653 
0654 template <> struct DenseMapInfo<hash_code, void> {
0655   static inline hash_code getEmptyKey() { return hash_code(-1); }
0656   static inline hash_code getTombstoneKey() { return hash_code(-2); }
0657   static unsigned getHashValue(hash_code val) {
0658     return static_cast<unsigned>(size_t(val));
0659   }
0660   static bool isEqual(hash_code LHS, hash_code RHS) { return LHS == RHS; }
0661 };
0662 
0663 } // namespace llvm
0664 
0665 /// Implement std::hash so that hash_code can be used in STL containers.
0666 namespace std {
0667 
0668 template<>
0669 struct hash<llvm::hash_code> {
0670   size_t operator()(llvm::hash_code const& Val) const {
0671     return Val;
0672   }
0673 };
0674 
0675 } // namespace std;
0676 
0677 #endif