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