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
481a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#include "llvm/Support/DataTypes.h"
49dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth#include "llvm/Support/Host.h"
50dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth#include "llvm/Support/SwapByteOrder.h"
510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include "llvm/Support/type_traits.h"
520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <algorithm>
530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <cassert>
546592eacf9006d046e8bc4999600e2973a3b56eacJay Foad#include <cstring>
556948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include <string>
560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <utility>
570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
581a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talinnamespace llvm {
591a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief An opaque object representing a hash code.
610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This object represents the result of hashing some entity. It is intended to
630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// be used to implement hashtables or other hashing-based data structures.
640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// While it wraps and exposes a numeric value, this value should not be
650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// trusted to be stable or predictable across processes or executions.
660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// In order to obtain the hash_code for an object 'x':
680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \code
690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///   using llvm::hash_value;
700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///   llvm::hash_code code = hash_value(x);
710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \endcode
720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthclass hash_code {
730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t value;
740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
751a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talinpublic:
764166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  /// \brief Default construct a hash_code.
774166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  /// Note that this leaves the value uninitialized.
780c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  hash_code() = default;
791a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Form a hash code directly from a numerical value.
814166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  hash_code(size_t value) : value(value) {}
821a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Convert the hash code to its numerical value for use.
840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /*explicit*/ operator size_t() const { return value; }
850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return lhs.value == rhs.value;
880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend bool operator!=(const hash_code &lhs, const hash_code &rhs) {
900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return lhs.value != rhs.value;
911a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
921a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Allow a hash_code to be directly run through hash_value.
940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend size_t hash_value(const hash_code &code) { return code.value; }
950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth};
960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
97c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for any integer value.
98c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
99c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Note that this function is intended to compute the same hash_code for
100c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// a particular value without regard to the pre-promotion type. This is in
101c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// contrast to hash_combine which may produce different hash_codes for
102c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// differing argument types even if they would implicit promote to a common
103c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// type without changing the value.
104c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T>
10536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
10636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hineshash_value(T value);
107c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
108c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pointer's address.
109c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
110c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// N.B.: This hashes the *address*. Not the value and not the type.
111c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr);
112c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
113c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pair of objects.
114c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U>
115c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg);
116c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
1179406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth/// \brief Compute a hash_code for a standard string.
1189406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthtemplate <typename T>
1199406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthhash_code hash_value(const std::basic_string<T> &arg);
1209406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth
121c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
122c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Override the execution seed with a fixed value.
123c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
124c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// This hashing library uses a per-execution seed designed to change on each
125c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// run with high probability in order to ensure that the hash codes are not
126c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// attackable and to ensure that output which is intended to be stable does
127c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// not rely on the particulars of the hash codes produced.
128c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
129c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// That said, there are use cases where it is important to be able to
130c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// reproduce *exactly* a specific behavior. To that end, we provide a function
131c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// which will forcibly set the seed to a fixed value. This must be done at the
132c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// start of the program, before any hashes are computed. Also, it cannot be
133c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// undone. This makes it thread-hostile and very hard to use outside of
134c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// immediately on start of a simple program designed for reproducible
135c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// behavior.
136c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthvoid set_fixed_execution_hash_seed(size_t fixed_value);
137c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
1380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// All of the implementation details of actually computing the various hash
1400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// code values are held within this namespace. These routines are included in
1410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// the header file mainly to allow inlining and constant propagation.
1420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace hashing {
1430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace detail {
1440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t fetch64(const char *p) {
1460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t result;
1470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  memcpy(&result, p, sizeof(result));
14821a01d1ea89dba97c4f9e1f9f41485729a4046bcRafael Espindola  if (sys::IsBigEndianHost)
149c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    sys::swapByteOrder(result);
1500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return result;
1510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint32_t fetch32(const char *p) {
1540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint32_t result;
1550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  memcpy(&result, p, sizeof(result));
15621a01d1ea89dba97c4f9e1f9f41485729a4046bcRafael Espindola  if (sys::IsBigEndianHost)
157c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    sys::swapByteOrder(result);
1580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return result;
1590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Some primes between 2^63 and 2^64 for various uses.
1620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
1630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k1 = 0xb492b66fbe98f273ULL;
1640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k2 = 0x9ae16a3b2f90404fULL;
1650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k3 = 0xc949d7c7509e6557ULL;
1660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Bitwise right rotate.
1680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Normally this will compile to a single instruction, especially if the
1690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// shift is a manifest constant.
1700c7374d87e6bf002028cd19f8ae9547927c9c645Benjamin Kramerinline uint64_t rotate(uint64_t val, size_t shift) {
1710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Avoid shifting by 64: doing so yields an undefined result.
1720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
1730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t shift_mix(uint64_t val) {
1760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return val ^ (val >> 47);
1770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
1800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Murmur-inspired hashing.
1810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const uint64_t kMul = 0x9ddfea08eb382d69ULL;
1820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = (low ^ high) * kMul;
1830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a ^= (a >> 47);
1840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t b = (high ^ a) * kMul;
1850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  b ^= (b >> 47);
1860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  b *= kMul;
1870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return b;
1880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) {
1910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint8_t a = s[0];
1920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint8_t b = s[len >> 1];
1930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint8_t c = s[len - 1];
1940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
1950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint32_t z = len + (static_cast<uint32_t>(c) << 2);
1960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return shift_mix(y * k2 ^ z * k3 ^ seed) * k2;
1970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
1980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
1990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) {
2000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = fetch32(s);
2010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4));
2020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) {
205171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer  uint64_t a = fetch64(s);
206171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer  uint64_t b = fetch64(s + len - 8);
207171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer  return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
2080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
2110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = fetch64(s) * k1;
2120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t b = fetch64(s + 8);
2130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t c = fetch64(s + len - 8) * k2;
2140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t d = fetch64(s + len - 16) * k0;
2150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d,
2160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                       a + rotate(b ^ k3, 20) - c + len + seed);
2170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
2200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t z = fetch64(s + 24);
2210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
2220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t b = rotate(a + z, 52);
2230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t c = rotate(a, 37);
2240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + 8);
2250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  c += rotate(a, 7);
2260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + 16);
2270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t vf = a + z;
2280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t vs = b + rotate(a, 31) + c;
2290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a = fetch64(s + 16) + fetch64(s + len - 32);
2300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  z = fetch64(s + len - 8);
2310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  b = rotate(a + z, 52);
2320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  c = rotate(a, 37);
2330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + len - 24);
2340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  c += rotate(a, 7);
2350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  a += fetch64(s + len - 16);
2360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t wf = a + z;
2370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t ws = b + rotate(a, 31) + c;
2380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
239b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth  return shift_mix((seed ^ (r * k0)) + vs) * k2;
2400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
2430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length >= 4 && length <= 8)
2444166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_4to8_bytes(s, length, seed);
2454166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length > 8 && length <= 16)
2464166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_9to16_bytes(s, length, seed);
2474166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length > 16 && length <= 32)
2484166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_17to32_bytes(s, length, seed);
2494166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length > 32)
2504166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_33to64_bytes(s, length, seed);
2514166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  if (length != 0)
2524166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_1to3_bytes(s, length, seed);
2534166989f10eaecfb357551788a3d91275e75f119Chandler Carruth
2544166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  return k2 ^ seed;
2550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
2560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief The intermediate state used during hashing.
2580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Currently, the algorithm for computing hash codes is based on CityHash and
2590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// keeps 56 bytes of arbitrary state.
2600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstruct hash_state {
2610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t h0, h1, h2, h3, h4, h5, h6;
2620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
2630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Create a new hash_state structure and initialize it based on the
2640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// seed and the first 64-byte chunk.
2650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This effectively performs the initial mix.
2660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static hash_state create(const char *s, uint64_t seed) {
2670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    hash_state state = {
2680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49),
269dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      seed * k1, shift_mix(seed), 0 };
270a2a9b9e7752f1eeb8c158112fc940ff67b04d7a1Daniel Dunbar    state.h6 = hash_16_bytes(state.h4, state.h5);
2710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s);
2720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return state;
2731a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
2741a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
2750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Mix 32-bytes from the input sequence into the 16-bytes of 'a'
2760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// and 'b', including whatever is already in 'a' and 'b'.
2770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
2780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += fetch64(s);
2790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t c = fetch64(s + 24);
2800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    b = rotate(b + a + c, 21);
2810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t d = a;
2820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += fetch64(s + 8) + fetch64(s + 16);
2830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    b += rotate(a, 44) + d;
2840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += c;
2851a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
2861a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
2870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Mix in a 64-byte buffer of data.
2880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// We mix all 64 bytes even when the chunk length is smaller, but we
2890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// record the actual length.
2900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  void mix(const char *s) {
2910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
2920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1;
2930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h0 ^= h6;
2940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h1 += h3 + fetch64(s + 40);
2950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h2 = rotate(h2 + h5, 33) * k1;
2960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h3 = h4 * k1;
2970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h4 = h0 + h5;
2980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    mix_32_bytes(s, h3, h4);
2990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h5 = h2 + h6;
3000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h6 = h1 + fetch64(s + 16);
3010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    mix_32_bytes(s + 32, h5, h6);
3020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::swap(h2, h0);
3031a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
3041a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
3050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Compute the final 64-bit hash code value based on the current
3060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// state and the length of bytes hashed.
3070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t finalize(size_t length) {
3084166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
3094166989f10eaecfb357551788a3d91275e75f119Chandler Carruth                         hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
3101a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
3110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth};
3121a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
3130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief A global, fixed seed-override variable.
3150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variable can be set using the \see llvm::set_fixed_execution_seed
3170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// function. See that function for details. Do not, under any circumstances,
3180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// set or read this variable.
3190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthextern size_t fixed_seed_override;
3200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline size_t get_execution_seed() {
3220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // FIXME: This needs to be a per-execution seed. This is just a placeholder
3230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // implementation. Switching to a per-execution seed is likely to flush out
3240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // instability bugs and so will happen as its own commit.
3250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  //
3260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // However, if there is a fixed seed override set the first time this is
3270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // called, return that instead of the per-execution seed.
328b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth  const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
3290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static size_t seed = fixed_seed_override ? fixed_seed_override
330179bc7cb597c98045fae055fae754da5690ba187Chandler Carruth                                           : (size_t)seed_prime;
3310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return seed;
3320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Trait to indicate whether a type's bits can be hashed directly.
3360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// A type trait which is true if we want to combine values for hashing by
3380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// reading the underlying data. It is false if values of this type must
3390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// first be passed to hash_value, and the resulting hash_codes combined.
3400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
341d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// FIXME: We want to replace is_integral_or_enum and is_pointer here with
342d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// a predicate which asserts that comparing the underlying storage of two
343d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// values of the type for equality is equivalent to comparing the two values
344d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// for equality. For all the platforms we care about, this holds for integers
345d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// and pointers, but there are platforms where it doesn't and we would like to
346d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// support user-defined types which happen to satisfy this property.
3470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T> struct is_hashable_data
34836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
34936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                   std::is_pointer<T>::value) &&
35036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                  64 % sizeof(T) == 0)> {};
3510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3524d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// Special case std::pair to detect when both types are viable and when there
3534d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// is no alignment-derived padding in the pair. This is a bit of a lie because
3544d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// std::pair isn't truly POD, but it's close enough in all reasonable
3554d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// implementations for our use case of hashing the underlying data.
3564d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruthtemplate <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
35736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  : std::integral_constant<bool, (is_hashable_data<T>::value &&
35836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                  is_hashable_data<U>::value &&
35936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                  (sizeof(T) + sizeof(U)) ==
36036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                   sizeof(std::pair<T, U>))> {};
3614d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth
3620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to get the hashable data representation for a type.
3630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variant is enabled when the type itself can be used.
3640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T>
36536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_hashable_data<T>::value, T>::type
3660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthget_hashable_data(const T &value) {
3670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return value;
3680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to get the hashable data representation for a type.
3700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variant is enabled when we must first call hash_value and use the
3710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// result as our data.
3720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T>
37336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<!is_hashable_data<T>::value, size_t>::type
3740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthget_hashable_data(const T &value) {
3750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  using ::llvm::hash_value;
3760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return hash_value(value);
3770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to store data from a value into a buffer and advance the
3800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// pointer into that buffer.
3810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This routine first checks whether there is enough space in the provided
3830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// buffer, and if not immediately returns false. If there is space, it
3840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// copies the underlying bytes of value into the buffer, advances the
3850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// buffer_ptr past the copied bytes, and returns true.
3860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T>
3870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthbool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
3880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                       size_t offset = 0) {
3890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t store_size = sizeof(value) - offset;
3900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (buffer_ptr + store_size > buffer_end)
3910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return false;
3920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *value_data = reinterpret_cast<const char *>(&value);
3930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  memcpy(buffer_ptr, value_data + offset, store_size);
3940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  buffer_ptr += store_size;
3950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return true;
3960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Implement the combining of integral values into a hash_code.
3990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This overload is selected when the value type of the iterator is
4010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// integral. Rather than computing a hash_code for each object and then
4020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// combining them, this (as an optimization) directly combines the integers.
4030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename InputIteratorT>
4040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
4050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t seed = get_execution_seed();
4060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char buffer[64], *buffer_ptr = buffer;
407dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  char *const buffer_end = std::end(buffer);
4080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (first != last && store_and_advance(buffer_ptr, buffer_end,
4090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                            get_hashable_data(*first)))
4100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    ++first;
4110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (first == last)
4120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_short(buffer, buffer_ptr - buffer, seed);
4130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  assert(buffer_ptr == buffer_end);
4140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state = state.create(buffer, seed);
4160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t length = 64;
4170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (first != last) {
4180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Fill up the buffer. We don't clear it, which re-mixes the last round
4190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // when only a partial 64-byte chunk is left.
4200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    buffer_ptr = buffer;
4210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    while (first != last && store_and_advance(buffer_ptr, buffer_end,
4220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                              get_hashable_data(*first)))
4230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      ++first;
4240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Rotate the buffer if we did a partial fill in order to simulate doing
4260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // a mix of the last 64-bytes. That is how the algorithm works when we
4270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // have a contiguous byte sequence, and we want to emulate that here.
4280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::rotate(buffer, buffer_ptr, buffer_end);
4290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Mix this chunk into the current state.
4310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(buffer);
4320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    length += buffer_ptr - buffer;
4330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  };
4340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return state.finalize(length);
4360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Implement the combining of integral values into a hash_code.
4390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This overload is selected when the value type of the iterator is integral
4410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and when the input iterator is actually a pointer. Rather than computing
4420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a hash_code for each object and then combining them, this (as an
4430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// optimization) directly combines the integers. Also, because the integers
4440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// are stored in contiguous memory, this routine avoids copying each value
4450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and directly reads from the underlying memory.
4460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename ValueT>
44736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_hashable_data<ValueT>::value, hash_code>::type
448005874056e536324cba9fa253d7e14a6f9dcf4faChandler Carruthhash_combine_range_impl(ValueT *first, ValueT *last) {
4490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t seed = get_execution_seed();
4500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_begin = reinterpret_cast<const char *>(first);
4510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_end = reinterpret_cast<const char *>(last);
4520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t length = std::distance(s_begin, s_end);
4530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length <= 64)
4540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_short(s_begin, length, seed);
4550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_aligned_end = s_begin + (length & ~63);
4570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state = state.create(s_begin, seed);
4580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  s_begin += 64;
4590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (s_begin != s_aligned_end) {
4600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s_begin);
4610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    s_begin += 64;
4621a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
4630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length & 63)
4640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s_end - 64);
4651a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
4660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return state.finalize(length);
4670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace detail
4700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace hashing
4710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Compute a hash_code for a sequence of values.
4740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This hashes a sequence of values. It produces the same hash_code as
4760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
4770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and is significantly faster given pointers and types which can be hashed as
4780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a sequence of bytes.
4790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename InputIteratorT>
4800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
4810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
4820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// Implementation details for hash_combine.
4860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace hashing {
4870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace detail {
4880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper class to manage the recursive combining of hash_combine
4900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// arguments.
4910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This class exists to manage the state and various calls involved in the
4930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// recursive combining of arguments used in hash_combine. It is particularly
4940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// useful at minimizing the code in the recursive calls to ease the pain
4950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// caused by a lack of variadic functions.
4965cd79bc14ca51019af4db735d13eac95dab088edChandler Carruthstruct hash_combine_recursive_helper {
4970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char buffer[64];
4980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state;
4995cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  const size_t seed;
5000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthpublic:
5020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Construct a recursive hash combining helper.
5030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This sets up the state for a recursive hash combine, including getting
5050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// the seed and buffer setup.
5060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_combine_recursive_helper()
5075cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    : seed(get_execution_seed()) {}
5080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Combine one chunk of data into the current in-flight hash.
5100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This merges one chunk of data into the hash. First it tries to buffer
5120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// the data. If the buffer is full, it hashes the buffer into its
5130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// hash_state, empties it, and then merges the new chunk in. This also
5140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// handles cases where the data straddles the end of the buffer.
5155cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  template <typename T>
5165cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
5170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    if (!store_and_advance(buffer_ptr, buffer_end, data)) {
5180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Check for skew which prevents the buffer from being packed, and do
5190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // a partial store into the buffer to fill it. This is only a concern
5200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // with the variadic combine because that formation can have varying
5210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // argument types.
5220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      size_t partial_store_size = buffer_end - buffer_ptr;
5230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      memcpy(buffer_ptr, &data, partial_store_size);
5240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // If the store fails, our buffer is full and ready to hash. We have to
5260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // either initialize the hash state (on the first full buffer) or mix
5270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // this buffer into the existing hash state. Length tracks the *hashed*
5280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // length, not the buffered length.
5290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      if (length == 0) {
5300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        state = state.create(buffer, seed);
5310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        length = 64;
5320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      } else {
5330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        // Mix this chunk into the current state and bump length up by 64.
5340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        state.mix(buffer);
5350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        length += 64;
5360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      }
5370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Reset the buffer_ptr to the head of the buffer for the next chunk of
5380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // data.
5390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      buffer_ptr = buffer;
5400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Try again to store into the buffer -- this cannot fail as we only
5420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // store types smaller than the buffer.
5430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      if (!store_and_advance(buffer_ptr, buffer_end, data,
5440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                             partial_store_size))
5450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        abort();
5461a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin    }
5475cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return buffer_ptr;
5486592eacf9006d046e8bc4999600e2973a3b56eacJay Foad  }
5491a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
5500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Recursive, variadic combining method.
5510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This function recurses through each argument, combining that argument
5530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// into a single hash.
5540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T, typename ...Ts>
5555cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
5565cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth                    const T &arg, const Ts &...args) {
5575cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
5580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Recurse to the next argument.
5605cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth    return combine(length, buffer_ptr, buffer_end, args...);
5610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Base case for recursive, variadic combining.
5640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// The base case when combining arguments recursively is reached when all
5660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// arguments have been handled. It flushes the remaining buffer and
5670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// constructs a hash_code.
5685cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
5690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Check whether the entire set of values fit in the buffer. If so, we'll
5700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // use the optimized short hashing routine and skip state entirely.
5710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    if (length == 0)
5720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      return hash_short(buffer, buffer_ptr - buffer, seed);
5730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Mix the final buffer, rotating it if we did a partial fill in order to
5750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // simulate doing a mix of the last 64-bytes. That is how the algorithm
5760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // works when we have a contiguous byte sequence, and we want to emulate
5770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // that here.
5780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::rotate(buffer, buffer_ptr, buffer_end);
5790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Mix this chunk into the current state.
5810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(buffer);
5820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    length += buffer_ptr - buffer;
5830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return state.finalize(length);
5851a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
5861a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin};
5871a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
5880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace detail
5890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace hashing
5900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Combine values into a single hash_code.
5920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
5930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This routine accepts a varying number of arguments of any type. It will
5940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// attempt to combine them into a single hash_code. For user-defined types it
5950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// attempts to call a \see hash_value overload (via ADL) for the type. For
5960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// integer and pointer types it directly combines their data into the
5970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// resulting hash_code.
5980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
5990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// The result is suitable for returning from a user's hash_value
6000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// *implementation* for their user-defined type. Consumers of a type should
6010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// *not* call this routine, they should instead call 'hash_value'.
6020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename ...Ts> hash_code hash_combine(const Ts &...args) {
6030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Recursively hash each argument using a helper class.
6040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6055cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth  return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
6060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
608b2eb7406719a0cd70489a1229af643b04882b046Nick Lewycky// Implementation details for implementations of hash_value overloads provided
609c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// here.
610c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace hashing {
611c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace detail {
612c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
613c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Helper to hash the value of a single integer.
614c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
615c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Overloads for smaller integer types are not provided to ensure consistent
616c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// behavior in the presence of integral promotions. Essentially,
617c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
618c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthinline hash_code hash_integer_value(uint64_t value) {
619c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  // Similar to hash_4to8_bytes but using a seed instead of length.
620c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const uint64_t seed = get_execution_seed();
621c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const char *s = reinterpret_cast<const char *>(&value);
622c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const uint64_t a = fetch32(s);
623c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
624c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
625c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
626c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace detail
627c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace hashing
628c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
629c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
630c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
631c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T>
63236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
633d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruthhash_value(T value) {
634de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return ::llvm::hashing::detail::hash_integer_value(
635de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      static_cast<uint64_t>(value));
636c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
637c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
638c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
639c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
640c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr) {
641c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return ::llvm::hashing::detail::hash_integer_value(
642c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth    reinterpret_cast<uintptr_t>(ptr));
643c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
644c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
645c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
646c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
647c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U>
648c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg) {
649c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return hash_combine(arg.first, arg.second);
650c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
651c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
6529406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
6539406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth// infrastructure is available.
6549406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthtemplate <typename T>
6559406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthhash_code hash_value(const std::basic_string<T> &arg) {
6569406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth  return hash_combine_range(arg.begin(), arg.end());
6579406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth}
6589406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth
6590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace llvm
6601a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
6611a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#endif
662