WholeProgramDevirt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- WholeProgramDevirt.h - Whole-program devirt pass ---------*- 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. //
  14. // This file defines parts of the whole-program devirtualization pass
  15. // implementation that may be usefully unit tested.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H
  19. #define LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/PassManager.h"
  22. #include "llvm/Transforms/IPO/FunctionImport.h"
  23. #include <cassert>
  24. #include <cstdint>
  25. #include <set>
  26. #include <utility>
  27. #include <vector>
  28. namespace llvm {
  29. template <typename T> class ArrayRef;
  30. template <typename T> class MutableArrayRef;
  31. class Function;
  32. class GlobalVariable;
  33. class ModuleSummaryIndex;
  34. struct ValueInfo;
  35. namespace wholeprogramdevirt {
  36. // A bit vector that keeps track of which bits are used. We use this to
  37. // pack constant values compactly before and after each virtual table.
  38. struct AccumBitVector {
  39. std::vector<uint8_t> Bytes;
  40. // Bits in BytesUsed[I] are 1 if matching bit in Bytes[I] is used, 0 if not.
  41. std::vector<uint8_t> BytesUsed;
  42. std::pair<uint8_t *, uint8_t *> getPtrToData(uint64_t Pos, uint8_t Size) {
  43. if (Bytes.size() < Pos + Size) {
  44. Bytes.resize(Pos + Size);
  45. BytesUsed.resize(Pos + Size);
  46. }
  47. return std::make_pair(Bytes.data() + Pos, BytesUsed.data() + Pos);
  48. }
  49. // Set little-endian value Val with size Size at bit position Pos,
  50. // and mark bytes as used.
  51. void setLE(uint64_t Pos, uint64_t Val, uint8_t Size) {
  52. assert(Pos % 8 == 0);
  53. auto DataUsed = getPtrToData(Pos / 8, Size);
  54. for (unsigned I = 0; I != Size; ++I) {
  55. DataUsed.first[I] = Val >> (I * 8);
  56. assert(!DataUsed.second[I]);
  57. DataUsed.second[I] = 0xff;
  58. }
  59. }
  60. // Set big-endian value Val with size Size at bit position Pos,
  61. // and mark bytes as used.
  62. void setBE(uint64_t Pos, uint64_t Val, uint8_t Size) {
  63. assert(Pos % 8 == 0);
  64. auto DataUsed = getPtrToData(Pos / 8, Size);
  65. for (unsigned I = 0; I != Size; ++I) {
  66. DataUsed.first[Size - I - 1] = Val >> (I * 8);
  67. assert(!DataUsed.second[Size - I - 1]);
  68. DataUsed.second[Size - I - 1] = 0xff;
  69. }
  70. }
  71. // Set bit at bit position Pos to b and mark bit as used.
  72. void setBit(uint64_t Pos, bool b) {
  73. auto DataUsed = getPtrToData(Pos / 8, 1);
  74. if (b)
  75. *DataUsed.first |= 1 << (Pos % 8);
  76. assert(!(*DataUsed.second & (1 << Pos % 8)));
  77. *DataUsed.second |= 1 << (Pos % 8);
  78. }
  79. };
  80. // The bits that will be stored before and after a particular vtable.
  81. struct VTableBits {
  82. // The vtable global.
  83. GlobalVariable *GV;
  84. // Cache of the vtable's size in bytes.
  85. uint64_t ObjectSize = 0;
  86. // The bit vector that will be laid out before the vtable. Note that these
  87. // bytes are stored in reverse order until the globals are rebuilt. This means
  88. // that any values in the array must be stored using the opposite endianness
  89. // from the target.
  90. AccumBitVector Before;
  91. // The bit vector that will be laid out after the vtable.
  92. AccumBitVector After;
  93. };
  94. // Information about a member of a particular type identifier.
  95. struct TypeMemberInfo {
  96. // The VTableBits for the vtable.
  97. VTableBits *Bits;
  98. // The offset in bytes from the start of the vtable (i.e. the address point).
  99. uint64_t Offset;
  100. bool operator<(const TypeMemberInfo &other) const {
  101. return Bits < other.Bits || (Bits == other.Bits && Offset < other.Offset);
  102. }
  103. };
  104. // A virtual call target, i.e. an entry in a particular vtable.
  105. struct VirtualCallTarget {
  106. VirtualCallTarget(Function *Fn, const TypeMemberInfo *TM);
  107. // For testing only.
  108. VirtualCallTarget(const TypeMemberInfo *TM, bool IsBigEndian)
  109. : Fn(nullptr), TM(TM), IsBigEndian(IsBigEndian), WasDevirt(false) {}
  110. // The function stored in the vtable.
  111. Function *Fn;
  112. // A pointer to the type identifier member through which the pointer to Fn is
  113. // accessed.
  114. const TypeMemberInfo *TM;
  115. // When doing virtual constant propagation, this stores the return value for
  116. // the function when passed the currently considered argument list.
  117. uint64_t RetVal;
  118. // Whether the target is big endian.
  119. bool IsBigEndian;
  120. // Whether at least one call site to the target was devirtualized.
  121. bool WasDevirt;
  122. // The minimum byte offset before the address point. This covers the bytes in
  123. // the vtable object before the address point (e.g. RTTI, access-to-top,
  124. // vtables for other base classes) and is equal to the offset from the start
  125. // of the vtable object to the address point.
  126. uint64_t minBeforeBytes() const { return TM->Offset; }
  127. // The minimum byte offset after the address point. This covers the bytes in
  128. // the vtable object after the address point (e.g. the vtable for the current
  129. // class and any later base classes) and is equal to the size of the vtable
  130. // object minus the offset from the start of the vtable object to the address
  131. // point.
  132. uint64_t minAfterBytes() const { return TM->Bits->ObjectSize - TM->Offset; }
  133. // The number of bytes allocated (for the vtable plus the byte array) before
  134. // the address point.
  135. uint64_t allocatedBeforeBytes() const {
  136. return minBeforeBytes() + TM->Bits->Before.Bytes.size();
  137. }
  138. // The number of bytes allocated (for the vtable plus the byte array) after
  139. // the address point.
  140. uint64_t allocatedAfterBytes() const {
  141. return minAfterBytes() + TM->Bits->After.Bytes.size();
  142. }
  143. // Set the bit at position Pos before the address point to RetVal.
  144. void setBeforeBit(uint64_t Pos) {
  145. assert(Pos >= 8 * minBeforeBytes());
  146. TM->Bits->Before.setBit(Pos - 8 * minBeforeBytes(), RetVal);
  147. }
  148. // Set the bit at position Pos after the address point to RetVal.
  149. void setAfterBit(uint64_t Pos) {
  150. assert(Pos >= 8 * minAfterBytes());
  151. TM->Bits->After.setBit(Pos - 8 * minAfterBytes(), RetVal);
  152. }
  153. // Set the bytes at position Pos before the address point to RetVal.
  154. // Because the bytes in Before are stored in reverse order, we use the
  155. // opposite endianness to the target.
  156. void setBeforeBytes(uint64_t Pos, uint8_t Size) {
  157. assert(Pos >= 8 * minBeforeBytes());
  158. if (IsBigEndian)
  159. TM->Bits->Before.setLE(Pos - 8 * minBeforeBytes(), RetVal, Size);
  160. else
  161. TM->Bits->Before.setBE(Pos - 8 * minBeforeBytes(), RetVal, Size);
  162. }
  163. // Set the bytes at position Pos after the address point to RetVal.
  164. void setAfterBytes(uint64_t Pos, uint8_t Size) {
  165. assert(Pos >= 8 * minAfterBytes());
  166. if (IsBigEndian)
  167. TM->Bits->After.setBE(Pos - 8 * minAfterBytes(), RetVal, Size);
  168. else
  169. TM->Bits->After.setLE(Pos - 8 * minAfterBytes(), RetVal, Size);
  170. }
  171. };
  172. // Find the minimum offset that we may store a value of size Size bits at. If
  173. // IsAfter is set, look for an offset before the object, otherwise look for an
  174. // offset after the object.
  175. uint64_t findLowestOffset(ArrayRef<VirtualCallTarget> Targets, bool IsAfter,
  176. uint64_t Size);
  177. // Set the stored value in each of Targets to VirtualCallTarget::RetVal at the
  178. // given allocation offset before the vtable address. Stores the computed
  179. // byte/bit offset to OffsetByte/OffsetBit.
  180. void setBeforeReturnValues(MutableArrayRef<VirtualCallTarget> Targets,
  181. uint64_t AllocBefore, unsigned BitWidth,
  182. int64_t &OffsetByte, uint64_t &OffsetBit);
  183. // Set the stored value in each of Targets to VirtualCallTarget::RetVal at the
  184. // given allocation offset after the vtable address. Stores the computed
  185. // byte/bit offset to OffsetByte/OffsetBit.
  186. void setAfterReturnValues(MutableArrayRef<VirtualCallTarget> Targets,
  187. uint64_t AllocAfter, unsigned BitWidth,
  188. int64_t &OffsetByte, uint64_t &OffsetBit);
  189. } // end namespace wholeprogramdevirt
  190. struct WholeProgramDevirtPass : public PassInfoMixin<WholeProgramDevirtPass> {
  191. ModuleSummaryIndex *ExportSummary;
  192. const ModuleSummaryIndex *ImportSummary;
  193. bool UseCommandLine = false;
  194. WholeProgramDevirtPass()
  195. : ExportSummary(nullptr), ImportSummary(nullptr), UseCommandLine(true) {}
  196. WholeProgramDevirtPass(ModuleSummaryIndex *ExportSummary,
  197. const ModuleSummaryIndex *ImportSummary)
  198. : ExportSummary(ExportSummary), ImportSummary(ImportSummary) {
  199. assert(!(ExportSummary && ImportSummary));
  200. }
  201. PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
  202. };
  203. struct VTableSlotSummary {
  204. StringRef TypeID;
  205. uint64_t ByteOffset;
  206. };
  207. void updateVCallVisibilityInModule(
  208. Module &M, bool WholeProgramVisibilityEnabledInLTO,
  209. const DenseSet<GlobalValue::GUID> &DynamicExportSymbols);
  210. void updateVCallVisibilityInIndex(
  211. ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO,
  212. const DenseSet<GlobalValue::GUID> &DynamicExportSymbols);
  213. /// Perform index-based whole program devirtualization on the \p Summary
  214. /// index. Any devirtualized targets used by a type test in another module
  215. /// are added to the \p ExportedGUIDs set. For any local devirtualized targets
  216. /// only used within the defining module, the information necessary for
  217. /// locating the corresponding WPD resolution is recorded for the ValueInfo
  218. /// in case it is exported by cross module importing (in which case the
  219. /// devirtualized target name will need adjustment).
  220. void runWholeProgramDevirtOnIndex(
  221. ModuleSummaryIndex &Summary, std::set<GlobalValue::GUID> &ExportedGUIDs,
  222. std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap);
  223. /// Call after cross-module importing to update the recorded single impl
  224. /// devirt target names for any locals that were exported.
  225. void updateIndexWPDForExports(
  226. ModuleSummaryIndex &Summary,
  227. function_ref<bool(StringRef, ValueInfo)> isExported,
  228. std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap);
  229. } // end namespace llvm
  230. #endif // LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H
  231. #ifdef __GNUC__
  232. #pragma GCC diagnostic pop
  233. #endif