]> granicus.if.org Git - clang/commitdiff
Correctly align large arrays in x86-64. This fixes PR5599.
authorRafael Espindola <rafael.espindola@gmail.com>
Fri, 4 Jun 2010 23:15:27 +0000 (23:15 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Fri, 4 Jun 2010 23:15:27 +0000 (23:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105500 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/TargetInfo.h
lib/AST/ASTContext.cpp
lib/Basic/TargetInfo.cpp
lib/Basic/Targets.cpp
test/CodeGen/const-arithmetic.c
test/CodeGenCXX/static-init.cpp
test/Sema/align-x86-64.c [new file with mode: 0644]

index 00fd9b92e13ec60d3fab94a36d975d1a80df798e..0cef286afef38699a0cd71839bbe4725392529ff 100644 (file)
@@ -51,6 +51,7 @@ protected:
   unsigned char FloatWidth, FloatAlign;
   unsigned char DoubleWidth, DoubleAlign;
   unsigned char LongDoubleWidth, LongDoubleAlign;
+  unsigned char LargeArrayMinWidth, LargeArrayAlign;
   unsigned char LongWidth, LongAlign;
   unsigned char LongLongWidth, LongLongAlign;
   const char *DescriptionString;
@@ -194,6 +195,11 @@ public:
     return *LongDoubleFormat;
   }
 
+  // getLargeArrayMinWidth/Align - Return the minimum array size that is
+  // 'large' and its alignment.
+  unsigned getLargeArrayMinWidth() const { return LargeArrayMinWidth; }
+  unsigned getLargeArrayAlign() const { return LargeArrayAlign; }
+
   /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this
   /// target, in bits.
   unsigned getIntMaxTWidth() const {
index 43bf21313f6c5cdec39161af4f02d233954b3e1b..6e7bb0773cf619c6948b1711aff0514d1ae0b612 100644 (file)
@@ -414,6 +414,15 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
         T = getPointerType(RT->getPointeeType());
     }
     if (!T->isIncompleteType() && !T->isFunctionType()) {
+      unsigned MinWidth = Target.getLargeArrayMinWidth();
+      unsigned ArrayAlign = Target.getLargeArrayAlign();
+      if (isa<VariableArrayType>(T) && MinWidth != 0)
+        Align = std::max(Align, ArrayAlign);
+      if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
+        unsigned Size = getTypeSize(CT);
+        if (MinWidth != 0 && MinWidth <= Size)
+          Align = std::max(Align, ArrayAlign);
+      }
       // Incomplete or function types default to 1.
       while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
         T = cast<ArrayType>(T)->getElementType();
index 6692e641f2a420d4073bb88a461180aea93d0ce3..98249c029bbda4c1169692d89fba125b135f92f8 100644 (file)
@@ -34,6 +34,8 @@ TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
   DoubleAlign = 64;
   LongDoubleWidth = 64;
   LongDoubleAlign = 64;
+  LargeArrayMinWidth = 0;
+  LargeArrayAlign = 0;
   SizeType = UnsignedLong;
   PtrDiffType = SignedLong;
   IntMaxType = SignedLongLong;
index 255c71d9b78a2d6d1df16e7ceb8145bc59ae9155..82c6507190b4cbeae8ca876c486c2ebbb7692cb0 100644 (file)
@@ -1278,6 +1278,8 @@ public:
     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
     LongDoubleWidth = 128;
     LongDoubleAlign = 128;
+    LargeArrayMinWidth = 128;
+    LargeArrayAlign = 128;
     IntMaxType = SignedLong;
     UIntMaxType = UnsignedLong;
     Int64Type = SignedLong;
index e12b4f6d92c1d5714540ff26c987d1202ebe8a7b..92c02f0b3dfb62fcfb87d0a9fe2e2bee1ce404ba 100644 (file)
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
 
-// CHECK: @g1 = global [2 x i8*] [i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -2), i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -46)], align 8 ; <[2 x i8*]*> [#uses=0]
-// CHECK: @g2 = global [2 x i8*] [i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -2), i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -46)], align 8 ; <[2 x i8*]*> [#uses=0]
+// CHECK: @g1 = global [2 x i8*] [i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -2), i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -46)], align 16 ; <[2 x i8*]*> [#uses=0]
+// CHECK: @g2 = global [2 x i8*] [i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -2), i8* getelementptr (i8* getelementptr inbounds ([0 x %struct.anon]* @g0, i32 0, i32 0, i32 0), i64 -46)], align 16 ; <[2 x i8*]*> [#uses=0]
 
 extern struct { unsigned char a, b; } g0[];
 void *g1[] = {g0 + -1, g0 + -23 };
index 9ad87df8f0c36289332aa3e76d6f9ec7d37cdff3..09b398a530a13f76bb273ce55e2018fb1d4fb0af 100644 (file)
@@ -2,7 +2,7 @@
 
 // CHECK: @_ZZ1hvE1i = internal global i32 0, align 4
 
-// CHECK: @_ZZN5test16getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 4
+// CHECK: @_ZZN5test16getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 16
 // CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0
 // CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0
 
diff --git a/test/Sema/align-x86-64.c b/test/Sema/align-x86-64.c
new file mode 100644 (file)
index 0000000..6dcf571
--- /dev/null
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify %s
+
+// PR5599
+
+void frob(void *);
+
+void foo(void) {
+  float x[4];
+  char y[__alignof__(x) == 16 ? 1 : -1];
+  frob(y);
+}