Daily bump.
[official-gcc.git] / gcc / testsuite / gcc.dg / strlenopt-66.c
blob469f3fd8457ef0c55a7224a087180ffb9a10005f
1 /* PRE tree-optimization/90626 - fold strcmp(a, b) == 0 to zero when
2 one string length is exact and the other is unequal
3 { dg-do run }
4 { dg-options "-O2 -Wall" } */
6 #include "strlenopt.h"
8 #define A(expr) \
9 ((expr) \
10 ? (void)0 \
11 : (__builtin_printf ("assertion failed on line %i: %s\n", \
12 __LINE__, #expr), \
13 __builtin_abort ()))
16 __attribute__ ((noclone, noinline, noipa)) void
17 clobber (void *p, int x, size_t n)
19 for (volatile unsigned char *q = p; n--; )
20 *q = x;
23 __attribute__ ((noclone, noinline, noipa)) void
24 test_strcmp (void)
26 char a[8], b[8];
27 strcpy (a, "1235");
28 strcpy (b, "1234");
30 A (strcmp (a, b));
32 clobber (a, 0, sizeof a);
33 clobber (b, 0, sizeof b);
34 clobber (b + 4, '5', 1);
36 memcpy (a, "1234", 4);
37 memcpy (b, "1234", 4);
39 A (0 > strcmp (a, b));
40 A (0 < strcmp (b, a));
43 __attribute__ ((noclone, noinline, noipa)) void
44 test_strncmp (void)
46 char a[8], b[8];
47 strcpy (a, "1235");
48 strcpy (b, "1234");
50 A (0 == strncmp (a, b, 1));
51 A (0 == strncmp (a, b, 2));
52 A (0 == strncmp (a, b, 3));
53 A (0 < strncmp (a, b, 4));
54 A (0 > strncmp (b, a, 4));
56 clobber (a, 0, sizeof a);
57 clobber (b, 0, sizeof b);
58 clobber (b + 4, '5', 1);
60 memcpy (a, "1234", 4);
61 memcpy (b, "1234", 4);
63 A (0 == strncmp (a, b, 4));
64 A (0 > strncmp (a, b, 5));
65 A (0 < strncmp (b, a, 5));
69 __attribute__ ((noclone, noinline, noipa)) void
70 test_strncmp_a4_cond_s5_s2_2 (const char *s, int i)
72 char a4[4];
73 strcpy (a4, s);
74 A (0 == strncmp (a4, i ? "12345" : "12", 2));
78 __attribute__ ((noclone, noinline, noipa)) void
79 test_strncmp_a4_cond_a5_s2_5 (const char *s, const char *t, int i)
81 char a4[4], a5[5];
82 strcpy (a4, s);
83 strcpy (a5, t);
84 A (0 == strncmp (a4, i ? a5 : "12", 5));
87 __attribute__ ((noclone, noinline, noipa)) void
88 test_strncmp_a4_cond_a5_a3_n (const char *s1, const char *s2, const char *s3,
89 int i, unsigned n)
91 char a3[3], a4[4], a5[5];
92 strcpy (a3, s1);
93 strcpy (a4, s2);
94 strcpy (a5, s3);
95 A (0 == strncmp (a4, i ? a5 : a3, n));
99 int main (void)
101 test_strcmp ();
102 test_strncmp ();
103 test_strncmp_a4_cond_s5_s2_2 ("12", 0);
104 test_strncmp_a4_cond_a5_s2_5 ("12", "1234", 0);
106 test_strncmp_a4_cond_a5_a3_n ("12", "1", "1", 0, 1);
107 test_strncmp_a4_cond_a5_a3_n ("", "1", "1234", 1, 1);
109 test_strncmp_a4_cond_a5_a3_n ("12", "12", "1", 0, 2);
110 test_strncmp_a4_cond_a5_a3_n ("", "12", "1234", 1, 2);
112 test_strncmp_a4_cond_a5_a3_n ("12", "123", "1", 0, 2);
113 test_strncmp_a4_cond_a5_a3_n ("", "123", "1234", 1, 3);