LLVM 24.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"
26
27namespace llvm {
28
29/// A container which contains a StringRef plus a precomputed hash.
31 const char *P;
32 uint32_t Size;
33 uint32_t Hash;
34
35public:
36 // Explicit because hashing a string isn't free.
38 : CachedHashStringRef(S, DenseMapInfo<StringRef>::getHashValue(S)) {}
39
41 : P(S.data()), Size(S.size()), Hash(Hash) {
42 assert(S.size() <= std::numeric_limits<uint32_t>::max());
43 }
44
45 StringRef val() const { return StringRef(P, Size); }
46 const char *data() const { return P; }
47 uint32_t size() const { return Size; }
48 uint32_t hash() const { return Hash; }
49};
50
51template <> struct DenseMapInfo<CachedHashStringRef> {
52 static unsigned getHashValue(const CachedHashStringRef &S) {
53 return S.hash();
54 }
55 static bool isEqual(const CachedHashStringRef &LHS,
56 const CachedHashStringRef &RHS) {
57 return LHS.hash() == RHS.hash() &&
59 }
60};
61
62/// A container which contains a string, which it owns, plus a precomputed hash.
63///
64/// We do not null-terminate the string.
66 friend struct DenseMapInfo<CachedHashString>;
67
68 char *P;
69 uint32_t Size;
70 uint32_t Hash;
71
72 static char *getEmptyKeyPtr() {
73 // Assume no pointer requires more than 4096 (1 << 12) bytes of alignment.
74 return reinterpret_cast<char *>(static_cast<uintptr_t>(-1) << 12);
75 }
76
77 bool isEmpty() const { return P == getEmptyKeyPtr(); }
78
79 // TODO: Use small-string optimization to avoid allocating.
80
81public:
82 explicit CachedHashString(const char *S) : CachedHashString(StringRef(S)) {}
83
84 // Explicit because copying and hashing a string isn't free.
86 : CachedHashString(S, DenseMapInfo<StringRef>::getHashValue(S)) {}
87
89 : P(new char[S.size()]), Size(S.size()), Hash(Hash) {
90 memcpy(P, S.data(), S.size());
91 }
92
93 // Ideally this class would not be copyable. But SetVector requires copyable
94 // keys, and we want this to be usable there.
96 : Size(Other.Size), Hash(Other.Hash) {
97 if (Other.isEmpty()) {
98 P = Other.P;
99 } else {
100 P = new char[Size];
101 memcpy(P, Other.P, Size);
102 }
103 }
104
106 swap(*this, Other);
107 return *this;
108 }
109
111 : P(Other.P), Size(Other.Size), Hash(Other.Hash) {
112 Other.P = getEmptyKeyPtr();
113 }
114
116 if (!isEmpty())
117 delete[] P;
118 }
119
120 StringRef val() const { return StringRef(P, Size); }
121 uint32_t size() const { return Size; }
122 uint32_t hash() const { return Hash; }
123
124 operator StringRef() const { return val(); }
125 operator CachedHashStringRef() const {
126 return CachedHashStringRef(val(), Hash);
127 }
128
130 using std::swap;
131 swap(LHS.P, RHS.P);
132 swap(LHS.Size, RHS.Size);
133 swap(LHS.Hash, RHS.Hash);
134 }
135};
136
137template <> struct DenseMapInfo<CachedHashString> {
138 static unsigned getHashValue(const CachedHashString &S) { return S.hash(); }
139 static bool isEqual(const CachedHashString &LHS,
140 const CachedHashString &RHS) {
141 if (LHS.hash() != RHS.hash())
142 return false;
143 if (LHS.P == CachedHashString::getEmptyKeyPtr())
144 return RHS.P == CachedHashString::getEmptyKeyPtr();
145
146 // This is safe because if RHS.P is the empty key, it will have length 0, so
147 // we'll never dereference its pointer.
148 return LHS.val() == RHS.val();
149 }
150};
151
152} // namespace llvm
153
154#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ATTRIBUTE_WARN_UNUSED
Definition Compiler.h:240
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:880
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...