Issue 74240
Summary Wrong code at -O2/3 since `clang-14.0.0`
Labels new issue
Assignees
Reporter jun-wei-zeng
    Compiler explorer: https://godbolt.org/z/97Yv6dEa7

I compiled the following code with `clang-15.0.0 and above version` at `-O2/3`, and the program output `nothing` and return `48`. And when I compiled with `clang-14.0.0` at `-O2/3`, and the program output `1` and return `0`.

So, this bug exists in `clang-14.0.0 and above version`. 

If the code is compiled correctly, the execution should output `nothing` and return `0`.


```c
$ cat test.c
int printf(const char*, ...);
union a {
  int d, b[4];
};
int main() {
  union a c = {0};
  int d = 0;
  for (; d < 4; ++d)
    if (c.b[d])
        printf("1\n"); 
}
$
$ clang-16.0.0 -O1 test.c; ./a.out
$ echo $?
0
$
$ clang-16.0.0 -O2 test.c; ./a.out
$ echo $?
48
$
$ clang-16.0.0 -O3 test.c; ./a.out
$ echo $?
48
$
$ clang-14.0.0 -O2 test.c; ./a.out
1
$ echo $?
0
$
$ clang-16.0.0 --version
clang version 16.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
$
$ clang-14.0.0 --version
clang version 14.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to