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 &&
94 "The type record size is not a multiple of 4 bytes which will cause "
95 "misalignment in the output TPI stream!");
97 LocallyHashedType WeakHash
{Hash
, Record
};
98 auto Result
= HashedRecords
.try_emplace(WeakHash
, nextTypeIndex());
101 ArrayRef
<uint8_t> RecordData
= stabilize(RecordStorage
, Record
);
102 Result
.first
->first
.RecordData
= RecordData
;
103 SeenRecords
.push_back(RecordData
);
106 // Update the caller's copy of Record to point a stable copy.
107 TypeIndex ActualTI
= Result
.first
->second
;
108 Record
= SeenRecords
[ActualTI
.toArrayIndex()];
113 MergingTypeTableBuilder::insertRecordBytes(ArrayRef
<uint8_t> &Record
) {
114 return insertRecordAs(hash_value(Record
), Record
);
118 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder
&Builder
) {
120 auto Fragments
= Builder
.end(nextTypeIndex());
121 assert(!Fragments
.empty());
122 for (auto C
: Fragments
)
123 TI
= insertRecordBytes(C
.RecordData
);
127 bool MergingTypeTableBuilder::replaceType(TypeIndex
&Index
, CVType Data
,
129 assert(Index
.toArrayIndex() < SeenRecords
.size() &&
130 "This function cannot be used to insert records!");
132 ArrayRef
<uint8_t> Record
= Data
.data();
133 assert(Record
.size() < UINT32_MAX
&& "Record too big");
134 assert(Record
.size() % 4 == 0 &&
135 "The type record size is not a multiple of 4 bytes which will cause "
136 "misalignment in the output TPI stream!");
138 LocallyHashedType WeakHash
{hash_value(Record
), Record
};
139 auto Result
= HashedRecords
.try_emplace(WeakHash
, Index
.toArrayIndex());
140 if (!Result
.second
) {
141 Index
= Result
.first
->second
;
142 return false; // The record is already there, at a different location
146 Record
= stabilize(RecordStorage
, Record
);
147 Result
.first
->first
.RecordData
= Record
;
150 SeenRecords
[Index
.toArrayIndex()] = Record
;