Add support to create vector, array, enums etc...
[oota-llvm.git] / lib / Analysis / 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/Analysis/DIBuilder.h"
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/IntrinsicInst.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Dwarf.h"
21
22 using namespace llvm;
23 using namespace llvm::dwarf;
24
25 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
26   assert((Tag & LLVMDebugVersionMask) == 0 &&
27          "Tag too large for debug encoding!");
28   return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
29 }
30
31 DIBuilder::DIBuilder(Module &m)
32   : M(m), VMContext(M.getContext()), TheCU(0), DeclareFn(0), ValueFn(0) {}
33
34 /// CreateCompileUnit - A CompileUnit provides an anchor for all debugging
35 /// information generated during this instance of compilation.
36 void DIBuilder::CreateCompileUnit(unsigned Lang, StringRef Filename, 
37                                   StringRef Directory, StringRef Producer, 
38                                   bool isOptimized, StringRef Flags, 
39                                   unsigned RunTimeVer) {
40   Value *Elts[] = {
41     GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
42     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
43     ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
44     MDString::get(VMContext, Filename),
45     MDString::get(VMContext, Directory),
46     MDString::get(VMContext, Producer),
47     // Deprecate isMain field.
48     ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
49     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
50     MDString::get(VMContext, Flags),
51     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
52   };
53   TheCU = DICompileUnit(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
54 }
55
56 /// CreateFile - Create a file descriptor to hold debugging information
57 /// for a file.
58 DIFile DIBuilder::CreateFile(StringRef Filename, StringRef Directory) {
59   assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
60   Value *Elts[] = {
61     GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
62     MDString::get(VMContext, Filename),
63     MDString::get(VMContext, Directory),
64     TheCU
65   };
66   return DIFile(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
67 }
68
69 /// CreateEnumerator - Create a single enumerator value.
70 DIEnumerator DIBuilder::CreateEnumerator(StringRef Name, uint64_t Val) {
71   Value *Elts[] = {
72     GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
73     MDString::get(VMContext, Name),
74     ConstantInt::get(Type::getInt64Ty(VMContext), Val)
75   };
76   return DIEnumerator(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
77 }
78
79 /// CreateBasicType - Create debugging information entry for a basic 
80 /// type, e.g 'char'.
81 DIType DIBuilder::CreateBasicType(StringRef Name, uint64_t SizeInBits, 
82                                   uint64_t AlignInBits,
83                                   unsigned Encoding) {
84   // Basic types are encoded in DIBasicType format. Line number, filename,
85   // offset and flags are always empty here.
86   Value *Elts[] = {
87     GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
88     TheCU,
89     MDString::get(VMContext, Name),
90     NULL, // Filename
91     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
92     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
93     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
94     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
95     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
96     ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
97   };
98   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
99 }
100
101 /// CreateQaulifiedType - Create debugging information entry for a qualified
102 /// type, e.g. 'const int'.
103 DIType DIBuilder::CreateQualifiedType(unsigned Tag, DIType FromTy) {
104   // Qualified types are encoded in DIDerivedType format.
105   Value *Elts[] = {
106     GetTagConstant(VMContext, Tag),
107     TheCU,
108     MDString::get(VMContext, StringRef()), // Empty name.
109     NULL, // Filename
110     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
111     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
112     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
113     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
114     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
115     FromTy
116   };
117   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
118 }
119
120 /// CreatePointerType - Create debugging information entry for a pointer.
121 DIType DIBuilder::CreatePointerType(DIType PointeeTy, uint64_t SizeInBits,
122                                     uint64_t AlignInBits, StringRef Name) {
123   // Pointer types are encoded in DIDerivedType format.
124   Value *Elts[] = {
125     GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
126     TheCU,
127     MDString::get(VMContext, Name),
128     NULL, // Filename
129     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
130     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
131     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
132     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
133     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
134     PointeeTy
135   };
136   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
137 }
138
139 /// CreateReferenceType - Create debugging information entry for a reference.
140 DIType DIBuilder::CreateReferenceType(DIType RTy) {
141   // References are encoded in DIDerivedType format.
142   Value *Elts[] = {
143     GetTagConstant(VMContext, dwarf::DW_TAG_reference_type),
144     TheCU,
145     NULL, // Name
146     NULL, // Filename
147     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
148     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
149     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
150     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
151     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
152     RTy
153   };
154   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
155 }
156
157 /// CreateTypedef - Create debugging information entry for a typedef.
158 DIType DIBuilder::CreateTypedef(DIType Ty, StringRef Name, DIFile File,
159                                 unsigned LineNo) {
160   // typedefs are encoded in DIDerivedType format.
161   assert(Ty.Verify() && "Invalid typedef type!");
162   Value *Elts[] = {
163     GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
164     Ty.getContext(),
165     MDString::get(VMContext, Name),
166     File,
167     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
168     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
169     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
170     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
171     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
172     Ty
173   };
174   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
175 }
176
177 /// CreateFriend - Create debugging information entry for a 'friend'.
178 DIType DIBuilder::CreateFriend(DIType Ty, DIType FriendTy) {
179   // typedefs are encoded in DIDerivedType format.
180   assert(Ty.Verify() && "Invalid type!");
181   assert(FriendTy.Verify() && "Invalid friend type!");
182   Value *Elts[] = {
183     GetTagConstant(VMContext, dwarf::DW_TAG_friend),
184     Ty,
185     NULL, // Name
186     Ty.getFile(),
187     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
188     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
189     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
190     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
191     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
192     FriendTy
193   };
194   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
195 }
196
197 /// CreateInheritance - Create debugging information entry to establish
198 /// inheritnace relationship between two types.
199 DIType DIBuilder::CreateInheritance(DIType Ty, DIType BaseTy, 
200                                     uint64_t BaseOffset, unsigned Flags) {
201   // TAG_inheritance is encoded in DIDerivedType format.
202   Value *Elts[] = {
203     GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
204     Ty,
205     NULL, // Name
206     NULL, // File
207     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
208     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
209     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
210     ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
211     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
212     BaseTy
213   };
214   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
215 }
216
217 /// CreateMemberType - Create debugging information entry for a member.
218 DIType DIBuilder::CreateMemberType(StringRef Name, 
219                                    DIFile File, unsigned LineNumber, 
220                                    uint64_t SizeInBits, uint64_t AlignInBits,
221                                    uint64_t OffsetInBits, unsigned Flags, 
222                                    DIType Ty) {
223   // TAG_member is encoded in DIDerivedType format.
224   Value *Elts[] = {
225     GetTagConstant(VMContext, dwarf::DW_TAG_member),
226     File, // Or TheCU ? Ty ?
227     MDString::get(VMContext, Name),
228     File,
229     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
230     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
231     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
232     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
233     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
234     Ty
235   };
236   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
237 }
238
239 /// CreateStructType - Create debugging information entry for a struct.
240 DIType DIBuilder::CreateStructType(DIDescriptor Context, StringRef Name, 
241                                    DIFile File, unsigned LineNumber, 
242                                    uint64_t SizeInBits, uint64_t AlignInBits,
243                                    unsigned Flags, DIArray Elements, 
244                                    unsigned RunTimeLang) {
245  // TAG_structure_type is encoded in DICompositeType format.
246   Value *Elts[] = {
247     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
248     Context,
249     MDString::get(VMContext, Name),
250     File,
251     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
252     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
253     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
254     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
255     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
256     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
257     Elements,
258     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
259     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
260   };
261   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
262 }
263
264 /// CreateUnionType - Create debugging information entry for an union.
265 DIType DIBuilder::CreateUnionType(DIDescriptor Scope, StringRef Name, 
266                                   DIFile File,
267                                   unsigned LineNumber, uint64_t SizeInBits,
268                                   uint64_t AlignInBits, unsigned Flags,
269                                   DIArray Elements, unsigned RunTimeLang) {
270   // TAG_union_type is encoded in DICompositeType format.
271   Value *Elts[] = {
272     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
273     Scope,
274     MDString::get(VMContext, Name),
275     File,
276     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
277     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
278     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
279     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
280     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
281     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
282     Elements,
283     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
284     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
285   };
286   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
287 }
288
289 /// CreateSubroutineType - Create subroutine type.
290 DIType DIBuilder::CreateSubroutineType(DIFile File, DIArray ParameterTypes) {
291   // TAG_subroutine_type is encoded in DICompositeType format.
292   Value *Elts[] = {
293     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
294     File,
295     MDString::get(VMContext, ""),
296     File,
297     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
298     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
299     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
300     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
301     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
302     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
303     ParameterTypes,
304     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
305     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
306   };
307   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
308 }
309
310 /// CreateEnumerationType - Create debugging information entry for an 
311 /// enumeration.
312 DIType DIBuilder::CreateEnumerationType(DIDescriptor Scope, StringRef Name, 
313                                         DIFile File, unsigned LineNumber, 
314                                         uint64_t SizeInBits, 
315                                         uint64_t AlignInBits, DIArray Elements) {
316   // TAG_enumeration_type is encoded in DICompositeType format.
317   Value *Elts[] = {
318     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
319     Scope,
320     MDString::get(VMContext, Name),
321     File,
322     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
323     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
324     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
325     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
326     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
327     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
328     Elements,
329     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
330     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
331   };
332   MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
333   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
334   NMD->addOperand(Node);
335   return DIType(Node);
336 }
337
338 /// CreateArrayType - Create debugging information entry for an array.
339 DIType DIBuilder::CreateArrayType(uint64_t Size, uint64_t AlignInBits, 
340                                   DIType Ty, DIArray Subscripts) {
341   // TAG_array_type is encoded in DICompositeType format.
342   Value *Elts[] = {
343     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
344     TheCU,
345     MDString::get(VMContext, ""),
346     TheCU,
347     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
348     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
349     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
350     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
351     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
352     Ty,
353     Subscripts,
354     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
355     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
356   };
357   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
358 }
359
360 /// CreateVectorType - Create debugging information entry for a vector.
361 DIType DIBuilder::CreateVectorType(uint64_t Size, uint64_t AlignInBits, 
362                                    DIType Ty, DIArray Subscripts) {
363   // TAG_vector_type is encoded in DICompositeType format.
364   Value *Elts[] = {
365     GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
366     TheCU,
367     MDString::get(VMContext, ""),
368     TheCU,
369     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
370     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
371     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
372     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
373     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
374     Ty,
375     Subscripts,
376     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
377     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
378   };
379   return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
380 }
381
382 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
383 DIType DIBuilder::CreateArtificialType(DIType Ty) {
384   if (Ty.isArtificial())
385     return Ty;
386
387   SmallVector<Value *, 9> Elts;
388   MDNode *N = Ty;
389   assert (N && "Unexpected input DIType!");
390   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
391     if (Value *V = N->getOperand(i))
392       Elts.push_back(V);
393     else
394       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
395   }
396
397   unsigned CurFlags = Ty.getFlags();
398   CurFlags = CurFlags | DIType::FlagArtificial;
399
400   // Flags are stored at this slot.
401   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
402
403   return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
404 }
405
406 /// RetainType - Retain DIType in a module even if it is not referenced 
407 /// through debug info anchors.
408 void DIBuilder::RetainType(DIType T) {
409   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
410   NMD->addOperand(T);
411 }
412
413 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
414 /// for the subroutine type.
415 DIDescriptor DIBuilder::CreateUnspecifiedParameter() {
416   Value *Elts[] = { 
417     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters) 
418   };
419   return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
420 }
421
422 /// CreateTemporaryType - Create a temporary forward-declared type.
423 DIType DIBuilder::CreateTemporaryType() {
424   // Give the temporary MDNode a tag. It doesn't matter what tag we
425   // use here as long as DIType accepts it.
426   Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
427   MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
428   return DIType(Node);
429 }
430
431 /// CreateTemporaryType - Create a temporary forward-declared type.
432 DIType DIBuilder::CreateTemporaryType(DIFile F) {
433   // Give the temporary MDNode a tag. It doesn't matter what tag we
434   // use here as long as DIType accepts it.
435   Value *Elts[] = {
436     GetTagConstant(VMContext, DW_TAG_base_type),
437     F.getCompileUnit(),
438     NULL,
439     F
440   };
441   MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
442   return DIType(Node);
443 }
444
445 /// GetOrCreateArray - Get a DIArray, create one if required.
446 DIArray DIBuilder::GetOrCreateArray(Value *const *Elements, unsigned NumElements) {
447   if (NumElements == 0) {
448     Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
449     return DIArray(MDNode::get(VMContext, &Null, 1));
450   }
451   return DIArray(MDNode::get(VMContext, Elements, NumElements));
452 }
453
454 /// GetOrCreateSubrange - Create a descriptor for a value range.  This
455 /// implicitly uniques the values returned.
456 DISubrange DIBuilder::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
457   Value *Elts[] = {
458     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
459     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
460     ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
461   };
462
463   return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
464 }
465
466 /// CreateGlobalVariable - Create a new descriptor for the specified global.
467 DIGlobalVariable DIBuilder::
468 CreateGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber, 
469                      DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
470   Value *Elts[] = {
471     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
472     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
473     TheCU,
474     MDString::get(VMContext, Name),
475     MDString::get(VMContext, Name),
476     MDString::get(VMContext, Name),
477     F,
478     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
479     Ty,
480     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
481     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
482     Val
483   };
484   MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
485   // Create a named metadata so that we do not lose this mdnode.
486   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
487   NMD->addOperand(Node);
488   return DIGlobalVariable(Node);
489 }
490
491 /// CreateStaticVariable - Create a new descriptor for the specified static
492 /// variable.
493 DIGlobalVariable DIBuilder::
494 CreateStaticVariable(DIDescriptor Context, StringRef Name, 
495                      StringRef LinkageName, DIFile F, unsigned LineNumber, 
496                      DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
497   Value *Elts[] = {
498     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
499     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
500     Context,
501     MDString::get(VMContext, Name),
502     MDString::get(VMContext, Name),
503     MDString::get(VMContext, LinkageName),
504     F,
505     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
506     Ty,
507     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
508     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
509     Val
510   };
511   MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
512   // Create a named metadata so that we do not lose this mdnode.
513   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
514   NMD->addOperand(Node);
515   return DIGlobalVariable(Node);
516 }
517
518 /// CreateVariable - Create a new descriptor for the specified variable.
519 DIVariable DIBuilder::CreateLocalVariable(unsigned Tag, DIDescriptor Scope,
520                                           StringRef Name, DIFile File,
521                                           unsigned LineNo, DIType Ty, 
522                                           bool AlwaysPreserve, unsigned Flags) {
523   Value *Elts[] = {
524     GetTagConstant(VMContext, Tag),
525     Scope,
526     MDString::get(VMContext, Name),
527     File,
528     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
529     Ty,
530     ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
531   };
532   MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
533   if (AlwaysPreserve) {
534     // The optimizer may remove local variable. If there is an interest
535     // to preserve variable info in such situation then stash it in a
536     // named mdnode.
537     DISubprogram Fn(getDISubprogram(Scope));
538     StringRef FName = "fn";
539     if (Fn.getFunction())
540       FName = Fn.getFunction()->getName();
541     char One = '\1';
542     if (FName.startswith(StringRef(&One, 1)))
543       FName = FName.substr(1);
544     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
545     FnLocals->addOperand(Node);
546   }
547   return DIVariable(Node);
548 }
549
550 /// CreateComplexVariable - Create a new descriptor for the specified variable
551 /// which has a complex address expression for its address.
552 DIVariable DIBuilder::CreateComplexVariable(unsigned Tag, DIDescriptor Scope,
553                                             StringRef Name, DIFile F,
554                                             unsigned LineNo,
555                                             DIType Ty, Value *const *Addr,
556                                             unsigned NumAddr) {
557   SmallVector<Value *, 15> Elts;
558   Elts.push_back(GetTagConstant(VMContext, Tag));
559   Elts.push_back(Scope);
560   Elts.push_back(MDString::get(VMContext, Name));
561   Elts.push_back(F);
562   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
563   Elts.push_back(Ty);
564   Elts.append(Addr, Addr+NumAddr);
565
566   return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
567 }
568
569 /// CreateNameSpace - This creates new descriptor for a namespace
570 /// with the specified parent scope.
571 DINameSpace DIBuilder::CreateNameSpace(DIDescriptor Scope, StringRef Name,
572                                        DIFile File, unsigned LineNo) {
573   Value *Elts[] = {
574     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
575     Scope,
576     MDString::get(VMContext, Name),
577     File,
578     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
579   };
580   return DINameSpace(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
581 }
582
583 DILexicalBlock DIBuilder::CreateLexicalBlock(DIDescriptor Scope, DIFile File,
584                                              unsigned Line, unsigned Col) {
585   // Defeat MDNode uniqing for lexical blocks by using unique id.
586   static unsigned int unique_id = 0;
587   Value *Elts[] = {
588     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
589     Scope,
590     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
591     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
592     File,
593     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
594   };
595   return DILexicalBlock(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
596 }
597
598 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
599 Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo,
600                                       Instruction *InsertBefore) {
601   assert(Storage && "no storage passed to dbg.declare");
602   assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
603   if (!DeclareFn)
604     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
605
606   Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
607   return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
608 }
609
610 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
611 Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo,
612                                       BasicBlock *InsertAtEnd) {
613   assert(Storage && "no storage passed to dbg.declare");
614   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
615   if (!DeclareFn)
616     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
617
618   Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
619
620   // If this block already has a terminator then insert this intrinsic
621   // before the terminator.
622   if (TerminatorInst *T = InsertAtEnd->getTerminator())
623     return CallInst::Create(DeclareFn, Args, Args+2, "", T);
624   else
625     return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
626 }
627
628 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
629 Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
630                                                 DIVariable VarInfo,
631                                                 Instruction *InsertBefore) {
632   assert(V && "no value passed to dbg.value");
633   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
634   if (!ValueFn)
635     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
636
637   Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
638                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
639                     VarInfo };
640   return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
641 }
642
643 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
644 Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
645                                                 DIVariable VarInfo,
646                                                 BasicBlock *InsertAtEnd) {
647   assert(V && "no value passed to dbg.value");
648   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
649   if (!ValueFn)
650     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
651
652   Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
653                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
654                     VarInfo };
655   return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
656 }
657