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