[llvm-commits] [llvm] r100160 - in /llvm/trunk: lib/Transforms/IPO/GlobalOpt.cpp test/Transforms/GlobalOpt/gv-align.ll

Dan Gohman gohman at apple.com
Thu Apr 1 17:14:16 PDT 2010


Author: djg
Date: Thu Apr  1 19:14:16 2010
New Revision: 100160

URL: http://llvm.org/viewvc/llvm-project?rev=100160&view=rev
Log:
Make globalopt refine global variable alignment.

Added:
    llvm/trunk/test/Transforms/GlobalOpt/gv-align.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=100160&r1=100159&r2=100160&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Thu Apr  1 19:14:16 2010
@@ -1880,6 +1880,7 @@
 
 bool GlobalOpt::OptimizeGlobalVars(Module &M) {
   bool Changed = false;
+  TargetData *TD = getAnalysisIfAvailable<TargetData>();
   for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
        GVI != E; ) {
     GlobalVariable *GV = GVI++;
@@ -1889,11 +1890,16 @@
     // Simplify the initializer.
     if (GV->hasInitializer())
       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
-        TargetData *TD = getAnalysisIfAvailable<TargetData>();
         Constant *New = ConstantFoldConstantExpression(CE, TD);
         if (New && New != CE)
           GV->setInitializer(New);
       }
+    // Refine the alignment value.
+    if (TD && GV->hasDefinitiveInitializer()) {
+      unsigned Align = TD->getPreferredAlignment(GV);
+      if (Align > GV->getAlignment())
+        GV->setAlignment(Align);
+    }
     // Do more involved optimizations if the global is internal.
     if (!GV->isConstant() && GV->hasLocalLinkage() &&
         GV->hasInitializer())

Added: llvm/trunk/test/Transforms/GlobalOpt/gv-align.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/gv-align.ll?rev=100160&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/gv-align.ll (added)
+++ llvm/trunk/test/Transforms/GlobalOpt/gv-align.ll Thu Apr  1 19:14:16 2010
@@ -0,0 +1,6 @@
+; RUN: opt -globalopt -S < %s | grep {, align 16}
+
+; Globalopt should refine the alignment for global variables.
+
+target datalayout = "e-p:64:64:64"
+ at a = global [4 x i32] [i32 2, i32 3, i32 4, i32 5 ]





More information about the llvm-commits mailing list