LowerBitSets.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //===- LowerBitSets.h - Bitset lowering pass --------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines parts of the bitset lowering pass implementation that may
  11. // be usefully unit tested.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TRANSFORMS_IPO_LOWERBITSETS_H
  15. #define LLVM_TRANSFORMS_IPO_LOWERBITSETS_H
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include <stdint.h>
  19. #include <limits>
  20. #include <set>
  21. #include <vector>
  22. namespace llvm {
  23. class DataLayout;
  24. class GlobalVariable;
  25. class Value;
  26. struct BitSetInfo {
  27. // The indices of the set bits in the bitset.
  28. std::set<uint64_t> Bits;
  29. // The byte offset into the combined global represented by the bitset.
  30. uint64_t ByteOffset;
  31. // The size of the bitset in bits.
  32. uint64_t BitSize;
  33. // Log2 alignment of the bit set relative to the combined global.
  34. // For example, a log2 alignment of 3 means that bits in the bitset
  35. // represent addresses 8 bytes apart.
  36. unsigned AlignLog2;
  37. bool isSingleOffset() const {
  38. return Bits.size() == 1;
  39. }
  40. bool isAllOnes() const {
  41. return Bits.size() == BitSize;
  42. }
  43. bool containsGlobalOffset(uint64_t Offset) const;
  44. bool containsValue(const DataLayout &DL,
  45. const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout,
  46. Value *V, uint64_t COffset = 0) const;
  47. };
  48. struct BitSetBuilder {
  49. SmallVector<uint64_t, 16> Offsets;
  50. uint64_t Min, Max;
  51. BitSetBuilder() : Min(std::numeric_limits<uint64_t>::max()), Max(0) {}
  52. void addOffset(uint64_t Offset) {
  53. if (Min > Offset)
  54. Min = Offset;
  55. if (Max < Offset)
  56. Max = Offset;
  57. Offsets.push_back(Offset);
  58. }
  59. BitSetInfo build();
  60. };
  61. /// This class implements a layout algorithm for globals referenced by bit sets
  62. /// that tries to keep members of small bit sets together. This can
  63. /// significantly reduce bit set sizes in many cases.
  64. ///
  65. /// It works by assembling fragments of layout from sets of referenced globals.
  66. /// Each set of referenced globals causes the algorithm to create a new
  67. /// fragment, which is assembled by appending each referenced global in the set
  68. /// into the fragment. If a referenced global has already been referenced by an
  69. /// fragment created earlier, we instead delete that fragment and append its
  70. /// contents into the fragment we are assembling.
  71. ///
  72. /// By starting with the smallest fragments, we minimize the size of the
  73. /// fragments that are copied into larger fragments. This is most intuitively
  74. /// thought about when considering the case where the globals are virtual tables
  75. /// and the bit sets represent their derived classes: in a single inheritance
  76. /// hierarchy, the optimum layout would involve a depth-first search of the
  77. /// class hierarchy (and in fact the computed layout ends up looking a lot like
  78. /// a DFS), but a naive DFS would not work well in the presence of multiple
  79. /// inheritance. This aspect of the algorithm ends up fitting smaller
  80. /// hierarchies inside larger ones where that would be beneficial.
  81. ///
  82. /// For example, consider this class hierarchy:
  83. ///
  84. /// A B
  85. /// \ / | \
  86. /// C D E
  87. ///
  88. /// We have five bit sets: bsA (A, C), bsB (B, C, D, E), bsC (C), bsD (D) and
  89. /// bsE (E). If we laid out our objects by DFS traversing B followed by A, our
  90. /// layout would be {B, C, D, E, A}. This is optimal for bsB as it needs to
  91. /// cover the only 4 objects in its hierarchy, but not for bsA as it needs to
  92. /// cover 5 objects, i.e. the entire layout. Our algorithm proceeds as follows:
  93. ///
  94. /// Add bsC, fragments {{C}}
  95. /// Add bsD, fragments {{C}, {D}}
  96. /// Add bsE, fragments {{C}, {D}, {E}}
  97. /// Add bsA, fragments {{A, C}, {D}, {E}}
  98. /// Add bsB, fragments {{B, A, C, D, E}}
  99. ///
  100. /// This layout is optimal for bsA, as it now only needs to cover two (i.e. 3
  101. /// fewer) objects, at the cost of bsB needing to cover 1 more object.
  102. ///
  103. /// The bit set lowering pass assigns an object index to each object that needs
  104. /// to be laid out, and calls addFragment for each bit set passing the object
  105. /// indices of its referenced globals. It then assembles a layout from the
  106. /// computed layout in the Fragments field.
  107. struct GlobalLayoutBuilder {
  108. /// The computed layout. Each element of this vector contains a fragment of
  109. /// layout (which may be empty) consisting of object indices.
  110. std::vector<std::vector<uint64_t>> Fragments;
  111. /// Mapping from object index to fragment index.
  112. std::vector<uint64_t> FragmentMap;
  113. GlobalLayoutBuilder(uint64_t NumObjects)
  114. : Fragments(1), FragmentMap(NumObjects) {}
  115. /// Add F to the layout while trying to keep its indices contiguous.
  116. /// If a previously seen fragment uses any of F's indices, that
  117. /// fragment will be laid out inside F.
  118. void addFragment(const std::set<uint64_t> &F);
  119. };
  120. /// This class is used to build a byte array containing overlapping bit sets. By
  121. /// loading from indexed offsets into the byte array and applying a mask, a
  122. /// program can test bits from the bit set with a relatively short instruction
  123. /// sequence. For example, suppose we have 15 bit sets to lay out:
  124. ///
  125. /// A (16 bits), B (15 bits), C (14 bits), D (13 bits), E (12 bits),
  126. /// F (11 bits), G (10 bits), H (9 bits), I (7 bits), J (6 bits), K (5 bits),
  127. /// L (4 bits), M (3 bits), N (2 bits), O (1 bit)
  128. ///
  129. /// These bits can be laid out in a 16-byte array like this:
  130. ///
  131. /// Byte Offset
  132. /// 0123456789ABCDEF
  133. /// Bit
  134. /// 7 HHHHHHHHHIIIIIII
  135. /// 6 GGGGGGGGGGJJJJJJ
  136. /// 5 FFFFFFFFFFFKKKKK
  137. /// 4 EEEEEEEEEEEELLLL
  138. /// 3 DDDDDDDDDDDDDMMM
  139. /// 2 CCCCCCCCCCCCCCNN
  140. /// 1 BBBBBBBBBBBBBBBO
  141. /// 0 AAAAAAAAAAAAAAAA
  142. ///
  143. /// For example, to test bit X of A, we evaluate ((bits[X] & 1) != 0), or to
  144. /// test bit X of I, we evaluate ((bits[9 + X] & 0x80) != 0). This can be done
  145. /// in 1-2 machine instructions on x86, or 4-6 instructions on ARM.
  146. ///
  147. /// This is a byte array, rather than (say) a 2-byte array or a 4-byte array,
  148. /// because for one thing it gives us better packing (the more bins there are,
  149. /// the less evenly they will be filled), and for another, the instruction
  150. /// sequences can be slightly shorter, both on x86 and ARM.
  151. struct ByteArrayBuilder {
  152. /// The byte array built so far.
  153. std::vector<uint8_t> Bytes;
  154. enum { BitsPerByte = 8 };
  155. /// The number of bytes allocated so far for each of the bits.
  156. uint64_t BitAllocs[BitsPerByte];
  157. ByteArrayBuilder() {
  158. memset(BitAllocs, 0, sizeof(BitAllocs));
  159. }
  160. /// Allocate BitSize bits in the byte array where Bits contains the bits to
  161. /// set. AllocByteOffset is set to the offset within the byte array and
  162. /// AllocMask is set to the bitmask for those bits. This uses the LPT (Longest
  163. /// Processing Time) multiprocessor scheduling algorithm to lay out the bits
  164. /// efficiently; the pass allocates bit sets in decreasing size order.
  165. void allocate(const std::set<uint64_t> &Bits, uint64_t BitSize,
  166. uint64_t &AllocByteOffset, uint8_t &AllocMask);
  167. };
  168. } // namespace llvm
  169. #endif