Line data Source code
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 : //===----------------------------------------------------------------------===//
9 :
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>
27 :
28 : using namespace llvm;
29 : using namespace llvm::codeview;
30 :
31 2827 : TypeIndex MergingTypeTableBuilder::nextTypeIndex() const {
32 5654 : return TypeIndex::fromArrayIndex(SeenRecords.size());
33 : }
34 :
35 6186 : MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator &Storage)
36 6186 : : RecordStorage(Storage) {
37 : SeenRecords.reserve(4096);
38 6186 : }
39 :
40 : MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
41 :
42 182 : Optional<TypeIndex> MergingTypeTableBuilder::getFirst() {
43 182 : if (empty())
44 : return None;
45 :
46 68 : return TypeIndex(TypeIndex::FirstNonSimpleIndex);
47 : }
48 :
49 1188 : Optional<TypeIndex> MergingTypeTableBuilder::getNext(TypeIndex Prev) {
50 1188 : if (++Prev == nextTypeIndex())
51 : return None;
52 : return Prev;
53 : }
54 :
55 1253 : CVType MergingTypeTableBuilder::getType(TypeIndex Index) {
56 : CVType Type;
57 2506 : Type.RecordData = SeenRecords[Index.toArrayIndex()];
58 : const RecordPrefix *P =
59 1253 : reinterpret_cast<const RecordPrefix *>(Type.RecordData.data());
60 1253 : Type.Type = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
61 1253 : return Type;
62 : }
63 :
64 0 : StringRef MergingTypeTableBuilder::getTypeName(TypeIndex Index) {
65 0 : llvm_unreachable("Method not implemented");
66 : }
67 :
68 0 : bool MergingTypeTableBuilder::contains(TypeIndex Index) {
69 0 : if (Index.isSimple() || Index.isNoneType())
70 0 : return false;
71 :
72 0 : return Index.toArrayIndex() < SeenRecords.size();
73 : }
74 :
75 182 : uint32_t MergingTypeTableBuilder::size() { return SeenRecords.size(); }
76 :
77 0 : uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords.size(); }
78 :
79 8 : ArrayRef<ArrayRef<uint8_t>> MergingTypeTableBuilder::records() const {
80 8 : return SeenRecords;
81 : }
82 :
83 0 : void MergingTypeTableBuilder::reset() {
84 0 : HashedRecords.clear();
85 : SeenRecords.clear();
86 0 : }
87 :
88 1257 : static inline ArrayRef<uint8_t> stabilize(BumpPtrAllocator &Alloc,
89 : ArrayRef<uint8_t> Data) {
90 : uint8_t *Stable = Alloc.Allocate<uint8_t>(Data.size());
91 1257 : memcpy(Stable, Data.data(), Data.size());
92 1257 : return makeArrayRef(Stable, Data.size());
93 : }
94 :
95 1639 : 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!");
99 :
100 1639 : LocallyHashedType WeakHash{Hash, Record};
101 1639 : auto Result = HashedRecords.try_emplace(WeakHash, nextTypeIndex());
102 :
103 1639 : if (Result.second) {
104 1257 : ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Record);
105 1257 : Result.first->first.RecordData = RecordData;
106 1257 : SeenRecords.push_back(RecordData);
107 : }
108 :
109 : // Update the caller's copy of Record to point a stable copy.
110 1639 : TypeIndex ActualTI = Result.first->second;
111 3278 : Record = SeenRecords[ActualTI.toArrayIndex()];
112 1639 : return ActualTI;
113 : }
114 :
115 : TypeIndex
116 1639 : MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
117 1639 : return insertRecordAs(hash_value(Record), Record);
118 : }
119 :
120 : TypeIndex
121 0 : MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
122 : TypeIndex TI;
123 0 : auto Fragments = Builder.end(nextTypeIndex());
124 : assert(!Fragments.empty());
125 0 : for (auto C : Fragments)
126 0 : TI = insertRecordBytes(C.RecordData);
127 0 : return TI;
128 : }
|