[llvm-commits] [gcc-plugin] r83455 - in /gcc-plugin/trunk: llvm-convert.cpp llvm-debug.cpp llvm-debug.h

Duncan Sands baldrick at free.fr
Wed Oct 7 01:46:22 PDT 2009


Author: baldrick
Date: Wed Oct  7 03:46:21 2009
New Revision: 83455

URL: http://llvm.org/viewvc/llvm-project?rev=83455&view=rev
Log:
Give LLVM values corresponding to GCC SSA names the same name as they would
get in a gcc dump file.  Because this fattens up the bitcode considerably it
is only turned on if the -fverbose-asm flag is passed to GCC.

Modified:
    gcc-plugin/trunk/llvm-convert.cpp
    gcc-plugin/trunk/llvm-debug.cpp
    gcc-plugin/trunk/llvm-debug.h

Modified: gcc-plugin/trunk/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/gcc-plugin/trunk/llvm-convert.cpp?rev=83455&r1=83454&r2=83455&view=diff

==============================================================================
--- gcc-plugin/trunk/llvm-convert.cpp (original)
+++ gcc-plugin/trunk/llvm-convert.cpp Wed Oct  7 03:46:21 2009
@@ -155,6 +155,51 @@
   return align ? align : 1;
 }
 
+/// NameValue - Try to name the given value after the given GCC tree node.  If
+/// the GCC tree node has no sensible name then it does nothing.  If the value
+/// already has a name then it is not changed.
+static void NameValue(Value *V, tree t, Twine Prefix = Twine(),
+                      Twine Postfix = Twine()) {
+  // If the value already has a name, do not change it.
+  if (V->hasName())
+    return;
+
+  // No sensible name - give up, discarding any pre- and post-fixes.
+  if (!t)
+    return;
+
+  switch (TREE_CODE(t)) {
+  default:
+    // Unhandled case - give up.
+    return;
+
+  case CONST_DECL:
+  case FIELD_DECL:
+  case FUNCTION_DECL:
+  case NAMESPACE_DECL:
+  case PARM_DECL:
+  case VAR_DECL: {
+    if (DECL_NAME(t)) {
+      V->setName(Prefix + IDENTIFIER_POINTER(DECL_NAME(t)) + Postfix);
+      return;
+    }
+    const char *Annotation = TREE_CODE(t) == CONST_DECL ? "C." : "D.";
+    Twine UID(DECL_UID(t));
+    V->setName(Prefix + Annotation + UID + Postfix);
+    return;
+  }
+
+  case RESULT_DECL:
+    V->setName(Prefix + "<retval>" + Postfix);
+    return;
+
+  case SSA_NAME:
+    Twine NameVersion(SSA_NAME_VERSION(t));
+    NameValue(V, SSA_NAME_VAR(t), Prefix, "_" + NameVersion + Postfix);
+    return;
+  }
+}
+
 //===----------------------------------------------------------------------===//
 //                         ... High-Level Methods ...
 //===----------------------------------------------------------------------===//
@@ -956,6 +1001,8 @@
     assert(TREE_CODE(name) == SSA_NAME && "PHI result not an SSA name!");
     assert(SSANames.find(name) == SSANames.end() &&
            "Multiply defined SSA name!");
+    if (flag_verbose_asm)
+      NameValue(PHI, name);
     SSANames[name] = PHI;
 
     // The phi operands will be populated later - remember the phi node.
@@ -1695,22 +1742,13 @@
       Alignment = DECL_ALIGN(decl) / 8;
   }
 
-  const char *Name;      // Name of variable
-  if (DECL_NAME(decl))
-    Name = IDENTIFIER_POINTER(DECL_NAME(decl));
-  else if (TREE_CODE(decl) == RESULT_DECL)
-    Name = "retval";
-  else
-    Name = "";
-
   // Insert an alloca for this variable.
   AllocaInst *AI;
-  if (!Size) {                           // Fixed size alloca -> entry block.
+  if (!Size)                             // Fixed size alloca -> entry block.
     AI = CreateTemporary(Ty);
-    AI->setName(Name);
-  } else {
-    AI = Builder.CreateAlloca(Ty, Size, Name);
-  }
+  else
+    AI = Builder.CreateAlloca(Ty, Size);
+  NameValue(AI, decl);
 
   AI->setAlignment(Alignment);
 
@@ -1734,11 +1772,11 @@
   if (TheDebugInfo) {
     if (DECL_NAME(decl)) {
       TheDebugInfo->EmitDeclare(decl, dwarf::DW_TAG_auto_variable,
-                                Name, TREE_TYPE(decl), AI,
+                                AI->getName(), TREE_TYPE(decl), AI,
                                 Builder.GetInsertBlock());
     } else if (TREE_CODE(decl) == RESULT_DECL) {
       TheDebugInfo->EmitDeclare(decl, dwarf::DW_TAG_return_variable,
-                                Name, TREE_TYPE(decl), AI,
+                                AI->getName(), TREE_TYPE(decl), AI,
                                 Builder.GetInsertBlock());
     }
   }
@@ -2136,21 +2174,19 @@
   unsigned Alignment = DECL_ALIGN(var);
   assert(Alignment != 0 && "Parameter with unknown alignment!");
 
-  const char *ParameterName =
-    DECL_NAME(var) ? IDENTIFIER_POINTER(DECL_NAME(var)) : "anon";
-
   const Type *Ty = ConvertType(TREE_TYPE(reg));
 
   // Perform the load in the entry block, after all parameters have been set up
   // with their initial values, and before any modifications to their values.
-  LoadInst *LI = new LoadInst(DECL_LOCAL_IF_SET(var), ParameterName,
-                              SSAInsertionPoint);
+  LoadInst *LI = new LoadInst(DECL_LOCAL_IF_SET(var), "", SSAInsertionPoint);
   LI->setAlignment(Alignment);
 
   // Potentially perform a useless type conversion (useless_type_conversion_p).
   Value *Def = LI;
   if (LI->getType() != Ty)
     Def = new BitCastInst(Def, Ty, "", SSAInsertionPoint);
+  if (flag_verbose_asm)
+    NameValue(Def, reg);
   return SSANames[reg] = Def;
 }
 
@@ -8050,6 +8086,8 @@
   // If this is the definition of an ssa name, record it in the SSANames map.
   if (TREE_CODE(lhs) == SSA_NAME) {
     assert(SSANames.find(lhs) == SSANames.end() &&"Multiply defined SSA name!");
+    if (flag_verbose_asm)
+      NameValue(RHS, lhs);
     SSANames[lhs] = RHS;
     return;
   }

Modified: gcc-plugin/trunk/llvm-debug.cpp
URL: http://llvm.org/viewvc/llvm-project/gcc-plugin/trunk/llvm-debug.cpp?rev=83455&r1=83454&r2=83455&view=diff

==============================================================================
--- gcc-plugin/trunk/llvm-debug.cpp (original)
+++ gcc-plugin/trunk/llvm-debug.cpp Wed Oct  7 03:46:21 2009
@@ -309,7 +309,7 @@
 
 /// EmitDeclare - Constructs the debug code for allocation of a new variable.
 /// region - "llvm.dbg.declare."
-void DebugInfo::EmitDeclare(tree decl, unsigned Tag, const char *Name,
+void DebugInfo::EmitDeclare(tree decl, unsigned Tag, StringRef Name,
                             tree type, Value *AI, BasicBlock *CurBB) {
 
   // Do not emit variable declaration info, for now.

Modified: gcc-plugin/trunk/llvm-debug.h
URL: http://llvm.org/viewvc/llvm-project/gcc-plugin/trunk/llvm-debug.h?rev=83455&r1=83454&r2=83455&view=diff

==============================================================================
--- gcc-plugin/trunk/llvm-debug.h (original)
+++ gcc-plugin/trunk/llvm-debug.h Wed Oct  7 03:46:21 2009
@@ -90,7 +90,7 @@
 
   /// EmitDeclare - Constructs the debug code for allocation of a new variable.
   /// region - "llvm.dbg.declare."
-  void EmitDeclare(tree_node *decl, unsigned Tag, const char *Name,
+  void EmitDeclare(tree_node *decl, unsigned Tag, StringRef Name,
                    tree_node *type, Value *AI,
                    BasicBlock *CurBB);
 





More information about the llvm-commits mailing list