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