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

    <tr>
        <th>Summary</th>
        <td>
            [AMDGPU] `mul` used with v1i16 causes crash during IR optimizations due to type mismatch
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    ## Description

For `amdgcn`, when `mul` is used with v1i16 causes crash during IR optimizations due to type mismatch.

## Cause

In function [`AMDGPUCodeGenPrepare::replaceMulWithMul24`](https://github.com/llvm/llvm-project/blob/12c6a41f5249fa97041754d38cac28877ed49611/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp#L509), 

https://github.com/llvm/llvm-project/blob/12c6a41f5249fa97041754d38cac28877ed49611/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp#L570-L573

`insertValues` will turn a vector of size 1 into a scalar due to the following logic:

https://github.com/llvm/llvm-project/blob/12c6a41f5249fa97041754d38cac28877ed49611/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp#L468-L472

As a result, when `replaceAllUsesWith` is called, the assertion `New->getType() == getType() && "replaceAllUses of value with new value of different type!"` failed because the old type (vector) and the new type (scaler) are not the same.

## Minimal Reproduction

https://godbolt.org/z/d411xPYPv

### Code

```ll
define <2 x i8> @f(<1 x i16> %0) {
BB:
  %B = mul <1 x i16> %0, <i16 42>
  %1 = bitcast <1 x i16> %B to <2 x i8>
  ret <2 x i8> %1
}
```

### Stack Trace

```
llc: /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/Value.cpp:506: void llvm::Value::doRAUW(llvm::Value *, llvm::Value::ReplaceMetadataUses): Assertion `New->getType() == getType() && "replaceAllUses of value with new value of different type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ./llvm-project/build-debug/bin/llc ./crash-reports/dagisel-amdgcn/1.ll -mtriple=amdgcn
1.      Running pass 'Function Pass Manager' on module './crash-reports/dagisel-amdgcn/1.ll'.
2.      Running pass 'AMDGPU IR optimizations' on function '@f'
 #0 0x0000000003af78fa llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:569:11
 #1 0x0000000003af7aab PrintStackTraceSignalHandler(void*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:636:1
 #2 0x0000000003af60f6 llvm::sys::RunSignalHandlers() /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Signals.cpp:104:5
 #3 0x0000000003af81d5 SignalHandler(int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x00007fe6d0720980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #5 0x00007fe6cf610e87 raise /build/glibc-uZu3wS/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #6 0x00007fe6cf6127f1 abort /build/glibc-uZu3wS/glibc-2.27/stdlib/abort.c:81:0
 #7 0x00007fe6cf6023fa __assert_fail_base /build/glibc-uZu3wS/glibc-2.27/assert/assert.c:89:0
 #8 0x00007fe6cf602472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
 #9 0x00000000030596bd llvm::Value::doRAUW(llvm::Value*, llvm::Value::ReplaceMetadataUses) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/Value.cpp:0:3
#10 0x0000000003059cd2 llvm::Value::replaceAllUsesWith(llvm::Value*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/Value.cpp:534:1
#11 0x0000000001b19503 (anonymous namespace)::AMDGPUCodeGenPrepare::replaceMulWithMul24(llvm::BinaryOperator&) const /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp:573:3
#12 0x0000000001b17cdd (anonymous namespace)::AMDGPUCodeGenPrepare::visitBinaryOperator(llvm::BinaryOperator&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp:1240:7
#13 0x0000000001b167bd llvm::InstVisitor<(anonymous namespace)::AMDGPUCodeGenPrepare, bool>::visitMul(llvm::BinaryOperator&) /home/henry/aflplusplus-isel/llvm-project/llvm/include/llvm/IR/Instruction.def:151:1
#14 0x0000000001b1600c llvm::InstVisitor<(anonymous namespace)::AMDGPUCodeGenPrepare, bool>::visit(llvm::Instruction&) /home/henry/aflplusplus-isel/llvm-project/llvm/include/llvm/IR/Instruction.def:151:1
#15 0x0000000001b15cf2 (anonymous namespace)::AMDGPUCodeGenPrepare::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp:1440:21
#16 0x0000000002f995d6 llvm::FPPassManager::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1430:23
#17 0x0000000002f9e402 llvm::FPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1476:16
#18 0x0000000002f99ea9 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1545:23
#19 0x0000000002f99a1d llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:16
#20 0x0000000002f9e6e1 llvm::legacy::PassManager::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1672:3
#21 0x0000000000d391ec compileModule(char**, llvm::LLVMContext&) /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:737:41
#22 0x0000000000d37592 main /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:418:13
#23 0x00007fe6cf5f3c87 __libc_start_main /build/glibc-uZu3wS/glibc-2.27/csu/../csu/libc-start.c:344:0
#24 0x0000000000d36d9a _start (./llvm-project/build-debug/bin/llc+0xd36d9a)
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVWVlv4zgS_jXOC2GDpO4HPzhJZyZAshOkj8Hui0FJlM1dSjJEKkf_-qmiZFtSG-ike9KZDWwdpMT6vrpY5aR1_ryccQ8-5FKarFE7q-pqRi9ndNUdr-qGzEIqynyTVXAx4xfkcSsrHCxbDUeiDGmNzMmjslvywBQLSSZgxJCsEWZL8rZR1YZc35Mali_VV4FCDIxLYmtin3eSlMqUwmbbxVB2j-wCFxuOX1ekaKsMVyGz4BwwrG4vf7v7fFHn8jdZ3TVyJxo581bwgWstMnnb6j8BHpy4jyyCyxmPt9buDD7Gr-Czgfk2XWR1CTdaP-xP811T_1dmFm5TXadwYjwLhc-KgPtJIZKI-iwK_NyLM5HxOI4imftJyNhgHYXvfRLNRuI6HdzDxRj3ItvtgPZNQJMZT1DfQ-7_cMwRncPBG1kxpKoysrFfhG6lQY95VFoT2zYVEeQBYIKP1QUx6qskjKgKnEIQkwktmoOTbCUpaq3rR3QlXW9Uhkr4v1GMH8bzGz_iQ8QrAzQbaVpth1HVe-xK688QQ-i1fZCBQrTM8VHUhjCoUxcCIf2XfJzPvA8A6BNEE7g2eA6ZeZfwIZNBHsIHTnwsBw3wgAbqwriSj_0tjOeqKGQjK-tidcZBFxwxFUIBIJJKF-4OVa3zLqBBXGdZFCqq3M3iqvtZtK_sZhuYqa17wohSnsoBt6pSpdDkXoIB8zabpqmJ9es8rbVd1M0G7r7CN_cZe7r7993Dt2u7FANGm_hs99G6G8hloSrA7V1w8kRUDMomM58WqFXvguEYC90gD6jTc3TevXl-fnBUgrPnaBcCqZOc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