1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64 -emit-llvm -o - %s | opt -S -strip -o %t
2*f4a2713aSLionel Sambuc // RUN: FileCheck --check-prefix=CHECK-GLOBAL < %t %s
3*f4a2713aSLionel Sambuc // RUN: FileCheck --check-prefix=CHECK-FUNCTIONS < %t %s
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc struct s0 {
6*f4a2713aSLionel Sambuc   int x;
7*f4a2713aSLionel Sambuc   int y __attribute__((packed));
8*f4a2713aSLionel Sambuc };
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s0_align_x = global i32 4
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s0_align_y = global i32 1
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s0_align = global i32 4
15*f4a2713aSLionel Sambuc int s0_align_x = __alignof(((struct s0*)0)->x);
16*f4a2713aSLionel Sambuc int s0_align_y = __alignof(((struct s0*)0)->y);
17*f4a2713aSLionel Sambuc int s0_align   = __alignof(struct s0);
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define i32 @s0_load_x
20*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: [[s0_load_x:%.*]] = load i32* {{.*}}, align 4
21*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: ret i32 [[s0_load_x]]
s0_load_x(struct s0 * a)22*f4a2713aSLionel Sambuc int s0_load_x(struct s0 *a) { return a->x; }
23*f4a2713aSLionel Sambuc // FIXME: This seems like it should be align 1. This is actually something which
24*f4a2713aSLionel Sambuc // has changed in llvm-gcc recently, previously both x and y would be loaded
25*f4a2713aSLionel Sambuc // with align 1 (in 2363.1 at least).
26*f4a2713aSLionel Sambuc //
27*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define i32 @s0_load_y
28*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: [[s0_load_y:%.*]] = load i32* {{.*}}, align 1
29*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: ret i32 [[s0_load_y]]
s0_load_y(struct s0 * a)30*f4a2713aSLionel Sambuc int s0_load_y(struct s0 *a) { return a->y; }
31*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define void @s0_copy
32*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: call void @llvm.memcpy.p0i8.p0i8.i64(i8* {{.*}}, i8* {{.*}}, i64 8, i32 4, i1 false)
s0_copy(struct s0 * a,struct s0 * b)33*f4a2713aSLionel Sambuc void s0_copy(struct s0 *a, struct s0 *b) { *b = *a; }
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc //
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc struct s1 {
38*f4a2713aSLionel Sambuc   int x;
39*f4a2713aSLionel Sambuc   int y;
40*f4a2713aSLionel Sambuc } __attribute__((packed));
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s1_align_x = global i32 1
43*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s1_align_y = global i32 1
44*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s1_align = global i32 1
45*f4a2713aSLionel Sambuc int s1_align_x = __alignof(((struct s1*)0)->x);
46*f4a2713aSLionel Sambuc int s1_align_y = __alignof(((struct s1*)0)->y);
47*f4a2713aSLionel Sambuc int s1_align   = __alignof(struct s1);
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define i32 @s1_load_x
50*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: [[s1_load_x:%.*]] = load i32* {{.*}}, align 1
51*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: ret i32 [[s1_load_x]]
s1_load_x(struct s1 * a)52*f4a2713aSLionel Sambuc int s1_load_x(struct s1 *a) { return a->x; }
53*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define i32 @s1_load_y
54*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: [[s1_load_y:%.*]] = load i32* {{.*}}, align 1
55*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: ret i32 [[s1_load_y]]
s1_load_y(struct s1 * a)56*f4a2713aSLionel Sambuc int s1_load_y(struct s1 *a) { return a->y; }
57*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define void @s1_copy
58*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: call void @llvm.memcpy.p0i8.p0i8.i64(i8* {{.*}}, i8* {{.*}}, i64 8, i32 1, i1 false)
s1_copy(struct s1 * a,struct s1 * b)59*f4a2713aSLionel Sambuc void s1_copy(struct s1 *a, struct s1 *b) { *b = *a; }
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc //
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc #pragma pack(push,2)
64*f4a2713aSLionel Sambuc struct s2 {
65*f4a2713aSLionel Sambuc   int x;
66*f4a2713aSLionel Sambuc   int y;
67*f4a2713aSLionel Sambuc };
68*f4a2713aSLionel Sambuc #pragma pack(pop)
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s2_align_x = global i32 2
71*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s2_align_y = global i32 2
72*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s2_align = global i32 2
73*f4a2713aSLionel Sambuc int s2_align_x = __alignof(((struct s2*)0)->x);
74*f4a2713aSLionel Sambuc int s2_align_y = __alignof(((struct s2*)0)->y);
75*f4a2713aSLionel Sambuc int s2_align   = __alignof(struct s2);
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define i32 @s2_load_x
78*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: [[s2_load_y:%.*]] = load i32* {{.*}}, align 2
79*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: ret i32 [[s2_load_y]]
s2_load_x(struct s2 * a)80*f4a2713aSLionel Sambuc int s2_load_x(struct s2 *a) { return a->x; }
81*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define i32 @s2_load_y
82*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: [[s2_load_y:%.*]] = load i32* {{.*}}, align 2
83*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: ret i32 [[s2_load_y]]
s2_load_y(struct s2 * a)84*f4a2713aSLionel Sambuc int s2_load_y(struct s2 *a) { return a->y; }
85*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define void @s2_copy
86*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS: call void @llvm.memcpy.p0i8.p0i8.i64(i8* {{.*}}, i8* {{.*}}, i64 8, i32 2, i1 false)
s2_copy(struct s2 * a,struct s2 * b)87*f4a2713aSLionel Sambuc void s2_copy(struct s2 *a, struct s2 *b) { *b = *a; }
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc struct __attribute__((packed, aligned)) s3 {
90*f4a2713aSLionel Sambuc   short aShort;
91*f4a2713aSLionel Sambuc   int anInt;
92*f4a2713aSLionel Sambuc };
93*f4a2713aSLionel Sambuc // CHECK-GLOBAL: @s3_1 = global i32 1
94*f4a2713aSLionel Sambuc int s3_1 = __alignof(((struct s3*) 0)->anInt);
95*f4a2713aSLionel Sambuc // CHECK-FUNCTIONS-LABEL: define i32 @test3(
test3(struct s3 * ptr)96*f4a2713aSLionel Sambuc int test3(struct s3 *ptr) {
97*f4a2713aSLionel Sambuc   // CHECK-FUNCTIONS:      [[PTR:%.*]] = getelementptr inbounds {{%.*}}* {{%.*}}, i32 0, i32 1
98*f4a2713aSLionel Sambuc   // CHECK-FUNCTIONS-NEXT: load i32* [[PTR]], align 1
99*f4a2713aSLionel Sambuc   return ptr->anInt;
100*f4a2713aSLionel Sambuc }
101