AccelTable.h 14 KB

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