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