MDBuilder.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //===---- llvm/MDBuilder.h - Builder for LLVM metadata ----------*- 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 the MDBuilder class, which is used as a convenient way to
  11. // create LLVM metadata with a consistent and simplified interface.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IR_MDBUILDER_H
  15. #define LLVM_IR_MDBUILDER_H
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include <utility>
  19. namespace llvm {
  20. class APInt;
  21. template <typename T> class ArrayRef;
  22. class LLVMContext;
  23. class Constant;
  24. class ConstantAsMetadata;
  25. class MDNode;
  26. class MDString;
  27. class MDBuilder {
  28. LLVMContext &Context;
  29. public:
  30. MDBuilder(LLVMContext &context) : Context(context) {}
  31. /// \brief Return the given string as metadata.
  32. MDString *createString(StringRef Str);
  33. /// \brief Return the given constant as metadata.
  34. ConstantAsMetadata *createConstant(Constant *C);
  35. //===------------------------------------------------------------------===//
  36. // FPMath metadata.
  37. //===------------------------------------------------------------------===//
  38. /// \brief Return metadata with the given settings. The special value 0.0
  39. /// for the Accuracy parameter indicates the default (maximal precision)
  40. /// setting.
  41. MDNode *createFPMath(float Accuracy);
  42. //===------------------------------------------------------------------===//
  43. // Prof metadata.
  44. //===------------------------------------------------------------------===//
  45. /// \brief Return metadata containing two branch weights.
  46. MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
  47. /// \brief Return metadata containing a number of branch weights.
  48. MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
  49. /// Return metadata containing the entry count for a function.
  50. MDNode *createFunctionEntryCount(uint64_t Count);
  51. //===------------------------------------------------------------------===//
  52. // Range metadata.
  53. //===------------------------------------------------------------------===//
  54. /// \brief Return metadata describing the range [Lo, Hi).
  55. MDNode *createRange(const APInt &Lo, const APInt &Hi);
  56. /// \brief Return metadata describing the range [Lo, Hi).
  57. MDNode *createRange(Constant *Lo, Constant *Hi);
  58. //===------------------------------------------------------------------===//
  59. // AA metadata.
  60. //===------------------------------------------------------------------===//
  61. protected:
  62. /// \brief Return metadata appropriate for a AA root node (scope or TBAA).
  63. /// Each returned node is distinct from all other metadata and will never
  64. /// be identified (uniqued) with anything else.
  65. MDNode *createAnonymousAARoot(StringRef Name = StringRef(),
  66. MDNode *Extra = nullptr);
  67. public:
  68. /// \brief Return metadata appropriate for a TBAA root node. Each returned
  69. /// node is distinct from all other metadata and will never be identified
  70. /// (uniqued) with anything else.
  71. MDNode *createAnonymousTBAARoot() {
  72. return createAnonymousAARoot();
  73. }
  74. /// \brief Return metadata appropriate for an alias scope domain node.
  75. /// Each returned node is distinct from all other metadata and will never
  76. /// be identified (uniqued) with anything else.
  77. MDNode *createAnonymousAliasScopeDomain(StringRef Name = StringRef()) {
  78. return createAnonymousAARoot(Name);
  79. }
  80. /// \brief Return metadata appropriate for an alias scope root node.
  81. /// Each returned node is distinct from all other metadata and will never
  82. /// be identified (uniqued) with anything else.
  83. MDNode *createAnonymousAliasScope(MDNode *Domain,
  84. StringRef Name = StringRef()) {
  85. return createAnonymousAARoot(Name, Domain);
  86. }
  87. /// \brief Return metadata appropriate for a TBAA root node with the given
  88. /// name. This may be identified (uniqued) with other roots with the same
  89. /// name.
  90. MDNode *createTBAARoot(StringRef Name);
  91. /// \brief Return metadata appropriate for an alias scope domain node with
  92. /// the given name. This may be identified (uniqued) with other roots with
  93. /// the same name.
  94. MDNode *createAliasScopeDomain(StringRef Name);
  95. /// \brief Return metadata appropriate for an alias scope node with
  96. /// the given name. This may be identified (uniqued) with other scopes with
  97. /// the same name and domain.
  98. MDNode *createAliasScope(StringRef Name, MDNode *Domain);
  99. /// \brief Return metadata for a non-root TBAA node with the given name,
  100. /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
  101. MDNode *createTBAANode(StringRef Name, MDNode *Parent,
  102. bool isConstant = false);
  103. struct TBAAStructField {
  104. uint64_t Offset;
  105. uint64_t Size;
  106. MDNode *TBAA;
  107. TBAAStructField(uint64_t Offset, uint64_t Size, MDNode *TBAA) :
  108. Offset(Offset), Size(Size), TBAA(TBAA) {}
  109. };
  110. /// \brief Return metadata for a tbaa.struct node with the given
  111. /// struct field descriptions.
  112. MDNode *createTBAAStructNode(ArrayRef<TBAAStructField> Fields);
  113. /// \brief Return metadata for a TBAA struct node in the type DAG
  114. /// with the given name, a list of pairs (offset, field type in the type DAG).
  115. MDNode *
  116. createTBAAStructTypeNode(StringRef Name,
  117. ArrayRef<std::pair<MDNode *, uint64_t>> Fields);
  118. /// \brief Return metadata for a TBAA scalar type node with the
  119. /// given name, an offset and a parent in the TBAA type DAG.
  120. MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
  121. uint64_t Offset = 0);
  122. /// \brief Return metadata for a TBAA tag node with the given
  123. /// base type, access type and offset relative to the base type.
  124. MDNode *createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
  125. uint64_t Offset, bool IsConstant = false);
  126. };
  127. } // end namespace llvm
  128. #endif