diff options
author | Torok Edwin <edwintorok@gmail.com> | 2009-03-10 13:41:26 +0000 |
---|---|---|
committer | Torok Edwin <edwintorok@gmail.com> | 2009-03-10 13:41:26 +0000 |
commit | 51b4a28878a3356eb510a07954ddd96137f5a7e1 (patch) | |
tree | f34465412f6dd3eb1eb27df9b9b24857b6a7554a /llvm/lib/Analysis | |
parent | afb355f28147f6a935216bc2eb2e46fbb424a00e (diff) | |
download | bcm5719-llvm-51b4a28878a3356eb510a07954ddd96137f5a7e1.tar.gz bcm5719-llvm-51b4a28878a3356eb510a07954ddd96137f5a7e1.zip |
Global variables don't have a corresponding llvm.dbg.declare, yet it is possible
to obtain debug info about them.
Introduce helpers to access debug info for global variables. Also introduce a
helper that works for both local and global variables.
llvm-svn: 66541
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r-- | llvm/lib/Analysis/DbgInfoPrinter.cpp | 23 | ||||
-rw-r--r-- | llvm/lib/Analysis/DebugInfo.cpp | 58 |
2 files changed, 75 insertions, 6 deletions
diff --git a/llvm/lib/Analysis/DbgInfoPrinter.cpp b/llvm/lib/Analysis/DbgInfoPrinter.cpp index 127f931b6ed..e43bc81ffac 100644 --- a/llvm/lib/Analysis/DbgInfoPrinter.cpp +++ b/llvm/lib/Analysis/DbgInfoPrinter.cpp @@ -19,6 +19,7 @@ #include "llvm/Module.h" #include "llvm/Value.h" #include "llvm/IntrinsicInst.h" +#include "llvm/Assembly/Writer.h" #include "llvm/Analysis/DebugInfo.h" #include "llvm/Analysis/Passes.h" #include "llvm/Analysis/ValueTracking.h" @@ -57,12 +58,17 @@ FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); } void PrintDbgInfo::printVariableDeclaration(const Value *V) { - if(const DbgDeclareInst* DDI = findDbgDeclare(V)) { - DIVariable Var(cast<GlobalVariable>(DDI->getVariable())); - std::string Res1, Res2; - Out << "; variable " << Var.getName(Res1) - << " of type " << Var.getType().getName(Res2) - << " at line " << Var.getLineNumber() << "\n"; + std::string DisplayName, File, Directory, Type; + unsigned LineNo; + if (getLocationInfo(V, DisplayName, Type, LineNo, File, Directory)) { + Out << "; "; + WriteAsOperand(Out, V, false, 0); + Out << " is variable " << DisplayName + << " of type " << Type << " declared at "; + if (PrintDirectory) { + Out << Directory << "/"; + } + Out << File << ":" << LineNo << "\n"; } } @@ -140,6 +146,11 @@ bool PrintDbgInfo::runOnFunction(Function &F) } Out << *i; printVariableDeclaration(i); + if (const User *U = dyn_cast<User>(i)) { + for(unsigned i=0;i<U->getNumOperands();i++) { + printVariableDeclaration(U->getOperand(i)); + } + } } } } diff --git a/llvm/lib/Analysis/DebugInfo.cpp b/llvm/lib/Analysis/DebugInfo.cpp index 4e229e96690..d9f0aa53e9d 100644 --- a/llvm/lib/Analysis/DebugInfo.cpp +++ b/llvm/lib/Analysis/DebugInfo.cpp @@ -841,6 +841,34 @@ namespace llvm { return 0; } + Value *findDbgGlobalDeclare(GlobalVariable *V) + { + const Module *M = V->getParent(); + const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type"); + if (!Ty) + return 0; + Ty = PointerType::get(Ty, 0); + + Value *Val = V->stripPointerCasts(); + for (Value::use_iterator I = Val->use_begin(), E =Val->use_end(); + I != E; ++I) { + if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) { + if (CE->getOpcode() == Instruction::BitCast) { + Value *VV = CE; + while (VV->hasOneUse()) { + VV = *VV->use_begin(); + } + if (VV->getType() == Ty) + return VV; + } + } + } + + if (Val->getType() == Ty) + return Val; + return 0; + } + /// Finds the dbg.declare intrinsic corresponding to this value if any. /// It looks through pointer casts too. const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) @@ -864,6 +892,36 @@ namespace llvm { } return 0; } + + bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type, + unsigned &LineNo, std::string &File, std::string &Dir) + { + DICompileUnit Unit; + DIType TypeD; + if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) { + Value *DIGV = findDbgGlobalDeclare(GV); + if (!DIGV) + return false; + DIGlobalVariable Var(cast<GlobalVariable>(DIGV)); + Var.getDisplayName(DisplayName); + LineNo = Var.getLineNumber(); + Unit = Var.getCompileUnit(); + TypeD = Var.getType(); + } else { + const DbgDeclareInst *DDI = findDbgDeclare(V); + if (!DDI) + return false; + DIVariable Var(cast<GlobalVariable>(DDI->getVariable())); + Var.getName(DisplayName); + LineNo = Var.getLineNumber(); + Unit = Var.getCompileUnit(); + TypeD = Var.getType(); + } + TypeD.getName(Type); + Unit.getFilename(File); + Unit.getDirectory(Dir); + return true; + } } /// dump - print compile unit. |