Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

clang strict-aliasing optimizations vs unreachable code #40148

Open Quuxplusone opened 5 years ago

Quuxplusone commented 5 years ago
Bugzilla Link PR41178
Status NEW
Importance P enhancement
Reported by Pavel Stepanov (paul_st@list.ru)
Reported on 2019-03-21 04:30:18 -0700
Last modified on 2019-05-14 07:38:21 -0700
Version 7.0
Hardware PC Linux
CC hfinkel@anl.gov, k_satoda@f2.dion.ne.jp, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments r.c (753 bytes, text/x-csrc)
Blocks
Blocked by
See also
Created attachment 21640
test with unreachable code

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.
Quuxplusone commented 5 years ago

Attached r.c (753 bytes, text/x-csrc): test with unreachable code

Quuxplusone 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.
Quuxplusone 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 <stdio.h>

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);
}