llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.68k stars 11.86k forks source link

clang strict-aliasing optimizations vs unreachable code #40523

Open llvmbot opened 5 years ago

llvmbot commented 5 years ago
Bugzilla Link 41178
Version 7.0
OS Linux
Attachments test with unreachable code
Reporter LLVM Bugzilla Contributor
CC @hfinkel,@k-satoda

Extended Description

Lets consider example r.c from attachment

$ clang-7.0 -O0 r.c -o r0.out $ clang-7.0 -O1 r.c -fno-strict-aliasing -o r1.nsa.out $ clang-7.0 -O1 r.c -o r1.sa.out

$ ./r0.out this should be zero: 0

$ ./r1.nsa.out this should be zero: 0

$ ./r1.sa.out this should be zero: -1

static inline void attribute((always_inline)) longLongAssign( int cond, char ptr) { if ( cond ) { ((long long *)ptr) = 0; } }

I suppose test doesn't violate strict-aliasing rule, because 1) argument "ptr" has type "char *" 2) condition "cond" is always zero

Why I got different results?

P.S. I am new therefore apologize for my mistakes.

a62a2cf6-873b-4f58-ae25-048414c98fc7 commented 5 years ago

Here is a reduced example: https://wandbox.org/permlink/etMQoctaLhXPIoFl It prints "this should be zero: -1" with Clang 8.0.0.

include

void funcA(int g) { int grp[3] = {-1}; int i, k = 0; for (i = 0; i < 1; ++i) { if (grp[g] > 0) { ((long long)&grp[0]) = 0; } grp[k++] = 0; } printf("this should be zero: %d\n", grp[0]); }

int main(void) { funcA(1); }

hfinkel commented 5 years ago

1) argument "ptr" has type "char *"

But you're not accessing the variable via a char , but instead, you're casting to a long long .

2) condition "cond" is always zero

This seems more concerning.