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