[llvm-commits] [llvm] r66541 - in /llvm/trunk: include/llvm/Analysis/DebugInfo.h lib/Analysis/DbgInfoPrinter.cpp lib/Analysis/DebugInfo.cpp test/DebugInfo/printdbginfo.ll test/DebugInfo/printdbginfo2.ll

Torok Edwin edwintorok at gmail.com
Tue Mar 10 06:41:29 PDT 2009


Author: edwin
Date: Tue Mar 10 08:41:26 2009
New Revision: 66541

URL: http://llvm.org/viewvc/llvm-project?rev=66541&view=rev
Log:
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.

Added:
    llvm/trunk/test/DebugInfo/printdbginfo2.ll
Modified:
    llvm/trunk/include/llvm/Analysis/DebugInfo.h
    llvm/trunk/lib/Analysis/DbgInfoPrinter.cpp
    llvm/trunk/lib/Analysis/DebugInfo.cpp
    llvm/trunk/test/DebugInfo/printdbginfo.ll

Modified: llvm/trunk/include/llvm/Analysis/DebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/DebugInfo.h?rev=66541&r1=66540&r2=66541&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Analysis/DebugInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/DebugInfo.h Tue Mar 10 08:41:26 2009
@@ -507,6 +507,12 @@
   /// 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 = true);
+
+  /// Find the debug info descriptor corresponding to this global variable.
+  Value *findDbgGlobalDeclare(GlobalVariable *V);
+
+  bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type, 
+                       unsigned &LineNo, std::string &File, std::string &Dir); 
 } // end namespace llvm
 
 #endif

Modified: llvm/trunk/lib/Analysis/DbgInfoPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/DbgInfoPrinter.cpp?rev=66541&r1=66540&r2=66541&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/DbgInfoPrinter.cpp (original)
+++ llvm/trunk/lib/Analysis/DbgInfoPrinter.cpp Tue Mar 10 08:41:26 2009
@@ -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 @@
 
 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 @@
         }
         Out << *i;
         printVariableDeclaration(i);
+        if (const User *U = dyn_cast<User>(i)) {
+          for(unsigned i=0;i<U->getNumOperands();i++) {
+            printVariableDeclaration(U->getOperand(i));
+          }
+        }
       }
     }
   }

Modified: llvm/trunk/lib/Analysis/DebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/DebugInfo.cpp?rev=66541&r1=66540&r2=66541&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/DebugInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/DebugInfo.cpp Tue Mar 10 08:41:26 2009
@@ -841,6 +841,34 @@
     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 @@
     }
     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.

Modified: llvm/trunk/test/DebugInfo/printdbginfo.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/DebugInfo/printdbginfo.ll?rev=66541&r1=66540&r2=66541&view=diff

==============================================================================
--- llvm/trunk/test/DebugInfo/printdbginfo.ll (original)
+++ llvm/trunk/test/DebugInfo/printdbginfo.ll Tue Mar 10 08:41:26 2009
@@ -1,6 +1,6 @@
 ; RUN: llvm-as < %s | opt -print-dbginfo -disable-output > %t1
 ; RUN: %prcontext {function name: Bar::bar return type: int at line 12} 1 < %t1 | grep {(tst.cpp:14)}
-; RUN: %prcontext {%%tmp1} 1 < %t1 | grep -E {variable tmp.+at line 23}
+; RUN: %prcontext {%%tmp1} 1 < %t1 | grep -E {variable tmp.+at tst.cpp:23}
 ; RUN: %prcontext {; tst.cpp:24} 2 < %t1 | grep {%%6}
 	%llvm.dbg.anchor.type = type { i32, i32 }
 	%llvm.dbg.basictype.type = type { i32, { }*, i8*, { }*, i32, i64, i64, i64, i32, i32 }

Added: llvm/trunk/test/DebugInfo/printdbginfo2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/DebugInfo/printdbginfo2.ll?rev=66541&view=auto

==============================================================================
--- llvm/trunk/test/DebugInfo/printdbginfo2.ll (added)
+++ llvm/trunk/test/DebugInfo/printdbginfo2.ll Tue Mar 10 08:41:26 2009
@@ -0,0 +1,74 @@
+; RUN: llvm-as < %s | opt -print-dbginfo -disable-output > %t1
+; RUN: grep {%b is variable b of type x declared at x.c:7} %t1
+; RUN: grep {%2 is variable b of type x declared at x.c:7} %t1
+; RUN: grep {@c.1442 is variable c of type int declared at x.c:4} %t1
+	type { }		; type %0
+	%llvm.dbg.anchor.type = type { i32, i32 }
+	%llvm.dbg.basictype.type = type { i32, %0*, i8*, %0*, i32, i64, i64, i64, i32, i32 }
+	%llvm.dbg.compile_unit.type = type { i32, %0*, i32, i8*, i8*, i8*, i1, i1, i8*, i32 }
+	%llvm.dbg.composite.type = type { i32, %0*, i8*, %0*, i32, i64, i64, i64, i32, %0*, %0*, i32 }
+	%llvm.dbg.derivedtype.type = type { i32, %0*, i8*, %0*, i32, i64, i64, i64, i32, %0* }
+	%llvm.dbg.global_variable.type = type { i32, %0*, %0*, i8*, i8*, i8*, %0*, i32, %0*, i1, i1, %0* }
+	%llvm.dbg.subprogram.type = type { i32, %0*, %0*, i8*, i8*, i8*, %0*, i32, %0*, i1, i1 }
+	%llvm.dbg.subrange.type = type { i32, i64, i64 }
+	%llvm.dbg.variable.type = type { i32, %0*, i8*, %0*, i32, %0* }
+	%struct..0x = type { i32 }
+ at llvm.dbg.compile_units = linkonce constant %llvm.dbg.anchor.type { i32 458752, i32 17 }, section "llvm.metadata"		; <%llvm.dbg.anchor.type*> [#uses=1]
+ at .str = internal constant [4 x i8] c"x.c\00", section "llvm.metadata"		; <[4 x i8]*> [#uses=1]
+ at .str1 = internal constant [27 x i8] c"/home/edwin/llvm-svn/llvm/\00", section "llvm.metadata"		; <[27 x i8]*> [#uses=1]
+ at .str2 = internal constant [52 x i8] c"4.2.1 (Based on Apple Inc. build 5641) (LLVM build)\00", section "llvm.metadata"		; <[52 x i8]*> [#uses=1]
+ at llvm.dbg.compile_unit = internal constant %llvm.dbg.compile_unit.type { i32 458769, %0* bitcast (%llvm.dbg.anchor.type* @llvm.dbg.compile_units to %0*), i32 1, i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0), i8* getelementptr ([27 x i8]* @.str1, i32 0, i32 0), i8* getelementptr ([52 x i8]* @.str2, i32 0, i32 0), i1 true, i1 false, i8* null, i32 0 }, section "llvm.metadata"		; <%llvm.dbg.compile_unit.type*> [#uses=1]
+ at .str3 = internal constant [4 x i8] c"int\00", section "llvm.metadata"		; <[4 x i8]*> [#uses=1]
+ at llvm.dbg.basictype = internal constant %llvm.dbg.basictype.type { i32 458788, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i8* getelementptr ([4 x i8]* @.str3, i32 0, i32 0), %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 0, i64 32, i64 32, i64 0, i32 0, i32 5 }, section "llvm.metadata"		; <%llvm.dbg.basictype.type*> [#uses=1]
+ at llvm.dbg.array = internal constant [1 x %0*] [%0* bitcast (%llvm.dbg.basictype.type* @llvm.dbg.basictype to %0*)], section "llvm.metadata"		; <[1 x %0*]*> [#uses=1]
+ at llvm.dbg.composite = internal constant %llvm.dbg.composite.type { i32 458773, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i8* null, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 0, i64 0, i64 0, i64 0, i32 0, %0* null, %0* bitcast ([1 x %0*]* @llvm.dbg.array to %0*), i32 0 }, section "llvm.metadata"		; <%llvm.dbg.composite.type*> [#uses=1]
+ at llvm.dbg.subprograms = linkonce constant %llvm.dbg.anchor.type { i32 458752, i32 46 }, section "llvm.metadata"		; <%llvm.dbg.anchor.type*> [#uses=1]
+ at .str4 = internal constant [5 x i8] c"main\00", section "llvm.metadata"		; <[5 x i8]*> [#uses=1]
+ at llvm.dbg.subprogram = internal constant %llvm.dbg.subprogram.type { i32 458798, %0* bitcast (%llvm.dbg.anchor.type* @llvm.dbg.subprograms to %0*), %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i8* getelementptr ([5 x i8]* @.str4, i32 0, i32 0), i8* getelementptr ([5 x i8]* @.str4, i32 0, i32 0), i8* null, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 2, %0* bitcast (%llvm.dbg.composite.type* @llvm.dbg.composite to %0*), i1 false, i1 true }, section "llvm.metadata"		; <%llvm.dbg.subprogram.type*> [#uses=1]
+ at .str5 = internal constant [2 x i8] c"x\00", section "llvm.metadata"		; <[2 x i8]*> [#uses=1]
+ at .str7 = internal constant [2 x i8] c"a\00", section "llvm.metadata"		; <[2 x i8]*> [#uses=1]
+ at llvm.dbg.derivedtype = internal constant %llvm.dbg.derivedtype.type { i32 458765, %0* bitcast (%llvm.dbg.subprogram.type* @llvm.dbg.subprogram to %0*), i8* getelementptr ([2 x i8]* @.str7, i32 0, i32 0), %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 6, i64 32, i64 32, i64 0, i32 0, %0* bitcast (%llvm.dbg.basictype.type* @llvm.dbg.basictype to %0*) }, section "llvm.metadata"		; <%llvm.dbg.derivedtype.type*> [#uses=1]
+ at llvm.dbg.array8 = internal constant [1 x %0*] [%0* bitcast (%llvm.dbg.derivedtype.type* @llvm.dbg.derivedtype to %0*)], section "llvm.metadata"		; <[1 x %0*]*> [#uses=1]
+ at llvm.dbg.composite9 = internal constant %llvm.dbg.composite.type { i32 458771, %0* bitcast (%llvm.dbg.subprogram.type* @llvm.dbg.subprogram to %0*), i8* getelementptr ([2 x i8]* @.str5, i32 0, i32 0), %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 5, i64 32, i64 32, i64 0, i32 0, %0* null, %0* bitcast ([1 x %0*]* @llvm.dbg.array8 to %0*), i32 0 }, section "llvm.metadata"		; <%llvm.dbg.composite.type*> [#uses=1]
+ at .str10 = internal constant [2 x i8] c"b\00", section "llvm.metadata"		; <[2 x i8]*> [#uses=1]
+ at llvm.dbg.variable = internal constant %llvm.dbg.variable.type { i32 459008, %0* bitcast (%llvm.dbg.subprogram.type* @llvm.dbg.subprogram to %0*), i8* getelementptr ([2 x i8]* @.str10, i32 0, i32 0), %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 7, %0* bitcast (%llvm.dbg.composite.type* @llvm.dbg.composite9 to %0*) }, section "llvm.metadata"		; <%llvm.dbg.variable.type*> [#uses=1]
+ at llvm.dbg.subrange = internal constant %llvm.dbg.subrange.type { i32 458785, i64 0, i64 3 }, section "llvm.metadata"		; <%llvm.dbg.subrange.type*> [#uses=1]
+ at llvm.dbg.array11 = internal constant [1 x %0*] [%0* bitcast (%llvm.dbg.subrange.type* @llvm.dbg.subrange to %0*)], section "llvm.metadata"		; <[1 x %0*]*> [#uses=1]
+ at llvm.dbg.composite12 = internal constant %llvm.dbg.composite.type { i32 458753, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i8* null, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 0, i64 128, i64 32, i64 0, i32 0, %0* bitcast (%llvm.dbg.basictype.type* @llvm.dbg.basictype to %0*), %0* bitcast ([1 x %0*]* @llvm.dbg.array11 to %0*), i32 0 }, section "llvm.metadata"		; <%llvm.dbg.composite.type*> [#uses=1]
+ at llvm.dbg.variable13 = internal constant %llvm.dbg.variable.type { i32 459008, %0* bitcast (%llvm.dbg.subprogram.type* @llvm.dbg.subprogram to %0*), i8* getelementptr ([2 x i8]* @.str7, i32 0, i32 0), %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 3, %0* bitcast (%llvm.dbg.composite.type* @llvm.dbg.composite12 to %0*) }, section "llvm.metadata"		; <%llvm.dbg.variable.type*> [#uses=1]
+ at c.1442 = internal global i32 5		; <i32*> [#uses=2]
+ at llvm.dbg.global_variables = linkonce constant %llvm.dbg.anchor.type { i32 458752, i32 52 }, section "llvm.metadata"		; <%llvm.dbg.anchor.type*> [#uses=1]
+ at .str14 = internal constant [7 x i8] c"c.1442\00", section "llvm.metadata"		; <[7 x i8]*> [#uses=1]
+ at .str15 = internal constant [2 x i8] c"c\00", section "llvm.metadata"		; <[2 x i8]*> [#uses=1]
+ at llvm.dbg.global_variable = internal constant %llvm.dbg.global_variable.type { i32 458804, %0* bitcast (%llvm.dbg.anchor.type* @llvm.dbg.global_variables to %0*), %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i8* getelementptr ([7 x i8]* @.str14, i32 0, i32 0), i8* getelementptr ([2 x i8]* @.str15, i32 0, i32 0), i8* null, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*), i32 4, %0* bitcast (%llvm.dbg.basictype.type* @llvm.dbg.basictype to %0*), i1 true, i1 true, %0* bitcast (i32* @c.1442 to %0*) }, section "llvm.metadata"		; <%llvm.dbg.global_variable.type*> [#uses=0]
+
+define i32 @main() nounwind {
+entry:
+	%b = alloca %struct..0x		; <%struct..0x*> [#uses=2]
+	%a = alloca [4 x i32]		; <[4 x i32]*> [#uses=1]
+	%"alloca point" = bitcast i32 0 to i32		; <i32> [#uses=0]
+	call void @llvm.dbg.func.start(%0* bitcast (%llvm.dbg.subprogram.type* @llvm.dbg.subprogram to %0*))
+	%0 = bitcast %struct..0x* %b to %0*		; <%0*> [#uses=1]
+	call void @llvm.dbg.declare(%0* %0, %0* bitcast (%llvm.dbg.variable.type* @llvm.dbg.variable to %0*))
+	%1 = bitcast [4 x i32]* %a to %0*		; <%0*> [#uses=1]
+	call void @llvm.dbg.declare(%0* %1, %0* bitcast (%llvm.dbg.variable.type* @llvm.dbg.variable13 to %0*))
+	call void @llvm.dbg.stoppoint(i32 8, i32 0, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*))
+	%2 = getelementptr %struct..0x* %b, i32 0, i32 0		; <i32*> [#uses=1]
+	store i32 5, i32* %2, align 4
+	call void @llvm.dbg.stoppoint(i32 9, i32 0, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*))
+	%3 = load i32* @c.1442, align 4		; <i32> [#uses=1]
+	br label %return
+
+return:		; preds = %entry
+	call void @llvm.dbg.stoppoint(i32 9, i32 0, %0* bitcast (%llvm.dbg.compile_unit.type* @llvm.dbg.compile_unit to %0*))
+	call void @llvm.dbg.region.end(%0* bitcast (%llvm.dbg.subprogram.type* @llvm.dbg.subprogram to %0*))
+	ret i32 %3
+}
+
+declare void @llvm.dbg.func.start(%0*) nounwind readnone
+
+declare void @llvm.dbg.declare(%0*, %0*) nounwind readnone
+
+declare void @llvm.dbg.stoppoint(i32, i32, %0*) nounwind readnone
+
+declare void @llvm.dbg.region.end(%0*) nounwind readnone





More information about the llvm-commits mailing list