LLVM 20.0.0git
StableHashing.h
Go to the documentation of this file.
1//===- llvm/ADT/StableHashing.h - Utilities for stable hashing * C++ *-----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides types and functions for computing and combining stable
10// hashes. Stable hashes can be useful for hashing across different modules,
11// processes, or compiler runs.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_STABLEHASHING_H
16#define LLVM_ADT_STABLEHASHING_H
17
18#include "llvm/ADT/StringRef.h"
19
20namespace llvm {
21
22/// An opaque object representing a stable hash code. It can be serialized,
23/// deserialized, and is stable across processes and executions.
25
26// Implementation details
27namespace hashing {
28namespace detail {
29
30// Stable hashes are based on the 64-bit FNV-1 hash:
31// https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function
32
33const uint64_t FNV_PRIME_64 = 1099511628211u;
34const uint64_t FNV_OFFSET_64 = 14695981039346656037u;
35
36inline void stable_hash_append(stable_hash &Hash, const char Value) {
37 Hash = Hash ^ (Value & 0xFF);
38 Hash = Hash * FNV_PRIME_64;
39}
40
42 for (unsigned I = 0; I < 8; ++I) {
43 stable_hash_append(Hash, static_cast<char>(Value));
44 Value >>= 8;
45 }
46}
47
48} // namespace detail
49} // namespace hashing
50
55 return Hash;
56}
57
59 stable_hash C) {
64 return Hash;
65}
66
74 return Hash;
75}
76
77/// Compute a stable_hash for a sequence of values.
78///
79/// This hashes a sequence of values. It produces the same stable_hash as
80/// 'stable_hash_combine(a, b, c, ...)', but can run over arbitrary sized
81/// sequences and is significantly faster given pointers and types which
82/// can be hashed as a sequence of bytes.
83template <typename InputIteratorT>
85 InputIteratorT Last) {
87 for (auto I = First; I != Last; ++I)
89 return Hash;
90}
91
94 for (size_t I = 0; I < C; ++I)
96 return Hash;
97}
98} // namespace llvm
99
100#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
void stable_hash_append(stable_hash &Hash, const char Value)
Definition: StableHashing.h:36
const uint64_t FNV_PRIME_64
Definition: StableHashing.h:33
const uint64_t FNV_OFFSET_64
Definition: StableHashing.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
stable_hash stable_hash_combine_range(InputIteratorT First, InputIteratorT Last)
Compute a stable_hash for a sequence of values.
Definition: StableHashing.h:84
stable_hash stable_hash_combine_array(const stable_hash *P, size_t C)
Definition: StableHashing.h:92
stable_hash stable_hash_combine(stable_hash A, stable_hash B)
Definition: StableHashing.h:51
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
uint64_t stable_hash
An opaque object representing a stable hash code.
Definition: StableHashing.h:24