blob: 1df54e550980f6d0abef5fc6cb1ded72b7f998a0 [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++)
67 if (RetainSet.insert(AllRetainTypes[I]))
68 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
Manman Ren3c6acec2013-06-07 18:35:53 +0000409DIDerivedType
410DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
411 DIFile File, unsigned LineNumber,
412 DIType Ty, unsigned Flags,
413 llvm::Value *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,
492 DIType Ty, Value *Val,
493 MDNode *File, unsigned LineNo,
494 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000495 return createTemplateValueParameterHelper(
496 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty, Val,
497 File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000498}
499
500DITemplateValueParameter
501DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
502 DIType Ty, StringRef Val,
503 MDNode *File, unsigned LineNo,
504 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000505 return createTemplateValueParameterHelper(
506 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
David Blaikie2b380232013-06-22 18:59:11 +0000507 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
508}
509
510DITemplateValueParameter
511DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
512 DIType Ty, DIArray Val,
513 MDNode *File, unsigned LineNo,
514 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000515 return createTemplateValueParameterHelper(
516 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
517 Val, File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000518}
519
David Blaikiea7310a32013-03-26 23:46:39 +0000520DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
521 DIFile File, unsigned LineNumber,
522 uint64_t SizeInBits,
523 uint64_t AlignInBits,
524 uint64_t OffsetInBits,
525 unsigned Flags, DIType DerivedFrom,
526 DIArray Elements,
Manman Ren27552062013-09-06 23:54:23 +0000527 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000528 MDNode *TemplateParams,
529 StringRef UniqueIdentifier) {
Manman Ren74c188f2013-07-01 21:02:01 +0000530 assert((!Context || Context.isScope() || Context.isType()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000531 "createClassType should be called with a valid Context");
532 // TAG_class_type is encoded in DICompositeType format.
Eric Christopher17426692012-07-06 02:35:57 +0000533 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000534 HeaderBuilder::get(dwarf::DW_TAG_class_type)
535 .concat(Name)
536 .concat(LineNumber)
537 .concat(SizeInBits)
538 .concat(AlignInBits)
539 .concat(OffsetInBits)
540 .concat(Flags)
541 .concat(0)
542 .get(VMContext),
543 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
544 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams,
545 UniqueIdentifier.empty() ? nullptr
546 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikiea7310a32013-03-26 23:46:39 +0000547 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000548 assert(R.isCompositeType() &&
549 "createClassType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000550 if (!UniqueIdentifier.empty())
551 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000552 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000553}
554
David Blaikiebbe0e1a2013-02-25 01:07:18 +0000555DICompositeType DIBuilder::createStructType(DIDescriptor Context,
556 StringRef Name, DIFile File,
557 unsigned LineNumber,
558 uint64_t SizeInBits,
559 uint64_t AlignInBits,
560 unsigned Flags, DIType DerivedFrom,
561 DIArray Elements,
562 unsigned RunTimeLang,
Manman Ren27552062013-09-06 23:54:23 +0000563 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000564 StringRef UniqueIdentifier) {
Devang Patel746660f2010-12-07 23:25:47 +0000565 // TAG_structure_type is encoded in DICompositeType format.
566 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000567 HeaderBuilder::get(dwarf::DW_TAG_structure_type)
568 .concat(Name)
569 .concat(LineNumber)
570 .concat(SizeInBits)
571 .concat(AlignInBits)
572 .concat(0)
573 .concat(Flags)
574 .concat(RunTimeLang)
575 .get(VMContext),
576 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
577 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr,
578 UniqueIdentifier.empty() ? nullptr
579 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie085abe32013-03-11 23:21:19 +0000580 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000581 assert(R.isCompositeType() &&
582 "createStructType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000583 if (!UniqueIdentifier.empty())
584 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000585 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000586}
587
Eric Christopher17dd8f02013-04-02 22:55:52 +0000588DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
589 DIFile File, unsigned LineNumber,
590 uint64_t SizeInBits,
591 uint64_t AlignInBits, unsigned Flags,
592 DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000593 unsigned RunTimeLang,
594 StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000595 // TAG_union_type is encoded in DICompositeType format.
596 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000597 HeaderBuilder::get(dwarf::DW_TAG_union_type)
598 .concat(Name)
599 .concat(LineNumber)
600 .concat(SizeInBits)
601 .concat(AlignInBits)
602 .concat(0) // Offset
603 .concat(Flags)
604 .concat(RunTimeLang)
605 .get(VMContext),
606 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
607 nullptr, Elements, nullptr, nullptr,
608 UniqueIdentifier.empty() ? nullptr
609 : MDString::get(VMContext, UniqueIdentifier)};
Manman Ren0b410402013-08-29 23:17:54 +0000610 DICompositeType R(MDNode::get(VMContext, Elts));
611 if (!UniqueIdentifier.empty())
612 retainType(R);
613 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000614}
615
Manman Renf8a19672014-07-28 22:24:06 +0000616DISubroutineType DIBuilder::createSubroutineType(DIFile File,
617 DITypeArray ParameterTypes,
618 unsigned Flags) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000619 // TAG_subroutine_type is encoded in DICompositeType format.
620 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000621 HeaderBuilder::get(dwarf::DW_TAG_subroutine_type)
622 .concat(StringRef())
623 .concat(0) // Line
624 .concat(0) // Size
625 .concat(0) // Align
626 .concat(0) // Offset
627 .concat(Flags) // Flags
628 .concat(0)
629 .get(VMContext),
630 nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr,
631 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000632 };
Manman Renf8a19672014-07-28 22:24:06 +0000633 return DISubroutineType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000634}
635
David Blaikief11de2f2013-02-18 06:41:57 +0000636DICompositeType DIBuilder::createEnumerationType(
637 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
638 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000639 DIType UnderlyingType, StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000640 // TAG_enumeration_type is encoded in DICompositeType format.
641 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000642 HeaderBuilder::get(dwarf::DW_TAG_enumeration_type)
643 .concat(Name)
644 .concat(LineNumber)
645 .concat(SizeInBits)
646 .concat(AlignInBits)
647 .concat(0) // Offset
648 .concat(0) // Flags
649 .concat(0)
650 .get(VMContext),
651 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
652 UnderlyingType.getRef(), Elements, nullptr, nullptr,
653 UniqueIdentifier.empty() ? nullptr
654 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000655 DICompositeType CTy(MDNode::get(VMContext, Elts));
656 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000657 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000658 retainType(CTy);
659 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000660}
661
David Blaikief11de2f2013-02-18 06:41:57 +0000662DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
663 DIType Ty, DIArray Subscripts) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000664 // TAG_array_type is encoded in DICompositeType format.
665 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000666 HeaderBuilder::get(dwarf::DW_TAG_array_type)
667 .concat(StringRef())
668 .concat(0) // Line
669 .concat(Size)
670 .concat(AlignInBits)
671 .concat(0) // Offset
672 .concat(0) // Flags
673 .concat(0)
674 .get(VMContext),
675 nullptr, // Filename/Directory,
676 nullptr, // Unused
677 Ty.getRef(), Subscripts, nullptr, nullptr,
678 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000679 };
David Blaikie5692e722013-03-28 02:44:59 +0000680 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000681}
682
Manman Ren60711602013-06-07 03:13:46 +0000683DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
684 DIType Ty, DIArray Subscripts) {
Eric Christopher72a52952013-01-08 01:53:52 +0000685 // A vector is an array type with the FlagVector flag applied.
Devang Patel89ea4f22010-12-08 01:50:15 +0000686 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000687 HeaderBuilder::get(dwarf::DW_TAG_array_type)
688 .concat("")
689 .concat(0) // Line
690 .concat(Size)
691 .concat(AlignInBits)
692 .concat(0) // Offset
693 .concat(DIType::FlagVector)
694 .concat(0)
695 .get(VMContext),
696 nullptr, // Filename/Directory,
697 nullptr, // Unused
698 Ty.getRef(), Subscripts, nullptr, nullptr,
699 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000700 };
Manman Ren60711602013-06-07 03:13:46 +0000701 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000702}
Devang Patel746660f2010-12-07 23:25:47 +0000703
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000704static HeaderBuilder setTypeFlagsInHeader(StringRef Header,
705 unsigned FlagsToSet) {
706 DIHeaderFieldIterator I(Header);
707 std::advance(I, 6);
708
709 unsigned Flags;
710 if (I->getAsInteger(0, Flags))
711 Flags = 0;
712 Flags |= FlagsToSet;
713
714 return HeaderBuilder(Twine(I.getPrefix())).concat(Flags).concat(
715 I.getSuffix());
716}
717
718static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty,
719 unsigned FlagsToSet) {
720 SmallVector<Value *, 9> Elts;
721 MDNode *N = Ty;
722 assert(N && "Unexpected input DIType!");
723 // Update header field.
724 Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context));
725 for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I)
726 Elts.push_back(N->getOperand(I));
727
728 return DIType(MDNode::get(Context, Elts));
729}
730
Devang Patel9b412732011-02-22 18:56:12 +0000731DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel57c5a202010-11-04 15:01:38 +0000732 if (Ty.isArtificial())
733 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000734 return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000735}
Devang Patel746660f2010-12-07 23:25:47 +0000736
Eric Christophere3417762012-09-12 23:36:19 +0000737DIType DIBuilder::createObjectPointerType(DIType Ty) {
738 if (Ty.isObjectPointer())
739 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000740 unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
741 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000742}
743
Devang Patel9b412732011-02-22 18:56:12 +0000744void DIBuilder::retainType(DIType T) {
Manman Ren0b410402013-08-29 23:17:54 +0000745 AllRetainTypes.push_back(TrackingVH<MDNode>(T));
Devang Patel89ea4f22010-12-08 01:50:15 +0000746}
747
Manman Renf93ac4b2014-07-29 18:20:39 +0000748DIBasicType DIBuilder::createUnspecifiedParameter() {
Manman Ren72b07e82014-07-29 22:58:13 +0000749 return DIBasicType();
Devang Patel89ea4f22010-12-08 01:50:15 +0000750}
751
Eric Christopher98f9c232013-10-15 23:31:31 +0000752DICompositeType
753DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
754 DIFile F, unsigned Line, unsigned RuntimeLang,
755 uint64_t SizeInBits, uint64_t AlignInBits,
756 StringRef UniqueIdentifier) {
Eric Christopherae56eec2012-02-08 00:22:26 +0000757 // Create a temporary MDNode.
758 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000759 HeaderBuilder::get(Tag)
760 .concat(Name)
761 .concat(Line)
762 .concat(SizeInBits)
763 .concat(AlignInBits)
764 .concat(0) // Offset
765 .concat(DIDescriptor::FlagFwdDecl)
766 .concat(RuntimeLang)
767 .get(VMContext),
768 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
769 DIArray(), nullptr,
770 nullptr, // TemplateParams
771 UniqueIdentifier.empty() ? nullptr
772 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikied3f094a2014-05-06 03:41:57 +0000773 MDNode *Node = MDNode::get(VMContext, Elts);
774 DICompositeType RetTy(Node);
775 assert(RetTy.isCompositeType() &&
776 "createForwardDecl result should be a DIType");
777 if (!UniqueIdentifier.empty())
778 retainType(RetTy);
779 return RetTy;
780}
781
David Blaikied3f094a2014-05-06 03:41:57 +0000782DICompositeType DIBuilder::createReplaceableForwardDecl(
783 unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
784 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
785 StringRef UniqueIdentifier) {
786 // Create a temporary MDNode.
787 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000788 HeaderBuilder::get(Tag)
789 .concat(Name)
790 .concat(Line)
791 .concat(SizeInBits)
792 .concat(AlignInBits)
793 .concat(0) // Offset
794 .concat(DIDescriptor::FlagFwdDecl)
795 .concat(RuntimeLang)
796 .get(VMContext),
797 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
798 DIArray(), nullptr,
799 nullptr, // TemplateParams
800 UniqueIdentifier.empty() ? nullptr
801 : MDString::get(VMContext, UniqueIdentifier)};
Eric Christopherae56eec2012-02-08 00:22:26 +0000802 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
David Blaikied4e106e2013-08-16 20:42:14 +0000803 DICompositeType RetTy(Node);
804 assert(RetTy.isCompositeType() &&
Frederic Rissa8734142014-09-10 16:03:14 +0000805 "createReplaceableForwardDecl result should be a DIType");
Manman Ren0b410402013-08-29 23:17:54 +0000806 if (!UniqueIdentifier.empty())
807 retainType(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000808 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000809}
810
Jay Foaddbf81d82011-04-24 10:11:03 +0000811DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
Jay Foaddbf81d82011-04-24 10:11:03 +0000812 return DIArray(MDNode::get(VMContext, Elements));
Devang Patel746660f2010-12-07 23:25:47 +0000813}
814
Manman Ren1a125c92014-07-28 19:33:20 +0000815DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
816 SmallVector<llvm::Value *, 16> Elts;
817 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
818 if (Elements[i] && isa<MDNode>(Elements[i]))
819 Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
820 else
821 Elts.push_back(Elements[i]);
822 }
823 return DITypeArray(MDNode::get(VMContext, Elts));
824}
825
Bill Wendlingd7767122012-12-04 21:34:03 +0000826DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000827 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type)
828 .concat(Lo)
829 .concat(Count)
830 .get(VMContext)};
Devang Patel89ea4f22010-12-08 01:50:15 +0000831
Devang Patel0c773242011-04-18 23:51:03 +0000832 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000833}
834
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000835static DIGlobalVariable
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000836createGlobalVariableHelper(LLVMContext &VMContext, DIDescriptor Context,
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000837 StringRef Name, StringRef LinkageName, DIFile F,
838 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit,
839 Value *Val, MDNode *Decl, bool isDefinition,
840 std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000841 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable)
842 .concat(Name)
843 .concat(Name)
844 .concat(LinkageName)
845 .concat(LineNumber)
846 .concat(isLocalToUnit)
847 .concat(isDefinition)
848 .get(VMContext),
849 getNonCompileUnitScope(Context), F, Ty, Val,
850 DIDescriptor(Decl)};
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000851
852 return DIGlobalVariable(CreateFunc(Elts));
853}
854
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000855DIGlobalVariable DIBuilder::createGlobalVariable(DIDescriptor Context,
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000856 StringRef Name,
857 StringRef LinkageName,
858 DIFile F, unsigned LineNumber,
859 DITypeRef Ty,
860 bool isLocalToUnit,
861 Value *Val, MDNode *Decl) {
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000862 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000863 LineNumber, Ty, isLocalToUnit, Val, Decl, true,
864 [&] (ArrayRef<Value *> Elts) -> MDNode * {
865 MDNode *Node = MDNode::get(VMContext, Elts);
866 AllGVs.push_back(Node);
867 return Node;
868 });
869}
870
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000871DIGlobalVariable
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000872DIBuilder::createTempGlobalVariableFwdDecl(DIDescriptor Context,
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000873 StringRef Name,
874 StringRef LinkageName,
875 DIFile F, unsigned LineNumber,
876 DITypeRef Ty,
877 bool isLocalToUnit,
878 Value *Val, MDNode *Decl) {
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000879 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000880 LineNumber, Ty, isLocalToUnit, Val, Decl, false,
881 [&] (ArrayRef<Value *> Elts) {
882 return MDNode::getTemporary(VMContext, Elts);
883 });
Devang Patel746660f2010-12-07 23:25:47 +0000884}
885
Devang Patel9b412732011-02-22 18:56:12 +0000886DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel63f83cd2010-12-07 23:58:00 +0000887 StringRef Name, DIFile File,
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000888 unsigned LineNo, DITypeRef Ty,
Devang Patel40eee1e2011-03-01 22:58:13 +0000889 bool AlwaysPreserve, unsigned Flags,
890 unsigned ArgNo) {
David Blaikie085abe32013-03-11 23:21:19 +0000891 DIDescriptor Context(getNonCompileUnitScope(Scope));
Manman Ren74c188f2013-07-01 21:02:01 +0000892 assert((!Context || Context.isScope()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000893 "createLocalVariable should be called with a valid Context");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000894 Value *Elts[] = {HeaderBuilder::get(Tag)
895 .concat(Name)
896 .concat(LineNo | (ArgNo << 24))
897 .concat(Flags)
898 .get(VMContext),
899 getNonCompileUnitScope(Scope), File, Ty};
Devang Patel0c773242011-04-18 23:51:03 +0000900 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel63f83cd2010-12-07 23:58:00 +0000901 if (AlwaysPreserve) {
902 // The optimizer may remove local variable. If there is an interest
903 // to preserve variable info in such situation then stash it in a
904 // named mdnode.
905 DISubprogram Fn(getDISubprogram(Scope));
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000906 assert(Fn && "Missing subprogram for local variable");
907 PreservedVariables[Fn].push_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000908 }
Manman Rend0e67aa2013-07-02 18:37:35 +0000909 DIVariable RetVar(Node);
910 assert(RetVar.isVariable() &&
Manman Ren74c188f2013-07-01 21:02:01 +0000911 "createLocalVariable should return a valid DIVariable");
Manman Rend0e67aa2013-07-02 18:37:35 +0000912 return RetVar;
Devang Patel63f83cd2010-12-07 23:58:00 +0000913}
914
Duncan P. N. Exon Smith611afb22014-10-01 20:26:08 +0000915DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Addr) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000916 auto Header = HeaderBuilder::get(DW_TAG_expression);
Duncan P. N. Exon Smith611afb22014-10-01 20:26:08 +0000917 for (int64_t I : Addr)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000918 Header.concat(I);
919 Value *Elts[] = {Header.get(VMContext)};
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000920 return DIExpression(MDNode::get(VMContext, Elts));
Devang Patel746660f2010-12-07 23:25:47 +0000921}
922
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000923DIExpression DIBuilder::createPieceExpression(unsigned OffsetInBytes,
924 unsigned SizeInBytes) {
925 int64_t Addr[] = {dwarf::DW_OP_piece, OffsetInBytes, SizeInBytes};
926 return createExpression(Addr);
927}
928
Eric Christopher98f9c232013-10-15 23:31:31 +0000929DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
930 StringRef LinkageName, DIFile File,
931 unsigned LineNo, DICompositeType Ty,
Manman Renc50fa112013-10-10 18:40:01 +0000932 bool isLocalToUnit, bool isDefinition,
Eric Christopher98f9c232013-10-15 23:31:31 +0000933 unsigned ScopeLine, unsigned Flags,
934 bool isOptimized, Function *Fn,
935 MDNode *TParams, MDNode *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000936 // dragonegg does not generate identifier for types, so using an empty map
937 // to resolve the context should be fine.
938 DITypeIdentifierMap EmptyMap;
939 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
940 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
941 Flags, isOptimized, Fn, TParams, Decl);
942}
943
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000944static DISubprogram
945createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
946 StringRef LinkageName, DIFile File, unsigned LineNo,
947 DICompositeType Ty, bool isLocalToUnit, bool isDefinition,
948 unsigned ScopeLine, unsigned Flags, bool isOptimized,
949 Function *Fn, MDNode *TParams, MDNode *Decl,
950 std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
David Blaikie5174c842013-05-22 23:22:18 +0000951 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
952 "function types should be subroutines");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000953 Value *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
Devang Patelb68c6232010-12-08 20:42:44 +0000954 Value *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000955 HeaderBuilder::get(dwarf::DW_TAG_subprogram)
956 .concat(Name)
957 .concat(Name)
958 .concat(LinkageName)
959 .concat(LineNo)
960 .concat(isLocalToUnit)
961 .concat(isDefinition)
962 .concat(0)
963 .concat(0)
964 .concat(Flags)
965 .concat(isOptimized)
966 .concat(ScopeLine)
967 .get(VMContext),
968 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(), Ty,
969 nullptr, Fn, TParams, Decl, MDNode::getTemporary(VMContext, TElts)};
Devang Patelb68c6232010-12-08 20:42:44 +0000970
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000971 DISubprogram S(CreateFunc(Elts));
Eric Christopher961959f2014-02-28 21:27:59 +0000972 assert(S.isSubprogram() &&
973 "createFunction should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +0000974 return S;
Devang Patelb68c6232010-12-08 20:42:44 +0000975}
976
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000977
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000978DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
979 StringRef LinkageName, DIFile File,
980 unsigned LineNo, DICompositeType Ty,
981 bool isLocalToUnit, bool isDefinition,
982 unsigned ScopeLine, unsigned Flags,
983 bool isOptimized, Function *Fn,
984 MDNode *TParams, MDNode *Decl) {
985 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
986 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
987 Flags, isOptimized, Fn, TParams, Decl,
988 [&] (ArrayRef<Value *> Elts) -> MDNode *{
989 MDNode *Node = MDNode::get(VMContext, Elts);
990 // Create a named metadata so that we
991 // do not lose this mdnode.
992 if (isDefinition)
993 AllSubprograms.push_back(Node);
994 return Node;
995 });
996}
997
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000998DISubprogram
999DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
1000 StringRef LinkageName, DIFile File,
1001 unsigned LineNo, DICompositeType Ty,
1002 bool isLocalToUnit, bool isDefinition,
1003 unsigned ScopeLine, unsigned Flags,
1004 bool isOptimized, Function *Fn,
1005 MDNode *TParams, MDNode *Decl) {
1006 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
1007 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1008 Flags, isOptimized, Fn, TParams, Decl,
1009 [&] (ArrayRef<Value *> Elts) {
1010 return MDNode::getTemporary(VMContext, Elts);
1011 });
1012}
1013
Eric Christopher98f9c232013-10-15 23:31:31 +00001014DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1015 StringRef LinkageName, DIFile F,
David Blaikie5174c842013-05-22 23:22:18 +00001016 unsigned LineNo, DICompositeType Ty,
Eric Christopher98f9c232013-10-15 23:31:31 +00001017 bool isLocalToUnit, bool isDefinition,
Devang Patelb68c6232010-12-08 20:42:44 +00001018 unsigned VK, unsigned VIndex,
Eric Christopher98f9c232013-10-15 23:31:31 +00001019 DIType VTableHolder, unsigned Flags,
1020 bool isOptimized, Function *Fn,
Devang Patel9f738842011-04-05 22:52:06 +00001021 MDNode *TParam) {
David Blaikie5174c842013-05-22 23:22:18 +00001022 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1023 "function types should be subroutines");
Eric Christopher5cb56322013-10-15 23:31:36 +00001024 assert(getNonCompileUnitScope(Context) &&
1025 "Methods should have both a Context and a context that isn't "
1026 "the compile unit.");
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001027 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
1028 .concat(Name)
1029 .concat(Name)
1030 .concat(LinkageName)
1031 .concat(LineNo)
1032 .concat(isLocalToUnit)
1033 .concat(isDefinition)
1034 .concat(VK)
1035 .concat(VIndex)
1036 .concat(Flags)
1037 .concat(isOptimized)
1038 .concat(LineNo)
1039 // FIXME: Do we want to use different scope/lines?
1040 .get(VMContext),
1041 F.getFileNode(), DIScope(Context).getRef(), Ty,
1042 VTableHolder.getRef(), Fn, TParam, nullptr, nullptr};
Devang Patel0c773242011-04-18 23:51:03 +00001043 MDNode *Node = MDNode::get(VMContext, Elts);
David Blaikie595eb442013-02-18 07:10:22 +00001044 if (isDefinition)
1045 AllSubprograms.push_back(Node);
David Blaikiecc8d0902013-03-21 20:28:52 +00001046 DISubprogram S(Node);
Manman Ren74c188f2013-07-01 21:02:01 +00001047 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +00001048 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001049}
1050
Devang Patel9b412732011-02-22 18:56:12 +00001051DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patel746660f2010-12-07 23:25:47 +00001052 DIFile File, unsigned LineNo) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001053 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace)
1054 .concat(Name)
1055 .concat(LineNo)
1056 .get(VMContext),
1057 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001058 DINameSpace R(MDNode::get(VMContext, Elts));
1059 assert(R.Verify() &&
1060 "createNameSpace should return a verifiable DINameSpace");
1061 return R;
Devang Patel746660f2010-12-07 23:25:47 +00001062}
1063
Eric Christopher6647b832011-10-11 22:59:11 +00001064DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001065 DIFile File,
1066 unsigned Discriminator) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001067 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1068 .concat(Discriminator)
1069 .get(VMContext),
1070 File.getFileNode(), Scope};
David Blaikie085abe32013-03-11 23:21:19 +00001071 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1072 assert(
1073 R.Verify() &&
1074 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1075 return R;
Eric Christopher6647b832011-10-11 22:59:11 +00001076}
1077
Devang Patel9b412732011-02-22 18:56:12 +00001078DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001079 unsigned Line, unsigned Col) {
David Blaikie6c217162014-05-15 20:09:55 +00001080 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1081 // I believe the right way is to have a self-referential element in the node.
1082 // Also: why do we bother with line/column - they're not used and the
1083 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1084 // for uniquing, yet then we have this other solution (because line/col were
1085 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1086
Adrian Prantl21e8d4a2013-06-24 21:19:43 +00001087 // Defeat MDNode uniquing for lexical blocks by using unique id.
Devang Patel89ea4f22010-12-08 01:50:15 +00001088 static unsigned int unique_id = 0;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001089 Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1090 .concat(Line)
1091 .concat(Col)
1092 .concat(unique_id++)
1093 .get(VMContext),
1094 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001095 DILexicalBlock R(MDNode::get(VMContext, Elts));
1096 assert(R.Verify() &&
1097 "createLexicalBlock should return a verifiable DILexicalBlock");
1098 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +00001099}
1100
Devang Patel9b412732011-02-22 18:56:12 +00001101Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001102 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001103 Instruction *InsertBefore) {
1104 assert(Storage && "no storage passed to dbg.declare");
Manman Ren9822a112013-06-29 05:01:19 +00001105 assert(VarInfo.isVariable() &&
1106 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001107 if (!DeclareFn)
1108 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1109
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001110 Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
Jay Foad5bd375a2011-07-15 08:37:34 +00001111 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001112}
1113
Devang Patel9b412732011-02-22 18:56:12 +00001114Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001115 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001116 BasicBlock *InsertAtEnd) {
1117 assert(Storage && "no storage passed to dbg.declare");
Manman Ren9822a112013-06-29 05:01:19 +00001118 assert(VarInfo.isVariable() &&
1119 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001120 if (!DeclareFn)
1121 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1122
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001123 Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
Devang Patel746660f2010-12-07 23:25:47 +00001124
1125 // If this block already has a terminator then insert this intrinsic
1126 // before the terminator.
1127 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foad5bd375a2011-07-15 08:37:34 +00001128 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patel746660f2010-12-07 23:25:47 +00001129 else
Jay Foad5bd375a2011-07-15 08:37:34 +00001130 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001131}
1132
Devang Patel9b412732011-02-22 18:56:12 +00001133Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001134 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001135 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001136 Instruction *InsertBefore) {
1137 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001138 assert(VarInfo.isVariable() &&
1139 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001140 if (!ValueFn)
1141 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1142
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001143 Value *Args[] = {MDNode::get(V->getContext(), V),
1144 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1145 VarInfo, Expr};
Jay Foad5bd375a2011-07-15 08:37:34 +00001146 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001147}
1148
Devang Patel9b412732011-02-22 18:56:12 +00001149Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001150 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001151 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001152 BasicBlock *InsertAtEnd) {
1153 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001154 assert(VarInfo.isVariable() &&
1155 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001156 if (!ValueFn)
1157 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1158
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001159 Value *Args[] = {MDNode::get(V->getContext(), V),
1160 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1161 VarInfo, Expr};
Jay Foad5bd375a2011-07-15 08:37:34 +00001162 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001163}