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

    <tr>
        <th>Summary</th>
        <td>
            [x86_64] `insertelement` after `extractelement` for vxi1 crashes when targeting `sapphirerapids` / `znver4` CPU
        </td>
    </tr>

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

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

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

<pre>
    ## Description

When targeting Intel `sapphirerapids` or AMD `znver4` CPU, the following code crashes `x86_64` backend with assertion `Op.getScalarValueSizeInBits() == BitWidth && "Mask size mismatches value type size!"` failed during `TargetLowering::SimplifyDemandedBits` in 'X86 DAG->DAG Instruction Selection' pass.

The problem doesn't exist when targeting `znver3`, `alderlake`, or `raptorlake` CPUs, or when CPU is not specified.

## Minimal Reproduction

https://godbolt.org/z/s7jcjbfbr

### Code

```ll
define void @f(ptr %0, <32 x i1> %1, i32 %2) {
BB:
  %E = extractelement <32 x i1> %1, i32 3
  %I = insertelement <32 x i1> %1, i1 %E, i32 %2
  store <32 x i1> %I, ptr %0
  ret void
}
```

### Stack Trace

```console
llc: /root/llvm-project/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp:1075: bool llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, const llvm::APInt&, const llvm::APInt&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const: Assertion `Op.getScalarValueSizeInBits() == BitWidth && "Mask size mismatches value type size!"' 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=x86_64 -mcpu=znver4 <source>
1.      Running pass 'Function Pass Manager' on module '<source>'.
2.      Running pass 'X86 DAG->DAG Instruction Selection' on function '@f'
 #0 0x0000000003398418 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3398418)
 #1 0x0000000003395d0c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f6b2d987420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f6b2d44a00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f6b2d429859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007f6b2d429729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007f6b2d43afd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x00000000031cc515 llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x31cc515)
 #8 0x0000000001d05986 llvm::X86TargetLowering::SimplifyDemandedBitsForTargetNode(llvm::SDValue, llvm::APInt const&, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x1d05986)
 #9 0x00000000031c3adb llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x31c3adb)
#10 0x00000000031c537e llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x31c537e)
#11 0x00000000031c4baa llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x31c4baa)
#12 0x0000000001d069f9 llvm::X86TargetLowering::SimplifyDemandedBitsForTargetNode(llvm::SDValue, llvm::APInt const&, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x1d069f9)
#13 0x00000000031c3adb llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x31c3adb)
#14 0x00000000031d7299 llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::KnownBits&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x31d7299)
#15 0x00000000031d7459 llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::TargetLowering::DAGCombinerInfo&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x31d7459)
#16 0x0000000001ce887e combineVectorShiftImm(llvm::SDNode*, llvm::SelectionDAG&, llvm::TargetLowering::DAGCombinerInfo&, llvm::X86Subtarget const&) X86ISelLowering.cpp:0:0
#17 0x0000000001cf5149 llvm::X86TargetLowering::PerformDAGCombine(llvm::SDNode*, llvm::TargetLowering::DAGCombinerInfo&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x1cf5149)
#18 0x0000000003019451 (anonymous namespace)::DAGCombiner::combine(llvm::SDNode*) DAGCombiner.cpp:0:0
#19 0x000000000301af0c (anonymous namespace)::DAGCombiner::Run(llvm::CombineLevel) DAGCombiner.cpp:0:0
#20 0x000000000301d50c llvm::SelectionDAG::Combine(llvm::CombineLevel, llvm::AAResults*, llvm::CodeGenOpt::Level) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x301d50c)
#21 0x000000000316c6e7 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x316c6e7)
#22 0x000000000316ff88 llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x316ff88)
#23 0x0000000003171c82 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (.part.0) SelectionDAGISel.cpp:0:0
#24 0x0000000001b894bc (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) X86ISelDAGToDAG.cpp:0:0
#25 0x0000000002680049 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#26 0x0000000002bf8bb9 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2bf8bb9)
#27 0x0000000002bf8df1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2bf8df1)
#28 0x0000000002bf9612 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2bf9612)
#29 0x000000000075e092 compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#30 0x000000000069a7e6 main (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x69a7e6)
#31 0x00007f6b2d42b083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#32 0x0000000000754a4e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x754a4e)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWl9z26jb_TTkhrEHof8XuXDiOJt5m3czdbfbuwySHtlsJdAAapx--t-AZMco3jTbzXZn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