Issue 114879
Summary WRONG code: GVN? Loop opts?
Labels miscompilation
Assignees
Reporter JonPsson1
    cat wrong0.i
```
int printf(const char *, ...);
unsigned int IntArr[6];
unsigned int GlobIntONE = 1, GlobIntZERO = 0;
const unsigned int *GlobIntPtr = 0;
unsigned long Res;
unsigned long *ResPtr = &Res;
const unsigned int **GlobIntPtrPTr = &GlobIntPtr;
unsigned int *func() {
  int *GlobIntONEPtr = &GlobIntONE;
  for (int Idx = 0; Idx <= 7; Idx += 1) {
    int Idx2 = 1;
    if (Idx > 0) {
      for (; Idx2 <= 7; Idx2 += 1)
        ;
      return GlobIntONEPtr;
 }
  }
  0 != &GlobIntONEPtr;
  return &GlobIntZERO;
}

int main() {
  IntArr[GlobIntZERO] = GlobIntZERO;
  *GlobIntPtrPTr = func();
  unsigned char Byte = *GlobIntPtr;
  *ResPtr = Byte;
 printf("checksum = %X\n", Res);
}
```


In the above file, func() should obviously should return GlobIntONEPtr, but it does not if compiled with '-fno-inline  -mllvm -unroll-full-max-count=1'.

```
clang -O0 -march=z16 wrong0.i -o a.out -w -fno-inline  ; ./a.out
checksum = 1

clang -O3 -march=z16 wrong0.i -o a.out -w -fno-inline  ; ./a.out
checksum = 1

clang -O3 -march=z16 wrong0.i -o a.out -w -fno-inline  -mllvm -unroll-full-max-count=1 ; ./a.out
checksum = 0

```
It may be that GVN does something wrong, as after it @func() is always returning ptr undef. Or it could very well be some of the loop optimizations that is run just before it.

A bisect shows that

```
d77067d08a3f56dc2d0e6c95bd2852c943df743a is the first bad commit
commit d77067d08a3f56dc2d0e6c95bd2852c943df743a
Author: Nikita Popov <[email protected]>
Date:   Wed Dec 6 14:17:18 2023 +0100

 [ValueTracking] Add dominating condition support in computeKnownBits() (#73662)

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

Reply via email to