1 // RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck %s
2 
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};
7 
8 // CHECK-DAG: %struct.M = type { [2 x %struct.I] }
9 // CHECK-DAG: %struct.I = type { [3 x i32] }
10 
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 global [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
14 
15 void f1() {
16   // Scalars in braces.
17   int a = { 1 };
18 }
19 
20 void f2() {
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"};
27 }
28 
29 typedef void (* F)(void);
30 extern void foo(void);
31 struct S { F f; };
32 void f3() {
33   struct S a[1] = { { foo } };
34 }
35 
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() {
41   static const int g4 = 12;
42   return g4;
43 }
44 
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   }};
54 }
55 
56 // rdar://problem/8154689
57 void f6() {
58   int x;
59   long ids[] = { (long) &x };
60 }
61 
62 
63 
64 
65 // CHECK-DAG: @test7 = global{{.*}}{ i32 0, [4 x i8] c"bar\00" }
66 // PR8217
67 struct a7 {
68   int  b;
69   char v[];
70 };
71 
72 struct a7 test7 = { .b = 0, .v = "bar" };
73 
74 
75 // CHECK-DAG: @huge_array = global {{.*}} <{ i32 1, i32 0, i32 2, i32 0, i32 3, [999999995 x i32] zeroinitializer }>
76 int huge_array[1000000000] = {1, 0, 2, 0, 3, 0, 0, 0};
77 
78 // CHECK-DAG: @huge_struct = global {{.*}} { i32 1, <{ i32, [999999999 x i32] }> <{ i32 2, [999999999 x i32] zeroinitializer }> }
79 struct Huge {
80   int a;
81   int arr[1000 * 1000 * 1000];
82 } huge_struct = {1, {2, 0, 0, 0}};
83 
84 // 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 }>
85 const char large_array_with_zeroes[1000] = {
86   'a', 'b', 'c', 1, 2, 3, 'x', 'y', 'z', 'z', 'y', [20] = 'q'
87 };
88 
89 // PR279 comment #3
90 char test8(int X) {
91   char str[100000] = "abc"; // tail should be memset.
92   return str[X];
93   // CHECK-LABEL: @test8(
94   // CHECK: call void @llvm.memset
95   // CHECK: store i8 97, i8* %{{[0-9]*}}, align 1
96   // CHECK: store i8 98, i8* %{{[0-9]*}}, align 1
97   // CHECK: store i8 99, i8* %{{[0-9]*}}, align 1
98   // CHECK-NOT: getelementptr
99   // CHECK: load
100 }
101 
102 void bar(void*);
103 
104 // PR279
105 void test9(int X) {
106   int Arr[100] = { X };     // Should use memset
107   bar(Arr);
108   // CHECK-LABEL: @test9(
109   // CHECK: call void @llvm.memset
110   // CHECK-NOT: store i32 0
111   // CHECK: call void @bar
112 }
113 
114 struct a {
115   int a, b, c, d, e, f, g, h, i, j, k, *p;
116 };
117 
118 struct b {
119   struct a a,b,c,d,e,f,g;
120 };
121 
122 void test10(int X) {
123   struct b S = { .a.a = X, .d.e = X, .f.e = 0, .f.f = 0, .f.p = 0 };
124   bar(&S);
125 
126   // CHECK-LABEL: @test10(
127   // CHECK: call void @llvm.memset
128   // CHECK-NOT: store i32 0
129   // CHECK: call void @bar
130 }
131 
132 
133 // PR9257
134 struct test11S {
135   int A[10];
136 };
137 void test11(struct test11S *P) {
138   *P = (struct test11S) { .A = { [0 ... 3] = 4 } };
139   // CHECK-LABEL: @test11(
140   // CHECK: store i32 4, i32* %{{.*}}, align 4
141   // CHECK: store i32 4, i32* %{{.*}}, align 4
142   // CHECK: store i32 4, i32* %{{.*}}, align 4
143   // CHECK: store i32 4, i32* %{{.*}}, align 4
144   // CHECK: ret void
145 }
146 
147 
148 // Verify that we can convert a recursive struct with a memory that returns
149 // an instance of the struct we're converting.
150 struct test12 {
151   struct test12 (*p)(void);
152 } test12g;
153 
154 
155 void test13(int x) {
156   struct X { int a; int b : 10; int c; };
157   struct X y = {.c = x};
158   // CHECK-LABEL: @test13(
159   // CHECK: and i16 {{.*}}, -1024
160 }
161 
162 // CHECK-LABEL: @PR20473(
163 void PR20473() {
164   // CHECK: memcpy{{.*}}getelementptr inbounds ([2 x i8], [2 x i8]* @
165   bar((char[2]) {""});
166   // CHECK: memcpy{{.*}}getelementptr inbounds ([3 x i8], [3 x i8]* @
167   bar((char[3]) {""});
168 }
169 
170 // Test that we initialize large member arrays by copying from a global and not
171 // with a series of stores.
172 struct S14 { int a[16]; };
173 
174 void test14(struct S14 *s14) {
175   // CHECK-LABEL: @test14(
176   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 {{.*}}, i8* align 4 {{.*}} [[INIT14]] {{.*}}, i32 64, i1 false)
177   // CHECK-NOT: store
178   // CHECK: ret void
179   *s14 = (struct S14) { { [5 ... 11] = 17 } };
180 }
181