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