LLVM  6.0.0svn
LazyRandomTypeCollection.cpp
Go to the documentation of this file.
1 //===- LazyRandomTypeCollection.cpp ---------------------------------------===//
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 
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/None.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/Error.h"
21 #include <algorithm>
22 #include <cassert>
23 #include <cstdint>
24 #include <iterator>
25 
26 using namespace llvm;
27 using namespace llvm::codeview;
28 
29 static void error(Error &&EC) {
30  assert(!static_cast<bool>(EC));
31  if (EC)
32  consumeError(std::move(EC));
33 }
34 
36  : LazyRandomTypeCollection(CVTypeArray(), RecordCountHint,
37  PartialOffsetArray()) {}
38 
40  const CVTypeArray &Types, uint32_t RecordCountHint,
41  PartialOffsetArray PartialOffsets)
42  : NameStorage(Allocator), Types(Types), PartialOffsets(PartialOffsets) {
43  Records.resize(RecordCountHint);
44 }
45 
47  uint32_t RecordCountHint)
48  : LazyRandomTypeCollection(RecordCountHint) {
49 }
50 
52  uint32_t RecordCountHint)
54  makeArrayRef(Data.bytes_begin(), Data.bytes_end()), RecordCountHint) {
55 }
56 
58  uint32_t NumRecords)
59  : LazyRandomTypeCollection(Types, NumRecords, PartialOffsetArray()) {}
60 
61 void LazyRandomTypeCollection::reset(StringRef Data, uint32_t RecordCountHint) {
62  Count = 0;
63  PartialOffsets = PartialOffsetArray();
64 
65  BinaryStreamReader Reader(Data, support::little);
66  error(Reader.readArray(Types, Reader.getLength()));
67 
68  // Clear and then resize, to make sure existing data gets destroyed.
69  Records.clear();
70  Records.resize(RecordCountHint);
71 }
72 
74  uint32_t RecordCountHint) {
75  reset(toStringRef(Data), RecordCountHint);
76 }
77 
79  error(ensureTypeExists(Index));
80  assert(contains(Index));
81 
82  return Records[Index.toArrayIndex()].Offset;
83 }
84 
86  auto EC = ensureTypeExists(Index);
87  error(std::move(EC));
88  assert(contains(Index));
89 
90  return Records[Index.toArrayIndex()].Type;
91 }
92 
94  if (auto EC = ensureTypeExists(Index)) {
95  consumeError(std::move(EC));
96  return None;
97  }
98 
99  assert(contains(Index));
100  return Records[Index.toArrayIndex()].Type;
101 }
102 
104  if (Index.isNoneType() || Index.isSimple())
105  return TypeIndex::simpleTypeName(Index);
106 
107  // Try to make sure the type exists. Even if it doesn't though, it may be
108  // because we're dumping a symbol stream with no corresponding type stream
109  // present, in which case we still want to be able to print <unknown UDT>
110  // for the type names.
111  if (auto EC = ensureTypeExists(Index)) {
112  consumeError(std::move(EC));
113  return "<unknown UDT>";
114  }
115 
116  uint32_t I = Index.toArrayIndex();
117  ensureCapacityFor(Index);
118  if (Records[I].Name.data() == nullptr) {
119  StringRef Result = NameStorage.save(computeTypeName(*this, Index));
120  Records[I].Name = Result;
121  }
122  return Records[I].Name;
123 }
124 
126  if (Index.isSimple() || Index.isNoneType())
127  return false;
128 
129  if (Records.size() <= Index.toArrayIndex())
130  return false;
131  if (!Records[Index.toArrayIndex()].Type.valid())
132  return false;
133  return true;
134 }
135 
137 
138 uint32_t LazyRandomTypeCollection::capacity() { return Records.size(); }
139 
140 Error LazyRandomTypeCollection::ensureTypeExists(TypeIndex TI) {
141  if (contains(TI))
142  return Error::success();
143 
144  return visitRangeForType(TI);
145 }
146 
147 void LazyRandomTypeCollection::ensureCapacityFor(TypeIndex Index) {
148  uint32_t MinSize = Index.toArrayIndex() + 1;
149 
150  if (MinSize <= capacity())
151  return;
152 
153  uint32_t NewCapacity = MinSize * 3 / 2;
154 
155  assert(NewCapacity > capacity());
156  Records.resize(NewCapacity);
157 }
158 
159 Error LazyRandomTypeCollection::visitRangeForType(TypeIndex TI) {
160  if (PartialOffsets.empty())
161  return fullScanForType(TI);
162 
163  auto Next = std::upper_bound(PartialOffsets.begin(), PartialOffsets.end(), TI,
164  [](TypeIndex Value, const TypeIndexOffset &IO) {
165  return Value < IO.Type;
166  });
167 
168  assert(Next != PartialOffsets.begin());
169  auto Prev = std::prev(Next);
170 
171  TypeIndex TIB = Prev->Type;
172  if (contains(TIB)) {
173  // They've asked us to fetch a type index, but the entry we found in the
174  // partial offsets array has already been visited. Since we visit an entire
175  // block every time, that means this record should have been previously
176  // discovered. Ultimately, this means this is a request for a non-existant
177  // type index.
178  return make_error<CodeViewError>("Invalid type index");
179  }
180 
181  TypeIndex TIE;
182  if (Next == PartialOffsets.end()) {
184  } else {
185  TIE = Next->Type;
186  }
187 
188  visitRange(TIB, Prev->Offset, TIE);
189  return Error::success();
190 }
191 
194  if (auto EC = ensureTypeExists(TI)) {
195  consumeError(std::move(EC));
196  return None;
197  }
198  return TI;
199 }
200 
202  // We can't be sure how long this type stream is, given that the initial count
203  // given to the constructor is just a hint. So just try to make sure the next
204  // record exists, and if anything goes wrong, we must be at the end.
205  if (auto EC = ensureTypeExists(Prev + 1)) {
206  consumeError(std::move(EC));
207  return None;
208  }
209 
210  return Prev + 1;
211 }
212 
213 Error LazyRandomTypeCollection::fullScanForType(TypeIndex TI) {
214  assert(PartialOffsets.empty());
215 
216  TypeIndex CurrentTI = TypeIndex::fromArrayIndex(0);
217  auto Begin = Types.begin();
218 
219  if (Count > 0) {
220  // In the case of type streams which we don't know the number of records of,
221  // it's possible to search for a type index triggering a full scan, but then
222  // later additional records are added since we didn't know how many there
223  // would be until we did a full visitation, then you try to access the new
224  // type triggering another full scan. To avoid this, we assume that if the
225  // database has some records, this must be what's going on. We can also
226  // assume that this index must be larger than the largest type index we've
227  // visited, so we start from there and scan forward.
228  uint32_t Offset = Records[LargestTypeIndex.toArrayIndex()].Offset;
229  CurrentTI = LargestTypeIndex + 1;
230  Begin = Types.at(Offset);
231  ++Begin;
232  }
233 
234  auto End = Types.end();
235  while (Begin != End) {
236  ensureCapacityFor(CurrentTI);
237  LargestTypeIndex = std::max(LargestTypeIndex, CurrentTI);
238  auto Idx = CurrentTI.toArrayIndex();
239  Records[Idx].Type = *Begin;
240  Records[Idx].Offset = Begin.offset();
241  ++Count;
242  ++Begin;
243  ++CurrentTI;
244  }
245  if (CurrentTI <= TI) {
246  return make_error<CodeViewError>("Type Index does not exist!");
247  }
248  return Error::success();
249 }
250 
251 void LazyRandomTypeCollection::visitRange(TypeIndex Begin, uint32_t BeginOffset,
252  TypeIndex End) {
253  auto RI = Types.at(BeginOffset);
254  assert(RI != Types.end());
255 
256  ensureCapacityFor(End);
257  while (Begin != End) {
258  LargestTypeIndex = std::max(LargestTypeIndex, Begin);
259  auto Idx = Begin.toArrayIndex();
260  Records[Idx].Type = *RI;
261  Records[Idx].Offset = RI.offset();
262  ++Count;
263  ++Begin;
264  ++RI;
265  }
266 }
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Iterator end() const
std::string computeTypeName(TypeCollection &Types, TypeIndex Index)
Definition: RecordName.cpp:236
Optional< TypeIndex > getNext(TypeIndex Prev) override
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
bool isNoneType() const
Definition: TypeIndex.h:114
void reset(ArrayRef< uint8_t > Data, uint32_t RecordCountHint)
static void error(Error &&EC)
A 32-bit type reference.
Definition: TypeIndex.h:96
uint32_t toArrayIndex() const
Definition: TypeIndex.h:116
static TypeIndex fromArrayIndex(uint32_t Index)
Definition: TypeIndex.h:121
static StringRef simpleTypeName(TypeIndex TI)
Definition: TypeIndex.cpp:71
Optional< CVType > tryGetType(TypeIndex Index)
static const unsigned End
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:43
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:962
StringRef getTypeName(TypeIndex Index) override
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
StringRef save(const char *S)
Definition: StringSaver.h:26
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FixedStreamArrayIterator< T > end() const
Iterator at(uint32_t Offset) const
given an offset into the array&#39;s underlying stream, return an iterator to the record at that offset...
Iterator begin(bool *HadError=nullptr) const
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
FixedStreamArrayIterator< T > begin() 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 ...
Provides amortized O(1) random access to a CodeView type stream.