| 1 |
/* { dg-do run } |
| 2 |
* { dg-options "-std=gnu23 -O2" } |
| 3 |
*/ |
| 4 |
|
| 5 |
/* This test checks that different field offsets imply |
| 6 |
* that the types can be assumed not to alias |
| 7 |
* and that this is exploited during optimization. */ |
| 8 |
|
| 9 |
|
| 10 |
struct bar0 { int x; int f[3]; int y; }; |
| 11 |
|
| 12 |
[[gnu::noinline,gnu::noipa]] |
| 13 |
int test_bar0(struct bar0* a, void* b) |
| 14 |
{ |
| 15 |
a->x = 1; |
| 16 |
|
| 17 |
struct bar0 { int x; int f[4]; int y; }* p = b; |
| 18 |
p->x = 2; |
| 19 |
|
| 20 |
return a->x; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/* While these tests check that different structs with different |
| 25 |
* sizes in arrays pointed to by field members can alias, |
| 26 |
* even though the types are incompatible. */ |
| 27 |
|
| 28 |
|
| 29 |
struct bar1 { int x; int (*f)[3]; }; |
| 30 |
|
| 31 |
[[gnu::noinline,gnu::noipa]] |
| 32 |
int test_bar1(struct bar1* a, void* b) |
| 33 |
{ |
| 34 |
a->x = 1; |
| 35 |
|
| 36 |
struct bar1 { int x; int (*f)[3]; }* p = b; |
| 37 |
p->x = 2; |
| 38 |
|
| 39 |
return a->x; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
struct bar2 { int x; int (*f)[3]; }; |
| 44 |
|
| 45 |
[[gnu::noinline,gnu::noipa]] |
| 46 |
int test_bar2(struct bar2* a, void* b) |
| 47 |
{ |
| 48 |
a->x = 1; |
| 49 |
|
| 50 |
struct bar2 { int x; int (*f)[4]; }* p = b; |
| 51 |
p->x = 2; |
| 52 |
|
| 53 |
return a->x; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
/* This test checks that different structs with pointers to |
| 59 |
* different compatible arrays types can alias. */ |
| 60 |
|
| 61 |
|
| 62 |
struct bar3 { int x; int (*f)[3]; }; |
| 63 |
|
| 64 |
[[gnu::noinline,gnu::noipa]] |
| 65 |
int test_bar3(struct bar3* a, void* b) |
| 66 |
{ |
| 67 |
a->x = 1; |
| 68 |
|
| 69 |
struct bar3 { int x; int (*f)[]; }* p = b; |
| 70 |
p->x = 2; |
| 71 |
|
| 72 |
return a->x; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
int main() |
| 79 |
{ |
| 80 |
// control |
| 81 |
|
| 82 |
struct bar0 z0; |
| 83 |
|
| 84 |
if (1 != test_bar0(&z0, &z0)) |
| 85 |
__builtin_abort(); |
| 86 |
|
| 87 |
// this could be different |
| 88 |
struct bar1 z1; |
| 89 |
|
| 90 |
if (2 != test_bar1(&z1, &z1)) |
| 91 |
__builtin_abort(); |
| 92 |
|
| 93 |
struct bar2 z2; |
| 94 |
|
| 95 |
if (2 != test_bar2(&z2, &z2)) |
| 96 |
__builtin_abort(); |
| 97 |
|
| 98 |
struct bar3 z3; |
| 99 |
|
| 100 |
if (2 != test_bar3(&z3, &z3)) |
| 101 |
__builtin_abort(); |
| 102 |
|
| 103 |
|
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
|