LLVM 23.0.0git
CachedHashString.h
Go to the documentation of this file.
1//===- llvm/ADT/CachedHashString.h - Prehashed string/StringRef -*- 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/// \file
10/// This file defines CachedHashString and CachedHashStringRef. These are
11/// owning and not-owning string types that store their hash in addition to
12/// their string data.
13///
14/// Unlike std::string, CachedHashString can be used in DenseSet/DenseMap
15/// (because, unlike std::string, CachedHashString lets us have an empty
16/// value).
17///
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_ADT_CACHEDHASHSTRING_H
21#define LLVM_ADT_CACHEDHASHSTRING_H
22
24#include "llvm/ADT/StringRef.h"
25
26namespace llvm {
27
28/// A container which contains a StringRef plus a precomputed hash.
30 const char *P;
31 uint32_t Size;
32 uint32_t Hash;
33
34public:
35 // Explicit because hashing a string isn't free.
37 : CachedHashStringRef(S, DenseMapInfo<StringRef>::getHashValue(S)) {}
38
40 : P(S.data()), Size(S.size()), Hash(Hash) {
41 assert(S.size() <= std::numeric_limits<uint32_t>::max());
42 }
43
44 StringRef val() const { return StringRef(P, Size); }
45 const char *data() const { return P; }
46 uint32_t size() const { return Size; }
47 uint32_t hash() const { return Hash; }
48};
49
50template <> struct DenseMapInfo<CachedHashStringRef> {
51 static unsigned getHashValue(const CachedHashStringRef &S) {
52 return S.hash();
53 }
54 static bool isEqual(const CachedHashStringRef &LHS,
55 const CachedHashStringRef &RHS) {
56 return LHS.hash() == RHS.hash() &&
58 }
59};
60
61/// A container which contains a string, which it owns, plus a precomputed hash.
62///
63/// We do not null-terminate the string.
65 friend struct DenseMapInfo<CachedHashString>;
66
67 char *P;
68 uint32_t Size;
69 uint32_t Hash;
70
71 static char *getEmptyKeyPtr() {
72 // Assume no pointer requires more than 4096 (1 << 12) bytes of alignment.
73 return reinterpret_cast<char *>(static_cast<uintptr_t>(-1) << 12);
74 }
75
76 bool isEmpty() const { return P == getEmptyKeyPtr(); }
77
78 // TODO: Use small-string optimization to avoid allocating.
79
80public:
81 explicit CachedHashString(const char *S) : CachedHashString(StringRef(S)) {}
82
83 // Explicit because copying and hashing a string isn't free.
85 : CachedHashString(S, DenseMapInfo<StringRef>::getHashValue(S)) {}
86
88 : P(new char[S.size()]), Size(S.size()), Hash(Hash) {
89 memcpy(P, S.data(), S.size());
90 }
91
92 // Ideally this class would not be copyable. But SetVector requires copyable
93 // keys, and we want this to be usable there.
95 : Size(Other.Size), Hash(Other.Hash) {
96 if (Other.isEmpty()) {
97 P = Other.P;
98 } else {
99 P = new char[Size];
100 memcpy(P, Other.P, Size);
101 }
102 }
103
105 swap(*this, Other);
106 return *this;
107 }
108
110 : P(Other.P), Size(Other.Size), Hash(Other.Hash) {
111 Other.P = getEmptyKeyPtr();
112 }
113
115 if (!isEmpty())
116 delete[] P;
117 }
118
119 StringRef val() const { return StringRef(P, Size); }
120 uint32_t size() const { return Size; }
121 uint32_t hash() const { return Hash; }
122
123 operator StringRef() const { return val(); }
124 operator CachedHashStringRef() const {
125 return CachedHashStringRef(val(), Hash);
126 }
127
129 using std::swap;
130 swap(LHS.P, RHS.P);
131 swap(LHS.Size, RHS.Size);
132 swap(LHS.Hash, RHS.Hash);
133 }
134};
135
136template <> struct DenseMapInfo<CachedHashString> {
137 static unsigned getHashValue(const CachedHashString &S) { return S.hash(); }
138 static bool isEqual(const CachedHashString &LHS,
139 const CachedHashString &RHS) {
140 if (LHS.hash() != RHS.hash())
141 return false;
142 if (LHS.P == CachedHashString::getEmptyKeyPtr())
143 return RHS.P == CachedHashString::getEmptyKeyPtr();
144
145 // This is safe because if RHS.P is the empty key, it will have length 0, so
146 // we'll never dereference its pointer.
147 return LHS.val() == RHS.val();
148 }
149};
150
151} // namespace llvm
152
153#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines DenseMapInfo traits for DenseMap.
#define P(N)
Value * RHS
Value * LHS
A container which contains a StringRef plus a precomputed hash.
const char * data() const
CachedHashStringRef(StringRef S, uint32_t Hash)
A container which contains a string, which it owns, plus a precomputed hash.
CachedHashString & operator=(CachedHashString Other)
CachedHashString(const char *S)
CachedHashString(StringRef S, uint32_t Hash)
friend void swap(CachedHashString &LHS, CachedHashString &RHS)
CachedHashString(const CachedHashString &Other)
CachedHashString(CachedHashString &&Other) noexcept
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
This is an optimization pass for GlobalISel generic memory operations.
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
@ Other
Any other memory.
Definition ModRef.h:68
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
static unsigned getHashValue(const CachedHashStringRef &S)
static unsigned getHashValue(const CachedHashString &S)
static bool isEqual(const CachedHashString &LHS, const CachedHashString &RHS)
An information struct used to provide DenseMap with the various necessary components for a given valu...