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