| 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" } */ |
| 5 |
|
| 6 |
#include "strlenopt.h" |
| 7 |
|
| 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 ())) |
| 14 |
|
| 15 |
|
| 16 |
__attribute__ ((noclone, noinline, noipa)) void |
| 17 |
clobber (void *p, int x, size_t n) |
| 18 |
{ |
| 19 |
for (volatile unsigned char *q = p; n--; ) |
| 20 |
*q = x; |
| 21 |
} |
| 22 |
|
| 23 |
__attribute__ ((noclone, noinline, noipa)) void |
| 24 |
test_strcmp (void) |
| 25 |
{ |
| 26 |
char a[8], b[8]; |
| 27 |
strcpy (a, "1235"); |
| 28 |
strcpy (b, "1234"); |
| 29 |
|
| 30 |
A (strcmp (a, b)); |
| 31 |
|
| 32 |
clobber (a, 0, sizeof a); |
| 33 |
clobber (b, 0, sizeof b); |
| 34 |
clobber (b + 4, '5', 1); |
| 35 |
|
| 36 |
memcpy (a, "1234", 4); |
| 37 |
memcpy (b, "1234", 4); |
| 38 |
|
| 39 |
A (0 > strcmp (a, b)); |
| 40 |
A (0 < strcmp (b, a)); |
| 41 |
} |
| 42 |
|
| 43 |
__attribute__ ((noclone, noinline, noipa)) void |
| 44 |
test_strncmp (void) |
| 45 |
{ |
| 46 |
char a[8], b[8]; |
| 47 |
strcpy (a, "1235"); |
| 48 |
strcpy (b, "1234"); |
| 49 |
|
| 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)); |
| 55 |
|
| 56 |
clobber (a, 0, sizeof a); |
| 57 |
clobber (b, 0, sizeof b); |
| 58 |
clobber (b + 4, '5', 1); |
| 59 |
|
| 60 |
memcpy (a, "1234", 4); |
| 61 |
memcpy (b, "1234", 4); |
| 62 |
|
| 63 |
A (0 == strncmp (a, b, 4)); |
| 64 |
A (0 > strncmp (a, b, 5)); |
| 65 |
A (0 < strncmp (b, a, 5)); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
__attribute__ ((noclone, noinline, noipa)) void |
| 70 |
test_strncmp_a4_cond_s5_s2_2 (const char *s, int i) |
| 71 |
{ |
| 72 |
char a4[4]; |
| 73 |
strcpy (a4, s); |
| 74 |
A (0 == strncmp (a4, i ? "12345" : "12", 2)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
__attribute__ ((noclone, noinline, noipa)) void |
| 79 |
test_strncmp_a4_cond_a5_s2_5 (const char *s, const char *t, int i) |
| 80 |
{ |
| 81 |
char a4[4], a5[5]; |
| 82 |
strcpy (a4, s); |
| 83 |
strcpy (a5, t); |
| 84 |
A (0 == strncmp (a4, i ? a5 : "12", 5)); |
| 85 |
} |
| 86 |
|
| 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) |
| 90 |
{ |
| 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)); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
int main (void) |
| 100 |
{ |
| 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); |
| 105 |
|
| 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); |
| 108 |
|
| 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); |
| 111 |
|
| 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); |
| 114 |
} |