]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/DebugInfo/CodeView/MergingTypeTableBuilder.cpp
Merge ^/vendor/lldb/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / DebugInfo / CodeView / MergingTypeTableBuilder.cpp
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
30 TypeIndex MergingTypeTableBuilder::nextTypeIndex() const {
31   return TypeIndex::fromArrayIndex(SeenRecords.size());
32 }
33
34 MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator &Storage)
35     : RecordStorage(Storage) {
36   SeenRecords.reserve(4096);
37 }
38
39 MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
40
41 Optional<TypeIndex> MergingTypeTableBuilder::getFirst() {
42   if (empty())
43     return None;
44
45   return TypeIndex(TypeIndex::FirstNonSimpleIndex);
46 }
47
48 Optional<TypeIndex> MergingTypeTableBuilder::getNext(TypeIndex Prev) {
49   if (++Prev == nextTypeIndex())
50     return None;
51   return Prev;
52 }
53
54 CVType MergingTypeTableBuilder::getType(TypeIndex Index) {
55   CVType Type(SeenRecords[Index.toArrayIndex()]);
56   return Type;
57 }
58
59 StringRef MergingTypeTableBuilder::getTypeName(TypeIndex Index) {
60   llvm_unreachable("Method not implemented");
61 }
62
63 bool MergingTypeTableBuilder::contains(TypeIndex Index) {
64   if (Index.isSimple() || Index.isNoneType())
65     return false;
66
67   return Index.toArrayIndex() < SeenRecords.size();
68 }
69
70 uint32_t MergingTypeTableBuilder::size() { return SeenRecords.size(); }
71
72 uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords.size(); }
73
74 ArrayRef<ArrayRef<uint8_t>> MergingTypeTableBuilder::records() const {
75   return SeenRecords;
76 }
77
78 void MergingTypeTableBuilder::reset() {
79   HashedRecords.clear();
80   SeenRecords.clear();
81 }
82
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
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!");
94
95   LocallyHashedType WeakHash{Hash, Record};
96   auto Result = HashedRecords.try_emplace(WeakHash, nextTypeIndex());
97
98   if (Result.second) {
99     ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Record);
100     Result.first->first.RecordData = RecordData;
101     SeenRecords.push_back(RecordData);
102   }
103
104   // Update the caller's copy of Record to point a stable copy.
105   TypeIndex ActualTI = Result.first->second;
106   Record = SeenRecords[ActualTI.toArrayIndex()];
107   return ActualTI;
108 }
109
110 TypeIndex
111 MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
112   return insertRecordAs(hash_value(Record), Record);
113 }
114
115 TypeIndex
116 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
117   TypeIndex TI;
118   auto Fragments = Builder.end(nextTypeIndex());
119   assert(!Fragments.empty());
120   for (auto C : Fragments)
121     TI = insertRecordBytes(C.RecordData);
122   return TI;
123 }