0622b90b458b437507feb886a29949310a61bda2
[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, //TheCU,
150     MDString::get(VMContext, Name),
151     NULL, // Filename
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, //TheCU,
173     MDString::get(VMContext, Name),
174     NULL, // Filename
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, //TheCU,
192     MDString::get(VMContext, StringRef()), // Empty name.
193     NULL, // Filename
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, //TheCU,
212     MDString::get(VMContext, Name),
213     NULL, // Filename
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, //TheCU,
229     NULL,
230     NULL, // Filename
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, // TheCU,
250     NULL, // Name
251     NULL, // Filename
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     getNonCompileUnitScope(Context),
270     MDString::get(VMContext, Name),
271     File,
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     Ty,
290     NULL, // Name
291     Ty.getFile(),
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     Ty,
311     NULL, // Name
312     Ty.getFile(),
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     getNonCompileUnitScope(Scope),
332     MDString::get(VMContext, Name),
333     File,
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     getNonCompileUnitScope(Scope),
355     MDString::get(VMContext, Name),
356     File,
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     getNonCompileUnitScope(File),
381     MDString::get(VMContext, Name),
382     File,
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     getNonCompileUnitScope(File),
408     MDString::get(VMContext, Name),
409     File,
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     getNonCompileUnitScope(Context),
494     MDString::get(VMContext, Name),
495     File,
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     getNonCompileUnitScope(Context),
526     MDString::get(VMContext, Name),
527     File,
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     getNonCompileUnitScope(Scope),
553     MDString::get(VMContext, Name),
554     File,
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     MDString::get(VMContext, ""),
576     Constant::getNullValue(Type::getInt32Ty(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     getNonCompileUnitScope(Scope),
600     MDString::get(VMContext, Name),
601     File,
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, //TheCU,
624     MDString::get(VMContext, ""),
625     NULL, //TheCU,
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, //TheCU,
647     MDString::get(VMContext, ""),
648     NULL, //TheCU,
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 /// createTemporaryType - Create a temporary forward-declared type.
727 DIType DIBuilder::createTemporaryType() {
728   // Give the temporary MDNode a tag. It doesn't matter what tag we
729   // use here as long as DIType accepts it.
730   Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
731   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
732   return DIType(Node);
733 }
734
735 /// createTemporaryType - Create a temporary forward-declared type.
736 DIType DIBuilder::createTemporaryType(DIFile F) {
737   // Give the temporary MDNode a tag. It doesn't matter what tag we
738   // use here as long as DIType accepts it.
739   Value *Elts[] = {
740     GetTagConstant(VMContext, DW_TAG_base_type),
741     TheCU,
742     NULL,
743     F
744   };
745   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
746   return DIType(Node);
747 }
748
749 /// createForwardDecl - Create a temporary forward-declared type that
750 /// can be RAUW'd if the full type is seen.
751 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
752                                     DIDescriptor Scope, DIFile F,
753                                     unsigned Line, unsigned RuntimeLang,
754                                     uint64_t SizeInBits,
755                                     uint64_t AlignInBits) {
756   // Create a temporary MDNode.
757   Value *Elts[] = {
758     GetTagConstant(VMContext, Tag),
759     getNonCompileUnitScope(Scope),
760     MDString::get(VMContext, Name),
761     F,
762     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
763     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
764     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
765     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
766     ConstantInt::get(Type::getInt32Ty(VMContext),
767                      DIDescriptor::FlagFwdDecl),
768     NULL,
769     DIArray(),
770     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
771   };
772   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
773   assert(DIType(Node).Verify() &&
774          "createForwardDecl result should be verifiable");
775   return DIType(Node);
776 }
777
778 /// getOrCreateArray - Get a DIArray, create one if required.
779 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
780   if (Elements.empty()) {
781     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
782     return DIArray(MDNode::get(VMContext, Null));
783   }
784   return DIArray(MDNode::get(VMContext, Elements));
785 }
786
787 /// getOrCreateSubrange - Create a descriptor for a value range.  This
788 /// implicitly uniques the values returned.
789 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
790   Value *Elts[] = {
791     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
792     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
793     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
794   };
795
796   return DISubrange(MDNode::get(VMContext, Elts));
797 }
798
799 /// createGlobalVariable - Create a new descriptor for the specified global.
800 DIGlobalVariable DIBuilder::
801 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
802                      DIType Ty, bool isLocalToUnit, Value *Val) {
803   Value *Elts[] = {
804     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
805     Constant::getNullValue(Type::getInt32Ty(VMContext)),
806     NULL, // TheCU,
807     MDString::get(VMContext, Name),
808     MDString::get(VMContext, Name),
809     MDString::get(VMContext, Name),
810     F,
811     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
812     Ty,
813     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
814     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
815     Val,
816     DIDescriptor()
817   };
818   MDNode *Node = MDNode::get(VMContext, Elts);
819   AllGVs.push_back(Node);
820   return DIGlobalVariable(Node);
821 }
822
823 /// createStaticVariable - Create a new descriptor for the specified static
824 /// variable.
825 DIGlobalVariable DIBuilder::
826 createStaticVariable(DIDescriptor Context, StringRef Name,
827                      StringRef LinkageName, DIFile F, unsigned LineNumber,
828                      DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
829   Value *Elts[] = {
830     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
831     Constant::getNullValue(Type::getInt32Ty(VMContext)),
832     getNonCompileUnitScope(Context),
833     MDString::get(VMContext, Name),
834     MDString::get(VMContext, Name),
835     MDString::get(VMContext, LinkageName),
836     F,
837     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
838     Ty,
839     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
840     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
841     Val,
842     DIDescriptor(Decl)
843   };
844   MDNode *Node = MDNode::get(VMContext, Elts);
845   AllGVs.push_back(Node);
846   return DIGlobalVariable(Node);
847 }
848
849 /// createVariable - Create a new descriptor for the specified variable.
850 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
851                                           StringRef Name, DIFile File,
852                                           unsigned LineNo, DIType Ty,
853                                           bool AlwaysPreserve, unsigned Flags,
854                                           unsigned ArgNo) {
855   DIDescriptor Context(getNonCompileUnitScope(Scope));
856   assert((!Context || Context.Verify()) &&
857          "createLocalVariable should be called with a valid Context");
858   assert(Ty.Verify() &&
859          "createLocalVariable should be called with a valid type");
860   Value *Elts[] = {
861     GetTagConstant(VMContext, Tag),
862     getNonCompileUnitScope(Scope),
863     MDString::get(VMContext, Name),
864     File,
865     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
866     Ty,
867     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
868     Constant::getNullValue(Type::getInt32Ty(VMContext))
869   };
870   MDNode *Node = MDNode::get(VMContext, Elts);
871   if (AlwaysPreserve) {
872     // The optimizer may remove local variable. If there is an interest
873     // to preserve variable info in such situation then stash it in a
874     // named mdnode.
875     DISubprogram Fn(getDISubprogram(Scope));
876     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
877     FnLocals->addOperand(Node);
878   }
879   assert(DIVariable(Node).Verify() &&
880          "createLocalVariable should return a verifiable DIVariable");
881   return DIVariable(Node);
882 }
883
884 /// createComplexVariable - Create a new descriptor for the specified variable
885 /// which has a complex address expression for its address.
886 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
887                                             StringRef Name, DIFile F,
888                                             unsigned LineNo,
889                                             DIType Ty, ArrayRef<Value *> Addr,
890                                             unsigned ArgNo) {
891   SmallVector<Value *, 15> Elts;
892   Elts.push_back(GetTagConstant(VMContext, Tag));
893   Elts.push_back(getNonCompileUnitScope(Scope)),
894   Elts.push_back(MDString::get(VMContext, Name));
895   Elts.push_back(F);
896   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
897                                   (LineNo | (ArgNo << 24))));
898   Elts.push_back(Ty);
899   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
900   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
901   Elts.append(Addr.begin(), Addr.end());
902
903   return DIVariable(MDNode::get(VMContext, Elts));
904 }
905
906 /// createFunction - Create a new descriptor for the specified function.
907 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
908                                        StringRef Name,
909                                        StringRef LinkageName,
910                                        DIFile File, unsigned LineNo,
911                                        DIType Ty,
912                                        bool isLocalToUnit, bool isDefinition,
913                                        unsigned ScopeLine,
914                                        unsigned Flags, bool isOptimized,
915                                        Function *Fn,
916                                        MDNode *TParams,
917                                        MDNode *Decl) {
918   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
919   Value *Elts[] = {
920     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
921     Constant::getNullValue(Type::getInt32Ty(VMContext)),
922     getNonCompileUnitScope(Context),
923     MDString::get(VMContext, Name),
924     MDString::get(VMContext, Name),
925     MDString::get(VMContext, LinkageName),
926     File,
927     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
928     Ty,
929     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
930     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
931     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
932     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
933     NULL,
934     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
935     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
936     Fn,
937     TParams,
938     Decl,
939     MDNode::getTemporary(VMContext, TElts),
940     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
941   };
942   MDNode *Node = MDNode::get(VMContext, Elts);
943
944   // Create a named metadata so that we do not lose this mdnode.
945   if (isDefinition)
946     AllSubprograms.push_back(Node);
947   return DISubprogram(Node);
948 }
949
950 /// createMethod - Create a new descriptor for the specified C++ method.
951 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
952                                      StringRef Name,
953                                      StringRef LinkageName,
954                                      DIFile F,
955                                      unsigned LineNo, DIType Ty,
956                                      bool isLocalToUnit,
957                                      bool isDefinition,
958                                      unsigned VK, unsigned VIndex,
959                                      MDNode *VTableHolder,
960                                      unsigned Flags,
961                                      bool isOptimized,
962                                      Function *Fn,
963                                      MDNode *TParam) {
964   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
965   Value *Elts[] = {
966     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
967     Constant::getNullValue(Type::getInt32Ty(VMContext)),
968     getNonCompileUnitScope(Context),
969     MDString::get(VMContext, Name),
970     MDString::get(VMContext, Name),
971     MDString::get(VMContext, LinkageName),
972     F,
973     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
974     Ty,
975     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
976     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
977     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
978     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
979     VTableHolder,
980     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
981     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
982     Fn,
983     TParam,
984     Constant::getNullValue(Type::getInt32Ty(VMContext)),
985     MDNode::getTemporary(VMContext, TElts),
986     // FIXME: Do we want to use different scope/lines?
987     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
988   };
989   MDNode *Node = MDNode::get(VMContext, Elts);
990   if (isDefinition)
991     AllSubprograms.push_back(Node);
992   return DISubprogram(Node);
993 }
994
995 /// createNameSpace - This creates new descriptor for a namespace
996 /// with the specified parent scope.
997 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
998                                        DIFile File, unsigned LineNo) {
999   Value *Elts[] = {
1000     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1001     getNonCompileUnitScope(Scope),
1002     MDString::get(VMContext, Name),
1003     File,
1004     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1005   };
1006   DINameSpace R(MDNode::get(VMContext, Elts));
1007   assert(R.Verify() &&
1008          "createNameSpace should return a verifiable DINameSpace");
1009   return R;
1010 }
1011
1012 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1013 /// an existing scope with a new filename.
1014 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1015                                                      DIFile File) {
1016   Value *Elts[] = {
1017     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1018     Scope,
1019     File
1020   };
1021   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1022   assert(
1023       R.Verify() &&
1024       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1025   return R;
1026 }
1027
1028 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1029                                              unsigned Line, unsigned Col) {
1030   // Defeat MDNode uniqing for lexical blocks by using unique id.
1031   static unsigned int unique_id = 0;
1032   Value *Elts[] = {
1033     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1034     getNonCompileUnitScope(Scope),
1035     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1036     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1037     File,
1038     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1039   };
1040   DILexicalBlock R(MDNode::get(VMContext, Elts));
1041   assert(R.Verify() &&
1042          "createLexicalBlock should return a verifiable DILexicalBlock");
1043   return R;
1044 }
1045
1046 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1047 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1048                                       Instruction *InsertBefore) {
1049   assert(Storage && "no storage passed to dbg.declare");
1050   assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1051   if (!DeclareFn)
1052     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1053
1054   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1055   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1056 }
1057
1058 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1059 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1060                                       BasicBlock *InsertAtEnd) {
1061   assert(Storage && "no storage passed to dbg.declare");
1062   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1063   if (!DeclareFn)
1064     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1065
1066   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1067
1068   // If this block already has a terminator then insert this intrinsic
1069   // before the terminator.
1070   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1071     return CallInst::Create(DeclareFn, Args, "", T);
1072   else
1073     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1074 }
1075
1076 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1077 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1078                                                 DIVariable VarInfo,
1079                                                 Instruction *InsertBefore) {
1080   assert(V && "no value passed to dbg.value");
1081   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1082   if (!ValueFn)
1083     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1084
1085   Value *Args[] = { MDNode::get(V->getContext(), V),
1086                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1087                     VarInfo };
1088   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1089 }
1090
1091 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1092 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1093                                                 DIVariable VarInfo,
1094                                                 BasicBlock *InsertAtEnd) {
1095   assert(V && "no value passed to dbg.value");
1096   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1097   if (!ValueFn)
1098     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1099
1100   Value *Args[] = { MDNode::get(V->getContext(), V),
1101                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1102                     VarInfo };
1103   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1104 }