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