[llvm] r267393 - [GlobalOpt] Allow constant globals to be SRA'd

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 25 03:48:30 PDT 2016


Author: jamesm
Date: Mon Apr 25 05:48:29 2016
New Revision: 267393

URL: http://llvm.org/viewvc/llvm-project?rev=267393&view=rev
Log:
[GlobalOpt] Allow constant globals to be SRA'd

The current logic assumes that any constant global will never be SRA'd. I presume this is because normally constant globals can be pushed into their uses and deleted. However, that sometimes can't happen (which is where you really want SRA, so the elements that can be eliminated, are!).

There seems to be no reason why we can't SRA constants too, so let's do it.

Modified:
    llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
    llvm/trunk/test/Transforms/GlobalOpt/globalsra.ll

Modified: llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=267393&r1=267392&r2=267393&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Mon Apr 25 05:48:29 2016
@@ -474,7 +474,7 @@ static GlobalVariable *SRAGlobal(GlobalV
   if (!GlobalUsersSafeToSRA(GV))
     return nullptr;
 
-  assert(GV->hasLocalLinkage() && !GV->isConstant());
+  assert(GV->hasLocalLinkage());
   Constant *Init = GV->getInitializer();
   Type *Ty = Init->getType();
 
@@ -1928,7 +1928,8 @@ bool GlobalOpt::processInternalGlobal(Gl
     }
     return Changed;
 
-  } else if (GS.StoredType <= GlobalStatus::InitializerStored) {
+  }
+  if (GS.StoredType <= GlobalStatus::InitializerStored) {
     DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
     GV->setConstant(true);
 
@@ -1941,15 +1942,18 @@ bool GlobalOpt::processInternalGlobal(Gl
             << "all users and delete global!\n");
       GV->eraseFromParent();
       ++NumDeleted;
+      return true;
     }
 
+    // Fall through to the next check; see if we can optimize further.
     ++NumMarked;
-    return true;
-  } else if (!GV->getInitializer()->getType()->isSingleValueType()) {
+  }
+  if (!GV->getInitializer()->getType()->isSingleValueType()) {
     const DataLayout &DL = GV->getParent()->getDataLayout();
     if (SRAGlobal(GV, DL))
       return true;
-  } else if (GS.StoredType == GlobalStatus::StoredOnce && GS.StoredOnceValue) {
+  }
+  if (GS.StoredType == GlobalStatus::StoredOnce && GS.StoredOnceValue) {
     // If the initial value for the global was an undef value, and if only
     // one other value was stored into it, we can just change the
     // initializer to be the stored value, then delete all stores to the

Modified: llvm/trunk/test/Transforms/GlobalOpt/globalsra.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/globalsra.ll?rev=267393&r1=267392&r2=267393&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/globalsra.ll (original)
+++ llvm/trunk/test/Transforms/GlobalOpt/globalsra.ll Mon Apr 25 05:48:29 2016
@@ -22,3 +22,24 @@ define double @constantize() {
         ret double %X
 }
 
+ at G2 = internal constant { i32, float, { double } } {
+    i32 1, 
+    float 1.000000e+00, 
+    { double } { double 1.727000e+01 } }                ; <{ i32, float, { double } }*> [#uses=3]
+
+define void @onlystore2() {
+        store i32 123, i32* getelementptr ({ i32, float, { double } }, { i32, float, { double } }* @G2, i32 0, i32 0)
+        ret void
+}
+
+define float @storeinit2() {
+        store float 1.000000e+00, float* getelementptr ({ i32, float, { double } }, { i32, float, { double } }* @G2, i32 0, i32 1)
+        %X = load float, float* getelementptr ({ i32, float, { double } }, { i32, float, { double } }* @G2, i32 0, i32 1)           ; <float> [#uses=1]
+        ret float %X
+}
+
+define double @constantize2() {
+        %X = load double, double* getelementptr ({ i32, float, { double } }, { i32, float, { double } }* @G2, i32 0, i32 2, i32 0)           ; <double> [#uses=1]
+        ret double %X
+}
+




More information about the llvm-commits mailing list