LLVM 22.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"
22#include <cassert>
23#include <cstdint>
24
25namespace llvm {
26namespace codeview {
27
28class ContinuationRecordBuilder;
29
31 /// Storage for records. These need to outlive the TypeTableBuilder.
32 BumpPtrAllocator &RecordStorage;
33
34 /// A serializer that can write non-continuation leaf types. Only used as
35 /// a convenience function so that we can provide an interface method to
36 /// write an unserialized record.
37 SimpleTypeSerializer SimpleSerializer;
38
39 /// Hash table.
41
42 /// Contains a list of all records indexed by TypeIndex.toArrayIndex().
43 SmallVector<ArrayRef<uint8_t>, 2> SeenRecords;
44
45 /// Contains a list of all hash values indexed by TypeIndex.toArrayIndex().
47
48public:
51
52 // TypeCollection overrides
53 std::optional<TypeIndex> getFirst() override;
54 std::optional<TypeIndex> getNext(TypeIndex Prev) override;
55 CVType getType(TypeIndex Index) override;
57 bool contains(TypeIndex Index) override;
58 uint32_t size() override;
59 uint32_t capacity() override;
60 bool replaceType(TypeIndex &Index, CVType Data, bool Stabilize) override;
61
62 // public interface
63 void reset();
64 TypeIndex nextTypeIndex() const;
65
66 BumpPtrAllocator &getAllocator() { return RecordStorage; }
67
68 ArrayRef<ArrayRef<uint8_t>> records() const;
69 ArrayRef<GloballyHashedType> hashes() const;
70
71 template <typename CreateFunc>
73 CreateFunc Create) {
74 assert(RecordSize < UINT32_MAX && "Record too big");
75 assert(RecordSize % 4 == 0 &&
76 "RecordSize is not a multiple of 4 bytes which will cause "
77 "misalignment in the output TPI stream!");
78
79 auto Result = HashedRecords.try_emplace(Hash, nextTypeIndex());
80
81 if (LLVM_UNLIKELY(Result.second /*inserted*/ ||
82 Result.first->second.isSimple())) {
83 uint8_t *Stable = RecordStorage.Allocate<uint8_t>(RecordSize);
84 MutableArrayRef<uint8_t> Data(Stable, RecordSize);
85 ArrayRef<uint8_t> StableRecord = Create(Data);
86 if (StableRecord.empty()) {
87 // Records with forward references into the Type stream will be deferred
88 // for insertion at a later time, on the second pass.
89 Result.first->getSecond() = TypeIndex(SimpleTypeKind::NotTranslated);
90 return TypeIndex(SimpleTypeKind::NotTranslated);
91 }
92 if (Result.first->second.isSimple()) {
93 assert(Result.first->second.getIndex() ==
94 (uint32_t)SimpleTypeKind::NotTranslated);
95 // On the second pass, update with index to remapped record. The
96 // (initially misbehaved) record will now come *after* other records
97 // resolved in the first pass, with proper *back* references in the
98 // stream.
99 Result.first->second = nextTypeIndex();
100 }
101 SeenRecords.push_back(StableRecord);
102 SeenHashes.push_back(Hash);
103 }
104
105 return Result.first->second;
106 }
107
108 TypeIndex insertRecordBytes(ArrayRef<uint8_t> Data);
109 TypeIndex insertRecord(ContinuationRecordBuilder &Builder);
110
111 template <typename T> TypeIndex writeLeafType(T &Record) {
112 ArrayRef<uint8_t> Data = SimpleSerializer.serialize(Record);
113 return insertRecordBytes(Data);
114 }
115};
116
117} // end namespace codeview
118} // end namespace llvm
119
120#endif // LLVM_DEBUGINFO_CODEVIEW_GLOBALTYPETABLEBUILDER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:336
#define LLVM_ABI
Definition: Compiler.h:213
static std::string getTypeName(OverloadKind Kind, Type *Ty)
This file defines the DenseMap class.
uint32_t Index
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:480
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
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:142
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:245
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize, CreateFunc Create)
ArrayRef< uint8_t > serialize(T &Record)
A 32-bit type reference.
Definition: TypeIndex.h:97
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1702
A globally hashed type represents a hash value that is sufficient to uniquely identify a record acros...
Definition: TypeHashing.h:81