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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V] Miscompile on rv64gcv with -O[23]
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    Testcase:
```c
unsigned short a[][21];
long long p;
short c = 26161;
short h[21];
unsigned short a[21][21];
int main() {
  for (int k = 0; k < 21; ++k)
    h[k] = k;
  for (short k = 2; k < 18; k += 4)
    for (int l = 0; l < 21; l += c - 26158)
      a[k][l] = ({
                  int m = h[l];
                  int n = m > 0 ? m : 0;
                  n;
                }) > (h[l] ^ (h[l] ?: 8));
  for (int k = 0; k < 21; ++k)
    p ^= a[k][0];
  __builtin_printf("%llu\n", p);
}
```

Commands:
```
# O3
$ /scratch/ewlu/daily-upstream-build/build-gcv/bin/clang -march=rv64gcv -flto -O3 red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv 
$ QEMU_CPU=rv64,vlen=128,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /scratch/ewlu/daily-upstream-build/build-gcv/bin/qemu-riscv64 user-config.out 1
1

# O2
$ /scratch/ewlu/daily-upstream-build/build-gcv/bin/clang -march=rv64gcv -flto -O2 red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv 
$ QEMU_CPU=rv64,vlen=128,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /scratch/ewlu/daily-upstream-build/build-gcv/bin/qemu-riscv64 user-config.out 1
1

# O1
$ /scratch/ewlu/daily-upstream-build/build-gcv/bin/clang -march=rv64gcv -flto -O1 red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv 
$ QEMU_CPU=rv64,vlen=128,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /scratch/ewlu/daily-upstream-build/build-gcv/bin/qemu-riscv64 user-config.out 1
0

# x86
$ ./native.out
0
```

Bisected to 2b712693886a2a8339c0452f12f06b676e946d49 as the first bad commit

Tried replacing 
```c
      a[k][l] = ({
                  int m = h[l];
 int n = m > 0 ? m : 0;
                  n;
                }) > (h[l] ^ (h[l] ?: 8));
```
with
```c
      a[k][l] = (h[l] > 0 ? h[l] : 0) > (h[l] ^ (h[l] ?: 8));
```
but the error goes away. Same with eliminating the computation with `short c`

Found via fuzzer
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVk-Tm7gT_TTypQsXtADDwQf_CVW_Qyq_3SR7dQkhjNYCeSXBZObTb0nYY89kLqmdTe0hU1OmkV4_vdctQMxaeRyEWJNsS7L9go2u02YtHtS4qHXzuP4irOPMCkI3JN6QPJ7_OYk34xByG7CdNg7YTEGyLSb-Srck3ig9HCH8nOeBGcuB0D1gnuTJ_XD3MvmNBebZFyg5OOiZHAgWBEsgKz8I0GoDBAs_ewqrxYRuQ7gD9KsCwS3B7YlgGRIgLH8i2T7ATzP7M9GsYabCG1VSXGLc-pn0xnYnQN0EqDsB6prFIfLFyIpbNgS7p9mtuoryHi_-Xv-FMgRQd8m4GngLOQSkx3-AGAitQrwJGt9MGt6eIKt9KDr94LV1z1qzV_e08uzBH5avKvsjLTp7ag-9q0585_VwqEepnBwOZyMH14ZNgQQzpUaS7YZws4Pzswpv4G5f-zje7HTfs6Gxrza9j5HCJxqCFAhWlhvmeEew8o8MwaphUj1G49k6I1gfeTUNwSpcoyOffOz3asUVG44Q9czwjtC9mfL0yCeIWuU0RJ8oGNEsOUQaRitMxPXQyuNSjw6idn4sIt4xA1E76Mg6I7mLmJLM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