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