blob: 68dbbd0885378ff40edff9902811ad0bdd3894a1 [file] [log] [blame]
Zachary Turner11036a92017-01-19 23:31:24 +00001//===- HashTable.cpp - PDB Hash Table ---------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/PDB/Raw/HashTable.h"
11
12#include "llvm/ADT/Optional.h"
13#include "llvm/ADT/SparseBitVector.h"
14#include "llvm/DebugInfo/PDB/Raw/RawError.h"
15
16using namespace llvm;
17using namespace llvm::pdb;
18
19HashTable::HashTable() : HashTable(8) {}
20
21HashTable::HashTable(uint32_t Capacity) { Buckets.resize(Capacity); }
22
23Error HashTable::load(msf::StreamReader &Stream) {
24 const Header *H;
25 if (auto EC = Stream.readObject(H))
26 return EC;
27 if (H->Capacity == 0)
28 return make_error<RawError>(raw_error_code::corrupt_file,
29 "Invalid Hash Table Capacity");
30 if (H->Size > maxLoad(H->Capacity))
31 return make_error<RawError>(raw_error_code::corrupt_file,
32 "Invalid Hash Table Size");
33
34 Buckets.resize(H->Capacity);
35
36 if (auto EC = readSparseBitVector(Stream, Present))
37 return EC;
38 if (Present.count() != H->Size)
39 return make_error<RawError>(raw_error_code::corrupt_file,
40 "Present bit vector does not match size!");
41
42 if (auto EC = readSparseBitVector(Stream, Deleted))
43 return EC;
44 if (Present.intersects(Deleted))
45 return make_error<RawError>(raw_error_code::corrupt_file,
46 "Present bit vector interesects deleted!");
47
48 for (uint32_t P : Present) {
49 if (auto EC = Stream.readInteger(Buckets[P].first))
50 return EC;
51 if (auto EC = Stream.readInteger(Buckets[P].second))
52 return EC;
53 }
54
55 return Error::success();
56}
57
58uint32_t HashTable::calculateSerializedLength() const {
59 uint32_t Size = sizeof(Header);
60
61 int NumBitsP = Present.find_last() + 1;
62 int NumBitsD = Deleted.find_last() + 1;
63
64 // Present bit set number of words, followed by that many actual words.
65 Size += sizeof(uint32_t);
66 Size += alignTo(NumBitsP, sizeof(uint32_t));
67
68 // Deleted bit set number of words, followed by that many actual words.
69 Size += sizeof(uint32_t);
70 Size += alignTo(NumBitsD, sizeof(uint32_t));
71
72 // One (Key, Value) pair for each entry Present.
73 Size += 2 * sizeof(uint32_t) * size();
74
75 return Size;
76}
77
78Error HashTable::commit(msf::StreamWriter &Writer) const {
79 Header H;
80 H.Size = size();
81 H.Capacity = capacity();
82 if (auto EC = Writer.writeObject(H))
83 return EC;
84
85 if (auto EC = writeSparseBitVector(Writer, Present))
86 return EC;
87
88 if (auto EC = writeSparseBitVector(Writer, Deleted))
89 return EC;
90
91 for (const auto &Entry : *this) {
92 if (auto EC = Writer.writeInteger(Entry.first))
93 return EC;
94 if (auto EC = Writer.writeInteger(Entry.second))
95 return EC;
96 }
97 return Error::success();
98}
99
100uint32_t HashTable::capacity() const { return Buckets.size(); }
101uint32_t HashTable::size() const { return Present.count(); }
102
103HashTableIterator HashTable::begin() const { return HashTableIterator(*this); }
104HashTableIterator HashTable::end() const {
105 return HashTableIterator(*this, 0, true);
106}
107
108HashTableIterator HashTable::find(uint32_t K) {
109 uint32_t H = K % capacity();
110 uint32_t I = H;
111 Optional<uint32_t> FirstUnused;
112 do {
113 if (isPresent(I)) {
114 if (Buckets[I].first == K)
115 return HashTableIterator(*this, I, false);
116 } else {
117 if (!FirstUnused)
118 FirstUnused = I;
119 // Insertion occurs via linear probing from the slot hint, and will be
120 // inserted at the first empty / deleted location. Therefore, if we are
121 // probing and find a location that is neither present nor deleted, then
122 // nothing must have EVER been inserted at this location, and thus it is
123 // not possible for a matching value to occur later.
124 if (!isDeleted(I))
125 break;
126 }
127 I = (I + 1) % capacity();
128 } while (I != H);
129
130 // The only way FirstUnused would not be set is if every single entry in the
131 // table were Present. But this would violate the load factor constraints
132 // that we impose, so it should never happen.
133 assert(FirstUnused);
134 return HashTableIterator(*this, *FirstUnused, true);
135}
136
137void HashTable::set(uint32_t K, uint32_t V) {
138 auto Entry = find(K);
139 if (Entry != end()) {
140 assert(isPresent(Entry.index()));
141 assert(Buckets[Entry.index()].first == K);
142 // We're updating, no need to do anything special.
143 Buckets[Entry.index()].second = V;
144 return;
145 }
146
147 auto &B = Buckets[Entry.index()];
148 assert(!isPresent(Entry.index()));
149 assert(Entry.isEnd());
150 B.first = K;
151 B.second = V;
152 Present.set(Entry.index());
153 Deleted.reset(Entry.index());
154
155 grow();
156
157 assert(find(K) != end());
158}
159
160void HashTable::remove(uint32_t K) {
161 auto Iter = find(K);
162 // It wasn't here to begin with, just exit.
163 if (Iter == end())
164 return;
165
166 assert(Present.test(Iter.index()));
167 assert(!Deleted.test(Iter.index()));
168 Deleted.set(Iter.index());
169 Present.reset(Iter.index());
170}
171
172uint32_t HashTable::get(uint32_t K) {
173 auto I = find(K);
174 assert(I != end());
175 return (*I).second;
176}
177
178uint32_t HashTable::maxLoad(uint32_t capacity) { return capacity * 2 / 3 + 1; }
179
180void HashTable::grow() {
181 uint32_t S = size();
182 if (S < maxLoad(capacity()))
183 return;
Zachary Turnerd54deae2017-01-19 23:41:11 +0000184 assert(capacity() != UINT32_MAX && "Can't grow Hash table!");
Zachary Turner11036a92017-01-19 23:31:24 +0000185
186 uint32_t NewCapacity =
187 (capacity() <= INT32_MAX) ? capacity() * 2 : UINT32_MAX;
188
189 // Growing requires rebuilding the table and re-hashing every item. Make a
190 // copy with a larger capacity, insert everything into the copy, then swap
191 // it in.
192 HashTable NewMap(NewCapacity);
193 for (auto I : Present) {
194 NewMap.set(Buckets[I].first, Buckets[I].second);
195 }
196
197 Buckets.swap(NewMap.Buckets);
198 std::swap(Present, NewMap.Present);
199 std::swap(Deleted, NewMap.Deleted);
200 assert(capacity() == NewCapacity);
201 assert(size() == S);
202}
203
204Error HashTable::readSparseBitVector(msf::StreamReader &Stream,
205 SparseBitVector<> &V) {
206 uint32_t NumWords;
207 if (auto EC = Stream.readInteger(NumWords))
208 return joinErrors(
209 std::move(EC),
210 make_error<RawError>(raw_error_code::corrupt_file,
211 "Expected hash table number of words"));
212
213 for (uint32_t I = 0; I != NumWords; ++I) {
214 uint32_t Word;
215 if (auto EC = Stream.readInteger(Word))
216 return joinErrors(std::move(EC),
217 make_error<RawError>(raw_error_code::corrupt_file,
218 "Expected hash table word"));
219 for (unsigned Idx = 0; Idx < 32; ++Idx)
220 if (Word & (1U << Idx))
221 V.set((I * 32) + Idx);
222 }
223 return Error::success();
224}
225
226Error HashTable::writeSparseBitVector(msf::StreamWriter &Writer,
227 SparseBitVector<> &Vec) {
228 int ReqBits = Vec.find_last() + 1;
229 uint32_t NumWords = alignTo(ReqBits, sizeof(uint32_t)) / sizeof(uint32_t);
230 if (auto EC = Writer.writeInteger(NumWords))
231 return joinErrors(
232 std::move(EC),
233 make_error<RawError>(raw_error_code::corrupt_file,
234 "Could not write linear map number of words"));
235
236 uint32_t Idx = 0;
237 for (uint32_t I = 0; I != NumWords; ++I) {
238 uint32_t Word = 0;
239 for (uint32_t WordIdx = 0; WordIdx < 32; ++WordIdx, ++Idx) {
240 if (Vec.test(Idx))
241 Word |= (1 << WordIdx);
242 }
243 if (auto EC = Writer.writeInteger(Word))
244 return joinErrors(std::move(EC), make_error<RawError>(
245 raw_error_code::corrupt_file,
246 "Could not write linear map word"));
247 }
248 return Error::success();
249}
250
251HashTableIterator::HashTableIterator(const HashTable &Map, uint32_t Index,
252 bool IsEnd)
253 : Map(&Map), Index(Index), IsEnd(IsEnd) {}
254
255HashTableIterator::HashTableIterator(const HashTable &Map) : Map(&Map) {
256 int I = Map.Present.find_first();
257 if (I == -1) {
258 Index = 0;
259 IsEnd = true;
260 } else {
261 Index = static_cast<uint32_t>(I);
262 IsEnd = false;
263 }
264}
265
266HashTableIterator &HashTableIterator::operator=(const HashTableIterator &R) {
267 Map = R.Map;
268 return *this;
269}
270
271bool HashTableIterator::operator==(const HashTableIterator &R) const {
272 if (IsEnd && R.IsEnd)
273 return true;
274 if (IsEnd != R.IsEnd)
275 return false;
276
277 return (Map == R.Map) && (Index == R.Index);
278}
279
280const std::pair<uint32_t, uint32_t> &HashTableIterator::operator*() const {
281 assert(Map->Present.test(Index));
282 return Map->Buckets[Index];
283}
284
285HashTableIterator &HashTableIterator::operator++() {
286 while (Index < Map->Buckets.size()) {
287 ++Index;
288 if (Map->Present.test(Index))
289 return *this;
290 }
291
292 IsEnd = true;
293 return *this;
294}