blob: 204817f84e8268fbfd959524a593b97cb696b4e9 [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"
22
23using namespace llvm;
24using namespace llvm::dwarf;
25
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000026namespace {
27class HeaderBuilder {
28 SmallVector<char, 256> Chars;
29
30public:
31 explicit HeaderBuilder(Twine T) { T.toVector(Chars); }
32 HeaderBuilder(const HeaderBuilder &X) : Chars(X.Chars) {}
33 HeaderBuilder(HeaderBuilder &&X) : Chars(std::move(X.Chars)) {}
34
35 template <class Twineable> HeaderBuilder &concat(Twineable &&X) {
36 Chars.push_back(0);
37 Twine(X).toVector(Chars);
38 return *this;
39 }
40
41 MDString *get(LLVMContext &Context) const {
42 return MDString::get(Context, StringRef(Chars.begin(), Chars.size()));
43 }
44
45 static HeaderBuilder get(unsigned Tag) {
46 return HeaderBuilder("0x" + Twine::utohexstr(Tag));
47 }
48};
Devang Patel57c5a202010-11-04 15:01:38 +000049}
Devang Patel63f83cd2010-12-07 23:58:00 +000050
Devang Patel57c5a202010-11-04 15:01:38 +000051DIBuilder::DIBuilder(Module &m)
Craig Topperc6207612014-04-09 06:08:46 +000052 : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr),
53 TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr),
54 DeclareFn(nullptr), ValueFn(nullptr) {}
Devang Patel57c5a202010-11-04 15:01:38 +000055
Devang Patel2b8acaf2011-08-15 23:00:00 +000056void DIBuilder::finalize() {
Devang Pateleb1bb4e2011-08-16 22:09:43 +000057 DIArray Enums = getOrCreateArray(AllEnumTypes);
58 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
59
Manman Ren0b410402013-08-29 23:17:54 +000060 SmallVector<Value *, 16> RetainValues;
61 // Declarations and definitions of the same type may be retained. Some
62 // clients RAUW these pairs, leaving duplicates in the retained types
63 // list. Use a set to remove the duplicates while we transform the
64 // TrackingVHs back into Values.
65 SmallPtrSet<Value *, 16> RetainSet;
66 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
David Blaikie70573dc2014-11-19 07:49:26 +000067 if (RetainSet.insert(AllRetainTypes[I]).second)
Manman Ren0b410402013-08-29 23:17:54 +000068 RetainValues.push_back(AllRetainTypes[I]);
69 DIArray RetainTypes = getOrCreateArray(RetainValues);
Devang Pateleb1bb4e2011-08-16 22:09:43 +000070 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
71
72 DIArray SPs = getOrCreateArray(AllSubprograms);
73 DIType(TempSubprograms).replaceAllUsesWith(SPs);
Devang Patel59e27c52011-08-19 23:28:12 +000074 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
75 DISubprogram SP(SPs.getElement(i));
Eric Christopher27deb262012-04-23 19:00:11 +000076 if (MDNode *Temp = SP.getVariablesNodes()) {
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +000077 SmallVector<Value *, 4> Variables;
78 for (Value *V : PreservedVariables.lookup(SP))
79 Variables.push_back(V);
Eric Christopher27deb262012-04-23 19:00:11 +000080 DIArray AV = getOrCreateArray(Variables);
81 DIType(Temp).replaceAllUsesWith(AV);
82 }
Devang Patel59e27c52011-08-19 23:28:12 +000083 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000084
85 DIArray GVs = getOrCreateArray(AllGVs);
86 DIType(TempGVs).replaceAllUsesWith(GVs);
David Blaikief55abea2013-04-22 06:12:31 +000087
Eric Christopher2c3a6dc2014-02-28 21:27:57 +000088 SmallVector<Value *, 16> RetainValuesI;
89 for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++)
90 RetainValuesI.push_back(AllImportedModules[I]);
91 DIArray IMs = getOrCreateArray(RetainValuesI);
David Blaikief55abea2013-04-22 06:12:31 +000092 DIType(TempImportedModules).replaceAllUsesWith(IMs);
Devang Pateleb1bb4e2011-08-16 22:09:43 +000093}
94
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +000095/// If N is compile unit return NULL otherwise return N.
Devang Pateleb1bb4e2011-08-16 22:09:43 +000096static MDNode *getNonCompileUnitScope(MDNode *N) {
97 if (DIDescriptor(N).isCompileUnit())
Craig Topperc6207612014-04-09 06:08:46 +000098 return nullptr;
Devang Pateleb1bb4e2011-08-16 22:09:43 +000099 return N;
Devang Patel2b8acaf2011-08-15 23:00:00 +0000100}
101
David Blaikieefb0d652013-03-20 23:58:12 +0000102static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
103 StringRef Directory) {
104 assert(!Filename.empty() && "Unable to create file without name");
105 Value *Pair[] = {
106 MDString::get(VMContext, Filename),
Eric Christopher98f9c232013-10-15 23:31:31 +0000107 MDString::get(VMContext, Directory)
David Blaikieefb0d652013-03-20 23:58:12 +0000108 };
109 return MDNode::get(VMContext, Pair);
110}
111
Eric Christopher03b3e112013-07-19 00:51:47 +0000112DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
113 StringRef Directory,
114 StringRef Producer, bool isOptimized,
115 StringRef Flags, unsigned RunTimeVer,
Eric Christopher75d49db2014-02-27 01:24:56 +0000116 StringRef SplitName,
Diego Novillo56653fd2014-06-24 17:02:03 +0000117 DebugEmissionKind Kind,
118 bool EmitDebugInfo) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000119
Peter Collingbourneb2f70c72014-04-28 18:11:01 +0000120 assert(((Lang <= dwarf::DW_LANG_OCaml && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000121 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
122 "Invalid Language tag");
123 assert(!Filename.empty() &&
124 "Unable to create compile unit without filename");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000125 Value *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000126 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000127
128 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000129
130 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000131
132 TempGVs = MDNode::getTemporary(VMContext, TElts);
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000133
David Blaikief55abea2013-04-22 06:12:31 +0000134 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
135
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000136 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit)
137 .concat(Lang)
138 .concat(Producer)
139 .concat(isOptimized)
140 .concat(Flags)
141 .concat(RunTimeVer)
142 .concat(SplitName)
143 .concat(Kind)
144 .get(VMContext),
145 createFilePathPair(VMContext, Filename, Directory),
146 TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs,
147 TempImportedModules};
Eric Christopher03b3e112013-07-19 00:51:47 +0000148
149 MDNode *CUNode = MDNode::get(VMContext, Elts);
Devang Patel09fa69e2011-05-03 16:18:28 +0000150
151 // Create a named metadata so that it is easier to find cu in a module.
Diego Novillo56653fd2014-06-24 17:02:03 +0000152 // Note that we only generate this when the caller wants to actually
153 // emit debug information. When we are only interested in tracking
154 // source line locations throughout the backend, we prevent codegen from
155 // emitting debug info in the final output by not generating llvm.dbg.cu.
156 if (EmitDebugInfo) {
157 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
158 NMD->addOperand(CUNode);
159 }
Eric Christopher03b3e112013-07-19 00:51:47 +0000160
161 return DICompileUnit(CUNode);
Devang Patel57c5a202010-11-04 15:01:38 +0000162}
163
David Blaikiee63d5d12013-05-20 22:50:35 +0000164static DIImportedEntity
David Blaikie2a40c142014-04-06 06:29:01 +0000165createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
166 Value *NS, unsigned Line, StringRef Name,
167 SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) {
David Blaikiee63d5d12013-05-20 22:50:35 +0000168 const MDNode *R;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000169 Value *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C),
170 Context, NS};
171 R = MDNode::get(C, Elts);
David Blaikiee63d5d12013-05-20 22:50:35 +0000172 DIImportedEntity M(R);
David Blaikie1fd43652013-05-07 21:35:53 +0000173 assert(M.Verify() && "Imported module should be valid");
Eric Christopher2c3a6dc2014-02-28 21:27:57 +0000174 AllImportedModules.push_back(TrackingVH<MDNode>(M));
David Blaikie1fd43652013-05-07 21:35:53 +0000175 return M;
176}
177
David Blaikiee63d5d12013-05-20 22:50:35 +0000178DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
David Blaikie2a40c142014-04-06 06:29:01 +0000179 DINameSpace NS,
180 unsigned Line) {
181 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
182 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000183}
184
185DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
186 DIImportedEntity NS,
David Blaikie2a40c142014-04-06 06:29:01 +0000187 unsigned Line) {
188 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
189 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000190}
191
David Blaikie1fd43652013-05-07 21:35:53 +0000192DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000193 DIDescriptor Decl,
David Blaikie2a40c142014-04-06 06:29:01 +0000194 unsigned Line, StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000195 // Make sure to use the unique identifier based metadata reference for
196 // types that have one.
Frederic Riss051cd752014-11-06 19:00:47 +0000197 Value *V = Decl.isType() ? static_cast<Value*>(DIType(Decl).getRef()) : Decl;
David Blaikie2a40c142014-04-06 06:29:01 +0000198 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000199 Context, V, Line, Name,
David Blaikie2a40c142014-04-06 06:29:01 +0000200 AllImportedModules);
201}
202
203DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
204 DIImportedEntity Imp,
205 unsigned Line, StringRef Name) {
206 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
207 Context, Imp, Line, Name, AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000208}
209
Devang Patel9b412732011-02-22 18:56:12 +0000210DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000211 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext),
212 createFilePathPair(VMContext, Filename, Directory)};
David Blaikie5692e722013-03-28 02:44:59 +0000213 return DIFile(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000214}
215
David Blaikieb7619002013-06-24 17:34:33 +0000216DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000217 assert(!Name.empty() && "Unable to create enumerator without name");
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000218 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000219 HeaderBuilder::get(dwarf::DW_TAG_enumerator).concat(Name).concat(Val).get(
220 VMContext)};
David Blaikie5692e722013-03-28 02:44:59 +0000221 return DIEnumerator(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000222}
223
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000224DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000225 assert(!Name.empty() && "Unable to create type without name");
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000226 // Unspecified types are encoded in DIBasicType format. Line number, filename,
227 // size, alignment, offset and flags are always empty here.
Devang Patel04d6d472011-09-14 23:13:28 +0000228 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000229 HeaderBuilder::get(dwarf::DW_TAG_unspecified_type)
230 .concat(Name)
231 .concat(0)
232 .concat(0)
233 .concat(0)
234 .concat(0)
235 .concat(0)
236 .concat(0)
237 .get(VMContext),
238 nullptr, // Filename
239 nullptr // Unused
Devang Patel04d6d472011-09-14 23:13:28 +0000240 };
Manman Ren3c6acec2013-06-07 18:35:53 +0000241 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel04d6d472011-09-14 23:13:28 +0000242}
243
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000244DIBasicType DIBuilder::createNullPtrType() {
245 return createUnspecifiedType("decltype(nullptr)");
246}
247
David Blaikie209d63a2013-02-12 00:40:41 +0000248DIBasicType
249DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
250 uint64_t AlignInBits, unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000251 assert(!Name.empty() && "Unable to create type without name");
Devang Patel57c5a202010-11-04 15:01:38 +0000252 // Basic types are encoded in DIBasicType format. Line number, filename,
253 // offset and flags are always empty here.
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000254 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000255 HeaderBuilder::get(dwarf::DW_TAG_base_type)
256 .concat(Name)
257 .concat(0) // Line
258 .concat(SizeInBits)
259 .concat(AlignInBits)
260 .concat(0) // Offset
261 .concat(0) // Flags
262 .concat(Encoding)
263 .get(VMContext),
264 nullptr, // Filename
265 nullptr // Unused
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000266 };
David Blaikie5692e722013-03-28 02:44:59 +0000267 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000268}
269
David Blaikief11de2f2013-02-18 06:41:57 +0000270DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000271 // Qualified types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000272 Value *Elts[] = {HeaderBuilder::get(Tag)
273 .concat(StringRef()) // Name
274 .concat(0) // Line
275 .concat(0) // Size
276 .concat(0) // Align
277 .concat(0) // Offset
278 .concat(0) // Flags
279 .get(VMContext),
280 nullptr, // Filename
281 nullptr, // Unused
282 FromTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000283 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000284}
285
David Blaikief11de2f2013-02-18 06:41:57 +0000286DIDerivedType
287DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
288 uint64_t AlignInBits, StringRef Name) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000289 // Pointer types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000290 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type)
291 .concat(Name)
292 .concat(0) // Line
293 .concat(SizeInBits)
294 .concat(AlignInBits)
295 .concat(0) // Offset
296 .concat(0) // Flags
297 .get(VMContext),
298 nullptr, // Filename
299 nullptr, // Unused
300 PointeeTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000301 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000302}
303
Eric Christopher89e594d2013-04-19 20:37:12 +0000304DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
305 DIType Base) {
David Blaikie5d3249b2013-01-07 05:51:15 +0000306 // Pointer types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000307 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type)
308 .concat(StringRef())
309 .concat(0) // Line
310 .concat(0) // Size
311 .concat(0) // Align
312 .concat(0) // Offset
313 .concat(0) // Flags
314 .get(VMContext),
315 nullptr, // Filename
316 nullptr, // Unused
317 PointeeTy.getRef(), Base.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000318 return DIDerivedType(MDNode::get(VMContext, Elts));
David Blaikie5d3249b2013-01-07 05:51:15 +0000319}
320
David Blaikief11de2f2013-02-18 06:41:57 +0000321DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
Manman Ren74c188f2013-07-01 21:02:01 +0000322 assert(RTy.isType() && "Unable to create reference type");
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000323 // References are encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000324 Value *Elts[] = {HeaderBuilder::get(Tag)
325 .concat(StringRef()) // Name
326 .concat(0) // Line
327 .concat(0) // Size
328 .concat(0) // Align
329 .concat(0) // Offset
330 .concat(0) // Flags
331 .get(VMContext),
332 nullptr, // Filename
333 nullptr, // TheCU,
334 RTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000335 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000336}
337
David Blaikief11de2f2013-02-18 06:41:57 +0000338DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
339 unsigned LineNo, DIDescriptor Context) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000340 // typedefs are encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000341 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef)
342 .concat(Name)
343 .concat(LineNo)
344 .concat(0) // Size
345 .concat(0) // Align
346 .concat(0) // Offset
347 .concat(0) // Flags
348 .get(VMContext),
349 File.getFileNode(),
350 DIScope(getNonCompileUnitScope(Context)).getRef(),
351 Ty.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000352 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000353}
354
Manman Ren3c6acec2013-06-07 18:35:53 +0000355DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000356 // typedefs are encoded in DIDerivedType format.
Manman Ren74c188f2013-07-01 21:02:01 +0000357 assert(Ty.isType() && "Invalid type!");
358 assert(FriendTy.isType() && "Invalid friend type!");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000359 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend)
360 .concat(StringRef()) // Name
361 .concat(0) // Line
362 .concat(0) // Size
363 .concat(0) // Align
364 .concat(0) // Offset
365 .concat(0) // Flags
366 .get(VMContext),
367 nullptr, Ty.getRef(), FriendTy.getRef()};
Manman Ren3c6acec2013-06-07 18:35:53 +0000368 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000369}
370
Eric Christopher98f9c232013-10-15 23:31:31 +0000371DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
372 uint64_t BaseOffset,
373 unsigned Flags) {
Manman Ren74c188f2013-07-01 21:02:01 +0000374 assert(Ty.isType() && "Unable to create inheritance");
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000375 // TAG_inheritance is encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000376 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance)
377 .concat(StringRef()) // Name
378 .concat(0) // Line
379 .concat(0) // Size
380 .concat(0) // Align
381 .concat(BaseOffset)
382 .concat(Flags)
383 .get(VMContext),
384 nullptr, Ty.getRef(), BaseTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000385 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000386}
387
Eric Christopher98f9c232013-10-15 23:31:31 +0000388DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
389 DIFile File, unsigned LineNumber,
390 uint64_t SizeInBits,
391 uint64_t AlignInBits,
392 uint64_t OffsetInBits, unsigned Flags,
393 DIType Ty) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000394 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000395 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
396 .concat(Name)
397 .concat(LineNumber)
398 .concat(SizeInBits)
399 .concat(AlignInBits)
400 .concat(OffsetInBits)
401 .concat(Flags)
402 .get(VMContext),
403 File.getFileNode(),
404 DIScope(getNonCompileUnitScope(Scope)).getRef(),
405 Ty.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000406 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000407}
408
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000409DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope,
410 StringRef Name, DIFile File,
411 unsigned LineNumber, DIType Ty,
412 unsigned Flags,
413 llvm::Constant *Val) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000414 // TAG_member is encoded in DIDerivedType format.
415 Flags |= DIDescriptor::FlagStaticMember;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000416 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
417 .concat(Name)
418 .concat(LineNumber)
419 .concat(0) // Size
420 .concat(0) // Align
421 .concat(0) // Offset
422 .concat(Flags)
423 .get(VMContext),
424 File.getFileNode(),
425 DIScope(getNonCompileUnitScope(Scope)).getRef(), Ty.getRef(),
426 Val};
Manman Ren3c6acec2013-06-07 18:35:53 +0000427 return DIDerivedType(MDNode::get(VMContext, Elts));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000428}
429
Eric Christopher98f9c232013-10-15 23:31:31 +0000430DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
431 unsigned LineNumber,
432 uint64_t SizeInBits,
433 uint64_t AlignInBits,
434 uint64_t OffsetInBits, unsigned Flags,
435 DIType Ty, MDNode *PropertyNode) {
Devang Patel44882172012-02-06 17:49:43 +0000436 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000437 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
438 .concat(Name)
439 .concat(LineNumber)
440 .concat(SizeInBits)
441 .concat(AlignInBits)
442 .concat(OffsetInBits)
443 .concat(Flags)
444 .get(VMContext),
445 File.getFileNode(), getNonCompileUnitScope(File), Ty,
446 PropertyNode};
Manman Ren3c6acec2013-06-07 18:35:53 +0000447 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel44882172012-02-06 17:49:43 +0000448}
449
Eric Christopher98f9c232013-10-15 23:31:31 +0000450DIObjCProperty
451DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
452 StringRef GetterName, StringRef SetterName,
453 unsigned PropertyAttributes, DIType Ty) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000454 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property)
455 .concat(Name)
456 .concat(LineNumber)
457 .concat(GetterName)
458 .concat(SetterName)
459 .concat(PropertyAttributes)
460 .get(VMContext),
461 File, Ty};
David Blaikie5692e722013-03-28 02:44:59 +0000462 return DIObjCProperty(MDNode::get(VMContext, Elts));
Devang Patelcc481592012-02-04 00:59:25 +0000463}
464
Eric Christopher3cc90fe2011-08-26 21:02:40 +0000465DITemplateTypeParameter
Devang Patel9b412732011-02-22 18:56:12 +0000466DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
Devang Patel3a9e65e2011-02-02 21:38:25 +0000467 DIType Ty, MDNode *File, unsigned LineNo,
468 unsigned ColumnNo) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000469 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter)
470 .concat(Name)
471 .concat(LineNo)
472 .concat(ColumnNo)
473 .get(VMContext),
474 DIScope(getNonCompileUnitScope(Context)).getRef(),
475 Ty.getRef(), File};
David Blaikie5692e722013-03-28 02:44:59 +0000476 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000477}
478
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000479static DITemplateValueParameter createTemplateValueParameterHelper(
480 LLVMContext &VMContext, unsigned Tag, DIDescriptor Context, StringRef Name,
481 DIType Ty, Value *Val, MDNode *File, unsigned LineNo, unsigned ColumnNo) {
Devang Patelbe933b42011-02-02 22:35:53 +0000482 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000483 HeaderBuilder::get(Tag).concat(Name).concat(LineNo).concat(ColumnNo).get(
484 VMContext),
485 DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), Val,
486 File};
David Blaikie5692e722013-03-28 02:44:59 +0000487 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
Devang Patelbe933b42011-02-02 22:35:53 +0000488}
489
David Blaikie2b380232013-06-22 18:59:11 +0000490DITemplateValueParameter
491DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000492 DIType Ty, Constant *Val, MDNode *File,
493 unsigned LineNo, unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000494 return createTemplateValueParameterHelper(
495 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty, Val,
496 File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000497}
498
499DITemplateValueParameter
500DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
501 DIType Ty, StringRef Val,
502 MDNode *File, unsigned LineNo,
503 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000504 return createTemplateValueParameterHelper(
505 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
David Blaikie2b380232013-06-22 18:59:11 +0000506 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
507}
508
509DITemplateValueParameter
510DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
511 DIType Ty, DIArray Val,
512 MDNode *File, unsigned LineNo,
513 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000514 return createTemplateValueParameterHelper(
515 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
516 Val, File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000517}
518
David Blaikiea7310a32013-03-26 23:46:39 +0000519DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
520 DIFile File, unsigned LineNumber,
521 uint64_t SizeInBits,
522 uint64_t AlignInBits,
523 uint64_t OffsetInBits,
524 unsigned Flags, DIType DerivedFrom,
525 DIArray Elements,
Manman Ren27552062013-09-06 23:54:23 +0000526 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000527 MDNode *TemplateParams,
528 StringRef UniqueIdentifier) {
Manman Ren74c188f2013-07-01 21:02:01 +0000529 assert((!Context || Context.isScope() || Context.isType()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000530 "createClassType should be called with a valid Context");
531 // TAG_class_type is encoded in DICompositeType format.
Eric Christopher17426692012-07-06 02:35:57 +0000532 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000533 HeaderBuilder::get(dwarf::DW_TAG_class_type)
534 .concat(Name)
535 .concat(LineNumber)
536 .concat(SizeInBits)
537 .concat(AlignInBits)
538 .concat(OffsetInBits)
539 .concat(Flags)
540 .concat(0)
541 .get(VMContext),
542 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
543 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams,
544 UniqueIdentifier.empty() ? nullptr
545 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikiea7310a32013-03-26 23:46:39 +0000546 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000547 assert(R.isCompositeType() &&
548 "createClassType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000549 if (!UniqueIdentifier.empty())
550 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000551 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000552}
553
David Blaikiebbe0e1a2013-02-25 01:07:18 +0000554DICompositeType DIBuilder::createStructType(DIDescriptor Context,
555 StringRef Name, DIFile File,
556 unsigned LineNumber,
557 uint64_t SizeInBits,
558 uint64_t AlignInBits,
559 unsigned Flags, DIType DerivedFrom,
560 DIArray Elements,
561 unsigned RunTimeLang,
Manman Ren27552062013-09-06 23:54:23 +0000562 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000563 StringRef UniqueIdentifier) {
Devang Patel746660f2010-12-07 23:25:47 +0000564 // TAG_structure_type is encoded in DICompositeType format.
565 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000566 HeaderBuilder::get(dwarf::DW_TAG_structure_type)
567 .concat(Name)
568 .concat(LineNumber)
569 .concat(SizeInBits)
570 .concat(AlignInBits)
571 .concat(0)
572 .concat(Flags)
573 .concat(RunTimeLang)
574 .get(VMContext),
575 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
576 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr,
577 UniqueIdentifier.empty() ? nullptr
578 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie085abe32013-03-11 23:21:19 +0000579 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000580 assert(R.isCompositeType() &&
581 "createStructType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000582 if (!UniqueIdentifier.empty())
583 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000584 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000585}
586
Eric Christopher17dd8f02013-04-02 22:55:52 +0000587DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
588 DIFile File, unsigned LineNumber,
589 uint64_t SizeInBits,
590 uint64_t AlignInBits, unsigned Flags,
591 DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000592 unsigned RunTimeLang,
593 StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000594 // TAG_union_type is encoded in DICompositeType format.
595 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000596 HeaderBuilder::get(dwarf::DW_TAG_union_type)
597 .concat(Name)
598 .concat(LineNumber)
599 .concat(SizeInBits)
600 .concat(AlignInBits)
601 .concat(0) // Offset
602 .concat(Flags)
603 .concat(RunTimeLang)
604 .get(VMContext),
605 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
606 nullptr, Elements, nullptr, nullptr,
607 UniqueIdentifier.empty() ? nullptr
608 : MDString::get(VMContext, UniqueIdentifier)};
Manman Ren0b410402013-08-29 23:17:54 +0000609 DICompositeType R(MDNode::get(VMContext, Elts));
610 if (!UniqueIdentifier.empty())
611 retainType(R);
612 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000613}
614
Manman Renf8a19672014-07-28 22:24:06 +0000615DISubroutineType DIBuilder::createSubroutineType(DIFile File,
616 DITypeArray ParameterTypes,
617 unsigned Flags) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000618 // TAG_subroutine_type is encoded in DICompositeType format.
619 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000620 HeaderBuilder::get(dwarf::DW_TAG_subroutine_type)
621 .concat(StringRef())
622 .concat(0) // Line
623 .concat(0) // Size
624 .concat(0) // Align
625 .concat(0) // Offset
626 .concat(Flags) // Flags
627 .concat(0)
628 .get(VMContext),
629 nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr,
630 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000631 };
Manman Renf8a19672014-07-28 22:24:06 +0000632 return DISubroutineType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000633}
634
David Blaikief11de2f2013-02-18 06:41:57 +0000635DICompositeType DIBuilder::createEnumerationType(
636 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
637 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000638 DIType UnderlyingType, StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000639 // TAG_enumeration_type is encoded in DICompositeType format.
640 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000641 HeaderBuilder::get(dwarf::DW_TAG_enumeration_type)
642 .concat(Name)
643 .concat(LineNumber)
644 .concat(SizeInBits)
645 .concat(AlignInBits)
646 .concat(0) // Offset
647 .concat(0) // Flags
648 .concat(0)
649 .get(VMContext),
650 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
651 UnderlyingType.getRef(), Elements, nullptr, nullptr,
652 UniqueIdentifier.empty() ? nullptr
653 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000654 DICompositeType CTy(MDNode::get(VMContext, Elts));
655 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000656 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000657 retainType(CTy);
658 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000659}
660
David Blaikief11de2f2013-02-18 06:41:57 +0000661DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
662 DIType Ty, DIArray Subscripts) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000663 // TAG_array_type is encoded in DICompositeType format.
664 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000665 HeaderBuilder::get(dwarf::DW_TAG_array_type)
666 .concat(StringRef())
667 .concat(0) // Line
668 .concat(Size)
669 .concat(AlignInBits)
670 .concat(0) // Offset
671 .concat(0) // Flags
672 .concat(0)
673 .get(VMContext),
674 nullptr, // Filename/Directory,
675 nullptr, // Unused
676 Ty.getRef(), Subscripts, nullptr, nullptr,
677 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000678 };
David Blaikie5692e722013-03-28 02:44:59 +0000679 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000680}
681
Manman Ren60711602013-06-07 03:13:46 +0000682DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
683 DIType Ty, DIArray Subscripts) {
Eric Christopher72a52952013-01-08 01:53:52 +0000684 // A vector is an array type with the FlagVector flag applied.
Devang Patel89ea4f22010-12-08 01:50:15 +0000685 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000686 HeaderBuilder::get(dwarf::DW_TAG_array_type)
687 .concat("")
688 .concat(0) // Line
689 .concat(Size)
690 .concat(AlignInBits)
691 .concat(0) // Offset
692 .concat(DIType::FlagVector)
693 .concat(0)
694 .get(VMContext),
695 nullptr, // Filename/Directory,
696 nullptr, // Unused
697 Ty.getRef(), Subscripts, nullptr, nullptr,
698 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000699 };
Manman Ren60711602013-06-07 03:13:46 +0000700 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000701}
Devang Patel746660f2010-12-07 23:25:47 +0000702
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000703static HeaderBuilder setTypeFlagsInHeader(StringRef Header,
704 unsigned FlagsToSet) {
705 DIHeaderFieldIterator I(Header);
706 std::advance(I, 6);
707
708 unsigned Flags;
709 if (I->getAsInteger(0, Flags))
710 Flags = 0;
711 Flags |= FlagsToSet;
712
713 return HeaderBuilder(Twine(I.getPrefix())).concat(Flags).concat(
714 I.getSuffix());
715}
716
717static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty,
718 unsigned FlagsToSet) {
719 SmallVector<Value *, 9> Elts;
720 MDNode *N = Ty;
721 assert(N && "Unexpected input DIType!");
722 // Update header field.
723 Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context));
724 for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I)
725 Elts.push_back(N->getOperand(I));
726
727 return DIType(MDNode::get(Context, Elts));
728}
729
Devang Patel9b412732011-02-22 18:56:12 +0000730DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel57c5a202010-11-04 15:01:38 +0000731 if (Ty.isArtificial())
732 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000733 return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000734}
Devang Patel746660f2010-12-07 23:25:47 +0000735
Eric Christophere3417762012-09-12 23:36:19 +0000736DIType DIBuilder::createObjectPointerType(DIType Ty) {
737 if (Ty.isObjectPointer())
738 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000739 unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
740 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000741}
742
Devang Patel9b412732011-02-22 18:56:12 +0000743void DIBuilder::retainType(DIType T) {
Manman Ren0b410402013-08-29 23:17:54 +0000744 AllRetainTypes.push_back(TrackingVH<MDNode>(T));
Devang Patel89ea4f22010-12-08 01:50:15 +0000745}
746
Manman Renf93ac4b2014-07-29 18:20:39 +0000747DIBasicType DIBuilder::createUnspecifiedParameter() {
Manman Ren72b07e82014-07-29 22:58:13 +0000748 return DIBasicType();
Devang Patel89ea4f22010-12-08 01:50:15 +0000749}
750
Eric Christopher98f9c232013-10-15 23:31:31 +0000751DICompositeType
752DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
753 DIFile F, unsigned Line, unsigned RuntimeLang,
754 uint64_t SizeInBits, uint64_t AlignInBits,
755 StringRef UniqueIdentifier) {
Eric Christopherae56eec2012-02-08 00:22:26 +0000756 // Create a temporary MDNode.
757 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000758 HeaderBuilder::get(Tag)
759 .concat(Name)
760 .concat(Line)
761 .concat(SizeInBits)
762 .concat(AlignInBits)
763 .concat(0) // Offset
764 .concat(DIDescriptor::FlagFwdDecl)
765 .concat(RuntimeLang)
766 .get(VMContext),
767 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
768 DIArray(), nullptr,
769 nullptr, // TemplateParams
770 UniqueIdentifier.empty() ? nullptr
771 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikied3f094a2014-05-06 03:41:57 +0000772 MDNode *Node = MDNode::get(VMContext, Elts);
773 DICompositeType RetTy(Node);
774 assert(RetTy.isCompositeType() &&
775 "createForwardDecl result should be a DIType");
776 if (!UniqueIdentifier.empty())
777 retainType(RetTy);
778 return RetTy;
779}
780
David Blaikied3f094a2014-05-06 03:41:57 +0000781DICompositeType DIBuilder::createReplaceableForwardDecl(
782 unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
783 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
784 StringRef UniqueIdentifier) {
785 // Create a temporary MDNode.
786 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000787 HeaderBuilder::get(Tag)
788 .concat(Name)
789 .concat(Line)
790 .concat(SizeInBits)
791 .concat(AlignInBits)
792 .concat(0) // Offset
793 .concat(DIDescriptor::FlagFwdDecl)
794 .concat(RuntimeLang)
795 .get(VMContext),
796 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
797 DIArray(), nullptr,
798 nullptr, // TemplateParams
799 UniqueIdentifier.empty() ? nullptr
800 : MDString::get(VMContext, UniqueIdentifier)};
Eric Christopherae56eec2012-02-08 00:22:26 +0000801 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
David Blaikied4e106e2013-08-16 20:42:14 +0000802 DICompositeType RetTy(Node);
803 assert(RetTy.isCompositeType() &&
Frederic Rissa8734142014-09-10 16:03:14 +0000804 "createReplaceableForwardDecl result should be a DIType");
Manman Ren0b410402013-08-29 23:17:54 +0000805 if (!UniqueIdentifier.empty())
806 retainType(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000807 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000808}
809
Jay Foaddbf81d82011-04-24 10:11:03 +0000810DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
Jay Foaddbf81d82011-04-24 10:11:03 +0000811 return DIArray(MDNode::get(VMContext, Elements));
Devang Patel746660f2010-12-07 23:25:47 +0000812}
813
Manman Ren1a125c92014-07-28 19:33:20 +0000814DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
815 SmallVector<llvm::Value *, 16> Elts;
816 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
817 if (Elements[i] && isa<MDNode>(Elements[i]))
818 Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
819 else
820 Elts.push_back(Elements[i]);
821 }
822 return DITypeArray(MDNode::get(VMContext, Elts));
823}
824
Bill Wendlingd7767122012-12-04 21:34:03 +0000825DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000826 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type)
827 .concat(Lo)
828 .concat(Count)
829 .get(VMContext)};
Devang Patel89ea4f22010-12-08 01:50:15 +0000830
Devang Patel0c773242011-04-18 23:51:03 +0000831 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000832}
833
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000834static DIGlobalVariable createGlobalVariableHelper(
835 LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
836 StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty,
837 bool isLocalToUnit, Constant *Val, MDNode *Decl, bool isDefinition,
838 std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000839 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable)
840 .concat(Name)
841 .concat(Name)
842 .concat(LinkageName)
843 .concat(LineNumber)
844 .concat(isLocalToUnit)
845 .concat(isDefinition)
846 .get(VMContext),
Manman Ren554865d2014-11-18 00:29:08 +0000847 DIScope(getNonCompileUnitScope(Context)).getRef(), F, Ty, Val,
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000848 DIDescriptor(Decl)};
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000849
850 return DIGlobalVariable(CreateFunc(Elts));
851}
852
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000853DIGlobalVariable DIBuilder::createGlobalVariable(
854 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
855 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
856 MDNode *Decl) {
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000857 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000858 LineNumber, Ty, isLocalToUnit, Val, Decl, true,
859 [&] (ArrayRef<Value *> Elts) -> MDNode * {
860 MDNode *Node = MDNode::get(VMContext, Elts);
861 AllGVs.push_back(Node);
862 return Node;
863 });
864}
865
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000866DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl(
867 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
868 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
869 MDNode *Decl) {
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000870 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000871 LineNumber, Ty, isLocalToUnit, Val, Decl, false,
872 [&] (ArrayRef<Value *> Elts) {
873 return MDNode::getTemporary(VMContext, Elts);
874 });
Devang Patel746660f2010-12-07 23:25:47 +0000875}
876
Devang Patel9b412732011-02-22 18:56:12 +0000877DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel63f83cd2010-12-07 23:58:00 +0000878 StringRef Name, DIFile File,
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000879 unsigned LineNo, DITypeRef Ty,
Devang Patel40eee1e2011-03-01 22:58:13 +0000880 bool AlwaysPreserve, unsigned Flags,
881 unsigned ArgNo) {
David Blaikie085abe32013-03-11 23:21:19 +0000882 DIDescriptor Context(getNonCompileUnitScope(Scope));
Manman Ren74c188f2013-07-01 21:02:01 +0000883 assert((!Context || Context.isScope()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000884 "createLocalVariable should be called with a valid Context");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000885 Value *Elts[] = {HeaderBuilder::get(Tag)
886 .concat(Name)
887 .concat(LineNo | (ArgNo << 24))
888 .concat(Flags)
889 .get(VMContext),
890 getNonCompileUnitScope(Scope), File, Ty};
Devang Patel0c773242011-04-18 23:51:03 +0000891 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel63f83cd2010-12-07 23:58:00 +0000892 if (AlwaysPreserve) {
893 // The optimizer may remove local variable. If there is an interest
894 // to preserve variable info in such situation then stash it in a
895 // named mdnode.
896 DISubprogram Fn(getDISubprogram(Scope));
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000897 assert(Fn && "Missing subprogram for local variable");
898 PreservedVariables[Fn].push_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000899 }
Manman Rend0e67aa2013-07-02 18:37:35 +0000900 DIVariable RetVar(Node);
901 assert(RetVar.isVariable() &&
Manman Ren74c188f2013-07-01 21:02:01 +0000902 "createLocalVariable should return a valid DIVariable");
Manman Rend0e67aa2013-07-02 18:37:35 +0000903 return RetVar;
Devang Patel63f83cd2010-12-07 23:58:00 +0000904}
905
Duncan P. N. Exon Smith611afb22014-10-01 20:26:08 +0000906DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Addr) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000907 auto Header = HeaderBuilder::get(DW_TAG_expression);
Duncan P. N. Exon Smith611afb22014-10-01 20:26:08 +0000908 for (int64_t I : Addr)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000909 Header.concat(I);
910 Value *Elts[] = {Header.get(VMContext)};
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000911 return DIExpression(MDNode::get(VMContext, Elts));
Devang Patel746660f2010-12-07 23:25:47 +0000912}
913
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000914DIExpression DIBuilder::createPieceExpression(unsigned OffsetInBytes,
915 unsigned SizeInBytes) {
916 int64_t Addr[] = {dwarf::DW_OP_piece, OffsetInBytes, SizeInBytes};
917 return createExpression(Addr);
918}
919
Eric Christopher98f9c232013-10-15 23:31:31 +0000920DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
921 StringRef LinkageName, DIFile File,
922 unsigned LineNo, DICompositeType Ty,
Manman Renc50fa112013-10-10 18:40:01 +0000923 bool isLocalToUnit, bool isDefinition,
Eric Christopher98f9c232013-10-15 23:31:31 +0000924 unsigned ScopeLine, unsigned Flags,
925 bool isOptimized, Function *Fn,
926 MDNode *TParams, MDNode *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000927 // dragonegg does not generate identifier for types, so using an empty map
928 // to resolve the context should be fine.
929 DITypeIdentifierMap EmptyMap;
930 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
931 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
932 Flags, isOptimized, Fn, TParams, Decl);
933}
934
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000935static DISubprogram
936createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
937 StringRef LinkageName, DIFile File, unsigned LineNo,
938 DICompositeType Ty, bool isLocalToUnit, bool isDefinition,
939 unsigned ScopeLine, unsigned Flags, bool isOptimized,
940 Function *Fn, MDNode *TParams, MDNode *Decl,
941 std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
David Blaikie5174c842013-05-22 23:22:18 +0000942 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
943 "function types should be subroutines");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000944 Value *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
Devang Patelb68c6232010-12-08 20:42:44 +0000945 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000946 HeaderBuilder::get(dwarf::DW_TAG_subprogram)
947 .concat(Name)
948 .concat(Name)
949 .concat(LinkageName)
950 .concat(LineNo)
951 .concat(isLocalToUnit)
952 .concat(isDefinition)
953 .concat(0)
954 .concat(0)
955 .concat(Flags)
956 .concat(isOptimized)
957 .concat(ScopeLine)
958 .get(VMContext),
959 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(), Ty,
960 nullptr, Fn, TParams, Decl, MDNode::getTemporary(VMContext, TElts)};
Devang Patelb68c6232010-12-08 20:42:44 +0000961
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000962 DISubprogram S(CreateFunc(Elts));
Eric Christopher961959f2014-02-28 21:27:59 +0000963 assert(S.isSubprogram() &&
964 "createFunction should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +0000965 return S;
Devang Patelb68c6232010-12-08 20:42:44 +0000966}
967
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000968
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000969DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
970 StringRef LinkageName, DIFile File,
971 unsigned LineNo, DICompositeType Ty,
972 bool isLocalToUnit, bool isDefinition,
973 unsigned ScopeLine, unsigned Flags,
974 bool isOptimized, Function *Fn,
975 MDNode *TParams, MDNode *Decl) {
976 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
977 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
978 Flags, isOptimized, Fn, TParams, Decl,
979 [&] (ArrayRef<Value *> Elts) -> MDNode *{
980 MDNode *Node = MDNode::get(VMContext, Elts);
981 // Create a named metadata so that we
982 // do not lose this mdnode.
983 if (isDefinition)
984 AllSubprograms.push_back(Node);
985 return Node;
986 });
987}
988
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000989DISubprogram
990DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
991 StringRef LinkageName, DIFile File,
992 unsigned LineNo, DICompositeType Ty,
993 bool isLocalToUnit, bool isDefinition,
994 unsigned ScopeLine, unsigned Flags,
995 bool isOptimized, Function *Fn,
996 MDNode *TParams, MDNode *Decl) {
997 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
998 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
999 Flags, isOptimized, Fn, TParams, Decl,
1000 [&] (ArrayRef<Value *> Elts) {
1001 return MDNode::getTemporary(VMContext, Elts);
1002 });
1003}
1004
Eric Christopher98f9c232013-10-15 23:31:31 +00001005DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1006 StringRef LinkageName, DIFile F,
David Blaikie5174c842013-05-22 23:22:18 +00001007 unsigned LineNo, DICompositeType Ty,
Eric Christopher98f9c232013-10-15 23:31:31 +00001008 bool isLocalToUnit, bool isDefinition,
Devang Patelb68c6232010-12-08 20:42:44 +00001009 unsigned VK, unsigned VIndex,
Eric Christopher98f9c232013-10-15 23:31:31 +00001010 DIType VTableHolder, unsigned Flags,
1011 bool isOptimized, Function *Fn,
Devang Patel9f738842011-04-05 22:52:06 +00001012 MDNode *TParam) {
David Blaikie5174c842013-05-22 23:22:18 +00001013 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1014 "function types should be subroutines");
Eric Christopher5cb56322013-10-15 23:31:36 +00001015 assert(getNonCompileUnitScope(Context) &&
1016 "Methods should have both a Context and a context that isn't "
1017 "the compile unit.");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001018 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
1019 .concat(Name)
1020 .concat(Name)
1021 .concat(LinkageName)
1022 .concat(LineNo)
1023 .concat(isLocalToUnit)
1024 .concat(isDefinition)
1025 .concat(VK)
1026 .concat(VIndex)
1027 .concat(Flags)
1028 .concat(isOptimized)
1029 .concat(LineNo)
1030 // FIXME: Do we want to use different scope/lines?
1031 .get(VMContext),
1032 F.getFileNode(), DIScope(Context).getRef(), Ty,
1033 VTableHolder.getRef(), Fn, TParam, nullptr, nullptr};
Devang Patel0c773242011-04-18 23:51:03 +00001034 MDNode *Node = MDNode::get(VMContext, Elts);
David Blaikie595eb442013-02-18 07:10:22 +00001035 if (isDefinition)
1036 AllSubprograms.push_back(Node);
David Blaikiecc8d0902013-03-21 20:28:52 +00001037 DISubprogram S(Node);
Manman Ren74c188f2013-07-01 21:02:01 +00001038 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +00001039 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001040}
1041
Devang Patel9b412732011-02-22 18:56:12 +00001042DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patel746660f2010-12-07 23:25:47 +00001043 DIFile File, unsigned LineNo) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001044 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace)
1045 .concat(Name)
1046 .concat(LineNo)
1047 .get(VMContext),
1048 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001049 DINameSpace R(MDNode::get(VMContext, Elts));
1050 assert(R.Verify() &&
1051 "createNameSpace should return a verifiable DINameSpace");
1052 return R;
Devang Patel746660f2010-12-07 23:25:47 +00001053}
1054
Eric Christopher6647b832011-10-11 22:59:11 +00001055DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001056 DIFile File,
1057 unsigned Discriminator) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001058 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1059 .concat(Discriminator)
1060 .get(VMContext),
1061 File.getFileNode(), Scope};
David Blaikie085abe32013-03-11 23:21:19 +00001062 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1063 assert(
1064 R.Verify() &&
1065 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1066 return R;
Eric Christopher6647b832011-10-11 22:59:11 +00001067}
1068
Devang Patel9b412732011-02-22 18:56:12 +00001069DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001070 unsigned Line, unsigned Col) {
David Blaikie6c217162014-05-15 20:09:55 +00001071 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1072 // I believe the right way is to have a self-referential element in the node.
1073 // Also: why do we bother with line/column - they're not used and the
1074 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1075 // for uniquing, yet then we have this other solution (because line/col were
1076 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1077
Adrian Prantl21e8d4a2013-06-24 21:19:43 +00001078 // Defeat MDNode uniquing for lexical blocks by using unique id.
Devang Patel89ea4f22010-12-08 01:50:15 +00001079 static unsigned int unique_id = 0;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001080 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1081 .concat(Line)
1082 .concat(Col)
1083 .concat(unique_id++)
1084 .get(VMContext),
1085 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001086 DILexicalBlock R(MDNode::get(VMContext, Elts));
1087 assert(R.Verify() &&
1088 "createLexicalBlock should return a verifiable DILexicalBlock");
1089 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +00001090}
1091
Devang Patel9b412732011-02-22 18:56:12 +00001092Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001093 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001094 Instruction *InsertBefore) {
1095 assert(Storage && "no storage passed to dbg.declare");
Manman Ren9822a112013-06-29 05:01:19 +00001096 assert(VarInfo.isVariable() &&
1097 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001098 if (!DeclareFn)
1099 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1100
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001101 Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
Jay Foad5bd375a2011-07-15 08:37:34 +00001102 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001103}
1104
Devang Patel9b412732011-02-22 18:56:12 +00001105Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001106 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001107 BasicBlock *InsertAtEnd) {
1108 assert(Storage && "no storage passed to dbg.declare");
Manman Ren9822a112013-06-29 05:01:19 +00001109 assert(VarInfo.isVariable() &&
1110 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001111 if (!DeclareFn)
1112 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1113
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001114 Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
Devang Patel746660f2010-12-07 23:25:47 +00001115
1116 // If this block already has a terminator then insert this intrinsic
1117 // before the terminator.
1118 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foad5bd375a2011-07-15 08:37:34 +00001119 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patel746660f2010-12-07 23:25:47 +00001120 else
Jay Foad5bd375a2011-07-15 08:37:34 +00001121 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001122}
1123
Devang Patel9b412732011-02-22 18:56:12 +00001124Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001125 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001126 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001127 Instruction *InsertBefore) {
1128 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001129 assert(VarInfo.isVariable() &&
1130 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001131 if (!ValueFn)
1132 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1133
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001134 Value *Args[] = {MDNode::get(V->getContext(), V),
1135 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1136 VarInfo, Expr};
Jay Foad5bd375a2011-07-15 08:37:34 +00001137 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001138}
1139
Devang Patel9b412732011-02-22 18:56:12 +00001140Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001141 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001142 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001143 BasicBlock *InsertAtEnd) {
1144 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001145 assert(VarInfo.isVariable() &&
1146 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001147 if (!ValueFn)
1148 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1149
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001150 Value *Args[] = {MDNode::get(V->getContext(), V),
1151 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1152 VarInfo, Expr};
Jay Foad5bd375a2011-07-15 08:37:34 +00001153 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001154}