[llvm-bugs] [Bug 35568] New: [Clang-optimization] wrong code for all optimizations with an union assignment in a loop

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Dec 7 18:18:10 PST 2017


https://bugs.llvm.org/show_bug.cgi?id=35568

            Bug ID: 35568
           Summary: [Clang-optimization] wrong code for all optimizations
                    with an union assignment in a loop
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: yangyibiao at nju.edu.cn
                CC: llvm-bugs at lists.llvm.org

$ clang -v
clang version 6.0.0-svn320088-1~exp1 (trunk)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.2.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64


$ cat small.c
int printf(const char *, ...);
union {
  long a;
  int b;
  int c;
} d;
int *e = &d.c;
char f, g;
int main() {
  for (; f >= 0; f--) {
    long *h = &d.a;
    *h = g;
    *e = 1;
  }
  printf("%d\n", d.b);
}


$ clang -O0 small.c; ./a.out
1
$ clang -O1 small.c; ./a.out
0
$ clang -O2 small.c; ./a.out
0
$ clang -O3 small.c; ./a.out
0
$ clang -Ofast small.c; ./a.out
0

As we can find that, when no optimization is outcome is 1. When enabling
optimization, the output is 0. When remoing the for loop in the main function,
the result is correct. 

$ cat small-correct.c
int printf(const char *, ...);
union {
  long a;
  int b;
  int c;
} d;
int *e = &d.c;
char f, g;
int main() {
  // for (; f >= 0; f--) {
    long *h = &d.a;
    *h = g;
    *e = 1;
  // }
  printf("%d\n", d.b);
}

$ clang -O0 small-correct.c; ./a.out
1
$ clang -O1 small-correct.c; ./a.out
1
$ clang -O2 small-correct.c; ./a.out
1
$ clang -O3 small-correct.c; ./a.out
1
$ clang -Ofast small-correct.c; ./a.out
1

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171208/16df6180/attachment.html>


More information about the llvm-bugs mailing list