blob: 7754ac03b43d23db81edaa9b58dc023e2c2d2673 [file] [log] [blame]
Devang Patel57c5a202010-11-04 15:01:38 +00001//===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DIBuilder.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth12664a02014-03-06 00:22:06 +000014#include "llvm/IR/DIBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000017#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/IR/Module.h"
Eric Christopher34164192012-04-03 00:43:49 +000020#include "llvm/Support/Debug.h"
Devang Patel57c5a202010-11-04 15:01:38 +000021#include "llvm/Support/Dwarf.h"
Amjad Aboud62f6f5c2016-03-13 11:11:39 +000022#include "LLVMContextImpl.h"
Devang Patel57c5a202010-11-04 15:01:38 +000023
24using namespace llvm;
25using namespace llvm::dwarf;
26
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000027DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes)
Adrian Prantl18c073a2015-07-02 22:32:52 +000028 : M(m), VMContext(M.getContext()), CUNode(nullptr),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000029 DeclareFn(nullptr), ValueFn(nullptr),
30 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
31
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000032void DIBuilder::trackIfUnresolved(MDNode *N) {
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000033 if (!N)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000034 return;
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000035 if (N->isResolved())
36 return;
37
38 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
39 UnresolvedNodes.emplace_back(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000040}
Devang Patel57c5a202010-11-04 15:01:38 +000041
Keno Fischer3cdd4932017-06-01 20:42:44 +000042void DIBuilder::finalizeSubprogram(DISubprogram *SP) {
43 MDTuple *Temp = SP->getVariables().get();
44 if (!Temp || !Temp->isTemporary())
45 return;
46
47 SmallVector<Metadata *, 4> Variables;
48
49 auto PV = PreservedVariables.find(SP);
50 if (PV != PreservedVariables.end())
51 Variables.append(PV->second.begin(), PV->second.end());
52
53 DINodeArray AV = getOrCreateArray(Variables);
54 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
55}
56
Devang Patel2b8acaf2011-08-15 23:00:00 +000057void DIBuilder::finalize() {
Adrian Prantl0e224a62015-07-06 16:22:12 +000058 if (!CUNode) {
59 assert(!AllowUnresolvedNodes &&
60 "creating type nodes without a CU is not supported");
61 return;
Devang Patel59e27c52011-08-19 23:28:12 +000062 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000063
Adrian Prantl0e224a62015-07-06 16:22:12 +000064 CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
65
66 SmallVector<Metadata *, 16> RetainValues;
67 // Declarations and definitions of the same type may be retained. Some
68 // clients RAUW these pairs, leaving duplicates in the retained types
69 // list. Use a set to remove the duplicates while we transform the
70 // TrackingVHs back into Values.
71 SmallPtrSet<Metadata *, 16> RetainSet;
72 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
73 if (RetainSet.insert(AllRetainTypes[I]).second)
74 RetainValues.push_back(AllRetainTypes[I]);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000075
76 if (!RetainValues.empty())
77 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
Adrian Prantl0e224a62015-07-06 16:22:12 +000078
79 DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
Adrian Prantl75819ae2016-04-15 15:57:41 +000080 for (auto *SP : SPs)
Keno Fischer3cdd4932017-06-01 20:42:44 +000081 finalizeSubprogram(SP);
Adrian Prantl75819ae2016-04-15 15:57:41 +000082 for (auto *N : RetainValues)
83 if (auto *SP = dyn_cast<DISubprogram>(N))
Keno Fischer3cdd4932017-06-01 20:42:44 +000084 finalizeSubprogram(SP);
Adrian Prantl0e224a62015-07-06 16:22:12 +000085
Adrian Prantl4276d4a2015-07-06 16:36:02 +000086 if (!AllGVs.empty())
87 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Adrian Prantl0e224a62015-07-06 16:22:12 +000088
Adrian Prantl4276d4a2015-07-06 16:36:02 +000089 if (!AllImportedModules.empty())
90 CUNode->replaceImportedEntities(MDTuple::get(
91 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
92 AllImportedModules.end())));
Adrian Prantl0e224a62015-07-06 16:22:12 +000093
Amjad Aboud96075712017-01-12 15:49:46 +000094 for (const auto &I : AllMacrosPerParent) {
95 // DIMacroNode's with nullptr parent are DICompileUnit direct children.
96 if (!I.first) {
97 CUNode->replaceMacros(MDTuple::get(VMContext, I.second.getArrayRef()));
98 continue;
99 }
100 // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
101 auto *TMF = cast<DIMacroFile>(I.first);
102 auto *MF = DIMacroFile::get(VMContext, dwarf::DW_MACINFO_start_file,
103 TMF->getLine(), TMF->getFile(),
104 getOrCreateMacroArray(I.second.getArrayRef()));
105 replaceTemporary(llvm::TempDIMacroNode(TMF), MF);
106 }
107
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000108 // Now that all temp nodes have been replaced or deleted, resolve remaining
109 // cycles.
110 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000111 if (N && !N->isResolved())
112 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000113 UnresolvedNodes.clear();
114
115 // Can't handle unresolved nodes anymore.
116 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000117}
118
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000119/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000120static DIScope *getNonCompileUnitScope(DIScope *N) {
121 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000122 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000123 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000124}
125
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000126DICompileUnit *DIBuilder::createCompileUnit(
Amjad Aboud43c8b6b2016-12-14 20:24:54 +0000127 unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized,
128 StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
David Blaikiea01f2952016-08-24 18:29:49 +0000129 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId,
Dehao Chen0944a8c2017-02-01 22:45:09 +0000130 bool SplitDebugInlining, bool DebugInfoForProfiling) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000131
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000132 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000133 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
134 "Invalid Language tag");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000135
Adrian Prantl18c073a2015-07-02 22:32:52 +0000136 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
137 CUNode = DICompileUnit::getDistinct(
Amjad Aboud43c8b6b2016-12-14 20:24:54 +0000138 VMContext, Lang, File, Producer, isOptimized, Flags, RunTimeVer,
139 SplitName, Kind, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId,
Dehao Chen0944a8c2017-02-01 22:45:09 +0000140 SplitDebugInlining, DebugInfoForProfiling);
Devang Patel09fa69e2011-05-03 16:18:28 +0000141
142 // Create a named metadata so that it is easier to find cu in a module.
Adrian Prantl5992a722016-04-08 22:43:03 +0000143 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
144 NMD->addOperand(CUNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000145 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000146 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000147}
148
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149static DIImportedEntity *
150createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000151 Metadata *NS, unsigned Line, StringRef Name,
152 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000153 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000154 auto *M = DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), Line, Name);
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000155 if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
156 // A new Imported Entity was just added to the context.
157 // Add it to the Imported Modules list.
158 AllImportedModules.emplace_back(M);
David Blaikie1fd43652013-05-07 21:35:53 +0000159 return M;
160}
161
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000162DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
163 DINamespace *NS,
164 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000165 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
166 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000167}
168
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000169DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
170 DIImportedEntity *NS,
171 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000172 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
173 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000174}
175
Adrian Prantlab1243f2015-06-29 23:03:47 +0000176DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
177 unsigned Line) {
178 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
179 Context, M, Line, StringRef(), AllImportedModules);
180}
181
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000182DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
183 DINode *Decl,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000184 unsigned Line,
185 StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000186 // Make sure to use the unique identifier based metadata reference for
187 // types that have one.
David Blaikie2a40c142014-04-06 06:29:01 +0000188 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000189 Context, Decl, Line, Name, AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000190}
191
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000192DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory,
193 DIFile::ChecksumKind CSKind, StringRef Checksum) {
194 return DIFile::get(VMContext, Filename, Directory, CSKind, Checksum);
Devang Patel57c5a202010-11-04 15:01:38 +0000195}
196
Amjad Aboud96075712017-01-12 15:49:46 +0000197DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
198 unsigned MacroType, StringRef Name,
199 StringRef Value) {
200 assert(!Name.empty() && "Unable to create macro without name");
201 assert((MacroType == dwarf::DW_MACINFO_undef ||
202 MacroType == dwarf::DW_MACINFO_define) &&
203 "Unexpected macro type");
204 auto *M = DIMacro::get(VMContext, MacroType, LineNumber, Name, Value);
205 AllMacrosPerParent[Parent].insert(M);
206 return M;
207}
208
209DIMacroFile *DIBuilder::createTempMacroFile(DIMacroFile *Parent,
210 unsigned LineNumber, DIFile *File) {
211 auto *MF = DIMacroFile::getTemporary(VMContext, dwarf::DW_MACINFO_start_file,
212 LineNumber, File, DIMacroNodeArray())
213 .release();
214 AllMacrosPerParent[Parent].insert(MF);
215 // Add the new temporary DIMacroFile to the macro per parent map as a parent.
216 // This is needed to assure DIMacroFile with no children to have an entry in
217 // the map. Otherwise, it will not be resolved in DIBuilder::finalize().
218 AllMacrosPerParent.insert({MF, {}});
219 return MF;
220}
221
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000222DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000223 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000224 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000225}
226
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000227DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000228 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000229 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000230}
231
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000232DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000233 return createUnspecifiedType("decltype(nullptr)");
234}
235
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000236DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000237 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000238 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000239 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000240 0, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000241}
242
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000244 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000245 0, 0, None, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000246}
247
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000248DIDerivedType *DIBuilder::createPointerType(
249 DIType *PointeeTy,
250 uint64_t SizeInBits,
251 uint32_t AlignInBits,
252 Optional<unsigned> DWARFAddressSpace,
253 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000254 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000255 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000256 nullptr, 0, nullptr, PointeeTy, SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000257 AlignInBits, 0, DWARFAddressSpace,
258 DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000259}
260
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000261DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
262 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000263 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000264 uint32_t AlignInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000265 DINode::DIFlags Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000266 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000267 nullptr, 0, nullptr, PointeeTy, SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000268 AlignInBits, 0, None, Flags, Base);
David Blaikie5d3249b2013-01-07 05:51:15 +0000269}
270
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000271DIDerivedType *DIBuilder::createReferenceType(
272 unsigned Tag, DIType *RTy,
273 uint64_t SizeInBits,
274 uint32_t AlignInBits,
275 Optional<unsigned> DWARFAddressSpace) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000276 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000277 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000278 SizeInBits, AlignInBits, 0, DWARFAddressSpace,
279 DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000280}
281
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000282DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
283 DIFile *File, unsigned LineNo,
284 DIScope *Context) {
285 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000286 LineNo, getNonCompileUnitScope(Context), Ty, 0, 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000287 0, None, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000288}
289
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000290DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000291 assert(Ty && "Invalid type!");
292 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000293 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000294 FriendTy, 0, 0, 0, None, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000295}
296
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000297DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000298 uint64_t BaseOffset,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000299 DINode::DIFlags Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000300 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000301 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000302 0, Ty, BaseTy, 0, 0, BaseOffset, None, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000303}
304
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000305DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
306 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000307 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000308 uint32_t AlignInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000309 uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000310 DINode::DIFlags Flags, DIType *Ty) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000311 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
312 LineNumber, getNonCompileUnitScope(Scope), Ty,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000313 SizeInBits, AlignInBits, OffsetInBits, None, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000314}
315
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000316static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000317 if (C)
318 return ConstantAsMetadata::get(C);
319 return nullptr;
320}
321
David Majnemer9319cbc2016-06-30 03:00:20 +0000322DIDerivedType *DIBuilder::createBitFieldMemberType(
323 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000324 uint64_t SizeInBits, uint64_t OffsetInBits, uint64_t StorageOffsetInBits,
325 DINode::DIFlags Flags, DIType *Ty) {
David Majnemer9319cbc2016-06-30 03:00:20 +0000326 Flags |= DINode::FlagBitField;
327 return DIDerivedType::get(
328 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000329 getNonCompileUnitScope(Scope), Ty, SizeInBits, /* AlignInBits */ 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000330 OffsetInBits, None, Flags,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000331 ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext, 64),
332 StorageOffsetInBits)));
David Majnemer9319cbc2016-06-30 03:00:20 +0000333}
334
Leny Kholodov40c62352016-09-06 17:03:02 +0000335DIDerivedType *
336DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
337 unsigned LineNumber, DIType *Ty,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000338 DINode::DIFlags Flags, llvm::Constant *Val,
339 uint32_t AlignInBits) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000340 Flags |= DINode::FlagStaticMember;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000341 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000342 LineNumber, getNonCompileUnitScope(Scope), Ty, 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000343 AlignInBits, 0, None, Flags,
344 getConstantOrNull(Val));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000345}
346
Leny Kholodov40c62352016-09-06 17:03:02 +0000347DIDerivedType *
348DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000349 uint64_t SizeInBits, uint32_t AlignInBits,
Leny Kholodov40c62352016-09-06 17:03:02 +0000350 uint64_t OffsetInBits, DINode::DIFlags Flags,
351 DIType *Ty, MDNode *PropertyNode) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000352 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
353 LineNumber, getNonCompileUnitScope(File), Ty,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000354 SizeInBits, AlignInBits, OffsetInBits, None, Flags,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000355 PropertyNode);
Devang Patel44882172012-02-06 17:49:43 +0000356}
357
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000358DIObjCProperty *
359DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
Eric Christopher98f9c232013-10-15 23:31:31 +0000360 StringRef GetterName, StringRef SetterName,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000361 unsigned PropertyAttributes, DIType *Ty) {
362 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000363 SetterName, PropertyAttributes, Ty);
Devang Patelcc481592012-02-04 00:59:25 +0000364}
365
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000366DITemplateTypeParameter *
367DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
368 DIType *Ty) {
369 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000370 return DITemplateTypeParameter::get(VMContext, Name, Ty);
Devang Patel3a9e65e2011-02-02 21:38:25 +0000371}
372
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000373static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000374createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000375 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000376 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000377 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000378 return DITemplateValueParameter::get(VMContext, Tag, Name, Ty, MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000379}
380
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000381DITemplateValueParameter *
382DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
383 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000384 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000385 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000386 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000387}
388
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389DITemplateValueParameter *
390DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
391 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000392 return createTemplateValueParameterHelper(
393 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000394 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000395}
396
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000397DITemplateValueParameter *
398DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
399 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000400 return createTemplateValueParameterHelper(
401 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000402 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000403}
404
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000405DICompositeType *DIBuilder::createClassType(
406 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000407 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000408 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000409 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
410 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000411 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000412
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000413 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000414 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000415 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits,
416 OffsetInBits, Flags, Elements, 0, VTableHolder,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000417 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000418 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000419 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000420}
421
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000422DICompositeType *DIBuilder::createStructType(
423 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000424 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000425 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
426 DIType *VTableHolder, StringRef UniqueIdentifier) {
427 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000428 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000429 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0,
430 Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000431 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000432 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000433}
434
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000435DICompositeType *DIBuilder::createUnionType(
436 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000437 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000438 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
439 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000440 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000441 getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
442 Elements, RunTimeLang, nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000443 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000444 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000445}
446
Eric Christopherbdafb3c2015-10-15 06:56:10 +0000447DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
Leny Kholodov40c62352016-09-06 17:03:02 +0000448 DINode::DIFlags Flags,
449 unsigned CC) {
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000450 return DISubroutineType::get(VMContext, Flags, CC, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000451}
452
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000453DICompositeType *DIBuilder::createEnumerationType(
454 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000455 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000456 DIType *UnderlyingType, StringRef UniqueIdentifier) {
457 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000458 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000459 getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000460 DINode::FlagZero, Elements, 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000461 AllEnumTypes.push_back(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000462 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000463 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000464}
465
Victor Leschuk197aa312016-10-18 14:31:22 +0000466DICompositeType *DIBuilder::createArrayType(uint64_t Size,
467 uint32_t AlignInBits, DIType *Ty,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000468 DINodeArray Subscripts) {
469 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000470 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000471 DINode::FlagZero, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000472 trackIfUnresolved(R);
473 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000474}
475
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000476DICompositeType *DIBuilder::createVectorType(uint64_t Size,
Victor Leschuk197aa312016-10-18 14:31:22 +0000477 uint32_t AlignInBits, DIType *Ty,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000478 DINodeArray Subscripts) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000479 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
480 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
481 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000482 trackIfUnresolved(R);
483 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000484}
Devang Patel746660f2010-12-07 23:25:47 +0000485
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000486static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000487 DINode::DIFlags FlagsToSet) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000488 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000489 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
490 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000491}
492
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000493DIType *DIBuilder::createArtificialType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000494 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000495 if (Ty->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000496 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000497 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000498}
Devang Patel746660f2010-12-07 23:25:47 +0000499
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000500DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000501 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000502 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000503 return Ty;
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000504 DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000505 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000506}
507
Adrian Prantl75819ae2016-04-15 15:57:41 +0000508void DIBuilder::retainType(DIScope *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000509 assert(T && "Expected non-null type");
Adrian Prantl75819ae2016-04-15 15:57:41 +0000510 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
511 cast<DISubprogram>(T)->isDefinition() == false)) &&
512 "Expected type or subprogram declaration");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000513 AllRetainTypes.emplace_back(T);
514}
Devang Patel89ea4f22010-12-08 01:50:15 +0000515
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000516DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000517
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000518DICompositeType *
519DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
520 DIFile *F, unsigned Line, unsigned RuntimeLang,
Victor Leschuk197aa312016-10-18 14:31:22 +0000521 uint64_t SizeInBits, uint32_t AlignInBits,
Eric Christopher98f9c232013-10-15 23:31:31 +0000522 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000523 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
524 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000525 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000526 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
527 SizeInBits, AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang,
528 nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000529 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000530 return RetTy;
531}
532
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000533DICompositeType *DIBuilder::createReplaceableCompositeType(
534 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
Victor Leschuk197aa312016-10-18 14:31:22 +0000535 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000536 DINode::DIFlags Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000537 auto *RetTy =
538 DICompositeType::getTemporary(
539 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
540 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang, nullptr,
541 nullptr, UniqueIdentifier)
542 .release();
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000543 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000544 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000545}
546
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000547DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000548 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000549}
550
Amjad Aboud96075712017-01-12 15:49:46 +0000551DIMacroNodeArray
552DIBuilder::getOrCreateMacroArray(ArrayRef<Metadata *> Elements) {
553 return MDTuple::get(VMContext, Elements);
554}
555
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000556DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000557 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000558 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
559 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000560 Elts.push_back(cast<DIType>(Elements[i]));
Manman Ren1a125c92014-07-28 19:33:20 +0000561 else
562 Elts.push_back(Elements[i]);
563 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000564 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000565}
566
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000567DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
568 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000569}
570
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000571static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000572#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000573 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000574 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000575 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000576 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000577#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000578}
579
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000580DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000581 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000582 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, DIExpression *Expr,
583 MDNode *Decl, uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000584 checkGlobalVariableScope(Context);
585
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000586 auto *GV = DIGlobalVariable::getDistinct(
Duncan P. N. Exon Smithc5225df2016-04-23 22:29:09 +0000587 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000588 LineNumber, Ty, isLocalToUnit, true, cast_or_null<DIDerivedType>(Decl),
589 AlignInBits);
590 auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000591 AllGVs.push_back(N);
592 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000593}
594
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000595DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
596 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000597 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, MDNode *Decl,
598 uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000599 checkGlobalVariableScope(Context);
600
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000601 return DIGlobalVariable::getTemporary(
602 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000603 LineNumber, Ty, isLocalToUnit, false,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000604 cast_or_null<DIDerivedType>(Decl), AlignInBits)
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000605 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000606}
607
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000608static DILocalVariable *createLocalVariable(
609 LLVMContext &VMContext,
Duncan P. N. Exon Smith3c406c22016-04-20 20:14:09 +0000610 DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> &PreservedVariables,
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000611 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000612 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
613 uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000614 // FIXME: Why getNonCompileUnitScope()?
615 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000616 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000617 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000618 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000619
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000620 auto *Node =
621 DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000622 File, LineNo, Ty, ArgNo, Flags, AlignInBits);
Devang Patel63f83cd2010-12-07 23:58:00 +0000623 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000624 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000625 // to preserve variable info in such situation then stash it in a
626 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000627 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000628 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000629 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000630 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000631 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000632}
633
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000634DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
635 DIFile *File, unsigned LineNo,
636 DIType *Ty, bool AlwaysPreserve,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000637 DINode::DIFlags Flags,
638 uint32_t AlignInBits) {
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000639 return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
640 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000641 Flags, AlignInBits);
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000642}
643
644DILocalVariable *DIBuilder::createParameterVariable(
645 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000646 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags) {
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000647 assert(ArgNo && "Expected non-zero argument number for parameter");
648 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000649 File, LineNo, Ty, AlwaysPreserve, Flags,
650 /* AlignInBits */0);
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000651}
652
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000653DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
654 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000655}
656
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000657DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000658 // TODO: Remove the callers of this signed version and delete.
659 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
660 return createExpression(Addr);
661}
662
Adrian Prantl941fa752016-12-05 18:04:47 +0000663DIExpression *DIBuilder::createFragmentExpression(unsigned OffsetInBytes,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000664 unsigned SizeInBytes) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000665 uint64_t Addr[] = {dwarf::DW_OP_LLVM_fragment, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000666 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000667}
668
Duncan P. N. Exon Smithb2df6472015-08-26 22:50:16 +0000669template <class... Ts>
670static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
671 if (IsDistinct)
672 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
673 return DISubprogram::get(std::forward<Ts>(Args)...);
674}
675
Peter Collingbourned4bff302015-11-05 22:03:56 +0000676DISubprogram *DIBuilder::createFunction(
677 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
678 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
Leny Kholodov40c62352016-09-06 17:03:02 +0000679 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000680 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl,
681 DITypeArray ThrownTypes) {
Adrian Prantl75819ae2016-04-15 15:57:41 +0000682 auto *Node = getSubprogram(
683 /* IsDistinct = */ isDefinition, VMContext,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000684 getNonCompileUnitScope(Context), Name, LinkageName, File, LineNo, Ty,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000685 isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, 0, Flags,
686 isOptimized, isDefinition ? CUNode : nullptr, TParams, Decl,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000687 MDTuple::getTemporary(VMContext, None).release(), ThrownTypes);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000688
689 if (isDefinition)
690 AllSubprograms.push_back(Node);
691 trackIfUnresolved(Node);
692 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000693}
694
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000695DISubprogram *DIBuilder::createTempFunctionFwdDecl(
696 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
697 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
Leny Kholodov40c62352016-09-06 17:03:02 +0000698 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000699 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl,
700 DITypeArray ThrownTypes) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000701 return DISubprogram::getTemporary(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000702 VMContext, getNonCompileUnitScope(Context), Name, LinkageName,
703 File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000704 0, 0, 0, Flags, isOptimized, isDefinition ? CUNode : nullptr,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000705 TParams, Decl, nullptr, ThrownTypes)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000706 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000707}
708
Adrian Prantl1d12b882017-04-26 22:56:44 +0000709DISubprogram *DIBuilder::createMethod(
710 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
711 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
712 bool isDefinition, unsigned VK, unsigned VIndex, int ThisAdjustment,
713 DIType *VTableHolder, DINode::DIFlags Flags, bool isOptimized,
714 DITemplateParameterArray TParams, DITypeArray ThrownTypes) {
Eric Christopher5cb56322013-10-15 23:31:36 +0000715 assert(getNonCompileUnitScope(Context) &&
716 "Methods should have both a Context and a context that isn't "
717 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000718 // FIXME: Do we want to use different scope/lines?
Peter Collingbourned4bff302015-11-05 22:03:56 +0000719 auto *SP = getSubprogram(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000720 /* IsDistinct = */ isDefinition, VMContext, cast<DIScope>(Context), Name,
721 LinkageName, F, LineNo, Ty, isLocalToUnit, isDefinition, LineNo,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000722 VTableHolder, VK, VIndex, ThisAdjustment, Flags, isOptimized,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000723 isDefinition ? CUNode : nullptr, TParams, nullptr, nullptr, ThrownTypes);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000724
David Blaikie595eb442013-02-18 07:10:22 +0000725 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000726 AllSubprograms.push_back(SP);
727 trackIfUnresolved(SP);
728 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000729}
730
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000731DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
Adrian Prantldbfda632016-11-03 19:42:02 +0000732 bool ExportSymbols) {
Adrian Prantlfed4f392017-04-28 22:25:46 +0000733
734 // It is okay to *not* make anonymous top-level namespaces distinct, because
735 // all nodes that have an anonymous namespace as their parent scope are
736 // guaranteed to be unique and/or are linked to their containing
737 // DICompileUnit. This decision is an explicit tradeoff of link time versus
738 // memory usage versus code simplicity and may get revisited in the future.
739 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), Name,
740 ExportSymbols);
Devang Patel746660f2010-12-07 23:25:47 +0000741}
742
Adrian Prantlab1243f2015-06-29 23:03:47 +0000743DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
744 StringRef ConfigurationMacros,
745 StringRef IncludePath,
746 StringRef ISysRoot) {
747 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
748 ConfigurationMacros, IncludePath, ISysRoot);
749}
750
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000751DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
752 DIFile *File,
753 unsigned Discriminator) {
754 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000755}
756
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000757DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
758 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000759 // Make these distinct, to avoid merging two lexical blocks on the same
760 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000761 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000762 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000763}
764
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000765static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
766 assert(V && "no value passed to dbg intrinsic");
767 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
768}
769
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000770static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
771 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000772 return I;
773}
774
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000775Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
776 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000777 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000778 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000779 assert(DL && "Expected debug loc");
780 assert(DL->getScope()->getSubprogram() ==
781 VarInfo->getScope()->getSubprogram() &&
782 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000783 if (!DeclareFn)
784 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
785
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000786 trackIfUnresolved(VarInfo);
787 trackIfUnresolved(Expr);
788 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
789 MetadataAsValue::get(VMContext, VarInfo),
790 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000791 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000792}
793
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000794Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
795 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000796 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000797 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000798 assert(DL && "Expected debug loc");
799 assert(DL->getScope()->getSubprogram() ==
800 VarInfo->getScope()->getSubprogram() &&
801 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000802 if (!DeclareFn)
803 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
804
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000805 trackIfUnresolved(VarInfo);
806 trackIfUnresolved(Expr);
807 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
808 MetadataAsValue::get(VMContext, VarInfo),
809 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000810
811 // If this block already has a terminator then insert this intrinsic
812 // before the terminator.
813 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000814 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
815 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000816}
817
Devang Patel9b412732011-02-22 18:56:12 +0000818Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000819 DILocalVariable *VarInfo,
820 DIExpression *Expr,
821 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000822 Instruction *InsertBefore) {
823 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000824 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000825 assert(DL && "Expected debug loc");
826 assert(DL->getScope()->getSubprogram() ==
827 VarInfo->getScope()->getSubprogram() &&
828 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000829 if (!ValueFn)
830 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
831
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000832 trackIfUnresolved(VarInfo);
833 trackIfUnresolved(Expr);
834 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
835 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
836 MetadataAsValue::get(VMContext, VarInfo),
837 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000838 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000839}
840
Devang Patel9b412732011-02-22 18:56:12 +0000841Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000842 DILocalVariable *VarInfo,
843 DIExpression *Expr,
844 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000845 BasicBlock *InsertAtEnd) {
846 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000847 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000848 assert(DL && "Expected debug loc");
849 assert(DL->getScope()->getSubprogram() ==
850 VarInfo->getScope()->getSubprogram() &&
851 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000852 if (!ValueFn)
853 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
854
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000855 trackIfUnresolved(VarInfo);
856 trackIfUnresolved(Expr);
857 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
858 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
859 MetadataAsValue::get(VMContext, VarInfo),
860 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000861
862 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000863}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000864
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000865void DIBuilder::replaceVTableHolder(DICompositeType *&T,
866 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000867 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000868 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000869 N->replaceVTableHolder(VTableHolder);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000870 T = N.get();
871 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000872
873 // If this didn't create a self-reference, just return.
874 if (T != VTableHolder)
875 return;
876
Adrian Prantl18a25b02015-02-11 17:45:10 +0000877 // Look for unresolved operands. T will drop RAUW support, orphaning any
878 // cycles underneath it.
879 if (T->isResolved())
880 for (const MDOperand &O : T->operands())
881 if (auto *N = dyn_cast_or_null<MDNode>(O))
882 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000883}
884
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000885void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
886 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000887 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000888 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000889 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000890 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000891 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000892 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000893 T = N.get();
894 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000895
896 // If T isn't resolved, there's no problem.
897 if (!T->isResolved())
898 return;
899
Adrian Prantl12d52842015-07-10 23:26:02 +0000900 // If T is resolved, it may be due to a self-reference cycle. Track the
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000901 // arrays explicitly if they're unresolved, or else the cycles will be
902 // orphaned.
903 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000904 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000905 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000906 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000907}