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

    <tr>
        <th>Summary</th>
        <td>
            [SLPVectorizer][RISC-V] rv64gcv miscompile with `slp-vectorizer` pass
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          patrick-rivos
      </td>
    </tr>
</table>

<pre>
    Testcase:
```c
int d = 16;
unsigned a = 3;
short e[18], f[18];
unsigned short *g = e;
int main() {
 for (long h = 0; h < 8; ++h) {
    e[h] = -1;
    f[h] = -2;
  }
 for (short h = 1; h; h += 5)
    for (int i = 0; i < d; i += 4)
 a = ({
        int j = 40 ? g[i] <= (unsigned)(1 ? f[i] : 0) : 0;
 a < j ? 0 : j;
      });
  __builtin_printf("%u\n", a);
}
```

Commands:
```bash
> /scratch/tc-testing/tc-apr-23/build-rv64gcv/build-llvm-linux/bin/clang -fno-strict-aliasing -march=rv64gcv -flto -O3 red.c -o red.out
> /scratch/tc-testing/tc-apr-23/build-rv64gcv/bin/qemu-riscv64 red.out
0
> /scratch/tc-testing/tc-apr-23/build-rv64gcv/build-llvm-linux/bin/clang -fno-strict-aliasing red.c -o red.out
> /scratch/tc-testing/tc-apr-23/build-rv64gcv/bin/qemu-riscv64 red.out
1
```

Reduced LLVM IR:
```llvm ir
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64-unknown-linux-gnu"

@.str = constant [4 x i8] c"%u\0A\00"

define i32 @main(ptr %a, ptr %f, i16 %0) #0 {
for.cond6.preheader.us.preheader:
  %1 = load i16, ptr %f, align 2
  %conv.us = zext i16 %1 to i32
  %conv13.us = sext i16 %0 to i32
 %cmp14.us.not = icmp ule i32 %conv.us, %conv13.us
  %conv.us.1 = zext i16 0 to i32
  %conv13.us.1 = sext i16 0 to i32
  %cmp14.us.1 = icmp ule i32 %conv.us.1, %conv13.us.1
  %.not5.not7.not10.not13 = and i1 %cmp14.us.not, %cmp14.us.1
  %conv.us.2 = zext i16 0 to i32
 %conv13.us.2 = sext i16 0 to i32
  %cmp14.us.2 = icmp ule i32 %conv.us.2, %conv13.us.2
  %conv.us.3 = zext i16 0 to i32
  %conv13.us.3 = sext i16 0 to i32
  %cmp14.us.3 = icmp ule i32 %conv.us.3, %conv13.us.3
  %2 = and i1 %.not5.not7.not10.not13, %cmp14.us.3
  %narrow = and i1 %2, %cmp14.us.2
  %cond.us.3 = zext i1 %narrow to i32
  %call = tail call i32 (ptr, ...) @printf(ptr @.str, i32 %cond.us.3)
  ret i32 0
}

declare i32 @printf(ptr, ...)

attributes #0 = { "target-features"="+64bit,+a,+v" }
```

Commands:
```
/scratch/tc-testing/tc-apr-23/build-rv64gcv/build-llvm-linux/bin/clang $1 -o baseline.out

/scratch/tc-testing/tc-apr-23/build-rv64gcv/build-llvm-linux/bin/opt -passes=slp-vectorizer $1 > opt.bc
/scratch/tc-testing/tc-apr-23/build-rv64gcv/build-llvm-linux/bin/clang opt.bc -o opt.out

/scratch/tc-testing/tc-apr-23/build-rv64gcv/bin/qemu-riscv64 opt.out
0
/scratch/tc-testing/tc-apr-23/build-rv64gcv/bin/qemu-riscv64 baseline.out
1
```

Found via fuzzer

@alexey-bataev
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV82O2zYQfhr6Qkjgj2RLBx_sdQ0ESNEiKXINKImSuKFIlaS8m336gpQsy3ZgpE3TLhY0f2a--WY4HFLMWtEozrcg3YP0sGKDa7XZ9swZUX6JjDhpuyp09XX7B7euZJYDugPoANAOrNH4X45joRysIKAHiNeA7sfJQQX8CrKwQucF22rjIAfpHmcgPQDyBOt5cKc9SgOyawIMnyW80Y4JBUgGSA7BZpqHtTYQkExq1cA2KCFA96H7BDPfBWQPyL69VoMwcGpBeghKEZ5N-bX6ao0s1sDmcG15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