11a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===//
21a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//
31a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//                     The LLVM Compiler Infrastructure
41a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//
51a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// This file is distributed under the University of Illinois Open Source
61a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// License. See LICENSE.TXT for details.
71a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//
81a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//===----------------------------------------------------------------------===//
91a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//
100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// This file implements the newly proposed standard C++ interfaces for hashing
110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// arbitrary data and building hash functions for user-defined types. This
120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// interface was originally proposed in N3333[1] and is currently under review
130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// for inclusion in a future TR and/or standard.
140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// The primary interfaces provide are comprised of one type and three functions:
160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//  -- 'hash_code' class is an opaque type representing the hash code for some
180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     data. It is the intended product of hashing, and can be used to implement
190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     hash tables, checksumming, and other common uses of hashes. It is not an
200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     integer type (although it can be converted to one) because it is risky
210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     to assume much about the internals of a hash_code. In particular, each
220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     execution of the program has a high probability of producing a different
230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     hash_code for a given input. Thus their values are not stable to save or
240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     persist, and should only be used during the execution for the
250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     construction of hashing datastructures.
260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//  -- 'hash_value' is a function designed to be overloaded for each
280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     user-defined type which wishes to be used within a hashing context. It
290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     should be overloaded within the user-defined type's namespace and found
300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//     via ADL. Overloads for primitive types are provided by this library.
310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//  -- 'hash_combine' and 'hash_combine_range' are functions designed to aid
330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//      programmers in easily and intuitively combining a set of data into
340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//      a single hash_code for their object. They should only logically be used
350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//      within the implementation of a 'hash_value' routine or similar context.
360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// Note that 'hash_combine_range' contains very special logic for hashing
380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// a contiguous array of integers or pointers. This logic is *extremely* fast,
390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were
400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys
410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// under 32-bytes.
421a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//
431a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//===----------------------------------------------------------------------===//
441a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
451a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#ifndef LLVM_ADT_HASHING_H
461a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#define LLVM_ADT_HASHING_H
471a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include "llvm/ADT/STLExtras.h"
491a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#include "llvm/Support/DataTypes.h"
50dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth#include "llvm/Support/Host.h"
51dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth#include "llvm/Support/SwapByteOrder.h"
520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include "llvm/Support/type_traits.h"
530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <algorithm>
540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <cassert>
556592eacf9006d046e8bc4999600e2973a3b56eacJay Foad#include <cstring>
560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <iterator>
570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <utility>
580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// Allow detecting C++11 feature availability when building with Clang without
600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// breaking other compilers.
610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#ifndef __has_feature
620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth# define __has_feature(x) 0
630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#endif
641a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
651a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talinnamespace llvm {
661a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief An opaque object representing a hash code.
680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This object represents the result of hashing some entity. It is intended to
700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// be used to implement hashtables or other hashing-based data structures.
710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// While it wraps and exposes a numeric value, this value should not be
720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// trusted to be stable or predictable across processes or executions.
730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// In order to obtain the hash_code for an object 'x':
750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \code
760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///   using llvm::hash_value;
770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///   llvm::hash_code code = hash_value(x);
780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \endcode
790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthclass hash_code {
800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t value;
810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
821a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talinpublic:
834166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  /// \brief Default construct a hash_code.
844166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  /// Note that this leaves the value uninitialized.
854166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  hash_code() {}
861a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Form a hash code directly from a numerical value.
884166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  hash_code(size_t value) : value(value) {}
891a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Convert the hash code to its numerical value for use.
910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /*explicit*/ operator size_t() const { return value; }
920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return lhs.value == rhs.value;
950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend bool operator!=(const hash_code &lhs, const hash_code &rhs) {
970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return lhs.value != rhs.value;
981a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
991a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
1000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Allow a hash_code to be directly run through hash_value.
1010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend size_t hash_value(const hash_code &code) { return code.value; }
1020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth};
1030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
104c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for any integer value.
105c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
106c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Note that this function is intended to compute the same hash_code for
107c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// a particular value without regard to the pre-promotion type. This is in
108c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// contrast to hash_combine which may produce different hash_codes for
109c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// differing argument types even if they would implicit promote to a common
110c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// type without changing the value.
111c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T>
112d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruthtypename enable_if<is_integral_or_enum<T>, hash_code>::type hash_value(T value);
113c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
114c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pointer's address.
115c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
116c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// N.B.: This hashes the *address*. Not the value and not the type.
117c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr);
118c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
119c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pair of objects.
120c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U>
121c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg);
122c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
1239406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth/// \brief Compute a hash_code for a standard string.
1249406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthtemplate <typename T>
1259406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthhash_code hash_value(const std::basic_string<T> &arg);
1269406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth
127c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
128c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Override the execution seed with a fixed value.
129c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
130c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// This hashing library uses a per-execution seed designed to change on each
131c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// run with high probability in order to ensure that the hash codes are not
132c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// attackable and to ensure that output which is intended to be stable does
133c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// not rely on the particulars of the hash codes produced.
134c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
135c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// That said, there are use cases where it is important to be able to
136c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// reproduce *exactly* a specific behavior. To that end, we provide a function
137c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// which will forcibly set the seed to a fixed value. This must be done at the
138c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// start of the program, before any hashes are computed. Also, it cannot be
139c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// undone. This makes it thread-hostile and very hard to use outside of
140c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// immediately on start of a simple program designed for reproducible
141c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// behavior.
142c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthvoid set_fixed_execution_hash_seed(size_t fixed_value);
143c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
1440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// All of the implementation details of actually computing the various hash
1460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// code values are held within this namespace. These routines are included in
1470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// the header file mainly to allow inlining and constant propagation.
1480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace hashing {
1490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace detail {
1500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t fetch64(const char *p) {
1520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t result;
1530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  memcpy(&result, p, sizeof(result));
154dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth  if (sys::isBigEndianHost())
155dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth    return sys::SwapByteOrder(result);
1560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return result;
1570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint32_t fetch32(const char *p) {
1600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint32_t result;
1610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  memcpy(&result, p, sizeof(result));
162dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth  if (sys::isBigEndianHost())
163dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth    return sys::SwapByteOrder(result);
1640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return result;
1650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Some primes between 2^63 and 2^64 for various uses.
1680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
1690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k1 = 0xb492b66fbe98f273ULL;
1700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k2 = 0x9ae16a3b2f90404fULL;
1710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k3 = 0xc949d7c7509e6557ULL;
1720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Bitwise right rotate.
1740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Normally this will compile to a single instruction, especially if the
1750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// shift is a manifest constant.
1760c7374d87e6bf002028cd19f8ae9547927c9c645Benjamin Kramerinline uint64_t rotate(uint64_t val, size_t shift) {
1770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Avoid shifting by 64: doing so yields an undefined result.
1780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
1790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t shift_mix(uint64_t val) {
1820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return val ^ (val >> 47);
1830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
1860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Murmur-inspired hashing.
1870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const uint64_t kMul = 0x9ddfea08eb382d69ULL;
1880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = (low ^ high) * kMul;
1890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a ^= (a >> 47);
1900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t b = (high ^ a) * kMul;
1910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  b ^= (b >> 47);
1920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  b *= kMul;
1930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return b;
1940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) {
1970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint8_t a = s[0];
1980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint8_t b = s[len >> 1];
1990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint8_t c = s[len - 1];
2000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
2010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint32_t z = len + (static_cast<uint32_t>(c) << 2);
2020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return shift_mix(y * k2 ^ z * k3 ^ seed) * k2;
2030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) {
2060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = fetch32(s);
2070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4));
2080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) {
211171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer  uint64_t a = fetch64(s);
212171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer  uint64_t b = fetch64(s + len - 8);
213171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer  return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
2140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
2170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = fetch64(s) * k1;
2180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t b = fetch64(s + 8);
2190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t c = fetch64(s + len - 8) * k2;
2200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t d = fetch64(s + len - 16) * k0;
2210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d,
2220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                       a + rotate(b ^ k3, 20) - c + len + seed);
2230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
2260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t z = fetch64(s + 24);
2270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
2280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t b = rotate(a + z, 52);
2290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t c = rotate(a, 37);
2300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + 8);
2310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  c += rotate(a, 7);
2320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + 16);
2330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t vf = a + z;
2340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t vs = b + rotate(a, 31) + c;
2350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a = fetch64(s + 16) + fetch64(s + len - 32);
2360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  z = fetch64(s + len - 8);
2370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  b = rotate(a + z, 52);
2380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  c = rotate(a, 37);
2390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + len - 24);
2400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  c += rotate(a, 7);
2410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + len - 16);
2420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t wf = a + z;
2430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t ws = b + rotate(a, 31) + c;
2440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
245b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth  return shift_mix((seed ^ (r * k0)) + vs) * k2;
2460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
2490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length >= 4 && length <= 8)
2504166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_4to8_bytes(s, length, seed);
2514166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length > 8 && length <= 16)
2524166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_9to16_bytes(s, length, seed);
2534166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length > 16 && length <= 32)
2544166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_17to32_bytes(s, length, seed);
2554166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length > 32)
2564166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_33to64_bytes(s, length, seed);
2574166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length != 0)
2584166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_1to3_bytes(s, length, seed);
2594166989f10eaecfb357551788a3d91275e75f119Chandler Carruth
2604166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  return k2 ^ seed;
2610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief The intermediate state used during hashing.
2640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Currently, the algorithm for computing hash codes is based on CityHash and
2650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// keeps 56 bytes of arbitrary state.
2660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstruct hash_state {
2670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t h0, h1, h2, h3, h4, h5, h6;
2680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t seed;
2690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Create a new hash_state structure and initialize it based on the
2710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// seed and the first 64-byte chunk.
2720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This effectively performs the initial mix.
2730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static hash_state create(const char *s, uint64_t seed) {
2740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    hash_state state = {
2750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49),
276a2a9b9e7752f1eeb8c158112fc940ff67b04d7a1Daniel Dunbar      seed * k1, shift_mix(seed), 0, seed };
277a2a9b9e7752f1eeb8c158112fc940ff67b04d7a1Daniel Dunbar    state.h6 = hash_16_bytes(state.h4, state.h5);
2780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s);
2790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return state;
2801a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
2811a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
2820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Mix 32-bytes from the input sequence into the 16-bytes of 'a'
2830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// and 'b', including whatever is already in 'a' and 'b'.
2840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
2850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += fetch64(s);
2860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t c = fetch64(s + 24);
2870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    b = rotate(b + a + c, 21);
2880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t d = a;
2890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += fetch64(s + 8) + fetch64(s + 16);
2900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    b += rotate(a, 44) + d;
2910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += c;
2921a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
2931a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
2940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Mix in a 64-byte buffer of data.
2950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// We mix all 64 bytes even when the chunk length is smaller, but we
2960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// record the actual length.
2970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  void mix(const char *s) {
2980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
2990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1;
3000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h0 ^= h6;
3010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h1 += h3 + fetch64(s + 40);
3020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h2 = rotate(h2 + h5, 33) * k1;
3030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h3 = h4 * k1;
3040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h4 = h0 + h5;
3050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    mix_32_bytes(s, h3, h4);
3060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h5 = h2 + h6;
3070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h6 = h1 + fetch64(s + 16);
3080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    mix_32_bytes(s + 32, h5, h6);
3090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::swap(h2, h0);
3101a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
3111a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
3120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Compute the final 64-bit hash code value based on the current
3130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// state and the length of bytes hashed.
3140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t finalize(size_t length) {
3154166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
3164166989f10eaecfb357551788a3d91275e75f119Chandler Carruth                         hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
3171a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
3180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth};
3191a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
3200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief A global, fixed seed-override variable.
3220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variable can be set using the \see llvm::set_fixed_execution_seed
3240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// function. See that function for details. Do not, under any circumstances,
3250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// set or read this variable.
3260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthextern size_t fixed_seed_override;
3270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline size_t get_execution_seed() {
3290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // FIXME: This needs to be a per-execution seed. This is just a placeholder
3300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // implementation. Switching to a per-execution seed is likely to flush out
3310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // instability bugs and so will happen as its own commit.
3320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  //
3330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // However, if there is a fixed seed override set the first time this is
3340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // called, return that instead of the per-execution seed.
335b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth  const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
3360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static size_t seed = fixed_seed_override ? fixed_seed_override
337179bc7cb597c98045fae055fae754da5690ba187Chandler Carruth                                           : (size_t)seed_prime;
3380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return seed;
3390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Trait to indicate whether a type's bits can be hashed directly.
3430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// A type trait which is true if we want to combine values for hashing by
3450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// reading the underlying data. It is false if values of this type must
3460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// first be passed to hash_value, and the resulting hash_codes combined.
3470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
348d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// FIXME: We want to replace is_integral_or_enum and is_pointer here with
349d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// a predicate which asserts that comparing the underlying storage of two
350d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// values of the type for equality is equivalent to comparing the two values
351d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// for equality. For all the platforms we care about, this holds for integers
352d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// and pointers, but there are platforms where it doesn't and we would like to
353d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// support user-defined types which happen to satisfy this property.
3540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T> struct is_hashable_data
355d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth  : integral_constant<bool, ((is_integral_or_enum<T>::value ||
356d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth                              is_pointer<T>::value) &&
3570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                             64 % sizeof(T) == 0)> {};
3580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3594d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// Special case std::pair to detect when both types are viable and when there
3604d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// is no alignment-derived padding in the pair. This is a bit of a lie because
3614d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// std::pair isn't truly POD, but it's close enough in all reasonable
3624d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// implementations for our use case of hashing the underlying data.
3634d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruthtemplate <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
3644d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth  : integral_constant<bool, (is_hashable_data<T>::value &&
3654d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth                             is_hashable_data<U>::value &&
3661c1448984d43f1f02c0235d35ebe8460c9b57afdChandler Carruth                             (sizeof(T) + sizeof(U)) ==
3671c1448984d43f1f02c0235d35ebe8460c9b57afdChandler Carruth                              sizeof(std::pair<T, U>))> {};
3684d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth
3690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to get the hashable data representation for a type.
3700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variant is enabled when the type itself can be used.
3710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T>
3720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtypename enable_if<is_hashable_data<T>, T>::type
3730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthget_hashable_data(const T &value) {
3740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return value;
3750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to get the hashable data representation for a type.
3770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variant is enabled when we must first call hash_value and use the
3780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// result as our data.
3790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T>
3800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtypename enable_if_c<!is_hashable_data<T>::value, size_t>::type
3810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthget_hashable_data(const T &value) {
3820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  using ::llvm::hash_value;
3830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return hash_value(value);
3840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to store data from a value into a buffer and advance the
3870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// pointer into that buffer.
3880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This routine first checks whether there is enough space in the provided
3900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// buffer, and if not immediately returns false. If there is space, it
3910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// copies the underlying bytes of value into the buffer, advances the
3920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// buffer_ptr past the copied bytes, and returns true.
3930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T>
3940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthbool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
3950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                       size_t offset = 0) {
3960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t store_size = sizeof(value) - offset;
3970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (buffer_ptr + store_size > buffer_end)
3980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return false;
3990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *value_data = reinterpret_cast<const char *>(&value);
4000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  memcpy(buffer_ptr, value_data + offset, store_size);
4010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  buffer_ptr += store_size;
4020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return true;
4030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Implement the combining of integral values into a hash_code.
4060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This overload is selected when the value type of the iterator is
4080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// integral. Rather than computing a hash_code for each object and then
4090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// combining them, this (as an optimization) directly combines the integers.
4100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename InputIteratorT>
4110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
4120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t seed = get_execution_seed();
4130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char buffer[64], *buffer_ptr = buffer;
4140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char *const buffer_end = buffer_ptr + array_lengthof(buffer);
4150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (first != last && store_and_advance(buffer_ptr, buffer_end,
4160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                            get_hashable_data(*first)))
4170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    ++first;
4180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (first == last)
4190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_short(buffer, buffer_ptr - buffer, seed);
4200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  assert(buffer_ptr == buffer_end);
4210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state = state.create(buffer, seed);
4230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t length = 64;
4240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (first != last) {
4250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Fill up the buffer. We don't clear it, which re-mixes the last round
4260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // when only a partial 64-byte chunk is left.
4270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    buffer_ptr = buffer;
4280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    while (first != last && store_and_advance(buffer_ptr, buffer_end,
4290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                              get_hashable_data(*first)))
4300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      ++first;
4310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Rotate the buffer if we did a partial fill in order to simulate doing
4330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // a mix of the last 64-bytes. That is how the algorithm works when we
4340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // have a contiguous byte sequence, and we want to emulate that here.
4350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::rotate(buffer, buffer_ptr, buffer_end);
4360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Mix this chunk into the current state.
4380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(buffer);
4390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    length += buffer_ptr - buffer;
4400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  };
4410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return state.finalize(length);
4430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Implement the combining of integral values into a hash_code.
4460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This overload is selected when the value type of the iterator is integral
4480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and when the input iterator is actually a pointer. Rather than computing
4490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a hash_code for each object and then combining them, this (as an
4500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// optimization) directly combines the integers. Also, because the integers
4510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// are stored in contiguous memory, this routine avoids copying each value
4520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and directly reads from the underlying memory.
4530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename ValueT>
4540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtypename enable_if<is_hashable_data<ValueT>, hash_code>::type
455005874056e536324cba9fa253d7e14a6f9dcf4faChandler Carruthhash_combine_range_impl(ValueT *first, ValueT *last) {
4560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t seed = get_execution_seed();
4570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_begin = reinterpret_cast<const char *>(first);
4580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_end = reinterpret_cast<const char *>(last);
4590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t length = std::distance(s_begin, s_end);
4600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length <= 64)
4610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_short(s_begin, length, seed);
4620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_aligned_end = s_begin + (length & ~63);
4640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state = state.create(s_begin, seed);
4650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  s_begin += 64;
4660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (s_begin != s_aligned_end) {
4670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s_begin);
4680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    s_begin += 64;
4691a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
4700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length & 63)
4710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s_end - 64);
4721a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
4730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return state.finalize(length);
4740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace detail
4770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace hashing
4780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Compute a hash_code for a sequence of values.
4810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This hashes a sequence of values. It produces the same hash_code as
4830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
4840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and is significantly faster given pointers and types which can be hashed as
4850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a sequence of bytes.
4860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename InputIteratorT>
4870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
4880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
4890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// Implementation details for hash_combine.
4930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace hashing {
4940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace detail {
4950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper class to manage the recursive combining of hash_combine
4970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// arguments.
4980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This class exists to manage the state and various calls involved in the
5000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// recursive combining of arguments used in hash_combine. It is particularly
5010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// useful at minimizing the code in the recursive calls to ease the pain
5020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// caused by a lack of variadic functions.
5035cd79bc14ca51019af4db735d13eac95dab088edChandler Carruthstruct hash_combine_recursive_helper {
5040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char buffer[64];
5050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state;
5065cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  const size_t seed;
5070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthpublic:
5090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Construct a recursive hash combining helper.
5100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This sets up the state for a recursive hash combine, including getting
5120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// the seed and buffer setup.
5130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_combine_recursive_helper()
5145cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    : seed(get_execution_seed()) {}
5150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Combine one chunk of data into the current in-flight hash.
5170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This merges one chunk of data into the hash. First it tries to buffer
5190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// the data. If the buffer is full, it hashes the buffer into its
5200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// hash_state, empties it, and then merges the new chunk in. This also
5210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// handles cases where the data straddles the end of the buffer.
5225cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  template <typename T>
5235cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
5240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    if (!store_and_advance(buffer_ptr, buffer_end, data)) {
5250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Check for skew which prevents the buffer from being packed, and do
5260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // a partial store into the buffer to fill it. This is only a concern
5270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // with the variadic combine because that formation can have varying
5280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // argument types.
5290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      size_t partial_store_size = buffer_end - buffer_ptr;
5300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      memcpy(buffer_ptr, &data, partial_store_size);
5310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // If the store fails, our buffer is full and ready to hash. We have to
5330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // either initialize the hash state (on the first full buffer) or mix
5340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // this buffer into the existing hash state. Length tracks the *hashed*
5350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // length, not the buffered length.
5360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      if (length == 0) {
5370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        state = state.create(buffer, seed);
5380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        length = 64;
5390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      } else {
5400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        // Mix this chunk into the current state and bump length up by 64.
5410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        state.mix(buffer);
5420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        length += 64;
5430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      }
5440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Reset the buffer_ptr to the head of the buffer for the next chunk of
5450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // data.
5460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      buffer_ptr = buffer;
5470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Try again to store into the buffer -- this cannot fail as we only
5490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // store types smaller than the buffer.
5500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      if (!store_and_advance(buffer_ptr, buffer_end, data,
5510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                             partial_store_size))
5520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        abort();
5531a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin    }
5545cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return buffer_ptr;
5556592eacf9006d046e8bc4999600e2973a3b56eacJay Foad  }
5561a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
5570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#if defined(__has_feature) && __has_feature(__cxx_variadic_templates__)
5580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Recursive, variadic combining method.
5600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This function recurses through each argument, combining that argument
5620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// into a single hash.
5630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T, typename ...Ts>
5645cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
5655cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T &arg, const Ts &...args) {
5665cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
5670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Recurse to the next argument.
5695cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end, args...);
5700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#else
5730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Manually expanded recursive combining methods. See variadic above for
5740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // documentation.
5750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3, typename T4, typename T5,
5770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth            typename T6>
5785cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
5795cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T1 &arg1, const T2 &arg2, const T3 &arg3,
5800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                    const T4 &arg4, const T5 &arg5, const T6 &arg6) {
5815cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
5825cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6);
5830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3, typename T4, typename T5>
5855cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
5865cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T1 &arg1, const T2 &arg2, const T3 &arg3,
5870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                    const T4 &arg4, const T5 &arg5) {
5885cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
5895cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5);
5900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3, typename T4>
5925cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
5935cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T1 &arg1, const T2 &arg2, const T3 &arg3,
5940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                    const T4 &arg4) {
5955cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
5965cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4);
5970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3>
5995cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
6005cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T1 &arg1, const T2 &arg2, const T3 &arg3) {
6015cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
6025cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end, arg2, arg3);
6030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
6040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2>
6055cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
6065cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T1 &arg1, const T2 &arg2) {
6075cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
6085cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end, arg2);
6090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
6100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1>
6115cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
6125cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T1 &arg1) {
6135cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1));
6145cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end);
6150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
6160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#endif
6180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Base case for recursive, variadic combining.
6200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
6210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// The base case when combining arguments recursively is reached when all
6220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// arguments have been handled. It flushes the remaining buffer and
6230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// constructs a hash_code.
6245cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
6250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Check whether the entire set of values fit in the buffer. If so, we'll
6260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // use the optimized short hashing routine and skip state entirely.
6270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    if (length == 0)
6280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      return hash_short(buffer, buffer_ptr - buffer, seed);
6290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Mix the final buffer, rotating it if we did a partial fill in order to
6310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // simulate doing a mix of the last 64-bytes. That is how the algorithm
6320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // works when we have a contiguous byte sequence, and we want to emulate
6330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // that here.
6340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::rotate(buffer, buffer_ptr, buffer_end);
6350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Mix this chunk into the current state.
6370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(buffer);
6380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    length += buffer_ptr - buffer;
6390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return state.finalize(length);
6411a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
6421a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin};
6431a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
6440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace detail
6450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace hashing
6460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#if __has_feature(__cxx_variadic_templates__)
6490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Combine values into a single hash_code.
6510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
6520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This routine accepts a varying number of arguments of any type. It will
6530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// attempt to combine them into a single hash_code. For user-defined types it
6540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// attempts to call a \see hash_value overload (via ADL) for the type. For
6550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// integer and pointer types it directly combines their data into the
6560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// resulting hash_code.
6570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
6580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// The result is suitable for returning from a user's hash_value
6590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// *implementation* for their user-defined type. Consumers of a type should
6600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// *not* call this routine, they should instead call 'hash_value'.
6610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename ...Ts> hash_code hash_combine(const Ts &...args) {
6620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Recursively hash each argument using a helper class.
6630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6645cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
6650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#else
6680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// What follows are manually exploded overloads for each argument width. See
6700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// the above variadic definition for documentation and specification.
6710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
6720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2, typename T3, typename T4, typename T5,
6730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth          typename T6>
6740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
6755cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                       const T4 &arg4, const T5 &arg5, const T6 &arg6) {
6760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6775cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64,
6785cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                        arg1, arg2, arg3, arg4, arg5, arg6);
6790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2, typename T3, typename T4, typename T5>
6810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
6825cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                       const T4 &arg4, const T5 &arg5) {
6830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6845cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64,
6855cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                        arg1, arg2, arg3, arg4, arg5);
6860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2, typename T3, typename T4>
6880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
6895cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                       const T4 &arg4) {
6900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6915cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64,
6925cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                        arg1, arg2, arg3, arg4);
6930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2, typename T3>
6950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3) {
6960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6975cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3);
6980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2>
7000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2) {
7010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
7025cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2);
7030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
7040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1>
7050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1) {
7060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
7075cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64, arg1);
7080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
7090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
7100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#endif
7110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
712c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
713c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Implementation details for implementatinos of hash_value overloads provided
714c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// here.
715c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace hashing {
716c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace detail {
717c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
718c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Helper to hash the value of a single integer.
719c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
720c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Overloads for smaller integer types are not provided to ensure consistent
721c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// behavior in the presence of integral promotions. Essentially,
722c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
723c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthinline hash_code hash_integer_value(uint64_t value) {
724c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  // Similar to hash_4to8_bytes but using a seed instead of length.
725c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const uint64_t seed = get_execution_seed();
726c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const char *s = reinterpret_cast<const char *>(&value);
727c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const uint64_t a = fetch32(s);
728c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
729c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
730c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
731c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace detail
732c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace hashing
733c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
734c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
735c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
736c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T>
737d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruthtypename enable_if<is_integral_or_enum<T>, hash_code>::type
738d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruthhash_value(T value) {
739c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return ::llvm::hashing::detail::hash_integer_value(value);
740c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
741c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
742c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
743c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
744c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr) {
745c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return ::llvm::hashing::detail::hash_integer_value(
746c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth    reinterpret_cast<uintptr_t>(ptr));
747c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
748c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
749c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
750c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
751c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U>
752c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg) {
753c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return hash_combine(arg.first, arg.second);
754c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
755c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
7569406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
7579406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth// infrastructure is available.
7589406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthtemplate <typename T>
7599406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthhash_code hash_value(const std::basic_string<T> &arg) {
7609406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth  return hash_combine_range(arg.begin(), arg.end());
7619406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth}
7629406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth
7630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace llvm
7641a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
7651a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#endif
766