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