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