| 1 |
/* PR tree-optimization/90879 - fold zero-equality of strcmp between |
| 2 |
a longer string and a smaller array |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-O2 -Wall -Wno-string-compare -fdump-tree-optimized -ftrack-macro-expansion=0" } */ |
| 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 |
void clobber (void*, ...); |
| 16 |
|
| 17 |
struct S { char a4[4], c; }; |
| 18 |
|
| 19 |
extern char a4[4]; |
| 20 |
extern char b4[4]; |
| 21 |
|
| 22 |
/* Verify that comparison of string literals with arrays with unknown |
| 23 |
content but size that prevents them from comparing equal is folded |
| 24 |
to a constant. */ |
| 25 |
|
| 26 |
void test_array_lit (void) |
| 27 |
{ |
| 28 |
A (strcmp (a4, "1234")); clobber (a4); |
| 29 |
A (strcmp (a4, "12345")); clobber (a4); |
| 30 |
A (strcmp (a4, "123456")); clobber (a4); |
| 31 |
A (strcmp ("1234", a4)); clobber (a4); |
| 32 |
A (strcmp ("12345", a4)); clobber (a4); |
| 33 |
A (strcmp ("123456", a4)); clobber (a4); |
| 34 |
} |
| 35 |
|
| 36 |
void test_memarray_lit (struct S *p) |
| 37 |
{ |
| 38 |
#if 0 |
| 39 |
/* Member arrays not handled due to the fix for PR 92765. */ |
| 40 |
A (strcmp (p->a4, "1234")); |
| 41 |
A (strcmp (p->a4, "12345")); |
| 42 |
A (strcmp (p->a4, "123456")); |
| 43 |
|
| 44 |
A (strcmp ("1234", p->a4)); |
| 45 |
A (strcmp ("12345", p->a4)); |
| 46 |
A (strcmp ("123456", p->a4)); |
| 47 |
#endif |
| 48 |
} |
| 49 |
|
| 50 |
/* Verify that the equality of empty strings is folded. */ |
| 51 |
|
| 52 |
void test_empty_string (void) |
| 53 |
{ |
| 54 |
A (0 == strcmp ("", "")); |
| 55 |
|
| 56 |
*a4 = '\0'; |
| 57 |
A (0 == strcmp (a4, "")); |
| 58 |
A (0 == strcmp ("", a4)); |
| 59 |
A (0 == strcmp (a4, a4)); |
| 60 |
|
| 61 |
char s[8] = ""; |
| 62 |
A (0 == strcmp (a4, s)); |
| 63 |
|
| 64 |
a4[1] = '\0'; |
| 65 |
b4[1] = '\0'; |
| 66 |
A (0 == strcmp (a4 + 1, b4 + 1)); |
| 67 |
|
| 68 |
a4[2] = '\0'; |
| 69 |
b4[2] = '\0'; |
| 70 |
A (0 == strcmp (&a4[2], &b4[2])); |
| 71 |
|
| 72 |
#if 0 |
| 73 |
/* The following isn't handled yet due to PR 92155. */ |
| 74 |
clobber (a4, b4); |
| 75 |
|
| 76 |
memset (a4, 0, sizeof a4); |
| 77 |
memset (b4, 0, sizeof b4); |
| 78 |
A (0 == strcmp (a4, b4)); |
| 79 |
#endif |
| 80 |
} |
| 81 |
|
| 82 |
/* Verify that comparison of dynamically created strings with unknown |
| 83 |
arrays is folded. */ |
| 84 |
|
| 85 |
void test_array_copy (void) |
| 86 |
{ |
| 87 |
char s[8]; |
| 88 |
strcpy (s, "1234"); |
| 89 |
A (strcmp (a4, s)); |
| 90 |
|
| 91 |
strcpy (s, "12345"); |
| 92 |
A (strlen (s) == 5); |
| 93 |
A (strcmp (a4, s)); clobber (a4); |
| 94 |
|
| 95 |
strcpy (s, "123456"); |
| 96 |
A (strcmp (a4, s)); clobber (a4); |
| 97 |
|
| 98 |
strcpy (s, "1234"); |
| 99 |
A (strcmp (s, a4)); clobber (a4); |
| 100 |
|
| 101 |
strcpy (s, "12345"); |
| 102 |
A (strcmp (s, a4)); clobber (a4); |
| 103 |
|
| 104 |
strcpy (s, "123456"); |
| 105 |
A (strcmp (s, a4)); clobber (a4); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
void test_array_bounded (void) |
| 110 |
{ |
| 111 |
A (strncmp (a4, "12345", 5)); clobber (a4); |
| 112 |
A (strncmp ("54321", a4, 5)); clobber (a4); |
| 113 |
|
| 114 |
A (strncmp (a4, "123456", 5)); clobber (a4); |
| 115 |
A (strncmp ("654321", a4, 5)); clobber (a4); |
| 116 |
} |
| 117 |
|
| 118 |
void test_array_copy_bounded (void) |
| 119 |
{ |
| 120 |
char s[8]; |
| 121 |
strcpy (s, "12345"); |
| 122 |
A (strncmp (a4, s, 5)); clobber (a4); |
| 123 |
strcpy (s, "54321"); |
| 124 |
A (strncmp (s, a4, 5)); clobber (a4); |
| 125 |
|
| 126 |
strcpy (s, "123456"); |
| 127 |
A (strncmp (a4, s, 5)); clobber (a4); |
| 128 |
strcpy (s, "654321"); |
| 129 |
A (strncmp (s, a4, 5)); clobber (a4); |
| 130 |
} |
| 131 |
|
| 132 |
/* { dg-final { scan-tree-dump-not "abort|strcmp|strncmp" "optimized" } } */ |