Hashing.h revision 0c7374d87e6bf002028cd19f8ae9547927c9c645
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 Carruth///
800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Also note that there are two numerical values which are reserved, and the
810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// implementation ensures will never be produced for real hash_codes. These
820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// can be used as sentinels within hashing data structures.
830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthclass hash_code {
840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t value;
850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
861a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talinpublic:
874166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  /// \brief Default construct a hash_code.
884166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  /// Note that this leaves the value uninitialized.
894166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  hash_code() {}
901a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Form a hash code directly from a numerical value.
924166989f10eaecfb357551788a3d91275e75f119Chandler Carruth  hash_code(size_t value) : value(value) {}
931a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Convert the hash code to its numerical value for use.
950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /*explicit*/ operator size_t() const { return value; }
960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return lhs.value == rhs.value;
990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
1000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend bool operator!=(const hash_code &lhs, const hash_code &rhs) {
1010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return lhs.value != rhs.value;
1021a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
1031a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
1040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Allow a hash_code to be directly run through hash_value.
1050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  friend size_t hash_value(const hash_code &code) { return code.value; }
1060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth};
1070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
108c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for any integer value.
109c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
110c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Note that this function is intended to compute the same hash_code for
111c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// a particular value without regard to the pre-promotion type. This is in
112c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// contrast to hash_combine which may produce different hash_codes for
113c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// differing argument types even if they would implicit promote to a common
114c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// type without changing the value.
115c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T>
116c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtypename enable_if<is_integral<T>, hash_code>::type hash_value(T value);
117c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
118c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pointer's address.
119c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
120c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// N.B.: This hashes the *address*. Not the value and not the type.
121c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr);
122c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
123c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pair of objects.
124c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U>
125c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg);
126c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler 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) {
2110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t a = fetch64(s);
2120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t b = fetch64(s + len - 8);
2130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    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),
2760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      seed * k1, shift_mix(seed), hash_16_bytes(state.h4, state.h5),
2770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      seed
2780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    };
2790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s);
2800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return state;
2811a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
2821a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
2830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Mix 32-bytes from the input sequence into the 16-bytes of 'a'
2840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// and 'b', including whatever is already in 'a' and 'b'.
2850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
2860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += fetch64(s);
2870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t c = fetch64(s + 24);
2880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    b = rotate(b + a + c, 21);
2890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    uint64_t d = a;
2900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += fetch64(s + 8) + fetch64(s + 16);
2910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    b += rotate(a, 44) + d;
2920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    a += c;
2931a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
2941a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
2950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Mix in a 64-byte buffer of data.
2960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// We mix all 64 bytes even when the chunk length is smaller, but we
2970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// record the actual length.
2980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  void mix(const char *s) {
2990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
3000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1;
3010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h0 ^= h6;
3020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h1 += h3 + fetch64(s + 40);
3030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h2 = rotate(h2 + h5, 33) * k1;
3040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h3 = h4 * k1;
3050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h4 = h0 + h5;
3060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    mix_32_bytes(s, h3, h4);
3070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h5 = h2 + h6;
3080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    h6 = h1 + fetch64(s + 16);
3090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    mix_32_bytes(s + 32, h5, h6);
3100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::swap(h2, h0);
3111a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
3121a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
3130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Compute the final 64-bit hash code value based on the current
3140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// state and the length of bytes hashed.
3150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  uint64_t finalize(size_t length) {
3164166989f10eaecfb357551788a3d91275e75f119Chandler Carruth    return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
3174166989f10eaecfb357551788a3d91275e75f119Chandler Carruth                         hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
3181a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
3190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth};
3201a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
3210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief A global, fixed seed-override variable.
3230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variable can be set using the \see llvm::set_fixed_execution_seed
3250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// function. See that function for details. Do not, under any circumstances,
3260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// set or read this variable.
3270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthextern size_t fixed_seed_override;
3280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline size_t get_execution_seed() {
3300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // FIXME: This needs to be a per-execution seed. This is just a placeholder
3310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // implementation. Switching to a per-execution seed is likely to flush out
3320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // instability bugs and so will happen as its own commit.
3330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  //
3340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // However, if there is a fixed seed override set the first time this is
3350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // called, return that instead of the per-execution seed.
336b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth  const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
3370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  static size_t seed = fixed_seed_override ? fixed_seed_override
338b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth                                           : static_cast<size_t>(seed_prime);
3390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return seed;
3400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
3410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
3430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Trait to indicate whether a type's bits can be hashed directly.
3440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
3450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// A type trait which is true if we want to combine values for hashing by
3460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// reading the underlying data. It is false if values of this type must
3470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// first be passed to hash_value, and the resulting hash_codes combined.
3480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth//
3490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// FIXME: We want to replace is_integral and is_pointer here with a predicate
3500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// which asserts that comparing the underlying storage of two values of the
3510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// type for equality is equivalent to comparing the two values for equality.
3520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// For all the platforms we care about, this holds for integers and pointers,
3530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// but there are platforms where it doesn't and we would like to support
3540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// user-defined types which happen to satisfy this property.
3550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T> struct is_hashable_data
3560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  : integral_constant<bool, ((is_integral<T>::value || 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  typedef typename std::iterator_traits<InputIteratorT>::value_type ValueT;
4130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t seed = get_execution_seed();
4140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char buffer[64], *buffer_ptr = buffer;
4150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char *const buffer_end = buffer_ptr + array_lengthof(buffer);
4160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (first != last && store_and_advance(buffer_ptr, buffer_end,
4170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                            get_hashable_data(*first)))
4180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    ++first;
4190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Metafunction that determines whether the given type is an integral
4200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// type.
4210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (first == last)
4220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_short(buffer, buffer_ptr - buffer, seed);
4230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  assert(buffer_ptr == buffer_end);
4240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state = state.create(buffer, seed);
4260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t length = 64;
4270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (first != last) {
4280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Fill up the buffer. We don't clear it, which re-mixes the last round
4290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // when only a partial 64-byte chunk is left.
4300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    buffer_ptr = buffer;
4310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    while (first != last && store_and_advance(buffer_ptr, buffer_end,
4320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                              get_hashable_data(*first)))
4330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      ++first;
4340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Rotate the buffer if we did a partial fill in order to simulate doing
4360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // a mix of the last 64-bytes. That is how the algorithm works when we
4370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // have a contiguous byte sequence, and we want to emulate that here.
4380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    std::rotate(buffer, buffer_ptr, buffer_end);
4390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Mix this chunk into the current state.
4410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(buffer);
4420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    length += buffer_ptr - buffer;
4430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  };
4440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return state.finalize(length);
4460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Implement the combining of integral values into a hash_code.
4490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This overload is selected when the value type of the iterator is integral
4510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and when the input iterator is actually a pointer. Rather than computing
4520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a hash_code for each object and then combining them, this (as an
4530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// optimization) directly combines the integers. Also, because the integers
4540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// are stored in contiguous memory, this routine avoids copying each value
4550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and directly reads from the underlying memory.
4560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename ValueT>
4570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtypename enable_if<is_hashable_data<ValueT>, hash_code>::type
4580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_combine_range_impl(const ValueT *first, const ValueT *last) {
4590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t seed = get_execution_seed();
4600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_begin = reinterpret_cast<const char *>(first);
4610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_end = reinterpret_cast<const char *>(last);
4620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t length = std::distance(s_begin, s_end);
4630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length <= 64)
4640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_short(s_begin, length, seed);
4650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const char *s_aligned_end = s_begin + (length & ~63);
4670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state = state.create(s_begin, seed);
4680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  s_begin += 64;
4690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  while (s_begin != s_aligned_end) {
4700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s_begin);
4710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    s_begin += 64;
4721a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin  }
4730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  if (length & 63)
4740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    state.mix(s_end - 64);
4751a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
4760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return state.finalize(length);
4770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace detail
4800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace hashing
4810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Compute a hash_code for a sequence of values.
4840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
4850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This hashes a sequence of values. It produces the same hash_code as
4860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
4870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and is significantly faster given pointers and types which can be hashed as
4880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a sequence of bytes.
4890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename InputIteratorT>
4900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
4910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
4920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
4930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// Implementation details for hash_combine.
4960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace hashing {
4970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace detail {
4980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
4990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper class to manage the recursive combining of hash_combine
5000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// arguments.
5010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth///
5020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This class exists to manage the state and various calls involved in the
5030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// recursive combining of arguments used in hash_combine. It is particularly
5040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// useful at minimizing the code in the recursive calls to ease the pain
5050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// caused by a lack of variadic functions.
5060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthclass hash_combine_recursive_helper {
5070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  const size_t seed;
5080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char buffer[64];
5090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char *const buffer_end;
5100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  char *buffer_ptr;
5110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  size_t length;
5120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_state state;
5130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthpublic:
5150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Construct a recursive hash combining helper.
5160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This sets up the state for a recursive hash combine, including getting
5180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// the seed and buffer setup.
5190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_combine_recursive_helper()
5200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    : seed(get_execution_seed()),
5210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      buffer_end(buffer + array_lengthof(buffer)),
5220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      buffer_ptr(buffer),
5230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      length(0) {}
5240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Combine one chunk of data into the current in-flight hash.
5260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This merges one chunk of data into the hash. First it tries to buffer
5280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// the data. If the buffer is full, it hashes the buffer into its
5290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// hash_state, empties it, and then merges the new chunk in. This also
5300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// handles cases where the data straddles the end of the buffer.
5310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T> void combine_data(T data) {
5320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    if (!store_and_advance(buffer_ptr, buffer_end, data)) {
5330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Check for skew which prevents the buffer from being packed, and do
5340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // a partial store into the buffer to fill it. This is only a concern
5350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // with the variadic combine because that formation can have varying
5360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // argument types.
5370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      size_t partial_store_size = buffer_end - buffer_ptr;
5380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      memcpy(buffer_ptr, &data, partial_store_size);
5390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // If the store fails, our buffer is full and ready to hash. We have to
5410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // either initialize the hash state (on the first full buffer) or mix
5420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // this buffer into the existing hash state. Length tracks the *hashed*
5430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // length, not the buffered length.
5440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      if (length == 0) {
5450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        state = state.create(buffer, seed);
5460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        length = 64;
5470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      } else {
5480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        // Mix this chunk into the current state and bump length up by 64.
5490b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        state.mix(buffer);
5500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        length += 64;
5510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      }
5520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Reset the buffer_ptr to the head of the buffer for the next chunk of
5530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // data.
5540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      buffer_ptr = buffer;
5550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // Try again to store into the buffer -- this cannot fail as we only
5570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      // store types smaller than the buffer.
5580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth      if (!store_and_advance(buffer_ptr, buffer_end, data,
5590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                             partial_store_size))
5600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth        abort();
5611a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin    }
5626592eacf9006d046e8bc4999600e2973a3b56eacJay Foad  }
5631a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
5640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#if defined(__has_feature) && __has_feature(__cxx_variadic_templates__)
5650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// \brief Recursive, variadic combining method.
5670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ///
5680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// This function recurses through each argument, combining that argument
5690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  /// into a single hash.
5700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T, typename ...Ts>
5710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine(const T &arg, const Ts &...args) {
5720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    combine_data( get_hashable_data(arg));
5730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    // Recurse to the next argument.
5750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return combine(args...);
5760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#else
5790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // Manually expanded recursive combining methods. See variadic above for
5800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  // documentation.
5810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
5820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3, typename T4, typename T5,
5830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth            typename T6>
5840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
5850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                    const T4 &arg4, const T5 &arg5, const T6 &arg6) {
5860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    combine_data(get_hashable_data(arg1));
5870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return combine(arg2, arg3, arg4, arg5, arg6);
5880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3, typename T4, typename T5>
5900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
5910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                    const T4 &arg4, const T5 &arg5) {
5920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    combine_data(get_hashable_data(arg1));
5930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return combine(arg2, arg3, arg4, arg5);
5940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
5950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3, typename T4>
5960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
5970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                    const T4 &arg4) {
5980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    combine_data(get_hashable_data(arg1));
5990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return combine(arg2, arg3, arg4);
6000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
6010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2, typename T3>
6020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine(const T1 &arg1, const T2 &arg2, const T3 &arg3) {
6030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    combine_data(get_hashable_data(arg1));
6040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return combine(arg2, arg3);
6050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
6060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1, typename T2>
6070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine(const T1 &arg1, const T2 &arg2) {
6080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    combine_data(get_hashable_data(arg1));
6090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return combine(arg2);
6100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  }
6110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  template <typename T1>
6120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine(const T1 &arg1) {
6130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    combine_data(get_hashable_data(arg1));
6140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return combine();
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.
6240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  hash_code combine() {
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;
6640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return helper.combine(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,
6750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                  const T4 &arg4, const T5 &arg5, const T6 &arg6) {
6760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return helper.combine(arg1, arg2, arg3, arg4, arg5, arg6);
6780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2, typename T3, typename T4, typename T5>
6800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
6810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                  const T4 &arg4, const T5 &arg5) {
6820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return helper.combine(arg1, arg2, arg3, arg4, arg5);
6840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2, typename T3, typename T4>
6860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3,
6870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                  const T4 &arg4) {
6880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return helper.combine(arg1, arg2, arg3, arg4);
6900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2, typename T3>
6920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3) {
6930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return helper.combine(arg1, arg2, arg3);
6950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
6960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1, typename T2>
6970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1, const T2 &arg2) {
6980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
6990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return helper.combine(arg1, arg2);
7000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
7010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T1>
7020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine(const T1 &arg1) {
7030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  ::llvm::hashing::detail::hash_combine_recursive_helper helper;
7040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth  return helper.combine(arg1);
7050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}
7060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
7070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#endif
7080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth
709c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
710c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Implementation details for implementatinos of hash_value overloads provided
711c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// here.
712c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace hashing {
713c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace detail {
714c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
715c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Helper to hash the value of a single integer.
716c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth///
717c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Overloads for smaller integer types are not provided to ensure consistent
718c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// behavior in the presence of integral promotions. Essentially,
719c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
720c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthinline hash_code hash_integer_value(uint64_t value) {
721c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  // Similar to hash_4to8_bytes but using a seed instead of length.
722c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const uint64_t seed = get_execution_seed();
723c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const char *s = reinterpret_cast<const char *>(&value);
724c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  const uint64_t a = fetch32(s);
725c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
726c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
727c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
728c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace detail
729c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace hashing
730c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
731c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
732c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
733c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T>
734c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtypename enable_if<is_integral<T>, hash_code>::type hash_value(T value) {
735c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return ::llvm::hashing::detail::hash_integer_value(value);
736c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
737c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
738c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
739c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
740c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr) {
741c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return ::llvm::hashing::detail::hash_integer_value(
742c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth    reinterpret_cast<uintptr_t>(ptr));
743c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
744c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
745c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing
746c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available.
747c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U>
748c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg) {
749c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth  return hash_combine(arg.first, arg.second);
750c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth}
751c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth
7520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace llvm
7531a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin
7541a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#endif
755