Implement GNU asm-label extension support in CodeGen. This fixes
scimark2 on Darwin.

 - Added Sema support for asm-label on variables, which I forgot before.

 - Update CodeGen to use GlobalDeclMap to determine if static Decls
   require emission (instead of LLVM module name lookup). Important
   since the Decl name and the LLVM module name can differ.

 - <rdar://problem/6116729>


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54388 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index 176576a..ebe75dd 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -184,6 +184,12 @@
   if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>())
     CodeGenModule::setVisibility(GV, attr->getVisibility());
   // FIXME: else handle -fvisibility
+
+  if (const AsmLabelAttr *ALA = FD->getAttr<AsmLabelAttr>()) {
+    // Prefaced with special LLVM marker to indicate that the name
+    // should not be munged.
+    GV->setName("\01" + ALA->getLabel());
+  }
 }
 
 void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD,
@@ -400,13 +406,8 @@
       // Check if we have used a decl with the same name
       // FIXME: The AST should have some sort of aggregate decls or
       // global symbol map.
-      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-        if (!getModule().getFunction(FD->getName()))
-          continue;
-      } else {
-        if (!getModule().getNamedGlobal(cast<VarDecl>(D)->getName()))
-          continue;
-      }
+      if (!GlobalDeclMap.count(D->getName()))
+        continue;
 
       // Emit the definition.
       EmitGlobalDefinition(D);
@@ -620,6 +621,12 @@
   if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
     setVisibility(GV, attr->getVisibility());
   // FIXME: else handle -fvisibility
+
+  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
+    // Prefaced with special LLVM marker to indicate that the name
+    // should not be munged.
+    GV->setName("\01" + ALA->getLabel());
+  }
   
   // Set the llvm linkage type as appropriate.
   if (D->getStorageClass() == VarDecl::Static)
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 19a2c28..b513dde 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -808,6 +808,14 @@
     // Handle attributes prior to checking for duplicates in MergeVarDecl
     ProcessDeclAttributes(NewVD, D);
 
+    // Handle GNU asm-label extension (encoded as an attribute).
+    if (Expr *E = (Expr*) D.getAsmLabel()) {
+      // The parser guarantees this is a string.
+      StringLiteral *SE = cast<StringLiteral>(E);  
+      NewVD->addAttr(new AsmLabelAttr(std::string(SE->getStrData(),
+                                                  SE->getByteLength())));
+    }
+
     // Emit an error if an address space was applied to decl with local storage.
     // This includes arrays of objects with address space qualifiers, but not
     // automatic variables that point to other address spaces.