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 14 #include "llvm/IR/MDBuilder.h" 15 #include "llvm/IR/Constants.h" 16 #include "llvm/IR/Function.h" 17 #include "llvm/IR/Metadata.h" 18 using namespace llvm; 19 20 MDString *MDBuilder::createString(StringRef Str) { 21 return MDString::get(Context, Str); 22 } 23 24 ConstantAsMetadata *MDBuilder::createConstant(Constant *C) { 25 return ConstantAsMetadata::get(C); 26 } 27 28 MDNode *MDBuilder::createFPMath(float Accuracy) { 29 if (Accuracy == 0.0) 30 return nullptr; 31 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!"); 32 auto *Op = 33 createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy)); 34 return MDNode::get(Context, Op); 35 } 36 37 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight, 38 uint32_t FalseWeight, bool IsExpected) { 39 return createBranchWeights({TrueWeight, FalseWeight}, IsExpected); 40 } 41 42 MDNode *MDBuilder::createLikelyBranchWeights() { 43 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp 44 return createBranchWeights((1U << 20) - 1, 1); 45 } 46 47 MDNode *MDBuilder::createUnlikelyBranchWeights() { 48 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp 49 return createBranchWeights(1, (1U << 20) - 1); 50 } 51 52 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights, 53 bool IsExpected) { 54 assert(Weights.size() >= 1 && "Need at least one branch weights!"); 55 56 unsigned int Offset = IsExpected ? 2 : 1; 57 SmallVector<Metadata *, 4> Vals(Weights.size() + Offset); 58 Vals[0] = createString("branch_weights"); 59 if (IsExpected) 60 Vals[1] = createString("expected"); 61 62 Type *Int32Ty = Type::getInt32Ty(Context); 63 for (unsigned i = 0, e = Weights.size(); i != e; ++i) 64 Vals[i + Offset] = createConstant(ConstantInt::get(Int32Ty, Weights[i])); 65 66 return MDNode::get(Context, Vals); 67 } 68 69 MDNode *MDBuilder::createUnpredictable() { return MDNode::get(Context, {}); } 70 71 MDNode *MDBuilder::createFunctionEntryCount( 72 uint64_t Count, bool Synthetic, 73 const DenseSet<GlobalValue::GUID> *Imports) { 74 Type *Int64Ty = Type::getInt64Ty(Context); 75 SmallVector<Metadata *, 8> Ops; 76 if (Synthetic) 77 Ops.push_back(createString("synthetic_function_entry_count")); 78 else 79 Ops.push_back(createString("function_entry_count")); 80 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count))); 81 if (Imports) { 82 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end()); 83 llvm::sort(OrderID); 84 for (auto ID : OrderID) 85 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID))); 86 } 87 return MDNode::get(Context, Ops); 88 } 89 90 MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) { 91 return MDNode::get( 92 Context, {createString("function_section_prefix"), createString(Prefix)}); 93 } 94 95 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) { 96 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!"); 97 98 Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); 99 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)); 100 } 101 102 MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) { 103 // If the range is everything then it is useless. 104 if (Hi == Lo) 105 return nullptr; 106 107 // Return the range [Lo, Hi). 108 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)}); 109 } 110 111 MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) { 112 SmallVector<Metadata *, 4> Ops; 113 for (Function *F : Callees) 114 Ops.push_back(createConstant(F)); 115 return MDNode::get(Context, Ops); 116 } 117 118 MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo, 119 ArrayRef<int> Arguments, 120 bool VarArgArePassed) { 121 SmallVector<Metadata *, 4> Ops; 122 123 Type *Int64 = Type::getInt64Ty(Context); 124 Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo))); 125 126 for (int ArgNo : Arguments) 127 Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true))); 128 129 Type *Int1 = Type::getInt1Ty(Context); 130 Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed))); 131 132 return MDNode::get(Context, Ops); 133 } 134 135 MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks, 136 MDNode *NewCB) { 137 if (!ExistingCallbacks) 138 return MDNode::get(Context, {NewCB}); 139 140 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0)); 141 uint64_t NewCBCalleeIdx = 142 cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue(); 143 (void)NewCBCalleeIdx; 144 145 SmallVector<Metadata *, 4> Ops; 146 unsigned NumExistingOps = ExistingCallbacks->getNumOperands(); 147 Ops.resize(NumExistingOps + 1); 148 149 for (unsigned u = 0; u < NumExistingOps; u++) { 150 Ops[u] = ExistingCallbacks->getOperand(u); 151 152 auto *OldCBCalleeIdxAsCM = 153 cast<ConstantAsMetadata>(cast<MDNode>(Ops[u])->getOperand(0)); 154 uint64_t OldCBCalleeIdx = 155 cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue(); 156 (void)OldCBCalleeIdx; 157 assert(NewCBCalleeIdx != OldCBCalleeIdx && 158 "Cannot map a callback callee index twice!"); 159 } 160 161 Ops[NumExistingOps] = NewCB; 162 return MDNode::get(Context, Ops); 163 } 164 165 MDNode *MDBuilder::createRTTIPointerPrologue(Constant *PrologueSig, 166 Constant *RTTI) { 167 SmallVector<Metadata *, 4> Ops; 168 Ops.push_back(createConstant(PrologueSig)); 169 Ops.push_back(createConstant(RTTI)); 170 return MDNode::get(Context, Ops); 171 } 172 173 MDNode *MDBuilder::createPCSections(ArrayRef<PCSection> Sections) { 174 SmallVector<Metadata *, 2> Ops; 175 176 for (const auto &Entry : Sections) { 177 const StringRef &Sec = Entry.first; 178 Ops.push_back(createString(Sec)); 179 180 // If auxiliary data for this section exists, append it. 181 const SmallVector<Constant *> &AuxConsts = Entry.second; 182 if (!AuxConsts.empty()) { 183 SmallVector<Metadata *, 1> AuxMDs; 184 AuxMDs.reserve(AuxConsts.size()); 185 for (Constant *C : AuxConsts) 186 AuxMDs.push_back(createConstant(C)); 187 Ops.push_back(MDNode::get(Context, AuxMDs)); 188 } 189 } 190 191 return MDNode::get(Context, Ops); 192 } 193 194 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { 195 SmallVector<Metadata *, 3> Args(1, nullptr); 196 if (Extra) 197 Args.push_back(Extra); 198 if (!Name.empty()) 199 Args.push_back(createString(Name)); 200 MDNode *Root = MDNode::getDistinct(Context, Args); 201 202 // At this point we have 203 // !0 = distinct !{null} <- root 204 // Replace the reserved operand with the root node itself. 205 Root->replaceOperandWith(0, Root); 206 207 // We now have 208 // !0 = distinct !{!0} <- root 209 return Root; 210 } 211 212 MDNode *MDBuilder::createTBAARoot(StringRef Name) { 213 return MDNode::get(Context, createString(Name)); 214 } 215 216 /// Return metadata for a non-root TBAA node with the given name, 217 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'. 218 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent, 219 bool isConstant) { 220 if (isConstant) { 221 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); 222 return MDNode::get(Context, 223 {createString(Name), Parent, createConstant(Flags)}); 224 } 225 return MDNode::get(Context, {createString(Name), Parent}); 226 } 227 228 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) { 229 return MDNode::get(Context, createString(Name)); 230 } 231 232 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { 233 return MDNode::get(Context, {createString(Name), Domain}); 234 } 235 236 /// Return metadata for a tbaa.struct node with the given 237 /// struct field descriptions. 238 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { 239 SmallVector<Metadata *, 4> Vals(Fields.size() * 3); 240 Type *Int64 = Type::getInt64Ty(Context); 241 for (unsigned i = 0, e = Fields.size(); i != e; ++i) { 242 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset)); 243 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size)); 244 Vals[i * 3 + 2] = Fields[i].Type; 245 } 246 return MDNode::get(Context, Vals); 247 } 248 249 /// Return metadata for a TBAA struct node in the type DAG 250 /// with the given name, a list of pairs (offset, field type in the type DAG). 251 MDNode *MDBuilder::createTBAAStructTypeNode( 252 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) { 253 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1); 254 Type *Int64 = Type::getInt64Ty(Context); 255 Ops[0] = createString(Name); 256 for (unsigned i = 0, e = Fields.size(); i != e; ++i) { 257 Ops[i * 2 + 1] = Fields[i].first; 258 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second)); 259 } 260 return MDNode::get(Context, Ops); 261 } 262 263 /// Return metadata for a TBAA scalar type node with the 264 /// given name, an offset and a parent in the TBAA type DAG. 265 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, 266 uint64_t Offset) { 267 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); 268 return MDNode::get(Context, 269 {createString(Name), Parent, createConstant(Off)}); 270 } 271 272 /// Return metadata for a TBAA tag node with the given 273 /// base type, access type and offset relative to the base type. 274 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, 275 uint64_t Offset, bool IsConstant) { 276 IntegerType *Int64 = Type::getInt64Ty(Context); 277 ConstantInt *Off = ConstantInt::get(Int64, Offset); 278 if (IsConstant) { 279 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off), 280 createConstant(ConstantInt::get(Int64, 1))}); 281 } 282 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)}); 283 } 284 285 MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size, 286 Metadata *Id, 287 ArrayRef<TBAAStructField> Fields) { 288 SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3); 289 Type *Int64 = Type::getInt64Ty(Context); 290 Ops[0] = Parent; 291 Ops[1] = createConstant(ConstantInt::get(Int64, Size)); 292 Ops[2] = Id; 293 for (unsigned I = 0, E = Fields.size(); I != E; ++I) { 294 Ops[I * 3 + 3] = Fields[I].Type; 295 Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset)); 296 Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size)); 297 } 298 return MDNode::get(Context, Ops); 299 } 300 301 MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType, 302 uint64_t Offset, uint64_t Size, 303 bool IsImmutable) { 304 IntegerType *Int64 = Type::getInt64Ty(Context); 305 auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset)); 306 auto *SizeNode = createConstant(ConstantInt::get(Int64, Size)); 307 if (IsImmutable) { 308 auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1)); 309 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode, 310 ImmutabilityFlagNode}); 311 } 312 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode}); 313 } 314 315 MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) { 316 MDNode *BaseType = cast<MDNode>(Tag->getOperand(0)); 317 MDNode *AccessType = cast<MDNode>(Tag->getOperand(1)); 318 Metadata *OffsetNode = Tag->getOperand(2); 319 uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue(); 320 321 bool NewFormat = isa<MDNode>(AccessType->getOperand(0)); 322 323 // See if the tag is already mutable. 324 unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3; 325 if (Tag->getNumOperands() <= ImmutabilityFlagOp) 326 return Tag; 327 328 // If Tag is already mutable then return it. 329 Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp); 330 if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue()) 331 return Tag; 332 333 // Otherwise, create another node. 334 if (!NewFormat) 335 return createTBAAStructTagNode(BaseType, AccessType, Offset); 336 337 Metadata *SizeNode = Tag->getOperand(3); 338 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue(); 339 return createTBAAAccessTag(BaseType, AccessType, Offset, Size); 340 } 341 342 MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) { 343 Metadata *Vals[] = { 344 createString("loop_header_weight"), 345 createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)), 346 }; 347 return MDNode::get(Context, Vals); 348 } 349 350 MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, 351 StringRef FName) { 352 auto *Int64Ty = Type::getInt64Ty(Context); 353 SmallVector<Metadata *, 3> Ops(3); 354 Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID)); 355 Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash)); 356 Ops[2] = createString(FName); 357 return MDNode::get(Context, Ops); 358 } 359 360 MDNode * 361 MDBuilder::createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStats) { 362 auto *Int64Ty = Type::getInt64Ty(Context); 363 SmallVector<Metadata *, 4> Ops(LLVMStats.size() * 2); 364 for (size_t I = 0; I < LLVMStats.size(); I++) { 365 Ops[I * 2] = createString(LLVMStats[I].first); 366 Ops[I * 2 + 1] = 367 createConstant(ConstantInt::get(Int64Ty, LLVMStats[I].second)); 368 } 369 return MDNode::get(Context, Ops); 370 } 371