[cfe-commits] r128691 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp test/CodeGen/packed-arrays.c

Daniel Dunbar daniel at zuster.org
Thu Mar 31 16:32:15 PDT 2011


Author: ddunbar
Date: Thu Mar 31 18:32:15 2011
New Revision: 128691

URL: http://llvm.org/viewvc/llvm-project?rev=128691&view=rev
Log:
IRgen: Improve GCC compatibility when dealing with packed arrays by propagating
the array alignment to the array access.
 - This is more or less the best we can do without having alignment present in
   the type system, but is a long way from truly matching how GCC handles this.

Added:
    cfe/trunk/test/CodeGen/packed-arrays.c
Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=128691&r1=128690&r2=128691&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Thu Mar 31 18:32:15 2011
@@ -1424,6 +1424,7 @@
   // We know that the pointer points to a type of the correct size, unless the
   // size is a VLA or Objective-C interface.
   llvm::Value *Address = 0;
+  unsigned ArrayAlignment = 0;
   if (const VariableArrayType *VAT =
         getContext().getAsVariableArrayType(E->getType())) {
     llvm::Value *VLASize = GetVLASize(VAT);
@@ -1459,10 +1460,14 @@
     // "gep x, i" here.  Emit one "gep A, 0, i".
     assert(Array->getType()->isArrayType() &&
            "Array to pointer decay must have array source type!");
-    llvm::Value *ArrayPtr = EmitLValue(Array).getAddress();
+    LValue ArrayLV = EmitLValue(Array);
+    llvm::Value *ArrayPtr = ArrayLV.getAddress();
     llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
     llvm::Value *Args[] = { Zero, Idx };
     
+    // Propagate the alignment from the array itself to the result.
+    ArrayAlignment = ArrayLV.getAlignment();
+
     if (getContext().getLangOptions().isSignedOverflowDefined())
       Address = Builder.CreateGEP(ArrayPtr, Args, Args+2, "arrayidx");
     else
@@ -1480,7 +1485,7 @@
   assert(!T.isNull() &&
          "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
 
-  LValue LV = MakeAddrLValue(Address, T);
+  LValue LV = MakeAddrLValue(Address, T, ArrayAlignment);
   LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
 
   if (getContext().getLangOptions().ObjC1 &&

Added: cfe/trunk/test/CodeGen/packed-arrays.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/packed-arrays.c?rev=128691&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/packed-arrays.c (added)
+++ cfe/trunk/test/CodeGen/packed-arrays.c Thu Mar 31 18:32:15 2011
@@ -0,0 +1,147 @@
+// RUN: %clang_cc1 -triple x86_64-unk-unk -emit-llvm -Os -o %t %s
+// RUN: FileCheck < %t %s
+
+struct s0 {
+  unsigned int x[2] __attribute__((packed));
+};
+
+struct s1 {
+  unsigned int x[2] __attribute__((packed));
+  unsigned int y;
+  unsigned int z __attribute__((packed));
+};
+
+struct s2 {
+  unsigned int x[2] __attribute__((packed));
+  unsigned int y __attribute__((packed));
+  unsigned int z __attribute__((packed));
+};
+
+struct __attribute__((packed)) s3 {
+  unsigned int x[2];
+  unsigned int y;
+  unsigned int z;
+};
+
+// CHECK: @align0 = global i32 1
+int align0 = __alignof(struct s0);
+// CHECK: @align1 = global i32 4
+int align1 = __alignof(struct s1);
+// CHECK: @align2 = global i32 1
+int align2 = __alignof(struct s2);
+// CHECK: @align3 = global i32 1
+int align3 = __alignof(struct s3);
+
+// CHECK: @align0_x = global i32 1
+int align0_x = __alignof(((struct s0*) 0)->x);
+// We are currently incompatible with GCC here. <rdar://problem/9217290>
+//
+// CHECK-XFAIL: @align1_x = global i32 1
+// CHECK: @align1_x = global i32 4
+int align1_x = __alignof(((struct s1*) 0)->x);
+// CHECK: @align2_x = global i32 1
+int align2_x = __alignof(((struct s2*) 0)->x);
+// CHECK: @align3_x = global i32 1
+int align3_x = __alignof(((struct s3*) 0)->x);
+
+// CHECK: @align0_x0 = global i32 4
+int align0_x0 = __alignof(((struct s0*) 0)->x[0]);
+// CHECK: @align1_x0 = global i32 4
+int align1_x0 = __alignof(((struct s1*) 0)->x[0]);
+// CHECK: @align2_x0 = global i32 4
+int align2_x0 = __alignof(((struct s2*) 0)->x[0]);
+// CHECK: @align3_x0 = global i32 4
+int align3_x0 = __alignof(((struct s3*) 0)->x[0]);
+
+// CHECK: define i32 @f0_a
+// CHECK:   load i32* %{{.*}}, align 1
+// CHECK: }
+// CHECK: define i32 @f0_b
+// CHECK:   load i32* %{{.*}}, align 4
+// CHECK: }
+int f0_a(struct s0 *a) {
+  return a->x[1];
+}
+int f0_b(struct s0 *a) {
+  return *(a->x + 1);
+}
+
+// CHECK: define i32 @f1_a
+// CHECK:   load i32* %{{.*}}, align 4
+// CHECK: }
+// CHECK: define i32 @f1_b
+// CHECK:   load i32* %{{.*}}, align 4
+// CHECK: }
+
+// Note that we are incompatible with GCC on these two examples.
+//
+// CHECK: define i32 @f1_c
+// CHECK-XFAIL:   load i32* %{{.*}}, align 1
+// CHECK:   load i32* %{{.*}}, align 4
+// CHECK: }
+// CHECK: define i32 @f1_d
+// CHECK-XFAIL:   load i32* %{{.*}}, align 1
+// CHECK:   load i32* %{{.*}}, align 4
+// CHECK: }
+int f1_a(struct s1 *a) {
+  return a->x[1];
+}
+int f1_b(struct s1 *a) {
+  return *(a->x + 1);
+}
+int f1_c(struct s1 *a) {
+  return a->y;
+}
+int f1_d(struct s1 *a) {
+  return a->z;
+}
+
+// CHECK: define i32 @f2_a
+// CHECK:   load i32* %{{.*}}, align 1
+// CHECK: }
+// CHECK: define i32 @f2_b
+// CHECK:   load i32* %{{.*}}, align 4
+// CHECK: }
+// CHECK: define i32 @f2_c
+// CHECK:   load i32* %{{.*}}, align 1
+// CHECK: }
+// CHECK: define i32 @f2_d
+// CHECK:   load i32* %{{.*}}, align 1
+// CHECK: }
+int f2_a(struct s2 *a) {
+  return a->x[1];
+}
+int f2_b(struct s2 *a) {
+  return *(a->x + 1);
+}
+int f2_c(struct s2 *a) {
+  return a->y;
+}
+int f2_d(struct s2 *a) {
+  return a->z;
+}
+
+// CHECK: define i32 @f3_a
+// CHECK:   load i32* %{{.*}}, align 1
+// CHECK: }
+// CHECK: define i32 @f3_b
+// CHECK:   load i32* %{{.*}}, align 4
+// CHECK: }
+// CHECK: define i32 @f3_c
+// CHECK:   load i32* %{{.*}}, align 1
+// CHECK: }
+// CHECK: define i32 @f3_d
+// CHECK:   load i32* %{{.*}}, align 1
+// CHECK: }
+int f3_a(struct s3 *a) {
+  return a->x[1];
+}
+int f3_b(struct s3 *a) {
+  return *(a->x + 1);
+}
+int f3_c(struct s3 *a) {
+  return a->y;
+}
+int f3_d(struct s3 *a) {
+  return a->z;
+}





More information about the cfe-commits mailing list