[llvm-commits] [128567] Add file and line number to annotate attribute.

lattner at apple.com lattner at apple.com
Mon Jun 18 15:30:01 PDT 2007


Revision: 128567
Author:   lattner
Date:     2007-06-18 15:30:01 -0700 (Mon, 18 Jun 2007)

Log Message:
-----------
Add file and line number to annotate attribute.

Modified Paths:
--------------
    apple-local/branches/llvm/gcc/llvm-backend.cpp
    apple-local/branches/llvm/gcc/llvm-convert.cpp
    apple-local/branches/llvm/gcc/llvm-internal.h

Modified: apple-local/branches/llvm/gcc/llvm-backend.cpp
===================================================================
--- apple-local/branches/llvm/gcc/llvm-backend.cpp	2007-06-18 22:03:55 UTC (rev 128566)
+++ apple-local/branches/llvm/gcc/llvm-backend.cpp	2007-06-18 22:30:01 UTC (rev 128567)
@@ -81,7 +81,7 @@
 std::vector<std::pair<Function*, int> > StaticCtors, StaticDtors;
 std::vector<Constant*> AttributeUsedGlobals;
 std::vector<Constant*> AttributeNoinlineFunctions;
-std::vector<std::pair<Constant*, Constant*> > AttributeAnnotateGlobals;
+std::vector<Constant*> AttributeAnnotateGlobals;
 
 /// PerFunctionPasses - This is the list of cleanup passes run per-function
 /// as each is compiled.  In cases where we are not doing IPO, it includes the 
@@ -492,21 +492,16 @@
   
   // Add llvm.global.annotations
   if (!AttributeAnnotateGlobals.empty()) {
-    std::vector<Constant*> AttrList;
     
-    for (unsigned i = 0, e = AttributeAnnotateGlobals.size(); i != e; ++i) {
-      Constant *Elts[2] = {AttributeAnnotateGlobals[i].first,
-        AttributeAnnotateGlobals[i].second };
-      AttrList.push_back(ConstantStruct::get(Elts, 2, false));
-    }
-    
     Constant *Array =
-      ConstantArray::get(ArrayType::get(AttrList[0]->getType(), AttrList.size()),
-                         AttrList);
+    ConstantArray::get(ArrayType::get(AttributeAnnotateGlobals[0]->getType(), 
+                                      AttributeAnnotateGlobals.size()),
+                       AttributeAnnotateGlobals);
     GlobalValue *gv = new GlobalVariable(Array->getType(), false, 
                                          GlobalValue::AppendingLinkage, Array, 
                                          "llvm.global.annotations", TheModule); 
     gv->setSection("llvm.metadata");
+    AttributeAnnotateGlobals.clear();
   
   }
   
@@ -671,15 +666,39 @@
   return;
 }
 
+// Convert string to global value. Use existing global if possible.
+Constant* ConvertMetadataStringToGV(const char *str) {
+  
+  Constant *Init = ConstantArray::get(std::string(str));
+
+  // Use cached string if it exists.
+  static std::map<Constant*, GlobalVariable*> StringCSTCache;
+  GlobalVariable *&Slot = StringCSTCache[Init];
+  if (Slot) return Slot;
+  
+  // Create a new string global.
+  GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
+                                          GlobalVariable::InternalLinkage,
+                                          Init, ".str", TheModule);
+  GV->setSection("llvm.metadata");
+  Slot = GV;
+  return GV;
+  
+}
+
 /// AddAnnotateAttrsToGlobal - Adds decls that have a
 /// annotate attribute to a vector to be emitted later.
 void AddAnnotateAttrsToGlobal(GlobalValue *GV, tree decl) {
   
   // Handle annotate attribute on global.
   tree annotateAttr = lookup_attribute("annotate", DECL_ATTRIBUTES (decl));
-  if (!annotateAttr)
-    return;
   
+  // Get file and line number
+ Constant *lineNo = ConstantInt::get(Type::Int32Ty, DECL_SOURCE_LINE(decl));
+ Constant *file = ConvertMetadataStringToGV(DECL_SOURCE_FILE(decl));
+ const Type *SBP= PointerType::get(Type::Int8Ty);
+ file = ConstantExpr::getBitCast(file, SBP);
+ 
   // There may be multiple annotate attributes. Pass return of lookup_attr 
   //  to successive lookups.
   while (annotateAttr) {
@@ -698,10 +717,12 @@
       assert(TREE_CODE(val) == STRING_CST && 
              "Annotate attribute arg should always be a string");
       Constant *strGV = TreeConstantToLLVM::EmitLV_STRING_CST(val);
-      const Type *SBP= PointerType::get(Type::Int8Ty);
-      AttributeAnnotateGlobals.push_back(
-                      std::make_pair(ConstantExpr::getBitCast(GV,SBP),
-                                     ConstantExpr::getBitCast(strGV,SBP)));
+      Constant *Element[4] = {ConstantExpr::getBitCast(GV,SBP),
+        ConstantExpr::getBitCast(strGV,SBP),
+        file,
+        lineNo};
+ 
+      AttributeAnnotateGlobals.push_back(ConstantStruct::get(Element, 4, false));
     }
       
     // Get next annotate attribute.

Modified: apple-local/branches/llvm/gcc/llvm-convert.cpp
===================================================================
--- apple-local/branches/llvm/gcc/llvm-convert.cpp	2007-06-18 22:03:55 UTC (rev 128566)
+++ apple-local/branches/llvm/gcc/llvm-convert.cpp	2007-06-18 22:30:01 UTC (rev 128567)
@@ -1411,6 +1411,12 @@
 
   Function *annotateFun = Intrinsic::getDeclaration(TheModule, 
                                                     Intrinsic::var_annotation);
+
+  // Get file and line number
+  Constant *lineNo = ConstantInt::get(Type::Int32Ty, DECL_SOURCE_LINE(decl));
+  Constant *file = ConvertMetadataStringToGV(DECL_SOURCE_FILE(decl));
+  const Type *SBP= PointerType::get(Type::Int8Ty);
+  file = ConstantExpr::getBitCast(file, SBP);
   
   // There may be multiple annotate attributes. Pass return of lookup_attr 
   //  to successive lookups.
@@ -1431,12 +1437,14 @@
              "Annotate attribute arg should always be a string");
       const Type *SBP = PointerType::get(Type::Int8Ty);
       Constant *strGV = TreeConstantToLLVM::EmitLV_STRING_CST(val);
-      Value *Ops[2] = {
+      Value *Ops[4] = {
         BitCastToType(V, SBP),
-        BitCastToType(strGV, SBP)
+        BitCastToType(strGV, SBP),
+        file, 
+        lineNo
       };
       
-      Builder.CreateCall(annotateFun, Ops, 2);
+      Builder.CreateCall(annotateFun, Ops, 4);
     }
     
     // Get next annotate attribute.

Modified: apple-local/branches/llvm/gcc/llvm-internal.h
===================================================================
--- apple-local/branches/llvm/gcc/llvm-internal.h	2007-06-18 22:03:55 UTC (rev 128566)
+++ apple-local/branches/llvm/gcc/llvm-internal.h	2007-06-18 22:30:01 UTC (rev 128567)
@@ -92,6 +92,8 @@
 /// marked attribute(noinline)
 extern std::vector<Constant*> AttributeNoinlineFunctions;
 
+extern Constant* ConvertMetadataStringToGV(const char* str);
+
 /// AddAnnotateAttrsToGlobal - Adds decls that have a
 /// annotate attribute to a vector to be emitted later.
 extern void AddAnnotateAttrsToGlobal(GlobalValue *GV, union tree_node* decl);
@@ -611,6 +613,7 @@
   static Constant *EmitLV_STRING_CST(tree_node *exp);
   static Constant *EmitLV_COMPONENT_REF(tree_node *exp);
   static Constant *EmitLV_ARRAY_REF(tree_node *exp);
+  
 };
 
 #endif





More information about the llvm-commits mailing list