LLVM 17.0.0git
LazyRandomTypeCollection.cpp
Go to the documentation of this file.
1//===- LazyRandomTypeCollection.cpp ---------------------------------------===//
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
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/Error.h"
20#include <algorithm>
21#include <cassert>
22#include <cstdint>
23#include <iterator>
24
25using namespace llvm;
26using namespace llvm::codeview;
27
28static void error(Error &&EC) {
29 assert(!static_cast<bool>(EC));
30 if (EC)
31 consumeError(std::move(EC));
32}
33
35 : LazyRandomTypeCollection(CVTypeArray(), RecordCountHint,
37
39 const CVTypeArray &Types, uint32_t RecordCountHint,
40 PartialOffsetArray PartialOffsets)
41 : NameStorage(Allocator), Types(Types), PartialOffsets(PartialOffsets) {
42 Records.resize(RecordCountHint);
43}
44
46 uint32_t RecordCountHint)
47 : LazyRandomTypeCollection(RecordCountHint) {
48}
49
51 uint32_t RecordCountHint)
52 : LazyRandomTypeCollection(ArrayRef(Data.bytes_begin(), Data.bytes_end()),
53 RecordCountHint) {}
54
56 uint32_t NumRecords)
57 : LazyRandomTypeCollection(Types, NumRecords, PartialOffsetArray()) {}
58
60 uint32_t RecordCountHint) {
61 Count = 0;
62 PartialOffsets = PartialOffsetArray();
63
64 error(Reader.readArray(Types, Reader.bytesRemaining()));
65
66 // Clear and then resize, to make sure existing data gets destroyed.
67 Records.clear();
68 Records.resize(RecordCountHint);
69}
70
73 reset(Reader, RecordCountHint);
74}
75
77 uint32_t RecordCountHint) {
79 reset(Reader, RecordCountHint);
80}
81
83 error(ensureTypeExists(Index));
85
86 return Records[Index.toArrayIndex()].Offset;
87}
88
90 assert(!Index.isSimple());
91
92 auto EC = ensureTypeExists(Index);
93 error(std::move(EC));
95
96 return Records[Index.toArrayIndex()].Type;
97}
98
100 if (Index.isSimple())
101 return std::nullopt;
102
103 if (auto EC = ensureTypeExists(Index)) {
104 consumeError(std::move(EC));
105 return std::nullopt;
106 }
107
109 return Records[Index.toArrayIndex()].Type;
110}
111
113 if (Index.isNoneType() || Index.isSimple())
115
116 // Try to make sure the type exists. Even if it doesn't though, it may be
117 // because we're dumping a symbol stream with no corresponding type stream
118 // present, in which case we still want to be able to print <unknown UDT>
119 // for the type names.
120 if (auto EC = ensureTypeExists(Index)) {
121 consumeError(std::move(EC));
122 return "<unknown UDT>";
123 }
124
125 uint32_t I = Index.toArrayIndex();
126 ensureCapacityFor(Index);
127 if (Records[I].Name.data() == nullptr) {
128 StringRef Result = NameStorage.save(computeTypeName(*this, Index));
129 Records[I].Name = Result;
130 }
131 return Records[I].Name;
132}
133
135 if (Index.isSimple() || Index.isNoneType())
136 return false;
137
138 if (Records.size() <= Index.toArrayIndex())
139 return false;
140 if (!Records[Index.toArrayIndex()].Type.valid())
141 return false;
142 return true;
143}
144
146
148
149Error LazyRandomTypeCollection::ensureTypeExists(TypeIndex TI) {
150 if (contains(TI))
151 return Error::success();
152
153 return visitRangeForType(TI);
154}
155
156void LazyRandomTypeCollection::ensureCapacityFor(TypeIndex Index) {
157 assert(!Index.isSimple());
158 uint32_t MinSize = Index.toArrayIndex() + 1;
159
160 if (MinSize <= capacity())
161 return;
162
163 uint32_t NewCapacity = MinSize * 3 / 2;
164
165 assert(NewCapacity > capacity());
166 Records.resize(NewCapacity);
167}
168
169Error LazyRandomTypeCollection::visitRangeForType(TypeIndex TI) {
170 assert(!TI.isSimple());
171 if (PartialOffsets.empty())
172 return fullScanForType(TI);
173
174 auto Next = llvm::upper_bound(PartialOffsets, TI,
175 [](TypeIndex Value, const TypeIndexOffset &IO) {
176 return Value < IO.Type;
177 });
178
179 assert(Next != PartialOffsets.begin());
180 auto Prev = std::prev(Next);
181
182 TypeIndex TIB = Prev->Type;
183 if (contains(TIB)) {
184 // They've asked us to fetch a type index, but the entry we found in the
185 // partial offsets array has already been visited. Since we visit an entire
186 // block every time, that means this record should have been previously
187 // discovered. Ultimately, this means this is a request for a non-existent
188 // type index.
189 return make_error<CodeViewError>("Invalid type index");
190 }
191
192 TypeIndex TIE;
193 if (Next == PartialOffsets.end()) {
195 } else {
196 TIE = Next->Type;
197 }
198
199 visitRange(TIB, Prev->Offset, TIE);
200 return Error::success();
201}
202
203std::optional<TypeIndex> LazyRandomTypeCollection::getFirst() {
205 if (auto EC = ensureTypeExists(TI)) {
206 consumeError(std::move(EC));
207 return std::nullopt;
208 }
209 return TI;
210}
211
212std::optional<TypeIndex> LazyRandomTypeCollection::getNext(TypeIndex Prev) {
213 // We can't be sure how long this type stream is, given that the initial count
214 // given to the constructor is just a hint. So just try to make sure the next
215 // record exists, and if anything goes wrong, we must be at the end.
216 if (auto EC = ensureTypeExists(Prev + 1)) {
217 consumeError(std::move(EC));
218 return std::nullopt;
219 }
220
221 return Prev + 1;
222}
223
224Error LazyRandomTypeCollection::fullScanForType(TypeIndex TI) {
225 assert(!TI.isSimple());
226 assert(PartialOffsets.empty());
227
229 auto Begin = Types.begin();
230
231 if (Count > 0) {
232 // In the case of type streams which we don't know the number of records of,
233 // it's possible to search for a type index triggering a full scan, but then
234 // later additional records are added since we didn't know how many there
235 // would be until we did a full visitation, then you try to access the new
236 // type triggering another full scan. To avoid this, we assume that if the
237 // database has some records, this must be what's going on. We can also
238 // assume that this index must be larger than the largest type index we've
239 // visited, so we start from there and scan forward.
240 uint32_t Offset = Records[LargestTypeIndex.toArrayIndex()].Offset;
241 CurrentTI = LargestTypeIndex + 1;
242 Begin = Types.at(Offset);
243 ++Begin;
244 }
245
246 auto End = Types.end();
247 while (Begin != End) {
248 ensureCapacityFor(CurrentTI);
249 LargestTypeIndex = std::max(LargestTypeIndex, CurrentTI);
250 auto Idx = CurrentTI.toArrayIndex();
251 Records[Idx].Type = *Begin;
252 Records[Idx].Offset = Begin.offset();
253 ++Count;
254 ++Begin;
255 ++CurrentTI;
256 }
257 if (CurrentTI <= TI) {
258 return make_error<CodeViewError>("Type Index does not exist!");
259 }
260 return Error::success();
261}
262
263void LazyRandomTypeCollection::visitRange(TypeIndex Begin, uint32_t BeginOffset,
264 TypeIndex End) {
265 auto RI = Types.at(BeginOffset);
266 assert(RI != Types.end());
267
268 ensureCapacityFor(End);
269 while (Begin != End) {
270 LargestTypeIndex = std::max(LargestTypeIndex, Begin);
271 auto Idx = Begin.toArrayIndex();
272 Records[Idx].Type = *RI;
273 Records[Idx].Offset = RI.offset();
274 ++Count;
275 ++Begin;
276 ++RI;
277 }
278}
279
281 bool Stabilize) {
282 llvm_unreachable("Method cannot be called");
283}
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
bool End
Definition: ELF_riscv.cpp:464
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
#define error(X)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Provides read only access to a subclass of BinaryStream.
uint64_t bytesRemaining() const
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
FixedStreamArrayIterator< T > begin() const
FixedStreamArrayIterator< T > end() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringRef save(const char *S)
Definition: StringSaver.h:30
LLVM Value Representation.
Definition: Value.h:74
Iterator at(uint32_t Offset) const
given an offset into the array's underlying stream, return an iterator to the record at that offset.
Iterator end() const
Iterator begin(bool *HadError=nullptr) const
Provides amortized O(1) random access to a CodeView type stream.
std::optional< TypeIndex > getNext(TypeIndex Prev) override
std::optional< CVType > tryGetType(TypeIndex Index)
bool replaceType(TypeIndex &Index, CVType Data, bool Stabilize) override
StringRef getTypeName(TypeIndex Index) override
std::optional< TypeIndex > getFirst() override
void reset(ArrayRef< uint8_t > Data, uint32_t RecordCountHint)
A 32-bit type reference.
Definition: TypeIndex.h:96
static TypeIndex fromArrayIndex(uint32_t Index)
Definition: TypeIndex.h:123
uint32_t toArrayIndex() const
Definition: TypeIndex.h:118
static StringRef simpleTypeName(TypeIndex TI)
Definition: TypeIndex.cpp:71
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::string computeTypeName(TypeCollection &Types, TypeIndex Index)
Definition: RecordName.cpp:254
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:2051
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043