1 //===--- 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"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Dwarf.h"
22
23 using namespace llvm;
24 using namespace llvm::dwarf;
25
GetTagConstant(LLVMContext & VMContext,unsigned Tag)26 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
27 assert((Tag & LLVMDebugVersionMask) == 0 &&
28 "Tag too large for debug encoding!");
29 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
30 }
31
DIBuilder(Module & m)32 DIBuilder::DIBuilder(Module &m)
33 : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
34 TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
35 ValueFn(0)
36 {}
37
38 /// finalize - Construct any deferred debug info descriptors.
finalize()39 void DIBuilder::finalize() {
40 DIArray Enums = getOrCreateArray(AllEnumTypes);
41 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
42
43 DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
44 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
45
46 DIArray SPs = getOrCreateArray(AllSubprograms);
47 DIType(TempSubprograms).replaceAllUsesWith(SPs);
48 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
49 DISubprogram SP(SPs.getElement(i));
50 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
51 SmallVector<Value *, 4> Variables;
52 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
53 Variables.push_back(NMD->getOperand(ii));
54 if (MDNode *Temp = SP.getVariablesNodes()) {
55 DIArray AV = getOrCreateArray(Variables);
56 DIType(Temp).replaceAllUsesWith(AV);
57 }
58 NMD->eraseFromParent();
59 }
60 }
61
62 DIArray GVs = getOrCreateArray(AllGVs);
63 DIType(TempGVs).replaceAllUsesWith(GVs);
64 }
65
66 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
67 /// N.
getNonCompileUnitScope(MDNode * N)68 static MDNode *getNonCompileUnitScope(MDNode *N) {
69 if (DIDescriptor(N).isCompileUnit())
70 return NULL;
71 return N;
72 }
73
74 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
75 /// information generated during this instance of compilation.
createCompileUnit(unsigned Lang,StringRef Filename,StringRef Directory,StringRef Producer,bool isOptimized,StringRef Flags,unsigned RunTimeVer)76 void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
77 StringRef Directory, StringRef Producer,
78 bool isOptimized, StringRef Flags,
79 unsigned RunTimeVer) {
80 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
81 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
82 "Invalid Language tag");
83 assert(!Filename.empty() &&
84 "Unable to create compile unit without filename");
85 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
86 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
87 Value *THElts[] = { TempEnumTypes };
88 MDNode *EnumHolder = MDNode::get(VMContext, THElts);
89
90 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
91 Value *TRElts[] = { TempRetainTypes };
92 MDNode *RetainHolder = MDNode::get(VMContext, TRElts);
93
94 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
95 Value *TSElts[] = { TempSubprograms };
96 MDNode *SPHolder = MDNode::get(VMContext, TSElts);
97
98 TempGVs = MDNode::getTemporary(VMContext, TElts);
99 Value *TVElts[] = { TempGVs };
100 MDNode *GVHolder = MDNode::get(VMContext, TVElts);
101
102 Value *Elts[] = {
103 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
104 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
105 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
106 MDString::get(VMContext, Filename),
107 MDString::get(VMContext, Directory),
108 MDString::get(VMContext, Producer),
109 // Deprecate isMain field.
110 ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
111 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
112 MDString::get(VMContext, Flags),
113 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
114 EnumHolder,
115 RetainHolder,
116 SPHolder,
117 GVHolder
118 };
119 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
120
121 // Create a named metadata so that it is easier to find cu in a module.
122 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
123 NMD->addOperand(TheCU);
124 }
125
126 /// createFile - Create a file descriptor to hold debugging information
127 /// for a file.
createFile(StringRef Filename,StringRef Directory)128 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
129 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
130 assert(!Filename.empty() && "Unable to create file without name");
131 Value *Elts[] = {
132 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
133 MDString::get(VMContext, Filename),
134 MDString::get(VMContext, Directory),
135 NULL // TheCU
136 };
137 return DIFile(MDNode::get(VMContext, Elts));
138 }
139
140 /// createEnumerator - Create a single enumerator value.
createEnumerator(StringRef Name,uint64_t Val)141 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
142 assert(!Name.empty() && "Unable to create enumerator without name");
143 Value *Elts[] = {
144 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
145 MDString::get(VMContext, Name),
146 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
147 };
148 return DIEnumerator(MDNode::get(VMContext, Elts));
149 }
150
151 /// createNullPtrType - Create C++0x nullptr type.
createNullPtrType(StringRef Name)152 DIType DIBuilder::createNullPtrType(StringRef Name) {
153 assert(!Name.empty() && "Unable to create type without name");
154 // nullptr is encoded in DIBasicType format. Line number, filename,
155 // ,size, alignment, offset and flags are always empty here.
156 Value *Elts[] = {
157 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
158 NULL, //TheCU,
159 MDString::get(VMContext, Name),
160 NULL, // Filename
161 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
162 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
163 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
164 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
165 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
166 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Encoding
167 };
168 return DIType(MDNode::get(VMContext, Elts));
169 }
170
171 /// createBasicType - Create debugging information entry for a basic
172 /// type, e.g 'char'.
createBasicType(StringRef Name,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Encoding)173 DIType DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
174 uint64_t AlignInBits,
175 unsigned Encoding) {
176 assert(!Name.empty() && "Unable to create type without name");
177 // Basic types are encoded in DIBasicType format. Line number, filename,
178 // offset and flags are always empty here.
179 Value *Elts[] = {
180 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
181 NULL, //TheCU,
182 MDString::get(VMContext, Name),
183 NULL, // Filename
184 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
185 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
186 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
187 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
188 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
189 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
190 };
191 return DIType(MDNode::get(VMContext, Elts));
192 }
193
194 /// createQualifiedType - Create debugging information entry for a qualified
195 /// type, e.g. 'const int'.
createQualifiedType(unsigned Tag,DIType FromTy)196 DIType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
197 // Qualified types are encoded in DIDerivedType format.
198 Value *Elts[] = {
199 GetTagConstant(VMContext, Tag),
200 NULL, //TheCU,
201 MDString::get(VMContext, StringRef()), // Empty name.
202 NULL, // Filename
203 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
204 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
205 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
206 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
207 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
208 FromTy
209 };
210 return DIType(MDNode::get(VMContext, Elts));
211 }
212
213 /// createPointerType - Create debugging information entry for a pointer.
createPointerType(DIType PointeeTy,uint64_t SizeInBits,uint64_t AlignInBits,StringRef Name)214 DIType DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
215 uint64_t AlignInBits, StringRef Name) {
216 // Pointer types are encoded in DIDerivedType format.
217 Value *Elts[] = {
218 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
219 NULL, //TheCU,
220 MDString::get(VMContext, Name),
221 NULL, // Filename
222 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
223 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
224 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
225 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
226 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
227 PointeeTy
228 };
229 return DIType(MDNode::get(VMContext, Elts));
230 }
231
232 /// createReferenceType - Create debugging information entry for a reference.
createReferenceType(DIType RTy)233 DIType DIBuilder::createReferenceType(DIType RTy) {
234 assert(RTy.Verify() && "Unable to create reference type");
235 // References are encoded in DIDerivedType format.
236 Value *Elts[] = {
237 GetTagConstant(VMContext, dwarf::DW_TAG_reference_type),
238 NULL, // TheCU,
239 NULL, // Name
240 NULL, // Filename
241 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
242 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
243 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
244 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
245 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
246 RTy
247 };
248 return DIType(MDNode::get(VMContext, Elts));
249 }
250
251 /// createTypedef - Create debugging information entry for a typedef.
createTypedef(DIType Ty,StringRef Name,DIFile File,unsigned LineNo,DIDescriptor Context)252 DIType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
253 unsigned LineNo, DIDescriptor Context) {
254 // typedefs are encoded in DIDerivedType format.
255 assert(Ty.Verify() && "Invalid typedef type!");
256 Value *Elts[] = {
257 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
258 getNonCompileUnitScope(Context),
259 MDString::get(VMContext, Name),
260 File,
261 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
262 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
263 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
264 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
265 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
266 Ty
267 };
268 return DIType(MDNode::get(VMContext, Elts));
269 }
270
271 /// createFriend - Create debugging information entry for a 'friend'.
createFriend(DIType Ty,DIType FriendTy)272 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
273 // typedefs are encoded in DIDerivedType format.
274 assert(Ty.Verify() && "Invalid type!");
275 assert(FriendTy.Verify() && "Invalid friend type!");
276 Value *Elts[] = {
277 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
278 Ty,
279 NULL, // Name
280 Ty.getFile(),
281 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
282 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
283 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
284 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
285 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
286 FriendTy
287 };
288 return DIType(MDNode::get(VMContext, Elts));
289 }
290
291 /// createInheritance - Create debugging information entry to establish
292 /// inheritance relationship between two types.
createInheritance(DIType Ty,DIType BaseTy,uint64_t BaseOffset,unsigned Flags)293 DIType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
294 uint64_t BaseOffset, unsigned Flags) {
295 assert(Ty.Verify() && "Unable to create inheritance");
296 // TAG_inheritance is encoded in DIDerivedType format.
297 Value *Elts[] = {
298 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
299 Ty,
300 NULL, // Name
301 Ty.getFile(),
302 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
303 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
304 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
305 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
306 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
307 BaseTy
308 };
309 return DIType(MDNode::get(VMContext, Elts));
310 }
311
312 /// createMemberType - Create debugging information entry for a member.
createMemberType(DIDescriptor Scope,StringRef Name,DIFile File,unsigned LineNumber,uint64_t SizeInBits,uint64_t AlignInBits,uint64_t OffsetInBits,unsigned Flags,DIType Ty)313 DIType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
314 DIFile File, unsigned LineNumber,
315 uint64_t SizeInBits, uint64_t AlignInBits,
316 uint64_t OffsetInBits, unsigned Flags,
317 DIType Ty) {
318 // TAG_member is encoded in DIDerivedType format.
319 Value *Elts[] = {
320 GetTagConstant(VMContext, dwarf::DW_TAG_member),
321 getNonCompileUnitScope(Scope),
322 MDString::get(VMContext, Name),
323 File,
324 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
325 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
326 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
327 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
328 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
329 Ty
330 };
331 return DIType(MDNode::get(VMContext, Elts));
332 }
333
334 /// createObjCIVar - Create debugging information entry for Objective-C
335 /// instance variable.
createObjCIVar(StringRef Name,DIFile File,unsigned LineNumber,uint64_t SizeInBits,uint64_t AlignInBits,uint64_t OffsetInBits,unsigned Flags,DIType Ty,StringRef PropertyName,StringRef GetterName,StringRef SetterName,unsigned PropertyAttributes)336 DIType DIBuilder::createObjCIVar(StringRef Name,
337 DIFile File, unsigned LineNumber,
338 uint64_t SizeInBits, uint64_t AlignInBits,
339 uint64_t OffsetInBits, unsigned Flags,
340 DIType Ty, StringRef PropertyName,
341 StringRef GetterName, StringRef SetterName,
342 unsigned PropertyAttributes) {
343 // TAG_member is encoded in DIDerivedType format.
344 Value *Elts[] = {
345 GetTagConstant(VMContext, dwarf::DW_TAG_member),
346 getNonCompileUnitScope(File),
347 MDString::get(VMContext, Name),
348 File,
349 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
350 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
351 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
352 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
353 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
354 Ty,
355 MDString::get(VMContext, PropertyName),
356 MDString::get(VMContext, GetterName),
357 MDString::get(VMContext, SetterName),
358 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
359 };
360 return DIType(MDNode::get(VMContext, Elts));
361 }
362
363 /// createObjCIVar - Create debugging information entry for Objective-C
364 /// instance variable.
createObjCIVar(StringRef Name,DIFile File,unsigned LineNumber,uint64_t SizeInBits,uint64_t AlignInBits,uint64_t OffsetInBits,unsigned Flags,DIType Ty,MDNode * PropertyNode)365 DIType DIBuilder::createObjCIVar(StringRef Name,
366 DIFile File, unsigned LineNumber,
367 uint64_t SizeInBits, uint64_t AlignInBits,
368 uint64_t OffsetInBits, unsigned Flags,
369 DIType Ty, MDNode *PropertyNode) {
370 // TAG_member is encoded in DIDerivedType format.
371 Value *Elts[] = {
372 GetTagConstant(VMContext, dwarf::DW_TAG_member),
373 getNonCompileUnitScope(File),
374 MDString::get(VMContext, Name),
375 File,
376 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
377 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
378 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
379 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
380 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
381 Ty,
382 PropertyNode
383 };
384 return DIType(MDNode::get(VMContext, Elts));
385 }
386
387 /// createObjCProperty - Create debugging information entry for Objective-C
388 /// property.
createObjCProperty(StringRef Name,DIFile File,unsigned LineNumber,StringRef GetterName,StringRef SetterName,unsigned PropertyAttributes,DIType Ty)389 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
390 DIFile File, unsigned LineNumber,
391 StringRef GetterName,
392 StringRef SetterName,
393 unsigned PropertyAttributes,
394 DIType Ty) {
395 Value *Elts[] = {
396 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
397 MDString::get(VMContext, Name),
398 File,
399 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
400 MDString::get(VMContext, GetterName),
401 MDString::get(VMContext, SetterName),
402 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
403 Ty
404 };
405 return DIObjCProperty(MDNode::get(VMContext, Elts));
406 }
407
408 /// createClassType - Create debugging information entry for a class.
createClassType(DIDescriptor Context,StringRef Name,DIFile File,unsigned LineNumber,uint64_t SizeInBits,uint64_t AlignInBits,uint64_t OffsetInBits,unsigned Flags,DIType DerivedFrom,DIArray Elements,MDNode * VTableHolder,MDNode * TemplateParams)409 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
410 DIFile File, unsigned LineNumber,
411 uint64_t SizeInBits, uint64_t AlignInBits,
412 uint64_t OffsetInBits, unsigned Flags,
413 DIType DerivedFrom, DIArray Elements,
414 MDNode *VTableHolder, MDNode *TemplateParams) {
415 // TAG_class_type is encoded in DICompositeType format.
416 Value *Elts[] = {
417 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
418 getNonCompileUnitScope(Context),
419 MDString::get(VMContext, Name),
420 File,
421 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
422 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
423 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
424 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
425 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
426 DerivedFrom,
427 Elements,
428 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
429 VTableHolder,
430 TemplateParams
431 };
432 return DIType(MDNode::get(VMContext, Elts));
433 }
434
435 /// createTemplateTypeParameter - Create debugging information for template
436 /// type parameter.
437 DITemplateTypeParameter
createTemplateTypeParameter(DIDescriptor Context,StringRef Name,DIType Ty,MDNode * File,unsigned LineNo,unsigned ColumnNo)438 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
439 DIType Ty, MDNode *File, unsigned LineNo,
440 unsigned ColumnNo) {
441 Value *Elts[] = {
442 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
443 getNonCompileUnitScope(Context),
444 MDString::get(VMContext, Name),
445 Ty,
446 File,
447 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
448 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
449 };
450 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
451 }
452
453 /// createTemplateValueParameter - Create debugging information for template
454 /// value parameter.
455 DITemplateValueParameter
createTemplateValueParameter(DIDescriptor Context,StringRef Name,DIType Ty,uint64_t Val,MDNode * File,unsigned LineNo,unsigned ColumnNo)456 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
457 DIType Ty, uint64_t Val,
458 MDNode *File, unsigned LineNo,
459 unsigned ColumnNo) {
460 Value *Elts[] = {
461 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
462 getNonCompileUnitScope(Context),
463 MDString::get(VMContext, Name),
464 Ty,
465 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
466 File,
467 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
468 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
469 };
470 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
471 }
472
473 /// createStructType - Create debugging information entry for a struct.
createStructType(DIDescriptor Context,StringRef Name,DIFile File,unsigned LineNumber,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Flags,DIArray Elements,unsigned RunTimeLang)474 DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
475 DIFile File, unsigned LineNumber,
476 uint64_t SizeInBits, uint64_t AlignInBits,
477 unsigned Flags, DIArray Elements,
478 unsigned RunTimeLang) {
479 // TAG_structure_type is encoded in DICompositeType format.
480 Value *Elts[] = {
481 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
482 getNonCompileUnitScope(Context),
483 MDString::get(VMContext, Name),
484 File,
485 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
486 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
487 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
488 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
489 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
490 NULL,
491 Elements,
492 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
493 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
494 };
495 return DIType(MDNode::get(VMContext, Elts));
496 }
497
498 /// createUnionType - Create debugging information entry for an union.
createUnionType(DIDescriptor Scope,StringRef Name,DIFile File,unsigned LineNumber,uint64_t SizeInBits,uint64_t AlignInBits,unsigned Flags,DIArray Elements,unsigned RunTimeLang)499 DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
500 DIFile File,
501 unsigned LineNumber, uint64_t SizeInBits,
502 uint64_t AlignInBits, unsigned Flags,
503 DIArray Elements, unsigned RunTimeLang) {
504 // TAG_union_type is encoded in DICompositeType format.
505 Value *Elts[] = {
506 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
507 getNonCompileUnitScope(Scope),
508 MDString::get(VMContext, Name),
509 File,
510 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
511 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
512 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
513 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
514 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
515 NULL,
516 Elements,
517 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
518 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
519 };
520 return DIType(MDNode::get(VMContext, Elts));
521 }
522
523 /// createSubroutineType - Create subroutine type.
createSubroutineType(DIFile File,DIArray ParameterTypes)524 DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
525 // TAG_subroutine_type is encoded in DICompositeType format.
526 Value *Elts[] = {
527 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
528 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
529 MDString::get(VMContext, ""),
530 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
531 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
532 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
533 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
534 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
535 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
536 NULL,
537 ParameterTypes,
538 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
539 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
540 };
541 return DIType(MDNode::get(VMContext, Elts));
542 }
543
544 /// createEnumerationType - Create debugging information entry for an
545 /// enumeration.
createEnumerationType(DIDescriptor Scope,StringRef Name,DIFile File,unsigned LineNumber,uint64_t SizeInBits,uint64_t AlignInBits,DIArray Elements)546 DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name,
547 DIFile File, unsigned LineNumber,
548 uint64_t SizeInBits,
549 uint64_t AlignInBits,
550 DIArray Elements) {
551 // TAG_enumeration_type is encoded in DICompositeType format.
552 Value *Elts[] = {
553 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
554 getNonCompileUnitScope(Scope),
555 MDString::get(VMContext, Name),
556 File,
557 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
558 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
559 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
560 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
561 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
562 NULL,
563 Elements,
564 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
565 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
566 };
567 MDNode *Node = MDNode::get(VMContext, Elts);
568 AllEnumTypes.push_back(Node);
569 return DIType(Node);
570 }
571
572 /// createArrayType - Create debugging information entry for an array.
createArrayType(uint64_t Size,uint64_t AlignInBits,DIType Ty,DIArray Subscripts)573 DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
574 DIType Ty, DIArray Subscripts) {
575 // TAG_array_type is encoded in DICompositeType format.
576 Value *Elts[] = {
577 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
578 NULL, //TheCU,
579 MDString::get(VMContext, ""),
580 NULL, //TheCU,
581 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
582 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
583 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
584 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
585 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
586 Ty,
587 Subscripts,
588 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
589 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
590 };
591 return DIType(MDNode::get(VMContext, Elts));
592 }
593
594 /// createVectorType - Create debugging information entry for a vector.
createVectorType(uint64_t Size,uint64_t AlignInBits,DIType Ty,DIArray Subscripts)595 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
596 DIType Ty, DIArray Subscripts) {
597 // TAG_vector_type is encoded in DICompositeType format.
598 Value *Elts[] = {
599 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
600 NULL, //TheCU,
601 MDString::get(VMContext, ""),
602 NULL, //TheCU,
603 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
604 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
605 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
606 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
607 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
608 Ty,
609 Subscripts,
610 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
611 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
612 };
613 return DIType(MDNode::get(VMContext, Elts));
614 }
615
616 /// createArtificialType - Create a new DIType with "artificial" flag set.
createArtificialType(DIType Ty)617 DIType DIBuilder::createArtificialType(DIType Ty) {
618 if (Ty.isArtificial())
619 return Ty;
620
621 SmallVector<Value *, 9> Elts;
622 MDNode *N = Ty;
623 assert (N && "Unexpected input DIType!");
624 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
625 if (Value *V = N->getOperand(i))
626 Elts.push_back(V);
627 else
628 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
629 }
630
631 unsigned CurFlags = Ty.getFlags();
632 CurFlags = CurFlags | DIType::FlagArtificial;
633
634 // Flags are stored at this slot.
635 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
636
637 return DIType(MDNode::get(VMContext, Elts));
638 }
639
640 /// retainType - Retain DIType in a module even if it is not referenced
641 /// through debug info anchors.
retainType(DIType T)642 void DIBuilder::retainType(DIType T) {
643 AllRetainTypes.push_back(T);
644 }
645
646 /// createUnspecifiedParameter - Create unspeicified type descriptor
647 /// for the subroutine type.
createUnspecifiedParameter()648 DIDescriptor DIBuilder::createUnspecifiedParameter() {
649 Value *Elts[] = {
650 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
651 };
652 return DIDescriptor(MDNode::get(VMContext, Elts));
653 }
654
655 /// createTemporaryType - Create a temporary forward-declared type.
createTemporaryType()656 DIType DIBuilder::createTemporaryType() {
657 // Give the temporary MDNode a tag. It doesn't matter what tag we
658 // use here as long as DIType accepts it.
659 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
660 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
661 return DIType(Node);
662 }
663
664 /// createTemporaryType - Create a temporary forward-declared type.
createTemporaryType(DIFile F)665 DIType DIBuilder::createTemporaryType(DIFile F) {
666 // Give the temporary MDNode a tag. It doesn't matter what tag we
667 // use here as long as DIType accepts it.
668 Value *Elts[] = {
669 GetTagConstant(VMContext, DW_TAG_base_type),
670 TheCU,
671 NULL,
672 F
673 };
674 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
675 return DIType(Node);
676 }
677
678 /// createForwardDecl - Create a temporary forward-declared type that
679 /// can be RAUW'd if the full type is seen.
createForwardDecl(unsigned Tag,StringRef Name,DIFile F,unsigned Line,unsigned RuntimeLang)680 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIFile F,
681 unsigned Line, unsigned RuntimeLang) {
682 // Create a temporary MDNode.
683 Value *Elts[] = {
684 GetTagConstant(VMContext, Tag),
685 NULL, // TheCU
686 MDString::get(VMContext, Name),
687 F,
688 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
689 // To ease transition include sizes etc of 0.
690 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
691 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
692 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
693 ConstantInt::get(Type::getInt32Ty(VMContext),
694 DIDescriptor::FlagFwdDecl),
695 NULL,
696 DIArray(),
697 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
698 };
699 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
700 return DIType(Node);
701 }
702
703 /// getOrCreateArray - Get a DIArray, create one if required.
getOrCreateArray(ArrayRef<Value * > Elements)704 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
705 if (Elements.empty()) {
706 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
707 return DIArray(MDNode::get(VMContext, Null));
708 }
709 return DIArray(MDNode::get(VMContext, Elements));
710 }
711
712 /// getOrCreateSubrange - Create a descriptor for a value range. This
713 /// implicitly uniques the values returned.
getOrCreateSubrange(int64_t Lo,int64_t Hi)714 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Hi) {
715 Value *Elts[] = {
716 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
717 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
718 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
719 };
720
721 return DISubrange(MDNode::get(VMContext, Elts));
722 }
723
724 /// createGlobalVariable - Create a new descriptor for the specified global.
725 DIGlobalVariable DIBuilder::
createGlobalVariable(StringRef Name,DIFile F,unsigned LineNumber,DIType Ty,bool isLocalToUnit,llvm::Value * Val)726 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
727 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
728 Value *Elts[] = {
729 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
730 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
731 NULL, // TheCU,
732 MDString::get(VMContext, Name),
733 MDString::get(VMContext, Name),
734 MDString::get(VMContext, Name),
735 F,
736 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
737 Ty,
738 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
739 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
740 Val
741 };
742 MDNode *Node = MDNode::get(VMContext, Elts);
743 AllGVs.push_back(Node);
744 return DIGlobalVariable(Node);
745 }
746
747 /// createStaticVariable - Create a new descriptor for the specified static
748 /// variable.
749 DIGlobalVariable DIBuilder::
createStaticVariable(DIDescriptor Context,StringRef Name,StringRef LinkageName,DIFile F,unsigned LineNumber,DIType Ty,bool isLocalToUnit,llvm::Value * Val)750 createStaticVariable(DIDescriptor Context, StringRef Name,
751 StringRef LinkageName, DIFile F, unsigned LineNumber,
752 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
753 Value *Elts[] = {
754 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
755 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
756 getNonCompileUnitScope(Context),
757 MDString::get(VMContext, Name),
758 MDString::get(VMContext, Name),
759 MDString::get(VMContext, LinkageName),
760 F,
761 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
762 Ty,
763 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
764 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
765 Val
766 };
767 MDNode *Node = MDNode::get(VMContext, Elts);
768 AllGVs.push_back(Node);
769 return DIGlobalVariable(Node);
770 }
771
772 /// createVariable - Create a new descriptor for the specified variable.
createLocalVariable(unsigned Tag,DIDescriptor Scope,StringRef Name,DIFile File,unsigned LineNo,DIType Ty,bool AlwaysPreserve,unsigned Flags,unsigned ArgNo)773 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
774 StringRef Name, DIFile File,
775 unsigned LineNo, DIType Ty,
776 bool AlwaysPreserve, unsigned Flags,
777 unsigned ArgNo) {
778 Value *Elts[] = {
779 GetTagConstant(VMContext, Tag),
780 getNonCompileUnitScope(Scope),
781 MDString::get(VMContext, Name),
782 File,
783 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
784 Ty,
785 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
786 Constant::getNullValue(Type::getInt32Ty(VMContext)),
787 };
788 MDNode *Node = MDNode::get(VMContext, Elts);
789 if (AlwaysPreserve) {
790 // The optimizer may remove local variable. If there is an interest
791 // to preserve variable info in such situation then stash it in a
792 // named mdnode.
793 DISubprogram Fn(getDISubprogram(Scope));
794 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
795 FnLocals->addOperand(Node);
796 }
797 return DIVariable(Node);
798 }
799
800 /// createComplexVariable - Create a new descriptor for the specified variable
801 /// which has a complex address expression for its address.
createComplexVariable(unsigned Tag,DIDescriptor Scope,StringRef Name,DIFile F,unsigned LineNo,DIType Ty,ArrayRef<Value * > Addr,unsigned ArgNo)802 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
803 StringRef Name, DIFile F,
804 unsigned LineNo,
805 DIType Ty, ArrayRef<Value *> Addr,
806 unsigned ArgNo) {
807 SmallVector<Value *, 15> Elts;
808 Elts.push_back(GetTagConstant(VMContext, Tag));
809 Elts.push_back(getNonCompileUnitScope(Scope)),
810 Elts.push_back(MDString::get(VMContext, Name));
811 Elts.push_back(F);
812 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
813 (LineNo | (ArgNo << 24))));
814 Elts.push_back(Ty);
815 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
816 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
817 Elts.append(Addr.begin(), Addr.end());
818
819 return DIVariable(MDNode::get(VMContext, Elts));
820 }
821
822 /// createFunction - Create a new descriptor for the specified function.
createFunction(DIDescriptor Context,StringRef Name,StringRef LinkageName,DIFile File,unsigned LineNo,DIType Ty,bool isLocalToUnit,bool isDefinition,unsigned ScopeLine,unsigned Flags,bool isOptimized,Function * Fn,MDNode * TParams,MDNode * Decl)823 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
824 StringRef Name,
825 StringRef LinkageName,
826 DIFile File, unsigned LineNo,
827 DIType Ty,
828 bool isLocalToUnit, bool isDefinition,
829 unsigned ScopeLine,
830 unsigned Flags, bool isOptimized,
831 Function *Fn,
832 MDNode *TParams,
833 MDNode *Decl) {
834 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
835 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
836 Value *TVElts[] = { Temp };
837 MDNode *THolder = MDNode::get(VMContext, TVElts);
838
839 Value *Elts[] = {
840 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
841 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
842 getNonCompileUnitScope(Context),
843 MDString::get(VMContext, Name),
844 MDString::get(VMContext, Name),
845 MDString::get(VMContext, LinkageName),
846 File,
847 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
848 Ty,
849 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
850 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
851 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
852 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
853 NULL,
854 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
855 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
856 Fn,
857 TParams,
858 Decl,
859 THolder,
860 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
861 };
862 MDNode *Node = MDNode::get(VMContext, Elts);
863
864 // Create a named metadata so that we do not lose this mdnode.
865 AllSubprograms.push_back(Node);
866 return DISubprogram(Node);
867 }
868
869 /// createMethod - Create a new descriptor for the specified C++ method.
createMethod(DIDescriptor Context,StringRef Name,StringRef LinkageName,DIFile F,unsigned LineNo,DIType Ty,bool isLocalToUnit,bool isDefinition,unsigned VK,unsigned VIndex,MDNode * VTableHolder,unsigned Flags,bool isOptimized,Function * Fn,MDNode * TParam)870 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
871 StringRef Name,
872 StringRef LinkageName,
873 DIFile F,
874 unsigned LineNo, DIType Ty,
875 bool isLocalToUnit,
876 bool isDefinition,
877 unsigned VK, unsigned VIndex,
878 MDNode *VTableHolder,
879 unsigned Flags,
880 bool isOptimized,
881 Function *Fn,
882 MDNode *TParam) {
883 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
884 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
885 Value *TVElts[] = { Temp };
886 MDNode *THolder = MDNode::get(VMContext, TVElts);
887
888 Value *Elts[] = {
889 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
890 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
891 getNonCompileUnitScope(Context),
892 MDString::get(VMContext, Name),
893 MDString::get(VMContext, Name),
894 MDString::get(VMContext, LinkageName),
895 F,
896 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
897 Ty,
898 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
899 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
900 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
901 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
902 VTableHolder,
903 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
904 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
905 Fn,
906 TParam,
907 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
908 THolder,
909 // FIXME: Do we want to use a different scope lines?
910 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
911 };
912 MDNode *Node = MDNode::get(VMContext, Elts);
913 return DISubprogram(Node);
914 }
915
916 /// createNameSpace - This creates new descriptor for a namespace
917 /// with the specified parent scope.
createNameSpace(DIDescriptor Scope,StringRef Name,DIFile File,unsigned LineNo)918 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
919 DIFile File, unsigned LineNo) {
920 Value *Elts[] = {
921 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
922 getNonCompileUnitScope(Scope),
923 MDString::get(VMContext, Name),
924 File,
925 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
926 };
927 return DINameSpace(MDNode::get(VMContext, Elts));
928 }
929
930 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
931 /// an existing scope with a new filename.
createLexicalBlockFile(DIDescriptor Scope,DIFile File)932 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
933 DIFile File) {
934 Value *Elts[] = {
935 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
936 Scope,
937 File
938 };
939 return DILexicalBlockFile(MDNode::get(VMContext, Elts));
940 }
941
createLexicalBlock(DIDescriptor Scope,DIFile File,unsigned Line,unsigned Col)942 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
943 unsigned Line, unsigned Col) {
944 // Defeat MDNode uniqing for lexical blocks by using unique id.
945 static unsigned int unique_id = 0;
946 Value *Elts[] = {
947 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
948 getNonCompileUnitScope(Scope),
949 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
950 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
951 File,
952 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
953 };
954 return DILexicalBlock(MDNode::get(VMContext, Elts));
955 }
956
957 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
insertDeclare(Value * Storage,DIVariable VarInfo,Instruction * InsertBefore)958 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
959 Instruction *InsertBefore) {
960 assert(Storage && "no storage passed to dbg.declare");
961 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
962 if (!DeclareFn)
963 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
964
965 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
966 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
967 }
968
969 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
insertDeclare(Value * Storage,DIVariable VarInfo,BasicBlock * InsertAtEnd)970 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
971 BasicBlock *InsertAtEnd) {
972 assert(Storage && "no storage passed to dbg.declare");
973 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
974 if (!DeclareFn)
975 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
976
977 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
978
979 // If this block already has a terminator then insert this intrinsic
980 // before the terminator.
981 if (TerminatorInst *T = InsertAtEnd->getTerminator())
982 return CallInst::Create(DeclareFn, Args, "", T);
983 else
984 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
985 }
986
987 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
insertDbgValueIntrinsic(Value * V,uint64_t Offset,DIVariable VarInfo,Instruction * InsertBefore)988 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
989 DIVariable VarInfo,
990 Instruction *InsertBefore) {
991 assert(V && "no value passed to dbg.value");
992 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
993 if (!ValueFn)
994 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
995
996 Value *Args[] = { MDNode::get(V->getContext(), V),
997 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
998 VarInfo };
999 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1000 }
1001
1002 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
insertDbgValueIntrinsic(Value * V,uint64_t Offset,DIVariable VarInfo,BasicBlock * InsertAtEnd)1003 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1004 DIVariable VarInfo,
1005 BasicBlock *InsertAtEnd) {
1006 assert(V && "no value passed to dbg.value");
1007 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1008 if (!ValueFn)
1009 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1010
1011 Value *Args[] = { MDNode::get(V->getContext(), V),
1012 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1013 VarInfo };
1014 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1015 }
1016