9fd0cd8683c584ccec084f81f1ed63977e79821f
[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 /// createArtificialType - Create a new DIType with "artificial" flag set.
663 DIType DIBuilder::createObjectPointerType(DIType Ty) {
664   if (Ty.isObjectPointer())
665     return Ty;
666
667   SmallVector<Value *, 9> Elts;
668   MDNode *N = Ty;
669   assert (N && "Unexpected input DIType!");
670   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
671     if (Value *V = N->getOperand(i))
672       Elts.push_back(V);
673     else
674       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
675   }
676
677   unsigned CurFlags = Ty.getFlags();
678   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
679
680   // Flags are stored at this slot.
681   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
682
683   return DIType(MDNode::get(VMContext, Elts));
684 }
685
686 /// retainType - Retain DIType in a module even if it is not referenced
687 /// through debug info anchors.
688 void DIBuilder::retainType(DIType T) {
689   AllRetainTypes.push_back(T);
690 }
691
692 /// createUnspecifiedParameter - Create unspeicified type descriptor
693 /// for the subroutine type.
694 DIDescriptor DIBuilder::createUnspecifiedParameter() {
695   Value *Elts[] = {
696     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
697   };
698   return DIDescriptor(MDNode::get(VMContext, Elts));
699 }
700
701 /// createTemporaryType - Create a temporary forward-declared type.
702 DIType DIBuilder::createTemporaryType() {
703   // Give the temporary MDNode a tag. It doesn't matter what tag we
704   // use here as long as DIType accepts it.
705   Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
706   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
707   return DIType(Node);
708 }
709
710 /// createTemporaryType - Create a temporary forward-declared type.
711 DIType DIBuilder::createTemporaryType(DIFile F) {
712   // Give the temporary MDNode a tag. It doesn't matter what tag we
713   // use here as long as DIType accepts it.
714   Value *Elts[] = {
715     GetTagConstant(VMContext, DW_TAG_base_type),
716     TheCU,
717     NULL,
718     F
719   };
720   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
721   return DIType(Node);
722 }
723
724 /// createForwardDecl - Create a temporary forward-declared type that
725 /// can be RAUW'd if the full type is seen.
726 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
727                                     DIDescriptor Scope, DIFile F,
728                                     unsigned Line, unsigned RuntimeLang,
729                                     uint64_t SizeInBits,
730                                     uint64_t AlignInBits) {
731   // Create a temporary MDNode.
732   Value *Elts[] = {
733     GetTagConstant(VMContext, Tag),
734     getNonCompileUnitScope(Scope),
735     MDString::get(VMContext, Name),
736     F,
737     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
738     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
739     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
740     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
741     ConstantInt::get(Type::getInt32Ty(VMContext),
742                      DIDescriptor::FlagFwdDecl),
743     NULL,
744     DIArray(),
745     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
746   };
747   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
748   return DIType(Node);
749 }
750
751 /// getOrCreateArray - Get a DIArray, create one if required.
752 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
753   if (Elements.empty()) {
754     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
755     return DIArray(MDNode::get(VMContext, Null));
756   }
757   return DIArray(MDNode::get(VMContext, Elements));
758 }
759
760 /// getOrCreateSubrange - Create a descriptor for a value range.  This
761 /// implicitly uniques the values returned.
762 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
763   Value *Elts[] = {
764     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
765     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
766     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
767   };
768
769   return DISubrange(MDNode::get(VMContext, Elts));
770 }
771
772 /// createGlobalVariable - Create a new descriptor for the specified global.
773 DIGlobalVariable DIBuilder::
774 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
775                      DIType Ty, bool isLocalToUnit, Value *Val) {
776   Value *Elts[] = {
777     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
778     Constant::getNullValue(Type::getInt32Ty(VMContext)),
779     NULL, // TheCU,
780     MDString::get(VMContext, Name),
781     MDString::get(VMContext, Name),
782     MDString::get(VMContext, Name),
783     F,
784     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
785     Ty,
786     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
787     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
788     Val
789   };
790   MDNode *Node = MDNode::get(VMContext, Elts);
791   AllGVs.push_back(Node);
792   return DIGlobalVariable(Node);
793 }
794
795 /// createStaticVariable - Create a new descriptor for the specified static
796 /// variable.
797 DIGlobalVariable DIBuilder::
798 createStaticVariable(DIDescriptor Context, StringRef Name,
799                      StringRef LinkageName, DIFile F, unsigned LineNumber,
800                      DIType Ty, bool isLocalToUnit, Value *Val) {
801   Value *Elts[] = {
802     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
803     Constant::getNullValue(Type::getInt32Ty(VMContext)),
804     getNonCompileUnitScope(Context),
805     MDString::get(VMContext, Name),
806     MDString::get(VMContext, Name),
807     MDString::get(VMContext, LinkageName),
808     F,
809     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
810     Ty,
811     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
812     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
813     Val
814   };
815   MDNode *Node = MDNode::get(VMContext, Elts);
816   AllGVs.push_back(Node);
817   return DIGlobalVariable(Node);
818 }
819
820 /// createVariable - Create a new descriptor for the specified variable.
821 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
822                                           StringRef Name, DIFile File,
823                                           unsigned LineNo, DIType Ty,
824                                           bool AlwaysPreserve, unsigned Flags,
825                                           unsigned ArgNo) {
826   Value *Elts[] = {
827     GetTagConstant(VMContext, Tag),
828     getNonCompileUnitScope(Scope),
829     MDString::get(VMContext, Name),
830     File,
831     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
832     Ty,
833     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
834     Constant::getNullValue(Type::getInt32Ty(VMContext))
835   };
836   MDNode *Node = MDNode::get(VMContext, Elts);
837   if (AlwaysPreserve) {
838     // The optimizer may remove local variable. If there is an interest
839     // to preserve variable info in such situation then stash it in a
840     // named mdnode.
841     DISubprogram Fn(getDISubprogram(Scope));
842     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
843     FnLocals->addOperand(Node);
844   }
845   return DIVariable(Node);
846 }
847
848 /// createComplexVariable - Create a new descriptor for the specified variable
849 /// which has a complex address expression for its address.
850 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
851                                             StringRef Name, DIFile F,
852                                             unsigned LineNo,
853                                             DIType Ty, ArrayRef<Value *> Addr,
854                                             unsigned ArgNo) {
855   SmallVector<Value *, 15> Elts;
856   Elts.push_back(GetTagConstant(VMContext, Tag));
857   Elts.push_back(getNonCompileUnitScope(Scope)),
858   Elts.push_back(MDString::get(VMContext, Name));
859   Elts.push_back(F);
860   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
861                                   (LineNo | (ArgNo << 24))));
862   Elts.push_back(Ty);
863   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
864   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
865   Elts.append(Addr.begin(), Addr.end());
866
867   return DIVariable(MDNode::get(VMContext, Elts));
868 }
869
870 /// createFunction - Create a new descriptor for the specified function.
871 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
872                                        StringRef Name,
873                                        StringRef LinkageName,
874                                        DIFile File, unsigned LineNo,
875                                        DIType Ty,
876                                        bool isLocalToUnit, bool isDefinition,
877                                        unsigned ScopeLine,
878                                        unsigned Flags, bool isOptimized,
879                                        Function *Fn,
880                                        MDNode *TParams,
881                                        MDNode *Decl) {
882   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
883   MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
884   Value *TVElts[] = { Temp };
885   MDNode *THolder = MDNode::get(VMContext, TVElts);
886
887   Value *Elts[] = {
888     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
889     Constant::getNullValue(Type::getInt32Ty(VMContext)),
890     getNonCompileUnitScope(Context),
891     MDString::get(VMContext, Name),
892     MDString::get(VMContext, Name),
893     MDString::get(VMContext, LinkageName),
894     File,
895     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
896     Ty,
897     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
898     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
899     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
900     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
901     NULL,
902     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
903     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
904     Fn,
905     TParams,
906     Decl,
907     THolder,
908     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
909   };
910   MDNode *Node = MDNode::get(VMContext, Elts);
911
912   // Create a named metadata so that we do not lose this mdnode.
913   AllSubprograms.push_back(Node);
914   return DISubprogram(Node);
915 }
916
917 /// createMethod - Create a new descriptor for the specified C++ method.
918 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
919                                      StringRef Name,
920                                      StringRef LinkageName,
921                                      DIFile F,
922                                      unsigned LineNo, DIType Ty,
923                                      bool isLocalToUnit,
924                                      bool isDefinition,
925                                      unsigned VK, unsigned VIndex,
926                                      MDNode *VTableHolder,
927                                      unsigned Flags,
928                                      bool isOptimized,
929                                      Function *Fn,
930                                      MDNode *TParam) {
931   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
932   MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
933   Value *TVElts[] = { Temp };
934   MDNode *THolder = MDNode::get(VMContext, TVElts);
935
936   Value *Elts[] = {
937     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
938     Constant::getNullValue(Type::getInt32Ty(VMContext)),
939     getNonCompileUnitScope(Context),
940     MDString::get(VMContext, Name),
941     MDString::get(VMContext, Name),
942     MDString::get(VMContext, LinkageName),
943     F,
944     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
945     Ty,
946     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
947     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
948     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
949     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
950     VTableHolder,
951     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
952     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
953     Fn,
954     TParam,
955     Constant::getNullValue(Type::getInt32Ty(VMContext)),
956     THolder,
957     // FIXME: Do we want to use different scope/lines?
958     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
959   };
960   MDNode *Node = MDNode::get(VMContext, Elts);
961   return DISubprogram(Node);
962 }
963
964 /// createNameSpace - This creates new descriptor for a namespace
965 /// with the specified parent scope.
966 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
967                                        DIFile File, unsigned LineNo) {
968   Value *Elts[] = {
969     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
970     getNonCompileUnitScope(Scope),
971     MDString::get(VMContext, Name),
972     File,
973     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
974   };
975   return DINameSpace(MDNode::get(VMContext, Elts));
976 }
977
978 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
979 /// an existing scope with a new filename.
980 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
981                                                      DIFile File) {
982   Value *Elts[] = {
983     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
984     Scope,
985     File
986   };
987   return DILexicalBlockFile(MDNode::get(VMContext, Elts));
988 }
989
990 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
991                                              unsigned Line, unsigned Col) {
992   // Defeat MDNode uniqing for lexical blocks by using unique id.
993   static unsigned int unique_id = 0;
994   Value *Elts[] = {
995     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
996     getNonCompileUnitScope(Scope),
997     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
998     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
999     File,
1000     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1001   };
1002   return DILexicalBlock(MDNode::get(VMContext, Elts));
1003 }
1004
1005 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1006 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1007                                       Instruction *InsertBefore) {
1008   assert(Storage && "no storage passed to dbg.declare");
1009   assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1010   if (!DeclareFn)
1011     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1012
1013   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1014   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1015 }
1016
1017 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1018 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1019                                       BasicBlock *InsertAtEnd) {
1020   assert(Storage && "no storage passed to dbg.declare");
1021   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1022   if (!DeclareFn)
1023     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1024
1025   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1026
1027   // If this block already has a terminator then insert this intrinsic
1028   // before the terminator.
1029   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1030     return CallInst::Create(DeclareFn, Args, "", T);
1031   else
1032     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1033 }
1034
1035 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1036 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1037                                                 DIVariable VarInfo,
1038                                                 Instruction *InsertBefore) {
1039   assert(V && "no value passed to dbg.value");
1040   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1041   if (!ValueFn)
1042     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1043
1044   Value *Args[] = { MDNode::get(V->getContext(), V),
1045                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1046                     VarInfo };
1047   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1048 }
1049
1050 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1051 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1052                                                 DIVariable VarInfo,
1053                                                 BasicBlock *InsertAtEnd) {
1054   assert(V && "no value passed to dbg.value");
1055   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1056   if (!ValueFn)
1057     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1058
1059   Value *Args[] = { MDNode::get(V->getContext(), V),
1060                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1061                     VarInfo };
1062   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1063 }