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