LLVM API Documentation

DebugInfo.cpp

Go to the documentation of this file.
00001 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the helper classes used to build and interpret debug
00011 // information in LLVM IR form.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Analysis/DebugInfo.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/DerivedTypes.h"
00018 #include "llvm/Intrinsics.h"
00019 #include "llvm/IntrinsicInst.h"
00020 #include "llvm/Instructions.h"
00021 #include "llvm/LLVMContext.h"
00022 #include "llvm/Module.h"
00023 #include "llvm/Analysis/ValueTracking.h"
00024 #include "llvm/Support/Dwarf.h"
00025 #include "llvm/Support/DebugLoc.h"
00026 #include "llvm/Support/Streams.h"
00027 
00028 using namespace llvm;
00029 using namespace llvm::dwarf;
00030 
00031 //===----------------------------------------------------------------------===//
00032 // DIDescriptor
00033 //===----------------------------------------------------------------------===//
00034 
00035 /// ValidDebugInfo - Return true if V represents valid debug info value.
00036 bool DIDescriptor::ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel) {
00037   if (!V)
00038     return false;
00039 
00040   GlobalVariable *GV = dyn_cast<GlobalVariable>(V->stripPointerCasts());
00041   if (!GV)
00042     return false;
00043 
00044   if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage())
00045     return false;
00046 
00047   DIDescriptor DI(GV);
00048 
00049   // Check current version. Allow Version6 for now.
00050   unsigned Version = DI.getVersion();
00051   if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
00052     return false;
00053 
00054   unsigned Tag = DI.getTag();
00055   switch (Tag) {
00056   case DW_TAG_variable:
00057     assert(DIVariable(GV).Verify() && "Invalid DebugInfo value");
00058     break;
00059   case DW_TAG_compile_unit:
00060     assert(DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
00061     break;
00062   case DW_TAG_subprogram:
00063     assert(DISubprogram(GV).Verify() && "Invalid DebugInfo value");
00064     break;
00065   case DW_TAG_lexical_block:
00066     // FIXME: This interfers with the quality of generated code during
00067     // optimization.
00068     if (OptLevel != CodeGenOpt::None)
00069       return false;
00070     // FALLTHROUGH
00071   default:
00072     break;
00073   }
00074 
00075   return true;
00076 }
00077 
00078 DIDescriptor::DIDescriptor(GlobalVariable *GV, unsigned RequiredTag) {
00079   DbgGV = GV;
00080   
00081   // If this is non-null, check to see if the Tag matches. If not, set to null.
00082   if (GV && getTag() != RequiredTag)
00083     DbgGV = 0;
00084 }
00085 
00086 const std::string &
00087 DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
00088   if (DbgGV == 0) {
00089     Result.clear();
00090     return Result;
00091   }
00092 
00093   Constant *C = DbgGV->getInitializer();
00094   if (C == 0 || Elt >= C->getNumOperands()) {
00095     Result.clear();
00096     return Result;
00097   }
00098 
00099   // Fills in the string if it succeeds
00100   if (!GetConstantStringInfo(C->getOperand(Elt), Result))
00101     Result.clear();
00102 
00103   return Result;
00104 }
00105 
00106 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
00107   if (DbgGV == 0) return 0;
00108   if (!DbgGV->hasInitializer()) return 0;
00109 
00110   Constant *C = DbgGV->getInitializer();
00111   if (C == 0 || Elt >= C->getNumOperands())
00112     return 0;
00113 
00114   if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
00115     return CI->getZExtValue();
00116   return 0;
00117 }
00118 
00119 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
00120   if (DbgGV == 0) return DIDescriptor();
00121 
00122   Constant *C = DbgGV->getInitializer();
00123   if (C == 0 || Elt >= C->getNumOperands())
00124     return DIDescriptor();
00125 
00126   C = C->getOperand(Elt);
00127   return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
00128 }
00129 
00130 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
00131   if (DbgGV == 0) return 0;
00132 
00133   Constant *C = DbgGV->getInitializer();
00134   if (C == 0 || Elt >= C->getNumOperands())
00135     return 0;
00136 
00137   C = C->getOperand(Elt);
00138   return dyn_cast<GlobalVariable>(C->stripPointerCasts());
00139 }
00140 
00141 //===----------------------------------------------------------------------===//
00142 // Simple Descriptor Constructors and other Methods
00143 //===----------------------------------------------------------------------===//
00144 
00145 // Needed by DIVariable::getType().
00146 DIType::DIType(GlobalVariable *GV) : DIDescriptor(GV) {
00147   if (!GV) return;
00148   unsigned tag = getTag();
00149   if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
00150       !DICompositeType::isCompositeType(tag))
00151     DbgGV = 0;
00152 }
00153 
00154 /// isDerivedType - Return true if the specified tag is legal for
00155 /// DIDerivedType.
00156 bool DIType::isDerivedType(unsigned Tag) {
00157   switch (Tag) {
00158   case dwarf::DW_TAG_typedef:
00159   case dwarf::DW_TAG_pointer_type:
00160   case dwarf::DW_TAG_reference_type:
00161   case dwarf::DW_TAG_const_type:
00162   case dwarf::DW_TAG_volatile_type:
00163   case dwarf::DW_TAG_restrict_type:
00164   case dwarf::DW_TAG_member:
00165   case dwarf::DW_TAG_inheritance:
00166     return true;
00167   default:
00168     // FIXME: Even though it doesn't make sense, CompositeTypes are current
00169     // modelled as DerivedTypes, this should return true for them as well.
00170     return false;
00171   }
00172 }
00173 
00174 /// isCompositeType - Return true if the specified tag is legal for
00175 /// DICompositeType.
00176 bool DIType::isCompositeType(unsigned TAG) {
00177   switch (TAG) {
00178   case dwarf::DW_TAG_array_type:
00179   case dwarf::DW_TAG_structure_type:
00180   case dwarf::DW_TAG_union_type:
00181   case dwarf::DW_TAG_enumeration_type:
00182   case dwarf::DW_TAG_vector_type:
00183   case dwarf::DW_TAG_subroutine_type:
00184   case dwarf::DW_TAG_class_type:
00185     return true;
00186   default:
00187     return false;
00188   }
00189 }
00190 
00191 /// isVariable - Return true if the specified tag is legal for DIVariable.
00192 bool DIVariable::isVariable(unsigned Tag) {
00193   switch (Tag) {
00194   case dwarf::DW_TAG_auto_variable:
00195   case dwarf::DW_TAG_arg_variable:
00196   case dwarf::DW_TAG_return_variable:
00197     return true;
00198   default:
00199     return false;
00200   }
00201 }
00202 
00203 unsigned DIArray::getNumElements() const {
00204   assert (DbgGV && "Invalid DIArray");
00205   Constant *C = DbgGV->getInitializer();
00206   assert (C && "Invalid DIArray initializer");
00207   return C->getNumOperands();
00208 }
00209 
00210 /// replaceAllUsesWith - Replace all uses of debug info referenced by
00211 /// this descriptor. After this completes, the current debug info value
00212 /// is erased.
00213 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
00214   if (isNull())
00215     return;
00216 
00217   assert (!D.isNull() && "Can not replace with null");
00218   getGV()->replaceAllUsesWith(D.getGV());
00219   getGV()->eraseFromParent();
00220 }
00221 
00222 /// Verify - Verify that a compile unit is well formed.
00223 bool DICompileUnit::Verify() const {
00224   if (isNull()) 
00225     return false;
00226   std::string Res;
00227   if (getFilename(Res).empty()) 
00228     return false;
00229   // It is possible that directory and produce string is empty.
00230   return true;
00231 }
00232 
00233 /// Verify - Verify that a type descriptor is well formed.
00234 bool DIType::Verify() const {
00235   if (isNull()) 
00236     return false;
00237   if (getContext().isNull()) 
00238     return false;
00239 
00240   DICompileUnit CU = getCompileUnit();
00241   if (!CU.isNull() && !CU.Verify()) 
00242     return false;
00243   return true;
00244 }
00245 
00246 /// Verify - Verify that a composite type descriptor is well formed.
00247 bool DICompositeType::Verify() const {
00248   if (isNull()) 
00249     return false;
00250   if (getContext().isNull()) 
00251     return false;
00252 
00253   DICompileUnit CU = getCompileUnit();
00254   if (!CU.isNull() && !CU.Verify()) 
00255     return false;
00256   return true;
00257 }
00258 
00259 /// Verify - Verify that a subprogram descriptor is well formed.
00260 bool DISubprogram::Verify() const {
00261   if (isNull())
00262     return false;
00263   
00264   if (getContext().isNull())
00265     return false;
00266 
00267   DICompileUnit CU = getCompileUnit();
00268   if (!CU.Verify()) 
00269     return false;
00270 
00271   DICompositeType Ty = getType();
00272   if (!Ty.isNull() && !Ty.Verify())
00273     return false;
00274   return true;
00275 }
00276 
00277 /// Verify - Verify that a global variable descriptor is well formed.
00278 bool DIGlobalVariable::Verify() const {
00279   if (isNull())
00280     return false;
00281   
00282   if (getContext().isNull())
00283     return false;
00284 
00285   DICompileUnit CU = getCompileUnit();
00286   if (!CU.isNull() && !CU.Verify()) 
00287     return false;
00288 
00289   DIType Ty = getType();
00290   if (!Ty.Verify())
00291     return false;
00292 
00293   if (!getGlobal())
00294     return false;
00295 
00296   return true;
00297 }
00298 
00299 /// Verify - Verify that a variable descriptor is well formed.
00300 bool DIVariable::Verify() const {
00301   if (isNull())
00302     return false;
00303   
00304   if (getContext().isNull())
00305     return false;
00306 
00307   DIType Ty = getType();
00308   if (!Ty.Verify())
00309     return false;
00310 
00311   return true;
00312 }
00313 
00314 /// getOriginalTypeSize - If this type is derived from a base type then
00315 /// return base type size.
00316 uint64_t DIDerivedType::getOriginalTypeSize() const {
00317   if (getTag() != dwarf::DW_TAG_member)
00318     return getSizeInBits();
00319   DIType BT = getTypeDerivedFrom();
00320   if (BT.getTag() != dwarf::DW_TAG_base_type)
00321     return getSizeInBits();
00322   return BT.getSizeInBits();
00323 }
00324 
00325 /// describes - Return true if this subprogram provides debugging
00326 /// information for the function F.
00327 bool DISubprogram::describes(const Function *F) {
00328   assert (F && "Invalid function");
00329   std::string Name;
00330   getLinkageName(Name);
00331   if (Name.empty())
00332     getName(Name);
00333   if (F->getName() == Name)
00334     return true;
00335   return false;
00336 }
00337 
00338 //===----------------------------------------------------------------------===//
00339 // DIDescriptor: dump routines for all descriptors.
00340 //===----------------------------------------------------------------------===//
00341 
00342 
00343 /// dump - Print descriptor.
00344 void DIDescriptor::dump() const {
00345   cerr << "[" << dwarf::TagString(getTag()) << "] ";
00346   cerr << std::hex << "[GV:" << DbgGV << "]" << std::dec;
00347 }
00348 
00349 /// dump - Print compile unit.
00350 void DICompileUnit::dump() const {
00351   if (getLanguage())
00352     cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
00353 
00354   std::string Res1, Res2;
00355   cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
00356 }
00357 
00358 /// dump - Print type.
00359 void DIType::dump() const {
00360   if (isNull()) return;
00361 
00362   std::string Res;
00363   if (!getName(Res).empty())
00364     cerr << " [" << Res << "] ";
00365 
00366   unsigned Tag = getTag();
00367   cerr << " [" << dwarf::TagString(Tag) << "] ";
00368 
00369   // TODO : Print context
00370   getCompileUnit().dump();
00371   cerr << " [" 
00372        << getLineNumber() << ", " 
00373        << getSizeInBits() << ", "
00374        << getAlignInBits() << ", "
00375        << getOffsetInBits() 
00376        << "] ";
00377 
00378   if (isPrivate()) 
00379     cerr << " [private] ";
00380   else if (isProtected())
00381     cerr << " [protected] ";
00382 
00383   if (isForwardDecl())
00384     cerr << " [fwd] ";
00385 
00386   if (isBasicType(Tag))
00387     DIBasicType(DbgGV).dump();
00388   else if (isDerivedType(Tag))
00389     DIDerivedType(DbgGV).dump();
00390   else if (isCompositeType(Tag))
00391     DICompositeType(DbgGV).dump();
00392   else {
00393     cerr << "Invalid DIType\n";
00394     return;
00395   }
00396 
00397   cerr << "\n";
00398 }
00399 
00400 /// dump - Print basic type.
00401 void DIBasicType::dump() const {
00402   cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
00403 }
00404 
00405 /// dump - Print derived type.
00406 void DIDerivedType::dump() const {
00407   cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
00408 }
00409 
00410 /// dump - Print composite type.
00411 void DICompositeType::dump() const {
00412   DIArray A = getTypeArray();
00413   if (A.isNull())
00414     return;
00415   cerr << " [" << A.getNumElements() << " elements]";
00416 }
00417 
00418 /// dump - Print global.
00419 void DIGlobal::dump() const {
00420   std::string Res;
00421   if (!getName(Res).empty())
00422     cerr << " [" << Res << "] ";
00423 
00424   unsigned Tag = getTag();
00425   cerr << " [" << dwarf::TagString(Tag) << "] ";
00426 
00427   // TODO : Print context
00428   getCompileUnit().dump();
00429   cerr << " [" << getLineNumber() << "] ";
00430 
00431   if (isLocalToUnit())
00432     cerr << " [local] ";
00433 
00434   if (isDefinition())
00435     cerr << " [def] ";
00436 
00437   if (isGlobalVariable(Tag))
00438     DIGlobalVariable(DbgGV).dump();
00439 
00440   cerr << "\n";
00441 }
00442 
00443 /// dump - Print subprogram.
00444 void DISubprogram::dump() const {
00445   DIGlobal::dump();
00446 }
00447 
00448 /// dump - Print global variable.
00449 void DIGlobalVariable::dump() const {
00450   cerr << " ["; getGlobal()->dump(); cerr << "] ";
00451 }
00452 
00453 /// dump - Print variable.
00454 void DIVariable::dump() const {
00455   std::string Res;
00456   if (!getName(Res).empty())
00457     cerr << " [" << Res << "] ";
00458 
00459   getCompileUnit().dump();
00460   cerr << " [" << getLineNumber() << "] ";
00461   getType().dump();
00462   cerr << "\n";
00463 }
00464 
00465 //===----------------------------------------------------------------------===//
00466 // DIFactory: Basic Helpers
00467 //===----------------------------------------------------------------------===//
00468 
00469 DIFactory::DIFactory(Module &m)
00470   : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0), 
00471     RegionStartFn(0), RegionEndFn(0),
00472     DeclareFn(0) {
00473   EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
00474 }
00475 
00476 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
00477 /// This is only valid when the descriptor is non-null.
00478 Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
00479   if (D.isNull()) return llvm::Constant::getNullValue(EmptyStructPtr);
00480   return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
00481 }
00482 
00483 Constant *DIFactory::GetTagConstant(unsigned TAG) {
00484   assert((TAG & LLVMDebugVersionMask) == 0 &&
00485          "Tag too large for debug encoding!");
00486   return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
00487 }
00488 
00489 Constant *DIFactory::GetStringConstant(const std::string &String) {
00490   // Check string cache for previous edition.
00491   Constant *&Slot = StringCache[String];
00492   
00493   // Return Constant if previously defined.
00494   if (Slot) return Slot;
00495   
00496   const PointerType *DestTy = PointerType::getUnqual(Type::getInt8Ty(VMContext));
00497   
00498   // If empty string then use a i8* null instead.
00499   if (String.empty())
00500     return Slot = ConstantPointerNull::get(DestTy);
00501 
00502   // Construct string as an llvm constant.
00503   Constant *ConstStr = ConstantArray::get(VMContext, String);
00504     
00505   // Otherwise create and return a new string global.
00506   GlobalVariable *StrGV = new GlobalVariable(M, ConstStr->getType(), true,
00507                                              GlobalVariable::InternalLinkage,
00508                                              ConstStr, ".str");
00509   StrGV->setSection("llvm.metadata");
00510   return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
00511 }
00512 
00513 //===----------------------------------------------------------------------===//
00514 // DIFactory: Primary Constructors
00515 //===----------------------------------------------------------------------===//
00516 
00517 /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
00518 /// This implicitly uniques the arrays created.
00519 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
00520   SmallVector<Constant*, 16> Elts;
00521   
00522   for (unsigned i = 0; i != NumTys; ++i)
00523     Elts.push_back(getCastToEmpty(Tys[i]));
00524   
00525   Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
00526                                                      Elts.size()),
00527                                       Elts.data(), Elts.size());
00528   // If we already have this array, just return the uniqued version.
00529   DIDescriptor &Entry = SimpleConstantCache[Init];
00530   if (!Entry.isNull()) return DIArray(Entry.getGV());
00531 
00532   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00533                                           GlobalValue::InternalLinkage,
00534                                           Init, "llvm.dbg.array");
00535   GV->setSection("llvm.metadata");
00536   Entry = DIDescriptor(GV);
00537   return DIArray(GV);
00538 }
00539 
00540 /// GetOrCreateSubrange - Create a descriptor for a value range.  This
00541 /// implicitly uniques the values returned.
00542 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
00543   Constant *Elts[] = {
00544     GetTagConstant(dwarf::DW_TAG_subrange_type),
00545     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
00546     ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
00547   };
00548   
00549   Constant *Init = ConstantStruct::get(VMContext, Elts,
00550                                        sizeof(Elts)/sizeof(Elts[0]));
00551 
00552   // If we already have this range, just return the uniqued version.
00553   DIDescriptor &Entry = SimpleConstantCache[Init];
00554   if (!Entry.isNull()) return DISubrange(Entry.getGV());
00555   
00556   M.addTypeName("llvm.dbg.subrange.type", Init->getType());
00557 
00558   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00559                                           GlobalValue::InternalLinkage,
00560                                           Init, "llvm.dbg.subrange");
00561   GV->setSection("llvm.metadata");
00562   Entry = DIDescriptor(GV);
00563   return DISubrange(GV);
00564 }
00565 
00566 
00567 
00568 /// CreateCompileUnit - Create a new descriptor for the specified compile
00569 /// unit.  Note that this does not unique compile units within the module.
00570 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
00571                                            const std::string &Filename,
00572                                            const std::string &Directory,
00573                                            const std::string &Producer,
00574                                            bool isMain,
00575                                            bool isOptimized,
00576                                            const char *Flags,
00577                                            unsigned RunTimeVer) {
00578   Constant *Elts[] = {
00579     GetTagConstant(dwarf::DW_TAG_compile_unit),
00580     llvm::Constant::getNullValue(EmptyStructPtr),
00581     ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
00582     GetStringConstant(Filename),
00583     GetStringConstant(Directory),
00584     GetStringConstant(Producer),
00585     ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
00586     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
00587     GetStringConstant(Flags),
00588     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
00589   };
00590   
00591   Constant *Init = ConstantStruct::get(VMContext, Elts,
00592                                        sizeof(Elts)/sizeof(Elts[0]));
00593   
00594   M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
00595   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00596                                           GlobalValue::InternalLinkage,
00597                                           Init, "llvm.dbg.compile_unit");
00598   GV->setSection("llvm.metadata");
00599   return DICompileUnit(GV);
00600 }
00601 
00602 /// CreateEnumerator - Create a single enumerator value.
00603 DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
00604   Constant *Elts[] = {
00605     GetTagConstant(dwarf::DW_TAG_enumerator),
00606     GetStringConstant(Name),
00607     ConstantInt::get(Type::getInt64Ty(VMContext), Val)
00608   };
00609   
00610   Constant *Init = ConstantStruct::get(VMContext, Elts,
00611                                        sizeof(Elts)/sizeof(Elts[0]));
00612   
00613   M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
00614   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00615                                           GlobalValue::InternalLinkage,
00616                                           Init, "llvm.dbg.enumerator");
00617   GV->setSection("llvm.metadata");
00618   return DIEnumerator(GV);
00619 }
00620 
00621 
00622 /// CreateBasicType - Create a basic type like int, float, etc.
00623 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
00624                                       const std::string &Name,
00625                                        DICompileUnit CompileUnit,
00626                                        unsigned LineNumber,
00627                                        uint64_t SizeInBits,
00628                                        uint64_t AlignInBits,
00629                                        uint64_t OffsetInBits, unsigned Flags,
00630                                        unsigned Encoding) {
00631   Constant *Elts[] = {
00632     GetTagConstant(dwarf::DW_TAG_base_type),
00633     getCastToEmpty(Context),
00634     GetStringConstant(Name),
00635     getCastToEmpty(CompileUnit),
00636     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00637     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00638     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00639     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00640     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00641     ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
00642   };
00643   
00644   Constant *Init = ConstantStruct::get(VMContext, Elts,
00645                                        sizeof(Elts)/sizeof(Elts[0]));
00646   
00647   M.addTypeName("llvm.dbg.basictype.type", Init->getType());
00648   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00649                                           GlobalValue::InternalLinkage,
00650                                           Init, "llvm.dbg.basictype");
00651   GV->setSection("llvm.metadata");
00652   return DIBasicType(GV);
00653 }
00654 
00655 /// CreateDerivedType - Create a derived type like const qualified type,
00656 /// pointer, typedef, etc.
00657 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
00658                                            DIDescriptor Context,
00659                                            const std::string &Name,
00660                                            DICompileUnit CompileUnit,
00661                                            unsigned LineNumber,
00662                                            uint64_t SizeInBits,
00663                                            uint64_t AlignInBits,
00664                                            uint64_t OffsetInBits,
00665                                            unsigned Flags,
00666                                            DIType DerivedFrom) {
00667   Constant *Elts[] = {
00668     GetTagConstant(Tag),
00669     getCastToEmpty(Context),
00670     GetStringConstant(Name),
00671     getCastToEmpty(CompileUnit),
00672     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00673     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00674     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00675     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00676     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00677     getCastToEmpty(DerivedFrom)
00678   };
00679   
00680   Constant *Init = ConstantStruct::get(VMContext, Elts,
00681                                        sizeof(Elts)/sizeof(Elts[0]));
00682   
00683   M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
00684   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00685                                           GlobalValue::InternalLinkage,
00686                                           Init, "llvm.dbg.derivedtype");
00687   GV->setSection("llvm.metadata");
00688   return DIDerivedType(GV);
00689 }
00690 
00691 /// CreateCompositeType - Create a composite type like array, struct, etc.
00692 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
00693                                                DIDescriptor Context,
00694                                                const std::string &Name,
00695                                                DICompileUnit CompileUnit,
00696                                                unsigned LineNumber,
00697                                                uint64_t SizeInBits,
00698                                                uint64_t AlignInBits,
00699                                                uint64_t OffsetInBits,
00700                                                unsigned Flags,
00701                                                DIType DerivedFrom,
00702                                                DIArray Elements,
00703                                                unsigned RuntimeLang) {
00704 
00705   Constant *Elts[] = {
00706     GetTagConstant(Tag),
00707     getCastToEmpty(Context),
00708     GetStringConstant(Name),
00709     getCastToEmpty(CompileUnit),
00710     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00711     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00712     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00713     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00714     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00715     getCastToEmpty(DerivedFrom),
00716     getCastToEmpty(Elements),
00717     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
00718   };
00719   
00720   Constant *Init = ConstantStruct::get(VMContext, Elts,
00721                                        sizeof(Elts)/sizeof(Elts[0]));
00722   
00723   M.addTypeName("llvm.dbg.composite.type", Init->getType());
00724   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00725                                           GlobalValue::InternalLinkage,
00726                                           Init, "llvm.dbg.composite");
00727   GV->setSection("llvm.metadata");
00728   return DICompositeType(GV);
00729 }
00730 
00731 
00732 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
00733 /// See comments in DISubprogram for descriptions of these fields.  This
00734 /// method does not unique the generated descriptors.
00735 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context, 
00736                                          const std::string &Name,
00737                                          const std::string &DisplayName,
00738                                          const std::string &LinkageName,
00739                                          DICompileUnit CompileUnit,
00740                                          unsigned LineNo, DIType Type,
00741                                          bool isLocalToUnit,
00742                                          bool isDefinition) {
00743 
00744   Constant *Elts[] = {
00745     GetTagConstant(dwarf::DW_TAG_subprogram),
00746     llvm::Constant::getNullValue(EmptyStructPtr),
00747     getCastToEmpty(Context),
00748     GetStringConstant(Name),
00749     GetStringConstant(DisplayName),
00750     GetStringConstant(LinkageName),
00751     getCastToEmpty(CompileUnit),
00752     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00753     getCastToEmpty(Type),
00754     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
00755     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
00756   };
00757   
00758   Constant *Init = ConstantStruct::get(VMContext, Elts,
00759                                        sizeof(Elts)/sizeof(Elts[0]));
00760   
00761   M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
00762   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00763                                           GlobalValue::InternalLinkage,
00764                                           Init, "llvm.dbg.subprogram");
00765   GV->setSection("llvm.metadata");
00766   return DISubprogram(GV);
00767 }
00768 
00769 /// CreateGlobalVariable - Create a new descriptor for the specified global.
00770 DIGlobalVariable
00771 DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
00772                                 const std::string &DisplayName,
00773                                 const std::string &LinkageName,
00774                                 DICompileUnit CompileUnit,
00775                                 unsigned LineNo, DIType Type,bool isLocalToUnit,
00776                                 bool isDefinition, llvm::GlobalVariable *Val) {
00777   Constant *Elts[] = {
00778     GetTagConstant(dwarf::DW_TAG_variable),
00779     llvm::Constant::getNullValue(EmptyStructPtr),
00780     getCastToEmpty(Context),
00781     GetStringConstant(Name),
00782     GetStringConstant(DisplayName),
00783     GetStringConstant(LinkageName),
00784     getCastToEmpty(CompileUnit),
00785     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00786     getCastToEmpty(Type),
00787     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
00788     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
00789     ConstantExpr::getBitCast(Val, EmptyStructPtr)
00790   };
00791   
00792   Constant *Init = ConstantStruct::get(VMContext, Elts,
00793                                        sizeof(Elts)/sizeof(Elts[0]));
00794   
00795   M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
00796   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00797                                           GlobalValue::LinkOnceAnyLinkage,
00798                                           Init, "llvm.dbg.global_variable");
00799   GV->setSection("llvm.metadata");
00800   return DIGlobalVariable(GV);
00801 }
00802 
00803 
00804 /// CreateVariable - Create a new descriptor for the specified variable.
00805 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
00806                                      const std::string &Name,
00807                                      DICompileUnit CompileUnit, unsigned LineNo,
00808                                      DIType Type) {
00809   Constant *Elts[] = {
00810     GetTagConstant(Tag),
00811     getCastToEmpty(Context),
00812     GetStringConstant(Name),
00813     getCastToEmpty(CompileUnit),
00814     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00815     getCastToEmpty(Type)
00816   };
00817   
00818   Constant *Init = ConstantStruct::get(VMContext, Elts,
00819                                        sizeof(Elts)/sizeof(Elts[0]));
00820   
00821   M.addTypeName("llvm.dbg.variable.type", Init->getType());
00822   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00823                                           GlobalValue::InternalLinkage,
00824                                           Init, "llvm.dbg.variable");
00825   GV->setSection("llvm.metadata");
00826   return DIVariable(GV);
00827 }
00828 
00829 
00830 /// CreateBlock - This creates a descriptor for a lexical block with the
00831 /// specified parent VMContext.
00832 DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
00833   Constant *Elts[] = {
00834     GetTagConstant(dwarf::DW_TAG_lexical_block),
00835     getCastToEmpty(Context)
00836   };
00837   
00838   Constant *Init = ConstantStruct::get(VMContext, Elts,
00839                                        sizeof(Elts)/sizeof(Elts[0]));
00840   
00841   M.addTypeName("llvm.dbg.block.type", Init->getType());
00842   GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
00843                                           GlobalValue::InternalLinkage,
00844                                           Init, "llvm.dbg.block");
00845   GV->setSection("llvm.metadata");
00846   return DIBlock(GV);
00847 }
00848 
00849 
00850 //===----------------------------------------------------------------------===//
00851 // DIFactory: Routines for inserting code into a function
00852 //===----------------------------------------------------------------------===//
00853 
00854 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
00855 /// inserting it at the end of the specified basic block.
00856 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
00857                                 unsigned ColNo, BasicBlock *BB) {
00858   
00859   // Lazily construct llvm.dbg.stoppoint function.
00860   if (!StopPointFn)
00861     StopPointFn = llvm::Intrinsic::getDeclaration(&M, 
00862                                               llvm::Intrinsic::dbg_stoppoint);
00863   
00864   // Invoke llvm.dbg.stoppoint
00865   Value *Args[] = {
00866     ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
00867     ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
00868     getCastToEmpty(CU)
00869   };
00870   CallInst::Create(StopPointFn, Args, Args+3, "", BB);
00871 }
00872 
00873 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
00874 /// mark the start of the specified subprogram.
00875 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
00876   // Lazily construct llvm.dbg.func.start.
00877   if (!FuncStartFn)
00878     FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
00879   
00880   // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
00881   CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
00882 }
00883 
00884 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
00885 /// mark the start of a region for the specified scoping descriptor.
00886 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
00887   // Lazily construct llvm.dbg.region.start function.
00888   if (!RegionStartFn)
00889     RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
00890 
00891   // Call llvm.dbg.func.start.
00892   CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
00893 }
00894 
00895 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
00896 /// mark the end of a region for the specified scoping descriptor.
00897 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
00898   // Lazily construct llvm.dbg.region.end function.
00899   if (!RegionEndFn)
00900     RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
00901 
00902   // Call llvm.dbg.region.end.
00903   CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
00904 }
00905 
00906 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
00907 void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
00908   // Cast the storage to a {}* for the call to llvm.dbg.declare.
00909   Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
00910   
00911   if (!DeclareFn)
00912     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
00913 
00914   Value *Args[] = { Storage, getCastToEmpty(D) };
00915   CallInst::Create(DeclareFn, Args, Args+2, "", BB);
00916 }
00917 
00918 //===----------------------------------------------------------------------===//
00919 // DebugInfoFinder implementations.
00920 //===----------------------------------------------------------------------===//
00921 
00922 /// processModule - Process entire module and collect debug info.
00923 void DebugInfoFinder::processModule(Module &M) {
00924   
00925   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00926     for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
00927       for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
00928            ++BI) {
00929         if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
00930           processStopPoint(SPI);
00931         else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
00932           processFuncStart(FSI);
00933         else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
00934           processRegionStart(DRS);
00935         else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
00936           processRegionEnd(DRE);
00937         else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
00938           processDeclare(DDI);
00939       }
00940   
00941   for (Module::global_iterator GVI = M.global_begin(), GVE = M.global_end();
00942        GVI != GVE; ++GVI) {
00943     GlobalVariable *GV = GVI;
00944     if (!GV->hasName() || !GV->isConstant() 
00945         || strncmp(GV->getName().data(), "llvm.dbg.global_variable", 24)
00946         || !GV->hasInitializer())
00947       continue;
00948     DIGlobalVariable DIG(GV);
00949     if (addGlobalVariable(DIG)) {
00950       addCompileUnit(DIG.getCompileUnit());
00951       processType(DIG.getType());
00952     }
00953   }
00954 }
00955     
00956 /// processType - Process DIType.
00957 void DebugInfoFinder::processType(DIType DT) {
00958   if (!addType(DT))
00959     return;
00960 
00961   addCompileUnit(DT.getCompileUnit());
00962   if (DT.isCompositeType(DT.getTag())) {
00963     DICompositeType DCT(DT.getGV());
00964     processType(DCT.getTypeDerivedFrom());
00965     DIArray DA = DCT.getTypeArray();
00966     if (!DA.isNull())
00967       for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
00968         DIDescriptor D = DA.getElement(i);
00969         DIType TypeE = DIType(D.getGV());
00970         if (!TypeE.isNull())
00971           processType(TypeE);
00972         else 
00973           processSubprogram(DISubprogram(D.getGV()));
00974       }
00975   } else if (DT.isDerivedType(DT.getTag())) {
00976     DIDerivedType DDT(DT.getGV());
00977     if (!DDT.isNull()) 
00978       processType(DDT.getTypeDerivedFrom());
00979   }
00980 }
00981 
00982 /// processSubprogram - Process DISubprogram.
00983 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
00984   if (SP.isNull())
00985     return;
00986   if (!addSubprogram(SP))
00987     return;
00988   addCompileUnit(SP.getCompileUnit());
00989   processType(SP.getType());
00990 }
00991 
00992 /// processStopPoint - Process DbgStopPointInst.
00993 void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
00994   GlobalVariable *Context = dyn_cast<GlobalVariable>(SPI->getContext());
00995   addCompileUnit(DICompileUnit(Context));
00996 }
00997 
00998 /// processFuncStart - Process DbgFuncStartInst.
00999 void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
01000   GlobalVariable *SP = dyn_cast<GlobalVariable>(FSI->getSubprogram());
01001   processSubprogram(DISubprogram(SP));
01002 }
01003 
01004 /// processRegionStart - Process DbgRegionStart.
01005 void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
01006   GlobalVariable *SP = dyn_cast<GlobalVariable>(DRS->getContext());
01007   processSubprogram(DISubprogram(SP));
01008 }
01009 
01010 /// processRegionEnd - Process DbgRegionEnd.
01011 void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
01012   GlobalVariable *SP = dyn_cast<GlobalVariable>(DRE->getContext());
01013   processSubprogram(DISubprogram(SP));
01014 }
01015 
01016 /// processDeclare - Process DbgDeclareInst.
01017 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
01018   DIVariable DV(cast<GlobalVariable>(DDI->getVariable()));
01019   if (DV.isNull())
01020     return;
01021 
01022   if (!NodesSeen.insert(DV.getGV()))
01023     return;
01024 
01025   addCompileUnit(DV.getCompileUnit());
01026   processType(DV.getType());
01027 }
01028 
01029 /// addType - Add type into Tys.
01030 bool DebugInfoFinder::addType(DIType DT) {
01031   if (DT.isNull())
01032     return false;
01033 
01034   if (!NodesSeen.insert(DT.getGV()))
01035     return false;
01036 
01037   TYs.push_back(DT.getGV());
01038   return true;
01039 }
01040 
01041 /// addCompileUnit - Add compile unit into CUs.
01042 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
01043   if (CU.isNull())
01044     return false;
01045 
01046   if (!NodesSeen.insert(CU.getGV()))
01047     return false;
01048 
01049   CUs.push_back(CU.getGV());
01050   return true;
01051 }
01052     
01053 /// addGlobalVariable - Add global variable into GVs.
01054 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
01055   if (DIG.isNull())
01056     return false;
01057 
01058   if (!NodesSeen.insert(DIG.getGV()))
01059     return false;
01060 
01061   GVs.push_back(DIG.getGV());
01062   return true;
01063 }
01064 
01065 // addSubprogram - Add subprgoram into SPs.
01066 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
01067   if (SP.isNull())
01068     return false;
01069   
01070   if (!NodesSeen.insert(SP.getGV()))
01071     return false;
01072 
01073   SPs.push_back(SP.getGV());
01074   return true;
01075 }
01076 
01077 namespace llvm {
01078   /// findStopPoint - Find the stoppoint coressponding to this instruction, that
01079   /// is the stoppoint that dominates this instruction.
01080   const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
01081     if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
01082       return DSI;
01083 
01084     const BasicBlock *BB = Inst->getParent();
01085     BasicBlock::const_iterator I = Inst, B;
01086     while (BB) {
01087       B = BB->begin();
01088 
01089       // A BB consisting only of a terminator can't have a stoppoint.
01090       while (I != B) {
01091         --I;
01092         if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
01093           return DSI;
01094       }
01095 
01096       // This BB didn't have a stoppoint: if there is only one predecessor, look
01097       // for a stoppoint there. We could use getIDom(), but that would require
01098       // dominator info.
01099       BB = I->getParent()->getUniquePredecessor();
01100       if (BB)
01101         I = BB->getTerminator();
01102     }
01103 
01104     return 0;
01105   }
01106 
01107   /// findBBStopPoint - Find the stoppoint corresponding to first real
01108   /// (non-debug intrinsic) instruction in this Basic Block, and return the
01109   /// stoppoint for it.
01110   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
01111     for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
01112       if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
01113         return DSI;
01114 
01115     // Fallback to looking for stoppoint of unique predecessor. Useful if this
01116     // BB contains no stoppoints, but unique predecessor does.
01117     BB = BB->getUniquePredecessor();
01118     if (BB)
01119       return findStopPoint(BB->getTerminator());
01120 
01121     return 0;
01122   }
01123 
01124   Value *findDbgGlobalDeclare(GlobalVariable *V) {
01125     const Module *M = V->getParent();
01126     
01127     const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
01128     if (!Ty) return 0;
01129 
01130     Ty = PointerType::get(Ty, 0);
01131 
01132     Value *Val = V->stripPointerCasts();
01133     for (Value::use_iterator I = Val->use_begin(), E = Val->use_end();
01134          I != E; ++I) {
01135       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
01136         if (CE->getOpcode() == Instruction::BitCast) {
01137           Value *VV = CE;
01138 
01139           while (VV->hasOneUse())
01140             VV = *VV->use_begin();
01141 
01142           if (VV->getType() == Ty)
01143             return VV;
01144         }
01145       }
01146     }
01147     
01148     if (Val->getType() == Ty)
01149       return Val;
01150 
01151     return 0;
01152   }
01153 
01154   /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
01155   /// It looks through pointer casts too.
01156   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
01157     if (stripCasts) {
01158       V = V->stripPointerCasts();
01159 
01160       // Look for the bitcast.
01161       for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
01162             I != E; ++I)
01163         if (isa<BitCastInst>(I))
01164           return findDbgDeclare(*I, false);
01165 
01166       return 0;
01167     }
01168 
01169     // Find llvm.dbg.declare among uses of the instruction.
01170     for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
01171           I != E; ++I)
01172       if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
01173         return DDI;
01174 
01175     return 0;
01176   }
01177 
01178   bool getLocationInfo(const Value *V, std::string &DisplayName,
01179                        std::string &Type, unsigned &LineNo, std::string &File,
01180                        std::string &Dir) {
01181     DICompileUnit Unit;
01182     DIType TypeD;
01183 
01184     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
01185       Value *DIGV = findDbgGlobalDeclare(GV);
01186       if (!DIGV) return false;
01187       DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
01188 
01189       Var.getDisplayName(DisplayName);
01190       LineNo = Var.getLineNumber();
01191       Unit = Var.getCompileUnit();
01192       TypeD = Var.getType();
01193     } else {
01194       const DbgDeclareInst *DDI = findDbgDeclare(V);
01195       if (!DDI) return false;
01196       DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
01197 
01198       Var.getName(DisplayName);
01199       LineNo = Var.getLineNumber();
01200       Unit = Var.getCompileUnit();
01201       TypeD = Var.getType();
01202     }
01203 
01204     TypeD.getName(Type);
01205     Unit.getFilename(File);
01206     Unit.getDirectory(Dir);
01207     return true;
01208   }
01209 
01210   /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug 
01211   /// info intrinsic.
01212   bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI, 
01213                                  CodeGenOpt::Level OptLev) {
01214     return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
01215   }
01216 
01217   /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug 
01218   /// info intrinsic.
01219   bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
01220                                  CodeGenOpt::Level OptLev) {
01221     return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
01222   }
01223 
01224   /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug 
01225   /// info intrinsic.
01226   bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
01227                                  CodeGenOpt::Level OptLev) {
01228     return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
01229   }
01230 
01231   /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug 
01232   /// info intrinsic.
01233   bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
01234                                  CodeGenOpt::Level OptLev) {
01235     return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
01236   }
01237 
01238 
01239   /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug 
01240   /// info intrinsic.
01241   bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
01242                                  CodeGenOpt::Level OptLev) {
01243     return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
01244   }
01245 
01246   /// ExtractDebugLocation - Extract debug location information 
01247   /// from llvm.dbg.stoppoint intrinsic.
01248   DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
01249                                 DebugLocTracker &DebugLocInfo) {
01250     DebugLoc DL;
01251     Value *Context = SPI.getContext();
01252 
01253     // If this location is already tracked then use it.
01254     DebugLocTuple Tuple(cast<GlobalVariable>(Context), SPI.getLine(), 
01255                         SPI.getColumn());
01256     DenseMap<DebugLocTuple, unsigned>::iterator II
01257       = DebugLocInfo.DebugIdMap.find(Tuple);
01258     if (II != DebugLocInfo.DebugIdMap.end())
01259       return DebugLoc::get(II->second);
01260 
01261     // Add a new location entry.
01262     unsigned Id = DebugLocInfo.DebugLocations.size();
01263     DebugLocInfo.DebugLocations.push_back(Tuple);
01264     DebugLocInfo.DebugIdMap[Tuple] = Id;
01265     
01266     return DebugLoc::get(Id);
01267   }
01268 
01269   /// ExtractDebugLocation - Extract debug location information 
01270   /// from llvm.dbg.func_start intrinsic.
01271   DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
01272                                 DebugLocTracker &DebugLocInfo) {
01273     DebugLoc DL;
01274     Value *SP = FSI.getSubprogram();
01275 
01276     DISubprogram Subprogram(cast<GlobalVariable>(SP));
01277     unsigned Line = Subprogram.getLineNumber();
01278     DICompileUnit CU(Subprogram.getCompileUnit());
01279 
01280     // If this location is already tracked then use it.
01281     DebugLocTuple Tuple(CU.getGV(), Line, /* Column */ 0);
01282     DenseMap<DebugLocTuple, unsigned>::iterator II
01283       = DebugLocInfo.DebugIdMap.find(Tuple);
01284     if (II != DebugLocInfo.DebugIdMap.end())
01285       return DebugLoc::get(II->second);
01286 
01287     // Add a new location entry.
01288     unsigned Id = DebugLocInfo.DebugLocations.size();
01289     DebugLocInfo.DebugLocations.push_back(Tuple);
01290     DebugLocInfo.DebugIdMap[Tuple] = Id;
01291     
01292     return DebugLoc::get(Id);
01293   }
01294 
01295   /// isInlinedFnStart - Return true if FSI is starting an inlined function.
01296   bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
01297     DISubprogram Subprogram(cast<GlobalVariable>(FSI.getSubprogram()));
01298     if (Subprogram.describes(CurrentFn))
01299       return false;
01300 
01301     return true;
01302   }
01303 
01304   /// isInlinedFnEnd - Return true if REI is ending an inlined function.
01305   bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
01306     DISubprogram Subprogram(cast<GlobalVariable>(REI.getContext()));
01307     if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
01308       return false;
01309 
01310     return true;
01311   }
01312 
01313 }