1 // RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - | FileCheck %s
2
3 int b(char* x);
4
5 // Extremely basic VLA test
a(int x)6 void a(int x) {
7 char arry[x];
8 arry[0] = 10;
9 b(arry);
10 }
11
c(int n)12 int c(int n)
13 {
14 return sizeof(int[n]);
15 }
16
f0(int x)17 int f0(int x) {
18 int vla[x];
19 return vla[x-1];
20 }
21
22 void
f(int count)23 f(int count)
24 {
25 int a[count];
26
27 do { } while (0);
28
29 if (a[0] != 3) {
30 }
31 }
32
g(int count)33 void g(int count) {
34 // Make sure we emit sizes correctly in some obscure cases
35 int (*a[5])[count];
36 int (*b)[][count];
37 }
38
39 // rdar://8403108
40 // CHECK-LABEL: define void @f_8403108
f_8403108(unsigned x)41 void f_8403108(unsigned x) {
42 // CHECK: call i8* @llvm.stacksave()
43 char s1[x];
44 while (1) {
45 // CHECK: call i8* @llvm.stacksave()
46 char s2[x];
47 if (1)
48 break;
49 // CHECK: call void @llvm.stackrestore(i8*
50 }
51 // CHECK: call void @llvm.stackrestore(i8*
52 }
53
54 // pr7827
function(short width,int data[][width])55 void function(short width, int data[][width]) {} // expected-note {{passing argument to parameter 'data' here}}
56
test()57 void test() {
58 int bork[4][13];
59 // CHECK: call void @function(i16 signext 1, i32* null)
60 function(1, 0);
61 // CHECK: call void @function(i16 signext 1, i32* inttoptr
62 function(1, 0xbadbeef); // expected-warning {{incompatible integer to pointer conversion passing}}
63 // CHECK: call void @function(i16 signext 1, i32* {{.*}})
64 function(1, bork);
65 }
66
function1(short width,int data[][width][width])67 void function1(short width, int data[][width][width]) {}
test1()68 void test1() {
69 int bork[4][13][15];
70 // CHECK: call void @function1(i16 signext 1, i32* {{.*}})
71 function1(1, bork);
72 // CHECK: call void @function(i16 signext 1, i32* {{.*}})
73 function(1, bork[2]);
74 }
75
76 // rdar://8476159
77 static int GLOB;
test2(int n)78 int test2(int n)
79 {
80 GLOB = 0;
81 char b[1][n+3]; /* Variable length array. */
82 // CHECK: [[tmp_1:%.*]] = load i32, i32* @GLOB, align 4
83 // CHECK-NEXT: add nsw i32 [[tmp_1]], 1
84 __typeof__(b[GLOB++]) c;
85 return GLOB;
86 }
87
88 // http://llvm.org/PR8567
89 // CHECK-LABEL: define double @test_PR8567
test_PR8567(int n,double (* p)[n][5])90 double test_PR8567(int n, double (*p)[n][5]) {
91 // CHECK: [[NV:%.*]] = alloca i32, align 4
92 // CHECK-NEXT: [[PV:%.*]] = alloca [5 x double]*, align 4
93 // CHECK-NEXT: store
94 // CHECK-NEXT: store
95 // CHECK-NEXT: [[N:%.*]] = load i32, i32* [[NV]], align 4
96 // CHECK-NEXT: [[P:%.*]] = load [5 x double]*, [5 x double]** [[PV]], align 4
97 // CHECK-NEXT: [[T0:%.*]] = mul nsw i32 1, [[N]]
98 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [5 x double], [5 x double]* [[P]], i32 [[T0]]
99 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [5 x double], [5 x double]* [[T1]], i32 2
100 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [5 x double], [5 x double]* [[T2]], i32 0, i32 3
101 // CHECK-NEXT: [[T4:%.*]] = load double, double* [[T3]]
102 // CHECK-NEXT: ret double [[T4]]
103 return p[1][2][3];
104 }
105
test4(unsigned n,char (* p)[n][n+1][6])106 int test4(unsigned n, char (*p)[n][n+1][6]) {
107 // CHECK-LABEL: define i32 @test4(
108 // CHECK: [[N:%.*]] = alloca i32, align 4
109 // CHECK-NEXT: [[P:%.*]] = alloca [6 x i8]*, align 4
110 // CHECK-NEXT: [[P2:%.*]] = alloca [6 x i8]*, align 4
111 // CHECK-NEXT: store i32
112 // CHECK-NEXT: store [6 x i8]*
113
114 // VLA captures.
115 // CHECK-NEXT: [[DIM0:%.*]] = load i32, i32* [[N]], align 4
116 // CHECK-NEXT: [[T0:%.*]] = load i32, i32* [[N]], align 4
117 // CHECK-NEXT: [[DIM1:%.*]] = add i32 [[T0]], 1
118
119 // CHECK-NEXT: [[T0:%.*]] = load [6 x i8]*, [6 x i8]** [[P]], align 4
120 // CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[N]], align 4
121 // CHECK-NEXT: [[T2:%.*]] = udiv i32 [[T1]], 2
122 // CHECK-NEXT: [[T3:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]
123 // CHECK-NEXT: [[T4:%.*]] = mul nsw i32 [[T2]], [[T3]]
124 // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds [6 x i8], [6 x i8]* [[T0]], i32 [[T4]]
125 // CHECK-NEXT: [[T6:%.*]] = load i32, i32* [[N]], align 4
126 // CHECK-NEXT: [[T7:%.*]] = udiv i32 [[T6]], 4
127 // CHECK-NEXT: [[T8:%.*]] = sub i32 0, [[T7]]
128 // CHECK-NEXT: [[T9:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]
129 // CHECK-NEXT: [[T10:%.*]] = mul nsw i32 [[T8]], [[T9]]
130 // CHECK-NEXT: [[T11:%.*]] = getelementptr inbounds [6 x i8], [6 x i8]* [[T5]], i32 [[T10]]
131 // CHECK-NEXT: store [6 x i8]* [[T11]], [6 x i8]** [[P2]], align 4
132 __typeof(p) p2 = (p + n/2) - n/4;
133
134 // CHECK-NEXT: [[T0:%.*]] = load [6 x i8]*, [6 x i8]** [[P2]], align 4
135 // CHECK-NEXT: [[T1:%.*]] = load [6 x i8]*, [6 x i8]** [[P]], align 4
136 // CHECK-NEXT: [[T2:%.*]] = ptrtoint [6 x i8]* [[T0]] to i32
137 // CHECK-NEXT: [[T3:%.*]] = ptrtoint [6 x i8]* [[T1]] to i32
138 // CHECK-NEXT: [[T4:%.*]] = sub i32 [[T2]], [[T3]]
139 // CHECK-NEXT: [[T5:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]
140 // CHECK-NEXT: [[T6:%.*]] = mul nuw i32 6, [[T5]]
141 // CHECK-NEXT: [[T7:%.*]] = sdiv exact i32 [[T4]], [[T6]]
142 // CHECK-NEXT: ret i32 [[T7]]
143 return p2 - p;
144 }
145
146 // rdar://11485774
test5(void)147 void test5(void)
148 {
149 // CHECK-LABEL: define void @test5(
150 int a[5], i = 0;
151 // CHECK: [[A:%.*]] = alloca [5 x i32], align 4
152 // CHECK-NEXT: [[I:%.*]] = alloca i32, align 4
153 // CHECK-NEXT: [[CL:%.*]] = alloca i32*, align 4
154 // CHECK-NEXT: store i32 0, i32* [[I]], align 4
155
156 (typeof(++i, (int (*)[i])a)){&a} += 0;
157 // CHECK-NEXT: [[Z:%.*]] = load i32, i32* [[I]], align 4
158 // CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[Z]], 1
159 // CHECK-NEXT: store i32 [[INC]], i32* [[I]], align 4
160 // CHECK-NEXT: [[O:%.*]] = load i32, i32* [[I]], align 4
161 // CHECK-NEXT: [[AR:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* [[A]], i32 0, i32 0
162 // CHECK-NEXT: [[T:%.*]] = bitcast [5 x i32]* [[A]] to i32*
163 // CHECK-NEXT: store i32* [[T]], i32** [[CL]]
164 // CHECK-NEXT: [[TH:%.*]] = load i32*, i32** [[CL]]
165 // CHECK-NEXT: [[VLAIX:%.*]] = mul nsw i32 0, [[O]]
166 // CHECK-NEXT: [[ADDPTR:%.*]] = getelementptr inbounds i32, i32* [[TH]], i32 [[VLAIX]]
167 // CHECK-NEXT: store i32* [[ADDPTR]], i32** [[CL]]
168 }
169
test6(void)170 void test6(void)
171 {
172 // CHECK-LABEL: define void @test6(
173 int n = 20, **a, i=0;
174 // CHECK: [[N:%.*]] = alloca i32, align 4
175 // CHECK-NEXT: [[A:%.*]] = alloca i32**, align 4
176 // CHECK-NEXT: [[I:%.*]] = alloca i32, align 4
177 (int (**)[i]){&a}[0][1][5] = 0;
178 // CHECK-NEXT: [[CL:%.*]] = alloca i32**, align 4
179 // CHECK-NEXT: store i32 20, i32* [[N]], align 4
180 // CHECK-NEXT: store i32 0, i32* [[I]], align 4
181 // CHECK-NEXT: [[Z:%.*]] = load i32, i32* [[I]], align 4
182 // CHECK-NEXT: [[O:%.*]] = bitcast i32*** [[A]] to i32**
183 // CHECK-NEXT: store i32** [[O]], i32*** [[CL]]
184 // CHECK-NEXT: [[T:%.*]] = load i32**, i32*** [[CL]]
185 // CHECK-NEXT: [[IX:%.*]] = getelementptr inbounds i32*, i32** [[T]], i32 0
186 // CHECK-NEXT: [[TH:%.*]] = load i32*, i32** [[IX]], align 4
187 // CHECK-NEXT: [[F:%.*]] = mul nsw i32 1, [[Z]]
188 // CHECK-NEXT: [[IX1:%.*]] = getelementptr inbounds i32, i32* [[TH]], i32 [[F]]
189 // CHECK-NEXT: [[IX2:%.*]] = getelementptr inbounds i32, i32* [[IX1]], i32 5
190 // CHECK-NEXT: store i32 0, i32* [[IX2]], align 4
191 }
192
193 // Follow gcc's behavior for VLAs in parameter lists. PR9559.
test7(int a[b (0)])194 void test7(int a[b(0)]) {
195 // CHECK-LABEL: define void @test7(
196 // CHECK: call i32 @b(i8* null)
197 }
198
199 // Make sure we emit dereferenceable or nonnull when the static keyword is
200 // provided.
test8(int a[static3])201 void test8(int a[static 3]) { }
202 // CHECK: define void @test8(i32* dereferenceable(12) %a)
203
test9(int n,int a[static n])204 void test9(int n, int a[static n]) { }
205 // CHECK: define void @test9(i32 %n, i32* nonnull %a)
206
207