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

    <tr>
        <th>Summary</th>
        <td>
            Assert when combining store in RISCVTargetLowering::PerformDAGCombine
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:RISC-V
      </td>
    </tr>

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

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

<pre>
    This IR
```
define void @bar(ptr %p, ptr %q) {
  %v = insertelement <64 x i64> zeroinitializer, i64 0, i32 1
  %trunc = trunc <64 x i64> %v to <64 x i1>
  %p1 = getelementptr i8, ptr %p, i32 0
  %p2 = getelementptr i8, ptr %p, i32 8
  store <64 x i1> %trunc, ptr %p1
  store <8 x i8> zeroinitializer, ptr %p2
  ret void
}
```
And running `llc -mtriple riscv64 -mattr=+zve64x` results in
```
llc: /root/llvm-project/llvm/lib/Support/APInt.cpp:370: void llvm::APInt::insertBits(const llvm::APInt&, unsigned int): Assertion `(subBitWidth + bitPosition) <= BitWidth && "Illegal bit insertion"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/llc -o /app/output.s -x86-asm-syntax=intel -mtriple riscv64 -mattr=+zve64x <source>
1.      Running pass 'Function Pass Manager' on module '<source>'.
2.      Running pass 'RISC-V DAG->DAG Pattern Instruction Selection' on function '@bar'
 #0 0x000000000339de18 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x339de18)
 #1 0x000000000339b70c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f9477089420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f9476b4c00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f9476b2b859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007f9476b2b729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007f9476b3cfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x00000000032bde80 llvm::APInt::insertBits(llvm::APInt const&, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x32bde80)
 #8 0x00000000018a8137 llvm::RISCVTargetLowering::PerformDAGCombine(llvm::SDNode*, llvm::TargetLowering::DAGCombinerInfo&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x18a8137)
```

This was found when experimenting to create a reproducer for a bug seen after https://reviews.llvm.org/D156349 . So it's hand written IR. No idea if this would be common in reality.

Here is a godbolt example: https://godbolt.org/z/Yo5d7o84q
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVt1u2zwSfRr6ZmCDomRKuvCFE8f9AnxbBHHRxV4FlDiW2VKkSlKJk6dfUJITO80C2d0G-aGZmTNnZg45FN6rxiCuyPKKLDcz0YeDdavqh-1wVln5vPp2UB5u7wndELomnE7fw0eJe2UQHq2SQDJaCUdY0QUHhC07wq5hWv8irASSX41eELcegaQbUMajC6ixRROApNc8gyMonpH0Bl7QWWVUUEKrF3QRT_EM6LBIGSRncMH1ph4gT6sLqCFgsG_bCUlvzty7ZPBt8MQlElfFWQrdKSo9d2OfditObj5Yh5dEXhM4d0x-cyiiffEfKnNyYyc3h2FozNS4fPNhB9dGguuNUaYBwqnWNczb4FSnEZzy9SPPYN6KEBxJN4RdvTwiz46EU3Doex08KPMhsNY1SddA2NZZGwjbav3Yzjtnf2B9-hj_qIqw7a7vOuvi9vru1oRF3XUkXac5jRCDugbzdE3S9WAxLkf1XKngCStqa3z4zY7xWJzeDCqXoOJWGVHXPvoqa2LahBW-r65U-KeS4QCEXUGlwp31KloM4k2vY6fPTDhhHAhjt1pjI3R0mOQ8uDDCctgLpVEuxorc_X2z3t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