[llvm-commits] [llvm] r66267 - in /llvm/trunk: lib/Transforms/IPO/GlobalOpt.cpp test/Transforms/GlobalOpt/2009-03-06-Anonymous.ll

Duncan Sands baldrick at free.fr
Fri Mar 6 02:22:06 PST 2009


Author: baldrick
Date: Fri Mar  6 04:21:56 2009
New Revision: 66267

URL: http://llvm.org/viewvc/llvm-project?rev=66267&view=rev
Log:
While thinking about the one-definition-rule and trying
to find a tiny mouse hole to squeeze through, it struck
me that globals without a name can be considered internal
since they can't be referenced from outside the current
module.  This patch makes GlobalOpt give them internal
linkage.  Also done for aliases even though they always
have names, since in my opinion anonymous aliases should
be allowed for consistency with global variables and
functions.  So if that happens one day, this code is ready!

Added:
    llvm/trunk/test/Transforms/GlobalOpt/2009-03-06-Anonymous.ll
Modified:
    llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp

Modified: llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=66267&r1=66266&r2=66267&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Fri Mar  6 04:21:56 2009
@@ -67,7 +67,7 @@
     GlobalVariable *FindGlobalCtors(Module &M);
     bool OptimizeFunctions(Module &M);
     bool OptimizeGlobalVars(Module &M);
-    bool ResolveAliases(Module &M);
+    bool OptimizeGlobalAliases(Module &M);
     bool OptimizeGlobalCtorsList(GlobalVariable *&GCL);
     bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI);
   };
@@ -1808,6 +1808,9 @@
   // Optimize functions.
   for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
     Function *F = FI++;
+    // Functions without names cannot be referenced outside this module.
+    if (!F->hasName() && !F->isDeclaration())
+      F->setLinkage(GlobalValue::InternalLinkage);
     F->removeDeadConstantUsers();
     if (F->use_empty() && (F->hasLocalLinkage() ||
                            F->hasLinkOnceLinkage())) {
@@ -1844,6 +1847,9 @@
   for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
        GVI != E; ) {
     GlobalVariable *GV = GVI++;
+    // Global variables without names cannot be referenced outside this module.
+    if (!GV->hasName() && !GV->isDeclaration())
+      GV->setLinkage(GlobalValue::InternalLinkage);
     if (!GV->isConstant() && GV->hasLocalLinkage() &&
         GV->hasInitializer())
       Changed |= ProcessInternalGlobal(GV, GVI);
@@ -2371,12 +2377,15 @@
   return true;
 }
 
-bool GlobalOpt::ResolveAliases(Module &M) {
+bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
   bool Changed = false;
 
   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
        I != E;) {
     Module::alias_iterator J = I++;
+    // Aliases without names cannot be referenced outside this module.
+    if (!J->hasName() && !J->isDeclaration())
+      J->setLinkage(GlobalValue::InternalLinkage);
     // If the aliasee may change at link time, nothing can be done - bail out.
     if (J->mayBeOverridden())
       continue;
@@ -2447,7 +2456,7 @@
     LocalChange |= OptimizeGlobalVars(M);
 
     // Resolve aliases, when possible.
-    LocalChange |= ResolveAliases(M);
+    LocalChange |= OptimizeGlobalAliases(M);
     Changed |= LocalChange;
   }
   

Added: llvm/trunk/test/Transforms/GlobalOpt/2009-03-06-Anonymous.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/2009-03-06-Anonymous.ll?rev=66267&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/2009-03-06-Anonymous.ll (added)
+++ llvm/trunk/test/Transforms/GlobalOpt/2009-03-06-Anonymous.ll Fri Mar  6 04:21:56 2009
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | opt -globalopt | llvm-dis | grep internal | count 2
+
+global i32 0
+define i32* @1() {
+	ret i32* @0
+}
+define i32* @f() {
+entry:
+	call i32* @1()
+	ret i32* %0
+}





More information about the llvm-commits mailing list