Revert "[llvm] Improve llvm.objectsize computation by computing GEP, alloca and mallo...
[llvm-project.git] / clang / test / CodeGen / linux-kernel-struct-union-initializer2.c
blobdf53d194c6aad77cbf3d5d4e68d2f4a7221d7c85
1 // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals all --version 5
2 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=gnu11 -verify -emit-llvm %s -o - | FileCheck %s
3 // expected-no-diagnostics
5 union U1 {
6 int x;
7 char y[5];
8 };
10 struct S1 {
11 int x;
12 long long y;
15 struct S2 {
16 unsigned char b1 : 3; // 1st 3 bits (in 1st byte) are b1
17 unsigned char : 2; // next 2 bits (in 1st byte) are blocked out as unused
18 unsigned char b2 : 6; // 6 bits for b2 - doesn't fit into the 1st byte => starts a 2nd
19 unsigned char b3 : 2; // 2 bits for b3 - next (and final) bits in the 2nd byte
20 int i;
23 struct S3 {
24 int x;
25 } __attribute__((__aligned__(8)));
27 struct S4 {
28 int a;
29 union U1 b;
32 struct S5 {
33 char x;
34 unsigned char y : 4;
35 unsigned char z : 7;
36 } __attribute__((packed));
38 // Test non-const initializer for union with padding.
39 // CHECK-LABEL: define dso_local void @test1(
40 // CHECK-SAME: i32 noundef [[X:%.*]]) #[[ATTR0:[0-9]+]] {
41 // CHECK-NEXT: [[ENTRY:.*:]]
42 // CHECK-NEXT: [[X_ADDR:%.*]] = alloca i32, align 4
43 // CHECK-NEXT: [[A:%.*]] = alloca [[UNION_U1:%.*]], align 4
44 // CHECK-NEXT: store i32 [[X]], ptr [[X_ADDR]], align 4
45 // CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[X_ADDR]], align 4
46 // CHECK-NEXT: store i32 [[TMP0]], ptr [[A]], align 4
47 // CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[A]], i64 4
48 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[TMP1]], i8 0, i64 4, i1 false)
49 // CHECK-NEXT: ret void
51 void test1(int x) {
52 union U1 a = {x};
55 // Test non-const initializer for struct with padding.
56 // CHECK-LABEL: define dso_local void @test2(
57 // CHECK-SAME: i64 noundef [[Y:%.*]]) #[[ATTR0]] {
58 // CHECK-NEXT: [[ENTRY:.*:]]
59 // CHECK-NEXT: [[Y_ADDR:%.*]] = alloca i64, align 8
60 // CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S1:%.*]], align 8
61 // CHECK-NEXT: store i64 [[Y]], ptr [[Y_ADDR]], align 8
62 // CHECK-NEXT: [[X:%.*]] = getelementptr inbounds nuw [[STRUCT_S1]], ptr [[S]], i32 0, i32 0
63 // CHECK-NEXT: store i32 0, ptr [[X]], align 8
64 // CHECK-NEXT: [[TMP0:%.*]] = getelementptr i8, ptr [[S]], i64 4
65 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[TMP0]], i8 0, i64 4, i1 false)
66 // CHECK-NEXT: [[Y1:%.*]] = getelementptr inbounds nuw [[STRUCT_S1]], ptr [[S]], i32 0, i32 1
67 // CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[Y_ADDR]], align 8
68 // CHECK-NEXT: store i64 [[TMP1]], ptr [[Y1]], align 8
69 // CHECK-NEXT: ret void
71 void test2(long long y) {
72 struct S1 s = {.y = y};
75 // Test non-const initializer for struct with padding and bit fields.
76 // CHECK-LABEL: define dso_local void @test3(
77 // CHECK-SAME: i8 noundef zeroext [[B:%.*]]) #[[ATTR0]] {
78 // CHECK-NEXT: [[ENTRY:.*:]]
79 // CHECK-NEXT: [[B_ADDR:%.*]] = alloca i8, align 1
80 // CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S2:%.*]], align 4
81 // CHECK-NEXT: store i8 [[B]], ptr [[B_ADDR]], align 1
82 // CHECK-NEXT: store i16 0, ptr [[S]], align 4
83 // CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[B_ADDR]], align 1
84 // CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[TMP0]] to i16
85 // CHECK-NEXT: [[BF_LOAD:%.*]] = load i16, ptr [[S]], align 4
86 // CHECK-NEXT: [[BF_VALUE:%.*]] = and i16 [[TMP1]], 7
87 // CHECK-NEXT: [[BF_CLEAR:%.*]] = and i16 [[BF_LOAD]], -8
88 // CHECK-NEXT: [[BF_SET:%.*]] = or i16 [[BF_CLEAR]], [[BF_VALUE]]
89 // CHECK-NEXT: store i16 [[BF_SET]], ptr [[S]], align 4
90 // CHECK-NEXT: [[BF_LOAD1:%.*]] = load i16, ptr [[S]], align 4
91 // CHECK-NEXT: [[BF_CLEAR2:%.*]] = and i16 [[BF_LOAD1]], -16129
92 // CHECK-NEXT: [[BF_SET3:%.*]] = or i16 [[BF_CLEAR2]], 0
93 // CHECK-NEXT: store i16 [[BF_SET3]], ptr [[S]], align 4
94 // CHECK-NEXT: [[BF_LOAD4:%.*]] = load i16, ptr [[S]], align 4
95 // CHECK-NEXT: [[BF_CLEAR5:%.*]] = and i16 [[BF_LOAD4]], 16383
96 // CHECK-NEXT: [[BF_SET6:%.*]] = or i16 [[BF_CLEAR5]], 0
97 // CHECK-NEXT: store i16 [[BF_SET6]], ptr [[S]], align 4
98 // CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[S]], i64 2
99 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 2 [[TMP2]], i8 0, i64 2, i1 false)
100 // CHECK-NEXT: [[I:%.*]] = getelementptr inbounds nuw [[STRUCT_S2]], ptr [[S]], i32 0, i32 1
101 // CHECK-NEXT: store i32 0, ptr [[I]], align 4
102 // CHECK-NEXT: ret void
104 void test3(unsigned char b) {
105 struct S2 s = {.b1 = b};
108 // Test non-const initializer for struct with padding at the end of the struct.
109 // CHECK-LABEL: define dso_local void @test4(
110 // CHECK-SAME: i32 noundef [[X:%.*]]) #[[ATTR0]] {
111 // CHECK-NEXT: [[ENTRY:.*:]]
112 // CHECK-NEXT: [[X_ADDR:%.*]] = alloca i32, align 4
113 // CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S3:%.*]], align 8
114 // CHECK-NEXT: store i32 [[X]], ptr [[X_ADDR]], align 4
115 // CHECK-NEXT: [[X1:%.*]] = getelementptr inbounds nuw [[STRUCT_S3]], ptr [[S]], i32 0, i32 0
116 // CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[X_ADDR]], align 4
117 // CHECK-NEXT: store i32 [[TMP0]], ptr [[X1]], align 8
118 // CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[S]], i64 4
119 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[TMP1]], i8 0, i64 4, i1 false)
120 // CHECK-NEXT: ret void
122 void test4(int x) {
123 struct S3 s = {x};
126 // Test non-const initializer for union in struct.
127 // CHECK-LABEL: define dso_local void @test5(
128 // CHECK-SAME: i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) #[[ATTR0]] {
129 // CHECK-NEXT: [[ENTRY:.*:]]
130 // CHECK-NEXT: [[A_ADDR:%.*]] = alloca i32, align 4
131 // CHECK-NEXT: [[B_ADDR:%.*]] = alloca i32, align 4
132 // CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S4:%.*]], align 4
133 // CHECK-NEXT: store i32 [[A]], ptr [[A_ADDR]], align 4
134 // CHECK-NEXT: store i32 [[B]], ptr [[B_ADDR]], align 4
135 // CHECK-NEXT: [[A1:%.*]] = getelementptr inbounds nuw [[STRUCT_S4]], ptr [[S]], i32 0, i32 0
136 // CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A_ADDR]], align 4
137 // CHECK-NEXT: store i32 [[TMP0]], ptr [[A1]], align 4
138 // CHECK-NEXT: [[B2:%.*]] = getelementptr inbounds nuw [[STRUCT_S4]], ptr [[S]], i32 0, i32 1
139 // CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[B_ADDR]], align 4
140 // CHECK-NEXT: store i32 [[TMP1]], ptr [[B2]], align 4
141 // CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[B2]], i64 4
142 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[TMP2]], i8 0, i64 4, i1 false)
143 // CHECK-NEXT: ret void
145 void test5(int a, int b) {
146 struct S4 s = {a, {b}};
149 // CHECK-LABEL: define dso_local void @test6(
150 // CHECK-SAME: i8 noundef signext [[X:%.*]]) #[[ATTR0]] {
151 // CHECK-NEXT: [[ENTRY:.*:]]
152 // CHECK-NEXT: [[X_ADDR:%.*]] = alloca i8, align 1
153 // CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S5:%.*]], align 1
154 // CHECK-NEXT: store i8 [[X]], ptr [[X_ADDR]], align 1
155 // CHECK-NEXT: [[X1:%.*]] = getelementptr inbounds nuw [[STRUCT_S5]], ptr [[S]], i32 0, i32 0
156 // CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[X_ADDR]], align 1
157 // CHECK-NEXT: store i8 [[TMP0]], ptr [[X1]], align 1
158 // CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[S]], i64 1
159 // CHECK-NEXT: store i16 0, ptr [[TMP1]], align 1
160 // CHECK-NEXT: [[Y:%.*]] = getelementptr inbounds nuw [[STRUCT_S5]], ptr [[S]], i32 0, i32 1
161 // CHECK-NEXT: [[BF_LOAD:%.*]] = load i16, ptr [[Y]], align 1
162 // CHECK-NEXT: [[BF_CLEAR:%.*]] = and i16 [[BF_LOAD]], -16
163 // CHECK-NEXT: [[BF_SET:%.*]] = or i16 [[BF_CLEAR]], 0
164 // CHECK-NEXT: store i16 [[BF_SET]], ptr [[Y]], align 1
165 // CHECK-NEXT: [[Z:%.*]] = getelementptr inbounds nuw [[STRUCT_S5]], ptr [[S]], i32 0, i32 1
166 // CHECK-NEXT: [[BF_LOAD2:%.*]] = load i16, ptr [[Z]], align 1
167 // CHECK-NEXT: [[BF_CLEAR3:%.*]] = and i16 [[BF_LOAD2]], -2033
168 // CHECK-NEXT: [[BF_SET4:%.*]] = or i16 [[BF_CLEAR3]], 0
169 // CHECK-NEXT: store i16 [[BF_SET4]], ptr [[Z]], align 1
170 // CHECK-NEXT: ret void
172 void test6(char x) {
173 struct S5 s = {.x = x};
176 // CHECK: attributes #[[ATTR0]] = { noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
177 // CHECK: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: write) }
179 // CHECK: [[META0:![0-9]+]] = !{i32 1, !"wchar_size", i32 4}
180 // CHECK: [[META1:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}