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 && 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 113 MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) { 114 return insertRecordAs(hash_value(Record), Record); 115 } 116 117 TypeIndex 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