MDBuilder.h 8.6 KB

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