LLVM  9.0.0svn
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 #include <memory>
24 #include <vector>
25 
26 namespace llvm {
27 namespace codeview {
28 
29 class ContinuationRecordBuilder;
30 
32  /// Storage for records. These need to outlive the TypeTableBuilder.
33  BumpPtrAllocator &RecordStorage;
34 
35  /// A serializer that can write non-continuation leaf types. Only used as
36  /// a convenience function so that we can provide an interface method to
37  /// write an unserialized record.
38  SimpleTypeSerializer SimpleSerializer;
39 
40  /// Hash table.
42 
43  /// Contains a list of all records indexed by TypeIndex.toArrayIndex().
44  SmallVector<ArrayRef<uint8_t>, 2> SeenRecords;
45 
46  /// Contains a list of all hash values inexed by TypeIndex.toArrayIndex().
48 
49 public:
50  explicit GlobalTypeTableBuilder(BumpPtrAllocator &Storage);
52 
53  // TypeTableCollection overrides
54  Optional<TypeIndex> getFirst() override;
55  Optional<TypeIndex> getNext(TypeIndex Prev) override;
56  CVType getType(TypeIndex Index) override;
57  StringRef getTypeName(TypeIndex Index) override;
58  bool contains(TypeIndex Index) override;
59  uint32_t size() override;
60  uint32_t capacity() override;
61 
62  // public interface
63  void reset();
64  TypeIndex nextTypeIndex() const;
65 
66  BumpPtrAllocator &getAllocator() { return RecordStorage; }
67 
70 
71  template <typename CreateFunc>
72  TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize,
73  CreateFunc Create) {
74  auto Result = HashedRecords.try_emplace(Hash, nextTypeIndex());
75 
76  if (LLVM_UNLIKELY(Result.second /*inserted*/ ||
77  Result.first->second.isSimple())) {
78  uint8_t *Stable = RecordStorage.Allocate<uint8_t>(RecordSize);
79  MutableArrayRef<uint8_t> Data(Stable, RecordSize);
80  ArrayRef<uint8_t> StableRecord = Create(Data);
81  if (StableRecord.empty()) {
82  // Records with forward references into the Type stream will be deferred
83  // for insertion at a later time, on the second pass.
84  Result.first->getSecond() = TypeIndex(SimpleTypeKind::NotTranslated);
86  }
87  if (Result.first->second.isSimple()) {
88  assert(Result.first->second.getIndex() ==
90  // On the second pass, update with index to remapped record. The
91  // (initially misbehaved) record will now come *after* other records
92  // resolved in the first pass, with proper *back* references in the
93  // stream.
94  Result.first->second = nextTypeIndex();
95  }
96  SeenRecords.push_back(StableRecord);
97  SeenHashes.push_back(Hash);
98  }
99 
100  return Result.first->second;
101  }
102 
105 
106  template <typename T> TypeIndex writeLeafType(T &Record) {
107  ArrayRef<uint8_t> Data = SimpleSerializer.serialize(Record);
108  return insertRecordBytes(Data);
109  }
110 };
111 
112 } // end namespace codeview
113 } // end namespace llvm
114 
115 #endif // LLVM_DEBUGINFO_CODEVIEW_MERGINGTYPETABLEBUILDER_H
CVType getType(TypeIndex Index) override
Optional< TypeIndex > getFirst() override
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:191
Optional< TypeIndex > getNext(TypeIndex Prev) override
GlobalTypeTableBuilder(BumpPtrAllocator &Storage)
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
TypeIndex insertRecord(ContinuationRecordBuilder &Builder)
ArrayRef< GloballyHashedType > hashes() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
A 32-bit type reference.
Definition: TypeIndex.h:95
A globally hashed type represents a hash value that is sufficient to uniquely identify a record acros...
Definition: TypeHashing.h:77
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
ArrayRef< uint8_t > serialize(T &Record)
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:214
ArrayRef< ArrayRef< uint8_t > > records() const
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef getTypeName(TypeIndex Index) override
TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize, CreateFunc Create)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
TypeIndex insertRecordBytes(ArrayRef< uint8_t > Data)
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143