1 //===- TypeMerger.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 LLD_COFF_TYPEMERGER_H
10 #define LLD_COFF_TYPEMERGER_H
11 
12 #include "Config.h"
13 #include "DebugTypes.h"
14 #include "lld/Common/Timer.h"
15 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
16 #include "llvm/DebugInfo/CodeView/TypeHashing.h"
17 #include "llvm/Support/Allocator.h"
18 #include <atomic>
19 
20 namespace lld::coff {
21 
22 using llvm::codeview::GloballyHashedType;
23 using llvm::codeview::TypeIndex;
24 
25 struct GHashState;
26 
27 class TypeMerger {
28 public:
29   TypeMerger(COFFLinkerContext &ctx, llvm::BumpPtrAllocator &alloc);
30 
31   ~TypeMerger();
32 
33   /// Get the type table or the global type table if /DEBUG:GHASH is enabled.
getTypeTable()34   inline llvm::codeview::TypeCollection &getTypeTable() {
35     assert(!ctx.config.debugGHashes);
36     return typeTable;
37   }
38 
39   /// Get the ID table or the global ID table if /DEBUG:GHASH is enabled.
getIDTable()40   inline llvm::codeview::TypeCollection &getIDTable() {
41     assert(!ctx.config.debugGHashes);
42     return idTable;
43   }
44 
45   /// Use global hashes to eliminate duplicate types and identify unique type
46   /// indices in each TpiSource.
47   void mergeTypesWithGHash();
48 
49   /// Map from PDB function id type indexes to PDB function type indexes.
50   /// Populated after mergeTypesWithGHash.
51   llvm::DenseMap<TypeIndex, TypeIndex> funcIdToType;
52 
53   /// Type records that will go into the PDB TPI stream.
54   llvm::codeview::MergingTypeTableBuilder typeTable;
55 
56   /// Item records that will go into the PDB IPI stream.
57   llvm::codeview::MergingTypeTableBuilder idTable;
58 
59   // When showSummary is enabled, these are histograms of TPI and IPI records
60   // keyed by type index.
61   SmallVector<uint32_t, 0> tpiCounts;
62   SmallVector<uint32_t, 0> ipiCounts;
63 
64   /// Dependency type sources, such as type servers or PCH object files. These
65   /// must be processed before objects that rely on them. Set by
66   /// sortDependencies.
67   ArrayRef<TpiSource *> dependencySources;
68 
69   /// Object file sources. These must be processed after dependencySources.
70   ArrayRef<TpiSource *> objectSources;
71 
72   /// Sorts the dependencies and reassigns TpiSource indices.
73   void sortDependencies();
74 
75 private:
76   void clearGHashes();
77 
78   COFFLinkerContext &ctx;
79 };
80 
81 } // namespace lld::coff
82 
83 #endif
84