llvm-project/clang/test/CodeGen/packed-arrays.c

158 lines
3.9 KiB
C
Raw Normal View History

// 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 = local_unnamed_addr global i32 1
int align0 = __alignof(struct s0);
// CHECK: @align1 = local_unnamed_addr global i32 4
int align1 = __alignof(struct s1);
// CHECK: @align2 = local_unnamed_addr global i32 1
int align2 = __alignof(struct s2);
// CHECK: @align3 = local_unnamed_addr global i32 1
int align3 = __alignof(struct s3);
// CHECK: @align0_x = local_unnamed_addr global i32 1
int align0_x = __alignof(((struct s0*) 0)->x);
//
// CHECK: @align1_x = local_unnamed_addr global i32 1
int align1_x = __alignof(((struct s1*) 0)->x);
// CHECK: @align2_x = local_unnamed_addr global i32 1
int align2_x = __alignof(((struct s2*) 0)->x);
// CHECK: @align3_x = local_unnamed_addr global i32 1
int align3_x = __alignof(((struct s3*) 0)->x);
// CHECK: @align0_x0 = local_unnamed_addr global i32 4
int align0_x0 = __alignof(((struct s0*) 0)->x[0]);
// CHECK: @align1_x0 = local_unnamed_addr global i32 4
int align1_x0 = __alignof(((struct s1*) 0)->x[0]);
// CHECK: @align2_x0 = local_unnamed_addr global i32 4
int align2_x0 = __alignof(((struct s2*) 0)->x[0]);
// CHECK: @align3_x0 = local_unnamed_addr global i32 4
int align3_x0 = __alignof(((struct s3*) 0)->x[0]);
// CHECK-LABEL: define i32 @f0_a
// CHECK: load i32, i32* %{{.*}}, align 1
// CHECK: }
// CHECK-LABEL: define i32 @f0_b
// CHECK: load i32, i32* %{{.*}}, align 4
// CHECK: }
int f0_a(struct s0 *a) {
return a->x[1];
}
int f0_b(struct s0 *a) {
return *(a->x + 1);
}
Compute and preserve alignment more faithfully in IR-generation. Introduce an Address type to bundle a pointer value with an alignment. Introduce APIs on CGBuilderTy to work with Address values. Change core APIs on CGF/CGM to traffic in Address where appropriate. Require alignments to be non-zero. Update a ton of code to compute and propagate alignment information. As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment helper function to CGF and made use of it in a number of places in the expression emitter. The end result is that we should now be significantly more correct when performing operations on objects that are locally known to be under-aligned. Since alignment is not reliably tracked in the type system, there are inherent limits to this, but at least we are no longer confused by standard operations like derived-to-base conversions and array-to-pointer decay. I've also fixed a large number of bugs where we were applying the complete-object alignment to a pointer instead of the non-virtual alignment, although most of these were hidden by the very conservative approach we took with member alignment. Also, because IRGen now reliably asserts on zero alignments, we should no longer be subject to an absurd but frustrating recurring bug where an incomplete type would report a zero alignment and then we'd naively do a alignmentAtOffset on it and emit code using an alignment equal to the largest power-of-two factor of the offset. We should also now be emitting much more aggressive alignment attributes in the presence of over-alignment. In particular, field access now uses alignmentAtOffset instead of min. Several times in this patch, I had to change the existing code-generation pattern in order to more effectively use the Address APIs. For the most part, this seems to be a strict improvement, like doing pointer arithmetic with GEPs instead of ptrtoint. That said, I've tried very hard to not change semantics, but it is likely that I've failed in a few places, for which I apologize. ABIArgInfo now always carries the assumed alignment of indirect and indirect byval arguments. In order to cut down on what was already a dauntingly large patch, I changed the code to never set align attributes in the IR on non-byval indirect arguments. That is, we still generate code which assumes that indirect arguments have the given alignment, but we don't express this information to the backend except where it's semantically required (i.e. on byvals). This is likely a minor regression for those targets that did provide this information, but it'll be trivial to add it back in a later patch. I partially punted on applying this work to CGBuiltin. Please do not add more uses of the CreateDefaultAligned{Load,Store} APIs; they will be going away eventually. llvm-svn: 246985
2015-09-08 16:05:57 +08:00
// Note that 'y' still causes struct s1 to be four-byte aligned.
// Note that we are incompatible with GCC on this example.
//
// CHECK-LABEL: define i32 @f1_a
Compute and preserve alignment more faithfully in IR-generation. Introduce an Address type to bundle a pointer value with an alignment. Introduce APIs on CGBuilderTy to work with Address values. Change core APIs on CGF/CGM to traffic in Address where appropriate. Require alignments to be non-zero. Update a ton of code to compute and propagate alignment information. As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment helper function to CGF and made use of it in a number of places in the expression emitter. The end result is that we should now be significantly more correct when performing operations on objects that are locally known to be under-aligned. Since alignment is not reliably tracked in the type system, there are inherent limits to this, but at least we are no longer confused by standard operations like derived-to-base conversions and array-to-pointer decay. I've also fixed a large number of bugs where we were applying the complete-object alignment to a pointer instead of the non-virtual alignment, although most of these were hidden by the very conservative approach we took with member alignment. Also, because IRGen now reliably asserts on zero alignments, we should no longer be subject to an absurd but frustrating recurring bug where an incomplete type would report a zero alignment and then we'd naively do a alignmentAtOffset on it and emit code using an alignment equal to the largest power-of-two factor of the offset. We should also now be emitting much more aggressive alignment attributes in the presence of over-alignment. In particular, field access now uses alignmentAtOffset instead of min. Several times in this patch, I had to change the existing code-generation pattern in order to more effectively use the Address APIs. For the most part, this seems to be a strict improvement, like doing pointer arithmetic with GEPs instead of ptrtoint. That said, I've tried very hard to not change semantics, but it is likely that I've failed in a few places, for which I apologize. ABIArgInfo now always carries the assumed alignment of indirect and indirect byval arguments. In order to cut down on what was already a dauntingly large patch, I changed the code to never set align attributes in the IR on non-byval indirect arguments. That is, we still generate code which assumes that indirect arguments have the given alignment, but we don't express this information to the backend except where it's semantically required (i.e. on byvals). This is likely a minor regression for those targets that did provide this information, but it'll be trivial to add it back in a later patch. I partially punted on applying this work to CGBuiltin. Please do not add more uses of the CreateDefaultAligned{Load,Store} APIs; they will be going away eventually. llvm-svn: 246985
2015-09-08 16:05:57 +08:00
// CHECK: load i32, i32* %{{.*}}, align 4
// CHECK: }
// CHECK-LABEL: define i32 @f1_b
// CHECK: load i32, i32* %{{.*}}, align 4
// CHECK: }
// Note that we are incompatible with GCC on this example.
//
// CHECK-LABEL: define i32 @f1_c
// CHECK: load i32, i32* %{{.*}}, align 4
// CHECK: }
// CHECK-LABEL: define i32 @f1_d
Compute and preserve alignment more faithfully in IR-generation. Introduce an Address type to bundle a pointer value with an alignment. Introduce APIs on CGBuilderTy to work with Address values. Change core APIs on CGF/CGM to traffic in Address where appropriate. Require alignments to be non-zero. Update a ton of code to compute and propagate alignment information. As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment helper function to CGF and made use of it in a number of places in the expression emitter. The end result is that we should now be significantly more correct when performing operations on objects that are locally known to be under-aligned. Since alignment is not reliably tracked in the type system, there are inherent limits to this, but at least we are no longer confused by standard operations like derived-to-base conversions and array-to-pointer decay. I've also fixed a large number of bugs where we were applying the complete-object alignment to a pointer instead of the non-virtual alignment, although most of these were hidden by the very conservative approach we took with member alignment. Also, because IRGen now reliably asserts on zero alignments, we should no longer be subject to an absurd but frustrating recurring bug where an incomplete type would report a zero alignment and then we'd naively do a alignmentAtOffset on it and emit code using an alignment equal to the largest power-of-two factor of the offset. We should also now be emitting much more aggressive alignment attributes in the presence of over-alignment. In particular, field access now uses alignmentAtOffset instead of min. Several times in this patch, I had to change the existing code-generation pattern in order to more effectively use the Address APIs. For the most part, this seems to be a strict improvement, like doing pointer arithmetic with GEPs instead of ptrtoint. That said, I've tried very hard to not change semantics, but it is likely that I've failed in a few places, for which I apologize. ABIArgInfo now always carries the assumed alignment of indirect and indirect byval arguments. In order to cut down on what was already a dauntingly large patch, I changed the code to never set align attributes in the IR on non-byval indirect arguments. That is, we still generate code which assumes that indirect arguments have the given alignment, but we don't express this information to the backend except where it's semantically required (i.e. on byvals). This is likely a minor regression for those targets that did provide this information, but it'll be trivial to add it back in a later patch. I partially punted on applying this work to CGBuiltin. Please do not add more uses of the CreateDefaultAligned{Load,Store} APIs; they will be going away eventually. llvm-svn: 246985
2015-09-08 16:05:57 +08:00
// CHECK: load i32, 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-LABEL: define i32 @f2_a
// CHECK: load i32, i32* %{{.*}}, align 1
// CHECK: }
// CHECK-LABEL: define i32 @f2_b
// CHECK: load i32, i32* %{{.*}}, align 4
// CHECK: }
// CHECK-LABEL: define i32 @f2_c
// CHECK: load i32, i32* %{{.*}}, align 1
// CHECK: }
// CHECK-LABEL: define i32 @f2_d
// CHECK: load i32, 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-LABEL: define i32 @f3_a
// CHECK: load i32, i32* %{{.*}}, align 1
// CHECK: }
// CHECK-LABEL: define i32 @f3_b
// CHECK: load i32, i32* %{{.*}}, align 4
// CHECK: }
// CHECK-LABEL: define i32 @f3_c
// CHECK: load i32, i32* %{{.*}}, align 1
// CHECK: }
// CHECK-LABEL: define i32 @f3_d
// CHECK: load i32, 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;
}
// Verify we don't claim things are overaligned.
//
// CHECK-LABEL: define double @f4
// CHECK: load double, double* {{.*}}, align 8
// CHECK: }
extern double g4[5] __attribute__((aligned(16)));
double f4() {
return g4[1];
}