SmallVector: Resolve a long-standing fixme by using the existing unitialized_copy...
[oota-llvm.git] / include / llvm / ADT / Hashing.h
1 //===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the newly proposed standard C++ interfaces for hashing
11 // arbitrary data and building hash functions for user-defined types. This
12 // interface was originally proposed in N3333[1] and is currently under review
13 // for inclusion in a future TR and/or standard.
14 //
15 // The primary interfaces provide are comprised of one type and three functions:
16 //
17 //  -- 'hash_code' class is an opaque type representing the hash code for some
18 //     data. It is the intended product of hashing, and can be used to implement
19 //     hash tables, checksumming, and other common uses of hashes. It is not an
20 //     integer type (although it can be converted to one) because it is risky
21 //     to assume much about the internals of a hash_code. In particular, each
22 //     execution of the program has a high probability of producing a different
23 //     hash_code for a given input. Thus their values are not stable to save or
24 //     persist, and should only be used during the execution for the
25 //     construction of hashing datastructures.
26 //
27 //  -- 'hash_value' is a function designed to be overloaded for each
28 //     user-defined type which wishes to be used within a hashing context. It
29 //     should be overloaded within the user-defined type's namespace and found
30 //     via ADL. Overloads for primitive types are provided by this library.
31 //
32 //  -- 'hash_combine' and 'hash_combine_range' are functions designed to aid
33 //      programmers in easily and intuitively combining a set of data into
34 //      a single hash_code for their object. They should only logically be used
35 //      within the implementation of a 'hash_value' routine or similar context.
36 //
37 // Note that 'hash_combine_range' contains very special logic for hashing
38 // a contiguous array of integers or pointers. This logic is *extremely* fast,
39 // on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were
40 // benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys
41 // under 32-bytes.
42 //
43 //===----------------------------------------------------------------------===//
44
45 #ifndef LLVM_ADT_HASHING_H
46 #define LLVM_ADT_HASHING_H
47
48 #include "llvm/Support/DataTypes.h"
49 #include "llvm/Support/Host.h"
50 #include "llvm/Support/SwapByteOrder.h"
51 #include "llvm/Support/type_traits.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <cstring>
55 #include <iterator>
56 #include <utility>
57
58 // Allow detecting C++11 feature availability when building with Clang without
59 // breaking other compilers.
60 #ifndef __has_feature
61 # define __has_feature(x) 0
62 #endif
63
64 namespace llvm {
65
66 /// \brief An opaque object representing a hash code.
67 ///
68 /// This object represents the result of hashing some entity. It is intended to
69 /// be used to implement hashtables or other hashing-based data structures.
70 /// While it wraps and exposes a numeric value, this value should not be
71 /// trusted to be stable or predictable across processes or executions.
72 ///
73 /// In order to obtain the hash_code for an object 'x':
74 /// \code
75 ///   using llvm::hash_value;
76 ///   llvm::hash_code code = hash_value(x);
77 /// \endcode
78 class hash_code {
79   size_t value;
80
81 public:
82   /// \brief Default construct a hash_code.
83   /// Note that this leaves the value uninitialized.
84   hash_code() {}
85
86   /// \brief Form a hash code directly from a numerical value.
87   hash_code(size_t value) : value(value) {}
88
89   /// \brief Convert the hash code to its numerical value for use.
90   /*explicit*/ operator size_t() const { return value; }
91
92   friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
93     return lhs.value == rhs.value;
94   }
95   friend bool operator!=(const hash_code &lhs, const hash_code &rhs) {
96     return lhs.value != rhs.value;
97   }
98
99   /// \brief Allow a hash_code to be directly run through hash_value.
100   friend size_t hash_value(const hash_code &code) { return code.value; }
101 };
102
103 /// \brief Compute a hash_code for any integer value.
104 ///
105 /// Note that this function is intended to compute the same hash_code for
106 /// a particular value without regard to the pre-promotion type. This is in
107 /// contrast to hash_combine which may produce different hash_codes for
108 /// differing argument types even if they would implicit promote to a common
109 /// type without changing the value.
110 template <typename T>
111 typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
112 hash_value(T value);
113
114 /// \brief Compute a hash_code for a pointer's address.
115 ///
116 /// N.B.: This hashes the *address*. Not the value and not the type.
117 template <typename T> hash_code hash_value(const T *ptr);
118
119 /// \brief Compute a hash_code for a pair of objects.
120 template <typename T, typename U>
121 hash_code hash_value(const std::pair<T, U> &arg);
122
123 /// \brief Compute a hash_code for a standard string.
124 template <typename T>
125 hash_code hash_value(const std::basic_string<T> &arg);
126
127
128 /// \brief Override the execution seed with a fixed value.
129 ///
130 /// This hashing library uses a per-execution seed designed to change on each
131 /// run with high probability in order to ensure that the hash codes are not
132 /// attackable and to ensure that output which is intended to be stable does
133 /// not rely on the particulars of the hash codes produced.
134 ///
135 /// That said, there are use cases where it is important to be able to
136 /// reproduce *exactly* a specific behavior. To that end, we provide a function
137 /// which will forcibly set the seed to a fixed value. This must be done at the
138 /// start of the program, before any hashes are computed. Also, it cannot be
139 /// undone. This makes it thread-hostile and very hard to use outside of
140 /// immediately on start of a simple program designed for reproducible
141 /// behavior.
142 void set_fixed_execution_hash_seed(size_t fixed_value);
143
144
145 // All of the implementation details of actually computing the various hash
146 // code values are held within this namespace. These routines are included in
147 // the header file mainly to allow inlining and constant propagation.
148 namespace hashing {
149 namespace detail {
150
151 inline uint64_t fetch64(const char *p) {
152   uint64_t result;
153   memcpy(&result, p, sizeof(result));
154   if (sys::IsBigEndianHost)
155     sys::swapByteOrder(result);
156   return result;
157 }
158
159 inline uint32_t fetch32(const char *p) {
160   uint32_t result;
161   memcpy(&result, p, sizeof(result));
162   if (sys::IsBigEndianHost)
163     sys::swapByteOrder(result);
164   return result;
165 }
166
167 /// Some primes between 2^63 and 2^64 for various uses.
168 static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
169 static const uint64_t k1 = 0xb492b66fbe98f273ULL;
170 static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
171 static const uint64_t k3 = 0xc949d7c7509e6557ULL;
172
173 /// \brief Bitwise right rotate.
174 /// Normally this will compile to a single instruction, especially if the
175 /// shift is a manifest constant.
176 inline uint64_t rotate(uint64_t val, size_t shift) {
177   // Avoid shifting by 64: doing so yields an undefined result.
178   return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
179 }
180
181 inline uint64_t shift_mix(uint64_t val) {
182   return val ^ (val >> 47);
183 }
184
185 inline uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
186   // Murmur-inspired hashing.
187   const uint64_t kMul = 0x9ddfea08eb382d69ULL;
188   uint64_t a = (low ^ high) * kMul;
189   a ^= (a >> 47);
190   uint64_t b = (high ^ a) * kMul;
191   b ^= (b >> 47);
192   b *= kMul;
193   return b;
194 }
195
196 inline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) {
197   uint8_t a = s[0];
198   uint8_t b = s[len >> 1];
199   uint8_t c = s[len - 1];
200   uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
201   uint32_t z = len + (static_cast<uint32_t>(c) << 2);
202   return shift_mix(y * k2 ^ z * k3 ^ seed) * k2;
203 }
204
205 inline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) {
206   uint64_t a = fetch32(s);
207   return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4));
208 }
209
210 inline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) {
211   uint64_t a = fetch64(s);
212   uint64_t b = fetch64(s + len - 8);
213   return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
214 }
215
216 inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
217   uint64_t a = fetch64(s) * k1;
218   uint64_t b = fetch64(s + 8);
219   uint64_t c = fetch64(s + len - 8) * k2;
220   uint64_t d = fetch64(s + len - 16) * k0;
221   return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d,
222                        a + rotate(b ^ k3, 20) - c + len + seed);
223 }
224
225 inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
226   uint64_t z = fetch64(s + 24);
227   uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
228   uint64_t b = rotate(a + z, 52);
229   uint64_t c = rotate(a, 37);
230   a += fetch64(s + 8);
231   c += rotate(a, 7);
232   a += fetch64(s + 16);
233   uint64_t vf = a + z;
234   uint64_t vs = b + rotate(a, 31) + c;
235   a = fetch64(s + 16) + fetch64(s + len - 32);
236   z = fetch64(s + len - 8);
237   b = rotate(a + z, 52);
238   c = rotate(a, 37);
239   a += fetch64(s + len - 24);
240   c += rotate(a, 7);
241   a += fetch64(s + len - 16);
242   uint64_t wf = a + z;
243   uint64_t ws = b + rotate(a, 31) + c;
244   uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
245   return shift_mix((seed ^ (r * k0)) + vs) * k2;
246 }
247
248 inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
249   if (length >= 4 && length <= 8)
250     return hash_4to8_bytes(s, length, seed);
251   if (length > 8 && length <= 16)
252     return hash_9to16_bytes(s, length, seed);
253   if (length > 16 && length <= 32)
254     return hash_17to32_bytes(s, length, seed);
255   if (length > 32)
256     return hash_33to64_bytes(s, length, seed);
257   if (length != 0)
258     return hash_1to3_bytes(s, length, seed);
259
260   return k2 ^ seed;
261 }
262
263 /// \brief The intermediate state used during hashing.
264 /// Currently, the algorithm for computing hash codes is based on CityHash and
265 /// keeps 56 bytes of arbitrary state.
266 struct hash_state {
267   uint64_t h0, h1, h2, h3, h4, h5, h6;
268
269   /// \brief Create a new hash_state structure and initialize it based on the
270   /// seed and the first 64-byte chunk.
271   /// This effectively performs the initial mix.
272   static hash_state create(const char *s, uint64_t seed) {
273     hash_state state = {
274       0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49),
275       seed * k1, shift_mix(seed), 0 };
276     state.h6 = hash_16_bytes(state.h4, state.h5);
277     state.mix(s);
278     return state;
279   }
280
281   /// \brief Mix 32-bytes from the input sequence into the 16-bytes of 'a'
282   /// and 'b', including whatever is already in 'a' and 'b'.
283   static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
284     a += fetch64(s);
285     uint64_t c = fetch64(s + 24);
286     b = rotate(b + a + c, 21);
287     uint64_t d = a;
288     a += fetch64(s + 8) + fetch64(s + 16);
289     b += rotate(a, 44) + d;
290     a += c;
291   }
292
293   /// \brief Mix in a 64-byte buffer of data.
294   /// We mix all 64 bytes even when the chunk length is smaller, but we
295   /// record the actual length.
296   void mix(const char *s) {
297     h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
298     h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1;
299     h0 ^= h6;
300     h1 += h3 + fetch64(s + 40);
301     h2 = rotate(h2 + h5, 33) * k1;
302     h3 = h4 * k1;
303     h4 = h0 + h5;
304     mix_32_bytes(s, h3, h4);
305     h5 = h2 + h6;
306     h6 = h1 + fetch64(s + 16);
307     mix_32_bytes(s + 32, h5, h6);
308     std::swap(h2, h0);
309   }
310
311   /// \brief Compute the final 64-bit hash code value based on the current
312   /// state and the length of bytes hashed.
313   uint64_t finalize(size_t length) {
314     return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
315                          hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
316   }
317 };
318
319
320 /// \brief A global, fixed seed-override variable.
321 ///
322 /// This variable can be set using the \see llvm::set_fixed_execution_seed
323 /// function. See that function for details. Do not, under any circumstances,
324 /// set or read this variable.
325 extern size_t fixed_seed_override;
326
327 inline size_t get_execution_seed() {
328   // FIXME: This needs to be a per-execution seed. This is just a placeholder
329   // implementation. Switching to a per-execution seed is likely to flush out
330   // instability bugs and so will happen as its own commit.
331   //
332   // However, if there is a fixed seed override set the first time this is
333   // called, return that instead of the per-execution seed.
334   const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
335   static size_t seed = fixed_seed_override ? fixed_seed_override
336                                            : (size_t)seed_prime;
337   return seed;
338 }
339
340
341 /// \brief Trait to indicate whether a type's bits can be hashed directly.
342 ///
343 /// A type trait which is true if we want to combine values for hashing by
344 /// reading the underlying data. It is false if values of this type must
345 /// first be passed to hash_value, and the resulting hash_codes combined.
346 //
347 // FIXME: We want to replace is_integral_or_enum and is_pointer here with
348 // a predicate which asserts that comparing the underlying storage of two
349 // values of the type for equality is equivalent to comparing the two values
350 // for equality. For all the platforms we care about, this holds for integers
351 // and pointers, but there are platforms where it doesn't and we would like to
352 // support user-defined types which happen to satisfy this property.
353 template <typename T> struct is_hashable_data
354   : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
355                                    std::is_pointer<T>::value) &&
356                                   64 % sizeof(T) == 0)> {};
357
358 // Special case std::pair to detect when both types are viable and when there
359 // is no alignment-derived padding in the pair. This is a bit of a lie because
360 // std::pair isn't truly POD, but it's close enough in all reasonable
361 // implementations for our use case of hashing the underlying data.
362 template <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
363   : std::integral_constant<bool, (is_hashable_data<T>::value &&
364                                   is_hashable_data<U>::value &&
365                                   (sizeof(T) + sizeof(U)) ==
366                                    sizeof(std::pair<T, U>))> {};
367
368 /// \brief Helper to get the hashable data representation for a type.
369 /// This variant is enabled when the type itself can be used.
370 template <typename T>
371 typename std::enable_if<is_hashable_data<T>::value, T>::type
372 get_hashable_data(const T &value) {
373   return value;
374 }
375 /// \brief Helper to get the hashable data representation for a type.
376 /// This variant is enabled when we must first call hash_value and use the
377 /// result as our data.
378 template <typename T>
379 typename std::enable_if<!is_hashable_data<T>::value, size_t>::type
380 get_hashable_data(const T &value) {
381   using ::llvm::hash_value;
382   return hash_value(value);
383 }
384
385 /// \brief Helper to store data from a value into a buffer and advance the
386 /// pointer into that buffer.
387 ///
388 /// This routine first checks whether there is enough space in the provided
389 /// buffer, and if not immediately returns false. If there is space, it
390 /// copies the underlying bytes of value into the buffer, advances the
391 /// buffer_ptr past the copied bytes, and returns true.
392 template <typename T>
393 bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
394                        size_t offset = 0) {
395   size_t store_size = sizeof(value) - offset;
396   if (buffer_ptr + store_size > buffer_end)
397     return false;
398   const char *value_data = reinterpret_cast<const char *>(&value);
399   memcpy(buffer_ptr, value_data + offset, store_size);
400   buffer_ptr += store_size;
401   return true;
402 }
403
404 /// \brief Implement the combining of integral values into a hash_code.
405 ///
406 /// This overload is selected when the value type of the iterator is
407 /// integral. Rather than computing a hash_code for each object and then
408 /// combining them, this (as an optimization) directly combines the integers.
409 template <typename InputIteratorT>
410 hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
411   const size_t seed = get_execution_seed();
412   char buffer[64], *buffer_ptr = buffer;
413   char *const buffer_end = std::end(buffer);
414   while (first != last && store_and_advance(buffer_ptr, buffer_end,
415                                             get_hashable_data(*first)))
416     ++first;
417   if (first == last)
418     return hash_short(buffer, buffer_ptr - buffer, seed);
419   assert(buffer_ptr == buffer_end);
420
421   hash_state state = state.create(buffer, seed);
422   size_t length = 64;
423   while (first != last) {
424     // Fill up the buffer. We don't clear it, which re-mixes the last round
425     // when only a partial 64-byte chunk is left.
426     buffer_ptr = buffer;
427     while (first != last && store_and_advance(buffer_ptr, buffer_end,
428                                               get_hashable_data(*first)))
429       ++first;
430
431     // Rotate the buffer if we did a partial fill in order to simulate doing
432     // a mix of the last 64-bytes. That is how the algorithm works when we
433     // have a contiguous byte sequence, and we want to emulate that here.
434     std::rotate(buffer, buffer_ptr, buffer_end);
435
436     // Mix this chunk into the current state.
437     state.mix(buffer);
438     length += buffer_ptr - buffer;
439   };
440
441   return state.finalize(length);
442 }
443
444 /// \brief Implement the combining of integral values into a hash_code.
445 ///
446 /// This overload is selected when the value type of the iterator is integral
447 /// and when the input iterator is actually a pointer. Rather than computing
448 /// a hash_code for each object and then combining them, this (as an
449 /// optimization) directly combines the integers. Also, because the integers
450 /// are stored in contiguous memory, this routine avoids copying each value
451 /// and directly reads from the underlying memory.
452 template <typename ValueT>
453 typename std::enable_if<is_hashable_data<ValueT>::value, hash_code>::type
454 hash_combine_range_impl(ValueT *first, ValueT *last) {
455   const size_t seed = get_execution_seed();
456   const char *s_begin = reinterpret_cast<const char *>(first);
457   const char *s_end = reinterpret_cast<const char *>(last);
458   const size_t length = std::distance(s_begin, s_end);
459   if (length <= 64)
460     return hash_short(s_begin, length, seed);
461
462   const char *s_aligned_end = s_begin + (length & ~63);
463   hash_state state = state.create(s_begin, seed);
464   s_begin += 64;
465   while (s_begin != s_aligned_end) {
466     state.mix(s_begin);
467     s_begin += 64;
468   }
469   if (length & 63)
470     state.mix(s_end - 64);
471
472   return state.finalize(length);
473 }
474
475 } // namespace detail
476 } // namespace hashing
477
478
479 /// \brief Compute a hash_code for a sequence of values.
480 ///
481 /// This hashes a sequence of values. It produces the same hash_code as
482 /// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
483 /// and is significantly faster given pointers and types which can be hashed as
484 /// a sequence of bytes.
485 template <typename InputIteratorT>
486 hash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
487   return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
488 }
489
490
491 // Implementation details for hash_combine.
492 namespace hashing {
493 namespace detail {
494
495 /// \brief Helper class to manage the recursive combining of hash_combine
496 /// arguments.
497 ///
498 /// This class exists to manage the state and various calls involved in the
499 /// recursive combining of arguments used in hash_combine. It is particularly
500 /// useful at minimizing the code in the recursive calls to ease the pain
501 /// caused by a lack of variadic functions.
502 struct hash_combine_recursive_helper {
503   char buffer[64];
504   hash_state state;
505   const size_t seed;
506
507 public:
508   /// \brief Construct a recursive hash combining helper.
509   ///
510   /// This sets up the state for a recursive hash combine, including getting
511   /// the seed and buffer setup.
512   hash_combine_recursive_helper()
513     : seed(get_execution_seed()) {}
514
515   /// \brief Combine one chunk of data into the current in-flight hash.
516   ///
517   /// This merges one chunk of data into the hash. First it tries to buffer
518   /// the data. If the buffer is full, it hashes the buffer into its
519   /// hash_state, empties it, and then merges the new chunk in. This also
520   /// handles cases where the data straddles the end of the buffer.
521   template <typename T>
522   char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
523     if (!store_and_advance(buffer_ptr, buffer_end, data)) {
524       // Check for skew which prevents the buffer from being packed, and do
525       // a partial store into the buffer to fill it. This is only a concern
526       // with the variadic combine because that formation can have varying
527       // argument types.
528       size_t partial_store_size = buffer_end - buffer_ptr;
529       memcpy(buffer_ptr, &data, partial_store_size);
530
531       // If the store fails, our buffer is full and ready to hash. We have to
532       // either initialize the hash state (on the first full buffer) or mix
533       // this buffer into the existing hash state. Length tracks the *hashed*
534       // length, not the buffered length.
535       if (length == 0) {
536         state = state.create(buffer, seed);
537         length = 64;
538       } else {
539         // Mix this chunk into the current state and bump length up by 64.
540         state.mix(buffer);
541         length += 64;
542       }
543       // Reset the buffer_ptr to the head of the buffer for the next chunk of
544       // data.
545       buffer_ptr = buffer;
546
547       // Try again to store into the buffer -- this cannot fail as we only
548       // store types smaller than the buffer.
549       if (!store_and_advance(buffer_ptr, buffer_end, data,
550                              partial_store_size))
551         abort();
552     }
553     return buffer_ptr;
554   }
555
556 #if defined(__has_feature) && __has_feature(__cxx_variadic_templates__)
557
558   /// \brief Recursive, variadic combining method.
559   ///
560   /// This function recurses through each argument, combining that argument
561   /// into a single hash.
562   template <typename T, typename ...Ts>
563   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
564                     const T &arg, const Ts &...args) {
565     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
566
567     // Recurse to the next argument.
568     return combine(length, buffer_ptr, buffer_end, args...);
569   }
570
571 #else
572   // Manually expanded recursive combining methods. See variadic above for
573   // documentation.
574
575   template <typename T1, typename T2, typename T3, typename T4, typename T5,
576             typename T6, typename T7, typename T8, typename T9, typename T10,
577             typename T11, typename T12, typename T13, typename T14,
578             typename T15, typename T16, typename T17, typename T18>
579   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
580                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
581                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
582                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
583                     const T10 &arg10, const T11 &arg11, const T12 &arg12,
584                     const T13 &arg13, const T14 &arg14, const T15 &arg15,
585                     const T16 &arg16, const T17 &arg17, const T18 &arg18) {
586     buffer_ptr =
587         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
588     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
589                    arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
590                    arg16, arg17, arg18);
591   }
592   template <typename T1, typename T2, typename T3, typename T4, typename T5,
593             typename T6, typename T7, typename T8, typename T9, typename T10,
594             typename T11, typename T12, typename T13, typename T14,
595             typename T15, typename T16, typename T17>
596   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
597                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
598                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
599                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
600                     const T10 &arg10, const T11 &arg11, const T12 &arg12,
601                     const T13 &arg13, const T14 &arg14, const T15 &arg15,
602                     const T16 &arg16, const T17 &arg17) {
603     buffer_ptr =
604         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
605     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
606                    arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
607                    arg16, arg17);
608   }
609   template <typename T1, typename T2, typename T3, typename T4, typename T5,
610             typename T6, typename T7, typename T8, typename T9, typename T10,
611             typename T11, typename T12, typename T13, typename T14,
612             typename T15, typename T16>
613   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
614                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
615                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
616                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
617                     const T10 &arg10, const T11 &arg11, const T12 &arg12,
618                     const T13 &arg13, const T14 &arg14, const T15 &arg15,
619                     const T16 &arg16) {
620     buffer_ptr =
621         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
622     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
623                    arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
624                    arg16);
625   }
626   template <typename T1, typename T2, typename T3, typename T4, typename T5,
627             typename T6, typename T7, typename T8, typename T9, typename T10,
628             typename T11, typename T12, typename T13, typename T14,
629             typename T15>
630   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
631                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
632                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
633                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
634                     const T10 &arg10, const T11 &arg11, const T12 &arg12,
635                     const T13 &arg13, const T14 &arg14, const T15 &arg15) {
636     buffer_ptr =
637         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
638     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
639                    arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
640   }
641   template <typename T1, typename T2, typename T3, typename T4, typename T5,
642             typename T6, typename T7, typename T8, typename T9, typename T10,
643             typename T11, typename T12, typename T13, typename T14>
644   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
645                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
646                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
647                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
648                     const T10 &arg10, const T11 &arg11, const T12 &arg12,
649                     const T13 &arg13, const T14 &arg14) {
650     buffer_ptr =
651         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
652     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
653                    arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
654   }
655   template <typename T1, typename T2, typename T3, typename T4, typename T5,
656             typename T6, typename T7, typename T8, typename T9, typename T10,
657             typename T11, typename T12, typename T13>
658   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
659                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
660                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
661                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
662                     const T10 &arg10, const T11 &arg11, const T12 &arg12,
663                     const T13 &arg13) {
664     buffer_ptr =
665         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
666     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
667                    arg7, arg8, arg9, arg10, arg11, arg12, arg13);
668   }
669   template <typename T1, typename T2, typename T3, typename T4, typename T5,
670             typename T6, typename T7, typename T8, typename T9, typename T10,
671             typename T11, typename T12>
672   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
673                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
674                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
675                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
676                     const T10 &arg10, const T11 &arg11, const T12 &arg12) {
677     buffer_ptr =
678         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
679     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
680                    arg7, arg8, arg9, arg10, arg11, arg12);
681   }
682   template <typename T1, typename T2, typename T3, typename T4, typename T5,
683             typename T6, typename T7, typename T8, typename T9, typename T10,
684             typename T11>
685   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
686                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
687                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
688                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
689                     const T10 &arg10, const T11 &arg11) {
690     buffer_ptr =
691         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
692     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
693                    arg7, arg8, arg9, arg10, arg11);
694   }
695   template <typename T1, typename T2, typename T3, typename T4, typename T5,
696             typename T6, typename T7, typename T8, typename T9, typename T10>
697   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
698                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
699                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
700                     const T7 &arg7, const T8 &arg8, const T9 &arg9,
701                     const T10 &arg10) {
702     buffer_ptr =
703         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
704     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
705                    arg7, arg8, arg9, arg10);
706   }
707   template <typename T1, typename T2, typename T3, typename T4, typename T5,
708             typename T6, typename T7, typename T8, typename T9>
709   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
710                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
711                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
712                     const T7 &arg7, const T8 &arg8, const T9 &arg9) {
713     buffer_ptr =
714         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
715     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
716                    arg7, arg8, arg9);
717   }
718   template <typename T1, typename T2, typename T3, typename T4, typename T5,
719             typename T6, typename T7, typename T8>
720   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
721                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
722                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
723                     const T7 &arg7, const T8 &arg8) {
724     buffer_ptr =
725         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
726     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
727                    arg7, arg8);
728   }
729   template <typename T1, typename T2, typename T3, typename T4, typename T5,
730             typename T6, typename T7>
731   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
732                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
733                     const T4 &arg4, const T5 &arg5, const T6 &arg6,
734                     const T7 &arg7) {
735     buffer_ptr =
736         combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
737     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6,
738                    arg7);
739   }
740   template <typename T1, typename T2, typename T3, typename T4, typename T5,
741             typename T6>
742   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
743                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
744                     const T4 &arg4, const T5 &arg5, const T6 &arg6) {
745     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
746     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6);
747   }
748   template <typename T1, typename T2, typename T3, typename T4, typename T5>
749   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
750                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
751                     const T4 &arg4, const T5 &arg5) {
752     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
753     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5);
754   }
755   template <typename T1, typename T2, typename T3, typename T4>
756   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
757                     const T1 &arg1, const T2 &arg2, const T3 &arg3,
758                     const T4 &arg4) {
759     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
760     return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4);
761   }
762   template <typename T1, typename T2, typename T3>
763   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
764                     const T1 &arg1, const T2 &arg2, const T3 &arg3) {
765     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
766     return combine(length, buffer_ptr, buffer_end, arg2, arg3);
767   }
768   template <typename T1, typename T2>
769   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
770                     const T1 &arg1, const T2 &arg2) {
771     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
772     return combine(length, buffer_ptr, buffer_end, arg2);
773   }
774   template <typename T1>
775   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
776                     const T1 &arg1) {
777     buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
778     return combine(length, buffer_ptr, buffer_end);
779   }
780
781 #endif
782
783   /// \brief Base case for recursive, variadic combining.
784   ///
785   /// The base case when combining arguments recursively is reached when all
786   /// arguments have been handled. It flushes the remaining buffer and
787   /// constructs a hash_code.
788   hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
789     // Check whether the entire set of values fit in the buffer. If so, we'll
790     // use the optimized short hashing routine and skip state entirely.
791     if (length == 0)
792       return hash_short(buffer, buffer_ptr - buffer, seed);
793
794     // Mix the final buffer, rotating it if we did a partial fill in order to
795     // simulate doing a mix of the last 64-bytes. That is how the algorithm
796     // works when we have a contiguous byte sequence, and we want to emulate
797     // that here.
798     std::rotate(buffer, buffer_ptr, buffer_end);
799
800     // Mix this chunk into the current state.
801     state.mix(buffer);
802     length += buffer_ptr - buffer;
803
804     return state.finalize(length);
805   }
806 };
807
808 } // namespace detail
809 } // namespace hashing
810
811
812 #if __has_feature(__cxx_variadic_templates__)
813
814 /// \brief Combine values into a single hash_code.
815 ///
816 /// This routine accepts a varying number of arguments of any type. It will
817 /// attempt to combine them into a single hash_code. For user-defined types it
818 /// attempts to call a \see hash_value overload (via ADL) for the type. For
819 /// integer and pointer types it directly combines their data into the
820 /// resulting hash_code.
821 ///
822 /// The result is suitable for returning from a user's hash_value
823 /// *implementation* for their user-defined type. Consumers of a type should
824 /// *not* call this routine, they should instead call 'hash_value'.
825 template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
826   // Recursively hash each argument using a helper class.
827   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
828   return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
829 }
830
831 #else
832
833 // What follows are manually exploded overloads for each argument width. See
834 // the above variadic definition for documentation and specification.
835
836 template <typename T1, typename T2, typename T3, typename T4, typename T5,
837           typename T6, typename T7, typename T8, typename T9, typename T10,
838           typename T11, typename T12, typename T13, typename T14, typename T15,
839           typename T16, typename T17, typename T18>
840 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
841                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
842                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
843                        const T10 &arg10, const T11 &arg11, const T12 &arg12,
844                        const T13 &arg13, const T14 &arg14, const T15 &arg15,
845                        const T16 &arg16, const T17 &arg17, const T18 &arg18) {
846   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
847   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
848                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12,
849                         arg13, arg14, arg15, arg16, arg17, arg18);
850 }
851 template <typename T1, typename T2, typename T3, typename T4, typename T5,
852           typename T6, typename T7, typename T8, typename T9, typename T10,
853           typename T11, typename T12, typename T13, typename T14, typename T15,
854           typename T16, typename T17>
855 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
856                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
857                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
858                        const T10 &arg10, const T11 &arg11, const T12 &arg12,
859                        const T13 &arg13, const T14 &arg14, const T15 &arg15,
860                        const T16 &arg16, const T17 &arg17) {
861   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
862   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
863                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12,
864                         arg13, arg14, arg15, arg16, arg17);
865 }
866 template <typename T1, typename T2, typename T3, typename T4, typename T5,
867           typename T6, typename T7, typename T8, typename T9, typename T10,
868           typename T11, typename T12, typename T13, typename T14, typename T15,
869           typename T16>
870 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
871                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
872                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
873                        const T10 &arg10, const T11 &arg11, const T12 &arg12,
874                        const T13 &arg13, const T14 &arg14, const T15 &arg15,
875                        const T16 &arg16) {
876   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
877   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
878                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12,
879                         arg13, arg14, arg15, arg16);
880 }
881 template <typename T1, typename T2, typename T3, typename T4, typename T5,
882           typename T6, typename T7, typename T8, typename T9, typename T10,
883           typename T11, typename T12, typename T13, typename T14, typename T15>
884 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
885                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
886                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
887                        const T10 &arg10, const T11 &arg11, const T12 &arg12,
888                        const T13 &arg13, const T14 &arg14, const T15 &arg15) {
889   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
890   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
891                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12,
892                         arg13, arg14, arg15);
893 }
894 template <typename T1, typename T2, typename T3, typename T4, typename T5,
895           typename T6, typename T7, typename T8, typename T9, typename T10,
896           typename T11, typename T12, typename T13, typename T14>
897 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
898                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
899                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
900                        const T10 &arg10, const T11 &arg11, const T12 &arg12,
901                        const T13 &arg13, const T14 &arg14) {
902   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
903   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
904                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12,
905                         arg13, arg14);
906 }
907 template <typename T1, typename T2, typename T3, typename T4, typename T5,
908           typename T6, typename T7, typename T8, typename T9, typename T10,
909           typename T11, typename T12, typename T13>
910 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
911                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
912                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
913                        const T10 &arg10, const T11 &arg11, const T12 &arg12,
914                        const T13 &arg13) {
915   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
916   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
917                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12,
918                         arg13);
919 }
920 template <typename T1, typename T2, typename T3, typename T4, typename T5,
921           typename T6, typename T7, typename T8, typename T9, typename T10,
922           typename T11, typename T12>
923 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
924                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
925                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
926                        const T10 &arg10, const T11 &arg11, const T12 &arg12) {
927   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
928   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
929                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11,
930                         arg12);
931 }
932 template <typename T1, typename T2, typename T3, typename T4, typename T5,
933           typename T6, typename T7, typename T8, typename T9, typename T10,
934           typename T11>
935 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
936                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
937                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
938                        const T10 &arg10, const T11 &arg11) {
939   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
940   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
941                         arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
942 }
943 template <typename T1, typename T2, typename T3, typename T4, typename T5,
944           typename T6, typename T7, typename T8, typename T9, typename T10>
945 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
946                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
947                        const T7 &arg7, const T8 &arg8, const T9 &arg9,
948                        const T10 &arg10) {
949   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
950   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
951                         arg4, arg5, arg6, arg7, arg8, arg9, arg10);
952 }
953 template <typename T1, typename T2, typename T3, typename T4, typename T5,
954           typename T6, typename T7, typename T8, typename T9>
955 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
956                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
957                        const T7 &arg7, const T8 &arg8, const T9 &arg9) {
958   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
959   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
960                         arg4, arg5, arg6, arg7, arg8, arg9);
961 }
962 template <typename T1, typename T2, typename T3, typename T4, typename T5,
963           typename T6, typename T7, typename T8>
964 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
965                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
966                        const T7 &arg7, const T8 &arg8) {
967   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
968   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
969                         arg4, arg5, arg6, arg7, arg8);
970 }
971 template <typename T1, typename T2, typename T3, typename T4, typename T5,
972           typename T6, typename T7>
973 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
974                        const T4 &arg4, const T5 &arg5, const T6 &arg6,
975                        const T7 &arg7) {
976   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
977   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3,
978                         arg4, arg5, arg6, arg7);
979 }
980 template <typename T1, typename T2, typename T3, typename T4, typename T5,
981           typename T6>
982 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
983                        const T4 &arg4, const T5 &arg5, const T6 &arg6) {
984   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
985   return helper.combine(0, helper.buffer, helper.buffer + 64,
986                         arg1, arg2, arg3, arg4, arg5, arg6);
987 }
988 template <typename T1, typename T2, typename T3, typename T4, typename T5>
989 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
990                        const T4 &arg4, const T5 &arg5) {
991   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
992   return helper.combine(0, helper.buffer, helper.buffer + 64,
993                         arg1, arg2, arg3, arg4, arg5);
994 }
995 template <typename T1, typename T2, typename T3, typename T4>
996 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
997                        const T4 &arg4) {
998   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
999   return helper.combine(0, helper.buffer, helper.buffer + 64,
1000                         arg1, arg2, arg3, arg4);
1001 }
1002 template <typename T1, typename T2, typename T3>
1003 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3) {
1004   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
1005   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3);
1006 }
1007 template <typename T1, typename T2>
1008 hash_code hash_combine(const T1 &arg1, const T2 &arg2) {
1009   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
1010   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2);
1011 }
1012 template <typename T1>
1013 hash_code hash_combine(const T1 &arg1) {
1014   ::llvm::hashing::detail::hash_combine_recursive_helper helper;
1015   return helper.combine(0, helper.buffer, helper.buffer + 64, arg1);
1016 }
1017
1018 #endif
1019
1020
1021 // Implementation details for implementations of hash_value overloads provided
1022 // here.
1023 namespace hashing {
1024 namespace detail {
1025
1026 /// \brief Helper to hash the value of a single integer.
1027 ///
1028 /// Overloads for smaller integer types are not provided to ensure consistent
1029 /// behavior in the presence of integral promotions. Essentially,
1030 /// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
1031 inline hash_code hash_integer_value(uint64_t value) {
1032   // Similar to hash_4to8_bytes but using a seed instead of length.
1033   const uint64_t seed = get_execution_seed();
1034   const char *s = reinterpret_cast<const char *>(&value);
1035   const uint64_t a = fetch32(s);
1036   return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
1037 }
1038
1039 } // namespace detail
1040 } // namespace hashing
1041
1042 // Declared and documented above, but defined here so that any of the hashing
1043 // infrastructure is available.
1044 template <typename T>
1045 typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
1046 hash_value(T value) {
1047   return ::llvm::hashing::detail::hash_integer_value(value);
1048 }
1049
1050 // Declared and documented above, but defined here so that any of the hashing
1051 // infrastructure is available.
1052 template <typename T> hash_code hash_value(const T *ptr) {
1053   return ::llvm::hashing::detail::hash_integer_value(
1054     reinterpret_cast<uintptr_t>(ptr));
1055 }
1056
1057 // Declared and documented above, but defined here so that any of the hashing
1058 // infrastructure is available.
1059 template <typename T, typename U>
1060 hash_code hash_value(const std::pair<T, U> &arg) {
1061   return hash_combine(arg.first, arg.second);
1062 }
1063
1064 // Declared and documented above, but defined here so that any of the hashing
1065 // infrastructure is available.
1066 template <typename T>
1067 hash_code hash_value(const std::basic_string<T> &arg) {
1068   return hash_combine_range(arg.begin(), arg.end());
1069 }
1070
1071 } // namespace llvm
1072
1073 #endif