comparison lib/IR/DIBuilder.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===// 1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file implements the DIBuilder. 9 // This file implements the DIBuilder.
11 // 10 //
12 //===----------------------------------------------------------------------===// 11 //===----------------------------------------------------------------------===//
13 12
14 #include "llvm/IR/DIBuilder.h" 13 #include "llvm/IR/DIBuilder.h"
15 #include "llvm/IR/IRBuilder.h" 14 #include "llvm/IR/IRBuilder.h"
16 #include "LLVMContextImpl.h" 15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/BinaryFormat/Dwarf.h" 18 #include "llvm/BinaryFormat/Dwarf.h"
19 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DebugInfo.h" 20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/IntrinsicInst.h" 21 #include "llvm/IR/IntrinsicInst.h"
30 llvm::cl::desc("Use llvm.dbg.addr for all local variables"), 30 llvm::cl::desc("Use llvm.dbg.addr for all local variables"),
31 cl::init(false), cl::Hidden); 31 cl::init(false), cl::Hidden);
32 32
33 DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes, DICompileUnit *CU) 33 DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes, DICompileUnit *CU)
34 : M(m), VMContext(M.getContext()), CUNode(CU), 34 : M(m), VMContext(M.getContext()), CUNode(CU),
35 DeclareFn(nullptr), ValueFn(nullptr), 35 DeclareFn(nullptr), ValueFn(nullptr), LabelFn(nullptr),
36 AllowUnresolvedNodes(AllowUnresolvedNodes) {} 36 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
37 37
38 void DIBuilder::trackIfUnresolved(MDNode *N) { 38 void DIBuilder::trackIfUnresolved(MDNode *N) {
39 if (!N) 39 if (!N)
40 return; 40 return;
44 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes"); 44 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
45 UnresolvedNodes.emplace_back(N); 45 UnresolvedNodes.emplace_back(N);
46 } 46 }
47 47
48 void DIBuilder::finalizeSubprogram(DISubprogram *SP) { 48 void DIBuilder::finalizeSubprogram(DISubprogram *SP) {
49 MDTuple *Temp = SP->getVariables().get(); 49 MDTuple *Temp = SP->getRetainedNodes().get();
50 if (!Temp || !Temp->isTemporary()) 50 if (!Temp || !Temp->isTemporary())
51 return; 51 return;
52 52
53 SmallVector<Metadata *, 4> Variables; 53 SmallVector<Metadata *, 16> RetainedNodes;
54 54
55 auto PV = PreservedVariables.find(SP); 55 auto PV = PreservedVariables.find(SP);
56 if (PV != PreservedVariables.end()) 56 if (PV != PreservedVariables.end())
57 Variables.append(PV->second.begin(), PV->second.end()); 57 RetainedNodes.append(PV->second.begin(), PV->second.end());
58 58
59 DINodeArray AV = getOrCreateArray(Variables); 59 auto PL = PreservedLabels.find(SP);
60 TempMDTuple(Temp)->replaceAllUsesWith(AV.get()); 60 if (PL != PreservedLabels.end())
61 RetainedNodes.append(PL->second.begin(), PL->second.end());
62
63 DINodeArray Node = getOrCreateArray(RetainedNodes);
64
65 TempMDTuple(Temp)->replaceAllUsesWith(Node.get());
61 } 66 }
62 67
63 void DIBuilder::finalize() { 68 void DIBuilder::finalize() {
64 if (!CUNode) { 69 if (!CUNode) {
65 assert(!AllowUnresolvedNodes && 70 assert(!AllowUnresolvedNodes &&
131 136
132 DICompileUnit *DIBuilder::createCompileUnit( 137 DICompileUnit *DIBuilder::createCompileUnit(
133 unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized, 138 unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized,
134 StringRef Flags, unsigned RunTimeVer, StringRef SplitName, 139 StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
135 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId, 140 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId,
136 bool SplitDebugInlining, bool DebugInfoForProfiling, bool GnuPubnames) { 141 bool SplitDebugInlining, bool DebugInfoForProfiling,
142 DICompileUnit::DebugNameTableKind NameTableKind, bool RangesBaseAddress) {
137 143
138 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) || 144 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
139 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) && 145 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
140 "Invalid Language tag"); 146 "Invalid Language tag");
141 147
142 assert(!CUNode && "Can only make one compile unit per DIBuilder instance"); 148 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
143 CUNode = DICompileUnit::getDistinct( 149 CUNode = DICompileUnit::getDistinct(
144 VMContext, Lang, File, Producer, isOptimized, Flags, RunTimeVer, 150 VMContext, Lang, File, Producer, isOptimized, Flags, RunTimeVer,
145 SplitName, Kind, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId, 151 SplitName, Kind, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId,
146 SplitDebugInlining, DebugInfoForProfiling, GnuPubnames); 152 SplitDebugInlining, DebugInfoForProfiling, NameTableKind,
153 RangesBaseAddress);
147 154
148 // Create a named metadata so that it is easier to find cu in a module. 155 // Create a named metadata so that it is easier to find cu in a module.
149 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); 156 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
150 NMD->addOperand(CUNode); 157 NMD->addOperand(CUNode);
151 trackIfUnresolved(CUNode); 158 trackIfUnresolved(CUNode);
157 Metadata *NS, DIFile *File, unsigned Line, StringRef Name, 164 Metadata *NS, DIFile *File, unsigned Line, StringRef Name,
158 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) { 165 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
159 if (Line) 166 if (Line)
160 assert(File && "Source location has line number but no file"); 167 assert(File && "Source location has line number but no file");
161 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size(); 168 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
162 auto *M = 169 auto *M = DIImportedEntity::get(C, Tag, Context, cast_or_null<DINode>(NS),
163 DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), File, Line, Name); 170 File, Line, Name);
164 if (EntitiesCount < C.pImpl->DIImportedEntitys.size()) 171 if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
165 // A new Imported Entity was just added to the context. 172 // A new Imported Entity was just added to the context.
166 // Add it to the Imported Modules list. 173 // Add it to the Imported Modules list.
167 AllImportedModules.emplace_back(M); 174 AllImportedModules.emplace_back(M);
168 return M; 175 return M;
202 Context, Decl, File, Line, Name, 209 Context, Decl, File, Line, Name,
203 AllImportedModules); 210 AllImportedModules);
204 } 211 }
205 212
206 DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory, 213 DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory,
207 Optional<DIFile::ChecksumInfo<StringRef>> CS) { 214 Optional<DIFile::ChecksumInfo<StringRef>> CS,
208 return DIFile::get(VMContext, Filename, Directory, CS); 215 Optional<StringRef> Source) {
216 return DIFile::get(VMContext, Filename, Directory, CS, Source);
209 } 217 }
210 218
211 DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber, 219 DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
212 unsigned MacroType, StringRef Name, 220 unsigned MacroType, StringRef Name,
213 StringRef Value) { 221 StringRef Value) {
247 DIBasicType *DIBuilder::createNullPtrType() { 255 DIBasicType *DIBuilder::createNullPtrType() {
248 return createUnspecifiedType("decltype(nullptr)"); 256 return createUnspecifiedType("decltype(nullptr)");
249 } 257 }
250 258
251 DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits, 259 DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
252 unsigned Encoding) { 260 unsigned Encoding,
261 DINode::DIFlags Flags) {
253 assert(!Name.empty() && "Unable to create type without name"); 262 assert(!Name.empty() && "Unable to create type without name");
254 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits, 263 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
255 0, Encoding); 264 0, Encoding, Flags);
256 } 265 }
257 266
258 DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) { 267 DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
259 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0, 268 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
260 0, 0, None, DINode::FlagZero); 269 0, 0, None, DINode::FlagZero);
309 FriendTy, 0, 0, 0, None, DINode::FlagZero); 318 FriendTy, 0, 0, 0, None, DINode::FlagZero);
310 } 319 }
311 320
312 DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy, 321 DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
313 uint64_t BaseOffset, 322 uint64_t BaseOffset,
323 uint32_t VBPtrOffset,
314 DINode::DIFlags Flags) { 324 DINode::DIFlags Flags) {
315 assert(Ty && "Unable to create inheritance"); 325 assert(Ty && "Unable to create inheritance");
326 Metadata *ExtraData = ConstantAsMetadata::get(
327 ConstantInt::get(IntegerType::get(VMContext, 32), VBPtrOffset));
316 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr, 328 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
317 0, Ty, BaseTy, 0, 0, BaseOffset, None, Flags); 329 0, Ty, BaseTy, 0, 0, BaseOffset, None,
330 Flags, ExtraData);
318 } 331 }
319 332
320 DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name, 333 DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
321 DIFile *File, unsigned LineNumber, 334 DIFile *File, unsigned LineNumber,
322 uint64_t SizeInBits, 335 uint64_t SizeInBits,
332 if (C) 345 if (C)
333 return ConstantAsMetadata::get(C); 346 return ConstantAsMetadata::get(C);
334 return nullptr; 347 return nullptr;
335 } 348 }
336 349
337 DIDerivedType *DIBuilder::createVariantMemberType(DIScope *Scope, StringRef Name, 350 DIDerivedType *DIBuilder::createVariantMemberType(
338 DIFile *File, unsigned LineNumber, 351 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
339 uint64_t SizeInBits, 352 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
340 uint32_t AlignInBits, 353 Constant *Discriminant, DINode::DIFlags Flags, DIType *Ty) {
341 uint64_t OffsetInBits,
342 Constant *Discriminant,
343 DINode::DIFlags Flags, DIType *Ty) {
344 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File, 354 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
345 LineNumber, getNonCompileUnitScope(Scope), Ty, 355 LineNumber, getNonCompileUnitScope(Scope), Ty,
346 SizeInBits, AlignInBits, OffsetInBits, None, Flags, 356 SizeInBits, AlignInBits, OffsetInBits, None, Flags,
347 getConstantOrNull(Discriminant)); 357 getConstantOrNull(Discriminant));
348 } 358 }
491 } 501 }
492 502
493 DICompositeType *DIBuilder::createEnumerationType( 503 DICompositeType *DIBuilder::createEnumerationType(
494 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 504 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
495 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements, 505 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
496 DIType *UnderlyingType, StringRef UniqueIdentifier, bool IsFixed) { 506 DIType *UnderlyingType, StringRef UniqueIdentifier, bool IsScoped) {
497 auto *CTy = DICompositeType::get( 507 auto *CTy = DICompositeType::get(
498 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber, 508 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
499 getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0, 509 getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0,
500 IsFixed ? DINode::FlagFixedEnum : DINode::FlagZero, Elements, 0, nullptr, 510 IsScoped ? DINode::FlagEnumClass : DINode::FlagZero, Elements, 0, nullptr,
501 nullptr, UniqueIdentifier); 511 nullptr, UniqueIdentifier);
502 AllEnumTypes.push_back(CTy); 512 AllEnumTypes.push_back(CTy);
503 trackIfUnresolved(CTy); 513 trackIfUnresolved(CTy);
504 return CTy; 514 return CTy;
505 } 515 }
522 DINode::FlagVector, Subscripts, 0, nullptr); 532 DINode::FlagVector, Subscripts, 0, nullptr);
523 trackIfUnresolved(R); 533 trackIfUnresolved(R);
524 return R; 534 return R;
525 } 535 }
526 536
527 static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty, 537 DISubprogram *DIBuilder::createArtificialSubprogram(DISubprogram *SP) {
538 auto NewSP = SP->cloneWithFlags(SP->getFlags() | DINode::FlagArtificial);
539 return MDNode::replaceWithDistinct(std::move(NewSP));
540 }
541
542 static DIType *createTypeWithFlags(const DIType *Ty,
528 DINode::DIFlags FlagsToSet) { 543 DINode::DIFlags FlagsToSet) {
529 auto NewTy = Ty->clone(); 544 auto NewTy = Ty->cloneWithFlags(Ty->getFlags() | FlagsToSet);
530 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
531 return MDNode::replaceWithUniqued(std::move(NewTy)); 545 return MDNode::replaceWithUniqued(std::move(NewTy));
532 } 546 }
533 547
534 DIType *DIBuilder::createArtificialType(DIType *Ty) { 548 DIType *DIBuilder::createArtificialType(DIType *Ty) {
535 // FIXME: Restrict this to the nodes where it's valid. 549 // FIXME: Restrict this to the nodes where it's valid.
536 if (Ty->isArtificial()) 550 if (Ty->isArtificial())
537 return Ty; 551 return Ty;
538 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial); 552 return createTypeWithFlags(Ty, DINode::FlagArtificial);
539 } 553 }
540 554
541 DIType *DIBuilder::createObjectPointerType(DIType *Ty) { 555 DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
542 // FIXME: Restrict this to the nodes where it's valid. 556 // FIXME: Restrict this to the nodes where it's valid.
543 if (Ty->isObjectPointer()) 557 if (Ty->isObjectPointer())
544 return Ty; 558 return Ty;
545 DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial; 559 DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
546 return createTypeWithFlags(VMContext, Ty, Flags); 560 return createTypeWithFlags(Ty, Flags);
547 } 561 }
548 562
549 void DIBuilder::retainType(DIScope *T) { 563 void DIBuilder::retainType(DIScope *T) {
550 assert(T && "Expected non-null type"); 564 assert(T && "Expected non-null type");
551 assert((isa<DIType>(T) || (isa<DISubprogram>(T) && 565 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
623 } 637 }
624 638
625 DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression( 639 DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression(
626 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F, 640 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
627 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, DIExpression *Expr, 641 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, DIExpression *Expr,
628 MDNode *Decl, uint32_t AlignInBits) { 642 MDNode *Decl, MDTuple *templateParams, uint32_t AlignInBits) {
629 checkGlobalVariableScope(Context); 643 checkGlobalVariableScope(Context);
630 644
631 auto *GV = DIGlobalVariable::getDistinct( 645 auto *GV = DIGlobalVariable::getDistinct(
632 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F, 646 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
633 LineNumber, Ty, isLocalToUnit, true, cast_or_null<DIDerivedType>(Decl), 647 LineNumber, Ty, isLocalToUnit, true, cast_or_null<DIDerivedType>(Decl),
634 AlignInBits); 648 templateParams, AlignInBits);
635 if (!Expr) 649 if (!Expr)
636 Expr = createExpression(); 650 Expr = createExpression();
637 auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr); 651 auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr);
638 AllGVs.push_back(N); 652 AllGVs.push_back(N);
639 return N; 653 return N;
640 } 654 }
641 655
642 DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl( 656 DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
643 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F, 657 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
644 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, MDNode *Decl, 658 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, MDNode *Decl,
645 uint32_t AlignInBits) { 659 MDTuple *templateParams, uint32_t AlignInBits) {
646 checkGlobalVariableScope(Context); 660 checkGlobalVariableScope(Context);
647 661
648 return DIGlobalVariable::getTemporary( 662 return DIGlobalVariable::getTemporary(
649 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F, 663 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
650 LineNumber, Ty, isLocalToUnit, false, 664 LineNumber, Ty, isLocalToUnit, false,
651 cast_or_null<DIDerivedType>(Decl), AlignInBits) 665 cast_or_null<DIDerivedType>(Decl), templateParams, AlignInBits)
652 .release(); 666 .release();
653 } 667 }
654 668
655 static DILocalVariable *createLocalVariable( 669 static DILocalVariable *createLocalVariable(
656 LLVMContext &VMContext, 670 LLVMContext &VMContext,
695 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo, 709 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
696 File, LineNo, Ty, AlwaysPreserve, Flags, 710 File, LineNo, Ty, AlwaysPreserve, Flags,
697 /* AlignInBits */0); 711 /* AlignInBits */0);
698 } 712 }
699 713
714 DILabel *DIBuilder::createLabel(
715 DIScope *Scope, StringRef Name, DIFile *File,
716 unsigned LineNo, bool AlwaysPreserve) {
717 DIScope *Context = getNonCompileUnitScope(Scope);
718
719 auto *Node =
720 DILabel::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
721 File, LineNo);
722
723 if (AlwaysPreserve) {
724 /// The optimizer may remove labels. If there is an interest
725 /// to preserve label info in such situation then append it to
726 /// the list of retained nodes of the DISubprogram.
727 DISubprogram *Fn = getDISubprogram(Scope);
728 assert(Fn && "Missing subprogram for label");
729 PreservedLabels[Fn].emplace_back(Node);
730 }
731 return Node;
732 }
733
700 DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) { 734 DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
701 return DIExpression::get(VMContext, Addr); 735 return DIExpression::get(VMContext, Addr);
702 } 736 }
703 737
704 DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) { 738 DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
714 return DISubprogram::get(std::forward<Ts>(Args)...); 748 return DISubprogram::get(std::forward<Ts>(Args)...);
715 } 749 }
716 750
717 DISubprogram *DIBuilder::createFunction( 751 DISubprogram *DIBuilder::createFunction(
718 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File, 752 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
719 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit, 753 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
720 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags, 754 DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags,
721 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl, 755 DITemplateParameterArray TParams, DISubprogram *Decl,
722 DITypeArray ThrownTypes) { 756 DITypeArray ThrownTypes) {
757 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
723 auto *Node = getSubprogram( 758 auto *Node = getSubprogram(
724 /* IsDistinct = */ isDefinition, VMContext, 759 /*IsDistinct=*/IsDefinition, VMContext, getNonCompileUnitScope(Context),
725 getNonCompileUnitScope(Context), Name, LinkageName, File, LineNo, Ty, 760 Name, LinkageName, File, LineNo, Ty, ScopeLine, nullptr, 0, 0, Flags,
726 isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, 0, Flags, 761 SPFlags, IsDefinition ? CUNode : nullptr, TParams, Decl,
727 isOptimized, isDefinition ? CUNode : nullptr, TParams, Decl,
728 MDTuple::getTemporary(VMContext, None).release(), ThrownTypes); 762 MDTuple::getTemporary(VMContext, None).release(), ThrownTypes);
729 763
730 if (isDefinition) 764 if (IsDefinition)
731 AllSubprograms.push_back(Node); 765 AllSubprograms.push_back(Node);
732 trackIfUnresolved(Node); 766 trackIfUnresolved(Node);
733 return Node; 767 return Node;
734 } 768 }
735 769
736 DISubprogram *DIBuilder::createTempFunctionFwdDecl( 770 DISubprogram *DIBuilder::createTempFunctionFwdDecl(
737 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File, 771 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
738 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit, 772 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
739 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags, 773 DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags,
740 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl, 774 DITemplateParameterArray TParams, DISubprogram *Decl,
741 DITypeArray ThrownTypes) { 775 DITypeArray ThrownTypes) {
742 return DISubprogram::getTemporary( 776 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
743 VMContext, getNonCompileUnitScope(Context), Name, LinkageName, 777 return DISubprogram::getTemporary(VMContext, getNonCompileUnitScope(Context),
744 File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr, 778 Name, LinkageName, File, LineNo, Ty,
745 0, 0, 0, Flags, isOptimized, isDefinition ? CUNode : nullptr, 779 ScopeLine, nullptr, 0, 0, Flags, SPFlags,
746 TParams, Decl, nullptr, ThrownTypes) 780 IsDefinition ? CUNode : nullptr, TParams,
781 Decl, nullptr, ThrownTypes)
747 .release(); 782 .release();
748 } 783 }
749 784
750 DISubprogram *DIBuilder::createMethod( 785 DISubprogram *DIBuilder::createMethod(
751 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F, 786 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
752 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit, 787 unsigned LineNo, DISubroutineType *Ty, unsigned VIndex, int ThisAdjustment,
753 bool isDefinition, unsigned VK, unsigned VIndex, int ThisAdjustment, 788 DIType *VTableHolder, DINode::DIFlags Flags,
754 DIType *VTableHolder, DINode::DIFlags Flags, bool isOptimized, 789 DISubprogram::DISPFlags SPFlags, DITemplateParameterArray TParams,
755 DITemplateParameterArray TParams, DITypeArray ThrownTypes) { 790 DITypeArray ThrownTypes) {
756 assert(getNonCompileUnitScope(Context) && 791 assert(getNonCompileUnitScope(Context) &&
757 "Methods should have both a Context and a context that isn't " 792 "Methods should have both a Context and a context that isn't "
758 "the compile unit."); 793 "the compile unit.");
759 // FIXME: Do we want to use different scope/lines? 794 // FIXME: Do we want to use different scope/lines?
795 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
760 auto *SP = getSubprogram( 796 auto *SP = getSubprogram(
761 /* IsDistinct = */ isDefinition, VMContext, cast<DIScope>(Context), Name, 797 /*IsDistinct=*/IsDefinition, VMContext, cast<DIScope>(Context), Name,
762 LinkageName, F, LineNo, Ty, isLocalToUnit, isDefinition, LineNo, 798 LinkageName, F, LineNo, Ty, LineNo, VTableHolder, VIndex, ThisAdjustment,
763 VTableHolder, VK, VIndex, ThisAdjustment, Flags, isOptimized, 799 Flags, SPFlags, IsDefinition ? CUNode : nullptr, TParams, nullptr,
764 isDefinition ? CUNode : nullptr, TParams, nullptr, nullptr, ThrownTypes); 800 nullptr, ThrownTypes);
765 801
766 if (isDefinition) 802 if (IsDefinition)
767 AllSubprograms.push_back(SP); 803 AllSubprograms.push_back(SP);
768 trackIfUnresolved(SP); 804 trackIfUnresolved(SP);
769 return SP; 805 return SP;
806 }
807
808 DICommonBlock *DIBuilder::createCommonBlock(
809 DIScope *Scope, DIGlobalVariable *Decl, StringRef Name, DIFile *File,
810 unsigned LineNo) {
811 return DICommonBlock::get(
812 VMContext, Scope, Decl, Name, File, LineNo);
770 } 813 }
771 814
772 DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name, 815 DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
773 bool ExportSymbols) { 816 bool ExportSymbols) {
774 817
815 BasicBlock *InsertAtEnd) { 858 BasicBlock *InsertAtEnd) {
816 // If this block already has a terminator then insert this intrinsic before 859 // If this block already has a terminator then insert this intrinsic before
817 // the terminator. Otherwise, put it at the end of the block. 860 // the terminator. Otherwise, put it at the end of the block.
818 Instruction *InsertBefore = InsertAtEnd->getTerminator(); 861 Instruction *InsertBefore = InsertAtEnd->getTerminator();
819 return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore); 862 return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
863 }
864
865 Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
866 Instruction *InsertBefore) {
867 return insertLabel(
868 LabelInfo, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
869 InsertBefore);
870 }
871
872 Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
873 BasicBlock *InsertAtEnd) {
874 return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr);
820 } 875 }
821 876
822 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, 877 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
823 DILocalVariable *VarInfo, 878 DILocalVariable *VarInfo,
824 DIExpression *Expr, 879 DIExpression *Expr,
902 957
903 IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore); 958 IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
904 return B.CreateCall(ValueFn, Args); 959 return B.CreateCall(ValueFn, Args);
905 } 960 }
906 961
962 Instruction *DIBuilder::insertLabel(
963 DILabel *LabelInfo, const DILocation *DL,
964 BasicBlock *InsertBB, Instruction *InsertBefore) {
965 assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
966 assert(DL && "Expected debug loc");
967 assert(DL->getScope()->getSubprogram() ==
968 LabelInfo->getScope()->getSubprogram() &&
969 "Expected matching subprograms");
970 if (!LabelFn)
971 LabelFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_label);
972
973 trackIfUnresolved(LabelInfo);
974 Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)};
975
976 IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
977 return B.CreateCall(LabelFn, Args);
978 }
979
907 void DIBuilder::replaceVTableHolder(DICompositeType *&T, 980 void DIBuilder::replaceVTableHolder(DICompositeType *&T,
908 DIType *VTableHolder) { 981 DIType *VTableHolder) {
909 { 982 {
910 TypedTrackingMDRef<DICompositeType> N(T); 983 TypedTrackingMDRef<DICompositeType> N(T);
911 N->replaceVTableHolder(VTableHolder); 984 N->replaceVTableHolder(VTableHolder);