[llvm-shlib] Fix the version naming style of libLLVM for Windows (#85710)
[llvm-project.git] / llvm / lib / DebugInfo / CodeView / MergingTypeTableBuilder.cpp
blob67f5d6b00686d47ca53553f557599cf82e45737e
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 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/DebugInfo/CodeView/CodeView.h"
12 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
13 #include "llvm/DebugInfo/CodeView/TypeHashing.h"
14 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
15 #include "llvm/Support/Allocator.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include <cassert>
18 #include <cstdint>
19 #include <cstring>
21 using namespace llvm;
22 using namespace llvm::codeview;
24 TypeIndex MergingTypeTableBuilder::nextTypeIndex() const {
25 return TypeIndex::fromArrayIndex(SeenRecords.size());
28 MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator &Storage)
29 : RecordStorage(Storage) {
30 SeenRecords.reserve(4096);
33 MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
35 std::optional<TypeIndex> MergingTypeTableBuilder::getFirst() {
36 if (empty())
37 return std::nullopt;
39 return TypeIndex(TypeIndex::FirstNonSimpleIndex);
42 std::optional<TypeIndex> MergingTypeTableBuilder::getNext(TypeIndex Prev) {
43 if (++Prev == nextTypeIndex())
44 return std::nullopt;
45 return Prev;
48 CVType MergingTypeTableBuilder::getType(TypeIndex Index) {
49 CVType Type(SeenRecords[Index.toArrayIndex()]);
50 return Type;
53 StringRef MergingTypeTableBuilder::getTypeName(TypeIndex Index) {
54 llvm_unreachable("Method not implemented");
57 bool MergingTypeTableBuilder::contains(TypeIndex Index) {
58 if (Index.isSimple() || Index.isNoneType())
59 return false;
61 return Index.toArrayIndex() < SeenRecords.size();
64 uint32_t MergingTypeTableBuilder::size() { return SeenRecords.size(); }
66 uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords.size(); }
68 ArrayRef<ArrayRef<uint8_t>> MergingTypeTableBuilder::records() const {
69 return SeenRecords;
72 void MergingTypeTableBuilder::reset() {
73 HashedRecords.clear();
74 SeenRecords.clear();
77 static inline ArrayRef<uint8_t> stabilize(BumpPtrAllocator &Alloc,
78 ArrayRef<uint8_t> Data) {
79 uint8_t *Stable = Alloc.Allocate<uint8_t>(Data.size());
80 memcpy(Stable, Data.data(), Data.size());
81 return ArrayRef(Stable, Data.size());
84 TypeIndex MergingTypeTableBuilder::insertRecordAs(hash_code Hash,
85 ArrayRef<uint8_t> &Record) {
86 assert(Record.size() < UINT32_MAX && "Record too big");
87 assert(Record.size() % 4 == 0 &&
88 "The type record size is not a multiple of 4 bytes which will cause "
89 "misalignment in the output TPI stream!");
91 LocallyHashedType WeakHash{Hash, Record};
92 auto Result = HashedRecords.try_emplace(WeakHash, nextTypeIndex());
94 if (Result.second) {
95 ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Record);
96 Result.first->first.RecordData = RecordData;
97 SeenRecords.push_back(RecordData);
100 // Update the caller's copy of Record to point a stable copy.
101 TypeIndex ActualTI = Result.first->second;
102 Record = SeenRecords[ActualTI.toArrayIndex()];
103 return ActualTI;
106 TypeIndex
107 MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
108 return insertRecordAs(hash_value(Record), Record);
111 TypeIndex
112 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
113 TypeIndex TI;
114 auto Fragments = Builder.end(nextTypeIndex());
115 assert(!Fragments.empty());
116 for (auto C : Fragments)
117 TI = insertRecordBytes(C.RecordData);
118 return TI;
121 bool MergingTypeTableBuilder::replaceType(TypeIndex &Index, CVType Data,
122 bool Stabilize) {
123 assert(Index.toArrayIndex() < SeenRecords.size() &&
124 "This function cannot be used to insert records!");
126 ArrayRef<uint8_t> Record = Data.data();
127 assert(Record.size() < UINT32_MAX && "Record too big");
128 assert(Record.size() % 4 == 0 &&
129 "The type record size is not a multiple of 4 bytes which will cause "
130 "misalignment in the output TPI stream!");
132 LocallyHashedType WeakHash{hash_value(Record), Record};
133 auto Result = HashedRecords.try_emplace(WeakHash, Index.toArrayIndex());
134 if (!Result.second) {
135 Index = Result.first->second;
136 return false; // The record is already there, at a different location
139 if (Stabilize) {
140 Record = stabilize(RecordStorage, Record);
141 Result.first->first.RecordData = Record;
144 SeenRecords[Index.toArrayIndex()] = Record;
145 return true;