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

    <tr>
        <th>Summary</th>
        <td>
            [VectorCombine] Crash occured while fold load-insertelement-store
        </td>
    </tr>

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

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

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

<pre>
    This is an IR sample that crashes.

```
define void @src_1_idx(ptr %q, i8 zeroext %s, i1 %idx) {
entry:
  %0 = load <16 x i8>, ptr %q
  %v1 = insertelement <16 x i8> %0, i8 %s, i1 %idx
  store <16 x i8> %v1, ptr %q
  ret void
}

define void @src_2_idx(ptr %q, i8 zeroext %s, i8 %idx) {
entry:
  %0 = load <256 x i8>, ptr %q
  %v1 = insertelement <256 x i8> %0, i8 %s, i8 %idx
  store <256 x i8> %v1, ptr %q
  ret void
}
```

and this is debugging message with crash dump
```
VECTORCOMBINE on src_2_idx
VC: Visiting:   %0 = load <16 x i8>, ptr %q, align 16
VC: Visiting:   %v1 = insertelement <16 x i8> %0, i8 %s, i1 %idx
VC: Visiting:   store <16 x i8> %v1, ptr %q, align 16
Assertion failed: (llvm::isUIntN(BitWidth, val) && "Value is not an N-bit unsigned value"), function APInt, file APInt.h, line 128.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: bin/opt -passes=vector-combine t1.ll -S -debug
1.      Running pass "function(vector-combine)" on module "t1.ll"
2.      Running pass "vector-combine" on function "src_2_idx"
 #0 0x00000001026ca6ac llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/b2sy/work/test3/bin/opt+0x1019566ac)
 #1 0x00000001026c8770 llvm::sys::RunSignalHandlers() (/Users/b2sy/work/test3/bin/opt+0x101954770)
 #2 0x00000001026cae7c SignalHandler(int, __siginfo*, void*) (/Users/b2sy/work/test3/bin/opt+0x101956e7c)
 #3 0x000000019733ede4 (/usr/lib/system/libsystem_platform.dylib+0x180482de4)
 #4 0x0000000197307f70 (/usr/lib/system/libsystem_pthread.dylib+0x18044bf70)
 #5 0x0000000197214908 (/usr/lib/system/libsystem_c.dylib+0x180358908)
 #6 0x0000000197213c1c (/usr/lib/system/libsystem_c.dylib+0x180357c1c)
 #7 0x00000001032ad794 isUpperSubvectorUndef(llvm::SDValue, llvm::SDLoc const&, llvm::SelectionDAG&) (.cold.2) (/Users/b2sy/work/test3/bin/opt+0x102539794)
 #8 0x00000001029c4990 canScalarizeAccess(llvm::VectorType*, llvm::Value*, llvm::Instruction*, llvm::AssumptionCache&, llvm::DominatorTree const&) (/Users/b2sy/work/test3/bin/opt+0x101c50990)
```

problem occured here 
```
// VectorCombine.cpp:1447
  unsigned IntWidth = Idx->getType()->getScalarSizeInBits();
  APInt Zero(IntWidth, 0);  
  APInt MaxElts(IntWidth, NumElements);  <<< crash
  ConstantRange ValidIndices(Zero, MaxElts);
  ConstantRange IdxRange(IntWidth, true);
```

I think this code is necessary because it checks to see if the index specified by insertelement is in an accessible range on the vector, so I think we need to add exception handling to fix the problem.

What do you think?

Note that this is not a real-world issue, but a bug that I discovered while writing the patch and creating the verification code.


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykV1Fv66gS_jXkZZQIYye2H_KQps3dSLvdqj2nK92XCsMk4RZDLuA0Ob_-Cpy2SdordbuVpRJgPoaPb5iBe6_WBnFKxldkfD3gXdhYN73j7vk3bpquHTRWHqY_NsqD8sANLO_B83arEcKGBxCO-w36EaGz-E3o8aMziStlEHZWSSAF9U48ZU9K7gmrtsEBYeP_EjYHVcEvdBb3IXb51JXFZppaAymvCJ2hCe5A8rgGxEEKJL8GbbkEks-zCexBVSS_ieZv6Me5uyxNVsajC6ixRRPOrRLi0ZmPTkQYH6zDj0a77MOCDkPac2SjvO5p-YQK9kUqqr9JBRt_i4sTs0_JqD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