Back out 60748 for now. It's breaking SPASS, 254.gap, and 464.h264ref.
[oota-llvm.git] / lib / Analysis / DebugInfo.cpp
1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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 helper classes used to build and interpret debug
11 // information in LLVM IR form.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/Support/Dwarf.h"
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 // DIDescriptor
27 //===----------------------------------------------------------------------===//
28
29 DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) {
30   GV = gv;
31   
32   // If this is non-null, check to see if the Tag matches.  If not, set to null.
33   if (GV && getTag() != RequiredTag)
34     GV = 0;
35 }
36
37
38 std::string DIDescriptor::getStringField(unsigned Elt) const {
39   if (GV == 0) return "";
40   Constant *C = GV->getInitializer();
41   if (C == 0 || Elt >= C->getNumOperands())
42     return "";
43   
44   std::string Result;
45   // Fills in the string if it succeeds
46   if (!GetConstantStringInfo(C->getOperand(Elt), Result))
47     Result.clear();
48   return Result;
49 }
50
51 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
52   if (GV == 0) return 0;
53   Constant *C = GV->getInitializer();
54   if (C == 0 || Elt >= C->getNumOperands())
55     return 0;
56   if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
57     return CI->getZExtValue();
58   return 0;
59 }
60
61
62 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
63   if (GV == 0) return DIDescriptor();
64   Constant *C = GV->getInitializer();
65   if (C == 0 || Elt >= C->getNumOperands())
66     return DIDescriptor();
67   C = C->getOperand(Elt);
68   return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
69 }
70
71 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
72   if (GV == 0) return 0;
73   Constant *C = GV->getInitializer();
74   if (C == 0 || Elt >= C->getNumOperands())
75     return 0;
76   C = C->getOperand(Elt);
77   
78   return dyn_cast<GlobalVariable>(C->stripPointerCasts());
79 }
80
81
82
83 //===----------------------------------------------------------------------===//
84 // Simple Descriptor Constructors and other Methods
85 //===----------------------------------------------------------------------===//
86
87 DIAnchor::DIAnchor(GlobalVariable *GV)
88   : DIDescriptor(GV, dwarf::DW_TAG_anchor) {}
89 DIEnumerator::DIEnumerator(GlobalVariable *GV)
90   : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
91 DISubrange::DISubrange(GlobalVariable *GV)
92   : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
93 DICompileUnit::DICompileUnit(GlobalVariable *GV)
94   : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
95 DIBasicType::DIBasicType(GlobalVariable *GV)
96   : DIType(GV, dwarf::DW_TAG_base_type) {}
97 DISubprogram::DISubprogram(GlobalVariable *GV)
98   : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
99 DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV)
100   : DIGlobal(GV, dwarf::DW_TAG_variable) {}
101 DIBlock::DIBlock(GlobalVariable *GV)
102   : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
103
104 /// isDerivedType - Return true if the specified tag is legal for
105 /// DIDerivedType.
106 bool DIDerivedType::isDerivedType(unsigned Tag) {
107   switch (Tag) {
108   case dwarf::DW_TAG_typedef:
109   case dwarf::DW_TAG_pointer_type:
110   case dwarf::DW_TAG_reference_type:
111   case dwarf::DW_TAG_const_type:
112   case dwarf::DW_TAG_volatile_type:
113   case dwarf::DW_TAG_restrict_type:
114   case dwarf::DW_TAG_member:
115   case dwarf::DW_TAG_inheritance:
116     return true;
117   default:
118     // FIXME: Even though it doesn't make sense, CompositeTypes are current
119     // modelled as DerivedTypes, this should return true for them as well.
120     return false;
121   }
122 }
123
124 DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) {
125   if (GV && !isDerivedType(getTag()))
126     GV = 0;
127 }
128
129 /// isCompositeType - Return true if the specified tag is legal for
130 /// DICompositeType.
131 bool DICompositeType::isCompositeType(unsigned TAG) {
132   switch (TAG) {
133   case dwarf::DW_TAG_array_type:
134   case dwarf::DW_TAG_structure_type:
135   case dwarf::DW_TAG_union_type:
136   case dwarf::DW_TAG_enumeration_type:
137   case dwarf::DW_TAG_vector_type:
138   case dwarf::DW_TAG_subroutine_type:
139     return true;
140   default:
141     return false;
142   }
143 }
144
145 DICompositeType::DICompositeType(GlobalVariable *GV)
146   : DIDerivedType(GV, true, true) {
147   if (GV && !isCompositeType(getTag()))
148     GV = 0;
149 }
150
151 /// isVariable - Return true if the specified tag is legal for DIVariable.
152 bool DIVariable::isVariable(unsigned Tag) {
153   switch (Tag) {
154   case dwarf::DW_TAG_auto_variable:
155   case dwarf::DW_TAG_arg_variable:
156   case dwarf::DW_TAG_return_variable:
157     return true;
158   default:
159     return false;
160   }
161 }
162
163 DIVariable::DIVariable(GlobalVariable *GV) : DIDescriptor(GV) {
164   if (GV && !isVariable(getTag()))
165     GV = 0;
166 }
167
168
169
170 //===----------------------------------------------------------------------===//
171 // DIFactory: Basic Helpers
172 //===----------------------------------------------------------------------===//
173
174 DIFactory::DIFactory(Module &m) : M(m) {
175   StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
176   EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
177 }
178
179 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
180 /// This is only valid when the descriptor is non-null.
181 Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
182   if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
183   return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
184 }
185
186 Constant *DIFactory::GetTagConstant(unsigned TAG) {
187   assert((TAG & DIDescriptor::VersionMask) == 0 &&
188          "Tag too large for debug encoding!");
189   return ConstantInt::get(Type::Int32Ty, TAG | DIDescriptor::Version6);
190 }
191
192 Constant *DIFactory::GetStringConstant(const std::string &String) {
193   // Check string cache for previous edition.
194   Constant *&Slot = StringCache[String];
195   
196   // Return Constant if previously defined.
197   if (Slot) return Slot;
198   
199   const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
200   
201   // If empty string then use a sbyte* null instead.
202   if (String.empty())
203     return Slot = ConstantPointerNull::get(DestTy);
204
205   // Construct string as an llvm constant.
206   Constant *ConstStr = ConstantArray::get(String);
207     
208   // Otherwise create and return a new string global.
209   GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
210                                              GlobalVariable::InternalLinkage,
211                                              ConstStr, ".str", &M);
212   StrGV->setSection("llvm.metadata");
213   return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
214 }
215
216 /// GetOrCreateAnchor - Look up an anchor for the specified tag and name.  If it
217 /// already exists, return it.  If not, create a new one and return it.
218 DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
219   const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
220   
221   // Otherwise, create the global or return it if already in the module.
222   Constant *C = M.getOrInsertGlobal(Name, EltTy);
223   assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
224   GlobalVariable *GV = cast<GlobalVariable>(C);
225   
226   // If it has an initializer, it is already in the module.
227   if (GV->hasInitializer()) 
228     return SubProgramAnchor = DIAnchor(GV);
229   
230   GV->setLinkage(GlobalValue::LinkOnceLinkage);
231   GV->setSection("llvm.metadata");
232   GV->setConstant(true);
233   M.addTypeName("llvm.dbg.anchor.type", EltTy);
234   
235   // Otherwise, set the initializer.
236   Constant *Elts[] = {
237     GetTagConstant(dwarf::DW_TAG_anchor),
238     ConstantInt::get(Type::Int32Ty, TAG)
239   };
240   
241   GV->setInitializer(ConstantStruct::get(Elts, 2));
242   return DIAnchor(GV);
243 }
244
245
246
247 //===----------------------------------------------------------------------===//
248 // DIFactory: Primary Constructors
249 //===----------------------------------------------------------------------===//
250
251 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
252 /// creating a new one if there isn't already one in the module.
253 DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
254   // If we already created one, just return it.
255   if (!CompileUnitAnchor.isNull())
256     return CompileUnitAnchor;
257   return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
258                                                "llvm.dbg.compile_units");
259 }
260
261 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
262 /// creating a new one if there isn't already one in the module.
263 DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
264   // If we already created one, just return it.
265   if (!SubProgramAnchor.isNull())
266     return SubProgramAnchor;
267   return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
268                                               "llvm.dbg.subprograms");
269 }
270
271 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
272 /// creating a new one if there isn't already one in the module.
273 DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
274   // If we already created one, just return it.
275   if (!GlobalVariableAnchor.isNull())
276     return GlobalVariableAnchor;
277   return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
278                                                   "llvm.dbg.global_variables");
279 }
280
281 /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
282 /// This implicitly uniques the arrays created.
283 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
284   SmallVector<Constant*, 16> Elts;
285   
286   for (unsigned i = 0; i != NumTys; ++i)
287     Elts.push_back(getCastToEmpty(Tys[i]));
288   
289   Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
290                                                      Elts.size()),
291                                       &Elts[0], Elts.size());
292   // If we already have this array, just return the uniqued version.
293   DIDescriptor &Entry = SimpleConstantCache[Init];
294   if (!Entry.isNull()) return DIArray(Entry.getGV());
295   
296   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
297                                           GlobalValue::InternalLinkage,
298                                           Init, "llvm.dbg.array", &M);
299   GV->setSection("llvm.metadata");
300   Entry = DIDescriptor(GV);
301   return DIArray(GV);
302 }
303
304 /// GetOrCreateSubrange - Create a descriptor for a value range.  This
305 /// implicitly uniques the values returned.
306 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
307   Constant *Elts[] = {
308     GetTagConstant(dwarf::DW_TAG_subrange_type),
309     ConstantInt::get(Type::Int64Ty, Lo),
310     ConstantInt::get(Type::Int64Ty, Hi)
311   };
312   
313   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
314
315   // If we already have this range, just return the uniqued version.
316   DIDescriptor &Entry = SimpleConstantCache[Init];
317   if (!Entry.isNull()) return DISubrange(Entry.getGV());
318   
319   M.addTypeName("llvm.dbg.subrange.type", Init->getType());
320
321   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
322                                           GlobalValue::InternalLinkage,
323                                           Init, "llvm.dbg.subrange", &M);
324   GV->setSection("llvm.metadata");
325   Entry = DIDescriptor(GV);
326   return DISubrange(GV);
327 }
328
329
330
331 /// CreateCompileUnit - Create a new descriptor for the specified compile
332 /// unit.  Note that this does not unique compile units within the module.
333 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
334                                            const std::string &Filename,
335                                            const std::string &Directory,
336                                            const std::string &Producer) {
337   Constant *Elts[] = {
338     GetTagConstant(dwarf::DW_TAG_compile_unit),
339     getCastToEmpty(GetOrCreateCompileUnitAnchor()),
340     ConstantInt::get(Type::Int32Ty, LangID),
341     GetStringConstant(Filename),
342     GetStringConstant(Directory),
343     GetStringConstant(Producer)
344   };
345   
346   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
347   
348   M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
349   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
350                                           GlobalValue::InternalLinkage,
351                                           Init, "llvm.dbg.compile_unit", &M);
352   GV->setSection("llvm.metadata");
353   return DICompileUnit(GV);
354 }
355
356 /// CreateEnumerator - Create a single enumerator value.
357 DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
358   Constant *Elts[] = {
359     GetTagConstant(dwarf::DW_TAG_enumerator),
360     GetStringConstant(Name),
361     ConstantInt::get(Type::Int64Ty, Val)
362   };
363   
364   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
365   
366   M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
367   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
368                                           GlobalValue::InternalLinkage,
369                                           Init, "llvm.dbg.enumerator", &M);
370   GV->setSection("llvm.metadata");
371   return DIEnumerator(GV);
372 }
373
374
375 /// CreateBasicType - Create a basic type like int, float, etc.
376 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
377                                       const std::string &Name,
378                                        DICompileUnit CompileUnit,
379                                        unsigned LineNumber,
380                                        uint64_t SizeInBits,
381                                        uint64_t AlignInBits,
382                                        uint64_t OffsetInBits, unsigned Flags,
383                                        unsigned Encoding) {
384   Constant *Elts[] = {
385     GetTagConstant(dwarf::DW_TAG_base_type),
386     getCastToEmpty(Context),
387     GetStringConstant(Name),
388     getCastToEmpty(CompileUnit),
389     ConstantInt::get(Type::Int32Ty, LineNumber),
390     ConstantInt::get(Type::Int64Ty, SizeInBits),
391     ConstantInt::get(Type::Int64Ty, AlignInBits),
392     ConstantInt::get(Type::Int64Ty, OffsetInBits),
393     ConstantInt::get(Type::Int32Ty, Flags),
394     ConstantInt::get(Type::Int32Ty, Encoding)
395   };
396   
397   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
398   
399   M.addTypeName("llvm.dbg.basictype.type", Init->getType());
400   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
401                                           GlobalValue::InternalLinkage,
402                                           Init, "llvm.dbg.basictype", &M);
403   GV->setSection("llvm.metadata");
404   return DIBasicType(GV);
405 }
406
407 /// CreateDerivedType - Create a derived type like const qualified type,
408 /// pointer, typedef, etc.
409 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
410                                            DIDescriptor Context,
411                                            const std::string &Name,
412                                            DICompileUnit CompileUnit,
413                                            unsigned LineNumber,
414                                            uint64_t SizeInBits,
415                                            uint64_t AlignInBits,
416                                            uint64_t OffsetInBits,
417                                            unsigned Flags,
418                                            DIType DerivedFrom) {
419   Constant *Elts[] = {
420     GetTagConstant(Tag),
421     getCastToEmpty(Context),
422     GetStringConstant(Name),
423     getCastToEmpty(CompileUnit),
424     ConstantInt::get(Type::Int32Ty, LineNumber),
425     ConstantInt::get(Type::Int64Ty, SizeInBits),
426     ConstantInt::get(Type::Int64Ty, AlignInBits),
427     ConstantInt::get(Type::Int64Ty, OffsetInBits),
428     ConstantInt::get(Type::Int32Ty, Flags),
429     getCastToEmpty(DerivedFrom)
430   };
431   
432   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
433   
434   M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
435   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
436                                           GlobalValue::InternalLinkage,
437                                           Init, "llvm.dbg.derivedtype", &M);
438   GV->setSection("llvm.metadata");
439   return DIDerivedType(GV);
440 }
441
442 /// CreateCompositeType - Create a composite type like array, struct, etc.
443 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
444                                                DIDescriptor Context,
445                                                const std::string &Name,
446                                                DICompileUnit CompileUnit,
447                                                unsigned LineNumber,
448                                                uint64_t SizeInBits,
449                                                uint64_t AlignInBits,
450                                                uint64_t OffsetInBits,
451                                                unsigned Flags,
452                                                DIType DerivedFrom,
453                                                DIArray Elements) {
454   Constant *Elts[] = {
455     GetTagConstant(Tag),
456     getCastToEmpty(Context),
457     GetStringConstant(Name),
458     getCastToEmpty(CompileUnit),
459     ConstantInt::get(Type::Int32Ty, LineNumber),
460     ConstantInt::get(Type::Int64Ty, SizeInBits),
461     ConstantInt::get(Type::Int64Ty, AlignInBits),
462     ConstantInt::get(Type::Int64Ty, OffsetInBits),
463     ConstantInt::get(Type::Int32Ty, Flags),
464     getCastToEmpty(DerivedFrom),
465     getCastToEmpty(Elements)
466   };
467   
468   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
469   
470   M.addTypeName("llvm.dbg.composite.type", Init->getType());
471   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
472                                           GlobalValue::InternalLinkage,
473                                           Init, "llvm.dbg.composite", &M);
474   GV->setSection("llvm.metadata");
475   return DICompositeType(GV);
476 }
477
478
479 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
480 /// See comments in DISubprogram for descriptions of these fields.  This
481 /// method does not unique the generated descriptors.
482 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context, 
483                                          const std::string &Name,
484                                          const std::string &DisplayName,
485                                          const std::string &LinkageName,
486                                          DICompileUnit CompileUnit,
487                                          unsigned LineNo, DIType Type,
488                                          bool isLocalToUnit,
489                                          bool isDefinition) {
490   Constant *Elts[] = {
491     GetTagConstant(dwarf::DW_TAG_subprogram),
492     getCastToEmpty(GetOrCreateSubprogramAnchor()),
493     getCastToEmpty(Context),
494     GetStringConstant(Name),
495     GetStringConstant(DisplayName),
496     GetStringConstant(LinkageName),
497     getCastToEmpty(CompileUnit),
498     ConstantInt::get(Type::Int32Ty, LineNo),
499     getCastToEmpty(Type),
500     ConstantInt::get(Type::Int1Ty, isLocalToUnit),
501     ConstantInt::get(Type::Int1Ty, isDefinition)
502   };
503   
504   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
505   
506   M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
507   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
508                                           GlobalValue::InternalLinkage,
509                                           Init, "llvm.dbg.subprogram", &M);
510   GV->setSection("llvm.metadata");
511   return DISubprogram(GV);
512 }
513
514 /// CreateGlobalVariable - Create a new descriptor for the specified global.
515 DIGlobalVariable
516 DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
517                                 const std::string &DisplayName,
518                                 const std::string &LinkageName,
519                                 DICompileUnit CompileUnit,
520                                 unsigned LineNo, DIType Type,bool isLocalToUnit,
521                                 bool isDefinition, llvm::GlobalVariable *Val) {
522   
523   Constant *Elts[] = {
524     GetTagConstant(dwarf::DW_TAG_variable),
525     getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
526     getCastToEmpty(Context),
527     GetStringConstant(Name),
528     GetStringConstant(DisplayName),
529     GetStringConstant(LinkageName),
530     getCastToEmpty(CompileUnit),
531     ConstantInt::get(Type::Int32Ty, LineNo),
532     getCastToEmpty(Type),
533     ConstantInt::get(Type::Int1Ty, isLocalToUnit),
534     ConstantInt::get(Type::Int1Ty, isDefinition),
535     ConstantExpr::getBitCast(Val, EmptyStructPtr)
536   };
537   
538   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
539   
540   M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
541   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
542                                           GlobalValue::InternalLinkage,
543                                           Init, "llvm.dbg.global_variable", &M);
544   GV->setSection("llvm.metadata");
545   return DIGlobalVariable(GV);
546 }
547
548
549 /// CreateVariable - Create a new descriptor for the specified variable.
550 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
551                                      const std::string &Name,
552                                      DICompileUnit CompileUnit, unsigned LineNo,
553                                      DIType Type) {
554   
555   Constant *Elts[] = {
556     GetTagConstant(Tag),
557     getCastToEmpty(Context),
558     GetStringConstant(Name),
559     getCastToEmpty(CompileUnit),
560     ConstantInt::get(Type::Int32Ty, LineNo),
561     getCastToEmpty(Type),
562   };
563   
564   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
565   
566   M.addTypeName("llvm.dbg.variable.type", Init->getType());
567   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
568                                           GlobalValue::InternalLinkage,
569                                           Init, "llvm.dbg.variable", &M);
570   GV->setSection("llvm.metadata");
571   return DIVariable(GV);
572 }
573
574
575 /// CreateBlock - This creates a descriptor for a lexical block with the
576 /// specified parent context.
577 DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
578   Constant *Elts[] = {
579     GetTagConstant(dwarf::DW_TAG_lexical_block),
580     getCastToEmpty(Context)
581   };
582   
583   Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
584   
585   M.addTypeName("llvm.dbg.block.type", Init->getType());
586   GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
587                                           GlobalValue::InternalLinkage,
588                                           Init, "llvm.dbg.block", &M);
589   GV->setSection("llvm.metadata");
590   return DIBlock(GV);
591 }
592
593
594 //===----------------------------------------------------------------------===//
595 // DIFactory: Routines for inserting code into a function
596 //===----------------------------------------------------------------------===//
597
598 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
599 /// inserting it at the end of the specified basic block.
600 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
601                                 unsigned ColNo, BasicBlock *BB) {
602   
603   // Lazily construct llvm.dbg.stoppoint function.
604   if (!StopPointFn)
605     StopPointFn = llvm::Intrinsic::getDeclaration(&M, 
606                                               llvm::Intrinsic::dbg_stoppoint);
607   
608   // Invoke llvm.dbg.stoppoint
609   Value *Args[] = {
610     llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
611     llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
612     getCastToEmpty(CU)
613   };
614   CallInst::Create(StopPointFn, Args, Args+3, "", BB);
615 }
616
617 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
618 /// mark the start of the specified subprogram.
619 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
620   // Lazily construct llvm.dbg.func.start.
621   if (!FuncStartFn)
622     FuncStartFn = llvm::Intrinsic::getDeclaration(&M, 
623                                               llvm::Intrinsic::dbg_func_start);
624   
625   // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
626   CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
627 }
628
629 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
630 /// mark the start of a region for the specified scoping descriptor.
631 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
632   // Lazily construct llvm.dbg.region.start function.
633   if (!RegionStartFn)
634     RegionStartFn = llvm::Intrinsic::getDeclaration(&M, 
635                                             llvm::Intrinsic::dbg_region_start);
636   // Call llvm.dbg.func.start.
637   CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
638 }
639
640
641 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
642 /// mark the end of a region for the specified scoping descriptor.
643 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
644   // Lazily construct llvm.dbg.region.end function.
645   if (!RegionEndFn)
646     RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
647                                                llvm::Intrinsic::dbg_region_end);
648   
649   CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
650 }
651
652 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
653 void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
654                               BasicBlock *BB) {
655   // Cast the storage to a {}* for the call to llvm.dbg.declare.
656   Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
657   
658   if (!DeclareFn)
659     DeclareFn = llvm::Intrinsic::getDeclaration(&M,
660                                                 llvm::Intrinsic::dbg_declare);
661   Value *Args[] = { Storage, getCastToEmpty(D) };
662   CallInst::Create(DeclareFn, Args, Args+2, "", BB);
663 }