AccelTable.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- include/llvm/CodeGen/AccelTable.h - Accelerator Tables -----*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. /// This file contains support for writing accelerator tables.
  15. ///
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_ACCELTABLE_H
  18. #define LLVM_CODEGEN_ACCELTABLE_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringMap.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/BinaryFormat/Dwarf.h"
  24. #include "llvm/CodeGen/DIE.h"
  25. #include "llvm/CodeGen/DwarfStringPoolEntry.h"
  26. #include "llvm/MC/MCSymbol.h"
  27. #include "llvm/Support/Allocator.h"
  28. #include "llvm/Support/DJB.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/Format.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include <cstddef>
  33. #include <cstdint>
  34. #include <vector>
  35. /// \file
  36. /// The DWARF and Apple accelerator tables are an indirect hash table optimized
  37. /// for null lookup rather than access to known data. The Apple accelerator
  38. /// tables are a precursor of the newer DWARF v5 accelerator tables. Both
  39. /// formats share common design ideas.
  40. ///
  41. /// The Apple accelerator table are output into an on-disk format that looks
  42. /// like this:
  43. ///
  44. /// .------------------.
  45. /// | HEADER |
  46. /// |------------------|
  47. /// | BUCKETS |
  48. /// |------------------|
  49. /// | HASHES |
  50. /// |------------------|
  51. /// | OFFSETS |
  52. /// |------------------|
  53. /// | DATA |
  54. /// `------------------'
  55. ///
  56. /// The header contains a magic number, version, type of hash function,
  57. /// the number of buckets, total number of hashes, and room for a special struct
  58. /// of data and the length of that struct.
  59. ///
  60. /// The buckets contain an index (e.g. 6) into the hashes array. The hashes
  61. /// section contains all of the 32-bit hash values in contiguous memory, and the
  62. /// offsets contain the offset into the data area for the particular hash.
  63. ///
  64. /// For a lookup example, we could hash a function name and take it modulo the
  65. /// number of buckets giving us our bucket. From there we take the bucket value
  66. /// as an index into the hashes table and look at each successive hash as long
  67. /// as the hash value is still the same modulo result (bucket value) as earlier.
  68. /// If we have a match we look at that same entry in the offsets table and grab
  69. /// the offset in the data for our final match.
  70. ///
  71. /// The DWARF v5 accelerator table consists of zero or more name indices that
  72. /// are output into an on-disk format that looks like this:
  73. ///
  74. /// .------------------.
  75. /// | HEADER |
  76. /// |------------------|
  77. /// | CU LIST |
  78. /// |------------------|
  79. /// | LOCAL TU LIST |
  80. /// |------------------|
  81. /// | FOREIGN TU LIST |
  82. /// |------------------|
  83. /// | HASH TABLE |
  84. /// |------------------|
  85. /// | NAME TABLE |
  86. /// |------------------|
  87. /// | ABBREV TABLE |
  88. /// |------------------|
  89. /// | ENTRY POOL |
  90. /// `------------------'
  91. ///
  92. /// For the full documentation please refer to the DWARF 5 standard.
  93. ///
  94. ///
  95. /// This file defines the class template AccelTable, which is represents an
  96. /// abstract view of an Accelerator table, without any notion of an on-disk
  97. /// layout. This class is parameterized by an entry type, which should derive
  98. /// from AccelTableData. This is the type of individual entries in the table,
  99. /// and it should store the data necessary to emit them. AppleAccelTableData is
  100. /// the base class for Apple Accelerator Table entries, which have a uniform
  101. /// structure based on a sequence of Atoms. There are different sub-classes
  102. /// derived from AppleAccelTable, which differ in the set of Atoms and how they
  103. /// obtain their values.
  104. ///
  105. /// An Apple Accelerator Table can be serialized by calling emitAppleAccelTable
  106. /// function.
  107. namespace llvm {
  108. class AsmPrinter;
  109. class DwarfCompileUnit;
  110. class DwarfDebug;
  111. /// Interface which the different types of accelerator table data have to
  112. /// conform. It serves as a base class for different values of the template
  113. /// argument of the AccelTable class template.
  114. class AccelTableData {
  115. public:
  116. virtual ~AccelTableData() = default;
  117. bool operator<(const AccelTableData &Other) const {
  118. return order() < Other.order();
  119. }
  120. // Subclasses should implement:
  121. // static uint32_t hash(StringRef Name);
  122. #ifndef NDEBUG
  123. virtual void print(raw_ostream &OS) const = 0;
  124. #endif
  125. protected:
  126. virtual uint64_t order() const = 0;
  127. };
  128. /// A base class holding non-template-dependant functionality of the AccelTable
  129. /// class. Clients should not use this class directly but rather instantiate
  130. /// AccelTable with a type derived from AccelTableData.
  131. class AccelTableBase {
  132. public:
  133. using HashFn = uint32_t(StringRef);
  134. /// Represents a group of entries with identical name (and hence, hash value).
  135. struct HashData {
  136. DwarfStringPoolEntryRef Name;
  137. uint32_t HashValue;
  138. std::vector<AccelTableData *> Values;
  139. MCSymbol *Sym;
  140. HashData(DwarfStringPoolEntryRef Name, HashFn *Hash)
  141. : Name(Name), HashValue(Hash(Name.getString())) {}
  142. #ifndef NDEBUG
  143. void print(raw_ostream &OS) const;
  144. void dump() const { print(dbgs()); }
  145. #endif
  146. };
  147. using HashList = std::vector<HashData *>;
  148. using BucketList = std::vector<HashList>;
  149. protected:
  150. /// Allocator for HashData and Values.
  151. BumpPtrAllocator Allocator;
  152. using StringEntries = StringMap<HashData, BumpPtrAllocator &>;
  153. StringEntries Entries;
  154. HashFn *Hash;
  155. uint32_t BucketCount;
  156. uint32_t UniqueHashCount;
  157. HashList Hashes;
  158. BucketList Buckets;
  159. void computeBucketCount();
  160. AccelTableBase(HashFn *Hash) : Entries(Allocator), Hash(Hash) {}
  161. public:
  162. void finalize(AsmPrinter *Asm, StringRef Prefix);
  163. ArrayRef<HashList> getBuckets() const { return Buckets; }
  164. uint32_t getBucketCount() const { return BucketCount; }
  165. uint32_t getUniqueHashCount() const { return UniqueHashCount; }
  166. uint32_t getUniqueNameCount() const { return Entries.size(); }
  167. #ifndef NDEBUG
  168. void print(raw_ostream &OS) const;
  169. void dump() const { print(dbgs()); }
  170. #endif
  171. AccelTableBase(const AccelTableBase &) = delete;
  172. void operator=(const AccelTableBase &) = delete;
  173. };
  174. /// This class holds an abstract representation of an Accelerator Table,
  175. /// consisting of a sequence of buckets, each bucket containint a sequence of
  176. /// HashData entries. The class is parameterized by the type of entries it
  177. /// holds. The type template parameter also defines the hash function to use for
  178. /// hashing names.
  179. template <typename DataT> class AccelTable : public AccelTableBase {
  180. public:
  181. AccelTable() : AccelTableBase(DataT::hash) {}
  182. template <typename... Types>
  183. void addName(DwarfStringPoolEntryRef Name, Types &&... Args);
  184. };
  185. template <typename AccelTableDataT>
  186. template <typename... Types>
  187. void AccelTable<AccelTableDataT>::addName(DwarfStringPoolEntryRef Name,
  188. Types &&... Args) {
  189. assert(Buckets.empty() && "Already finalized!");
  190. // If the string is in the list already then add this die to the list
  191. // otherwise add a new one.
  192. auto Iter = Entries.try_emplace(Name.getString(), Name, Hash).first;
  193. assert(Iter->second.Name == Name);
  194. Iter->second.Values.push_back(
  195. new (Allocator) AccelTableDataT(std::forward<Types>(Args)...));
  196. }
  197. /// A base class for different implementations of Data classes for Apple
  198. /// Accelerator Tables. The columns in the table are defined by the static Atoms
  199. /// variable defined on the subclasses.
  200. class AppleAccelTableData : public AccelTableData {
  201. public:
  202. /// An Atom defines the form of the data in an Apple accelerator table.
  203. /// Conceptually it is a column in the accelerator consisting of a type and a
  204. /// specification of the form of its data.
  205. struct Atom {
  206. /// Atom Type.
  207. const uint16_t Type;
  208. /// DWARF Form.
  209. const uint16_t Form;
  210. constexpr Atom(uint16_t Type, uint16_t Form) : Type(Type), Form(Form) {}
  211. #ifndef NDEBUG
  212. void print(raw_ostream &OS) const;
  213. void dump() const { print(dbgs()); }
  214. #endif
  215. };
  216. // Subclasses should define:
  217. // static constexpr Atom Atoms[];
  218. virtual void emit(AsmPrinter *Asm) const = 0;
  219. static uint32_t hash(StringRef Buffer) { return djbHash(Buffer); }
  220. };
  221. /// The Data class implementation for DWARF v5 accelerator table. Unlike the
  222. /// Apple Data classes, this class is just a DIE wrapper, and does not know to
  223. /// serialize itself. The complete serialization logic is in the
  224. /// emitDWARF5AccelTable function.
  225. class DWARF5AccelTableData : public AccelTableData {
  226. public:
  227. static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }
  228. DWARF5AccelTableData(const DIE &Die) : Die(Die) {}
  229. #ifndef NDEBUG
  230. void print(raw_ostream &OS) const override;
  231. #endif
  232. const DIE &getDie() const { return Die; }
  233. uint64_t getDieOffset() const { return Die.getOffset(); }
  234. unsigned getDieTag() const { return Die.getTag(); }
  235. protected:
  236. const DIE &Die;
  237. uint64_t order() const override { return Die.getOffset(); }
  238. };
  239. class DWARF5AccelTableStaticData : public AccelTableData {
  240. public:
  241. static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }
  242. DWARF5AccelTableStaticData(uint64_t DieOffset, unsigned DieTag,
  243. unsigned CUIndex)
  244. : DieOffset(DieOffset), DieTag(DieTag), CUIndex(CUIndex) {}
  245. #ifndef NDEBUG
  246. void print(raw_ostream &OS) const override;
  247. #endif
  248. uint64_t getDieOffset() const { return DieOffset; }
  249. unsigned getDieTag() const { return DieTag; }
  250. unsigned getCUIndex() const { return CUIndex; }
  251. protected:
  252. uint64_t DieOffset;
  253. unsigned DieTag;
  254. unsigned CUIndex;
  255. uint64_t order() const override { return DieOffset; }
  256. };
  257. void emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
  258. StringRef Prefix, const MCSymbol *SecBegin,
  259. ArrayRef<AppleAccelTableData::Atom> Atoms);
  260. /// Emit an Apple Accelerator Table consisting of entries in the specified
  261. /// AccelTable. The DataT template parameter should be derived from
  262. /// AppleAccelTableData.
  263. template <typename DataT>
  264. void emitAppleAccelTable(AsmPrinter *Asm, AccelTable<DataT> &Contents,
  265. StringRef Prefix, const MCSymbol *SecBegin) {
  266. static_assert(std::is_convertible<DataT *, AppleAccelTableData *>::value, "");
  267. emitAppleAccelTableImpl(Asm, Contents, Prefix, SecBegin, DataT::Atoms);
  268. }
  269. void emitDWARF5AccelTable(AsmPrinter *Asm,
  270. AccelTable<DWARF5AccelTableData> &Contents,
  271. const DwarfDebug &DD,
  272. ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs);
  273. void emitDWARF5AccelTable(
  274. AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
  275. ArrayRef<MCSymbol *> CUs,
  276. llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
  277. getCUIndexForEntry);
  278. /// Accelerator table data implementation for simple Apple accelerator tables
  279. /// with just a DIE reference.
  280. class AppleAccelTableOffsetData : public AppleAccelTableData {
  281. public:
  282. AppleAccelTableOffsetData(const DIE &D) : Die(D) {}
  283. void emit(AsmPrinter *Asm) const override;
  284. static constexpr Atom Atoms[] = {
  285. Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4)};
  286. #ifndef NDEBUG
  287. void print(raw_ostream &OS) const override;
  288. #endif
  289. protected:
  290. uint64_t order() const override { return Die.getOffset(); }
  291. const DIE &Die;
  292. };
  293. /// Accelerator table data implementation for Apple type accelerator tables.
  294. class AppleAccelTableTypeData : public AppleAccelTableOffsetData {
  295. public:
  296. AppleAccelTableTypeData(const DIE &D) : AppleAccelTableOffsetData(D) {}
  297. void emit(AsmPrinter *Asm) const override;
  298. static constexpr Atom Atoms[] = {
  299. Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
  300. Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
  301. Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
  302. #ifndef NDEBUG
  303. void print(raw_ostream &OS) const override;
  304. #endif
  305. };
  306. /// Accelerator table data implementation for simple Apple accelerator tables
  307. /// with a DIE offset but no actual DIE pointer.
  308. class AppleAccelTableStaticOffsetData : public AppleAccelTableData {
  309. public:
  310. AppleAccelTableStaticOffsetData(uint32_t Offset) : Offset(Offset) {}
  311. void emit(AsmPrinter *Asm) const override;
  312. static constexpr Atom Atoms[] = {
  313. Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4)};
  314. #ifndef NDEBUG
  315. void print(raw_ostream &OS) const override;
  316. #endif
  317. protected:
  318. uint64_t order() const override { return Offset; }
  319. uint32_t Offset;
  320. };
  321. /// Accelerator table data implementation for type accelerator tables with
  322. /// a DIE offset but no actual DIE pointer.
  323. class AppleAccelTableStaticTypeData : public AppleAccelTableStaticOffsetData {
  324. public:
  325. AppleAccelTableStaticTypeData(uint32_t Offset, uint16_t Tag,
  326. bool ObjCClassIsImplementation,
  327. uint32_t QualifiedNameHash)
  328. : AppleAccelTableStaticOffsetData(Offset),
  329. QualifiedNameHash(QualifiedNameHash), Tag(Tag),
  330. ObjCClassIsImplementation(ObjCClassIsImplementation) {}
  331. void emit(AsmPrinter *Asm) const override;
  332. static constexpr Atom Atoms[] = {
  333. Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
  334. Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
  335. Atom(5, dwarf::DW_FORM_data1), Atom(6, dwarf::DW_FORM_data4)};
  336. #ifndef NDEBUG
  337. void print(raw_ostream &OS) const override;
  338. #endif
  339. protected:
  340. uint64_t order() const override { return Offset; }
  341. uint32_t QualifiedNameHash;
  342. uint16_t Tag;
  343. bool ObjCClassIsImplementation;
  344. };
  345. } // end namespace llvm
  346. #endif // LLVM_CODEGEN_ACCELTABLE_H
  347. #ifdef __GNUC__
  348. #pragma GCC diagnostic pop
  349. #endif