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