xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/packed-arrays.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-unk-unk -emit-llvm -Os -o %t %s
2*f4a2713aSLionel Sambuc // RUN: FileCheck < %t %s
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc struct s0 {
5*f4a2713aSLionel Sambuc   unsigned int x[2] __attribute__((packed));
6*f4a2713aSLionel Sambuc };
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc struct s1 {
9*f4a2713aSLionel Sambuc   unsigned int x[2] __attribute__((packed));
10*f4a2713aSLionel Sambuc   unsigned int y;
11*f4a2713aSLionel Sambuc   unsigned int z __attribute__((packed));
12*f4a2713aSLionel Sambuc };
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc struct s2 {
15*f4a2713aSLionel Sambuc   unsigned int x[2] __attribute__((packed));
16*f4a2713aSLionel Sambuc   unsigned int y __attribute__((packed));
17*f4a2713aSLionel Sambuc   unsigned int z __attribute__((packed));
18*f4a2713aSLionel Sambuc };
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc struct __attribute__((packed)) s3 {
21*f4a2713aSLionel Sambuc   unsigned int x[2];
22*f4a2713aSLionel Sambuc   unsigned int y;
23*f4a2713aSLionel Sambuc   unsigned int z;
24*f4a2713aSLionel Sambuc };
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc // CHECK: @align0 = global i32 1
27*f4a2713aSLionel Sambuc int align0 = __alignof(struct s0);
28*f4a2713aSLionel Sambuc // CHECK: @align1 = global i32 4
29*f4a2713aSLionel Sambuc int align1 = __alignof(struct s1);
30*f4a2713aSLionel Sambuc // CHECK: @align2 = global i32 1
31*f4a2713aSLionel Sambuc int align2 = __alignof(struct s2);
32*f4a2713aSLionel Sambuc // CHECK: @align3 = global i32 1
33*f4a2713aSLionel Sambuc int align3 = __alignof(struct s3);
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc // CHECK: @align0_x = global i32 1
36*f4a2713aSLionel Sambuc int align0_x = __alignof(((struct s0*) 0)->x);
37*f4a2713aSLionel Sambuc //
38*f4a2713aSLionel Sambuc // CHECK: @align1_x = global i32 1
39*f4a2713aSLionel Sambuc int align1_x = __alignof(((struct s1*) 0)->x);
40*f4a2713aSLionel Sambuc // CHECK: @align2_x = global i32 1
41*f4a2713aSLionel Sambuc int align2_x = __alignof(((struct s2*) 0)->x);
42*f4a2713aSLionel Sambuc // CHECK: @align3_x = global i32 1
43*f4a2713aSLionel Sambuc int align3_x = __alignof(((struct s3*) 0)->x);
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc // CHECK: @align0_x0 = global i32 4
46*f4a2713aSLionel Sambuc int align0_x0 = __alignof(((struct s0*) 0)->x[0]);
47*f4a2713aSLionel Sambuc // CHECK: @align1_x0 = global i32 4
48*f4a2713aSLionel Sambuc int align1_x0 = __alignof(((struct s1*) 0)->x[0]);
49*f4a2713aSLionel Sambuc // CHECK: @align2_x0 = global i32 4
50*f4a2713aSLionel Sambuc int align2_x0 = __alignof(((struct s2*) 0)->x[0]);
51*f4a2713aSLionel Sambuc // CHECK: @align3_x0 = global i32 4
52*f4a2713aSLionel Sambuc int align3_x0 = __alignof(((struct s3*) 0)->x[0]);
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f0_a
55*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
56*f4a2713aSLionel Sambuc // CHECK: }
57*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f0_b
58*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 4
59*f4a2713aSLionel Sambuc // CHECK: }
f0_a(struct s0 * a)60*f4a2713aSLionel Sambuc int f0_a(struct s0 *a) {
61*f4a2713aSLionel Sambuc   return a->x[1];
62*f4a2713aSLionel Sambuc }
f0_b(struct s0 * a)63*f4a2713aSLionel Sambuc int f0_b(struct s0 *a) {
64*f4a2713aSLionel Sambuc   return *(a->x + 1);
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc // Note that we are incompatible with GCC on this example.
68*f4a2713aSLionel Sambuc //
69*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f1_a
70*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
71*f4a2713aSLionel Sambuc // CHECK: }
72*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f1_b
73*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 4
74*f4a2713aSLionel Sambuc // CHECK: }
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc // Note that we are incompatible with GCC on this example.
77*f4a2713aSLionel Sambuc //
78*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f1_c
79*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 4
80*f4a2713aSLionel Sambuc // CHECK: }
81*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f1_d
82*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
83*f4a2713aSLionel Sambuc // CHECK: }
f1_a(struct s1 * a)84*f4a2713aSLionel Sambuc int f1_a(struct s1 *a) {
85*f4a2713aSLionel Sambuc   return a->x[1];
86*f4a2713aSLionel Sambuc }
f1_b(struct s1 * a)87*f4a2713aSLionel Sambuc int f1_b(struct s1 *a) {
88*f4a2713aSLionel Sambuc   return *(a->x + 1);
89*f4a2713aSLionel Sambuc }
f1_c(struct s1 * a)90*f4a2713aSLionel Sambuc int f1_c(struct s1 *a) {
91*f4a2713aSLionel Sambuc   return a->y;
92*f4a2713aSLionel Sambuc }
f1_d(struct s1 * a)93*f4a2713aSLionel Sambuc int f1_d(struct s1 *a) {
94*f4a2713aSLionel Sambuc   return a->z;
95*f4a2713aSLionel Sambuc }
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f2_a
98*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
99*f4a2713aSLionel Sambuc // CHECK: }
100*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f2_b
101*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 4
102*f4a2713aSLionel Sambuc // CHECK: }
103*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f2_c
104*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
105*f4a2713aSLionel Sambuc // CHECK: }
106*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f2_d
107*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
108*f4a2713aSLionel Sambuc // CHECK: }
f2_a(struct s2 * a)109*f4a2713aSLionel Sambuc int f2_a(struct s2 *a) {
110*f4a2713aSLionel Sambuc   return a->x[1];
111*f4a2713aSLionel Sambuc }
f2_b(struct s2 * a)112*f4a2713aSLionel Sambuc int f2_b(struct s2 *a) {
113*f4a2713aSLionel Sambuc   return *(a->x + 1);
114*f4a2713aSLionel Sambuc }
f2_c(struct s2 * a)115*f4a2713aSLionel Sambuc int f2_c(struct s2 *a) {
116*f4a2713aSLionel Sambuc   return a->y;
117*f4a2713aSLionel Sambuc }
f2_d(struct s2 * a)118*f4a2713aSLionel Sambuc int f2_d(struct s2 *a) {
119*f4a2713aSLionel Sambuc   return a->z;
120*f4a2713aSLionel Sambuc }
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f3_a
123*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
124*f4a2713aSLionel Sambuc // CHECK: }
125*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f3_b
126*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 4
127*f4a2713aSLionel Sambuc // CHECK: }
128*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f3_c
129*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
130*f4a2713aSLionel Sambuc // CHECK: }
131*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f3_d
132*f4a2713aSLionel Sambuc // CHECK:   load i32* %{{.*}}, align 1
133*f4a2713aSLionel Sambuc // CHECK: }
f3_a(struct s3 * a)134*f4a2713aSLionel Sambuc int f3_a(struct s3 *a) {
135*f4a2713aSLionel Sambuc   return a->x[1];
136*f4a2713aSLionel Sambuc }
f3_b(struct s3 * a)137*f4a2713aSLionel Sambuc int f3_b(struct s3 *a) {
138*f4a2713aSLionel Sambuc   return *(a->x + 1);
139*f4a2713aSLionel Sambuc }
f3_c(struct s3 * a)140*f4a2713aSLionel Sambuc int f3_c(struct s3 *a) {
141*f4a2713aSLionel Sambuc   return a->y;
142*f4a2713aSLionel Sambuc }
f3_d(struct s3 * a)143*f4a2713aSLionel Sambuc int f3_d(struct s3 *a) {
144*f4a2713aSLionel Sambuc   return a->z;
145*f4a2713aSLionel Sambuc }
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc // Verify we don't claim things are overaligned.
148*f4a2713aSLionel Sambuc //
149*f4a2713aSLionel Sambuc // CHECK-LABEL: define double @f4
150*f4a2713aSLionel Sambuc // CHECK:   load double* {{.*}}, align 8
151*f4a2713aSLionel Sambuc // CHECK: }
152*f4a2713aSLionel Sambuc extern double g4[5] __attribute__((aligned(16)));
f4()153*f4a2713aSLionel Sambuc double f4() {
154*f4a2713aSLionel Sambuc   return g4[1];
155*f4a2713aSLionel Sambuc }
156