MDBuilder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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, std::nullopt);
  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::createRTTIPointerPrologue(Constant *PrologueSig,
  125. Constant *RTTI) {
  126. SmallVector<Metadata *, 4> Ops;
  127. Ops.push_back(createConstant(PrologueSig));
  128. Ops.push_back(createConstant(RTTI));
  129. return MDNode::get(Context, Ops);
  130. }
  131. MDNode *MDBuilder::createPCSections(ArrayRef<PCSection> Sections) {
  132. SmallVector<Metadata *, 2> Ops;
  133. for (const auto &Entry : Sections) {
  134. const StringRef &Sec = Entry.first;
  135. Ops.push_back(createString(Sec));
  136. // If auxiliary data for this section exists, append it.
  137. const SmallVector<Constant *> &AuxConsts = Entry.second;
  138. if (!AuxConsts.empty()) {
  139. SmallVector<Metadata *, 1> AuxMDs;
  140. AuxMDs.reserve(AuxConsts.size());
  141. for (Constant *C : AuxConsts)
  142. AuxMDs.push_back(createConstant(C));
  143. Ops.push_back(MDNode::get(Context, AuxMDs));
  144. }
  145. }
  146. return MDNode::get(Context, Ops);
  147. }
  148. MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
  149. SmallVector<Metadata *, 3> Args(1, nullptr);
  150. if (Extra)
  151. Args.push_back(Extra);
  152. if (!Name.empty())
  153. Args.push_back(createString(Name));
  154. MDNode *Root = MDNode::getDistinct(Context, Args);
  155. // At this point we have
  156. // !0 = distinct !{null} <- root
  157. // Replace the reserved operand with the root node itself.
  158. Root->replaceOperandWith(0, Root);
  159. // We now have
  160. // !0 = distinct !{!0} <- root
  161. return Root;
  162. }
  163. MDNode *MDBuilder::createTBAARoot(StringRef Name) {
  164. return MDNode::get(Context, createString(Name));
  165. }
  166. /// Return metadata for a non-root TBAA node with the given name,
  167. /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
  168. MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
  169. bool isConstant) {
  170. if (isConstant) {
  171. Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
  172. return MDNode::get(Context,
  173. {createString(Name), Parent, createConstant(Flags)});
  174. }
  175. return MDNode::get(Context, {createString(Name), Parent});
  176. }
  177. MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
  178. return MDNode::get(Context, createString(Name));
  179. }
  180. MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
  181. return MDNode::get(Context, {createString(Name), Domain});
  182. }
  183. /// Return metadata for a tbaa.struct node with the given
  184. /// struct field descriptions.
  185. MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
  186. SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
  187. Type *Int64 = Type::getInt64Ty(Context);
  188. for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
  189. Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
  190. Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
  191. Vals[i * 3 + 2] = Fields[i].Type;
  192. }
  193. return MDNode::get(Context, Vals);
  194. }
  195. /// Return metadata for a TBAA struct node in the type DAG
  196. /// with the given name, a list of pairs (offset, field type in the type DAG).
  197. MDNode *MDBuilder::createTBAAStructTypeNode(
  198. StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
  199. SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
  200. Type *Int64 = Type::getInt64Ty(Context);
  201. Ops[0] = createString(Name);
  202. for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
  203. Ops[i * 2 + 1] = Fields[i].first;
  204. Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
  205. }
  206. return MDNode::get(Context, Ops);
  207. }
  208. /// Return metadata for a TBAA scalar type node with the
  209. /// given name, an offset and a parent in the TBAA type DAG.
  210. MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
  211. uint64_t Offset) {
  212. ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
  213. return MDNode::get(Context,
  214. {createString(Name), Parent, createConstant(Off)});
  215. }
  216. /// Return metadata for a TBAA tag node with the given
  217. /// base type, access type and offset relative to the base type.
  218. MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
  219. uint64_t Offset, bool IsConstant) {
  220. IntegerType *Int64 = Type::getInt64Ty(Context);
  221. ConstantInt *Off = ConstantInt::get(Int64, Offset);
  222. if (IsConstant) {
  223. return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
  224. createConstant(ConstantInt::get(Int64, 1))});
  225. }
  226. return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
  227. }
  228. MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
  229. Metadata *Id,
  230. ArrayRef<TBAAStructField> Fields) {
  231. SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
  232. Type *Int64 = Type::getInt64Ty(Context);
  233. Ops[0] = Parent;
  234. Ops[1] = createConstant(ConstantInt::get(Int64, Size));
  235. Ops[2] = Id;
  236. for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
  237. Ops[I * 3 + 3] = Fields[I].Type;
  238. Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
  239. Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
  240. }
  241. return MDNode::get(Context, Ops);
  242. }
  243. MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
  244. uint64_t Offset, uint64_t Size,
  245. bool IsImmutable) {
  246. IntegerType *Int64 = Type::getInt64Ty(Context);
  247. auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
  248. auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
  249. if (IsImmutable) {
  250. auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
  251. return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
  252. ImmutabilityFlagNode});
  253. }
  254. return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
  255. }
  256. MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
  257. MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
  258. MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
  259. Metadata *OffsetNode = Tag->getOperand(2);
  260. uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
  261. bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
  262. // See if the tag is already mutable.
  263. unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
  264. if (Tag->getNumOperands() <= ImmutabilityFlagOp)
  265. return Tag;
  266. // If Tag is already mutable then return it.
  267. Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
  268. if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
  269. return Tag;
  270. // Otherwise, create another node.
  271. if (!NewFormat)
  272. return createTBAAStructTagNode(BaseType, AccessType, Offset);
  273. Metadata *SizeNode = Tag->getOperand(3);
  274. uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
  275. return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
  276. }
  277. MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
  278. Metadata *Vals[] = {
  279. createString("loop_header_weight"),
  280. createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
  281. };
  282. return MDNode::get(Context, Vals);
  283. }
  284. MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash,
  285. Function *F) {
  286. auto *Int64Ty = Type::getInt64Ty(Context);
  287. SmallVector<Metadata *, 3> Ops(3);
  288. Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));
  289. Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));
  290. Ops[2] = createString(F->getName());
  291. return MDNode::get(Context, Ops);
  292. }
  293. MDNode *
  294. MDBuilder::createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStats) {
  295. auto *Int64Ty = Type::getInt64Ty(Context);
  296. SmallVector<Metadata *, 4> Ops(LLVMStats.size() * 2);
  297. for (size_t I = 0; I < LLVMStats.size(); I++) {
  298. Ops[I * 2] = createString(LLVMStats[I].first);
  299. Ops[I * 2 + 1] =
  300. createConstant(ConstantInt::get(Int64Ty, LLVMStats[I].second));
  301. }
  302. return MDNode::get(Context, Ops);
  303. }