Solidify the assumption that a DW_TAG_subprogram's type is a DW_TAG_subroutine_type
[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 DIType 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 DIType(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 DIType 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 DIType(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 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
417                                          DIFile File, unsigned LineNumber,
418                                          DIType Ty, unsigned Flags,
419                                          llvm::Value *Val) {
420   // TAG_member is encoded in DIDerivedType format.
421   Flags |= DIDescriptor::FlagStaticMember;
422   Value *Elts[] = {
423     GetTagConstant(VMContext, dwarf::DW_TAG_member),
424     File.getFileNode(),
425     getNonCompileUnitScope(Scope),
426     MDString::get(VMContext, Name),
427     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
428     ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
429     ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
430     ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
431     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
432     Ty,
433     Val
434   };
435   return DIType(MDNode::get(VMContext, Elts));
436 }
437
438 /// createObjCIVar - Create debugging information entry for Objective-C
439 /// instance variable.
440 DIType DIBuilder::createObjCIVar(StringRef Name,
441                                  DIFile File, unsigned LineNumber,
442                                  uint64_t SizeInBits, uint64_t AlignInBits,
443                                  uint64_t OffsetInBits, unsigned Flags,
444                                  DIType Ty, StringRef PropertyName,
445                                  StringRef GetterName, StringRef SetterName,
446                                  unsigned PropertyAttributes) {
447   // TAG_member is encoded in DIDerivedType format.
448   Value *Elts[] = {
449     GetTagConstant(VMContext, dwarf::DW_TAG_member),
450     File.getFileNode(),
451     getNonCompileUnitScope(File),
452     MDString::get(VMContext, Name),
453     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
454     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
455     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
456     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
457     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
458     Ty,
459     MDString::get(VMContext, PropertyName),
460     MDString::get(VMContext, GetterName),
461     MDString::get(VMContext, SetterName),
462     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
463   };
464   return DIType(MDNode::get(VMContext, Elts));
465 }
466
467 /// createObjCIVar - Create debugging information entry for Objective-C
468 /// instance variable.
469 DIType DIBuilder::createObjCIVar(StringRef Name,
470                                  DIFile File, unsigned LineNumber,
471                                  uint64_t SizeInBits, uint64_t AlignInBits,
472                                  uint64_t OffsetInBits, unsigned Flags,
473                                  DIType Ty, MDNode *PropertyNode) {
474   // TAG_member is encoded in DIDerivedType format.
475   Value *Elts[] = {
476     GetTagConstant(VMContext, dwarf::DW_TAG_member),
477     File.getFileNode(),
478     getNonCompileUnitScope(File),
479     MDString::get(VMContext, Name),
480     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
481     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
482     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
483     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
484     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
485     Ty,
486     PropertyNode
487   };
488   return DIType(MDNode::get(VMContext, Elts));
489 }
490
491 /// createObjCProperty - Create debugging information entry for Objective-C
492 /// property.
493 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
494                                              DIFile File, unsigned LineNumber,
495                                              StringRef GetterName,
496                                              StringRef SetterName,
497                                              unsigned PropertyAttributes,
498                                              DIType Ty) {
499   Value *Elts[] = {
500     GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
501     MDString::get(VMContext, Name),
502     File,
503     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
504     MDString::get(VMContext, GetterName),
505     MDString::get(VMContext, SetterName),
506     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
507     Ty
508   };
509   return DIObjCProperty(MDNode::get(VMContext, Elts));
510 }
511
512 /// createTemplateTypeParameter - Create debugging information for template
513 /// type parameter.
514 DITemplateTypeParameter
515 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
516                                        DIType Ty, MDNode *File, unsigned LineNo,
517                                        unsigned ColumnNo) {
518   Value *Elts[] = {
519     GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
520     getNonCompileUnitScope(Context),
521     MDString::get(VMContext, Name),
522     Ty,
523     File,
524     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
525     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
526   };
527   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
528 }
529
530 /// createTemplateValueParameter - Create debugging information for template
531 /// value parameter.
532 DITemplateValueParameter
533 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
534                                         DIType Ty, Value *Val,
535                                         MDNode *File, unsigned LineNo,
536                                         unsigned ColumnNo) {
537   Value *Elts[] = {
538     GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
539     getNonCompileUnitScope(Context),
540     MDString::get(VMContext, Name),
541     Ty,
542     Val,
543     File,
544     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
545     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
546   };
547   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
548 }
549
550 /// createClassType - Create debugging information entry for a class.
551 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
552                                            DIFile File, unsigned LineNumber,
553                                            uint64_t SizeInBits,
554                                            uint64_t AlignInBits,
555                                            uint64_t OffsetInBits,
556                                            unsigned Flags, DIType DerivedFrom,
557                                            DIArray Elements,
558                                            MDNode *VTableHolder,
559                                            MDNode *TemplateParams) {
560   assert((!Context || Context.Verify()) &&
561          "createClassType should be called with a valid Context");
562   // TAG_class_type is encoded in DICompositeType format.
563   Value *Elts[] = {
564     GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
565     File.getFileNode(),
566     getNonCompileUnitScope(Context),
567     MDString::get(VMContext, Name),
568     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
569     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
570     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
571     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
572     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
573     DerivedFrom,
574     Elements,
575     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
576     VTableHolder,
577     TemplateParams
578   };
579   DICompositeType R(MDNode::get(VMContext, Elts));
580   assert(R.Verify() && "createClassType should return a verifiable DIType");
581   return R;
582 }
583
584 /// createStructType - Create debugging information entry for a struct.
585 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
586                                             StringRef Name, DIFile File,
587                                             unsigned LineNumber,
588                                             uint64_t SizeInBits,
589                                             uint64_t AlignInBits,
590                                             unsigned Flags, DIType DerivedFrom,
591                                             DIArray Elements,
592                                             unsigned RunTimeLang,
593                                             MDNode *VTableHolder) {
594  // TAG_structure_type is encoded in DICompositeType format.
595   Value *Elts[] = {
596     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
597     File.getFileNode(),
598     getNonCompileUnitScope(Context),
599     MDString::get(VMContext, Name),
600     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
601     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
602     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
603     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
604     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
605     DerivedFrom,
606     Elements,
607     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
608     VTableHolder,
609     NULL,
610   };
611   DICompositeType R(MDNode::get(VMContext, Elts));
612   assert(R.Verify() && "createStructType should return a verifiable DIType");
613   return R;
614 }
615
616 /// createUnionType - Create debugging information entry for an union.
617 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
618                                            DIFile File, unsigned LineNumber,
619                                            uint64_t SizeInBits,
620                                            uint64_t AlignInBits, unsigned Flags,
621                                            DIArray Elements,
622                                            unsigned RunTimeLang) {
623   // TAG_union_type is encoded in DICompositeType format.
624   Value *Elts[] = {
625     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
626     File.getFileNode(),
627     getNonCompileUnitScope(Scope),
628     MDString::get(VMContext, Name),
629     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
630     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
631     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
632     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
633     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
634     NULL,
635     Elements,
636     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
637     Constant::getNullValue(Type::getInt32Ty(VMContext)),
638     NULL
639   };
640   return DICompositeType(MDNode::get(VMContext, Elts));
641 }
642
643 /// createSubroutineType - Create subroutine type.
644 DICompositeType
645 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
646   // TAG_subroutine_type is encoded in DICompositeType format.
647   Value *Elts[] = {
648     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
649     Constant::getNullValue(Type::getInt32Ty(VMContext)),
650     Constant::getNullValue(Type::getInt32Ty(VMContext)),
651     MDString::get(VMContext, ""),
652     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
653     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
654     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
655     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
656     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
657     NULL,
658     ParameterTypes,
659     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
660     Constant::getNullValue(Type::getInt32Ty(VMContext))
661   };
662   return DICompositeType(MDNode::get(VMContext, Elts));
663 }
664
665 /// createEnumerationType - Create debugging information entry for an
666 /// enumeration.
667 DICompositeType DIBuilder::createEnumerationType(
668     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
669     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
670     DIType UnderlyingType) {
671   // TAG_enumeration_type is encoded in DICompositeType format.
672   Value *Elts[] = {
673     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
674     File.getFileNode(),
675     getNonCompileUnitScope(Scope),
676     MDString::get(VMContext, Name),
677     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
678     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
679     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
680     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
681     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
682     UnderlyingType,
683     Elements,
684     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
685     Constant::getNullValue(Type::getInt32Ty(VMContext))
686   };
687   MDNode *Node = MDNode::get(VMContext, Elts);
688   AllEnumTypes.push_back(Node);
689   return DICompositeType(Node);
690 }
691
692 /// createArrayType - Create debugging information entry for an array.
693 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
694                                            DIType Ty, DIArray Subscripts) {
695   // TAG_array_type is encoded in DICompositeType format.
696   Value *Elts[] = {
697     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
698     NULL, // Filename/Directory,
699     NULL, //TheCU,
700     MDString::get(VMContext, ""),
701     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
702     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
703     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
704     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
705     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
706     Ty,
707     Subscripts,
708     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
709     Constant::getNullValue(Type::getInt32Ty(VMContext))
710   };
711   return DICompositeType(MDNode::get(VMContext, Elts));
712 }
713
714 /// createVectorType - Create debugging information entry for a vector.
715 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
716                                    DIType Ty, DIArray Subscripts) {
717
718   // A vector is an array type with the FlagVector flag applied.
719   Value *Elts[] = {
720     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
721     NULL, // Filename/Directory,
722     NULL, //TheCU,
723     MDString::get(VMContext, ""),
724     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
725     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
726     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
727     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
728     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
729     Ty,
730     Subscripts,
731     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
732     Constant::getNullValue(Type::getInt32Ty(VMContext))
733   };
734   return DIType(MDNode::get(VMContext, Elts));
735 }
736
737 /// createArtificialType - Create a new DIType with "artificial" flag set.
738 DIType DIBuilder::createArtificialType(DIType Ty) {
739   if (Ty.isArtificial())
740     return Ty;
741
742   SmallVector<Value *, 9> Elts;
743   MDNode *N = Ty;
744   assert (N && "Unexpected input DIType!");
745   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
746     if (Value *V = N->getOperand(i))
747       Elts.push_back(V);
748     else
749       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
750   }
751
752   unsigned CurFlags = Ty.getFlags();
753   CurFlags = CurFlags | DIType::FlagArtificial;
754
755   // Flags are stored at this slot.
756   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
757
758   return DIType(MDNode::get(VMContext, Elts));
759 }
760
761 /// createObjectPointerType - Create a new type with both the object pointer
762 /// and artificial flags set.
763 DIType DIBuilder::createObjectPointerType(DIType Ty) {
764   if (Ty.isObjectPointer())
765     return Ty;
766
767   SmallVector<Value *, 9> Elts;
768   MDNode *N = Ty;
769   assert (N && "Unexpected input DIType!");
770   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
771     if (Value *V = N->getOperand(i))
772       Elts.push_back(V);
773     else
774       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
775   }
776
777   unsigned CurFlags = Ty.getFlags();
778   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
779
780   // Flags are stored at this slot.
781   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
782
783   return DIType(MDNode::get(VMContext, Elts));
784 }
785
786 /// retainType - Retain DIType in a module even if it is not referenced
787 /// through debug info anchors.
788 void DIBuilder::retainType(DIType T) {
789   AllRetainTypes.push_back(T);
790 }
791
792 /// createUnspecifiedParameter - Create unspeicified type descriptor
793 /// for the subroutine type.
794 DIDescriptor DIBuilder::createUnspecifiedParameter() {
795   Value *Elts[] = {
796     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
797   };
798   return DIDescriptor(MDNode::get(VMContext, Elts));
799 }
800
801 /// createForwardDecl - Create a temporary forward-declared type that
802 /// can be RAUW'd if the full type is seen.
803 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
804                                     DIDescriptor Scope, DIFile F,
805                                     unsigned Line, unsigned RuntimeLang,
806                                     uint64_t SizeInBits,
807                                     uint64_t AlignInBits) {
808   // Create a temporary MDNode.
809   Value *Elts[] = {
810     GetTagConstant(VMContext, Tag),
811     F.getFileNode(),
812     getNonCompileUnitScope(Scope),
813     MDString::get(VMContext, Name),
814     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
815     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
816     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
817     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
818     ConstantInt::get(Type::getInt32Ty(VMContext),
819                      DIDescriptor::FlagFwdDecl),
820     NULL,
821     DIArray(),
822     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
823   };
824   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
825   assert(DIType(Node).Verify() &&
826          "createForwardDecl result should be verifiable");
827   return DIType(Node);
828 }
829
830 /// getOrCreateArray - Get a DIArray, create one if required.
831 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
832   if (Elements.empty()) {
833     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
834     return DIArray(MDNode::get(VMContext, Null));
835   }
836   return DIArray(MDNode::get(VMContext, Elements));
837 }
838
839 /// getOrCreateSubrange - Create a descriptor for a value range.  This
840 /// implicitly uniques the values returned.
841 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
842   Value *Elts[] = {
843     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
844     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
845     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
846   };
847
848   return DISubrange(MDNode::get(VMContext, Elts));
849 }
850
851 /// \brief Create a new descriptor for the specified global.
852 DIGlobalVariable DIBuilder::
853 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
854                      unsigned LineNumber, DIType Ty, bool isLocalToUnit,
855                      Value *Val) {
856   Value *Elts[] = {
857     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
858     Constant::getNullValue(Type::getInt32Ty(VMContext)),
859     NULL, // TheCU,
860     MDString::get(VMContext, Name),
861     MDString::get(VMContext, Name),
862     MDString::get(VMContext, LinkageName),
863     F,
864     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
865     Ty,
866     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
867     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
868     Val,
869     DIDescriptor()
870   };
871   MDNode *Node = MDNode::get(VMContext, Elts);
872   AllGVs.push_back(Node);
873   return DIGlobalVariable(Node);
874 }
875
876 /// \brief Create a new descriptor for the specified global.
877 DIGlobalVariable DIBuilder::
878 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
879                      DIType Ty, bool isLocalToUnit, Value *Val) {
880   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
881                               Val);
882 }
883
884 /// createStaticVariable - Create a new descriptor for the specified static
885 /// variable.
886 DIGlobalVariable DIBuilder::
887 createStaticVariable(DIDescriptor Context, StringRef Name,
888                      StringRef LinkageName, DIFile F, unsigned LineNumber,
889                      DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
890   Value *Elts[] = {
891     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
892     Constant::getNullValue(Type::getInt32Ty(VMContext)),
893     getNonCompileUnitScope(Context),
894     MDString::get(VMContext, Name),
895     MDString::get(VMContext, Name),
896     MDString::get(VMContext, LinkageName),
897     F,
898     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
899     Ty,
900     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
901     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
902     Val,
903     DIDescriptor(Decl)
904   };
905   MDNode *Node = MDNode::get(VMContext, Elts);
906   AllGVs.push_back(Node);
907   return DIGlobalVariable(Node);
908 }
909
910 /// createVariable - Create a new descriptor for the specified variable.
911 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
912                                           StringRef Name, DIFile File,
913                                           unsigned LineNo, DIType Ty,
914                                           bool AlwaysPreserve, unsigned Flags,
915                                           unsigned ArgNo) {
916   DIDescriptor Context(getNonCompileUnitScope(Scope));
917   assert((!Context || Context.Verify()) &&
918          "createLocalVariable should be called with a valid Context");
919   assert(Ty.Verify() &&
920          "createLocalVariable should be called with a valid type");
921   Value *Elts[] = {
922     GetTagConstant(VMContext, Tag),
923     getNonCompileUnitScope(Scope),
924     MDString::get(VMContext, Name),
925     File,
926     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
927     Ty,
928     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
929     Constant::getNullValue(Type::getInt32Ty(VMContext))
930   };
931   MDNode *Node = MDNode::get(VMContext, Elts);
932   if (AlwaysPreserve) {
933     // The optimizer may remove local variable. If there is an interest
934     // to preserve variable info in such situation then stash it in a
935     // named mdnode.
936     DISubprogram Fn(getDISubprogram(Scope));
937     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
938     FnLocals->addOperand(Node);
939   }
940   assert(DIVariable(Node).Verify() &&
941          "createLocalVariable should return a verifiable DIVariable");
942   return DIVariable(Node);
943 }
944
945 /// createComplexVariable - Create a new descriptor for the specified variable
946 /// which has a complex address expression for its address.
947 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
948                                             StringRef Name, DIFile F,
949                                             unsigned LineNo,
950                                             DIType Ty, ArrayRef<Value *> Addr,
951                                             unsigned ArgNo) {
952   SmallVector<Value *, 15> Elts;
953   Elts.push_back(GetTagConstant(VMContext, Tag));
954   Elts.push_back(getNonCompileUnitScope(Scope)),
955   Elts.push_back(MDString::get(VMContext, Name));
956   Elts.push_back(F);
957   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
958                                   (LineNo | (ArgNo << 24))));
959   Elts.push_back(Ty);
960   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
961   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
962   Elts.append(Addr.begin(), Addr.end());
963
964   return DIVariable(MDNode::get(VMContext, Elts));
965 }
966
967 /// createFunction - Create a new descriptor for the specified function.
968 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
969                                        StringRef Name,
970                                        StringRef LinkageName,
971                                        DIFile File, unsigned LineNo,
972                                        DICompositeType Ty,
973                                        bool isLocalToUnit, bool isDefinition,
974                                        unsigned ScopeLine,
975                                        unsigned Flags, bool isOptimized,
976                                        Function *Fn,
977                                        MDNode *TParams,
978                                        MDNode *Decl) {
979   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
980          "function types should be subroutines");
981   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
982   Value *Elts[] = {
983     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
984     File.getFileNode(),
985     getNonCompileUnitScope(Context),
986     MDString::get(VMContext, Name),
987     MDString::get(VMContext, Name),
988     MDString::get(VMContext, LinkageName),
989     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
990     Ty,
991     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
992     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
993     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
994     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
995     NULL,
996     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
997     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
998     Fn,
999     TParams,
1000     Decl,
1001     MDNode::getTemporary(VMContext, TElts),
1002     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1003   };
1004   MDNode *Node = MDNode::get(VMContext, Elts);
1005
1006   // Create a named metadata so that we do not lose this mdnode.
1007   if (isDefinition)
1008     AllSubprograms.push_back(Node);
1009   DISubprogram S(Node);
1010   assert(S.Verify() && "createFunction should return a valid DISubprogram");
1011   return S;
1012 }
1013
1014 /// createMethod - Create a new descriptor for the specified C++ method.
1015 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
1016                                      StringRef Name,
1017                                      StringRef LinkageName,
1018                                      DIFile F,
1019                                      unsigned LineNo, DICompositeType Ty,
1020                                      bool isLocalToUnit,
1021                                      bool isDefinition,
1022                                      unsigned VK, unsigned VIndex,
1023                                      MDNode *VTableHolder,
1024                                      unsigned Flags,
1025                                      bool isOptimized,
1026                                      Function *Fn,
1027                                      MDNode *TParam) {
1028   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1029          "function types should be subroutines");
1030   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1031   Value *Elts[] = {
1032     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1033     F.getFileNode(),
1034     getNonCompileUnitScope(Context),
1035     MDString::get(VMContext, Name),
1036     MDString::get(VMContext, Name),
1037     MDString::get(VMContext, LinkageName),
1038     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1039     Ty,
1040     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1041     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1042     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1043     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1044     VTableHolder,
1045     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1046     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1047     Fn,
1048     TParam,
1049     Constant::getNullValue(Type::getInt32Ty(VMContext)),
1050     MDNode::getTemporary(VMContext, TElts),
1051     // FIXME: Do we want to use different scope/lines?
1052     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1053   };
1054   MDNode *Node = MDNode::get(VMContext, Elts);
1055   if (isDefinition)
1056     AllSubprograms.push_back(Node);
1057   DISubprogram S(Node);
1058   assert(S.Verify() && "createMethod should return a valid DISubprogram");
1059   return S;
1060 }
1061
1062 /// createNameSpace - This creates new descriptor for a namespace
1063 /// with the specified parent scope.
1064 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1065                                        DIFile File, unsigned LineNo) {
1066   Value *Elts[] = {
1067     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1068     File.getFileNode(),
1069     getNonCompileUnitScope(Scope),
1070     MDString::get(VMContext, Name),
1071     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1072   };
1073   DINameSpace R(MDNode::get(VMContext, Elts));
1074   assert(R.Verify() &&
1075          "createNameSpace should return a verifiable DINameSpace");
1076   return R;
1077 }
1078
1079 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1080 /// an existing scope with a new filename.
1081 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1082                                                      DIFile File) {
1083   Value *Elts[] = {
1084     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1085     File.getFileNode(),
1086     Scope
1087   };
1088   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1089   assert(
1090       R.Verify() &&
1091       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1092   return R;
1093 }
1094
1095 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1096                                              unsigned Line, unsigned Col) {
1097   // Defeat MDNode uniqing for lexical blocks by using unique id.
1098   static unsigned int unique_id = 0;
1099   Value *Elts[] = {
1100     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1101     File.getFileNode(),
1102     getNonCompileUnitScope(Scope),
1103     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1104     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1105     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1106   };
1107   DILexicalBlock R(MDNode::get(VMContext, Elts));
1108   assert(R.Verify() &&
1109          "createLexicalBlock should return a verifiable DILexicalBlock");
1110   return R;
1111 }
1112
1113 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1114 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1115                                       Instruction *InsertBefore) {
1116   assert(Storage && "no storage passed to dbg.declare");
1117   assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1118   if (!DeclareFn)
1119     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1120
1121   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1122   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1123 }
1124
1125 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1126 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1127                                       BasicBlock *InsertAtEnd) {
1128   assert(Storage && "no storage passed to dbg.declare");
1129   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1130   if (!DeclareFn)
1131     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1132
1133   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1134
1135   // If this block already has a terminator then insert this intrinsic
1136   // before the terminator.
1137   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1138     return CallInst::Create(DeclareFn, Args, "", T);
1139   else
1140     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1141 }
1142
1143 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1144 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1145                                                 DIVariable VarInfo,
1146                                                 Instruction *InsertBefore) {
1147   assert(V && "no value passed to dbg.value");
1148   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1149   if (!ValueFn)
1150     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1151
1152   Value *Args[] = { MDNode::get(V->getContext(), V),
1153                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1154                     VarInfo };
1155   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1156 }
1157
1158 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1159 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1160                                                 DIVariable VarInfo,
1161                                                 BasicBlock *InsertAtEnd) {
1162   assert(V && "no value passed to dbg.value");
1163   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1164   if (!ValueFn)
1165     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1166
1167   Value *Args[] = { MDNode::get(V->getContext(), V),
1168                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1169                     VarInfo };
1170   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1171 }