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