Fix comment.
[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 /// createObjCIVar - Create debugging information entry for Objective-C
354 /// instance variable.
355 DIType DIBuilder::createObjCIVar(StringRef Name,
356                                  DIFile File, unsigned LineNumber,
357                                  uint64_t SizeInBits, uint64_t AlignInBits,
358                                  uint64_t OffsetInBits, unsigned Flags,
359                                  DIType Ty, StringRef PropertyName,
360                                  StringRef GetterName, StringRef SetterName,
361                                  unsigned PropertyAttributes) {
362   // TAG_member is encoded in DIDerivedType format.
363   Value *Elts[] = {
364     GetTagConstant(VMContext, dwarf::DW_TAG_member),
365     getNonCompileUnitScope(File),
366     MDString::get(VMContext, Name),
367     File,
368     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
369     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
370     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
371     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
372     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
373     Ty,
374     MDString::get(VMContext, PropertyName),
375     MDString::get(VMContext, GetterName),
376     MDString::get(VMContext, SetterName),
377     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
378   };
379   return DIType(MDNode::get(VMContext, Elts));
380 }
381
382 /// createObjCIVar - Create debugging information entry for Objective-C
383 /// instance variable.
384 DIType DIBuilder::createObjCIVar(StringRef Name,
385                                  DIFile File, unsigned LineNumber,
386                                  uint64_t SizeInBits, uint64_t AlignInBits,
387                                  uint64_t OffsetInBits, unsigned Flags,
388                                  DIType Ty, MDNode *PropertyNode) {
389   // TAG_member is encoded in DIDerivedType format.
390   Value *Elts[] = {
391     GetTagConstant(VMContext, dwarf::DW_TAG_member),
392     getNonCompileUnitScope(File),
393     MDString::get(VMContext, Name),
394     File,
395     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
396     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
397     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
398     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
399     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
400     Ty,
401     PropertyNode
402   };
403   return DIType(MDNode::get(VMContext, Elts));
404 }
405
406 /// createObjCProperty - Create debugging information entry for Objective-C
407 /// property.
408 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
409                                              DIFile File, unsigned LineNumber,
410                                              StringRef GetterName,
411                                              StringRef SetterName, 
412                                              unsigned PropertyAttributes,
413                                              DIType Ty) {
414   Value *Elts[] = {
415     GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
416     MDString::get(VMContext, Name),
417     File,
418     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
419     MDString::get(VMContext, GetterName),
420     MDString::get(VMContext, SetterName),
421     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
422     Ty
423   };
424   return DIObjCProperty(MDNode::get(VMContext, Elts));
425 }
426
427 /// createTemplateTypeParameter - Create debugging information for template
428 /// type parameter.
429 DITemplateTypeParameter
430 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
431                                        DIType Ty, MDNode *File, unsigned LineNo,
432                                        unsigned ColumnNo) {
433   Value *Elts[] = {
434     GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
435     getNonCompileUnitScope(Context),
436     MDString::get(VMContext, Name),
437     Ty,
438     File,
439     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
440     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
441   };
442   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
443 }
444
445 /// createTemplateValueParameter - Create debugging information for template
446 /// value parameter.
447 DITemplateValueParameter
448 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
449                                         DIType Ty, uint64_t Val,
450                                         MDNode *File, unsigned LineNo,
451                                         unsigned ColumnNo) {
452   Value *Elts[] = {
453     GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
454     getNonCompileUnitScope(Context),
455     MDString::get(VMContext, Name),
456     Ty,
457     ConstantInt::get(Type::getInt64Ty(VMContext), Val),
458     File,
459     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
460     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
461   };
462   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
463 }
464
465 /// createClassType - Create debugging information entry for a class.
466 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
467                                   DIFile File, unsigned LineNumber,
468                                   uint64_t SizeInBits, uint64_t AlignInBits,
469                                   uint64_t OffsetInBits, unsigned Flags,
470                                   DIType DerivedFrom, DIArray Elements,
471                                   MDNode *VTableHolder,
472                                   MDNode *TemplateParams) {
473  // TAG_class_type is encoded in DICompositeType format.
474   Value *Elts[] = {
475     GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
476     getNonCompileUnitScope(Context),
477     MDString::get(VMContext, Name),
478     File,
479     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
480     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
481     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
482     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
483     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
484     DerivedFrom,
485     Elements,
486     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
487     VTableHolder,
488     TemplateParams
489   };
490   return DIType(MDNode::get(VMContext, Elts));
491 }
492
493 /// createStructType - Create debugging information entry for a struct.
494 DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
495                                    DIFile File, unsigned LineNumber,
496                                    uint64_t SizeInBits, uint64_t AlignInBits,
497                                    unsigned Flags, DIArray Elements,
498                                    unsigned RunTimeLang) {
499  // TAG_structure_type is encoded in DICompositeType format.
500   Value *Elts[] = {
501     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
502     getNonCompileUnitScope(Context),
503     MDString::get(VMContext, Name),
504     File,
505     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
506     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
507     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
508     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
509     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
510     NULL,
511     Elements,
512     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
513     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
514     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
515   };
516   return DIType(MDNode::get(VMContext, Elts));
517 }
518
519 /// createUnionType - Create debugging information entry for an union.
520 DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
521                                   DIFile File,
522                                   unsigned LineNumber, uint64_t SizeInBits,
523                                   uint64_t AlignInBits, unsigned Flags,
524                                   DIArray Elements, unsigned RunTimeLang) {
525   // TAG_union_type is encoded in DICompositeType format.
526   Value *Elts[] = {
527     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
528     getNonCompileUnitScope(Scope),
529     MDString::get(VMContext, Name),
530     File,
531     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
532     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
533     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
534     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
535     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
536     NULL,
537     Elements,
538     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
539     Constant::getNullValue(Type::getInt32Ty(VMContext))
540   };
541   return DIType(MDNode::get(VMContext, Elts));
542 }
543
544 /// createSubroutineType - Create subroutine type.
545 DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
546   // TAG_subroutine_type is encoded in DICompositeType format.
547   Value *Elts[] = {
548     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
549     Constant::getNullValue(Type::getInt32Ty(VMContext)),
550     MDString::get(VMContext, ""),
551     Constant::getNullValue(Type::getInt32Ty(VMContext)),
552     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
553     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
554     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
555     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
556     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
557     NULL,
558     ParameterTypes,
559     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
560     Constant::getNullValue(Type::getInt32Ty(VMContext))
561   };
562   return DIType(MDNode::get(VMContext, Elts));
563 }
564
565 /// createEnumerationType - Create debugging information entry for an
566 /// enumeration.
567 DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name,
568                                         DIFile File, unsigned LineNumber,
569                                         uint64_t SizeInBits,
570                                         uint64_t AlignInBits,
571                                         DIArray Elements,
572                                         DIType ClassType) {
573   // TAG_enumeration_type is encoded in DICompositeType format.
574   Value *Elts[] = {
575     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
576     getNonCompileUnitScope(Scope),
577     MDString::get(VMContext, Name),
578     File,
579     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
580     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
581     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
582     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
583     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
584     ClassType,
585     Elements,
586     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
587     Constant::getNullValue(Type::getInt32Ty(VMContext))
588   };
589   MDNode *Node = MDNode::get(VMContext, Elts);
590   AllEnumTypes.push_back(Node);
591   return DIType(Node);
592 }
593
594 /// createArrayType - Create debugging information entry for an array.
595 DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
596                                   DIType Ty, DIArray Subscripts) {
597   // TAG_array_type is encoded in DICompositeType format.
598   Value *Elts[] = {
599     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
600     NULL, //TheCU,
601     MDString::get(VMContext, ""),
602     NULL, //TheCU,
603     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
604     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
605     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
606     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
607     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
608     Ty,
609     Subscripts,
610     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
611     Constant::getNullValue(Type::getInt32Ty(VMContext))
612   };
613   return DIType(MDNode::get(VMContext, Elts));
614 }
615
616 /// createVectorType - Create debugging information entry for a vector.
617 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
618                                    DIType Ty, DIArray Subscripts) {
619   // TAG_vector_type is encoded in DICompositeType format.
620   Value *Elts[] = {
621     GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
622     NULL, //TheCU,
623     MDString::get(VMContext, ""),
624     NULL, //TheCU,
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 DIType(MDNode::get(VMContext, Elts));
636 }
637
638 /// createArtificialType - Create a new DIType with "artificial" flag set.
639 DIType DIBuilder::createArtificialType(DIType Ty) {
640   if (Ty.isArtificial())
641     return Ty;
642
643   SmallVector<Value *, 9> Elts;
644   MDNode *N = Ty;
645   assert (N && "Unexpected input DIType!");
646   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
647     if (Value *V = N->getOperand(i))
648       Elts.push_back(V);
649     else
650       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
651   }
652
653   unsigned CurFlags = Ty.getFlags();
654   CurFlags = CurFlags | DIType::FlagArtificial;
655
656   // Flags are stored at this slot.
657   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
658
659   return DIType(MDNode::get(VMContext, Elts));
660 }
661
662 /// createObjectPointerType - Create a new type with both the object pointer
663 /// and artificial flags set.
664 DIType DIBuilder::createObjectPointerType(DIType Ty) {
665   if (Ty.isObjectPointer())
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::FlagObjectPointer | 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 /// retainType - Retain DIType in a module even if it is not referenced
688 /// through debug info anchors.
689 void DIBuilder::retainType(DIType T) {
690   AllRetainTypes.push_back(T);
691 }
692
693 /// createUnspecifiedParameter - Create unspeicified type descriptor
694 /// for the subroutine type.
695 DIDescriptor DIBuilder::createUnspecifiedParameter() {
696   Value *Elts[] = {
697     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
698   };
699   return DIDescriptor(MDNode::get(VMContext, Elts));
700 }
701
702 /// createTemporaryType - Create a temporary forward-declared type.
703 DIType DIBuilder::createTemporaryType() {
704   // Give the temporary MDNode a tag. It doesn't matter what tag we
705   // use here as long as DIType accepts it.
706   Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
707   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
708   return DIType(Node);
709 }
710
711 /// createTemporaryType - Create a temporary forward-declared type.
712 DIType DIBuilder::createTemporaryType(DIFile F) {
713   // Give the temporary MDNode a tag. It doesn't matter what tag we
714   // use here as long as DIType accepts it.
715   Value *Elts[] = {
716     GetTagConstant(VMContext, DW_TAG_base_type),
717     TheCU,
718     NULL,
719     F
720   };
721   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
722   return DIType(Node);
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     getNonCompileUnitScope(Scope),
736     MDString::get(VMContext, Name),
737     F,
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   return DIType(Node);
750 }
751
752 /// getOrCreateArray - Get a DIArray, create one if required.
753 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
754   if (Elements.empty()) {
755     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
756     return DIArray(MDNode::get(VMContext, Null));
757   }
758   return DIArray(MDNode::get(VMContext, Elements));
759 }
760
761 /// getOrCreateSubrange - Create a descriptor for a value range.  This
762 /// implicitly uniques the values returned.
763 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
764   Value *Elts[] = {
765     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
766     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
767     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
768   };
769
770   return DISubrange(MDNode::get(VMContext, Elts));
771 }
772
773 /// createGlobalVariable - Create a new descriptor for the specified global.
774 DIGlobalVariable DIBuilder::
775 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
776                      DIType Ty, bool isLocalToUnit, Value *Val) {
777   Value *Elts[] = {
778     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
779     Constant::getNullValue(Type::getInt32Ty(VMContext)),
780     NULL, // TheCU,
781     MDString::get(VMContext, Name),
782     MDString::get(VMContext, Name),
783     MDString::get(VMContext, Name),
784     F,
785     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
786     Ty,
787     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
788     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
789     Val
790   };
791   MDNode *Node = MDNode::get(VMContext, Elts);
792   AllGVs.push_back(Node);
793   return DIGlobalVariable(Node);
794 }
795
796 /// createStaticVariable - Create a new descriptor for the specified static
797 /// variable.
798 DIGlobalVariable DIBuilder::
799 createStaticVariable(DIDescriptor Context, StringRef Name,
800                      StringRef LinkageName, 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     getNonCompileUnitScope(Context),
806     MDString::get(VMContext, Name),
807     MDString::get(VMContext, Name),
808     MDString::get(VMContext, LinkageName),
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   };
816   MDNode *Node = MDNode::get(VMContext, Elts);
817   AllGVs.push_back(Node);
818   return DIGlobalVariable(Node);
819 }
820
821 /// createVariable - Create a new descriptor for the specified variable.
822 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
823                                           StringRef Name, DIFile File,
824                                           unsigned LineNo, DIType Ty,
825                                           bool AlwaysPreserve, unsigned Flags,
826                                           unsigned ArgNo) {
827   Value *Elts[] = {
828     GetTagConstant(VMContext, Tag),
829     getNonCompileUnitScope(Scope),
830     MDString::get(VMContext, Name),
831     File,
832     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
833     Ty,
834     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
835     Constant::getNullValue(Type::getInt32Ty(VMContext))
836   };
837   MDNode *Node = MDNode::get(VMContext, Elts);
838   if (AlwaysPreserve) {
839     // The optimizer may remove local variable. If there is an interest
840     // to preserve variable info in such situation then stash it in a
841     // named mdnode.
842     DISubprogram Fn(getDISubprogram(Scope));
843     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
844     FnLocals->addOperand(Node);
845   }
846   return DIVariable(Node);
847 }
848
849 /// createComplexVariable - Create a new descriptor for the specified variable
850 /// which has a complex address expression for its address.
851 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
852                                             StringRef Name, DIFile F,
853                                             unsigned LineNo,
854                                             DIType Ty, ArrayRef<Value *> Addr,
855                                             unsigned ArgNo) {
856   SmallVector<Value *, 15> Elts;
857   Elts.push_back(GetTagConstant(VMContext, Tag));
858   Elts.push_back(getNonCompileUnitScope(Scope)),
859   Elts.push_back(MDString::get(VMContext, Name));
860   Elts.push_back(F);
861   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
862                                   (LineNo | (ArgNo << 24))));
863   Elts.push_back(Ty);
864   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
865   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
866   Elts.append(Addr.begin(), Addr.end());
867
868   return DIVariable(MDNode::get(VMContext, Elts));
869 }
870
871 /// createFunction - Create a new descriptor for the specified function.
872 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
873                                        StringRef Name,
874                                        StringRef LinkageName,
875                                        DIFile File, unsigned LineNo,
876                                        DIType Ty,
877                                        bool isLocalToUnit, bool isDefinition,
878                                        unsigned ScopeLine,
879                                        unsigned Flags, bool isOptimized,
880                                        Function *Fn,
881                                        MDNode *TParams,
882                                        MDNode *Decl) {
883   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
884   MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
885   Value *TVElts[] = { Temp };
886   MDNode *THolder = MDNode::get(VMContext, TVElts);
887
888   Value *Elts[] = {
889     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
890     Constant::getNullValue(Type::getInt32Ty(VMContext)),
891     getNonCompileUnitScope(Context),
892     MDString::get(VMContext, Name),
893     MDString::get(VMContext, Name),
894     MDString::get(VMContext, LinkageName),
895     File,
896     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
897     Ty,
898     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
899     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
900     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
901     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
902     NULL,
903     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
904     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
905     Fn,
906     TParams,
907     Decl,
908     THolder,
909     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
910   };
911   MDNode *Node = MDNode::get(VMContext, Elts);
912
913   // Create a named metadata so that we do not lose this mdnode.
914   AllSubprograms.push_back(Node);
915   return DISubprogram(Node);
916 }
917
918 /// createMethod - Create a new descriptor for the specified C++ method.
919 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
920                                      StringRef Name,
921                                      StringRef LinkageName,
922                                      DIFile F,
923                                      unsigned LineNo, DIType Ty,
924                                      bool isLocalToUnit,
925                                      bool isDefinition,
926                                      unsigned VK, unsigned VIndex,
927                                      MDNode *VTableHolder,
928                                      unsigned Flags,
929                                      bool isOptimized,
930                                      Function *Fn,
931                                      MDNode *TParam) {
932   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
933   MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
934   Value *TVElts[] = { Temp };
935   MDNode *THolder = MDNode::get(VMContext, TVElts);
936
937   Value *Elts[] = {
938     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
939     Constant::getNullValue(Type::getInt32Ty(VMContext)),
940     getNonCompileUnitScope(Context),
941     MDString::get(VMContext, Name),
942     MDString::get(VMContext, Name),
943     MDString::get(VMContext, LinkageName),
944     F,
945     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
946     Ty,
947     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
948     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
949     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
950     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
951     VTableHolder,
952     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
953     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
954     Fn,
955     TParam,
956     Constant::getNullValue(Type::getInt32Ty(VMContext)),
957     THolder,
958     // FIXME: Do we want to use different scope/lines?
959     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
960   };
961   MDNode *Node = MDNode::get(VMContext, Elts);
962   return DISubprogram(Node);
963 }
964
965 /// createNameSpace - This creates new descriptor for a namespace
966 /// with the specified parent scope.
967 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
968                                        DIFile File, unsigned LineNo) {
969   Value *Elts[] = {
970     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
971     getNonCompileUnitScope(Scope),
972     MDString::get(VMContext, Name),
973     File,
974     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
975   };
976   return DINameSpace(MDNode::get(VMContext, Elts));
977 }
978
979 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
980 /// an existing scope with a new filename.
981 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
982                                                      DIFile File) {
983   Value *Elts[] = {
984     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
985     Scope,
986     File
987   };
988   return DILexicalBlockFile(MDNode::get(VMContext, Elts));
989 }
990
991 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
992                                              unsigned Line, unsigned Col) {
993   // Defeat MDNode uniqing for lexical blocks by using unique id.
994   static unsigned int unique_id = 0;
995   Value *Elts[] = {
996     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
997     getNonCompileUnitScope(Scope),
998     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
999     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1000     File,
1001     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1002   };
1003   return DILexicalBlock(MDNode::get(VMContext, Elts));
1004 }
1005
1006 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1007 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1008                                       Instruction *InsertBefore) {
1009   assert(Storage && "no storage passed to dbg.declare");
1010   assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1011   if (!DeclareFn)
1012     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1013
1014   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1015   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1016 }
1017
1018 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1019 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1020                                       BasicBlock *InsertAtEnd) {
1021   assert(Storage && "no storage passed to dbg.declare");
1022   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1023   if (!DeclareFn)
1024     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1025
1026   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1027
1028   // If this block already has a terminator then insert this intrinsic
1029   // before the terminator.
1030   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1031     return CallInst::Create(DeclareFn, Args, "", T);
1032   else
1033     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1034 }
1035
1036 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1037 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1038                                                 DIVariable VarInfo,
1039                                                 Instruction *InsertBefore) {
1040   assert(V && "no value passed to dbg.value");
1041   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1042   if (!ValueFn)
1043     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1044
1045   Value *Args[] = { MDNode::get(V->getContext(), V),
1046                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1047                     VarInfo };
1048   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1049 }
1050
1051 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1052 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1053                                                 DIVariable VarInfo,
1054                                                 BasicBlock *InsertAtEnd) {
1055   assert(V && "no value passed to dbg.value");
1056   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1057   if (!ValueFn)
1058     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1059
1060   Value *Args[] = { MDNode::get(V->getContext(), V),
1061                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1062                     VarInfo };
1063   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1064 }