MDBuilder.h 8.8 KB

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