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