summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/strlenopt-40.c
blob: 7a97ebb8fe5be4b9e9ebd2d81238e9b63a4fd6e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* PR tree-optimization/83671 - fix for false positive reported by
   -Wstringop-overflow does not work with inlining
   { dg-do compile }
   { dg-options "-O1 -fdump-tree-optimized" } */

#include "strlenopt.h"

#define DIFF_MAX __PTRDIFF_MAX__

#define CAT(x, y) x ## y
#define CONCAT(x, y) CAT (x, y)
#define FAILNAME(name) CONCAT (call_ ## name ##_on_line_, __LINE__)

#define FAIL(name) do {				\
    extern void FAILNAME (name) (void);		\
    FAILNAME (name)();				\
  } while (0)

/* Macros to emit a call to funcation named
     call_in_{true,false}_branch_not_eliminated_on_line_NNN()
   for each call that's expected to be eliminated.  The dg-final
   scan-tree-dump-time directive at the bottom of the test verifies
   that no such call appears in output.  */
#define ELIM_TRUE(expr) \
  if (!(expr)) FAIL (in_true_branch_not_eliminated); else (void)0

#define ELIM_FALSE(expr)					\
  if (!!(expr)) FAIL (in_false_branch_not_eliminated); else (void)0

/* Macro to emit a call to a function named
     call_made_in_{true,false}_branch_on_line_NNN()
   for each call that's expected to be retained.  The dg-final
   scan-tree-dump-time directive at the bottom of the test verifies
   that the expected number of both kinds of calls appears in output
   (a pair for each line with the invocation of the KEEP() macro.  */
#define KEEP(expr)				\
  if (expr)					\
    FAIL (made_in_true_branch);			\
  else						\
    FAIL (made_in_false_branch)

typedef char A3[3], A5[5], A7[7], AX[];

typedef A3 A7_3[7];
typedef A3 AX_3[];
typedef A5 A7_5[7];
typedef A7 A5_7[5];

extern A7_3 a7_3;
extern A5_7 a5_7;
extern AX_3 ax_3;

extern A3 a3;
extern A7 a5;
extern A7 a7;
extern AX ax;

extern A3 *pa3;
extern A5 *pa5;
extern A7 *pa7;

extern A7_3 *pa7_3;
extern AX_3 *pax_3;
extern A5_7 *pa5_7;
extern A7_5 *pa7_5;

extern char *ptr;

struct MemArrays0 {
  A7_3 a7_3;
  A5_7 a5_7;
  char a3[3], a5[5], a0[0];
};
struct MemArraysX { char a3[3], a5[5], ax[]; };
struct MemArrays7 { char a3[3], a5[5], a7[7]; };

struct MemArrays0 ma0_3_5_7[3][5][7];

void elim_strings (int i)
{
  ELIM_TRUE (strlen (i < 0 ? "123" : "321") == 3);
  ELIM_FALSE (strlen (i < 0 ? "123" : "321") > 3);
  ELIM_FALSE (strlen (i < 0 ? "123" : "321") < 3);

  ELIM_TRUE (strlen (i < 0 ? "123" : "4321") >= 3);
  ELIM_FALSE (strlen (i < 0 ? "123" : "4321") > 4);
  ELIM_FALSE (strlen (i < 0 ? "123" : "4321") < 3);

  ELIM_TRUE (strlen (i < 0 ? "1234" : "321") >= 3);
  ELIM_FALSE (strlen (i < 0 ? "1234" : "321") < 3);
  ELIM_FALSE (strlen (i < 0 ? "1234" : "321") > 4);

  ELIM_TRUE (strlen (i < 0 ? "123" : "4321") <= 4);
  ELIM_TRUE (strlen (i < 0 ? "1234" : "321") <= 4);

  ELIM_TRUE (strlen (i < 0 ? "1" : "123456789") <= 9);
  ELIM_TRUE (strlen (i < 0 ? "1" : "123456789") >= 1);
}

/* Verify that strlen calls involving uninitialized global arrays
   of known size are eliminated when they appear in expressions
   that test for results that must be true.  */
void elim_global_arrays (int i)
{
  /* Verify that the expression involving the strlen call as well
     as whatever depends on it is eliminated  from the test output.
     All these expressions must be trivially true.  */
  ELIM_TRUE (strlen (a7_3[0]) < sizeof a7_3);
  ELIM_TRUE (strlen (a7_3[1]) < sizeof a7_3 - sizeof *a7_3);
  ELIM_TRUE (strlen (a7_3[6]) < sizeof a7_3 - 5 * sizeof *a7_3);
  ELIM_TRUE (strlen (a7_3[i]) < sizeof a7_3);

  ELIM_TRUE (strlen (a5_7[0]) < sizeof a5_7);
  ELIM_TRUE (strlen (a5_7[1]) < sizeof a5_7 - sizeof *a5_7);
  ELIM_TRUE (strlen (a5_7[4]) < sizeof a5_7 - 3 * sizeof *a5_7);
  ELIM_TRUE (strlen (a5_7[i]) < sizeof a5_7);

  /* Even when treating a multi-dimensional array as a single string
     the length must be less DIFF_MAX - (ax_3[i] - ax_3[0]) but GCC
     doesn't do that computation yet so avoid testing it.  */
  ELIM_TRUE (strlen (ax_3[0]) < DIFF_MAX);
  ELIM_TRUE (strlen (ax_3[1]) < DIFF_MAX);
  ELIM_TRUE (strlen (ax_3[9]) < DIFF_MAX);
  ELIM_TRUE (strlen (ax_3[i]) < DIFF_MAX);

  ELIM_TRUE (strlen (a3) < sizeof a3);
  ELIM_TRUE (strlen (a7) < sizeof a7);

  ELIM_TRUE (strlen (ax) != DIFF_MAX);
  /* ELIM_TRUE (strlen (ax) != DIFF_MAX - 1); */
  /* ELIM_TRUE (strlen (ax) < DIFF_MAX - 1); */
}

void elim_pointer_to_arrays (void)
{
  /* Unfortunately, GCC cannot be trusted not to misuse a pointer
     to a smaller array to point to an object of a bigger type so
     the strlen range optimization must assume each array pointer
     points effectively to an array of an unknown bound.  */
  ELIM_TRUE (strlen (*pa7) < DIFF_MAX);
  ELIM_TRUE (strlen (*pa5) < DIFF_MAX);
  ELIM_TRUE (strlen (*pa3) < DIFF_MAX);

  ELIM_TRUE (strlen ((*pa7_3)[0]) < DIFF_MAX);
  ELIM_TRUE (strlen ((*pa7_3)[1]) < DIFF_MAX);
  ELIM_TRUE (strlen ((*pa7_3)[6]) < DIFF_MAX);

  ELIM_TRUE (strlen ((*pax_3)[0]) < DIFF_MAX);
  ELIM_TRUE (strlen ((*pax_3)[1]) < DIFF_MAX);
  ELIM_TRUE (strlen ((*pax_3)[9]) < DIFF_MAX);

  ELIM_TRUE (strlen ((*pa5_7)[0]) < DIFF_MAX);
  ELIM_TRUE (strlen ((*pa5_7)[1]) < DIFF_MAX);
  ELIM_TRUE (strlen ((*pa5_7)[4]) < DIFF_MAX);
}

void elim_global_arrays_and_strings (int i)
{
  ELIM_TRUE (strlen (i < 0 ? a3 : "") < 3);
  ELIM_TRUE (strlen (i < 0 ? a3 : "1") < 3);
  ELIM_TRUE (strlen (i < 0 ? a3 : "12") < 3);
  ELIM_TRUE (strlen (i < 0 ? a3 : "123") < 4);

  ELIM_FALSE (strlen (i < 0 ? a3 : "") > 3);
  ELIM_FALSE (strlen (i < 0 ? a3 : "1") > 3);
  ELIM_FALSE (strlen (i < 0 ? a3 : "12") > 3);
  ELIM_FALSE (strlen (i < 0 ? a3 : "123") > 4);

  ELIM_TRUE (strlen (i < 0 ? a7 : "") < 7);
  ELIM_TRUE (strlen (i < 0 ? a7 : "1") < 7);
  ELIM_TRUE (strlen (i < 0 ? a7 : "12") < 7);
  ELIM_TRUE (strlen (i < 0 ? a7 : "123") < 7);
  ELIM_TRUE (strlen (i < 0 ? a7 : "123456") < 7);
  ELIM_TRUE (strlen (i < 0 ? a7 : "1234567") < 8);

  ELIM_FALSE (strlen (i < 0 ? a7 : "") > 6);
  ELIM_FALSE (strlen (i < 0 ? a7 : "1") > 6);
  ELIM_FALSE (strlen (i < 0 ? a7 : "12") > 6);
  ELIM_FALSE (strlen (i < 0 ? a7 : "123") > 6);
  ELIM_FALSE (strlen (i < 0 ? a7 : "123456") > 7);
  ELIM_FALSE (strlen (i < 0 ? a7 : "1234567") > 8);
}

void elim_member_arrays_obj (int i)
{
  ELIM_TRUE (strlen (ma0_3_5_7[0][0][0].a3) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[0][0][1].a3) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[0][0][2].a3) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[0][0][6].a3) < sizeof ma0_3_5_7);

  ELIM_TRUE (strlen (ma0_3_5_7[1][0][0].a3) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[2][0][1].a3) < sizeof ma0_3_5_7);

  ELIM_TRUE (strlen (ma0_3_5_7[1][1][0].a3) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[2][4][6].a3) < sizeof ma0_3_5_7);

  ELIM_TRUE (strlen (ma0_3_5_7[0][0][0].a5) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[0][0][1].a5) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[0][0][2].a5) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[0][0][6].a5) < sizeof ma0_3_5_7);

  ELIM_TRUE (strlen (ma0_3_5_7[1][0][0].a5) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[2][0][1].a5) < sizeof ma0_3_5_7);

  ELIM_TRUE (strlen (ma0_3_5_7[1][1][0].a5) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[2][4][6].a5) < sizeof ma0_3_5_7);

  ELIM_TRUE (strlen (ma0_3_5_7[0][0][0].a7_3[0]) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[2][4][6].a7_3[2]) < sizeof ma0_3_5_7);

  ELIM_TRUE (strlen (ma0_3_5_7[0][0][0].a5_7[0]) < sizeof ma0_3_5_7);
  ELIM_TRUE (strlen (ma0_3_5_7[2][4][6].a5_7[4]) < sizeof ma0_3_5_7);
}


#line 1000

/* Verify that strlen calls involving uninitialized global arrays
   of unknown size are not eliminated when they appear in expressions
   that test for results that need not be true.  */
void keep_global_arrays (int i)
{
  KEEP (strlen (a7_3[0]) < 2);
  KEEP (strlen (a7_3[1]) < 2);
  KEEP (strlen (a7_3[6]) < 2);
  KEEP (strlen (a7_3[i]) < 2);

  KEEP (strlen (a5_7[0]) < 6);
  KEEP (strlen (a5_7[1]) < 6);
  KEEP (strlen (a5_7[4]) < 6);
  KEEP (strlen (a5_7[i]) < 6);

  /* Verify also that tests (and strlen calls) are not eliminated
     for results greater than what would the size of the innermost
     array suggest might be possible (in case the element array is
     not nul-terminated), even though such calls are undefined.  */
  KEEP (strlen (a5_7[0]) > sizeof a5_7 - 2);
  KEEP (strlen (a5_7[1]) > sizeof a5_7 - sizeof a5_7[1] - 2);
  KEEP (strlen (a5_7[i]) > sizeof a5_7 - 2);

  KEEP (strlen (ax_3[0]) < 2);
  KEEP (strlen (ax_3[1]) < 2);
  KEEP (strlen (ax_3[2]) < 2);
  KEEP (strlen (ax_3[i]) < 2);

  /* Here again, verify that the ax_3 matrix is treated essentially
     as a flat array of unknown bound for the benefit of all the
     undefined code out there that might rely on it.  */
  KEEP (strlen (ax_3[0]) > 3);
  KEEP (strlen (ax_3[1]) > 9);
  KEEP (strlen (ax_3[2]) > 99);
  KEEP (strlen (ax_3[i]) > 999);

  KEEP (strlen (a3) < 2);
  KEEP (strlen (a7) < 6);

  KEEP (strlen (a3 + i) < 2);
  KEEP (strlen (a7 + i) < 2);

  /* The length of an array of unknown size may be as large as
     DIFF_MAX - 2.  */
  KEEP (strlen (ax) != DIFF_MAX - 2);
  KEEP (strlen (ax) < DIFF_MAX - 2);
  KEEP (strlen (ax) < 999);
  KEEP (strlen (ax) < 1);
}

void keep_pointer_to_arrays (int i)
{
  KEEP (strlen (*pa7) < 6);
  KEEP (strlen (*pa5) < 4);
  KEEP (strlen (*pa3) < 2);

  /* Since GCC cannot be trusted not to misuse a pointer to a smaller
     array to point to an object of a larger type verify that the bound
     in a pointer to an array of a known bound isn't relied on for
     the strlen range optimization.  If GCC is fixed to avoid these
     misuses these tests can be removed.  */
  KEEP (strlen (*pa7) > sizeof *pa7);
  KEEP (strlen (*pa5) > sizeof *pa5);
  KEEP (strlen (*pa3) > sizeof *pa3);

  KEEP (strlen ((*pa7_3)[0]) < 2);
  KEEP (strlen ((*pa7_3)[1]) < 2);
  KEEP (strlen ((*pa7_3)[6]) < 2);
  KEEP (strlen ((*pa7_3)[i]) < 2);

  /* Same as above.  */
  KEEP (strlen ((*pa7_3)[0]) > sizeof *pa7_3);
  KEEP (strlen ((*pa7_3)[i]) > sizeof *pa7_3);

  KEEP (strlen ((*pax_3)[0]) < 2);
  KEEP (strlen ((*pax_3)[1]) < 2);
  KEEP (strlen ((*pax_3)[9]) < 2);
  KEEP (strlen ((*pax_3)[i]) < 2);

  /* Same as above.  */
  KEEP (strlen ((*pax_3)[0]) > 3);
  KEEP (strlen ((*pax_3)[i]) > 333);

  KEEP (strlen ((*pa5_7)[0]) < 6);
  KEEP (strlen ((*pa5_7)[1]) < 6);
  KEEP (strlen ((*pa5_7)[4]) < 6);
  KEEP (strlen ((*pa5_7)[i]) < 6);

  /* Same as above.  */
  KEEP (strlen ((*pa5_7)[0]) > sizeof *pa5_7);
  KEEP (strlen ((*pa5_7)[i]) > sizeof *pa5_7);
 }

void keep_global_arrays_and_strings (int i)
{
  KEEP (strlen (i < 0 ? a3 : "") < 2);
  KEEP (strlen (i < 0 ? a3 : "1") < 2);
  KEEP (strlen (i < 0 ? a3 : "12") < 2);
  KEEP (strlen (i < 0 ? a3 : "123") < 3);

  KEEP (strlen (i < 0 ? a7 : "") < 5);
  KEEP (strlen (i < 0 ? a7 : "1") < 5);
  KEEP (strlen (i < 0 ? a7 : "12") < 5);
  KEEP (strlen (i < 0 ? a7 : "123") < 5);
  KEEP (strlen (i < 0 ? a7 : "123456") < 6);
  KEEP (strlen (i < 0 ? a7 : "1234567") < 6);

  /* Verify that a matrix is treated as a flat array even in a conditional
     expression (i.e., don't assume that a7_3[0] is nul-terminated, even
     though calling strlen() on such an array is undefined).  */
  KEEP (strlen (i < 0 ? a7_3[0] : "") > 7);
  KEEP (strlen (i < 0 ? a7_3[i] : "") > 7);
}

void keep_member_arrays_obj (int i)
{
  KEEP (strlen (ma0_3_5_7[0][0][0].a3) < 2);
  KEEP (strlen (ma0_3_5_7[0][0][1].a3) < 2);
  KEEP (strlen (ma0_3_5_7[0][0][2].a3) < 2);
  KEEP (strlen (ma0_3_5_7[0][0][6].a3) < 2);

  KEEP (strlen (ma0_3_5_7[1][0][0].a3) < 2);
  KEEP (strlen (ma0_3_5_7[2][0][1].a3) < 2);

  KEEP (strlen (ma0_3_5_7[1][1][0].a3) < 2);
  KEEP (strlen (ma0_3_5_7[2][4][6].a3) < 2);

  KEEP (strlen (ma0_3_5_7[0][0][0].a5) < 4);
  KEEP (strlen (ma0_3_5_7[0][0][1].a5) < 4);
  KEEP (strlen (ma0_3_5_7[0][0][2].a5) < 4);
  KEEP (strlen (ma0_3_5_7[0][0][6].a5) < 4);

  KEEP (strlen (ma0_3_5_7[1][0][0].a5) < 4);
  KEEP (strlen (ma0_3_5_7[2][0][1].a5) < 4);

  KEEP (strlen (ma0_3_5_7[1][1][0].a5) < 4);
  KEEP (strlen (ma0_3_5_7[2][4][6].a5) < 4);

  KEEP (strlen (ma0_3_5_7[0][0][0].a7_3[0]) < 2);
  KEEP (strlen (ma0_3_5_7[2][4][6].a7_3[2]) < 2);

  KEEP (strlen (ma0_3_5_7[0][0][0].a5_7[0]) < 6);
  KEEP (strlen (ma0_3_5_7[2][4][6].a5_7[4]) < 6);

  /* Again, verify that the .a3 array isn't assumed to necessarily
     be nul-terminated.  */
  KEEP (strlen (ma0_3_5_7[0][0][0].a3) > 2);
  KEEP (strlen (ma0_3_5_7[0][0][6].a3) > 2);
  KEEP (strlen (ma0_3_5_7[0][0][i].a3) > 2);
}

void keep_member_arrays_ptr (struct MemArrays0 *ma0,
			     struct MemArraysX *max,
			     struct MemArrays7 *ma7,
			     int i)
{
  KEEP (strlen (ma0->a7_3[0]) > 0);
  KEEP (strlen (ma0->a7_3[0]) < 2);
  KEEP (strlen (ma0->a7_3[1]) < 2);
  KEEP (strlen (ma0->a7_3[6]) < 2);
  KEEP (strlen (ma0->a7_3[6]) < 2);
  KEEP (strlen (ma0->a7_3[i]) > 0);
  KEEP (strlen (ma0->a7_3[i]) < 2);
  KEEP (strlen (ma0->a7_3[i]) < 2);

  /* Again, verify that the member array isn't assumed to necessarily
     be nul-terminated.  */
  KEEP (strlen (ma0->a7_3[0]) > sizeof ma0->a7_3);
  KEEP (strlen (ma0->a7_3[i]) > sizeof ma0->a7_3);

  KEEP (strlen (ma0->a5_7[0]) < 5);
  KEEP (strlen (ma0[0].a5_7[0]) < 5);
  KEEP (strlen (ma0[1].a5_7[0]) < 5);
  KEEP (strlen (ma0[9].a5_7[0]) < 5);
  KEEP (strlen (ma0[9].a5_7[4]) < 5);
  KEEP (strlen (ma0[i].a5_7[4]) < 5);
  KEEP (strlen (ma0[i].a5_7[i]) < 5);

  /* Same as above.  */
  KEEP (strlen (ma0[i].a5_7[i]) > sizeof ma0[i].a5_7);

  KEEP (strlen (ma0->a0) < DIFF_MAX - 2);
  KEEP (strlen (ma0->a0) < 999);
  KEEP (strlen (ma0->a0) < 1);

  KEEP (strlen (max->ax) < DIFF_MAX - 2);
  KEEP (strlen (max->ax) < 999);
  KEEP (strlen (max->ax) < 1);

  KEEP (strlen (ma7->a7) < DIFF_MAX - 2);
  KEEP (strlen (ma7->a7) < 999);
  KEEP (strlen (ma7->a7) < 1);
}

void keep_pointers (const char *s)
{
  KEEP (strlen (ptr) < DIFF_MAX - 2);
  KEEP (strlen (ptr) < 999);
  KEEP (strlen (ptr) < 1);

  KEEP (strlen (s) < DIFF_MAX - 2);
  KEEP (strlen (s) < 999);
  KEEP (strlen (s) < 1);
}


/* { dg-final { scan-tree-dump-times "call_in_true_branch_not_eliminated_" 0 "optimized" } }
   { dg-final { scan-tree-dump-times "call_in_false_branch_not_eliminated_" 0 "optimized" } }

   { dg-final { scan-tree-dump-times "call_made_in_true_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 119 "optimized" } }
   { dg-final { scan-tree-dump-times "call_made_in_false_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 119 "optimized" } } */