Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGen / init.c
blobcbf615bb9ddfea111ee9fab3aa5d51f6fa0b815d
1 // RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck %s
3 struct I { int k[3]; };
4 struct M { struct I o[2]; };
5 struct M v1[1] = { [0].o[0 ... 1].k[0 ... 1] = 4, 5 };
6 unsigned v2[2][3] = {[0 ... 1][0 ... 1] = 2222, 3333};
8 // CHECK-DAG: %struct.M = type { [2 x %struct.I] }
9 // CHECK-DAG: %struct.I = type { [3 x i32] }
11 // CHECK-DAG: [1 x %struct.M] [%struct.M { [2 x %struct.I] [%struct.I { [3 x i32] [i32 4, i32 4, i32 0] }, %struct.I { [3 x i32] [i32 4, i32 4, i32 5] }] }],
12 // CHECK-DAG: [2 x [3 x i32]] {{[[][[]}}3 x i32] [i32 2222, i32 2222, i32 0], [3 x i32] [i32 2222, i32 2222, i32 3333]],
13 // CHECK-DAG: [[INIT14:.*]] = private constant [16 x i32] [i32 0, i32 0, i32 0, i32 0, i32 0, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 0, i32 0, i32 0, i32 0], align 4
15 void f1(void) {
16 // Scalars in braces.
17 int a = { 1 };
20 void f2(void) {
21 int a[2][2] = { { 1, 2 }, { 3, 4 } };
22 int b[3][3] = { { 1, 2 }, { 3, 4 } };
23 int *c[2] = { &a[1][1], &b[2][2] };
24 int *d[2][2] = { {&a[1][1], &b[2][2]}, {&a[0][0], &b[1][1]} };
25 int *e[3][3] = { {&a[1][1], &b[2][2]}, {&a[0][0], &b[1][1]} };
26 char ext[3][3] = {".Y",".U",".V"};
29 typedef void (* F)(void);
30 extern void foo(void);
31 struct S { F f; };
32 void f3(void) {
33 struct S a[1] = { { foo } };
36 // Constants
37 // CHECK-DAG: @g3 ={{.*}} constant i32 10
38 // CHECK-DAG: @f4.g4 = internal constant i32 12
39 const int g3 = 10;
40 int f4(void) {
41 static const int g4 = 12;
42 return g4;
45 // PR6537
46 typedef union vec3 {
47 struct { double x, y, z; };
48 double component[3];
49 } vec3;
50 vec3 f5(vec3 value) {
51 return (vec3) {{
52 .x = value.x
53 }};
56 void f6(void) {
57 int x;
58 long ids[] = { (long) &x };
64 // CHECK-DAG: @test7 ={{.*}} global{{.*}}{ i32 0, [4 x i8] c"bar\00" }
65 // PR8217
66 struct a7 {
67 int b;
68 char v[];
71 struct a7 test7 = { .b = 0, .v = "bar" };
74 // CHECK-DAG: @huge_array ={{.*}} global {{.*}} <{ i32 1, i32 0, i32 2, i32 0, i32 3, [999999995 x i32] zeroinitializer }>
75 int huge_array[1000000000] = {1, 0, 2, 0, 3, 0, 0, 0};
77 // CHECK-DAG: @huge_struct ={{.*}} global {{.*}} { i32 1, <{ i32, [999999999 x i32] }> <{ i32 2, [999999999 x i32] zeroinitializer }> }
78 struct Huge {
79 int a;
80 int arr[1000 * 1000 * 1000];
81 } huge_struct = {1, {2, 0, 0, 0}};
83 // CHECK-DAG: @large_array_with_zeroes ={{.*}} constant <{ [21 x i8], [979 x i8] }> <{ [21 x i8] c"abc\01\02\03xyzzy\00\00\00\00\00\00\00\00\00q", [979 x i8] zeroinitializer }>
84 const char large_array_with_zeroes[1000] = {
85 'a', 'b', 'c', 1, 2, 3, 'x', 'y', 'z', 'z', 'y', [20] = 'q'
88 char global;
90 // CHECK-DAG: @large_array_with_zeroes_2 ={{.*}} global <{ [10 x ptr], [90 x ptr] }> <{ [10 x ptr] [ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr @global], [90 x ptr] zeroinitializer }>
91 const void *large_array_with_zeroes_2[100] = {
92 [9] = &global
94 // CHECK-DAG: @large_array_with_zeroes_3 ={{.*}} global <{ [10 x ptr], [990 x ptr] }> <{ [10 x ptr] [ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr @global], [990 x ptr] zeroinitializer }>
95 const void *large_array_with_zeroes_3[1000] = {
96 [9] = &global
99 // PR279 comment #3
100 char test8(int X) {
101 char str[100000] = "abc"; // tail should be memset.
102 return str[X];
103 // CHECK-LABEL: @test8(
104 // CHECK: call void @llvm.memset
105 // CHECK: store i8 97, ptr %{{[0-9]*}}, align 1
106 // CHECK: store i8 98, ptr %{{[0-9]*}}, align 1
107 // CHECK: store i8 99, ptr %{{[0-9]*}}, align 1
108 // CHECK-NOT: getelementptr
109 // CHECK: load
112 void bar(void*);
114 // PR279
115 void test9(int X) {
116 int Arr[100] = { X }; // Should use memset
117 bar(Arr);
118 // CHECK-LABEL: @test9(
119 // CHECK: call void @llvm.memset
120 // CHECK-NOT: store i32 0
121 // CHECK: call void @bar
124 struct a {
125 int a, b, c, d, e, f, g, h, i, j, k, *p;
128 struct b {
129 struct a a,b,c,d,e,f,g;
132 void test10(int X) {
133 struct b S = { .a.a = X, .d.e = X, .f.e = 0, .f.f = 0, .f.p = 0 };
134 bar(&S);
136 // CHECK-LABEL: @test10(
137 // CHECK: call void @llvm.memset
138 // CHECK-NOT: store i32 0
139 // CHECK: call void @bar
142 void nonzeroMemseti8(void) {
143 char arr[33] = { 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, };
144 // CHECK-LABEL: @nonzeroMemseti8(
145 // CHECK-NOT: store
146 // CHECK-NOT: memcpy
147 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 42, i32 33, i1 false)
150 void nonzeroMemseti16(void) {
151 unsigned short arr[17] = { 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, 0x4242, };
152 // CHECK-LABEL: @nonzeroMemseti16(
153 // CHECK-NOT: store
154 // CHECK-NOT: memcpy
155 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 66, i32 34, i1 false)
158 void nonzeroMemseti32(void) {
159 unsigned arr[9] = { 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, 0xF0F0F0F0, };
160 // CHECK-LABEL: @nonzeroMemseti32(
161 // CHECK-NOT: store
162 // CHECK-NOT: memcpy
163 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 -16, i32 36, i1 false)
166 void nonzeroMemseti64(void) {
167 unsigned long long arr[7] = { 0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAA, };
168 // CHECK-LABEL: @nonzeroMemseti64(
169 // CHECK-NOT: store
170 // CHECK-NOT: memcpy
171 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 -86, i32 56, i1 false)
174 void nonzeroMemsetf32(void) {
175 float arr[9] = { 0x1.cacacap+75, 0x1.cacacap+75, 0x1.cacacap+75, 0x1.cacacap+75, 0x1.cacacap+75, 0x1.cacacap+75, 0x1.cacacap+75, 0x1.cacacap+75, 0x1.cacacap+75, };
176 // CHECK-LABEL: @nonzeroMemsetf32(
177 // CHECK-NOT: store
178 // CHECK-NOT: memcpy
179 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 101, i32 36, i1 false)
182 void nonzeroMemsetf64(void) {
183 double arr[7] = { 0x1.4444444444444p+69, 0x1.4444444444444p+69, 0x1.4444444444444p+69, 0x1.4444444444444p+69, 0x1.4444444444444p+69, 0x1.4444444444444p+69, 0x1.4444444444444p+69, };
184 // CHECK-LABEL: @nonzeroMemsetf64(
185 // CHECK-NOT: store
186 // CHECK-NOT: memcpy
187 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 68, i32 56, i1 false)
190 void nonzeroPaddedUnionMemset(void) {
191 union U { char c; int i; };
192 union U arr[9] = { 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, };
193 // CHECK-LABEL: @nonzeroPaddedUnionMemset(
194 // CHECK-NOT: store
195 // CHECK-NOT: memcpy
196 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 -16, i32 36, i1 false)
199 void nonzeroNestedMemset(void) {
200 union U { char c; int i; };
201 struct S { union U u; short i; };
202 struct S arr[5] = { { {0xF0}, 0xF0F0 }, { {0xF0}, 0xF0F0 }, { {0xF0}, 0xF0F0 }, { {0xF0}, 0xF0F0 }, { {0xF0}, 0xF0F0 }, };
203 // CHECK-LABEL: @nonzeroNestedMemset(
204 // CHECK-NOT: store
205 // CHECK-NOT: memcpy
206 // CHECK: call void @llvm.memset.p0.i32(ptr {{.*}}, i8 -16, i32 40, i1 false)
209 // PR9257
210 struct test11S {
211 int A[10];
213 void test11(struct test11S *P) {
214 *P = (struct test11S) { .A = { [0 ... 3] = 4 } };
215 // CHECK-LABEL: @test11(
216 // CHECK: store i32 4, ptr %{{.*}}, align 4
217 // CHECK: store i32 4, ptr %{{.*}}, align 4
218 // CHECK: store i32 4, ptr %{{.*}}, align 4
219 // CHECK: store i32 4, ptr %{{.*}}, align 4
220 // CHECK: ret void
224 // Verify that we can convert a recursive struct with a memory that returns
225 // an instance of the struct we're converting.
226 struct test12 {
227 struct test12 (*p)(void);
228 } test12g;
231 void test13(int x) {
232 struct X { int a; int b : 10; int c; };
233 struct X y = {.c = x};
234 // CHECK-LABEL: @test13(
235 // CHECK: and i16 {{.*}}, -1024
238 // CHECK-LABEL: @PR20473(
239 void PR20473(void) {
240 // CHECK: memcpy{{.*}}
241 bar((char[2]) {""});
242 // CHECK: memcpy{{.*}}
243 bar((char[3]) {""});
246 // Test that we initialize large member arrays by copying from a global and not
247 // with a series of stores.
248 struct S14 { int a[16]; };
250 void test14(struct S14 *s14) {
251 // CHECK-LABEL: @test14(
252 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 4 {{.*}}, ptr align 4 [[INIT14]], i32 64, i1 false)
253 // CHECK-NOT: store
254 // CHECK: ret void
255 *s14 = (struct S14) { { [5 ... 11] = 17 } };