MDBuilder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
  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. #include "llvm/IR/MDBuilder.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/IR/Function.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. return createBranchWeights({TrueWeight, FalseWeight});
  35. }
  36. MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
  37. assert(Weights.size() >= 1 && "Need at least one branch weights!");
  38. SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
  39. Vals[0] = createString("branch_weights");
  40. Type *Int32Ty = Type::getInt32Ty(Context);
  41. for (unsigned i = 0, e = Weights.size(); i != e; ++i)
  42. Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
  43. return MDNode::get(Context, Vals);
  44. }
  45. MDNode *MDBuilder::createUnpredictable() {
  46. return MDNode::get(Context, None);
  47. }
  48. MDNode *MDBuilder::createFunctionEntryCount(
  49. uint64_t Count, bool Synthetic,
  50. const DenseSet<GlobalValue::GUID> *Imports) {
  51. Type *Int64Ty = Type::getInt64Ty(Context);
  52. SmallVector<Metadata *, 8> Ops;
  53. if (Synthetic)
  54. Ops.push_back(createString("synthetic_function_entry_count"));
  55. else
  56. Ops.push_back(createString("function_entry_count"));
  57. Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
  58. if (Imports) {
  59. SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
  60. llvm::sort(OrderID);
  61. for (auto ID : OrderID)
  62. Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
  63. }
  64. return MDNode::get(Context, Ops);
  65. }
  66. MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
  67. return MDNode::get(Context,
  68. {createString("function_section_prefix"),
  69. createString(Prefix)});
  70. }
  71. MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
  72. assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
  73. Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
  74. return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
  75. }
  76. MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
  77. // If the range is everything then it is useless.
  78. if (Hi == Lo)
  79. return nullptr;
  80. // Return the range [Lo, Hi).
  81. return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
  82. }
  83. MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
  84. SmallVector<Metadata *, 4> Ops;
  85. for (Function *F : Callees)
  86. Ops.push_back(createConstant(F));
  87. return MDNode::get(Context, Ops);
  88. }
  89. MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,
  90. ArrayRef<int> Arguments,
  91. bool VarArgArePassed) {
  92. SmallVector<Metadata *, 4> Ops;
  93. Type *Int64 = Type::getInt64Ty(Context);
  94. Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
  95. for (int ArgNo : Arguments)
  96. Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
  97. Type *Int1 = Type::getInt1Ty(Context);
  98. Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
  99. return MDNode::get(Context, Ops);
  100. }
  101. MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
  102. MDNode *NewCB) {
  103. if (!ExistingCallbacks)
  104. return MDNode::get(Context, {NewCB});
  105. auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
  106. uint64_t NewCBCalleeIdx =
  107. cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
  108. (void)NewCBCalleeIdx;
  109. SmallVector<Metadata *, 4> Ops;
  110. unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
  111. Ops.resize(NumExistingOps + 1);
  112. for (unsigned u = 0; u < NumExistingOps; u++) {
  113. Ops[u] = ExistingCallbacks->getOperand(u);
  114. auto *OldCBCalleeIdxAsCM = cast<ConstantAsMetadata>(Ops[u]);
  115. uint64_t OldCBCalleeIdx =
  116. cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
  117. (void)OldCBCalleeIdx;
  118. assert(NewCBCalleeIdx != OldCBCalleeIdx &&
  119. "Cannot map a callback callee index twice!");
  120. }
  121. Ops[NumExistingOps] = NewCB;
  122. return MDNode::get(Context, Ops);
  123. }
  124. MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
  125. SmallVector<Metadata *, 3> Args(1, nullptr);
  126. if (Extra)
  127. Args.push_back(Extra);
  128. if (!Name.empty())
  129. Args.push_back(createString(Name));
  130. MDNode *Root = MDNode::getDistinct(Context, Args);
  131. // At this point we have
  132. // !0 = distinct !{null} <- root
  133. // Replace the reserved operand with the root node itself.
  134. Root->replaceOperandWith(0, Root);
  135. // We now have
  136. // !0 = distinct !{!0} <- root
  137. return Root;
  138. }
  139. MDNode *MDBuilder::createTBAARoot(StringRef Name) {
  140. return MDNode::get(Context, createString(Name));
  141. }
  142. /// Return metadata for a non-root TBAA node with the given name,
  143. /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
  144. MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
  145. bool isConstant) {
  146. if (isConstant) {
  147. Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
  148. return MDNode::get(Context,
  149. {createString(Name), Parent, createConstant(Flags)});
  150. }
  151. return MDNode::get(Context, {createString(Name), Parent});
  152. }
  153. MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
  154. return MDNode::get(Context, createString(Name));
  155. }
  156. MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
  157. return MDNode::get(Context, {createString(Name), Domain});
  158. }
  159. /// Return metadata for a tbaa.struct node with the given
  160. /// struct field descriptions.
  161. MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
  162. SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
  163. Type *Int64 = Type::getInt64Ty(Context);
  164. for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
  165. Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
  166. Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
  167. Vals[i * 3 + 2] = Fields[i].Type;
  168. }
  169. return MDNode::get(Context, Vals);
  170. }
  171. /// Return metadata for a TBAA struct node in the type DAG
  172. /// with the given name, a list of pairs (offset, field type in the type DAG).
  173. MDNode *MDBuilder::createTBAAStructTypeNode(
  174. StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
  175. SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
  176. Type *Int64 = Type::getInt64Ty(Context);
  177. Ops[0] = createString(Name);
  178. for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
  179. Ops[i * 2 + 1] = Fields[i].first;
  180. Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
  181. }
  182. return MDNode::get(Context, Ops);
  183. }
  184. /// Return metadata for a TBAA scalar type node with the
  185. /// given name, an offset and a parent in the TBAA type DAG.
  186. MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
  187. uint64_t Offset) {
  188. ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
  189. return MDNode::get(Context,
  190. {createString(Name), Parent, createConstant(Off)});
  191. }
  192. /// Return metadata for a TBAA tag node with the given
  193. /// base type, access type and offset relative to the base type.
  194. MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
  195. uint64_t Offset, bool IsConstant) {
  196. IntegerType *Int64 = Type::getInt64Ty(Context);
  197. ConstantInt *Off = ConstantInt::get(Int64, Offset);
  198. if (IsConstant) {
  199. return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
  200. createConstant(ConstantInt::get(Int64, 1))});
  201. }
  202. return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
  203. }
  204. MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
  205. Metadata *Id,
  206. ArrayRef<TBAAStructField> Fields) {
  207. SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
  208. Type *Int64 = Type::getInt64Ty(Context);
  209. Ops[0] = Parent;
  210. Ops[1] = createConstant(ConstantInt::get(Int64, Size));
  211. Ops[2] = Id;
  212. for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
  213. Ops[I * 3 + 3] = Fields[I].Type;
  214. Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
  215. Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
  216. }
  217. return MDNode::get(Context, Ops);
  218. }
  219. MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
  220. uint64_t Offset, uint64_t Size,
  221. bool IsImmutable) {
  222. IntegerType *Int64 = Type::getInt64Ty(Context);
  223. auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
  224. auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
  225. if (IsImmutable) {
  226. auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
  227. return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
  228. ImmutabilityFlagNode});
  229. }
  230. return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
  231. }
  232. MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
  233. MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
  234. MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
  235. Metadata *OffsetNode = Tag->getOperand(2);
  236. uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
  237. bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
  238. // See if the tag is already mutable.
  239. unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
  240. if (Tag->getNumOperands() <= ImmutabilityFlagOp)
  241. return Tag;
  242. // If Tag is already mutable then return it.
  243. Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
  244. if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
  245. return Tag;
  246. // Otherwise, create another node.
  247. if (!NewFormat)
  248. return createTBAAStructTagNode(BaseType, AccessType, Offset);
  249. Metadata *SizeNode = Tag->getOperand(3);
  250. uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
  251. return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
  252. }
  253. MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
  254. Metadata *Vals[] = {
  255. createString("loop_header_weight"),
  256. createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
  257. };
  258. return MDNode::get(Context, Vals);
  259. }
  260. MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash,
  261. Function *F) {
  262. auto *Int64Ty = Type::getInt64Ty(Context);
  263. SmallVector<Metadata *, 3> Ops(3);
  264. Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));
  265. Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));
  266. Ops[2] = createString(F->getName());
  267. return MDNode::get(Context, Ops);
  268. }