[llvm-exegesis][NFC] Fix typo
[llvm-complete.git] / lib / DebugInfo / CodeView / MergingTypeTableBuilder.cpp
blob8aee4aa2e2aee7e1406d1ee12966490705ad4ad9
1 //===- MergingTypeTableBuilder.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 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/DebugInfo/CodeView/CodeView.h"
15 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
16 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
17 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/BinaryByteStream.h"
20 #include "llvm/Support/BinaryStreamWriter.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/Error.h"
23 #include <algorithm>
24 #include <cassert>
25 #include <cstdint>
26 #include <cstring>
28 using namespace llvm;
29 using namespace llvm::codeview;
31 TypeIndex MergingTypeTableBuilder::nextTypeIndex() const {
32 return TypeIndex::fromArrayIndex(SeenRecords.size());
35 MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator &Storage)
36 : RecordStorage(Storage) {
37 SeenRecords.reserve(4096);
40 MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
42 Optional<TypeIndex> MergingTypeTableBuilder::getFirst() {
43 if (empty())
44 return None;
46 return TypeIndex(TypeIndex::FirstNonSimpleIndex);
49 Optional<TypeIndex> MergingTypeTableBuilder::getNext(TypeIndex Prev) {
50 if (++Prev == nextTypeIndex())
51 return None;
52 return Prev;
55 CVType MergingTypeTableBuilder::getType(TypeIndex Index) {
56 CVType Type;
57 Type.RecordData = SeenRecords[Index.toArrayIndex()];
58 const RecordPrefix *P =
59 reinterpret_cast<const RecordPrefix *>(Type.RecordData.data());
60 Type.Type = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
61 return Type;
64 StringRef MergingTypeTableBuilder::getTypeName(TypeIndex Index) {
65 llvm_unreachable("Method not implemented");
68 bool MergingTypeTableBuilder::contains(TypeIndex Index) {
69 if (Index.isSimple() || Index.isNoneType())
70 return false;
72 return Index.toArrayIndex() < SeenRecords.size();
75 uint32_t MergingTypeTableBuilder::size() { return SeenRecords.size(); }
77 uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords.size(); }
79 ArrayRef<ArrayRef<uint8_t>> MergingTypeTableBuilder::records() const {
80 return SeenRecords;
83 void MergingTypeTableBuilder::reset() {
84 HashedRecords.clear();
85 SeenRecords.clear();
88 static inline ArrayRef<uint8_t> stabilize(BumpPtrAllocator &Alloc,
89 ArrayRef<uint8_t> Data) {
90 uint8_t *Stable = Alloc.Allocate<uint8_t>(Data.size());
91 memcpy(Stable, Data.data(), Data.size());
92 return makeArrayRef(Stable, Data.size());
95 TypeIndex MergingTypeTableBuilder::insertRecordAs(hash_code Hash,
96 ArrayRef<uint8_t> &Record) {
97 assert(Record.size() < UINT32_MAX && "Record too big");
98 assert(Record.size() % 4 == 0 && "Record is not aligned to 4 bytes!");
100 LocallyHashedType WeakHash{Hash, Record};
101 auto Result = HashedRecords.try_emplace(WeakHash, nextTypeIndex());
103 if (Result.second) {
104 ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Record);
105 Result.first->first.RecordData = RecordData;
106 SeenRecords.push_back(RecordData);
109 // Update the caller's copy of Record to point a stable copy.
110 TypeIndex ActualTI = Result.first->second;
111 Record = SeenRecords[ActualTI.toArrayIndex()];
112 return ActualTI;
115 TypeIndex
116 MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
117 return insertRecordAs(hash_value(Record), Record);
120 TypeIndex
121 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
122 TypeIndex TI;
123 auto Fragments = Builder.end(nextTypeIndex());
124 assert(!Fragments.empty());
125 for (auto C : Fragments)
126 TI = insertRecordBytes(C.RecordData);
127 return TI;