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