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

    <tr>
        <th>Summary</th>
        <td>
            InstCombine calculated a wrong `insertelement` instruction
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            miscompilation,
            llvm:optimizations,
            llvm:instcombine
      </td>
    </tr>

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

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

<pre>
    I tried this IR:

```llvm
define <4 x i32> @src(i32 %arg) {
  %i = zext i32 %arg to i64
  %i1 = shl i64 %i, 32
  %i2 = or i64 %i1, 65
 %i3 = zext i64 %i2 to i128
  %i4 = shl i128 %i3, 64
  %i5 = bitcast i128 %i4 to <4 x i32>
  ret <4 x i32> %i5
}

define <4 x i32> @tgt(i32 %arg) {
  %i5 = insertelement <4 x i32> <i32 0, i32 0, i32 0, i32 poison>, i32 %arg, i64 3
  ret <4 x i32> %i5
}
```

I expect `src` to be:

```llvm
define <4 x i32> @src(i32 %arg) {
  %i5 = insertelement <4 x i32> <i32 0, i32 0, i32 65, i32 poison>, i32 %arg, i64 3
  ret <4 x i32> %i5
}
```

But I'm getting the same instructions as `tgt`.

Godbolt: https://llvm.godbolt.org/z/KnMEY4rTq
Alive2: https://alive2.llvm.org/ce/z/7FIJNi
From: https://github.com/rust-lang/rust/issues/121996
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VE1v4zYQ_TXUhYhBDfV50MGxo8INWqBFLz1S1FhmQYmuSGWz-fULUnIsJ_uBxWIXMGxi5r03M49jCmtVNyBWJL0nAHslhr8eCQBJ95GY3MmM1RyLGtN-rA7UjQpb6k7K0sPfhG8J2xN2-c7Y_NH6qZ9DLR7VgJTwXUKfqeJA-AMlCbOjJFAoDpRAKsaOQElJfj-TqA8qSvievuCzo1cYdYaqLFnD4oCzJ-0TIUJgRzmsMRAwZnyFxB6TpQvGR_iq3AKCUC2GYi2VXMvFUMzUIHbTVBpQjXJSWHdFJl7xxosLaUT31iQvs_ia79c2f8lT17lveTr3pQaLo0ONPQ7vyvKdV2B-ps8fzkZZM_jel8Cl2i44x79zpMvOrCc8UHw-o3SUZGFTMuaNa_Cn7tsPeJOlv9Cc-8nRA4G8px06p4aOuhNSK3r0zbtxkk6ZwVJhvX1-KTK2WQv8ZtrGaEf4lp6cO1vvKtQEam_jppuzG-O7rl8I1I_DHw__JuM__8_0rVZPCO_ZIsQ3QWQmS1wU8vrw-59qptej6d-TO-VOU7ORpidQj5N1d1oM3XImUCtrJ7QE6hjissyituJtyUsRYRXnrMzSGMosOlWIRV62eVGk-RGaYyoxk1krpWAsSdJjG6kKGCSMszROWRFnm1IcBWCclwkvJD82JGHYC6VfB4lC7apIGKSRFg1qu7yWvbLS9GelhXfcv5qwIwBhG_nWnJ3q1UvI2bdJf1PS9I0acHltx8qn7pqpsyRhWllnry045TRWh8G63UyiUmg5aeGwpYJ-GM3Q-du-2V7_t1ltRDSNuvqK66Gz-efuPJr_UN4YH-b_FAAA__9XE7Ug">