1 //===- MergingTypeTableBuilder.cpp ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/DenseSet.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/DebugInfo/CodeView/CodeView.h"
14 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
15 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
16 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/BinaryByteStream.h"
19 #include "llvm/Support/BinaryStreamWriter.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
28 using namespace llvm::codeview
;
30 TypeIndex
MergingTypeTableBuilder::nextTypeIndex() const {
31 return TypeIndex::fromArrayIndex(SeenRecords
.size());
34 MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator
&Storage
)
35 : RecordStorage(Storage
) {
36 SeenRecords
.reserve(4096);
39 MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
41 Optional
<TypeIndex
> MergingTypeTableBuilder::getFirst() {
45 return TypeIndex(TypeIndex::FirstNonSimpleIndex
);
48 Optional
<TypeIndex
> MergingTypeTableBuilder::getNext(TypeIndex Prev
) {
49 if (++Prev
== nextTypeIndex())
54 CVType
MergingTypeTableBuilder::getType(TypeIndex Index
) {
55 CVType
Type(SeenRecords
[Index
.toArrayIndex()]);
59 StringRef
MergingTypeTableBuilder::getTypeName(TypeIndex Index
) {
60 llvm_unreachable("Method not implemented");
63 bool MergingTypeTableBuilder::contains(TypeIndex Index
) {
64 if (Index
.isSimple() || Index
.isNoneType())
67 return Index
.toArrayIndex() < SeenRecords
.size();
70 uint32_t MergingTypeTableBuilder::size() { return SeenRecords
.size(); }
72 uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords
.size(); }
74 ArrayRef
<ArrayRef
<uint8_t>> MergingTypeTableBuilder::records() const {
78 void MergingTypeTableBuilder::reset() {
79 HashedRecords
.clear();
83 static inline ArrayRef
<uint8_t> stabilize(BumpPtrAllocator
&Alloc
,
84 ArrayRef
<uint8_t> Data
) {
85 uint8_t *Stable
= Alloc
.Allocate
<uint8_t>(Data
.size());
86 memcpy(Stable
, Data
.data(), Data
.size());
87 return makeArrayRef(Stable
, Data
.size());
90 TypeIndex
MergingTypeTableBuilder::insertRecordAs(hash_code Hash
,
91 ArrayRef
<uint8_t> &Record
) {
92 assert(Record
.size() < UINT32_MAX
&& "Record too big");
93 assert(Record
.size() % 4 == 0 && "Record is not aligned to 4 bytes!");
95 LocallyHashedType WeakHash
{Hash
, Record
};
96 auto Result
= HashedRecords
.try_emplace(WeakHash
, nextTypeIndex());
99 ArrayRef
<uint8_t> RecordData
= stabilize(RecordStorage
, Record
);
100 Result
.first
->first
.RecordData
= RecordData
;
101 SeenRecords
.push_back(RecordData
);
104 // Update the caller's copy of Record to point a stable copy.
105 TypeIndex ActualTI
= Result
.first
->second
;
106 Record
= SeenRecords
[ActualTI
.toArrayIndex()];
111 MergingTypeTableBuilder::insertRecordBytes(ArrayRef
<uint8_t> &Record
) {
112 return insertRecordAs(hash_value(Record
), Record
);
116 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder
&Builder
) {
118 auto Fragments
= Builder
.end(nextTypeIndex());
119 assert(!Fragments
.empty());
120 for (auto C
: Fragments
)
121 TI
= insertRecordBytes(C
.RecordData
);