MDBuilder.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- 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 the MDBuilder class, which is used as a convenient way to
  15. // create LLVM metadata with a consistent and simplified interface.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_IR_MDBUILDER_H
  19. #define LLVM_IR_MDBUILDER_H
  20. #include "llvm/ADT/DenseSet.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/IR/GlobalValue.h"
  24. #include "llvm/Support/DataTypes.h"
  25. #include <utility>
  26. namespace llvm {
  27. class APInt;
  28. template <typename T> class ArrayRef;
  29. class LLVMContext;
  30. class Constant;
  31. class ConstantAsMetadata;
  32. class Function;
  33. class MDNode;
  34. class MDString;
  35. class Metadata;
  36. class MDBuilder {
  37. LLVMContext &Context;
  38. public:
  39. MDBuilder(LLVMContext &context) : Context(context) {}
  40. /// Return the given string as metadata.
  41. MDString *createString(StringRef Str);
  42. /// Return the given constant as metadata.
  43. ConstantAsMetadata *createConstant(Constant *C);
  44. //===------------------------------------------------------------------===//
  45. // FPMath metadata.
  46. //===------------------------------------------------------------------===//
  47. /// Return metadata with the given settings. The special value 0.0
  48. /// for the Accuracy parameter indicates the default (maximal precision)
  49. /// setting.
  50. MDNode *createFPMath(float Accuracy);
  51. //===------------------------------------------------------------------===//
  52. // Prof metadata.
  53. //===------------------------------------------------------------------===//
  54. /// Return metadata containing two branch weights.
  55. MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
  56. /// Return metadata containing a number of branch weights.
  57. MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
  58. /// Return metadata specifying that a branch or switch is unpredictable.
  59. MDNode *createUnpredictable();
  60. /// Return metadata containing the entry \p Count for a function, a boolean
  61. /// \Synthetic indicating whether the counts were synthetized, and the
  62. /// GUIDs stored in \p Imports that need to be imported for sample PGO, to
  63. /// enable the same inlines as the profiled optimized binary
  64. MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic,
  65. const DenseSet<GlobalValue::GUID> *Imports);
  66. /// Return metadata containing the section prefix for a function.
  67. MDNode *createFunctionSectionPrefix(StringRef Prefix);
  68. /// Return metadata containing the pseudo probe descriptor for a function.
  69. MDNode *createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, Function *F);
  70. /// Return metadata containing llvm statistics.
  71. MDNode *
  72. createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStatsVec);
  73. //===------------------------------------------------------------------===//
  74. // Range metadata.
  75. //===------------------------------------------------------------------===//
  76. /// Return metadata describing the range [Lo, Hi).
  77. MDNode *createRange(const APInt &Lo, const APInt &Hi);
  78. /// Return metadata describing the range [Lo, Hi).
  79. MDNode *createRange(Constant *Lo, Constant *Hi);
  80. //===------------------------------------------------------------------===//
  81. // Callees metadata.
  82. //===------------------------------------------------------------------===//
  83. /// Return metadata indicating the possible callees of indirect
  84. /// calls.
  85. MDNode *createCallees(ArrayRef<Function *> Callees);
  86. //===------------------------------------------------------------------===//
  87. // Callback metadata.
  88. //===------------------------------------------------------------------===//
  89. /// Return metadata describing a callback (see llvm::AbstractCallSite).
  90. MDNode *createCallbackEncoding(unsigned CalleeArgNo, ArrayRef<int> Arguments,
  91. bool VarArgsArePassed);
  92. /// Merge the new callback encoding \p NewCB into \p ExistingCallbacks.
  93. MDNode *mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB);
  94. /// Return metadata feeding to the CodeGen about how to generate a function
  95. /// prologue for the "function" santizier.
  96. MDNode *createRTTIPointerPrologue(Constant *PrologueSig, Constant *RTTI);
  97. //===------------------------------------------------------------------===//
  98. // PC sections metadata.
  99. //===------------------------------------------------------------------===//
  100. /// A pair of PC section name with auxilliary constant data.
  101. using PCSection = std::pair<StringRef, SmallVector<Constant *>>;
  102. /// Return metadata for PC sections.
  103. MDNode *createPCSections(ArrayRef<PCSection> Sections);
  104. //===------------------------------------------------------------------===//
  105. // AA metadata.
  106. //===------------------------------------------------------------------===//
  107. protected:
  108. /// Return metadata appropriate for a AA root node (scope or TBAA).
  109. /// Each returned node is distinct from all other metadata and will never
  110. /// be identified (uniqued) with anything else.
  111. MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
  112. MDNode *Extra = nullptr);
  113. public:
  114. /// Return metadata appropriate for a TBAA root node. Each returned
  115. /// node is distinct from all other metadata and will never be identified
  116. /// (uniqued) with anything else.
  117. MDNode *createAnonymousTBAARoot() {
  118. return createAnonymousAARoot();
  119. }
  120. /// Return metadata appropriate for an alias scope domain node.
  121. /// Each returned node is distinct from all other metadata and will never
  122. /// be identified (uniqued) with anything else.
  123. MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
  124. return createAnonymousAARoot(Name);
  125. }
  126. /// Return metadata appropriate for an alias scope root node.
  127. /// Each returned node is distinct from all other metadata and will never
  128. /// be identified (uniqued) with anything else.
  129. MDNode *createAnonymousAliasScope(MDNode *Domain,
  130. StringRef Name = StringRef()) {
  131. return createAnonymousAARoot(Name, Domain);
  132. }
  133. /// Return metadata appropriate for a TBAA root node with the given
  134. /// name. This may be identified (uniqued) with other roots with the same
  135. /// name.
  136. MDNode *createTBAARoot(StringRef Name);
  137. /// Return metadata appropriate for an alias scope domain node with
  138. /// the given name. This may be identified (uniqued) with other roots with
  139. /// the same name.
  140. MDNode *createAliasScopeDomain(StringRef Name);
  141. /// Return metadata appropriate for an alias scope node with
  142. /// the given name. This may be identified (uniqued) with other scopes with
  143. /// the same name and domain.
  144. MDNode *createAliasScope(StringRef Name, MDNode *Domain);
  145. /// Return metadata for a non-root TBAA node with the given name,
  146. /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
  147. MDNode *createTBAANode(StringRef Name, MDNode *Parent,
  148. bool isConstant = false);
  149. struct TBAAStructField {
  150. uint64_t Offset;
  151. uint64_t Size;
  152. MDNode *Type;
  153. TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *Type) :
  154. Offset(Offset), Size(Size), Type(Type) {}
  155. };
  156. /// Return metadata for a tbaa.struct node with the given
  157. /// struct field descriptions.
  158. MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
  159. /// Return metadata for a TBAA struct node in the type DAG
  160. /// with the given name, a list of pairs (offset, field type in the type DAG).
  161. MDNode *
  162. createTBAAStructTypeNode(StringRef Name,
  163. ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
  164. /// Return metadata for a TBAA scalar type node with the
  165. /// given name, an offset and a parent in the TBAA type DAG.
  166. MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
  167. uint64_t Offset = 0);
  168. /// Return metadata for a TBAA tag node with the given
  169. /// base type, access type and offset relative to the base type.
  170. MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
  171. uint64_t Offset, bool IsConstant = false);
  172. /// Return metadata for a TBAA type node in the TBAA type DAG with the
  173. /// given parent type, size in bytes, type identifier and a list of fields.
  174. MDNode *createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id,
  175. ArrayRef<TBAAStructField> Fields =
  176. ArrayRef<TBAAStructField>());
  177. /// Return metadata for a TBAA access tag with the given base type,
  178. /// final access type, offset of the access relative to the base type, size of
  179. /// the access and flag indicating whether the accessed object can be
  180. /// considered immutable for the purposes of the TBAA analysis.
  181. MDNode *createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
  182. uint64_t Offset, uint64_t Size,
  183. bool IsImmutable = false);
  184. /// Return mutable version of the given mutable or immutable TBAA
  185. /// access tag.
  186. MDNode *createMutableTBAAAccessTag(MDNode *Tag);
  187. /// Return metadata containing an irreducible loop header weight.
  188. MDNode *createIrrLoopHeaderWeight(uint64_t Weight);
  189. };
  190. } // end namespace llvm
  191. #endif
  192. #ifdef __GNUC__
  193. #pragma GCC diagnostic pop
  194. #endif