LLVM 17.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/DenseMap.h"
21#include <cassert>
22#include <cstdint>
23
24namespace llvm {
25namespace codeview {
26
27class 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
47public:
50
51 // TypeCollection overrides
52 std::optional<TypeIndex> getFirst() override;
53 std::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();
64
65 BumpPtrAllocator &getAllocator() { return RecordStorage; }
66
69
70 template <typename CreateFunc>
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
This file defines the BumpPtrAllocator interface.
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:210
This file defines the DenseMap class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:158
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:148
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:222
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:305
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::optional< TypeIndex > getNext(TypeIndex Prev) override
ArrayRef< ArrayRef< uint8_t > > records() const
TypeIndex insertRecord(ContinuationRecordBuilder &Builder)
CVType getType(TypeIndex Index) override
TypeIndex insertRecordBytes(ArrayRef< uint8_t > Data)
TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize, CreateFunc Create)
bool replaceType(TypeIndex &Index, CVType Data, bool Stabilize) override
ArrayRef< GloballyHashedType > hashes() const
std::optional< TypeIndex > getFirst() override
ArrayRef< uint8_t > serialize(T &Record)
A 32-bit type reference.
Definition: TypeIndex.h:96
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringRef getTypeName()
We provide a function which tries to compute the (demangled) name of a type statically.
Definition: TypeName.h:27
A globally hashed type represents a hash value that is sufficient to uniquely identify a record acros...
Definition: TypeHashing.h:80