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