blob: f8ae23d4395c763c65c1c5ace9761329bcad5d40 [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"
Reid Klecknerbc669472017-10-03 20:36:40 +000015#include "llvm/IR/IRBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "LLVMContextImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/STLExtras.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000018#include "llvm/BinaryFormat/Dwarf.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000020#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/IntrinsicInst.h"
22#include "llvm/IR/Module.h"
Eric Christopher34164192012-04-03 00:43:49 +000023#include "llvm/Support/Debug.h"
Devang Patel57c5a202010-11-04 15:01:38 +000024
25using namespace llvm;
26using namespace llvm::dwarf;
27
Reid Kleckner0fe506b2017-09-21 19:52:03 +000028cl::opt<bool>
29 UseDbgAddr("use-dbg-addr",
Zachary Turner8065f0b2017-12-01 00:53:10 +000030 llvm::cl::desc("Use llvm.dbg.addr for all local variables"),
31 cl::init(false), cl::Hidden);
Reid Kleckner0fe506b2017-09-21 19:52:03 +000032
Jessica Paquettea499c3c2018-01-19 21:21:49 +000033DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes, DICompileUnit *CU)
34 : M(m), VMContext(M.getContext()), CUNode(CU),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000035 DeclareFn(nullptr), ValueFn(nullptr),
36 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
37
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000038void DIBuilder::trackIfUnresolved(MDNode *N) {
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000039 if (!N)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000040 return;
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000041 if (N->isResolved())
42 return;
43
44 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
45 UnresolvedNodes.emplace_back(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000046}
Devang Patel57c5a202010-11-04 15:01:38 +000047
Keno Fischer3cdd4932017-06-01 20:42:44 +000048void DIBuilder::finalizeSubprogram(DISubprogram *SP) {
49 MDTuple *Temp = SP->getVariables().get();
50 if (!Temp || !Temp->isTemporary())
51 return;
52
53 SmallVector<Metadata *, 4> Variables;
54
55 auto PV = PreservedVariables.find(SP);
56 if (PV != PreservedVariables.end())
57 Variables.append(PV->second.begin(), PV->second.end());
58
59 DINodeArray AV = getOrCreateArray(Variables);
60 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
61}
62
Devang Patel2b8acaf2011-08-15 23:00:00 +000063void DIBuilder::finalize() {
Adrian Prantl0e224a62015-07-06 16:22:12 +000064 if (!CUNode) {
65 assert(!AllowUnresolvedNodes &&
66 "creating type nodes without a CU is not supported");
67 return;
Devang Patel59e27c52011-08-19 23:28:12 +000068 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000069
Adrian Prantl0e224a62015-07-06 16:22:12 +000070 CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
71
72 SmallVector<Metadata *, 16> RetainValues;
73 // Declarations and definitions of the same type may be retained. Some
74 // clients RAUW these pairs, leaving duplicates in the retained types
75 // list. Use a set to remove the duplicates while we transform the
76 // TrackingVHs back into Values.
77 SmallPtrSet<Metadata *, 16> RetainSet;
78 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
79 if (RetainSet.insert(AllRetainTypes[I]).second)
80 RetainValues.push_back(AllRetainTypes[I]);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000081
82 if (!RetainValues.empty())
83 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
Adrian Prantl0e224a62015-07-06 16:22:12 +000084
85 DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
Adrian Prantl75819ae2016-04-15 15:57:41 +000086 for (auto *SP : SPs)
Keno Fischer3cdd4932017-06-01 20:42:44 +000087 finalizeSubprogram(SP);
Adrian Prantl75819ae2016-04-15 15:57:41 +000088 for (auto *N : RetainValues)
89 if (auto *SP = dyn_cast<DISubprogram>(N))
Keno Fischer3cdd4932017-06-01 20:42:44 +000090 finalizeSubprogram(SP);
Adrian Prantl0e224a62015-07-06 16:22:12 +000091
Adrian Prantl4276d4a2015-07-06 16:36:02 +000092 if (!AllGVs.empty())
93 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Adrian Prantl0e224a62015-07-06 16:22:12 +000094
Adrian Prantl4276d4a2015-07-06 16:36:02 +000095 if (!AllImportedModules.empty())
96 CUNode->replaceImportedEntities(MDTuple::get(
97 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
98 AllImportedModules.end())));
Adrian Prantl0e224a62015-07-06 16:22:12 +000099
Amjad Aboud96075712017-01-12 15:49:46 +0000100 for (const auto &I : AllMacrosPerParent) {
101 // DIMacroNode's with nullptr parent are DICompileUnit direct children.
102 if (!I.first) {
103 CUNode->replaceMacros(MDTuple::get(VMContext, I.second.getArrayRef()));
104 continue;
105 }
106 // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
107 auto *TMF = cast<DIMacroFile>(I.first);
108 auto *MF = DIMacroFile::get(VMContext, dwarf::DW_MACINFO_start_file,
109 TMF->getLine(), TMF->getFile(),
110 getOrCreateMacroArray(I.second.getArrayRef()));
111 replaceTemporary(llvm::TempDIMacroNode(TMF), MF);
112 }
113
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000114 // Now that all temp nodes have been replaced or deleted, resolve remaining
115 // cycles.
116 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000117 if (N && !N->isResolved())
118 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000119 UnresolvedNodes.clear();
120
121 // Can't handle unresolved nodes anymore.
122 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000123}
124
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000125/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000126static DIScope *getNonCompileUnitScope(DIScope *N) {
127 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000128 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000129 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000130}
131
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000132DICompileUnit *DIBuilder::createCompileUnit(
Amjad Aboud43c8b6b2016-12-14 20:24:54 +0000133 unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized,
134 StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
David Blaikiea01f2952016-08-24 18:29:49 +0000135 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId,
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000136 bool SplitDebugInlining, bool DebugInfoForProfiling, bool GnuPubnames) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000137
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000138 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000139 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
140 "Invalid Language tag");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000141
Adrian Prantl18c073a2015-07-02 22:32:52 +0000142 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
143 CUNode = DICompileUnit::getDistinct(
Amjad Aboud43c8b6b2016-12-14 20:24:54 +0000144 VMContext, Lang, File, Producer, isOptimized, Flags, RunTimeVer,
145 SplitName, Kind, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId,
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000146 SplitDebugInlining, DebugInfoForProfiling, GnuPubnames);
Devang Patel09fa69e2011-05-03 16:18:28 +0000147
148 // Create a named metadata so that it is easier to find cu in a module.
Adrian Prantl5992a722016-04-08 22:43:03 +0000149 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
150 NMD->addOperand(CUNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000151 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000152 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000153}
154
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000155static DIImportedEntity *
156createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000157 Metadata *NS, DIFile *File, unsigned Line, StringRef Name,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000158 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Adrian Prantld63bfd22017-07-19 00:09:54 +0000159 if (Line)
160 assert(File && "Source location has line number but no file");
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000161 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
Adrian Prantld63bfd22017-07-19 00:09:54 +0000162 auto *M =
163 DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), File, Line, Name);
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000164 if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
165 // A new Imported Entity was just added to the context.
166 // Add it to the Imported Modules list.
167 AllImportedModules.emplace_back(M);
David Blaikie1fd43652013-05-07 21:35:53 +0000168 return M;
169}
170
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000171DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000172 DINamespace *NS, DIFile *File,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000173 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000174 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000175 Context, NS, File, Line, StringRef(),
176 AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000177}
178
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000179DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
180 DIImportedEntity *NS,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000181 DIFile *File, unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000182 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000183 Context, NS, File, Line, StringRef(),
184 AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000185}
186
Adrian Prantlab1243f2015-06-29 23:03:47 +0000187DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000188 DIFile *File, unsigned Line) {
Adrian Prantlab1243f2015-06-29 23:03:47 +0000189 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000190 Context, M, File, Line, StringRef(),
191 AllImportedModules);
Adrian Prantlab1243f2015-06-29 23:03:47 +0000192}
193
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000194DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
195 DINode *Decl,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000196 DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000197 unsigned Line,
198 StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000199 // Make sure to use the unique identifier based metadata reference for
200 // types that have one.
David Blaikie2a40c142014-04-06 06:29:01 +0000201 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Adrian Prantld63bfd22017-07-19 00:09:54 +0000202 Context, Decl, File, Line, Name,
203 AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000204}
205
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000206DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory,
207 DIFile::ChecksumKind CSKind, StringRef Checksum) {
208 return DIFile::get(VMContext, Filename, Directory, CSKind, Checksum);
Devang Patel57c5a202010-11-04 15:01:38 +0000209}
210
Amjad Aboud96075712017-01-12 15:49:46 +0000211DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
212 unsigned MacroType, StringRef Name,
213 StringRef Value) {
214 assert(!Name.empty() && "Unable to create macro without name");
215 assert((MacroType == dwarf::DW_MACINFO_undef ||
216 MacroType == dwarf::DW_MACINFO_define) &&
217 "Unexpected macro type");
218 auto *M = DIMacro::get(VMContext, MacroType, LineNumber, Name, Value);
219 AllMacrosPerParent[Parent].insert(M);
220 return M;
221}
222
223DIMacroFile *DIBuilder::createTempMacroFile(DIMacroFile *Parent,
224 unsigned LineNumber, DIFile *File) {
225 auto *MF = DIMacroFile::getTemporary(VMContext, dwarf::DW_MACINFO_start_file,
226 LineNumber, File, DIMacroNodeArray())
227 .release();
228 AllMacrosPerParent[Parent].insert(MF);
229 // Add the new temporary DIMacroFile to the macro per parent map as a parent.
230 // This is needed to assure DIMacroFile with no children to have an entry in
231 // the map. Otherwise, it will not be resolved in DIBuilder::finalize().
232 AllMacrosPerParent.insert({MF, {}});
233 return MF;
234}
235
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000236DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000237 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000239}
240
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000241DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000242 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000244}
245
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000246DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000247 return createUnspecifiedType("decltype(nullptr)");
248}
249
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000250DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000251 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000252 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000253 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000254 0, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000255}
256
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000257DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000258 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000259 0, 0, None, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000260}
261
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000262DIDerivedType *DIBuilder::createPointerType(
263 DIType *PointeeTy,
264 uint64_t SizeInBits,
265 uint32_t AlignInBits,
266 Optional<unsigned> DWARFAddressSpace,
267 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000268 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000269 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000270 nullptr, 0, nullptr, PointeeTy, SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000271 AlignInBits, 0, DWARFAddressSpace,
272 DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000273}
274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000275DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
276 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000277 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000278 uint32_t AlignInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000279 DINode::DIFlags Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000280 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000281 nullptr, 0, nullptr, PointeeTy, SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000282 AlignInBits, 0, None, Flags, Base);
David Blaikie5d3249b2013-01-07 05:51:15 +0000283}
284
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000285DIDerivedType *DIBuilder::createReferenceType(
286 unsigned Tag, DIType *RTy,
287 uint64_t SizeInBits,
288 uint32_t AlignInBits,
289 Optional<unsigned> DWARFAddressSpace) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000290 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000291 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000292 SizeInBits, AlignInBits, 0, DWARFAddressSpace,
293 DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000294}
295
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000296DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
297 DIFile *File, unsigned LineNo,
298 DIScope *Context) {
299 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000300 LineNo, getNonCompileUnitScope(Context), Ty, 0, 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000301 0, None, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000302}
303
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000304DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000305 assert(Ty && "Invalid type!");
306 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000307 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000308 FriendTy, 0, 0, 0, None, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000309}
310
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000311DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000312 uint64_t BaseOffset,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000313 DINode::DIFlags Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000314 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000315 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000316 0, Ty, BaseTy, 0, 0, BaseOffset, None, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000317}
318
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000319DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
320 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000321 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000322 uint32_t AlignInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000323 uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000324 DINode::DIFlags Flags, DIType *Ty) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000325 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
326 LineNumber, getNonCompileUnitScope(Scope), Ty,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000327 SizeInBits, AlignInBits, OffsetInBits, None, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000328}
329
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000330static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000331 if (C)
332 return ConstantAsMetadata::get(C);
333 return nullptr;
334}
335
David Majnemer9319cbc2016-06-30 03:00:20 +0000336DIDerivedType *DIBuilder::createBitFieldMemberType(
337 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000338 uint64_t SizeInBits, uint64_t OffsetInBits, uint64_t StorageOffsetInBits,
339 DINode::DIFlags Flags, DIType *Ty) {
David Majnemer9319cbc2016-06-30 03:00:20 +0000340 Flags |= DINode::FlagBitField;
341 return DIDerivedType::get(
342 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000343 getNonCompileUnitScope(Scope), Ty, SizeInBits, /* AlignInBits */ 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000344 OffsetInBits, None, Flags,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000345 ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext, 64),
346 StorageOffsetInBits)));
David Majnemer9319cbc2016-06-30 03:00:20 +0000347}
348
Leny Kholodov40c62352016-09-06 17:03:02 +0000349DIDerivedType *
350DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
351 unsigned LineNumber, DIType *Ty,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000352 DINode::DIFlags Flags, llvm::Constant *Val,
353 uint32_t AlignInBits) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000354 Flags |= DINode::FlagStaticMember;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000355 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000356 LineNumber, getNonCompileUnitScope(Scope), Ty, 0,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000357 AlignInBits, 0, None, Flags,
358 getConstantOrNull(Val));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000359}
360
Leny Kholodov40c62352016-09-06 17:03:02 +0000361DIDerivedType *
362DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000363 uint64_t SizeInBits, uint32_t AlignInBits,
Leny Kholodov40c62352016-09-06 17:03:02 +0000364 uint64_t OffsetInBits, DINode::DIFlags Flags,
365 DIType *Ty, MDNode *PropertyNode) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000366 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
367 LineNumber, getNonCompileUnitScope(File), Ty,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000368 SizeInBits, AlignInBits, OffsetInBits, None, Flags,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000369 PropertyNode);
Devang Patel44882172012-02-06 17:49:43 +0000370}
371
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000372DIObjCProperty *
373DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
Eric Christopher98f9c232013-10-15 23:31:31 +0000374 StringRef GetterName, StringRef SetterName,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000375 unsigned PropertyAttributes, DIType *Ty) {
376 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000377 SetterName, PropertyAttributes, Ty);
Devang Patelcc481592012-02-04 00:59:25 +0000378}
379
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000380DITemplateTypeParameter *
381DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
382 DIType *Ty) {
383 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000384 return DITemplateTypeParameter::get(VMContext, Name, Ty);
Devang Patel3a9e65e2011-02-02 21:38:25 +0000385}
386
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000387static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000388createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000390 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000391 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000392 return DITemplateValueParameter::get(VMContext, Tag, Name, Ty, MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000393}
394
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000395DITemplateValueParameter *
396DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
397 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000398 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000399 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000400 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000401}
402
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000403DITemplateValueParameter *
404DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
405 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000406 return createTemplateValueParameterHelper(
407 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000408 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000409}
410
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000411DITemplateValueParameter *
412DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
413 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000414 return createTemplateValueParameterHelper(
415 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000416 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000417}
418
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000419DICompositeType *DIBuilder::createClassType(
420 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000421 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000422 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000423 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
424 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000425 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000426
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000427 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,
430 OffsetInBits, Flags, Elements, 0, VTableHolder,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000431 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000432 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000433 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000434}
435
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000436DICompositeType *DIBuilder::createStructType(
437 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000438 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000439 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
440 DIType *VTableHolder, StringRef UniqueIdentifier) {
441 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000442 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000443 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0,
444 Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000445 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000446 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000447}
448
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000449DICompositeType *DIBuilder::createUnionType(
450 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000451 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000452 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
453 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000454 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000455 getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
456 Elements, RunTimeLang, nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000457 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000458 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000459}
460
Eric Christopherbdafb3c2015-10-15 06:56:10 +0000461DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
Leny Kholodov40c62352016-09-06 17:03:02 +0000462 DINode::DIFlags Flags,
463 unsigned CC) {
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000464 return DISubroutineType::get(VMContext, Flags, CC, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000465}
466
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000467DICompositeType *DIBuilder::createEnumerationType(
468 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000469 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000470 DIType *UnderlyingType, StringRef UniqueIdentifier) {
471 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000472 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000473 getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000474 DINode::FlagZero, Elements, 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000475 AllEnumTypes.push_back(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000476 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000477 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000478}
479
Victor Leschuk197aa312016-10-18 14:31:22 +0000480DICompositeType *DIBuilder::createArrayType(uint64_t Size,
481 uint32_t AlignInBits, DIType *Ty,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000482 DINodeArray Subscripts) {
483 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000484 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000485 DINode::FlagZero, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000486 trackIfUnresolved(R);
487 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000488}
489
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000490DICompositeType *DIBuilder::createVectorType(uint64_t Size,
Victor Leschuk197aa312016-10-18 14:31:22 +0000491 uint32_t AlignInBits, DIType *Ty,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000492 DINodeArray Subscripts) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000493 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
494 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
495 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000496 trackIfUnresolved(R);
497 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000498}
Devang Patel746660f2010-12-07 23:25:47 +0000499
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000500static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000501 DINode::DIFlags FlagsToSet) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000502 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000503 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
504 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000505}
506
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000507DIType *DIBuilder::createArtificialType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000508 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000509 if (Ty->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000510 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000511 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000512}
Devang Patel746660f2010-12-07 23:25:47 +0000513
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000514DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000515 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000516 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000517 return Ty;
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000518 DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000519 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000520}
521
Adrian Prantl75819ae2016-04-15 15:57:41 +0000522void DIBuilder::retainType(DIScope *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000523 assert(T && "Expected non-null type");
Adrian Prantl75819ae2016-04-15 15:57:41 +0000524 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
525 cast<DISubprogram>(T)->isDefinition() == false)) &&
526 "Expected type or subprogram declaration");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000527 AllRetainTypes.emplace_back(T);
528}
Devang Patel89ea4f22010-12-08 01:50:15 +0000529
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000530DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000531
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000532DICompositeType *
533DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
534 DIFile *F, unsigned Line, unsigned RuntimeLang,
Victor Leschuk197aa312016-10-18 14:31:22 +0000535 uint64_t SizeInBits, uint32_t AlignInBits,
Eric Christopher98f9c232013-10-15 23:31:31 +0000536 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000537 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
538 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000539 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000540 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
541 SizeInBits, AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang,
542 nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000543 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000544 return RetTy;
545}
546
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000547DICompositeType *DIBuilder::createReplaceableCompositeType(
548 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
Victor Leschuk197aa312016-10-18 14:31:22 +0000549 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000550 DINode::DIFlags Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000551 auto *RetTy =
552 DICompositeType::getTemporary(
553 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
554 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang, nullptr,
555 nullptr, UniqueIdentifier)
556 .release();
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000557 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000558 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000559}
560
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000561DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000562 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000563}
564
Amjad Aboud96075712017-01-12 15:49:46 +0000565DIMacroNodeArray
566DIBuilder::getOrCreateMacroArray(ArrayRef<Metadata *> Elements) {
567 return MDTuple::get(VMContext, Elements);
568}
569
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000570DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000571 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000572 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
573 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000574 Elts.push_back(cast<DIType>(Elements[i]));
Manman Ren1a125c92014-07-28 19:33:20 +0000575 else
576 Elts.push_back(Elements[i]);
577 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000578 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000579}
580
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000581DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
582 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000583}
584
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000585static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000586#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000587 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000588 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000589 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000590 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000591#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000592}
593
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000594DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000595 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000596 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, DIExpression *Expr,
597 MDNode *Decl, uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000598 checkGlobalVariableScope(Context);
599
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000600 auto *GV = DIGlobalVariable::getDistinct(
Duncan P. N. Exon Smithc5225df2016-04-23 22:29:09 +0000601 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000602 LineNumber, Ty, isLocalToUnit, true, cast_or_null<DIDerivedType>(Decl),
603 AlignInBits);
Adrian Prantl05782212017-08-30 18:06:51 +0000604 if (!Expr)
605 Expr = createExpression();
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000606 auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000607 AllGVs.push_back(N);
608 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000609}
610
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000611DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
612 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000613 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, MDNode *Decl,
614 uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000615 checkGlobalVariableScope(Context);
616
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000617 return DIGlobalVariable::getTemporary(
618 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000619 LineNumber, Ty, isLocalToUnit, false,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000620 cast_or_null<DIDerivedType>(Decl), AlignInBits)
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000621 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000622}
623
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000624static DILocalVariable *createLocalVariable(
625 LLVMContext &VMContext,
Duncan P. N. Exon Smith3c406c22016-04-20 20:14:09 +0000626 DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> &PreservedVariables,
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000627 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000628 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
629 uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000630 // FIXME: Why getNonCompileUnitScope()?
631 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000632 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000633 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000634 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000635
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000636 auto *Node =
637 DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000638 File, LineNo, Ty, ArgNo, Flags, AlignInBits);
Devang Patel63f83cd2010-12-07 23:58:00 +0000639 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000640 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000641 // to preserve variable info in such situation then stash it in a
642 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000643 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000644 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000645 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000646 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000647 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000648}
649
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000650DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
651 DIFile *File, unsigned LineNo,
652 DIType *Ty, bool AlwaysPreserve,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000653 DINode::DIFlags Flags,
654 uint32_t AlignInBits) {
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000655 return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
656 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000657 Flags, AlignInBits);
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000658}
659
660DILocalVariable *DIBuilder::createParameterVariable(
661 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000662 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags) {
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000663 assert(ArgNo && "Expected non-zero argument number for parameter");
664 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000665 File, LineNo, Ty, AlwaysPreserve, Flags,
666 /* AlignInBits */0);
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000667}
668
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000669DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
670 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000671}
672
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000673DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000674 // TODO: Remove the callers of this signed version and delete.
675 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
676 return createExpression(Addr);
677}
678
Duncan P. N. Exon Smithb2df6472015-08-26 22:50:16 +0000679template <class... Ts>
680static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
681 if (IsDistinct)
682 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
683 return DISubprogram::get(std::forward<Ts>(Args)...);
684}
685
Peter Collingbourned4bff302015-11-05 22:03:56 +0000686DISubprogram *DIBuilder::createFunction(
687 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
688 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
Leny Kholodov40c62352016-09-06 17:03:02 +0000689 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000690 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl,
691 DITypeArray ThrownTypes) {
Adrian Prantl75819ae2016-04-15 15:57:41 +0000692 auto *Node = getSubprogram(
693 /* IsDistinct = */ isDefinition, VMContext,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000694 getNonCompileUnitScope(Context), Name, LinkageName, File, LineNo, Ty,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000695 isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, 0, Flags,
696 isOptimized, isDefinition ? CUNode : nullptr, TParams, Decl,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000697 MDTuple::getTemporary(VMContext, None).release(), ThrownTypes);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000698
699 if (isDefinition)
700 AllSubprograms.push_back(Node);
701 trackIfUnresolved(Node);
702 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000703}
704
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000705DISubprogram *DIBuilder::createTempFunctionFwdDecl(
706 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
707 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
Leny Kholodov40c62352016-09-06 17:03:02 +0000708 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000709 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl,
710 DITypeArray ThrownTypes) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000711 return DISubprogram::getTemporary(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000712 VMContext, getNonCompileUnitScope(Context), Name, LinkageName,
713 File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000714 0, 0, 0, Flags, isOptimized, isDefinition ? CUNode : nullptr,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000715 TParams, Decl, nullptr, ThrownTypes)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000716 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000717}
718
Adrian Prantl1d12b882017-04-26 22:56:44 +0000719DISubprogram *DIBuilder::createMethod(
720 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
721 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
722 bool isDefinition, unsigned VK, unsigned VIndex, int ThisAdjustment,
723 DIType *VTableHolder, DINode::DIFlags Flags, bool isOptimized,
724 DITemplateParameterArray TParams, DITypeArray ThrownTypes) {
Eric Christopher5cb56322013-10-15 23:31:36 +0000725 assert(getNonCompileUnitScope(Context) &&
726 "Methods should have both a Context and a context that isn't "
727 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000728 // FIXME: Do we want to use different scope/lines?
Peter Collingbourned4bff302015-11-05 22:03:56 +0000729 auto *SP = getSubprogram(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000730 /* IsDistinct = */ isDefinition, VMContext, cast<DIScope>(Context), Name,
731 LinkageName, F, LineNo, Ty, isLocalToUnit, isDefinition, LineNo,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000732 VTableHolder, VK, VIndex, ThisAdjustment, Flags, isOptimized,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000733 isDefinition ? CUNode : nullptr, TParams, nullptr, nullptr, ThrownTypes);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000734
David Blaikie595eb442013-02-18 07:10:22 +0000735 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000736 AllSubprograms.push_back(SP);
737 trackIfUnresolved(SP);
738 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000739}
740
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000741DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
Adrian Prantldbfda632016-11-03 19:42:02 +0000742 bool ExportSymbols) {
Adrian Prantlfed4f392017-04-28 22:25:46 +0000743
744 // It is okay to *not* make anonymous top-level namespaces distinct, because
745 // all nodes that have an anonymous namespace as their parent scope are
746 // guaranteed to be unique and/or are linked to their containing
747 // DICompileUnit. This decision is an explicit tradeoff of link time versus
748 // memory usage versus code simplicity and may get revisited in the future.
749 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), Name,
750 ExportSymbols);
Devang Patel746660f2010-12-07 23:25:47 +0000751}
752
Adrian Prantlab1243f2015-06-29 23:03:47 +0000753DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
754 StringRef ConfigurationMacros,
755 StringRef IncludePath,
756 StringRef ISysRoot) {
757 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
758 ConfigurationMacros, IncludePath, ISysRoot);
759}
760
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000761DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
762 DIFile *File,
763 unsigned Discriminator) {
764 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000765}
766
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000767DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
768 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000769 // Make these distinct, to avoid merging two lexical blocks on the same
770 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000771 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000772 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000773}
774
Reid Klecknerbc669472017-10-03 20:36:40 +0000775Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
776 DIExpression *Expr, const DILocation *DL,
777 Instruction *InsertBefore) {
778 return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(),
779 InsertBefore);
780}
781
782Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
783 DIExpression *Expr, const DILocation *DL,
784 BasicBlock *InsertAtEnd) {
785 // If this block already has a terminator then insert this intrinsic before
786 // the terminator. Otherwise, put it at the end of the block.
787 Instruction *InsertBefore = InsertAtEnd->getTerminator();
788 return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
789}
790
791Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
792 DILocalVariable *VarInfo,
793 DIExpression *Expr,
794 const DILocation *DL,
795 Instruction *InsertBefore) {
796 return insertDbgValueIntrinsic(
797 V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
798 InsertBefore);
799}
800
801Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
802 DILocalVariable *VarInfo,
803 DIExpression *Expr,
804 const DILocation *DL,
805 BasicBlock *InsertAtEnd) {
806 return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr);
807}
808
809/// Return an IRBuilder for inserting dbg.declare and dbg.value intrinsics. This
810/// abstracts over the various ways to specify an insert position.
811static IRBuilder<> getIRBForDbgInsertion(const DILocation *DL,
812 BasicBlock *InsertBB,
813 Instruction *InsertBefore) {
814 IRBuilder<> B(DL->getContext());
815 if (InsertBefore)
816 B.SetInsertPoint(InsertBefore);
817 else if (InsertBB)
818 B.SetInsertPoint(InsertBB);
819 B.SetCurrentDebugLocation(DL);
820 return B;
821}
822
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000823static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
824 assert(V && "no value passed to dbg intrinsic");
825 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
826}
827
Reid Kleckner0fe506b2017-09-21 19:52:03 +0000828static Function *getDeclareIntrin(Module &M) {
829 return Intrinsic::getDeclaration(&M, UseDbgAddr ? Intrinsic::dbg_addr
830 : Intrinsic::dbg_declare);
831}
832
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000833Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
834 DIExpression *Expr, const DILocation *DL,
Reid Klecknerbc669472017-10-03 20:36:40 +0000835 BasicBlock *InsertBB, Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000836 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000837 assert(DL && "Expected debug loc");
838 assert(DL->getScope()->getSubprogram() ==
839 VarInfo->getScope()->getSubprogram() &&
840 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000841 if (!DeclareFn)
Reid Kleckner0fe506b2017-09-21 19:52:03 +0000842 DeclareFn = getDeclareIntrin(M);
Devang Patel746660f2010-12-07 23:25:47 +0000843
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000844 trackIfUnresolved(VarInfo);
845 trackIfUnresolved(Expr);
846 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
847 MetadataAsValue::get(VMContext, VarInfo),
848 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000849
Reid Klecknerbc669472017-10-03 20:36:40 +0000850 IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
851 return B.CreateCall(DeclareFn, Args);
Devang Patel746660f2010-12-07 23:25:47 +0000852}
853
Reid Klecknerbc669472017-10-03 20:36:40 +0000854Instruction *DIBuilder::insertDbgValueIntrinsic(
855 Value *V, DILocalVariable *VarInfo, DIExpression *Expr,
856 const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) {
Devang Patel746660f2010-12-07 23:25:47 +0000857 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000858 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000859 assert(DL && "Expected debug loc");
860 assert(DL->getScope()->getSubprogram() ==
861 VarInfo->getScope()->getSubprogram() &&
862 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000863 if (!ValueFn)
864 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
865
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000866 trackIfUnresolved(VarInfo);
867 trackIfUnresolved(Expr);
868 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000869 MetadataAsValue::get(VMContext, VarInfo),
870 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000871
Reid Klecknerbc669472017-10-03 20:36:40 +0000872 IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
873 return B.CreateCall(ValueFn, Args);
Devang Patel746660f2010-12-07 23:25:47 +0000874}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000875
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000876void DIBuilder::replaceVTableHolder(DICompositeType *&T,
Adrian Prantla8e56452017-11-08 22:04:43 +0000877 DIType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000878 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000879 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000880 N->replaceVTableHolder(VTableHolder);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000881 T = N.get();
882 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000883
884 // If this didn't create a self-reference, just return.
885 if (T != VTableHolder)
886 return;
887
Adrian Prantl18a25b02015-02-11 17:45:10 +0000888 // Look for unresolved operands. T will drop RAUW support, orphaning any
889 // cycles underneath it.
890 if (T->isResolved())
891 for (const MDOperand &O : T->operands())
892 if (auto *N = dyn_cast_or_null<MDNode>(O))
893 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000894}
895
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000896void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
897 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000898 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000899 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000900 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000901 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000902 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000903 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000904 T = N.get();
905 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000906
907 // If T isn't resolved, there's no problem.
908 if (!T->isResolved())
909 return;
910
Adrian Prantl12d52842015-07-10 23:26:02 +0000911 // 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 +0000912 // arrays explicitly if they're unresolved, or else the cycles will be
913 // orphaned.
914 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000915 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000916 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000917 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000918}