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

    <tr>
        <th>Summary</th>
        <td>
            [AArch64/GlobalISel] `urem` with literal v2i16 as first operand crashes backend with error `cannot select`
        </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 AArch64, instruction `urem <2 x i16>` with the first operand being a literal vector causes `llc` to crash with error `cannot select: %10:fpr64(<2 x s32>) = G_ZEXT %3:fpr(<2 x s16>)`.

`<4 x i16>`, `<2 x i32>`, and `<2 x i64>` do not have the problem.
`<2 x i8>` crashes with another error `unable to legalize instruction: G_STORE`.

The first operand must be a literal to trigger the crash. 
The second operand must be either an identifier or a literal vector with at least one `0`-valued element to trigger the crash.

## Minimal Reproduction

https://godbolt.org/z/3hEYK5ebe

### Code

```ll
define void @f(<2 x i16> %0) {
BB:
  %A = alloca <2 x i16>
  %B = urem <2 x i16> <i16 1, i16 1>, %0
  store <2 x i16> %B, <2 x i16>* %A
  ret void
}
```

### Stack Trace

```
LLVM ERROR: cannot select: %10:fpr64(<2 x s32>) = G_ZEXT %3:fpr(<2 x s16>) (in function: f)
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/gisel-aarch64/5.ll -mtriple=aarch64 -global-isel
1.      Running pass 'Function Pass Manager' on module './crash-reports/gisel-aarch64/5.ll'.
2.      Running pass 'InstructionSelect' 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 0x00007f1782b32980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #5 0x00007f1781a22e87 raise /build/glibc-uZu3wS/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #6 0x00007f1781a247f1 abort /build/glibc-uZu3wS/glibc-2.27/stdlib/abort.c:81:0
 #7 0x0000000003a1d6d4 llvm::report_fatal_error(llvm::Twine const&, bool) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/ErrorHandling.cpp:125:5
 #8 0x000000000405d3cf reportGISelDiagnostic(llvm::DiagnosticSeverity, llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, llvm::MachineOptimizationRemarkMissed&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp:259:5
 #9 0x000000000405d435 llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, llvm::MachineOptimizationRemarkMissed&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp:273:1
#10 0x000000000405d60f llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, char const*, llvm::StringRef, llvm::MachineInstr const&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp:286:1
#11 0x0000000003fcbdbd llvm::InstructionSelect::runOnMachineFunction(llvm::MachineFunction&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp:191:9
#12 0x00000000028b22b5 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/MachineFunctionPass.cpp:91:8
#13 0x0000000002f995d6 llvm::FPPassManager::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1430:23
#14 0x0000000002f9e402 llvm::FPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1476:16
#15 0x0000000002f99ea9 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1545:23
#16 0x0000000002f99a1d llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:16
#17 0x0000000002f9e6e1 llvm::legacy::PassManager::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1672:3
#18 0x0000000000d391ec compileModule(char**, llvm::LLVMContext&) /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:737:41
#19 0x0000000000d37592 main /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:418:13
#20 0x00007f1781a05c87 __libc_start_main /build/glibc-uZu3wS/glibc-2.27/csu/../csu/libc-start.c:344:0
#21 0x0000000000d36d9a _start (./llvm-project/build-debug/bin/llc+0xd36d9a)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWEtv4zgS_jXOhbAhUe-DD05iZxrTQQdOdrHbF4OSKJszlGSIVDrpXz9VpGw9nN0JFt2ZOWzg2CJFVn1fvfhI6_x1OaMefMgtV1kjjlrU1cy5nTkr-72pG7JaNdkh9Gf0hohK6abNcBSZhU7b8JLMvBtKXohww5m3hk7yTegD0QdOCtEoTeojb1iVk5SLak8YkUJD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