1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===// 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 implements the DIBuilder. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/IR/DIBuilder.h" 14 #include "LLVMContextImpl.h" 15 #include "llvm/ADT/APInt.h" 16 #include "llvm/ADT/APSInt.h" 17 #include "llvm/BinaryFormat/Dwarf.h" 18 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/DebugInfo.h" 20 #include "llvm/IR/IRBuilder.h" 21 #include "llvm/IR/Module.h" 22 #include <optional> 23 24 using namespace llvm; 25 using namespace llvm::dwarf; 26 27 DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes, DICompileUnit *CU) 28 : M(m), VMContext(M.getContext()), CUNode(CU), DeclareFn(nullptr), 29 ValueFn(nullptr), LabelFn(nullptr), AssignFn(nullptr), 30 AllowUnresolvedNodes(AllowUnresolvedNodes) { 31 if (CUNode) { 32 if (const auto &ETs = CUNode->getEnumTypes()) 33 AllEnumTypes.assign(ETs.begin(), ETs.end()); 34 if (const auto &RTs = CUNode->getRetainedTypes()) 35 AllRetainTypes.assign(RTs.begin(), RTs.end()); 36 if (const auto &GVs = CUNode->getGlobalVariables()) 37 AllGVs.assign(GVs.begin(), GVs.end()); 38 if (const auto &IMs = CUNode->getImportedEntities()) 39 ImportedModules.assign(IMs.begin(), IMs.end()); 40 if (const auto &MNs = CUNode->getMacros()) 41 AllMacrosPerParent.insert({nullptr, {MNs.begin(), MNs.end()}}); 42 } 43 } 44 45 void DIBuilder::trackIfUnresolved(MDNode *N) { 46 if (!N) 47 return; 48 if (N->isResolved()) 49 return; 50 51 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes"); 52 UnresolvedNodes.emplace_back(N); 53 } 54 55 void DIBuilder::finalizeSubprogram(DISubprogram *SP) { 56 auto PN = SubprogramTrackedNodes.find(SP); 57 if (PN != SubprogramTrackedNodes.end()) 58 SP->replaceRetainedNodes( 59 MDTuple::get(VMContext, SmallVector<Metadata *, 16>(PN->second.begin(), 60 PN->second.end()))); 61 } 62 63 void DIBuilder::finalize() { 64 if (!CUNode) { 65 assert(!AllowUnresolvedNodes && 66 "creating type nodes without a CU is not supported"); 67 return; 68 } 69 70 if (!AllEnumTypes.empty()) 71 CUNode->replaceEnumTypes(MDTuple::get( 72 VMContext, SmallVector<Metadata *, 16>(AllEnumTypes.begin(), 73 AllEnumTypes.end()))); 74 75 SmallVector<Metadata *, 16> RetainValues; 76 // Declarations and definitions of the same type may be retained. Some 77 // clients RAUW these pairs, leaving duplicates in the retained types 78 // list. Use a set to remove the duplicates while we transform the 79 // TrackingVHs back into Values. 80 SmallPtrSet<Metadata *, 16> RetainSet; 81 for (const TrackingMDNodeRef &N : AllRetainTypes) 82 if (RetainSet.insert(N).second) 83 RetainValues.push_back(N); 84 85 if (!RetainValues.empty()) 86 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues)); 87 88 for (auto *SP : AllSubprograms) 89 finalizeSubprogram(SP); 90 for (auto *N : RetainValues) 91 if (auto *SP = dyn_cast<DISubprogram>(N)) 92 finalizeSubprogram(SP); 93 94 if (!AllGVs.empty()) 95 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs)); 96 97 if (!ImportedModules.empty()) 98 CUNode->replaceImportedEntities(MDTuple::get( 99 VMContext, SmallVector<Metadata *, 16>(ImportedModules.begin(), 100 ImportedModules.end()))); 101 102 for (const auto &I : AllMacrosPerParent) { 103 // DIMacroNode's with nullptr parent are DICompileUnit direct children. 104 if (!I.first) { 105 CUNode->replaceMacros(MDTuple::get(VMContext, I.second.getArrayRef())); 106 continue; 107 } 108 // Otherwise, it must be a temporary DIMacroFile that need to be resolved. 109 auto *TMF = cast<DIMacroFile>(I.first); 110 auto *MF = DIMacroFile::get(VMContext, dwarf::DW_MACINFO_start_file, 111 TMF->getLine(), TMF->getFile(), 112 getOrCreateMacroArray(I.second.getArrayRef())); 113 replaceTemporary(llvm::TempDIMacroNode(TMF), MF); 114 } 115 116 // Now that all temp nodes have been replaced or deleted, resolve remaining 117 // cycles. 118 for (const auto &N : UnresolvedNodes) 119 if (N && !N->isResolved()) 120 N->resolveCycles(); 121 UnresolvedNodes.clear(); 122 123 // Can't handle unresolved nodes anymore. 124 AllowUnresolvedNodes = false; 125 } 126 127 /// If N is compile unit return NULL otherwise return N. 128 static DIScope *getNonCompileUnitScope(DIScope *N) { 129 if (!N || isa<DICompileUnit>(N)) 130 return nullptr; 131 return cast<DIScope>(N); 132 } 133 134 DICompileUnit *DIBuilder::createCompileUnit( 135 unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized, 136 StringRef Flags, unsigned RunTimeVer, StringRef SplitName, 137 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId, 138 bool SplitDebugInlining, bool DebugInfoForProfiling, 139 DICompileUnit::DebugNameTableKind NameTableKind, bool RangesBaseAddress, 140 StringRef SysRoot, StringRef SDK) { 141 142 assert(((Lang <= dwarf::DW_LANG_Metal && Lang >= dwarf::DW_LANG_C89) || 143 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) && 144 "Invalid Language tag"); 145 146 assert(!CUNode && "Can only make one compile unit per DIBuilder instance"); 147 CUNode = DICompileUnit::getDistinct( 148 VMContext, Lang, File, Producer, isOptimized, Flags, RunTimeVer, 149 SplitName, Kind, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId, 150 SplitDebugInlining, DebugInfoForProfiling, NameTableKind, 151 RangesBaseAddress, SysRoot, SDK); 152 153 // Create a named metadata so that it is easier to find cu in a module. 154 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); 155 NMD->addOperand(CUNode); 156 trackIfUnresolved(CUNode); 157 return CUNode; 158 } 159 160 static DIImportedEntity * 161 createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context, 162 Metadata *NS, DIFile *File, unsigned Line, StringRef Name, 163 DINodeArray Elements, 164 SmallVectorImpl<TrackingMDNodeRef> &ImportedModules) { 165 if (Line) 166 assert(File && "Source location has line number but no file"); 167 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size(); 168 auto *M = DIImportedEntity::get(C, Tag, Context, cast_or_null<DINode>(NS), 169 File, Line, Name, Elements); 170 if (EntitiesCount < C.pImpl->DIImportedEntitys.size()) 171 // A new Imported Entity was just added to the context. 172 // Add it to the Imported Modules list. 173 ImportedModules.emplace_back(M); 174 return M; 175 } 176 177 DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, 178 DINamespace *NS, DIFile *File, 179 unsigned Line, 180 DINodeArray Elements) { 181 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module, 182 Context, NS, File, Line, StringRef(), Elements, 183 getImportTrackingVector(Context)); 184 } 185 186 DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, 187 DIImportedEntity *NS, 188 DIFile *File, unsigned Line, 189 DINodeArray Elements) { 190 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module, 191 Context, NS, File, Line, StringRef(), Elements, 192 getImportTrackingVector(Context)); 193 } 194 195 DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M, 196 DIFile *File, unsigned Line, 197 DINodeArray Elements) { 198 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module, 199 Context, M, File, Line, StringRef(), Elements, 200 getImportTrackingVector(Context)); 201 } 202 203 DIImportedEntity * 204 DIBuilder::createImportedDeclaration(DIScope *Context, DINode *Decl, 205 DIFile *File, unsigned Line, 206 StringRef Name, DINodeArray Elements) { 207 // Make sure to use the unique identifier based metadata reference for 208 // types that have one. 209 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration, 210 Context, Decl, File, Line, Name, Elements, 211 getImportTrackingVector(Context)); 212 } 213 214 DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory, 215 std::optional<DIFile::ChecksumInfo<StringRef>> CS, 216 std::optional<StringRef> Source) { 217 return DIFile::get(VMContext, Filename, Directory, CS, Source); 218 } 219 220 DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber, 221 unsigned MacroType, StringRef Name, 222 StringRef Value) { 223 assert(!Name.empty() && "Unable to create macro without name"); 224 assert((MacroType == dwarf::DW_MACINFO_undef || 225 MacroType == dwarf::DW_MACINFO_define) && 226 "Unexpected macro type"); 227 auto *M = DIMacro::get(VMContext, MacroType, LineNumber, Name, Value); 228 AllMacrosPerParent[Parent].insert(M); 229 return M; 230 } 231 232 DIMacroFile *DIBuilder::createTempMacroFile(DIMacroFile *Parent, 233 unsigned LineNumber, DIFile *File) { 234 auto *MF = DIMacroFile::getTemporary(VMContext, dwarf::DW_MACINFO_start_file, 235 LineNumber, File, DIMacroNodeArray()) 236 .release(); 237 AllMacrosPerParent[Parent].insert(MF); 238 // Add the new temporary DIMacroFile to the macro per parent map as a parent. 239 // This is needed to assure DIMacroFile with no children to have an entry in 240 // the map. Otherwise, it will not be resolved in DIBuilder::finalize(). 241 AllMacrosPerParent.insert({MF, {}}); 242 return MF; 243 } 244 245 DIEnumerator *DIBuilder::createEnumerator(StringRef Name, uint64_t Val, 246 bool IsUnsigned) { 247 assert(!Name.empty() && "Unable to create enumerator without name"); 248 return DIEnumerator::get(VMContext, APInt(64, Val, !IsUnsigned), IsUnsigned, 249 Name); 250 } 251 252 DIEnumerator *DIBuilder::createEnumerator(StringRef Name, const APSInt &Value) { 253 assert(!Name.empty() && "Unable to create enumerator without name"); 254 return DIEnumerator::get(VMContext, APInt(Value), Value.isUnsigned(), Name); 255 } 256 257 DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) { 258 assert(!Name.empty() && "Unable to create type without name"); 259 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name); 260 } 261 262 DIBasicType *DIBuilder::createNullPtrType() { 263 return createUnspecifiedType("decltype(nullptr)"); 264 } 265 266 DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits, 267 unsigned Encoding, 268 DINode::DIFlags Flags, 269 uint32_t NumExtraInhabitants) { 270 assert(!Name.empty() && "Unable to create type without name"); 271 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits, 272 0, Encoding, NumExtraInhabitants, Flags); 273 } 274 275 DIStringType *DIBuilder::createStringType(StringRef Name, uint64_t SizeInBits) { 276 assert(!Name.empty() && "Unable to create type without name"); 277 return DIStringType::get(VMContext, dwarf::DW_TAG_string_type, Name, 278 SizeInBits, 0); 279 } 280 281 DIStringType *DIBuilder::createStringType(StringRef Name, 282 DIVariable *StringLength, 283 DIExpression *StrLocationExp) { 284 assert(!Name.empty() && "Unable to create type without name"); 285 return DIStringType::get(VMContext, dwarf::DW_TAG_string_type, Name, 286 StringLength, nullptr, StrLocationExp, 0, 0, 0); 287 } 288 289 DIStringType *DIBuilder::createStringType(StringRef Name, 290 DIExpression *StringLengthExp, 291 DIExpression *StrLocationExp) { 292 assert(!Name.empty() && "Unable to create type without name"); 293 return DIStringType::get(VMContext, dwarf::DW_TAG_string_type, Name, nullptr, 294 StringLengthExp, StrLocationExp, 0, 0, 0); 295 } 296 297 DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) { 298 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0, 299 0, 0, std::nullopt, std::nullopt, DINode::FlagZero); 300 } 301 302 DIDerivedType *DIBuilder::createPtrAuthQualifiedType( 303 DIType *FromTy, unsigned Key, bool IsAddressDiscriminated, 304 unsigned ExtraDiscriminator, bool IsaPointer, 305 bool AuthenticatesNullValues) { 306 return DIDerivedType::get(VMContext, dwarf::DW_TAG_LLVM_ptrauth_type, "", 307 nullptr, 0, nullptr, FromTy, 0, 0, 0, std::nullopt, 308 std::optional<DIDerivedType::PtrAuthData>( 309 std::in_place, Key, IsAddressDiscriminated, 310 ExtraDiscriminator, IsaPointer, 311 AuthenticatesNullValues), 312 DINode::FlagZero); 313 } 314 315 DIDerivedType * 316 DIBuilder::createPointerType(DIType *PointeeTy, uint64_t SizeInBits, 317 uint32_t AlignInBits, 318 std::optional<unsigned> DWARFAddressSpace, 319 StringRef Name, DINodeArray Annotations) { 320 // FIXME: Why is there a name here? 321 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name, 322 nullptr, 0, nullptr, PointeeTy, SizeInBits, 323 AlignInBits, 0, DWARFAddressSpace, std::nullopt, 324 DINode::FlagZero, nullptr, Annotations); 325 } 326 327 DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy, 328 DIType *Base, 329 uint64_t SizeInBits, 330 uint32_t AlignInBits, 331 DINode::DIFlags Flags) { 332 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "", 333 nullptr, 0, nullptr, PointeeTy, SizeInBits, 334 AlignInBits, 0, std::nullopt, std::nullopt, Flags, 335 Base); 336 } 337 338 DIDerivedType * 339 DIBuilder::createReferenceType(unsigned Tag, DIType *RTy, uint64_t SizeInBits, 340 uint32_t AlignInBits, 341 std::optional<unsigned> DWARFAddressSpace) { 342 assert(RTy && "Unable to create reference type"); 343 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy, 344 SizeInBits, AlignInBits, 0, DWARFAddressSpace, {}, 345 DINode::FlagZero); 346 } 347 348 DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name, 349 DIFile *File, unsigned LineNo, 350 DIScope *Context, uint32_t AlignInBits, 351 DINode::DIFlags Flags, 352 DINodeArray Annotations) { 353 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File, 354 LineNo, getNonCompileUnitScope(Context), Ty, 0, 355 AlignInBits, 0, std::nullopt, std::nullopt, Flags, 356 nullptr, Annotations); 357 } 358 359 DIDerivedType * 360 DIBuilder::createTemplateAlias(DIType *Ty, StringRef Name, DIFile *File, 361 unsigned LineNo, DIScope *Context, 362 DINodeArray TParams, uint32_t AlignInBits, 363 DINode::DIFlags Flags, DINodeArray Annotations) { 364 return DIDerivedType::get(VMContext, dwarf::DW_TAG_template_alias, Name, File, 365 LineNo, getNonCompileUnitScope(Context), Ty, 0, 366 AlignInBits, 0, std::nullopt, std::nullopt, Flags, 367 TParams.get(), Annotations); 368 } 369 370 DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) { 371 assert(Ty && "Invalid type!"); 372 assert(FriendTy && "Invalid friend type!"); 373 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty, 374 FriendTy, 0, 0, 0, std::nullopt, std::nullopt, 375 DINode::FlagZero); 376 } 377 378 DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy, 379 uint64_t BaseOffset, 380 uint32_t VBPtrOffset, 381 DINode::DIFlags Flags) { 382 assert(Ty && "Unable to create inheritance"); 383 Metadata *ExtraData = ConstantAsMetadata::get( 384 ConstantInt::get(IntegerType::get(VMContext, 32), VBPtrOffset)); 385 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr, 386 0, Ty, BaseTy, 0, 0, BaseOffset, std::nullopt, 387 std::nullopt, Flags, ExtraData); 388 } 389 390 DIDerivedType *DIBuilder::createMemberType( 391 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 392 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 393 DINode::DIFlags Flags, DIType *Ty, DINodeArray Annotations) { 394 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File, 395 LineNumber, getNonCompileUnitScope(Scope), Ty, 396 SizeInBits, AlignInBits, OffsetInBits, std::nullopt, 397 std::nullopt, Flags, nullptr, Annotations); 398 } 399 400 static ConstantAsMetadata *getConstantOrNull(Constant *C) { 401 if (C) 402 return ConstantAsMetadata::get(C); 403 return nullptr; 404 } 405 406 DIDerivedType *DIBuilder::createVariantMemberType( 407 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 408 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 409 Constant *Discriminant, DINode::DIFlags Flags, DIType *Ty) { 410 return DIDerivedType::get( 411 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber, 412 getNonCompileUnitScope(Scope), Ty, SizeInBits, AlignInBits, OffsetInBits, 413 std::nullopt, std::nullopt, Flags, getConstantOrNull(Discriminant)); 414 } 415 416 DIDerivedType *DIBuilder::createBitFieldMemberType( 417 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 418 uint64_t SizeInBits, uint64_t OffsetInBits, uint64_t StorageOffsetInBits, 419 DINode::DIFlags Flags, DIType *Ty, DINodeArray Annotations) { 420 Flags |= DINode::FlagBitField; 421 return DIDerivedType::get( 422 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber, 423 getNonCompileUnitScope(Scope), Ty, SizeInBits, /*AlignInBits=*/0, 424 OffsetInBits, std::nullopt, std::nullopt, Flags, 425 ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext, 64), 426 StorageOffsetInBits)), 427 Annotations); 428 } 429 430 DIDerivedType * 431 DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File, 432 unsigned LineNumber, DIType *Ty, 433 DINode::DIFlags Flags, llvm::Constant *Val, 434 unsigned Tag, uint32_t AlignInBits) { 435 Flags |= DINode::FlagStaticMember; 436 return DIDerivedType::get(VMContext, Tag, Name, File, LineNumber, 437 getNonCompileUnitScope(Scope), Ty, 0, AlignInBits, 438 0, std::nullopt, std::nullopt, Flags, 439 getConstantOrNull(Val)); 440 } 441 442 DIDerivedType * 443 DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber, 444 uint64_t SizeInBits, uint32_t AlignInBits, 445 uint64_t OffsetInBits, DINode::DIFlags Flags, 446 DIType *Ty, MDNode *PropertyNode) { 447 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File, 448 LineNumber, getNonCompileUnitScope(File), Ty, 449 SizeInBits, AlignInBits, OffsetInBits, std::nullopt, 450 std::nullopt, Flags, PropertyNode); 451 } 452 453 DIObjCProperty * 454 DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber, 455 StringRef GetterName, StringRef SetterName, 456 unsigned PropertyAttributes, DIType *Ty) { 457 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName, 458 SetterName, PropertyAttributes, Ty); 459 } 460 461 DITemplateTypeParameter * 462 DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name, 463 DIType *Ty, bool isDefault) { 464 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit"); 465 return DITemplateTypeParameter::get(VMContext, Name, Ty, isDefault); 466 } 467 468 static DITemplateValueParameter * 469 createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag, 470 DIScope *Context, StringRef Name, DIType *Ty, 471 bool IsDefault, Metadata *MD) { 472 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit"); 473 return DITemplateValueParameter::get(VMContext, Tag, Name, Ty, IsDefault, MD); 474 } 475 476 DITemplateValueParameter * 477 DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name, 478 DIType *Ty, bool isDefault, 479 Constant *Val) { 480 return createTemplateValueParameterHelper( 481 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty, 482 isDefault, getConstantOrNull(Val)); 483 } 484 485 DITemplateValueParameter * 486 DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name, 487 DIType *Ty, StringRef Val, 488 bool IsDefault) { 489 return createTemplateValueParameterHelper( 490 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty, 491 IsDefault, MDString::get(VMContext, Val)); 492 } 493 494 DITemplateValueParameter * 495 DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name, 496 DIType *Ty, DINodeArray Val) { 497 return createTemplateValueParameterHelper( 498 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty, 499 false, Val.get()); 500 } 501 502 DICompositeType *DIBuilder::createClassType( 503 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber, 504 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 505 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements, 506 unsigned RunTimeLang, DIType *VTableHolder, MDNode *TemplateParams, 507 StringRef UniqueIdentifier) { 508 assert((!Context || isa<DIScope>(Context)) && 509 "createClassType should be called with a valid Context"); 510 511 auto *R = DICompositeType::get( 512 VMContext, dwarf::DW_TAG_class_type, Name, File, LineNumber, 513 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 514 OffsetInBits, Flags, Elements, RunTimeLang, VTableHolder, 515 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier); 516 trackIfUnresolved(R); 517 return R; 518 } 519 520 DICompositeType *DIBuilder::createStructType( 521 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber, 522 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags, 523 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang, 524 DIType *VTableHolder, StringRef UniqueIdentifier, DIType *Specification, 525 uint32_t NumExtraInhabitants) { 526 auto *R = DICompositeType::get( 527 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber, 528 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0, 529 Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier, 530 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, Specification, 531 NumExtraInhabitants); 532 trackIfUnresolved(R); 533 return R; 534 } 535 536 DICompositeType *DIBuilder::createUnionType( 537 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 538 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags, 539 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) { 540 auto *R = DICompositeType::get( 541 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber, 542 getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags, 543 Elements, RunTimeLang, nullptr, nullptr, UniqueIdentifier); 544 trackIfUnresolved(R); 545 return R; 546 } 547 548 DICompositeType * 549 DIBuilder::createVariantPart(DIScope *Scope, StringRef Name, DIFile *File, 550 unsigned LineNumber, uint64_t SizeInBits, 551 uint32_t AlignInBits, DINode::DIFlags Flags, 552 DIDerivedType *Discriminator, DINodeArray Elements, 553 StringRef UniqueIdentifier) { 554 auto *R = DICompositeType::get( 555 VMContext, dwarf::DW_TAG_variant_part, Name, File, LineNumber, 556 getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags, 557 Elements, 0, nullptr, nullptr, UniqueIdentifier, Discriminator); 558 trackIfUnresolved(R); 559 return R; 560 } 561 562 DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes, 563 DINode::DIFlags Flags, 564 unsigned CC) { 565 return DISubroutineType::get(VMContext, Flags, CC, ParameterTypes); 566 } 567 568 DICompositeType * 569 DIBuilder::createEnumerationType(DIScope *Scope, StringRef Name, DIFile *File, 570 unsigned LineNumber, uint64_t SizeInBits, 571 uint32_t AlignInBits, DINodeArray Elements, 572 DIType *UnderlyingType, unsigned RunTimeLang, 573 StringRef UniqueIdentifier, bool IsScoped) { 574 auto *CTy = DICompositeType::get( 575 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber, 576 getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0, 577 IsScoped ? DINode::FlagEnumClass : DINode::FlagZero, Elements, 578 RunTimeLang, nullptr, nullptr, UniqueIdentifier); 579 AllEnumTypes.emplace_back(CTy); 580 trackIfUnresolved(CTy); 581 return CTy; 582 } 583 584 DIDerivedType *DIBuilder::createSetType(DIScope *Scope, StringRef Name, 585 DIFile *File, unsigned LineNo, 586 uint64_t SizeInBits, 587 uint32_t AlignInBits, DIType *Ty) { 588 auto *R = DIDerivedType::get(VMContext, dwarf::DW_TAG_set_type, Name, File, 589 LineNo, getNonCompileUnitScope(Scope), Ty, 590 SizeInBits, AlignInBits, 0, std::nullopt, 591 std::nullopt, DINode::FlagZero); 592 trackIfUnresolved(R); 593 return R; 594 } 595 596 DICompositeType * 597 DIBuilder::createArrayType(uint64_t Size, uint32_t AlignInBits, DIType *Ty, 598 DINodeArray Subscripts, 599 PointerUnion<DIExpression *, DIVariable *> DL, 600 PointerUnion<DIExpression *, DIVariable *> AS, 601 PointerUnion<DIExpression *, DIVariable *> AL, 602 PointerUnion<DIExpression *, DIVariable *> RK) { 603 auto *R = DICompositeType::get( 604 VMContext, dwarf::DW_TAG_array_type, "", nullptr, 0, nullptr, Ty, Size, 605 AlignInBits, 0, DINode::FlagZero, Subscripts, 0, nullptr, nullptr, "", 606 nullptr, 607 isa<DIExpression *>(DL) ? (Metadata *)cast<DIExpression *>(DL) 608 : (Metadata *)cast<DIVariable *>(DL), 609 isa<DIExpression *>(AS) ? (Metadata *)cast<DIExpression *>(AS) 610 : (Metadata *)cast<DIVariable *>(AS), 611 isa<DIExpression *>(AL) ? (Metadata *)cast<DIExpression *>(AL) 612 : (Metadata *)cast<DIVariable *>(AL), 613 isa<DIExpression *>(RK) ? (Metadata *)cast<DIExpression *>(RK) 614 : (Metadata *)cast<DIVariable *>(RK)); 615 trackIfUnresolved(R); 616 return R; 617 } 618 619 DICompositeType *DIBuilder::createVectorType(uint64_t Size, 620 uint32_t AlignInBits, DIType *Ty, 621 DINodeArray Subscripts) { 622 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "", 623 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0, 624 DINode::FlagVector, Subscripts, 0, nullptr); 625 trackIfUnresolved(R); 626 return R; 627 } 628 629 DISubprogram *DIBuilder::createArtificialSubprogram(DISubprogram *SP) { 630 auto NewSP = SP->cloneWithFlags(SP->getFlags() | DINode::FlagArtificial); 631 return MDNode::replaceWithDistinct(std::move(NewSP)); 632 } 633 634 static DIType *createTypeWithFlags(const DIType *Ty, 635 DINode::DIFlags FlagsToSet) { 636 auto NewTy = Ty->cloneWithFlags(Ty->getFlags() | FlagsToSet); 637 return MDNode::replaceWithUniqued(std::move(NewTy)); 638 } 639 640 DIType *DIBuilder::createArtificialType(DIType *Ty) { 641 // FIXME: Restrict this to the nodes where it's valid. 642 if (Ty->isArtificial()) 643 return Ty; 644 return createTypeWithFlags(Ty, DINode::FlagArtificial); 645 } 646 647 DIType *DIBuilder::createObjectPointerType(DIType *Ty, bool Implicit) { 648 // FIXME: Restrict this to the nodes where it's valid. 649 if (Ty->isObjectPointer()) 650 return Ty; 651 DINode::DIFlags Flags = DINode::FlagObjectPointer; 652 653 if (Implicit) 654 Flags |= DINode::FlagArtificial; 655 656 return createTypeWithFlags(Ty, Flags); 657 } 658 659 void DIBuilder::retainType(DIScope *T) { 660 assert(T && "Expected non-null type"); 661 assert((isa<DIType>(T) || (isa<DISubprogram>(T) && 662 cast<DISubprogram>(T)->isDefinition() == false)) && 663 "Expected type or subprogram declaration"); 664 AllRetainTypes.emplace_back(T); 665 } 666 667 DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; } 668 669 DICompositeType * 670 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope, 671 DIFile *F, unsigned Line, unsigned RuntimeLang, 672 uint64_t SizeInBits, uint32_t AlignInBits, 673 StringRef UniqueIdentifier) { 674 // FIXME: Define in terms of createReplaceableForwardDecl() by calling 675 // replaceWithUniqued(). 676 auto *RetTy = DICompositeType::get( 677 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr, 678 SizeInBits, AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang, 679 nullptr, nullptr, UniqueIdentifier); 680 trackIfUnresolved(RetTy); 681 return RetTy; 682 } 683 684 DICompositeType *DIBuilder::createReplaceableCompositeType( 685 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line, 686 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 687 DINode::DIFlags Flags, StringRef UniqueIdentifier, 688 DINodeArray Annotations) { 689 auto *RetTy = 690 DICompositeType::getTemporary( 691 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr, 692 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang, nullptr, 693 nullptr, UniqueIdentifier, nullptr, nullptr, nullptr, nullptr, 694 nullptr, Annotations) 695 .release(); 696 trackIfUnresolved(RetTy); 697 return RetTy; 698 } 699 700 DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) { 701 return MDTuple::get(VMContext, Elements); 702 } 703 704 DIMacroNodeArray 705 DIBuilder::getOrCreateMacroArray(ArrayRef<Metadata *> Elements) { 706 return MDTuple::get(VMContext, Elements); 707 } 708 709 DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) { 710 SmallVector<llvm::Metadata *, 16> Elts; 711 for (Metadata *E : Elements) { 712 if (isa_and_nonnull<MDNode>(E)) 713 Elts.push_back(cast<DIType>(E)); 714 else 715 Elts.push_back(E); 716 } 717 return DITypeRefArray(MDNode::get(VMContext, Elts)); 718 } 719 720 DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) { 721 auto *LB = ConstantAsMetadata::get( 722 ConstantInt::getSigned(Type::getInt64Ty(VMContext), Lo)); 723 auto *CountNode = ConstantAsMetadata::get( 724 ConstantInt::getSigned(Type::getInt64Ty(VMContext), Count)); 725 return DISubrange::get(VMContext, CountNode, LB, nullptr, nullptr); 726 } 727 728 DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, Metadata *CountNode) { 729 auto *LB = ConstantAsMetadata::get( 730 ConstantInt::getSigned(Type::getInt64Ty(VMContext), Lo)); 731 return DISubrange::get(VMContext, CountNode, LB, nullptr, nullptr); 732 } 733 734 DISubrange *DIBuilder::getOrCreateSubrange(Metadata *CountNode, Metadata *LB, 735 Metadata *UB, Metadata *Stride) { 736 return DISubrange::get(VMContext, CountNode, LB, UB, Stride); 737 } 738 739 DIGenericSubrange *DIBuilder::getOrCreateGenericSubrange( 740 DIGenericSubrange::BoundType CountNode, DIGenericSubrange::BoundType LB, 741 DIGenericSubrange::BoundType UB, DIGenericSubrange::BoundType Stride) { 742 auto ConvToMetadata = [&](DIGenericSubrange::BoundType Bound) -> Metadata * { 743 return isa<DIExpression *>(Bound) ? (Metadata *)cast<DIExpression *>(Bound) 744 : (Metadata *)cast<DIVariable *>(Bound); 745 }; 746 return DIGenericSubrange::get(VMContext, ConvToMetadata(CountNode), 747 ConvToMetadata(LB), ConvToMetadata(UB), 748 ConvToMetadata(Stride)); 749 } 750 751 static void checkGlobalVariableScope(DIScope *Context) { 752 #ifndef NDEBUG 753 if (auto *CT = 754 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context))) 755 assert(CT->getIdentifier().empty() && 756 "Context of a global variable should not be a type with identifier"); 757 #endif 758 } 759 760 DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression( 761 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F, 762 unsigned LineNumber, DIType *Ty, bool IsLocalToUnit, bool isDefined, 763 DIExpression *Expr, MDNode *Decl, MDTuple *TemplateParams, 764 uint32_t AlignInBits, DINodeArray Annotations) { 765 checkGlobalVariableScope(Context); 766 767 auto *GV = DIGlobalVariable::getDistinct( 768 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F, 769 LineNumber, Ty, IsLocalToUnit, isDefined, 770 cast_or_null<DIDerivedType>(Decl), TemplateParams, AlignInBits, 771 Annotations); 772 if (!Expr) 773 Expr = createExpression(); 774 auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr); 775 AllGVs.push_back(N); 776 return N; 777 } 778 779 DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl( 780 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F, 781 unsigned LineNumber, DIType *Ty, bool IsLocalToUnit, MDNode *Decl, 782 MDTuple *TemplateParams, uint32_t AlignInBits) { 783 checkGlobalVariableScope(Context); 784 785 return DIGlobalVariable::getTemporary( 786 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F, 787 LineNumber, Ty, IsLocalToUnit, false, 788 cast_or_null<DIDerivedType>(Decl), TemplateParams, AlignInBits, 789 nullptr) 790 .release(); 791 } 792 793 static DILocalVariable *createLocalVariable( 794 LLVMContext &VMContext, 795 SmallVectorImpl<TrackingMDNodeRef> &PreservedNodes, 796 DIScope *Context, StringRef Name, unsigned ArgNo, DIFile *File, 797 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags, 798 uint32_t AlignInBits, DINodeArray Annotations = nullptr) { 799 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT 800 // the only valid scopes)? 801 auto *Scope = cast<DILocalScope>(Context); 802 auto *Node = DILocalVariable::get(VMContext, Scope, Name, File, LineNo, Ty, 803 ArgNo, Flags, AlignInBits, Annotations); 804 if (AlwaysPreserve) { 805 // The optimizer may remove local variables. If there is an interest 806 // to preserve variable info in such situation then stash it in a 807 // named mdnode. 808 PreservedNodes.emplace_back(Node); 809 } 810 return Node; 811 } 812 813 DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name, 814 DIFile *File, unsigned LineNo, 815 DIType *Ty, bool AlwaysPreserve, 816 DINode::DIFlags Flags, 817 uint32_t AlignInBits) { 818 assert(Scope && isa<DILocalScope>(Scope) && 819 "Unexpected scope for a local variable."); 820 return createLocalVariable( 821 VMContext, getSubprogramNodesTrackingVector(Scope), Scope, Name, 822 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve, Flags, AlignInBits); 823 } 824 825 DILocalVariable *DIBuilder::createParameterVariable( 826 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File, 827 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags, 828 DINodeArray Annotations) { 829 assert(ArgNo && "Expected non-zero argument number for parameter"); 830 assert(Scope && isa<DILocalScope>(Scope) && 831 "Unexpected scope for a local variable."); 832 return createLocalVariable( 833 VMContext, getSubprogramNodesTrackingVector(Scope), Scope, Name, ArgNo, 834 File, LineNo, Ty, AlwaysPreserve, Flags, /*AlignInBits=*/0, Annotations); 835 } 836 837 DILabel *DIBuilder::createLabel(DIScope *Context, StringRef Name, DIFile *File, 838 unsigned LineNo, bool AlwaysPreserve) { 839 auto *Scope = cast<DILocalScope>(Context); 840 auto *Node = DILabel::get(VMContext, Scope, Name, File, LineNo); 841 842 if (AlwaysPreserve) { 843 /// The optimizer may remove labels. If there is an interest 844 /// to preserve label info in such situation then append it to 845 /// the list of retained nodes of the DISubprogram. 846 getSubprogramNodesTrackingVector(Scope).emplace_back(Node); 847 } 848 return Node; 849 } 850 851 DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) { 852 return DIExpression::get(VMContext, Addr); 853 } 854 855 template <class... Ts> 856 static DISubprogram *getSubprogram(bool IsDistinct, Ts &&...Args) { 857 if (IsDistinct) 858 return DISubprogram::getDistinct(std::forward<Ts>(Args)...); 859 return DISubprogram::get(std::forward<Ts>(Args)...); 860 } 861 862 DISubprogram *DIBuilder::createFunction( 863 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File, 864 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine, 865 DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags, 866 DITemplateParameterArray TParams, DISubprogram *Decl, 867 DITypeArray ThrownTypes, DINodeArray Annotations, 868 StringRef TargetFuncName) { 869 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition; 870 auto *Node = getSubprogram( 871 /*IsDistinct=*/IsDefinition, VMContext, getNonCompileUnitScope(Context), 872 Name, LinkageName, File, LineNo, Ty, ScopeLine, nullptr, 0, 0, Flags, 873 SPFlags, IsDefinition ? CUNode : nullptr, TParams, Decl, nullptr, 874 ThrownTypes, Annotations, TargetFuncName); 875 876 if (IsDefinition) 877 AllSubprograms.push_back(Node); 878 trackIfUnresolved(Node); 879 return Node; 880 } 881 882 DISubprogram *DIBuilder::createTempFunctionFwdDecl( 883 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File, 884 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine, 885 DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags, 886 DITemplateParameterArray TParams, DISubprogram *Decl, 887 DITypeArray ThrownTypes) { 888 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition; 889 return DISubprogram::getTemporary(VMContext, getNonCompileUnitScope(Context), 890 Name, LinkageName, File, LineNo, Ty, 891 ScopeLine, nullptr, 0, 0, Flags, SPFlags, 892 IsDefinition ? CUNode : nullptr, TParams, 893 Decl, nullptr, ThrownTypes) 894 .release(); 895 } 896 897 DISubprogram *DIBuilder::createMethod( 898 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F, 899 unsigned LineNo, DISubroutineType *Ty, unsigned VIndex, int ThisAdjustment, 900 DIType *VTableHolder, DINode::DIFlags Flags, 901 DISubprogram::DISPFlags SPFlags, DITemplateParameterArray TParams, 902 DITypeArray ThrownTypes) { 903 assert(getNonCompileUnitScope(Context) && 904 "Methods should have both a Context and a context that isn't " 905 "the compile unit."); 906 // FIXME: Do we want to use different scope/lines? 907 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition; 908 auto *SP = getSubprogram( 909 /*IsDistinct=*/IsDefinition, VMContext, cast<DIScope>(Context), Name, 910 LinkageName, F, LineNo, Ty, LineNo, VTableHolder, VIndex, ThisAdjustment, 911 Flags, SPFlags, IsDefinition ? CUNode : nullptr, TParams, nullptr, 912 nullptr, ThrownTypes); 913 914 if (IsDefinition) 915 AllSubprograms.push_back(SP); 916 trackIfUnresolved(SP); 917 return SP; 918 } 919 920 DICommonBlock *DIBuilder::createCommonBlock(DIScope *Scope, 921 DIGlobalVariable *Decl, 922 StringRef Name, DIFile *File, 923 unsigned LineNo) { 924 return DICommonBlock::get(VMContext, Scope, Decl, Name, File, LineNo); 925 } 926 927 DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name, 928 bool ExportSymbols) { 929 930 // It is okay to *not* make anonymous top-level namespaces distinct, because 931 // all nodes that have an anonymous namespace as their parent scope are 932 // guaranteed to be unique and/or are linked to their containing 933 // DICompileUnit. This decision is an explicit tradeoff of link time versus 934 // memory usage versus code simplicity and may get revisited in the future. 935 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), Name, 936 ExportSymbols); 937 } 938 939 DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name, 940 StringRef ConfigurationMacros, 941 StringRef IncludePath, StringRef APINotesFile, 942 DIFile *File, unsigned LineNo, bool IsDecl) { 943 return DIModule::get(VMContext, File, getNonCompileUnitScope(Scope), Name, 944 ConfigurationMacros, IncludePath, APINotesFile, LineNo, 945 IsDecl); 946 } 947 948 DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope, 949 DIFile *File, 950 unsigned Discriminator) { 951 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator); 952 } 953 954 DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File, 955 unsigned Line, unsigned Col) { 956 // Make these distinct, to avoid merging two lexical blocks on the same 957 // file/line/column. 958 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope), 959 File, Line, Col); 960 } 961 962 DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, 963 DIExpression *Expr, const DILocation *DL, 964 Instruction *InsertBefore) { 965 return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(), 966 InsertBefore); 967 } 968 969 DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, 970 DIExpression *Expr, const DILocation *DL, 971 BasicBlock *InsertAtEnd) { 972 // If this block already has a terminator then insert this intrinsic before 973 // the terminator. Otherwise, put it at the end of the block. 974 Instruction *InsertBefore = InsertAtEnd->getTerminator(); 975 return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore); 976 } 977 978 DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val, 979 DILocalVariable *SrcVar, 980 DIExpression *ValExpr, Value *Addr, 981 DIExpression *AddrExpr, 982 const DILocation *DL) { 983 auto *Link = cast_or_null<DIAssignID>( 984 LinkedInstr->getMetadata(LLVMContext::MD_DIAssignID)); 985 assert(Link && "Linked instruction must have DIAssign metadata attached"); 986 987 if (M.IsNewDbgInfoFormat) { 988 DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign( 989 Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL); 990 BasicBlock *InsertBB = LinkedInstr->getParent(); 991 // Insert after LinkedInstr. 992 BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator()); 993 Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt; 994 insertDbgVariableRecord(DVR, InsertBB, InsertBefore, true); 995 return DVR; 996 } 997 998 LLVMContext &Ctx = LinkedInstr->getContext(); 999 Module *M = LinkedInstr->getModule(); 1000 if (!AssignFn) 1001 AssignFn = Intrinsic::getOrInsertDeclaration(M, Intrinsic::dbg_assign); 1002 1003 std::array<Value *, 6> Args = { 1004 MetadataAsValue::get(Ctx, ValueAsMetadata::get(Val)), 1005 MetadataAsValue::get(Ctx, SrcVar), 1006 MetadataAsValue::get(Ctx, ValExpr), 1007 MetadataAsValue::get(Ctx, Link), 1008 MetadataAsValue::get(Ctx, ValueAsMetadata::get(Addr)), 1009 MetadataAsValue::get(Ctx, AddrExpr), 1010 }; 1011 1012 IRBuilder<> B(Ctx); 1013 B.SetCurrentDebugLocation(DL); 1014 1015 auto *DVI = cast<DbgAssignIntrinsic>(B.CreateCall(AssignFn, Args)); 1016 DVI->insertAfter(LinkedInstr->getIterator()); 1017 return DVI; 1018 } 1019 1020 DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, 1021 Instruction *InsertBefore) { 1022 return insertLabel(LabelInfo, DL, 1023 InsertBefore ? InsertBefore->getParent() : nullptr, 1024 InsertBefore); 1025 } 1026 1027 DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, 1028 BasicBlock *InsertAtEnd) { 1029 return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr); 1030 } 1031 1032 DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V, 1033 DILocalVariable *VarInfo, 1034 DIExpression *Expr, 1035 const DILocation *DL, 1036 Instruction *InsertBefore) { 1037 DbgInstPtr DVI = insertDbgValueIntrinsic( 1038 V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr, 1039 InsertBefore); 1040 if (auto *Inst = dyn_cast<Instruction *>(DVI)) 1041 cast<CallInst>(Inst)->setTailCall(); 1042 return DVI; 1043 } 1044 1045 DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V, 1046 DILocalVariable *VarInfo, 1047 DIExpression *Expr, 1048 const DILocation *DL, 1049 BasicBlock *InsertAtEnd) { 1050 return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr); 1051 } 1052 1053 /// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics. 1054 /// This abstracts over the various ways to specify an insert position. 1055 static void initIRBuilder(IRBuilder<> &Builder, const DILocation *DL, 1056 BasicBlock *InsertBB, Instruction *InsertBefore) { 1057 if (InsertBefore) 1058 Builder.SetInsertPoint(InsertBefore); 1059 else if (InsertBB) 1060 Builder.SetInsertPoint(InsertBB); 1061 Builder.SetCurrentDebugLocation(DL); 1062 } 1063 1064 static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) { 1065 assert(V && "no value passed to dbg intrinsic"); 1066 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V)); 1067 } 1068 1069 static Function *getDeclareIntrin(Module &M) { 1070 return Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_declare); 1071 } 1072 1073 DbgInstPtr DIBuilder::insertDbgValueIntrinsic( 1074 llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr, 1075 const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) { 1076 if (M.IsNewDbgInfoFormat) { 1077 DbgVariableRecord *DVR = 1078 DbgVariableRecord::createDbgVariableRecord(Val, VarInfo, Expr, DL); 1079 insertDbgVariableRecord(DVR, InsertBB, InsertBefore); 1080 return DVR; 1081 } 1082 1083 if (!ValueFn) 1084 ValueFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_value); 1085 return insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertBB, 1086 InsertBefore); 1087 } 1088 1089 DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, 1090 DIExpression *Expr, const DILocation *DL, 1091 BasicBlock *InsertBB, 1092 Instruction *InsertBefore) { 1093 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare"); 1094 assert(DL && "Expected debug loc"); 1095 assert(DL->getScope()->getSubprogram() == 1096 VarInfo->getScope()->getSubprogram() && 1097 "Expected matching subprograms"); 1098 1099 if (M.IsNewDbgInfoFormat) { 1100 DbgVariableRecord *DVR = 1101 DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL); 1102 insertDbgVariableRecord(DVR, InsertBB, InsertBefore); 1103 return DVR; 1104 } 1105 1106 if (!DeclareFn) 1107 DeclareFn = getDeclareIntrin(M); 1108 1109 trackIfUnresolved(VarInfo); 1110 trackIfUnresolved(Expr); 1111 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage), 1112 MetadataAsValue::get(VMContext, VarInfo), 1113 MetadataAsValue::get(VMContext, Expr)}; 1114 1115 IRBuilder<> B(DL->getContext()); 1116 initIRBuilder(B, DL, InsertBB, InsertBefore); 1117 return B.CreateCall(DeclareFn, Args); 1118 } 1119 1120 void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR, 1121 BasicBlock *InsertBB, 1122 Instruction *InsertBefore, 1123 bool InsertAtHead) { 1124 assert(InsertBefore || InsertBB); 1125 trackIfUnresolved(DVR->getVariable()); 1126 trackIfUnresolved(DVR->getExpression()); 1127 if (DVR->isDbgAssign()) 1128 trackIfUnresolved(DVR->getAddressExpression()); 1129 1130 BasicBlock::iterator InsertPt; 1131 if (InsertBB && InsertBefore) 1132 InsertPt = InsertBefore->getIterator(); 1133 else if (InsertBB) 1134 InsertPt = InsertBB->end(); 1135 InsertPt.setHeadBit(InsertAtHead); 1136 InsertBB->insertDbgRecordBefore(DVR, InsertPt); 1137 } 1138 1139 Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn, 1140 Value *V, DILocalVariable *VarInfo, 1141 DIExpression *Expr, 1142 const DILocation *DL, 1143 BasicBlock *InsertBB, 1144 Instruction *InsertBefore) { 1145 assert(IntrinsicFn && "must pass a non-null intrinsic function"); 1146 assert(V && "must pass a value to a dbg intrinsic"); 1147 assert(VarInfo && 1148 "empty or invalid DILocalVariable* passed to debug intrinsic"); 1149 assert(DL && "Expected debug loc"); 1150 assert(DL->getScope()->getSubprogram() == 1151 VarInfo->getScope()->getSubprogram() && 1152 "Expected matching subprograms"); 1153 1154 trackIfUnresolved(VarInfo); 1155 trackIfUnresolved(Expr); 1156 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V), 1157 MetadataAsValue::get(VMContext, VarInfo), 1158 MetadataAsValue::get(VMContext, Expr)}; 1159 1160 IRBuilder<> B(DL->getContext()); 1161 initIRBuilder(B, DL, InsertBB, InsertBefore); 1162 return B.CreateCall(IntrinsicFn, Args); 1163 } 1164 1165 DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, 1166 BasicBlock *InsertBB, 1167 Instruction *InsertBefore) { 1168 assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label"); 1169 assert(DL && "Expected debug loc"); 1170 assert(DL->getScope()->getSubprogram() == 1171 LabelInfo->getScope()->getSubprogram() && 1172 "Expected matching subprograms"); 1173 1174 trackIfUnresolved(LabelInfo); 1175 if (M.IsNewDbgInfoFormat) { 1176 DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL); 1177 if (InsertBB && InsertBefore) 1178 InsertBB->insertDbgRecordBefore(DLR, InsertBefore->getIterator()); 1179 else if (InsertBB) 1180 InsertBB->insertDbgRecordBefore(DLR, InsertBB->end()); 1181 return DLR; 1182 } 1183 1184 if (!LabelFn) 1185 LabelFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_label); 1186 1187 Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)}; 1188 1189 IRBuilder<> B(DL->getContext()); 1190 initIRBuilder(B, DL, InsertBB, InsertBefore); 1191 return B.CreateCall(LabelFn, Args); 1192 } 1193 1194 void DIBuilder::replaceVTableHolder(DICompositeType *&T, DIType *VTableHolder) { 1195 { 1196 TypedTrackingMDRef<DICompositeType> N(T); 1197 N->replaceVTableHolder(VTableHolder); 1198 T = N.get(); 1199 } 1200 1201 // If this didn't create a self-reference, just return. 1202 if (T != VTableHolder) 1203 return; 1204 1205 // Look for unresolved operands. T will drop RAUW support, orphaning any 1206 // cycles underneath it. 1207 if (T->isResolved()) 1208 for (const MDOperand &O : T->operands()) 1209 if (auto *N = dyn_cast_or_null<MDNode>(O)) 1210 trackIfUnresolved(N); 1211 } 1212 1213 void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements, 1214 DINodeArray TParams) { 1215 { 1216 TypedTrackingMDRef<DICompositeType> N(T); 1217 if (Elements) 1218 N->replaceElements(Elements); 1219 if (TParams) 1220 N->replaceTemplateParams(DITemplateParameterArray(TParams)); 1221 T = N.get(); 1222 } 1223 1224 // If T isn't resolved, there's no problem. 1225 if (!T->isResolved()) 1226 return; 1227 1228 // If T is resolved, it may be due to a self-reference cycle. Track the 1229 // arrays explicitly if they're unresolved, or else the cycles will be 1230 // orphaned. 1231 if (Elements) 1232 trackIfUnresolved(Elements.get()); 1233 if (TParams) 1234 trackIfUnresolved(TParams.get()); 1235 } 1236