<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/114879>114879</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            WRONG code: GVN? Loop opts?
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            miscompilation
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          JonPsson1
      </td>
    </tr>
</table>

<pre>
    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 <npopov@redhat.com>
Date:   Wed Dec 6 14:17:18 2023 +0100

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

```
@nikic 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVtFvo7gT_muclxGRMRDIQx6Spqn6-53aqlrtnu7N2CZ4a-zINs1m__qTCSHQ5nbv8aQqYHvm-8Yz3wylzsm9FmKFsg3KtjPa-trY1f-MfnHO6HhWGn5aMerhaI3e47lEeIvwGi1w_9ctpfZwsFL7CpGCGe08sJpaQGSNyB3M53NElijZnK1b3ZFyCG6P2q-tRdlmgbLtbZMHZcpH7Z-f7gElW4gDZL_31_3rc7eJB9cz_QQAkXVv_-LtB_PBUBm9h1fh_uEEkfWrcBd_RBZj09ucE9qXL4PndfP2fRFZV61miBSILAHlvQ18uMvz0_0onOvmAApQmVCDossz_zHcvF_chXU-rMmmz-6E88z6yH-QPvnJ-KgK8Ge0e8CfXIcIehLykZWMacd-ABMiACt8azVM7n41Qfn2Yjx6xYBI_Ck_E8cL7tUiSGo4H8CuOm-o1J9LM8h4jJJtu5zdAga4qY5r3UeWgzi6ntqcvOiLvr6hJPig1GB-PRyaFBHCasHeXNv0YNmfKLvTiJDQXkHcoxiuaZj2_fj3UYOvBdDSvAuopBIBZ6RjV5tWcTDluzStU6fLxs3CkjsoWw_SAzfCgTY-aI2Z5iCV4HCUvgZE8qjSJpJaSS0Aokap9waiVlujVFS1SkUN_REx02qPkm2MSD6fxD29DFNU7yF6xhA11LIaJduf8WIYfBAZoHPTeoiOMCEOWp4jsutOe6xxbuMx6YUl-W-y_DaJv40D_yLFj6F9TlAK8DX18PD16VxfZxrha6n35whD-akDWnlhgwZQikdCkg6oOtKT66UT3A7eQqu5qObw3LmwTlvvwp7gKJQKlIEETNWpVBlzAHPwspE_qZdGu3NE0oFtNXxvnYdSVMYKkH4imjWU0gnmg3yPZ69fXJjnOV7kHBc0qbIFZ4RjsWDLrOSkyAhbpgmv8jShgTj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