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