blob: 5311d6f10f0554daddd2590986d142fc7ed224ab [file] [log] [blame]
Devang Patel35fcd652010-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
14#include "llvm/Analysis/DIBuilder.h"
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/IntrinsicInst.h"
18#include "llvm/Module.h"
Benjamin Kramer42c9b252010-11-04 18:45:27 +000019#include "llvm/ADT/STLExtras.h"
Devang Patel35fcd652010-11-04 15:01:38 +000020#include "llvm/Support/Dwarf.h"
21
22using namespace llvm;
23using namespace llvm::dwarf;
24
25static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
26 assert((Tag & LLVMDebugVersionMask) == 0 &&
27 "Tag too large for debug encoding!");
28 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
29}
Devang Patel48f17ba2010-12-07 23:58:00 +000030
Devang Patel35fcd652010-11-04 15:01:38 +000031DIBuilder::DIBuilder(Module &m)
Devang Patel94c7ddb2011-08-16 22:09:43 +000032 : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
33 TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0), ValueFn(0)
34{}
Devang Patel35fcd652010-11-04 15:01:38 +000035
Devang Patel6326a422011-08-15 23:00:00 +000036/// finalize - Construct any deferred debug info descriptors.
37void DIBuilder::finalize() {
Devang Patel94c7ddb2011-08-16 22:09:43 +000038 DIArray Enums = getOrCreateArray(AllEnumTypes);
39 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
40
41 DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
42 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
43
44 DIArray SPs = getOrCreateArray(AllSubprograms);
45 DIType(TempSubprograms).replaceAllUsesWith(SPs);
Devang Patel93d39be2011-08-19 23:28:12 +000046 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
47 DISubprogram SP(SPs.getElement(i));
48 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
49 SmallVector<Value *, 4> Variables;
50 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
51 Variables.push_back(NMD->getOperand(ii));
52 if (MDNode *Temp = SP.getVariablesNodes()) {
53 DIArray AV = getOrCreateArray(Variables);
54 DIType(Temp).replaceAllUsesWith(AV);
55 }
56 NMD->eraseFromParent();
57 }
58 }
Devang Patel94c7ddb2011-08-16 22:09:43 +000059
60 DIArray GVs = getOrCreateArray(AllGVs);
61 DIType(TempGVs).replaceAllUsesWith(GVs);
62}
63
64/// getNonCompileUnitScope - If N is compile unit return NULL otherwise return N.
65static MDNode *getNonCompileUnitScope(MDNode *N) {
66 if (DIDescriptor(N).isCompileUnit())
67 return NULL;
68 return N;
Devang Patel6326a422011-08-15 23:00:00 +000069}
70
Devang Patel50d280c2011-02-22 18:56:12 +000071/// createCompileUnit - A CompileUnit provides an anchor for all debugging
Devang Patel35fcd652010-11-04 15:01:38 +000072/// information generated during this instance of compilation.
Devang Patel50d280c2011-02-22 18:56:12 +000073void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
Devang Patel35fcd652010-11-04 15:01:38 +000074 StringRef Directory, StringRef Producer,
75 bool isOptimized, StringRef Flags,
76 unsigned RunTimeVer) {
Devang Patel94c7ddb2011-08-16 22:09:43 +000077 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
78 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
79 Value *THElts[] = { TempEnumTypes };
80 MDNode *EnumHolder = MDNode::get(VMContext, THElts);
81
82 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
83 Value *TRElts[] = { TempRetainTypes };
84 MDNode *RetainHolder = MDNode::get(VMContext, TRElts);
85
86 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
87 Value *TSElts[] = { TempSubprograms };
88 MDNode *SPHolder = MDNode::get(VMContext, TSElts);
89
90 TempGVs = MDNode::getTemporary(VMContext, TElts);
91 Value *TVElts[] = { TempGVs };
92 MDNode *GVHolder = MDNode::get(VMContext, TVElts);
93
Benjamin Kramer42c9b252010-11-04 18:45:27 +000094 Value *Elts[] = {
95 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
96 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
97 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
98 MDString::get(VMContext, Filename),
99 MDString::get(VMContext, Directory),
100 MDString::get(VMContext, Producer),
101 // Deprecate isMain field.
102 ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
103 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
104 MDString::get(VMContext, Flags),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000105 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
106 EnumHolder,
107 RetainHolder,
108 SPHolder,
109 GVHolder
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000110 };
Devang Patel1f48a952011-04-18 23:51:03 +0000111 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
Devang Patel464f4ef2011-05-03 16:18:28 +0000112
113 // Create a named metadata so that it is easier to find cu in a module.
114 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
115 NMD->addOperand(TheCU);
Devang Patel35fcd652010-11-04 15:01:38 +0000116}
117
Devang Patel50d280c2011-02-22 18:56:12 +0000118/// createFile - Create a file descriptor to hold debugging information
Devang Patel35fcd652010-11-04 15:01:38 +0000119/// for a file.
Devang Patel50d280c2011-02-22 18:56:12 +0000120DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000121 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
122 Value *Elts[] = {
123 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
124 MDString::get(VMContext, Filename),
125 MDString::get(VMContext, Directory),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000126 NULL // TheCU
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000127 };
Devang Patel1f48a952011-04-18 23:51:03 +0000128 return DIFile(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000129}
130
Devang Patel50d280c2011-02-22 18:56:12 +0000131/// createEnumerator - Create a single enumerator value.
132DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000133 Value *Elts[] = {
134 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
135 MDString::get(VMContext, Name),
136 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
137 };
Devang Patel1f48a952011-04-18 23:51:03 +0000138 return DIEnumerator(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000139}
140
Devang Patel50d280c2011-02-22 18:56:12 +0000141/// createBasicType - Create debugging information entry for a basic
Devang Patel35fcd652010-11-04 15:01:38 +0000142/// type, e.g 'char'.
Devang Patel50d280c2011-02-22 18:56:12 +0000143DIType DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Devang Patel35fcd652010-11-04 15:01:38 +0000144 uint64_t AlignInBits,
145 unsigned Encoding) {
146 // Basic types are encoded in DIBasicType format. Line number, filename,
147 // offset and flags are always empty here.
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000148 Value *Elts[] = {
149 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000150 NULL, //TheCU,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000151 MDString::get(VMContext, Name),
152 NULL, // Filename
153 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
154 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
155 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
156 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
157 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
158 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
159 };
Devang Patel1f48a952011-04-18 23:51:03 +0000160 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000161}
162
Devang Patel50d280c2011-02-22 18:56:12 +0000163/// createQaulifiedType - Create debugging information entry for a qualified
Devang Patel35fcd652010-11-04 15:01:38 +0000164/// type, e.g. 'const int'.
Devang Patel50d280c2011-02-22 18:56:12 +0000165DIType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000166 // Qualified types are encoded in DIDerivedType format.
167 Value *Elts[] = {
168 GetTagConstant(VMContext, Tag),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000169 NULL, //TheCU,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000170 MDString::get(VMContext, StringRef()), // Empty name.
171 NULL, // Filename
172 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
173 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
174 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
175 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
176 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
177 FromTy
178 };
Devang Patel1f48a952011-04-18 23:51:03 +0000179 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000180}
181
Devang Patel50d280c2011-02-22 18:56:12 +0000182/// createPointerType - Create debugging information entry for a pointer.
183DIType DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
Devang Patel35fcd652010-11-04 15:01:38 +0000184 uint64_t AlignInBits, StringRef Name) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000185 // Pointer types are encoded in DIDerivedType format.
186 Value *Elts[] = {
187 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000188 NULL, //TheCU,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000189 MDString::get(VMContext, Name),
190 NULL, // Filename
191 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
192 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
193 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
194 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
195 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
196 PointeeTy
197 };
Devang Patel1f48a952011-04-18 23:51:03 +0000198 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000199}
200
Devang Patel50d280c2011-02-22 18:56:12 +0000201/// createReferenceType - Create debugging information entry for a reference.
202DIType DIBuilder::createReferenceType(DIType RTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000203 // References are encoded in DIDerivedType format.
204 Value *Elts[] = {
205 GetTagConstant(VMContext, dwarf::DW_TAG_reference_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000206 NULL, //TheCU,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000207 NULL, // Name
208 NULL, // Filename
209 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
210 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
211 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
212 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
213 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
214 RTy
215 };
Devang Patel1f48a952011-04-18 23:51:03 +0000216 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000217}
218
Devang Patel50d280c2011-02-22 18:56:12 +0000219/// createTypedef - Create debugging information entry for a typedef.
220DIType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
Devang Patelda174c12011-06-03 17:04:51 +0000221 unsigned LineNo, DIDescriptor Context) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000222 // typedefs are encoded in DIDerivedType format.
223 assert(Ty.Verify() && "Invalid typedef type!");
224 Value *Elts[] = {
225 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000226 getNonCompileUnitScope(Context),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000227 MDString::get(VMContext, Name),
228 File,
229 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
230 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
231 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
232 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
233 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
234 Ty
235 };
Devang Patel1f48a952011-04-18 23:51:03 +0000236 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000237}
238
Devang Patel50d280c2011-02-22 18:56:12 +0000239/// createFriend - Create debugging information entry for a 'friend'.
240DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000241 // typedefs are encoded in DIDerivedType format.
242 assert(Ty.Verify() && "Invalid type!");
243 assert(FriendTy.Verify() && "Invalid friend type!");
244 Value *Elts[] = {
245 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
246 Ty,
247 NULL, // Name
248 Ty.getFile(),
249 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
250 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
251 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
252 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
253 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
254 FriendTy
255 };
Devang Patel1f48a952011-04-18 23:51:03 +0000256 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000257}
258
Devang Patel50d280c2011-02-22 18:56:12 +0000259/// createInheritance - Create debugging information entry to establish
Devang Patel35fcd652010-11-04 15:01:38 +0000260/// inheritnace relationship between two types.
Devang Patel50d280c2011-02-22 18:56:12 +0000261DIType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
Devang Patel35fcd652010-11-04 15:01:38 +0000262 uint64_t BaseOffset, unsigned Flags) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000263 // TAG_inheritance is encoded in DIDerivedType format.
264 Value *Elts[] = {
265 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
266 Ty,
267 NULL, // Name
Devang Patelb351ebf2010-12-08 21:46:37 +0000268 Ty.getFile(),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000269 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
270 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
271 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
272 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
273 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
274 BaseTy
275 };
Devang Patel1f48a952011-04-18 23:51:03 +0000276 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000277}
278
Devang Patel50d280c2011-02-22 18:56:12 +0000279/// createMemberType - Create debugging information entry for a member.
Devang Patelfa3f9c02011-06-24 22:00:39 +0000280DIType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
Devang Patelfe58f952010-12-07 23:25:47 +0000281 DIFile File, unsigned LineNumber,
Devang Patel35fcd652010-11-04 15:01:38 +0000282 uint64_t SizeInBits, uint64_t AlignInBits,
283 uint64_t OffsetInBits, unsigned Flags,
284 DIType Ty) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000285 // TAG_member is encoded in DIDerivedType format.
286 Value *Elts[] = {
287 GetTagConstant(VMContext, dwarf::DW_TAG_member),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000288 getNonCompileUnitScope(Scope),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000289 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +0000290 File,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000291 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
292 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
293 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
294 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
295 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
296 Ty
297 };
Devang Patel1f48a952011-04-18 23:51:03 +0000298 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000299}
300
Devang Patele9db5e22011-04-16 00:11:51 +0000301/// createObjCIVar - Create debugging information entry for Objective-C
302/// instance variable.
303DIType DIBuilder::createObjCIVar(StringRef Name,
304 DIFile File, unsigned LineNumber,
305 uint64_t SizeInBits, uint64_t AlignInBits,
306 uint64_t OffsetInBits, unsigned Flags,
307 DIType Ty, StringRef PropertyName,
308 StringRef GetterName, StringRef SetterName,
309 unsigned PropertyAttributes) {
310 // TAG_member is encoded in DIDerivedType format.
311 Value *Elts[] = {
312 GetTagConstant(VMContext, dwarf::DW_TAG_member),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000313 getNonCompileUnitScope(File),
Devang Patele9db5e22011-04-16 00:11:51 +0000314 MDString::get(VMContext, Name),
315 File,
316 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
317 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
318 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
319 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
320 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
321 Ty,
322 MDString::get(VMContext, PropertyName),
323 MDString::get(VMContext, GetterName),
324 MDString::get(VMContext, SetterName),
325 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
326 };
Devang Patel1f48a952011-04-18 23:51:03 +0000327 return DIType(MDNode::get(VMContext, Elts));
Devang Patele9db5e22011-04-16 00:11:51 +0000328}
329
Devang Patel50d280c2011-02-22 18:56:12 +0000330/// createClassType - Create debugging information entry for a class.
331DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
Devang Patel0a2551d2010-12-08 20:18:20 +0000332 DIFile File, unsigned LineNumber,
333 uint64_t SizeInBits, uint64_t AlignInBits,
334 uint64_t OffsetInBits, unsigned Flags,
335 DIType DerivedFrom, DIArray Elements,
Devang Patel7e2cb112011-02-02 21:38:25 +0000336 MDNode *VTableHoder, MDNode *TemplateParams) {
Devang Patel0a2551d2010-12-08 20:18:20 +0000337 // TAG_class_type is encoded in DICompositeType format.
338 Value *Elts[] = {
339 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000340 getNonCompileUnitScope(Context),
Devang Patel0a2551d2010-12-08 20:18:20 +0000341 MDString::get(VMContext, Name),
342 File,
343 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
344 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
345 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
346 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
347 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
348 DerivedFrom,
349 Elements,
350 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patel7e2cb112011-02-02 21:38:25 +0000351 VTableHoder,
352 TemplateParams
Devang Patel0a2551d2010-12-08 20:18:20 +0000353 };
Devang Patel1f48a952011-04-18 23:51:03 +0000354 return DIType(MDNode::get(VMContext, Elts));
Devang Patel0a2551d2010-12-08 20:18:20 +0000355}
356
Devang Patel50d280c2011-02-22 18:56:12 +0000357/// createTemplateTypeParameter - Create debugging information for template
Devang Patel7e2cb112011-02-02 21:38:25 +0000358/// type parameter.
359DITemplateTypeParameter
Devang Patel50d280c2011-02-22 18:56:12 +0000360DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
Devang Patel7e2cb112011-02-02 21:38:25 +0000361 DIType Ty, MDNode *File, unsigned LineNo,
362 unsigned ColumnNo) {
363 Value *Elts[] = {
364 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000365 getNonCompileUnitScope(Context),
Devang Patel7e2cb112011-02-02 21:38:25 +0000366 MDString::get(VMContext, Name),
367 Ty,
368 File,
369 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
370 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
371 };
Devang Patel1f48a952011-04-18 23:51:03 +0000372 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
Devang Patel7e2cb112011-02-02 21:38:25 +0000373}
374
Devang Patel50d280c2011-02-22 18:56:12 +0000375/// createTemplateValueParameter - Create debugging information for template
Devang Patele7d93872011-02-02 22:35:53 +0000376/// value parameter.
377DITemplateValueParameter
Devang Patel50d280c2011-02-22 18:56:12 +0000378DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
Devang Patele7d93872011-02-02 22:35:53 +0000379 DIType Ty, uint64_t Val,
380 MDNode *File, unsigned LineNo,
381 unsigned ColumnNo) {
382 Value *Elts[] = {
383 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000384 getNonCompileUnitScope(Context),
Devang Patele7d93872011-02-02 22:35:53 +0000385 MDString::get(VMContext, Name),
386 Ty,
387 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
388 File,
389 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
390 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
391 };
Devang Patel1f48a952011-04-18 23:51:03 +0000392 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
Devang Patele7d93872011-02-02 22:35:53 +0000393}
394
Devang Patel50d280c2011-02-22 18:56:12 +0000395/// createStructType - Create debugging information entry for a struct.
396DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
Devang Patel43c249c2010-12-08 01:50:15 +0000397 DIFile File, unsigned LineNumber,
398 uint64_t SizeInBits, uint64_t AlignInBits,
399 unsigned Flags, DIArray Elements,
400 unsigned RunTimeLang) {
Devang Patelfe58f952010-12-07 23:25:47 +0000401 // TAG_structure_type is encoded in DICompositeType format.
402 Value *Elts[] = {
403 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000404 getNonCompileUnitScope(Context),
Devang Patelfe58f952010-12-07 23:25:47 +0000405 MDString::get(VMContext, Name),
Devang Patel43c249c2010-12-08 01:50:15 +0000406 File,
Devang Patelfe58f952010-12-07 23:25:47 +0000407 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
408 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
409 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
Devang Patel43c249c2010-12-08 01:50:15 +0000410 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patelfe58f952010-12-07 23:25:47 +0000411 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
412 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
413 Elements,
414 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
415 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
416 };
Devang Patel1f48a952011-04-18 23:51:03 +0000417 return DIType(MDNode::get(VMContext, Elts));
Devang Patelfe58f952010-12-07 23:25:47 +0000418}
419
Devang Patel50d280c2011-02-22 18:56:12 +0000420/// createUnionType - Create debugging information entry for an union.
421DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
Devang Patel43c249c2010-12-08 01:50:15 +0000422 DIFile File,
423 unsigned LineNumber, uint64_t SizeInBits,
424 uint64_t AlignInBits, unsigned Flags,
425 DIArray Elements, unsigned RunTimeLang) {
426 // TAG_union_type is encoded in DICompositeType format.
427 Value *Elts[] = {
428 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000429 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +0000430 MDString::get(VMContext, Name),
431 File,
432 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
433 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
434 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
435 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
436 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
437 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
438 Elements,
439 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
440 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
441 };
Devang Patel1f48a952011-04-18 23:51:03 +0000442 return DIType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000443}
444
Devang Patel50d280c2011-02-22 18:56:12 +0000445/// createSubroutineType - Create subroutine type.
446DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
Devang Patel43c249c2010-12-08 01:50:15 +0000447 // TAG_subroutine_type is encoded in DICompositeType format.
448 Value *Elts[] = {
449 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
Devang Patel2bd62692011-08-18 23:50:57 +0000450 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel43c249c2010-12-08 01:50:15 +0000451 MDString::get(VMContext, ""),
Devang Patel2bd62692011-08-18 23:50:57 +0000452 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel43c249c2010-12-08 01:50:15 +0000453 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
454 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
455 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
456 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
457 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
458 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
459 ParameterTypes,
460 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
461 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
462 };
Devang Patel1f48a952011-04-18 23:51:03 +0000463 return DIType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000464}
465
Devang Patel50d280c2011-02-22 18:56:12 +0000466/// createEnumerationType - Create debugging information entry for an
Devang Patel43c249c2010-12-08 01:50:15 +0000467/// enumeration.
Devang Patel50d280c2011-02-22 18:56:12 +0000468DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name,
Devang Patel43c249c2010-12-08 01:50:15 +0000469 DIFile File, unsigned LineNumber,
470 uint64_t SizeInBits,
471 uint64_t AlignInBits, DIArray Elements) {
472 // TAG_enumeration_type is encoded in DICompositeType format.
473 Value *Elts[] = {
474 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000475 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +0000476 MDString::get(VMContext, Name),
477 File,
478 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
479 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
480 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
481 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
482 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
483 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
484 Elements,
485 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
486 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
487 };
Devang Patel1f48a952011-04-18 23:51:03 +0000488 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000489 AllEnumTypes.push_back(Node);
Devang Patel43c249c2010-12-08 01:50:15 +0000490 return DIType(Node);
491}
492
Devang Patel50d280c2011-02-22 18:56:12 +0000493/// createArrayType - Create debugging information entry for an array.
494DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
Devang Patel43c249c2010-12-08 01:50:15 +0000495 DIType Ty, DIArray Subscripts) {
496 // TAG_array_type is encoded in DICompositeType format.
497 Value *Elts[] = {
498 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000499 NULL, //TheCU,
Devang Patel43c249c2010-12-08 01:50:15 +0000500 MDString::get(VMContext, ""),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000501 NULL, //TheCU,
Devang Patel43c249c2010-12-08 01:50:15 +0000502 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
503 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
504 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
505 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
506 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
507 Ty,
508 Subscripts,
509 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
510 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
511 };
Devang Patel1f48a952011-04-18 23:51:03 +0000512 return DIType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000513}
514
Devang Patel50d280c2011-02-22 18:56:12 +0000515/// createVectorType - Create debugging information entry for a vector.
516DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
Devang Patel43c249c2010-12-08 01:50:15 +0000517 DIType Ty, DIArray Subscripts) {
518 // TAG_vector_type is encoded in DICompositeType format.
519 Value *Elts[] = {
520 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000521 NULL, //TheCU,
Devang Patel43c249c2010-12-08 01:50:15 +0000522 MDString::get(VMContext, ""),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000523 NULL, //TheCU,
Devang Patel43c249c2010-12-08 01:50:15 +0000524 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
525 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
526 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
527 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
528 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
529 Ty,
530 Subscripts,
531 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
532 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
533 };
Devang Patel1f48a952011-04-18 23:51:03 +0000534 return DIType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000535}
Devang Patelfe58f952010-12-07 23:25:47 +0000536
Devang Patel50d280c2011-02-22 18:56:12 +0000537/// createArtificialType - Create a new DIType with "artificial" flag set.
538DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel35fcd652010-11-04 15:01:38 +0000539 if (Ty.isArtificial())
540 return Ty;
541
542 SmallVector<Value *, 9> Elts;
543 MDNode *N = Ty;
544 assert (N && "Unexpected input DIType!");
545 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
546 if (Value *V = N->getOperand(i))
547 Elts.push_back(V);
548 else
549 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
550 }
551
552 unsigned CurFlags = Ty.getFlags();
553 CurFlags = CurFlags | DIType::FlagArtificial;
554
555 // Flags are stored at this slot.
556 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
557
Devang Patel1f48a952011-04-18 23:51:03 +0000558 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000559}
Devang Patelfe58f952010-12-07 23:25:47 +0000560
Devang Patel50d280c2011-02-22 18:56:12 +0000561/// retainType - Retain DIType in a module even if it is not referenced
Devang Patel43c249c2010-12-08 01:50:15 +0000562/// through debug info anchors.
Devang Patel50d280c2011-02-22 18:56:12 +0000563void DIBuilder::retainType(DIType T) {
Devang Patel94c7ddb2011-08-16 22:09:43 +0000564 AllRetainTypes.push_back(T);
Devang Patel43c249c2010-12-08 01:50:15 +0000565}
566
Devang Patel50d280c2011-02-22 18:56:12 +0000567/// createUnspecifiedParameter - Create unspeicified type descriptor
Devang Patel43c249c2010-12-08 01:50:15 +0000568/// for the subroutine type.
Devang Patel50d280c2011-02-22 18:56:12 +0000569DIDescriptor DIBuilder::createUnspecifiedParameter() {
Devang Patel43c249c2010-12-08 01:50:15 +0000570 Value *Elts[] = {
571 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
572 };
Devang Patel1f48a952011-04-18 23:51:03 +0000573 return DIDescriptor(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000574}
575
Devang Patel50d280c2011-02-22 18:56:12 +0000576/// createTemporaryType - Create a temporary forward-declared type.
577DIType DIBuilder::createTemporaryType() {
Devang Patelfe58f952010-12-07 23:25:47 +0000578 // Give the temporary MDNode a tag. It doesn't matter what tag we
579 // use here as long as DIType accepts it.
580 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
Jay Foadec9186b2011-04-21 19:59:31 +0000581 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
Devang Patelfe58f952010-12-07 23:25:47 +0000582 return DIType(Node);
583}
584
Devang Patel50d280c2011-02-22 18:56:12 +0000585/// createTemporaryType - Create a temporary forward-declared type.
586DIType DIBuilder::createTemporaryType(DIFile F) {
Devang Patelfe58f952010-12-07 23:25:47 +0000587 // Give the temporary MDNode a tag. It doesn't matter what tag we
588 // use here as long as DIType accepts it.
589 Value *Elts[] = {
590 GetTagConstant(VMContext, DW_TAG_base_type),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000591 TheCU,
Devang Patelfe58f952010-12-07 23:25:47 +0000592 NULL,
593 F
594 };
Jay Foadec9186b2011-04-21 19:59:31 +0000595 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
Devang Patelfe58f952010-12-07 23:25:47 +0000596 return DIType(Node);
597}
598
Devang Patel50d280c2011-02-22 18:56:12 +0000599/// getOrCreateArray - Get a DIArray, create one if required.
Jay Foad68550182011-04-24 10:11:03 +0000600DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
601 if (Elements.empty()) {
Devang Patelfe58f952010-12-07 23:25:47 +0000602 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
Jay Foadec9186b2011-04-21 19:59:31 +0000603 return DIArray(MDNode::get(VMContext, Null));
Devang Patelfe58f952010-12-07 23:25:47 +0000604 }
Jay Foad68550182011-04-24 10:11:03 +0000605 return DIArray(MDNode::get(VMContext, Elements));
Devang Patelfe58f952010-12-07 23:25:47 +0000606}
607
Devang Patel50d280c2011-02-22 18:56:12 +0000608/// getOrCreateSubrange - Create a descriptor for a value range. This
Devang Patel43c249c2010-12-08 01:50:15 +0000609/// implicitly uniques the values returned.
Devang Patel50d280c2011-02-22 18:56:12 +0000610DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patel43c249c2010-12-08 01:50:15 +0000611 Value *Elts[] = {
612 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
613 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
614 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
615 };
616
Devang Patel1f48a952011-04-18 23:51:03 +0000617 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000618}
619
Devang Patel50d280c2011-02-22 18:56:12 +0000620/// createGlobalVariable - Create a new descriptor for the specified global.
Devang Patelfe58f952010-12-07 23:25:47 +0000621DIGlobalVariable DIBuilder::
Devang Patel50d280c2011-02-22 18:56:12 +0000622createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
Devang Patelfe58f952010-12-07 23:25:47 +0000623 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
624 Value *Elts[] = {
625 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
626 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000627 NULL, // TheCU,
Devang Patelfe58f952010-12-07 23:25:47 +0000628 MDString::get(VMContext, Name),
629 MDString::get(VMContext, Name),
Devang Patel94541ef2010-12-08 00:06:22 +0000630 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +0000631 F,
632 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
633 Ty,
634 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
635 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
636 Val
637 };
Devang Patel1f48a952011-04-18 23:51:03 +0000638 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000639 AllGVs.push_back(Node);
Devang Patelfe58f952010-12-07 23:25:47 +0000640 return DIGlobalVariable(Node);
641}
642
Devang Patel50d280c2011-02-22 18:56:12 +0000643/// createStaticVariable - Create a new descriptor for the specified static
Devang Patelfe58f952010-12-07 23:25:47 +0000644/// variable.
645DIGlobalVariable DIBuilder::
Devang Patel50d280c2011-02-22 18:56:12 +0000646createStaticVariable(DIDescriptor Context, StringRef Name,
Devang Patelfe58f952010-12-07 23:25:47 +0000647 StringRef LinkageName, DIFile F, unsigned LineNumber,
648 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
649 Value *Elts[] = {
650 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
651 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000652 getNonCompileUnitScope(Context),
Devang Patelfe58f952010-12-07 23:25:47 +0000653 MDString::get(VMContext, Name),
654 MDString::get(VMContext, Name),
655 MDString::get(VMContext, LinkageName),
656 F,
657 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
658 Ty,
659 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
660 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
661 Val
662 };
Devang Patel1f48a952011-04-18 23:51:03 +0000663 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000664 AllGVs.push_back(Node);
Devang Patelfe58f952010-12-07 23:25:47 +0000665 return DIGlobalVariable(Node);
666}
667
Devang Patel50d280c2011-02-22 18:56:12 +0000668/// createVariable - Create a new descriptor for the specified variable.
669DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel48f17ba2010-12-07 23:58:00 +0000670 StringRef Name, DIFile File,
671 unsigned LineNo, DIType Ty,
Devang Patele9e16c52011-03-01 22:58:13 +0000672 bool AlwaysPreserve, unsigned Flags,
673 unsigned ArgNo) {
Devang Patel48f17ba2010-12-07 23:58:00 +0000674 Value *Elts[] = {
675 GetTagConstant(VMContext, Tag),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000676 getNonCompileUnitScope(Scope),
Devang Patel48f17ba2010-12-07 23:58:00 +0000677 MDString::get(VMContext, Name),
678 File,
Devang Patele9e16c52011-03-01 22:58:13 +0000679 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
Devang Patel48f17ba2010-12-07 23:58:00 +0000680 Ty,
Devang Patel23336b42011-07-19 19:41:54 +0000681 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
682 Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel48f17ba2010-12-07 23:58:00 +0000683 };
Devang Patel1f48a952011-04-18 23:51:03 +0000684 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel48f17ba2010-12-07 23:58:00 +0000685 if (AlwaysPreserve) {
686 // The optimizer may remove local variable. If there is an interest
687 // to preserve variable info in such situation then stash it in a
688 // named mdnode.
689 DISubprogram Fn(getDISubprogram(Scope));
Devang Patel93d39be2011-08-19 23:28:12 +0000690 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
Devang Patel48f17ba2010-12-07 23:58:00 +0000691 FnLocals->addOperand(Node);
692 }
693 return DIVariable(Node);
694}
695
Devang Patel50d280c2011-02-22 18:56:12 +0000696/// createComplexVariable - Create a new descriptor for the specified variable
Devang Patelfe58f952010-12-07 23:25:47 +0000697/// which has a complex address expression for its address.
Devang Patel50d280c2011-02-22 18:56:12 +0000698DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
Devang Patelfe58f952010-12-07 23:25:47 +0000699 StringRef Name, DIFile F,
700 unsigned LineNo,
Jay Foad68550182011-04-24 10:11:03 +0000701 DIType Ty, ArrayRef<Value *> Addr,
702 unsigned ArgNo) {
Devang Patelfe58f952010-12-07 23:25:47 +0000703 SmallVector<Value *, 15> Elts;
704 Elts.push_back(GetTagConstant(VMContext, Tag));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000705 Elts.push_back(getNonCompileUnitScope(Scope)),
Devang Patelfe58f952010-12-07 23:25:47 +0000706 Elts.push_back(MDString::get(VMContext, Name));
707 Elts.push_back(F);
Devang Patele9e16c52011-03-01 22:58:13 +0000708 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))));
Devang Patelfe58f952010-12-07 23:25:47 +0000709 Elts.push_back(Ty);
Devang Patel7b5bd372011-04-26 18:24:39 +0000710 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
Devang Patel23336b42011-07-19 19:41:54 +0000711 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
Jay Foad68550182011-04-24 10:11:03 +0000712 Elts.append(Addr.begin(), Addr.end());
Devang Patelfe58f952010-12-07 23:25:47 +0000713
Devang Patel1f48a952011-04-18 23:51:03 +0000714 return DIVariable(MDNode::get(VMContext, Elts));
Devang Patelfe58f952010-12-07 23:25:47 +0000715}
716
Devang Patel50d280c2011-02-22 18:56:12 +0000717/// createFunction - Create a new descriptor for the specified function.
718DISubprogram DIBuilder::createFunction(DIDescriptor Context,
Devang Patel44498a62010-12-08 20:42:44 +0000719 StringRef Name,
720 StringRef LinkageName,
721 DIFile File, unsigned LineNo,
722 DIType Ty,
723 bool isLocalToUnit, bool isDefinition,
724 unsigned Flags, bool isOptimized,
Devang Patelda194752011-04-05 22:52:06 +0000725 Function *Fn,
Devang Patel5e06bb82011-04-22 23:10:17 +0000726 MDNode *TParams,
727 MDNode *Decl) {
Devang Patel93d39be2011-08-19 23:28:12 +0000728 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
729 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
730 Value *TVElts[] = { Temp };
731 MDNode *THolder = MDNode::get(VMContext, TVElts);
732
Devang Patel44498a62010-12-08 20:42:44 +0000733 Value *Elts[] = {
734 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
735 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000736 getNonCompileUnitScope(Context),
Devang Patel44498a62010-12-08 20:42:44 +0000737 MDString::get(VMContext, Name),
738 MDString::get(VMContext, Name),
739 MDString::get(VMContext, LinkageName),
740 File,
741 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
742 Ty,
743 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
744 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
745 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
746 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
747 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
748 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
749 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patelda194752011-04-05 22:52:06 +0000750 Fn,
Devang Patel5e06bb82011-04-22 23:10:17 +0000751 TParams,
Devang Patel93d39be2011-08-19 23:28:12 +0000752 Decl,
753 THolder
Devang Patel44498a62010-12-08 20:42:44 +0000754 };
Devang Patel1f48a952011-04-18 23:51:03 +0000755 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel44498a62010-12-08 20:42:44 +0000756
757 // Create a named metadata so that we do not lose this mdnode.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000758 AllSubprograms.push_back(Node);
Devang Patel44498a62010-12-08 20:42:44 +0000759 return DISubprogram(Node);
760}
761
Devang Patel50d280c2011-02-22 18:56:12 +0000762/// createMethod - Create a new descriptor for the specified C++ method.
763DISubprogram DIBuilder::createMethod(DIDescriptor Context,
Devang Patel44498a62010-12-08 20:42:44 +0000764 StringRef Name,
765 StringRef LinkageName,
766 DIFile F,
767 unsigned LineNo, DIType Ty,
768 bool isLocalToUnit,
769 bool isDefinition,
770 unsigned VK, unsigned VIndex,
771 MDNode *VTableHolder,
772 unsigned Flags,
773 bool isOptimized,
Devang Patelda194752011-04-05 22:52:06 +0000774 Function *Fn,
775 MDNode *TParam) {
Devang Patel93d39be2011-08-19 23:28:12 +0000776 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
777 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
778 Value *TVElts[] = { Temp };
779 MDNode *THolder = MDNode::get(VMContext, TVElts);
780
Devang Patel44498a62010-12-08 20:42:44 +0000781 Value *Elts[] = {
782 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
783 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000784 getNonCompileUnitScope(Context),
Devang Patel44498a62010-12-08 20:42:44 +0000785 MDString::get(VMContext, Name),
786 MDString::get(VMContext, Name),
787 MDString::get(VMContext, LinkageName),
788 F,
789 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
790 Ty,
791 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
792 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
793 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
794 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
795 VTableHolder,
796 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
797 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patelda194752011-04-05 22:52:06 +0000798 Fn,
799 TParam,
Devang Patel93d39be2011-08-19 23:28:12 +0000800 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
801 THolder
Devang Patel44498a62010-12-08 20:42:44 +0000802 };
Devang Patel1f48a952011-04-18 23:51:03 +0000803 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel44498a62010-12-08 20:42:44 +0000804 return DISubprogram(Node);
805}
806
Devang Patel50d280c2011-02-22 18:56:12 +0000807/// createNameSpace - This creates new descriptor for a namespace
Devang Patelfe58f952010-12-07 23:25:47 +0000808/// with the specified parent scope.
Devang Patel50d280c2011-02-22 18:56:12 +0000809DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patelfe58f952010-12-07 23:25:47 +0000810 DIFile File, unsigned LineNo) {
811 Value *Elts[] = {
812 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000813 getNonCompileUnitScope(Scope),
Devang Patelfe58f952010-12-07 23:25:47 +0000814 MDString::get(VMContext, Name),
815 File,
816 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
817 };
Devang Patel1f48a952011-04-18 23:51:03 +0000818 return DINameSpace(MDNode::get(VMContext, Elts));
Devang Patelfe58f952010-12-07 23:25:47 +0000819}
820
Devang Patel50d280c2011-02-22 18:56:12 +0000821DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
Devang Patel43c249c2010-12-08 01:50:15 +0000822 unsigned Line, unsigned Col) {
823 // Defeat MDNode uniqing for lexical blocks by using unique id.
824 static unsigned int unique_id = 0;
825 Value *Elts[] = {
826 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000827 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +0000828 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
829 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
830 File,
831 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
832 };
Devang Patel1f48a952011-04-18 23:51:03 +0000833 return DILexicalBlock(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000834}
835
Devang Patel50d280c2011-02-22 18:56:12 +0000836/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
837Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Devang Patelfe58f952010-12-07 23:25:47 +0000838 Instruction *InsertBefore) {
839 assert(Storage && "no storage passed to dbg.declare");
840 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
841 if (!DeclareFn)
842 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
843
Jay Foadec9186b2011-04-21 19:59:31 +0000844 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +0000845 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patelfe58f952010-12-07 23:25:47 +0000846}
847
Devang Patel50d280c2011-02-22 18:56:12 +0000848/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
849Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Devang Patelfe58f952010-12-07 23:25:47 +0000850 BasicBlock *InsertAtEnd) {
851 assert(Storage && "no storage passed to dbg.declare");
852 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
853 if (!DeclareFn)
854 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
855
Jay Foadec9186b2011-04-21 19:59:31 +0000856 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
Devang Patelfe58f952010-12-07 23:25:47 +0000857
858 // If this block already has a terminator then insert this intrinsic
859 // before the terminator.
860 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foada3efbb12011-07-15 08:37:34 +0000861 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patelfe58f952010-12-07 23:25:47 +0000862 else
Jay Foada3efbb12011-07-15 08:37:34 +0000863 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patelfe58f952010-12-07 23:25:47 +0000864}
865
Devang Patel50d280c2011-02-22 18:56:12 +0000866/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
867Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patelfe58f952010-12-07 23:25:47 +0000868 DIVariable VarInfo,
869 Instruction *InsertBefore) {
870 assert(V && "no value passed to dbg.value");
871 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
872 if (!ValueFn)
873 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
874
Jay Foadec9186b2011-04-21 19:59:31 +0000875 Value *Args[] = { MDNode::get(V->getContext(), V),
Devang Patelfe58f952010-12-07 23:25:47 +0000876 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
877 VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +0000878 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patelfe58f952010-12-07 23:25:47 +0000879}
880
Devang Patel50d280c2011-02-22 18:56:12 +0000881/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
882Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patelfe58f952010-12-07 23:25:47 +0000883 DIVariable VarInfo,
884 BasicBlock *InsertAtEnd) {
885 assert(V && "no value passed to dbg.value");
886 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
887 if (!ValueFn)
888 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
889
Jay Foadec9186b2011-04-21 19:59:31 +0000890 Value *Args[] = { MDNode::get(V->getContext(), V),
Devang Patelfe58f952010-12-07 23:25:47 +0000891 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
892 VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +0000893 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patelfe58f952010-12-07 23:25:47 +0000894}
895