Add malloc predictor (PR middle-end/83023).
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-4.c
blobc22654dea2a2c97cdd47b8d7d140ebc2a1b7c15b
1 /* { dg-do run } */
2 /* { dg-options "-O2" } */
3 /* { dg-require-effective-target alloca } */
5 typedef __SIZE_TYPE__ size_t;
6 extern void abort (void);
7 extern void exit (int);
8 extern void *malloc (size_t);
9 extern void *calloc (size_t, size_t);
10 extern void *alloca (size_t);
11 extern void *memcpy (void *, const void *, size_t);
12 extern void *memset (void *, int, size_t);
13 extern char *strcpy (char *, const char *);
15 struct A
17 char a[10];
18 int b;
19 char c[10];
20 } y, w[4];
22 extern char exta[];
23 extern char extb[30];
24 extern struct A extc[];
25 struct A zerol[0];
27 void
28 __attribute__ ((noinline))
29 test1 (void *q, int x)
31 struct A a;
32 void *p = &a.a[3], *r;
33 char var[x + 10];
34 struct A vara[x + 10];
35 if (x < 0)
36 r = &a.a[9];
37 else
38 r = &a.c[1];
39 if (__builtin_object_size (p, 3) != sizeof (a.a) - 3)
40 abort ();
41 if (__builtin_object_size (&a.c[9], 3)
42 != sizeof (a.c) - 9)
43 abort ();
44 if (__builtin_object_size (q, 3) != 0)
45 abort ();
46 if (__builtin_object_size (r, 3) != sizeof (a.a) - 9)
47 abort ();
48 if (x < 6)
49 r = &w[2].a[1];
50 else
51 r = &a.a[6];
52 if (__builtin_object_size (&y, 3) != sizeof (y))
53 abort ();
54 if (__builtin_object_size (w, 3) != sizeof (w))
55 abort ();
56 if (__builtin_object_size (&y.b, 3) != sizeof (a.b))
57 abort ();
58 if (__builtin_object_size (r, 3) != sizeof (a.a) - 6)
59 abort ();
60 if (x < 20)
61 r = malloc (30);
62 else
63 r = calloc (2, 16);
64 if (__builtin_object_size (r, 3) != 30)
65 abort ();
66 if (x < 20)
67 r = malloc (30);
68 else
69 r = calloc (2, 14);
70 if (__builtin_object_size (r, 3) != 2 * 14)
71 abort ();
72 if (x < 30)
73 r = malloc (sizeof (a));
74 else
75 r = &a.a[3];
76 if (__builtin_object_size (r, 3) != sizeof (a.a) - 3)
77 abort ();
78 r = memcpy (r, "a", 2);
79 if (__builtin_object_size (r, 3) != sizeof (a.a) - 3)
80 abort ();
81 r = memcpy (r + 2, "b", 2) + 2;
82 if (__builtin_object_size (r, 3) != sizeof (a.a) - 3 - 4)
83 abort ();
84 r = &a.a[4];
85 r = memset (r, 'a', 2);
86 if (__builtin_object_size (r, 3) != sizeof (a.a) - 4)
87 abort ();
88 r = memset (r + 2, 'b', 2) + 2;
89 if (__builtin_object_size (r, 3) != sizeof (a.a) - 8)
90 abort ();
91 r = &a.a[1];
92 r = strcpy (r, "ab");
93 if (__builtin_object_size (r, 3) != sizeof (a.a) - 1)
94 abort ();
95 r = strcpy (r + 2, "cd") + 2;
96 if (__builtin_object_size (r, 3) != sizeof (a.a) - 5)
97 abort ();
98 if (__builtin_object_size (exta, 3) != 0)
99 abort ();
100 if (__builtin_object_size (exta + 10, 3) != 0)
101 abort ();
102 if (__builtin_object_size (&exta[5], 3) != 0)
103 abort ();
104 if (__builtin_object_size (extb, 3) != sizeof (extb))
105 abort ();
106 if (__builtin_object_size (extb + 10, 3) != sizeof (extb) - 10)
107 abort ();
108 if (__builtin_object_size (&extb[5], 3) != sizeof (extb) - 5)
109 abort ();
110 if (__builtin_object_size (extc, 3) != 0)
111 abort ();
112 if (__builtin_object_size (extc + 10, 3) != 0)
113 abort ();
114 if (__builtin_object_size (&extc[5], 3) != 0)
115 abort ();
116 if (__builtin_object_size (&extc->a, 3) != 0)
117 abort ();
118 if (__builtin_object_size (&(extc + 10)->b, 3) != 0)
119 abort ();
120 if (__builtin_object_size (&extc[5].c[3], 3) != 0)
121 abort ();
122 if (__builtin_object_size (var, 3) != 0)
123 abort ();
124 if (__builtin_object_size (var + 10, 3) != 0)
125 abort ();
126 if (__builtin_object_size (&var[5], 3) != 0)
127 abort ();
128 if (__builtin_object_size (vara, 3) != 0)
129 abort ();
130 if (__builtin_object_size (vara + 10, 3) != 0)
131 abort ();
132 if (__builtin_object_size (&vara[5], 3) != 0)
133 abort ();
134 if (__builtin_object_size (&vara[0].a, 3) != sizeof (vara[0].a))
135 abort ();
136 if (__builtin_object_size (&vara[10].a[0], 3) != sizeof (vara[0].a))
137 abort ();
138 if (__builtin_object_size (&vara[5].a[4], 3) != sizeof (vara[0].a) - 4)
139 abort ();
140 if (__builtin_object_size (&vara[5].b, 3) != sizeof (vara[0].b))
141 abort ();
142 if (__builtin_object_size (&vara[7].c[7], 3) != sizeof (vara[0].c) - 7)
143 abort ();
144 if (__builtin_object_size (zerol, 3) != 0)
145 abort ();
146 if (__builtin_object_size (&zerol, 3) != 0)
147 abort ();
148 if (__builtin_object_size (&zerol[0], 3) != 0)
149 abort ();
150 if (__builtin_object_size (zerol[0].a, 3) != 0)
151 abort ();
152 if (__builtin_object_size (&zerol[0].a[0], 3) != 0)
153 abort ();
154 if (__builtin_object_size (&zerol[0].b, 3) != 0)
155 abort ();
156 if (__builtin_object_size ("abcdefg", 3) != sizeof ("abcdefg"))
157 abort ();
158 if (__builtin_object_size ("abcd\0efg", 3) != sizeof ("abcd\0efg"))
159 abort ();
160 if (__builtin_object_size (&"abcd\0efg", 3) != sizeof ("abcd\0efg"))
161 abort ();
162 if (__builtin_object_size (&"abcd\0efg"[0], 3) != sizeof ("abcd\0efg"))
163 abort ();
164 if (__builtin_object_size (&"abcd\0efg"[4], 3) != sizeof ("abcd\0efg") - 4)
165 abort ();
166 if (__builtin_object_size ("abcd\0efg" + 5, 3) != sizeof ("abcd\0efg") - 5)
167 abort ();
168 if (__builtin_object_size (L"abcdefg", 3) != sizeof (L"abcdefg"))
169 abort ();
170 r = (char *) L"abcd\0efg";
171 if (__builtin_object_size (r + 2, 3) != sizeof (L"abcd\0efg") - 2)
172 abort ();
175 size_t l1 = 1;
177 void
178 __attribute__ ((noinline))
179 test2 (void)
181 struct B { char buf1[10]; char buf2[10]; } a;
182 char *r, buf3[20];
183 int i;
185 if (sizeof (a) != 20)
186 return;
188 r = buf3;
189 for (i = 0; i < 4; ++i)
191 if (i == l1 - 1)
192 r = &a.buf1[1];
193 else if (i == l1)
194 r = &a.buf2[7];
195 else if (i == l1 + 1)
196 r = &buf3[5];
197 else if (i == l1 + 2)
198 r = &a.buf1[9];
200 if (__builtin_object_size (r, 3) != sizeof (a.buf1) - 9)
201 abort ();
202 r = &buf3[20];
203 for (i = 0; i < 4; ++i)
205 if (i == l1 - 1)
206 r = &a.buf1[7];
207 else if (i == l1)
208 r = &a.buf2[7];
209 else if (i == l1 + 1)
210 r = &buf3[5];
211 else if (i == l1 + 2)
212 r = &a.buf1[9];
214 if (__builtin_object_size (r, 3) != 0)
215 abort ();
216 r = &buf3[1];
217 for (i = 0; i < 4; ++i)
219 if (i == l1 - 1)
220 r = &a.buf1[6];
221 else if (i == l1)
222 r = &a.buf2[4];
223 else if (i == l1 + 1)
224 r = &buf3[5];
225 else if (i == l1 + 2)
226 r = &a.buf1[2];
228 if (__builtin_object_size (r, 3) != sizeof (a.buf1) - 6)
229 abort ();
230 r += 2;
231 if (__builtin_object_size (r, 3) != sizeof (a.buf1) - 6 - 2)
232 abort ();
233 if (__builtin_object_size (r + 1, 3) != sizeof (a.buf1) - 6 - 3)
234 abort ();
237 void
238 __attribute__ ((noinline))
239 test3 (void)
241 char buf4[10];
242 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
243 _Complex double f; } x;
244 double y;
245 _Complex double z;
246 double *dp;
248 if (__builtin_object_size (buf4, 3) != sizeof (buf4))
249 abort ();
250 if (__builtin_object_size (&buf4, 3) != sizeof (buf4))
251 abort ();
252 if (__builtin_object_size (&buf4[0], 3) != sizeof (buf4))
253 abort ();
254 if (__builtin_object_size (&buf4[1], 3) != sizeof (buf4) - 1)
255 abort ();
256 if (__builtin_object_size (&x, 3) != sizeof (x))
257 abort ();
258 if (__builtin_object_size (&x.a, 3) != sizeof (x.a))
259 abort ();
260 if (__builtin_object_size (&x.a[0], 3) != sizeof (x.a))
261 abort ();
262 if (__builtin_object_size (&x.a[0].a, 3) != sizeof (x.a[0].a))
263 abort ();
264 if (__builtin_object_size (&x.a[0].a[0], 3) != sizeof (x.a[0].a))
265 abort ();
266 if (__builtin_object_size (&x.a[0].a[3], 3) != sizeof (x.a[0].a) - 3)
267 abort ();
268 if (__builtin_object_size (&x.a[0].b, 3) != sizeof (x.a[0].b))
269 abort ();
270 if (__builtin_object_size (&x.a[1].c, 3) != sizeof (x.a[1].c))
271 abort ();
272 if (__builtin_object_size (&x.a[1].c[0], 3) != sizeof (x.a[1].c))
273 abort ();
274 if (__builtin_object_size (&x.a[1].c[3], 3) != sizeof (x.a[1].c) - 3)
275 abort ();
276 if (__builtin_object_size (&x.b, 3) != sizeof (x.b))
277 abort ();
278 if (__builtin_object_size (&x.b.a, 3) != sizeof (x.b.a))
279 abort ();
280 if (__builtin_object_size (&x.b.a[0], 3) != sizeof (x.b.a))
281 abort ();
282 if (__builtin_object_size (&x.b.a[3], 3) != sizeof (x.b.a) - 3)
283 abort ();
284 if (__builtin_object_size (&x.b.b, 3) != sizeof (x.b.b))
285 abort ();
286 if (__builtin_object_size (&x.b.c, 3) != sizeof (x.b.c))
287 abort ();
288 if (__builtin_object_size (&x.b.c[0], 3) != sizeof (x.b.c))
289 abort ();
290 if (__builtin_object_size (&x.b.c[3], 3) != sizeof (x.b.c) - 3)
291 abort ();
292 if (__builtin_object_size (&x.c, 3) != sizeof (x.c))
293 abort ();
294 if (__builtin_object_size (&x.c[0], 3) != sizeof (x.c))
295 abort ();
296 if (__builtin_object_size (&x.c[1], 3) != sizeof (x.c) - 1)
297 abort ();
298 if (__builtin_object_size (&x.d, 3) != sizeof (x.d))
299 abort ();
300 if (__builtin_object_size (&x.e, 3) != sizeof (x.e))
301 abort ();
302 if (__builtin_object_size (&x.f, 3) != sizeof (x.f))
303 abort ();
304 dp = &__real__ x.f;
305 if (__builtin_object_size (dp, 3) != sizeof (x.f) / 2)
306 abort ();
307 dp = &__imag__ x.f;
308 if (__builtin_object_size (dp, 3) != sizeof (x.f) / 2)
309 abort ();
310 dp = &y;
311 if (__builtin_object_size (dp, 3) != sizeof (y))
312 abort ();
313 if (__builtin_object_size (&z, 3) != sizeof (z))
314 abort ();
315 dp = &__real__ z;
316 if (__builtin_object_size (dp, 3) != sizeof (z) / 2)
317 abort ();
318 dp = &__imag__ z;
319 if (__builtin_object_size (dp, 3) != sizeof (z) / 2)
320 abort ();
323 struct S { unsigned int a; };
325 char *
326 __attribute__ ((noinline))
327 test4 (char *x, int y)
329 register int i;
330 struct A *p;
332 for (i = 0; i < y; i++)
334 p = (struct A *) x;
335 x = (char *) &p[1];
336 if (__builtin_object_size (p, 3) != 0)
337 abort ();
339 return x;
342 void
343 __attribute__ ((noinline))
344 test5 (size_t x)
346 struct T { char buf[64]; char buf2[64]; } t;
347 char *p = &t.buf[8];
348 size_t i;
350 for (i = 0; i < x; ++i)
351 p = p + 4;
352 if (__builtin_object_size (p, 3) != 0)
353 abort ();
354 memset (p, ' ', sizeof (t.buf) - 8 - 4 * 4);
357 void
358 __attribute__ ((noinline))
359 test6 (void)
361 char buf[64];
362 struct T { char buf[64]; char buf2[64]; } t;
363 char *p = &buf[64], *q = &t.buf[64];
365 if (__builtin_object_size (p + 64, 3) != 0)
366 abort ();
367 if (__builtin_object_size (q + 0, 3) != 0)
368 abort ();
369 if (__builtin_object_size (q + 64, 3) != 0)
370 abort ();
373 void
374 __attribute__ ((noinline))
375 test7 (void)
377 struct T { char buf[10]; char buf2[10]; } t;
378 char *p = &t.buf2[-4];
379 char *q = &t.buf2[0];
380 if (__builtin_object_size (p, 3) != 0)
381 abort ();
382 if (__builtin_object_size (q, 3) != sizeof (t.buf2))
383 abort ();
384 q = &t.buf[10];
385 if (__builtin_object_size (q, 3) != 0)
386 abort ();
387 q = &t.buf[11];
388 if (__builtin_object_size (q, 3) != 0)
389 abort ();
390 p = &t.buf[-4];
391 if (__builtin_object_size (p, 3) != 0)
392 abort ();
396 main (void)
398 struct S s[10];
399 __asm ("" : "=r" (l1) : "0" (l1));
400 test1 (main, 6);
401 test2 ();
402 test3 ();
403 test4 ((char *) s, 10);
404 test5 (4);
405 test6 ();
406 test7 ();
407 exit (0);