MDBuilder.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
  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. #include "llvm/IR/MDBuilder.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Metadata.h"
  17. using namespace llvm;
  18. MDString *MDBuilder::createString(StringRef Str) {
  19. return MDString::get(Context, Str);
  20. }
  21. ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
  22. return ConstantAsMetadata::get(C);
  23. }
  24. MDNode *MDBuilder::createFPMath(float Accuracy) {
  25. if (Accuracy == 0.0)
  26. return nullptr;
  27. assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
  28. auto *Op =
  29. createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
  30. return MDNode::get(Context, Op);
  31. }
  32. MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
  33. uint32_t FalseWeight) {
  34. uint32_t Weights[] = {TrueWeight, FalseWeight};
  35. return createBranchWeights(Weights);
  36. }
  37. MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
  38. assert(Weights.size() >= 2 && "Need at least two branch weights!");
  39. SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
  40. Vals[0] = createString("branch_weights");
  41. Type *Int32Ty = Type::getInt32Ty(Context);
  42. for (unsigned i = 0, e = Weights.size(); i != e; ++i)
  43. Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
  44. return MDNode::get(Context, Vals);
  45. }
  46. MDNode *MDBuilder::createFunctionEntryCount(uint64_t Count) {
  47. SmallVector<Metadata *, 2> Vals(2);
  48. Vals[0] = createString("function_entry_count");
  49. Type *Int64Ty = Type::getInt64Ty(Context);
  50. Vals[1] = createConstant(ConstantInt::get(Int64Ty, Count));
  51. return MDNode::get(Context, Vals);
  52. }
  53. MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
  54. assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
  55. Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
  56. return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
  57. }
  58. MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
  59. // If the range is everything then it is useless.
  60. if (Hi == Lo)
  61. return nullptr;
  62. // Return the range [Lo, Hi).
  63. Metadata *Range[2] = {createConstant(Lo), createConstant(Hi)};
  64. return MDNode::get(Context, Range);
  65. }
  66. MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
  67. // To ensure uniqueness the root node is self-referential.
  68. auto Dummy = MDNode::getTemporary(Context, None);
  69. SmallVector<Metadata *, 3> Args(1, Dummy.get());
  70. if (Extra)
  71. Args.push_back(Extra);
  72. if (!Name.empty())
  73. Args.push_back(createString(Name));
  74. MDNode *Root = MDNode::get(Context, Args);
  75. // At this point we have
  76. // !0 = metadata !{} <- dummy
  77. // !1 = metadata !{metadata !0} <- root
  78. // Replace the dummy operand with the root node itself and delete the dummy.
  79. Root->replaceOperandWith(0, Root);
  80. // We now have
  81. // !1 = metadata !{metadata !1} <- self-referential root
  82. return Root;
  83. }
  84. MDNode *MDBuilder::createTBAARoot(StringRef Name) {
  85. return MDNode::get(Context, createString(Name));
  86. }
  87. /// \brief Return metadata for a non-root TBAA node with the given name,
  88. /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
  89. MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
  90. bool isConstant) {
  91. if (isConstant) {
  92. Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
  93. Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)};
  94. return MDNode::get(Context, Ops);
  95. } else {
  96. Metadata *Ops[2] = {createString(Name), Parent};
  97. return MDNode::get(Context, Ops);
  98. }
  99. }
  100. MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
  101. return MDNode::get(Context, createString(Name));
  102. }
  103. MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
  104. Metadata *Ops[2] = {createString(Name), Domain};
  105. return MDNode::get(Context, Ops);
  106. }
  107. /// \brief Return metadata for a tbaa.struct node with the given
  108. /// struct field descriptions.
  109. MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
  110. SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
  111. Type *Int64 = Type::getInt64Ty(Context);
  112. for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
  113. Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
  114. Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
  115. Vals[i * 3 + 2] = Fields[i].TBAA;
  116. }
  117. return MDNode::get(Context, Vals);
  118. }
  119. /// \brief Return metadata for a TBAA struct node in the type DAG
  120. /// with the given name, a list of pairs (offset, field type in the type DAG).
  121. MDNode *MDBuilder::createTBAAStructTypeNode(
  122. StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
  123. SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
  124. Type *Int64 = Type::getInt64Ty(Context);
  125. Ops[0] = createString(Name);
  126. for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
  127. Ops[i * 2 + 1] = Fields[i].first;
  128. Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
  129. }
  130. return MDNode::get(Context, Ops);
  131. }
  132. /// \brief Return metadata for a TBAA scalar type node with the
  133. /// given name, an offset and a parent in the TBAA type DAG.
  134. MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
  135. uint64_t Offset) {
  136. ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
  137. Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)};
  138. return MDNode::get(Context, Ops);
  139. }
  140. /// \brief Return metadata for a TBAA tag node with the given
  141. /// base type, access type and offset relative to the base type.
  142. MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
  143. uint64_t Offset, bool IsConstant) {
  144. Type *Int64 = Type::getInt64Ty(Context);
  145. if (IsConstant) {
  146. Metadata *Ops[4] = {BaseType, AccessType,
  147. createConstant(ConstantInt::get(Int64, Offset)),
  148. createConstant(ConstantInt::get(Int64, 1))};
  149. return MDNode::get(Context, Ops);
  150. } else {
  151. Metadata *Ops[3] = {BaseType, AccessType,
  152. createConstant(ConstantInt::get(Int64, Offset))};
  153. return MDNode::get(Context, Ops);
  154. }
  155. }