Add a TODO and explain when we can get rid of the isMain field.
[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     // isMain field can be removed when we remove the legacy debug info.
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 DIBasicType
166 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
167                            uint64_t AlignInBits, 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 DIBasicType(MDNode::get(VMContext, Elts));
184 }
185
186 /// createQualifiedType - Create debugging information entry for a qualified
187 /// type, e.g. 'const int'.
188 DIDerivedType 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 DIDerivedType(MDNode::get(VMContext, Elts));
203 }
204
205 /// createPointerType - Create debugging information entry for a pointer.
206 DIDerivedType
207 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
208                              uint64_t AlignInBits, StringRef Name) {
209   // Pointer types are encoded in DIDerivedType format.
210   Value *Elts[] = {
211     GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
212     NULL, //TheCU,
213     MDString::get(VMContext, Name),
214     NULL, // Filename
215     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
216     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
217     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
218     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
219     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
220     PointeeTy
221   };
222   return DIDerivedType(MDNode::get(VMContext, Elts));
223 }
224
225 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base) {
226   // Pointer types are encoded in DIDerivedType format.
227   Value *Elts[] = {
228     GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
229     NULL, //TheCU,
230     NULL,
231     NULL, // Filename
232     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
233     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
234     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
235     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
236     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
237     PointeeTy,
238     Base
239   };
240   return DIDerivedType(MDNode::get(VMContext, Elts));
241 }
242
243 /// createReferenceType - Create debugging information entry for a reference
244 /// type.
245 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
246   assert(RTy.Verify() && "Unable to create reference type");
247   // References are encoded in DIDerivedType format.
248   Value *Elts[] = {
249     GetTagConstant(VMContext, Tag),
250     NULL, // TheCU,
251     NULL, // Name
252     NULL, // Filename
253     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
254     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
255     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
256     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
257     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
258     RTy
259   };
260   return DIDerivedType(MDNode::get(VMContext, Elts));
261 }
262
263 /// createTypedef - Create debugging information entry for a typedef.
264 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
265                                        unsigned LineNo, DIDescriptor Context) {
266   // typedefs are encoded in DIDerivedType format.
267   assert(Ty.Verify() && "Invalid typedef type!");
268   Value *Elts[] = {
269     GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
270     getNonCompileUnitScope(Context),
271     MDString::get(VMContext, Name),
272     File,
273     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
274     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
275     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
276     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
277     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
278     Ty
279   };
280   return DIDerivedType(MDNode::get(VMContext, Elts));
281 }
282
283 /// createFriend - Create debugging information entry for a 'friend'.
284 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
285   // typedefs are encoded in DIDerivedType format.
286   assert(Ty.Verify() && "Invalid type!");
287   assert(FriendTy.Verify() && "Invalid friend type!");
288   Value *Elts[] = {
289     GetTagConstant(VMContext, dwarf::DW_TAG_friend),
290     Ty,
291     NULL, // Name
292     Ty.getFile(),
293     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
294     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
295     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
296     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
297     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
298     FriendTy
299   };
300   return DIType(MDNode::get(VMContext, Elts));
301 }
302
303 /// createInheritance - Create debugging information entry to establish
304 /// inheritance relationship between two types.
305 DIDerivedType DIBuilder::createInheritance(
306     DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
307   assert(Ty.Verify() && "Unable to create inheritance");
308   // TAG_inheritance is encoded in DIDerivedType format.
309   Value *Elts[] = {
310     GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
311     Ty,
312     NULL, // Name
313     Ty.getFile(),
314     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
315     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
316     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
317     ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
318     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
319     BaseTy
320   };
321   return DIDerivedType(MDNode::get(VMContext, Elts));
322 }
323
324 /// createMemberType - Create debugging information entry for a member.
325 DIDerivedType DIBuilder::createMemberType(
326     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
327     uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
328     unsigned Flags, 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 DIDerivedType(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     NULL,
530     NULL,
531   };
532   return DIType(MDNode::get(VMContext, Elts));
533 }
534
535 /// createUnionType - Create debugging information entry for an union.
536 DICompositeType DIBuilder::createUnionType(
537     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
538     uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags, DIArray Elements,
539     unsigned RunTimeLang) {
540   // TAG_union_type is encoded in DICompositeType format.
541   Value *Elts[] = {
542     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
543     getNonCompileUnitScope(Scope),
544     MDString::get(VMContext, Name),
545     File,
546     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
547     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
548     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
549     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
550     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
551     NULL,
552     Elements,
553     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
554     Constant::getNullValue(Type::getInt32Ty(VMContext))
555   };
556   return DICompositeType(MDNode::get(VMContext, Elts));
557 }
558
559 /// createSubroutineType - Create subroutine type.
560 DICompositeType
561 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 DICompositeType(MDNode::get(VMContext, Elts));
579 }
580
581 /// createEnumerationType - Create debugging information entry for an
582 /// enumeration.
583 DICompositeType DIBuilder::createEnumerationType(
584     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
585     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
586     DIType ClassType) {
587   // TAG_enumeration_type is encoded in DICompositeType format.
588   Value *Elts[] = {
589     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
590     getNonCompileUnitScope(Scope),
591     MDString::get(VMContext, Name),
592     File,
593     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
594     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
595     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
596     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
597     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
598     ClassType,
599     Elements,
600     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
601     Constant::getNullValue(Type::getInt32Ty(VMContext))
602   };
603   MDNode *Node = MDNode::get(VMContext, Elts);
604   AllEnumTypes.push_back(Node);
605   return DICompositeType(Node);
606 }
607
608 /// createArrayType - Create debugging information entry for an array.
609 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
610                                            DIType Ty, DIArray Subscripts) {
611   // TAG_array_type is encoded in DICompositeType format.
612   Value *Elts[] = {
613     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
614     NULL, //TheCU,
615     MDString::get(VMContext, ""),
616     NULL, //TheCU,
617     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
618     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
619     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
620     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
621     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
622     Ty,
623     Subscripts,
624     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
625     Constant::getNullValue(Type::getInt32Ty(VMContext))
626   };
627   return DICompositeType(MDNode::get(VMContext, Elts));
628 }
629
630 /// createVectorType - Create debugging information entry for a vector.
631 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
632                                    DIType Ty, DIArray Subscripts) {
633
634   // A vector is an array type with the FlagVector flag applied.
635   Value *Elts[] = {
636     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
637     NULL, //TheCU,
638     MDString::get(VMContext, ""),
639     NULL, //TheCU,
640     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
641     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
642     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
643     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
644     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
645     Ty,
646     Subscripts,
647     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
648     Constant::getNullValue(Type::getInt32Ty(VMContext))
649   };
650   return DIType(MDNode::get(VMContext, Elts));
651 }
652
653 /// createArtificialType - Create a new DIType with "artificial" flag set.
654 DIType DIBuilder::createArtificialType(DIType Ty) {
655   if (Ty.isArtificial())
656     return Ty;
657
658   SmallVector<Value *, 9> Elts;
659   MDNode *N = Ty;
660   assert (N && "Unexpected input DIType!");
661   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
662     if (Value *V = N->getOperand(i))
663       Elts.push_back(V);
664     else
665       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
666   }
667
668   unsigned CurFlags = Ty.getFlags();
669   CurFlags = CurFlags | DIType::FlagArtificial;
670
671   // Flags are stored at this slot.
672   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
673
674   return DIType(MDNode::get(VMContext, Elts));
675 }
676
677 /// createObjectPointerType - Create a new type with both the object pointer
678 /// and artificial flags set.
679 DIType DIBuilder::createObjectPointerType(DIType Ty) {
680   if (Ty.isObjectPointer())
681     return Ty;
682
683   SmallVector<Value *, 9> Elts;
684   MDNode *N = Ty;
685   assert (N && "Unexpected input DIType!");
686   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
687     if (Value *V = N->getOperand(i))
688       Elts.push_back(V);
689     else
690       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
691   }
692
693   unsigned CurFlags = Ty.getFlags();
694   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
695
696   // Flags are stored at this slot.
697   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
698
699   return DIType(MDNode::get(VMContext, Elts));
700 }
701
702 /// retainType - Retain DIType in a module even if it is not referenced
703 /// through debug info anchors.
704 void DIBuilder::retainType(DIType T) {
705   AllRetainTypes.push_back(T);
706 }
707
708 /// createUnspecifiedParameter - Create unspeicified type descriptor
709 /// for the subroutine type.
710 DIDescriptor DIBuilder::createUnspecifiedParameter() {
711   Value *Elts[] = {
712     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
713   };
714   return DIDescriptor(MDNode::get(VMContext, Elts));
715 }
716
717 /// createTemporaryType - Create a temporary forward-declared type.
718 DIType DIBuilder::createTemporaryType() {
719   // Give the temporary MDNode a tag. It doesn't matter what tag we
720   // use here as long as DIType accepts it.
721   Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
722   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
723   return DIType(Node);
724 }
725
726 /// createTemporaryType - Create a temporary forward-declared type.
727 DIType DIBuilder::createTemporaryType(DIFile F) {
728   // Give the temporary MDNode a tag. It doesn't matter what tag we
729   // use here as long as DIType accepts it.
730   Value *Elts[] = {
731     GetTagConstant(VMContext, DW_TAG_base_type),
732     TheCU,
733     NULL,
734     F
735   };
736   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
737   return DIType(Node);
738 }
739
740 /// createForwardDecl - Create a temporary forward-declared type that
741 /// can be RAUW'd if the full type is seen.
742 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
743                                     DIDescriptor Scope, DIFile F,
744                                     unsigned Line, unsigned RuntimeLang,
745                                     uint64_t SizeInBits,
746                                     uint64_t AlignInBits) {
747   // Create a temporary MDNode.
748   Value *Elts[] = {
749     GetTagConstant(VMContext, Tag),
750     getNonCompileUnitScope(Scope),
751     MDString::get(VMContext, Name),
752     F,
753     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
754     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
755     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
756     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
757     ConstantInt::get(Type::getInt32Ty(VMContext),
758                      DIDescriptor::FlagFwdDecl),
759     NULL,
760     DIArray(),
761     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
762   };
763   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
764   return DIType(Node);
765 }
766
767 /// getOrCreateArray - Get a DIArray, create one if required.
768 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
769   if (Elements.empty()) {
770     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
771     return DIArray(MDNode::get(VMContext, Null));
772   }
773   return DIArray(MDNode::get(VMContext, Elements));
774 }
775
776 /// getOrCreateSubrange - Create a descriptor for a value range.  This
777 /// implicitly uniques the values returned.
778 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
779   Value *Elts[] = {
780     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
781     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
782     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
783   };
784
785   return DISubrange(MDNode::get(VMContext, Elts));
786 }
787
788 /// createGlobalVariable - Create a new descriptor for the specified global.
789 DIGlobalVariable DIBuilder::
790 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
791                      DIType Ty, bool isLocalToUnit, Value *Val) {
792   Value *Elts[] = {
793     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
794     Constant::getNullValue(Type::getInt32Ty(VMContext)),
795     NULL, // TheCU,
796     MDString::get(VMContext, Name),
797     MDString::get(VMContext, Name),
798     MDString::get(VMContext, Name),
799     F,
800     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
801     Ty,
802     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
803     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
804     Val,
805     DIDescriptor()
806   };
807   MDNode *Node = MDNode::get(VMContext, Elts);
808   AllGVs.push_back(Node);
809   return DIGlobalVariable(Node);
810 }
811
812 /// createStaticVariable - Create a new descriptor for the specified static
813 /// variable.
814 DIGlobalVariable DIBuilder::
815 createStaticVariable(DIDescriptor Context, StringRef Name,
816                      StringRef LinkageName, DIFile F, unsigned LineNumber,
817                      DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
818   Value *Elts[] = {
819     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
820     Constant::getNullValue(Type::getInt32Ty(VMContext)),
821     getNonCompileUnitScope(Context),
822     MDString::get(VMContext, Name),
823     MDString::get(VMContext, Name),
824     MDString::get(VMContext, LinkageName),
825     F,
826     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
827     Ty,
828     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
829     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
830     Val,
831     DIDescriptor(Decl)
832   };
833   MDNode *Node = MDNode::get(VMContext, Elts);
834   AllGVs.push_back(Node);
835   return DIGlobalVariable(Node);
836 }
837
838 /// createVariable - Create a new descriptor for the specified variable.
839 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
840                                           StringRef Name, DIFile File,
841                                           unsigned LineNo, DIType Ty,
842                                           bool AlwaysPreserve, unsigned Flags,
843                                           unsigned ArgNo) {
844   Value *Elts[] = {
845     GetTagConstant(VMContext, Tag),
846     getNonCompileUnitScope(Scope),
847     MDString::get(VMContext, Name),
848     File,
849     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
850     Ty,
851     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
852     Constant::getNullValue(Type::getInt32Ty(VMContext))
853   };
854   MDNode *Node = MDNode::get(VMContext, Elts);
855   if (AlwaysPreserve) {
856     // The optimizer may remove local variable. If there is an interest
857     // to preserve variable info in such situation then stash it in a
858     // named mdnode.
859     DISubprogram Fn(getDISubprogram(Scope));
860     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
861     FnLocals->addOperand(Node);
862   }
863   return DIVariable(Node);
864 }
865
866 /// createComplexVariable - Create a new descriptor for the specified variable
867 /// which has a complex address expression for its address.
868 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
869                                             StringRef Name, DIFile F,
870                                             unsigned LineNo,
871                                             DIType Ty, ArrayRef<Value *> Addr,
872                                             unsigned ArgNo) {
873   SmallVector<Value *, 15> Elts;
874   Elts.push_back(GetTagConstant(VMContext, Tag));
875   Elts.push_back(getNonCompileUnitScope(Scope)),
876   Elts.push_back(MDString::get(VMContext, Name));
877   Elts.push_back(F);
878   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
879                                   (LineNo | (ArgNo << 24))));
880   Elts.push_back(Ty);
881   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
882   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
883   Elts.append(Addr.begin(), Addr.end());
884
885   return DIVariable(MDNode::get(VMContext, Elts));
886 }
887
888 /// createFunction - Create a new descriptor for the specified function.
889 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
890                                        StringRef Name,
891                                        StringRef LinkageName,
892                                        DIFile File, unsigned LineNo,
893                                        DIType Ty,
894                                        bool isLocalToUnit, bool isDefinition,
895                                        unsigned ScopeLine,
896                                        unsigned Flags, bool isOptimized,
897                                        Function *Fn,
898                                        MDNode *TParams,
899                                        MDNode *Decl) {
900   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
901   Value *Elts[] = {
902     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
903     Constant::getNullValue(Type::getInt32Ty(VMContext)),
904     getNonCompileUnitScope(Context),
905     MDString::get(VMContext, Name),
906     MDString::get(VMContext, Name),
907     MDString::get(VMContext, LinkageName),
908     File,
909     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
910     Ty,
911     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
912     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
913     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
914     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
915     NULL,
916     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
917     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
918     Fn,
919     TParams,
920     Decl,
921     MDNode::getTemporary(VMContext, TElts),
922     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
923   };
924   MDNode *Node = MDNode::get(VMContext, Elts);
925
926   // Create a named metadata so that we do not lose this mdnode.
927   if (isDefinition)
928     AllSubprograms.push_back(Node);
929   return DISubprogram(Node);
930 }
931
932 /// createMethod - Create a new descriptor for the specified C++ method.
933 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
934                                      StringRef Name,
935                                      StringRef LinkageName,
936                                      DIFile F,
937                                      unsigned LineNo, DIType Ty,
938                                      bool isLocalToUnit,
939                                      bool isDefinition,
940                                      unsigned VK, unsigned VIndex,
941                                      MDNode *VTableHolder,
942                                      unsigned Flags,
943                                      bool isOptimized,
944                                      Function *Fn,
945                                      MDNode *TParam) {
946   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
947   Value *Elts[] = {
948     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
949     Constant::getNullValue(Type::getInt32Ty(VMContext)),
950     getNonCompileUnitScope(Context),
951     MDString::get(VMContext, Name),
952     MDString::get(VMContext, Name),
953     MDString::get(VMContext, LinkageName),
954     F,
955     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
956     Ty,
957     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
958     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
959     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
960     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
961     VTableHolder,
962     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
963     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
964     Fn,
965     TParam,
966     Constant::getNullValue(Type::getInt32Ty(VMContext)),
967     MDNode::getTemporary(VMContext, TElts),
968     // FIXME: Do we want to use different scope/lines?
969     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
970   };
971   MDNode *Node = MDNode::get(VMContext, Elts);
972   if (isDefinition)
973     AllSubprograms.push_back(Node);
974   return DISubprogram(Node);
975 }
976
977 /// createNameSpace - This creates new descriptor for a namespace
978 /// with the specified parent scope.
979 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
980                                        DIFile File, unsigned LineNo) {
981   Value *Elts[] = {
982     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
983     getNonCompileUnitScope(Scope),
984     MDString::get(VMContext, Name),
985     File,
986     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
987   };
988   return DINameSpace(MDNode::get(VMContext, Elts));
989 }
990
991 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
992 /// an existing scope with a new filename.
993 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
994                                                      DIFile File) {
995   Value *Elts[] = {
996     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
997     Scope,
998     File
999   };
1000   return DILexicalBlockFile(MDNode::get(VMContext, Elts));
1001 }
1002
1003 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1004                                              unsigned Line, unsigned Col) {
1005   // Defeat MDNode uniqing for lexical blocks by using unique id.
1006   static unsigned int unique_id = 0;
1007   Value *Elts[] = {
1008     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1009     getNonCompileUnitScope(Scope),
1010     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1011     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1012     File,
1013     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1014   };
1015   return DILexicalBlock(MDNode::get(VMContext, Elts));
1016 }
1017
1018 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1019 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1020                                       Instruction *InsertBefore) {
1021   assert(Storage && "no storage passed to dbg.declare");
1022   assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1023   if (!DeclareFn)
1024     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1025
1026   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1027   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1028 }
1029
1030 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1031 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1032                                       BasicBlock *InsertAtEnd) {
1033   assert(Storage && "no storage passed to dbg.declare");
1034   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1035   if (!DeclareFn)
1036     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1037
1038   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1039
1040   // If this block already has a terminator then insert this intrinsic
1041   // before the terminator.
1042   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1043     return CallInst::Create(DeclareFn, Args, "", T);
1044   else
1045     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1046 }
1047
1048 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1049 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1050                                                 DIVariable VarInfo,
1051                                                 Instruction *InsertBefore) {
1052   assert(V && "no value passed to dbg.value");
1053   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1054   if (!ValueFn)
1055     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1056
1057   Value *Args[] = { MDNode::get(V->getContext(), V),
1058                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1059                     VarInfo };
1060   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1061 }
1062
1063 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1064 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1065                                                 DIVariable VarInfo,
1066                                                 BasicBlock *InsertAtEnd) {
1067   assert(V && "no value passed to dbg.value");
1068   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1069   if (!ValueFn)
1070     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1071
1072   Value *Args[] = { MDNode::get(V->getContext(), V),
1073                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1074                     VarInfo };
1075   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1076 }