| 1 |
// Test for std::byte aliasing properties. |
| 2 |
// { dg-do run { target c++17 } } |
| 3 |
// { dg-options "-O3" } |
| 4 |
|
| 5 |
#include <cstddef> |
| 6 |
|
| 7 |
using byte = std::byte; |
| 8 |
|
| 9 |
enum class notbyte: unsigned char {} *np; |
| 10 |
|
| 11 |
int main() |
| 12 |
{ |
| 13 |
int x; |
| 14 |
|
| 15 |
/* Stores through byte* can alias int, so the compiler can't optimize |
| 16 |
"x != 0". */ |
| 17 |
byte *p = (byte*)&x; |
| 18 |
x = 42; |
| 19 |
for (int i = 0; i < 4; ++i) |
| 20 |
p[i] = byte(0); |
| 21 |
if (x != 0) |
| 22 |
__builtin_abort(); |
| 23 |
|
| 24 |
/* Stores through notbyte* mustn't alias int, so at -O3 the compiler should |
| 25 |
optimize "x != 42" to false. */ |
| 26 |
notbyte *np = (notbyte*)&x; |
| 27 |
x = 42; |
| 28 |
for (int i = 0; i < 4; ++i) |
| 29 |
np[i] = notbyte(0); |
| 30 |
if (x != 42) |
| 31 |
__builtin_abort(); |
| 32 |
} |