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> {
54 static unsigned getHashValue(const CachedHashStringRef &S) {
55 assert(!isEqual(S, getEmptyKey()) && "Cannot hash the empty key!");
56 return S.hash();
57 }
58 static bool isEqual(const CachedHashStringRef &LHS,
59 const CachedHashStringRef &RHS) {
60 return LHS.hash() == RHS.hash() &&
62 }
63};
64
65/// A container which contains a string, which it owns, plus a precomputed hash.
66///
67/// We do not null-terminate the string.
68class CachedHashString {
69 friend struct DenseMapInfo<CachedHashString>;
70
71 char *P;
72 uint32_t Size;
73 uint32_t Hash;
74
75 static char *getEmptyKeyPtr() { return DenseMapInfo<char *>::getEmptyKey(); }
76
77 bool isEmpty() const { return P == getEmptyKeyPtr(); }
78
79 struct ConstructEmptyTy {};
80
81 CachedHashString(ConstructEmptyTy, char *EmptyKeyPtr)
82 : P(EmptyKeyPtr), Size(0), Hash(0) {
83 assert(isEmpty());
84 }
85
86 // TODO: Use small-string optimization to avoid allocating.
87
88public:
89 explicit CachedHashString(const char *S) : CachedHashString(StringRef(S)) {}
90
91 // Explicit because copying and hashing a string isn't free.
93 : CachedHashString(S, DenseMapInfo<StringRef>::getHashValue(S)) {}
94
96 : P(new char[S.size()]), Size(S.size()), Hash(Hash) {
97 memcpy(P, S.data(), S.size());
98 }
99
100 // Ideally this class would not be copyable. But SetVector requires copyable
101 // keys, and we want this to be usable there.
102 CachedHashString(const CachedHashString &Other)
103 : Size(Other.Size), Hash(Other.Hash) {
104 if (Other.isEmpty()) {
105 P = Other.P;
106 } else {
107 P = new char[Size];
108 memcpy(P, Other.P, Size);
109 }
110 }
111
112 CachedHashString &operator=(CachedHashString Other) {
113 swap(*this, Other);
114 return *this;
115 }
116
117 CachedHashString(CachedHashString &&Other) noexcept
118 : P(Other.P), Size(Other.Size), Hash(Other.Hash) {
119 Other.P = getEmptyKeyPtr();
120 }
121
123 if (!isEmpty())
124 delete[] P;
125 }
126
127 StringRef val() const { return StringRef(P, Size); }
128 uint32_t size() const { return Size; }
129 uint32_t hash() const { return Hash; }
130
131 operator StringRef() const { return val(); }
132 operator CachedHashStringRef() const {
133 return CachedHashStringRef(val(), Hash);
134 }
135
136 friend void swap(CachedHashString &LHS, CachedHashString &RHS) {
137 using std::swap;
138 swap(LHS.P, RHS.P);
139 swap(LHS.Size, RHS.Size);
140 swap(LHS.Hash, RHS.Hash);
141 }
142};
143
144template <> struct DenseMapInfo<CachedHashString> {
146 return CachedHashString(CachedHashString::ConstructEmptyTy(),
147 CachedHashString::getEmptyKeyPtr());
148 }
149 static unsigned getHashValue(const CachedHashString &S) {
150 assert(!isEqual(S, getEmptyKey()) && "Cannot hash the empty key!");
151 return S.hash();
152 }
153 static bool isEqual(const CachedHashString &LHS,
154 const CachedHashString &RHS) {
155 if (LHS.hash() != RHS.hash())
156 return false;
157 if (LHS.P == CachedHashString::getEmptyKeyPtr())
158 return RHS.P == CachedHashString::getEmptyKeyPtr();
159
160 // This is safe because if RHS.P is the empty key, it will have length 0, so
161 // we'll never dereference its pointer.
162 return LHS.val() == RHS.val();
163 }
164};
165
166} // namespace llvm
167
168#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:863
static unsigned getHashValue(const CachedHashStringRef &S)
static CachedHashStringRef getEmptyKey()
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...