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