1 //===- MergingTypeTableBuilder.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
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"
22 #include <algorithm>
23 #include <cassert>
24 #include <cstdint>
25 #include <cstring>
26
27 using namespace llvm;
28 using namespace llvm::codeview;
29
nextTypeIndex() const30 TypeIndex MergingTypeTableBuilder::nextTypeIndex() const {
31 return TypeIndex::fromArrayIndex(SeenRecords.size());
32 }
33
MergingTypeTableBuilder(BumpPtrAllocator & Storage)34 MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator &Storage)
35 : RecordStorage(Storage) {
36 SeenRecords.reserve(4096);
37 }
38
39 MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
40
getFirst()41 Optional<TypeIndex> MergingTypeTableBuilder::getFirst() {
42 if (empty())
43 return None;
44
45 return TypeIndex(TypeIndex::FirstNonSimpleIndex);
46 }
47
getNext(TypeIndex Prev)48 Optional<TypeIndex> MergingTypeTableBuilder::getNext(TypeIndex Prev) {
49 if (++Prev == nextTypeIndex())
50 return None;
51 return Prev;
52 }
53
getType(TypeIndex Index)54 CVType MergingTypeTableBuilder::getType(TypeIndex Index) {
55 CVType Type(SeenRecords[Index.toArrayIndex()]);
56 return Type;
57 }
58
getTypeName(TypeIndex Index)59 StringRef MergingTypeTableBuilder::getTypeName(TypeIndex Index) {
60 llvm_unreachable("Method not implemented");
61 }
62
contains(TypeIndex Index)63 bool MergingTypeTableBuilder::contains(TypeIndex Index) {
64 if (Index.isSimple() || Index.isNoneType())
65 return false;
66
67 return Index.toArrayIndex() < SeenRecords.size();
68 }
69
size()70 uint32_t MergingTypeTableBuilder::size() { return SeenRecords.size(); }
71
capacity()72 uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords.size(); }
73
records() const74 ArrayRef<ArrayRef<uint8_t>> MergingTypeTableBuilder::records() const {
75 return SeenRecords;
76 }
77
reset()78 void MergingTypeTableBuilder::reset() {
79 HashedRecords.clear();
80 SeenRecords.clear();
81 }
82
stabilize(BumpPtrAllocator & Alloc,ArrayRef<uint8_t> Data)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());
88 }
89
insertRecordAs(hash_code Hash,ArrayRef<uint8_t> & Record)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!");
96
97 LocallyHashedType WeakHash{Hash, Record};
98 auto Result = HashedRecords.try_emplace(WeakHash, nextTypeIndex());
99
100 if (Result.second) {
101 ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Record);
102 Result.first->first.RecordData = RecordData;
103 SeenRecords.push_back(RecordData);
104 }
105
106 // Update the caller's copy of Record to point a stable copy.
107 TypeIndex ActualTI = Result.first->second;
108 Record = SeenRecords[ActualTI.toArrayIndex()];
109 return ActualTI;
110 }
111
112 TypeIndex
insertRecordBytes(ArrayRef<uint8_t> & Record)113 MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
114 return insertRecordAs(hash_value(Record), Record);
115 }
116
117 TypeIndex
insertRecord(ContinuationRecordBuilder & Builder)118 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
119 TypeIndex TI;
120 auto Fragments = Builder.end(nextTypeIndex());
121 assert(!Fragments.empty());
122 for (auto C : Fragments)
123 TI = insertRecordBytes(C.RecordData);
124 return TI;
125 }
126
replaceType(TypeIndex & Index,CVType Data,bool Stabilize)127 bool MergingTypeTableBuilder::replaceType(TypeIndex &Index, CVType Data,
128 bool Stabilize) {
129 assert(Index.toArrayIndex() < SeenRecords.size() &&
130 "This function cannot be used to insert records!");
131
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!");
137
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
143 }
144
145 if (Stabilize) {
146 Record = stabilize(RecordStorage, Record);
147 Result.first->first.RecordData = Record;
148 }
149
150 SeenRecords[Index.toArrayIndex()] = Record;
151 return true;
152 }
153