| 1 |
/* PR tree-optimization/92412 - excessive errno aliasing assumption defeats |
| 2 |
optimization |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-O2 -Wall -fdump-tree-optimized" } */ |
| 5 |
|
| 6 |
typedef __SIZE_TYPE__ size_t; |
| 7 |
|
| 8 |
extern void* alloca (size_t); |
| 9 |
extern void* calloc (size_t, size_t); |
| 10 |
extern void* malloc (size_t); |
| 11 |
|
| 12 |
extern const char exta[4]; |
| 13 |
static char stata[] = "123"; |
| 14 |
|
| 15 |
void sink (const void*, ...); |
| 16 |
|
| 17 |
#define T(ptr, alloc) do { \ |
| 18 |
const char *p = ptr; \ |
| 19 |
if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0' \ |
| 20 |
|| __builtin_strlen (p) != 3) \ |
| 21 |
return; \ |
| 22 |
\ |
| 23 |
void *q = alloc; \ |
| 24 |
__builtin_strcpy (q, p); \ |
| 25 |
\ |
| 26 |
if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0' \ |
| 27 |
|| __builtin_strlen (p) != 3 \ |
| 28 |
|| __builtin_strlen (q) != 3) \ |
| 29 |
__builtin_abort (); \ |
| 30 |
\ |
| 31 |
sink (p, q); \ |
| 32 |
} while (0) |
| 33 |
|
| 34 |
|
| 35 |
void alloca_test_local (unsigned n) |
| 36 |
{ |
| 37 |
char loca[] = "123"; |
| 38 |
T (loca, alloca (n)); |
| 39 |
} |
| 40 |
|
| 41 |
void alloca_test_extern_const (unsigned n) |
| 42 |
{ |
| 43 |
T (exta, alloca (n)); |
| 44 |
} |
| 45 |
|
| 46 |
void alloca_test_static (unsigned n) |
| 47 |
{ |
| 48 |
T (stata, alloca (n)); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
// Verify fix for PR tree-optimization/92412. |
| 53 |
void calloc_test_local (unsigned m, unsigned n) |
| 54 |
{ |
| 55 |
char loca[] = "123"; |
| 56 |
T (loca, calloc (m, n)); |
| 57 |
} |
| 58 |
|
| 59 |
void calloc_test_extern_const (unsigned m, unsigned n) |
| 60 |
{ |
| 61 |
T (exta, calloc (m, n)); |
| 62 |
} |
| 63 |
|
| 64 |
void calloc_test_static (unsigned m, unsigned n) |
| 65 |
{ |
| 66 |
T (stata, calloc (m, n)); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
// Verify fix for PR tree-optimization/92412. |
| 71 |
void malloc_test_local (unsigned n) |
| 72 |
{ |
| 73 |
char loca[] = "123"; |
| 74 |
T (loca, malloc (n)); |
| 75 |
} |
| 76 |
|
| 77 |
void malloc_test_extern_const (unsigned n) |
| 78 |
{ |
| 79 |
T (exta, malloc (n)); |
| 80 |
} |
| 81 |
|
| 82 |
void malloc_test_static (unsigned n) |
| 83 |
{ |
| 84 |
T (stata, malloc (n)); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
#undef T |
| 89 |
#define T(ptr, n) do { \ |
| 90 |
const char *p = ptr; \ |
| 91 |
if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0' \ |
| 92 |
|| __builtin_strlen (p) != 3) \ |
| 93 |
return; \ |
| 94 |
\ |
| 95 |
char vla[n]; \ |
| 96 |
char *q = vla; \ |
| 97 |
__builtin_strcpy (q, p); \ |
| 98 |
\ |
| 99 |
if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0' \ |
| 100 |
|| __builtin_strlen (p) != 3 \ |
| 101 |
|| __builtin_strlen (q) != 3) \ |
| 102 |
__builtin_abort (); \ |
| 103 |
\ |
| 104 |
sink (p, vla); \ |
| 105 |
} while (0) |
| 106 |
|
| 107 |
|
| 108 |
void vla_test_local (unsigned n) |
| 109 |
{ |
| 110 |
char loca[] = "123"; |
| 111 |
T (loca, n); |
| 112 |
} |
| 113 |
|
| 114 |
void vla_test_extern_const (unsigned n) |
| 115 |
{ |
| 116 |
T (exta, n); |
| 117 |
} |
| 118 |
|
| 119 |
void vla_test_static (unsigned n) |
| 120 |
{ |
| 121 |
T (stata, n); |
| 122 |
} |
| 123 |
|
| 124 |
/* { dg-final { scan-tree-dump-not "abort" "optimized" } } */ |