LLVM  14.0.0git
GlobalTypeTableBuilder.h
Go to the documentation of this file.
1 //===- GlobalTypeTableBuilder.h ----------------------------------*- C++-*-===//
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 #ifndef LLVM_DEBUGINFO_CODEVIEW_GLOBALTYPETABLEBUILDER_H
10 #define LLVM_DEBUGINFO_CODEVIEW_GLOBALTYPETABLEBUILDER_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Support/Allocator.h"
21 #include <cassert>
22 #include <cstdint>
23 
24 namespace llvm {
25 namespace codeview {
26 
27 class ContinuationRecordBuilder;
28 
30  /// Storage for records. These need to outlive the TypeTableBuilder.
31  BumpPtrAllocator &RecordStorage;
32 
33  /// A serializer that can write non-continuation leaf types. Only used as
34  /// a convenience function so that we can provide an interface method to
35  /// write an unserialized record.
36  SimpleTypeSerializer SimpleSerializer;
37 
38  /// Hash table.
40 
41  /// Contains a list of all records indexed by TypeIndex.toArrayIndex().
42  SmallVector<ArrayRef<uint8_t>, 2> SeenRecords;
43 
44  /// Contains a list of all hash values indexed by TypeIndex.toArrayIndex().
46 
47 public:
48  explicit GlobalTypeTableBuilder(BumpPtrAllocator &Storage);
50 
51  // TypeCollection overrides
52  Optional<TypeIndex> getFirst() override;
53  Optional<TypeIndex> getNext(TypeIndex Prev) override;
54  CVType getType(TypeIndex Index) override;
56  bool contains(TypeIndex Index) override;
57  uint32_t size() override;
58  uint32_t capacity() override;
59  bool replaceType(TypeIndex &Index, CVType Data, bool Stabilize) override;
60 
61  // public interface
62  void reset();
63  TypeIndex nextTypeIndex() const;
64 
65  BumpPtrAllocator &getAllocator() { return RecordStorage; }
66 
69 
70  template <typename CreateFunc>
71  TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize,
72  CreateFunc Create) {
73  assert(RecordSize < UINT32_MAX && "Record too big");
74  assert(RecordSize % 4 == 0 &&
75  "RecordSize is not a multiple of 4 bytes which will cause "
76  "misalignment in the output TPI stream!");
77 
78  auto Result = HashedRecords.try_emplace(Hash, nextTypeIndex());
79 
80  if (LLVM_UNLIKELY(Result.second /*inserted*/ ||
81  Result.first->second.isSimple())) {
82  uint8_t *Stable = RecordStorage.Allocate<uint8_t>(RecordSize);
83  MutableArrayRef<uint8_t> Data(Stable, RecordSize);
84  ArrayRef<uint8_t> StableRecord = Create(Data);
85  if (StableRecord.empty()) {
86  // Records with forward references into the Type stream will be deferred
87  // for insertion at a later time, on the second pass.
88  Result.first->getSecond() = TypeIndex(SimpleTypeKind::NotTranslated);
90  }
91  if (Result.first->second.isSimple()) {
92  assert(Result.first->second.getIndex() ==
94  // On the second pass, update with index to remapped record. The
95  // (initially misbehaved) record will now come *after* other records
96  // resolved in the first pass, with proper *back* references in the
97  // stream.
98  Result.first->second = nextTypeIndex();
99  }
100  SeenRecords.push_back(StableRecord);
101  SeenHashes.push_back(Hash);
102  }
103 
104  return Result.first->second;
105  }
106 
109 
110  template <typename T> TypeIndex writeLeafType(T &Record) {
111  ArrayRef<uint8_t> Data = SimpleSerializer.serialize(Record);
112  return insertRecordBytes(Data);
113  }
114 };
115 
116 } // end namespace codeview
117 } // end namespace llvm
118 
119 #endif // LLVM_DEBUGINFO_CODEVIEW_GLOBALTYPETABLEBUILDER_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
Allocator.h
llvm::Optional
Definition: APInt.h:33
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
llvm::MutableArrayRef< uint8_t >
llvm::BumpPtrAllocatorImpl::Allocate
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:145
llvm::codeview::GlobalTypeTableBuilder::replaceType
bool replaceType(TypeIndex &Index, CVType Data, bool Stabilize) override
Definition: GlobalTypeTableBuilder.cpp:115
SimpleTypeSerializer.h
llvm::codeview::TypeCollection
Definition: TypeCollection.h:18
llvm::codeview::GlobalTypeTableBuilder::GlobalTypeTableBuilder
GlobalTypeTableBuilder(BumpPtrAllocator &Storage)
Definition: GlobalTypeTableBuilder.cpp:34
DenseSet.h
CodeView.h
llvm::codeview::GloballyHashedType
A globally hashed type represents a hash value that is sufficient to uniquely identify a record acros...
Definition: TypeHashing.h:77
llvm::codeview::GlobalTypeTableBuilder::capacity
uint32_t capacity() override
Definition: GlobalTypeTableBuilder.cpp:72
llvm::codeview::GlobalTypeTableBuilder::hashes
ArrayRef< GloballyHashedType > hashes() const
Definition: GlobalTypeTableBuilder.cpp:78
llvm::codeview::GlobalTypeTableBuilder
Definition: GlobalTypeTableBuilder.h:29
llvm::codeview::GlobalTypeTableBuilder::size
uint32_t size() override
Definition: GlobalTypeTableBuilder.cpp:70
llvm::codeview::ContinuationRecordBuilder
Definition: ContinuationRecordBuilder.h:33
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::codeview::GlobalTypeTableBuilder::getType
CVType getType(TypeIndex Index) override
Definition: GlobalTypeTableBuilder.cpp:54
llvm::codeview::GlobalTypeTableBuilder::~GlobalTypeTableBuilder
~GlobalTypeTableBuilder()
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
llvm::DenseMap
Definition: DenseMap.h:714
llvm::codeview::GlobalTypeTableBuilder::insertRecordAs
TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize, CreateFunc Create)
Definition: GlobalTypeTableBuilder.h:71
llvm::codeview::GlobalTypeTableBuilder::writeLeafType
TypeIndex writeLeafType(T &Record)
Definition: GlobalTypeTableBuilder.h:110
ArrayRef.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Record
Definition: Record.h:1472
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:650
llvm::codeview::GlobalTypeTableBuilder::getTypeName
StringRef getTypeName(TypeIndex Index) override
Definition: GlobalTypeTableBuilder.cpp:59
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
TypeHashing.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::codeview::GlobalTypeTableBuilder::getFirst
Optional< TypeIndex > getFirst() override
Definition: GlobalTypeTableBuilder.cpp:41
llvm::codeview::CVRecord< TypeLeafKind >
uint32_t
llvm::codeview::GlobalTypeTableBuilder::insertRecord
TypeIndex insertRecord(ContinuationRecordBuilder &Builder)
Definition: GlobalTypeTableBuilder.cpp:106
llvm::codeview::GlobalTypeTableBuilder::getNext
Optional< TypeIndex > getNext(TypeIndex Prev) override
Definition: GlobalTypeTableBuilder.cpp:48
TypeCollection.h
llvm::codeview::GlobalTypeTableBuilder::insertRecordBytes
TypeIndex insertRecordBytes(ArrayRef< uint8_t > Data)
Definition: GlobalTypeTableBuilder.cpp:94
TypeIndex.h
llvm::codeview::SimpleTypeSerializer::serialize
ArrayRef< uint8_t > serialize(T &Record)
Definition: SimpleTypeSerializer.cpp:35
llvm::codeview::GlobalTypeTableBuilder::getAllocator
BumpPtrAllocator & getAllocator()
Definition: GlobalTypeTableBuilder.h:65
SmallVector.h
llvm::codeview::GlobalTypeTableBuilder::records
ArrayRef< ArrayRef< uint8_t > > records() const
Definition: GlobalTypeTableBuilder.cpp:74
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::try_emplace
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:222
llvm::codeview::TypeIndex
A 32-bit type reference.
Definition: TypeIndex.h:95
llvm::codeview::SimpleTypeKind::NotTranslated
@ NotTranslated
llvm::codeview::SimpleTypeSerializer
Definition: SimpleTypeSerializer.h:19
LLVM_UNLIKELY
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:220
llvm::codeview::GlobalTypeTableBuilder::contains
bool contains(TypeIndex Index) override
Definition: GlobalTypeTableBuilder.cpp:63
llvm::codeview::GlobalTypeTableBuilder::nextTypeIndex
TypeIndex nextTypeIndex() const
Definition: GlobalTypeTableBuilder.cpp:30
llvm::codeview::GlobalTypeTableBuilder::reset
void reset()
Definition: GlobalTypeTableBuilder.cpp:82