[llvm-commits] [llvm] r50309 - in /llvm/branches/Apple/Tak: lib/Transforms/IPO/GlobalOpt.cpp test/Transforms/GlobalOpt/2008-04-26-SROA-Global-Align.ll

Bill Wendling isanbard at gmail.com
Sat Apr 26 02:30:01 PDT 2008


Author: void
Date: Sat Apr 26 04:30:01 2008
New Revision: 50309

URL: http://llvm.org/viewvc/llvm-project?rev=50309&view=rev
Log:
Pulled r50308 into Tak.

Added:
    llvm/branches/Apple/Tak/test/Transforms/GlobalOpt/2008-04-26-SROA-Global-Align.ll
Modified:
    llvm/branches/Apple/Tak/lib/Transforms/IPO/GlobalOpt.cpp

Modified: llvm/branches/Apple/Tak/lib/Transforms/IPO/GlobalOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Tak/lib/Transforms/IPO/GlobalOpt.cpp?rev=50309&r1=50308&r2=50309&view=diff

==============================================================================
--- llvm/branches/Apple/Tak/lib/Transforms/IPO/GlobalOpt.cpp (original)
+++ llvm/branches/Apple/Tak/lib/Transforms/IPO/GlobalOpt.cpp Sat Apr 26 04:30:01 2008
@@ -28,6 +28,7 @@
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
+#include "llvm/Support/MathExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
@@ -455,7 +456,7 @@
 /// behavior of the program in a more fine-grained way.  We have determined that
 /// this transformation is safe already.  We return the first global variable we
 /// insert so that the caller can reprocess it.
-static GlobalVariable *SRAGlobal(GlobalVariable *GV) {
+static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) {
   // Make sure this global only has simple uses that we can SRA.
   if (!GlobalUsersSafeToSRA(GV))
     return 0;
@@ -467,8 +468,14 @@
   std::vector<GlobalVariable*> NewGlobals;
   Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
 
+  // Get the alignment of the global, either explicit or target-specific.
+  unsigned StartAlignment = GV->getAlignment();
+  if (StartAlignment == 0)
+    StartAlignment = TD.getABITypeAlignment(GV->getType());
+   
   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
     NewGlobals.reserve(STy->getNumElements());
+    const StructLayout &Layout = *TD.getStructLayout(STy);
     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
       Constant *In = getAggregateConstantElement(Init,
                                             ConstantInt::get(Type::Int32Ty, i));
@@ -480,19 +487,28 @@
                                                GV->isThreadLocal());
       Globals.insert(GV, NGV);
       NewGlobals.push_back(NGV);
+      
+      // Calculate the known alignment of the field.  If the original aggregate
+      // had 256 byte alignment for example, something might depend on that:
+      // propagate info to each field.
+      uint64_t FieldOffset = Layout.getElementOffset(i);
+      unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
+      if (NewAlign > TD.getABITypeAlignment(STy->getElementType(i)))
+        NGV->setAlignment(NewAlign);
     }
   } else if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
     unsigned NumElements = 0;
     if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
       NumElements = ATy->getNumElements();
-    else if (const VectorType *PTy = dyn_cast<VectorType>(STy))
-      NumElements = PTy->getNumElements();
     else
-      assert(0 && "Unknown aggregate sequential type!");
+      NumElements = cast<VectorType>(STy)->getNumElements();
 
     if (NumElements > 16 && GV->hasNUsesOrMore(16))
       return 0; // It's not worth it.
     NewGlobals.reserve(NumElements);
+    
+    uint64_t EltSize = TD.getABITypeSize(STy->getElementType());
+    unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
     for (unsigned i = 0, e = NumElements; i != e; ++i) {
       Constant *In = getAggregateConstantElement(Init,
                                             ConstantInt::get(Type::Int32Ty, i));
@@ -505,6 +521,13 @@
                                                GV->isThreadLocal());
       Globals.insert(GV, NGV);
       NewGlobals.push_back(NGV);
+      
+      // Calculate the known alignment of the field.  If the original aggregate
+      // had 256 byte alignment for example, something might depend on that:
+      // propagate info to each field.
+      unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i);
+      if (NewAlign > EltAlign)
+        NGV->setAlignment(NewAlign);
     }
   }
 
@@ -804,6 +827,9 @@
                                              GV->getName()+".body",
                                              (Module *)NULL,
                                              GV->isThreadLocal());
+  // FIXME: This new global should have the alignment returned by malloc.  Code
+  // could depend on malloc returning large alignment (on the mac, 16 bytes) but
+  // this would only guarantee some lower alignment.
   GV->getParent()->getGlobalList().insert(GV, NewGV);
 
   // Anything that used the malloc now uses the global directly.
@@ -1520,7 +1546,8 @@
       ++NumMarked;
       return true;
     } else if (!GV->getInitializer()->getType()->isFirstClassType()) {
-      if (GlobalVariable *FirstNewGV = SRAGlobal(GV)) {
+      if (GlobalVariable *FirstNewGV = SRAGlobal(GV, 
+                                                 getAnalysis<TargetData>())) {
         GVI = FirstNewGV;  // Don't skip the newly produced globals!
         return true;
       }

Added: llvm/branches/Apple/Tak/test/Transforms/GlobalOpt/2008-04-26-SROA-Global-Align.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Tak/test/Transforms/GlobalOpt/2008-04-26-SROA-Global-Align.ll?rev=50309&view=auto

==============================================================================
--- llvm/branches/Apple/Tak/test/Transforms/GlobalOpt/2008-04-26-SROA-Global-Align.ll (added)
+++ llvm/branches/Apple/Tak/test/Transforms/GlobalOpt/2008-04-26-SROA-Global-Align.ll Sat Apr 26 04:30:01 2008
@@ -0,0 +1,32 @@
+; Verify that when @G is SROA'd that the new globals have correct 
+; alignments.  Elements 0 and 2 must be 16-byte aligned, and element 
+; 1 must be at least 8 byte aligned (but could be more). 
+
+; RUN: llvm-as < %s | opt -globalopt | llvm-dis | grep {@G.0 = internal global .*align 16}
+; RUN: llvm-as < %s | opt -globalopt | llvm-dis | grep {@G.1 = internal global .*align 8}
+; RUN: llvm-as < %s | opt -globalopt | llvm-dis | grep {@G.2 = internal global .*align 16}
+; rdar://5891920
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
+target triple = "x86_64-apple-darwin8"
+
+%T = type { double, double, double }
+
+ at G = internal global %T zeroinitializer, align 16
+
+
+define void @test() {
+  store double 1.0, double* getelementptr (%T* @G, i32 0, i32 0), align 16
+  store double 2.0, double* getelementptr (%T* @G, i32 0, i32 1), align 8
+  store double 3.0, double* getelementptr (%T* @G, i32 0, i32 2), align 16
+  ret void
+}
+
+define double @test2() {
+  %V1 = load double* getelementptr (%T* @G, i32 0, i32 0), align 16
+  %V2 = load double* getelementptr (%T* @G, i32 0, i32 1), align 8
+  %V3 = load double* getelementptr (%T* @G, i32 0, i32 2), align 16
+  %R = add double %V1, %V2
+  %R2 = add double %R, %V3
+  ret double %R2
+}





More information about the llvm-commits mailing list