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

    <tr>
        <th>Summary</th>
        <td>
            [WASM] Assertion `isReg() && "This is not a register operand!"` failed during pass `Machine Common Subexpression Elimination`
        </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

When targeting `wasm32` / `wasm64`, the following code crashes the backend during pass `Machine Common Subexpression Elimination` with assertion `isReg() && "This is not a register operand!"` failed.

The crash happens specifically when the second operand for `urem` is -1.
This problem does not exist when targeting `aarch64`, `x86_64`, or `riscv64`.

## Minimal Reproduction

https://godbolt.org/z/hKr3jo1s6

### Code

```ll
define i64 @f() {
BB:
  %A = alloca i64
  %C2 = ptrtoint i64* %A to i64
  %B2 = urem i64 %C2, -1
  ret i64 %B2
}
```

### Stack Trace

```
llc: /home/henry/aflplusplus-isel/llvm-project/llvm/include/llvm/CodeGen/MachineOperand.h:360: llvm::Register llvm::MachineOperand::getReg() const: Assertion `isReg() && "This is not a register operand!"' 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 -mtriple=wasm64 ./crash-reports/dagisel-wasm64/1.ll
1.      Running pass 'Function Pass Manager' on module './crash-reports/dagisel-wasm64/1.ll'.
2.      Running pass 'Machine Common Subexpression Elimination' on function '@f'
 #0 0x00000000047ada8a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:569:11
 #1 0x00000000047adc3b PrintStackTraceSignalHandler(void*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:636:1
 #2 0x00000000047ac286 llvm::sys::RunSignalHandlers() /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Signals.cpp:104:5
 #3 0x00000000047ae365 SignalHandler(int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x00007f4404fd8980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #5 0x00007f4403ec8e87 raise /build/glibc-CVJwZb/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #6 0x00007f4403eca7f1 abort /build/glibc-CVJwZb/glibc-2.27/stdlib/abort.c:81:0
 #7 0x00007f4403eba3fa __assert_fail_base /build/glibc-CVJwZb/glibc-2.27/assert/assert.c:89:0
 #8 0x00007f4403eba472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
 #9 0x000000000104784c llvm::MachineOperand::getReg() const /home/henry/aflplusplus-isel/llvm-project/llvm/include/llvm/CodeGen/MachineOperand.h:0:5
#10 0x0000000003513514 (anonymous namespace)::MachineCSE::PerformTrivialCopyPropagation(llvm::MachineInstr*, llvm::MachineBasicBlock*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/MachineCSE.cpp:188:44
#11 0x000000000351221d (anonymous namespace)::MachineCSE::ProcessBlockCSE(llvm::MachineBasicBlock*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/MachineCSE.cpp:541:11
#12 0x0000000003510845 (anonymous namespace)::MachineCSE::PerformCSE(llvm::DomTreeNodeBase<llvm::MachineBasicBlock>*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/MachineCSE.cpp:790:16
#13 0x00000000035104e3 (anonymous namespace)::MachineCSE::runOnMachineFunction(llvm::MachineFunction&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/MachineCSE.cpp:947:14
#14 0x00000000035783e5 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/MachineFunctionPass.cpp:91:8
#15 0x0000000003c55c66 llvm::FPPassManager::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1430:23
#16 0x0000000003c5aa92 llvm::FPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1476:16
#17 0x0000000003c56539 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1545:23
#18 0x0000000003c560ad llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:16
#19 0x0000000003c5ad71 llvm::legacy::PassManager::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1672:3
#20 0x0000000000d38a5c compileModule(char**, llvm::LLVMContext&) /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:737:41
#21 0x0000000000d36e02 main /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:418:13
#22 0x00007f4403eabc87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#23 0x0000000000d3660a _start (./llvm-project/build-debug/bin/llc+0xd3660a)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNWNly2zYU_Rr5BSMNCe4PepBku0kbtx47TWb6ogFBSEQLEhwA9JKv7wVIihLtaZzNqYbigu2eczdcMpfF43KGAzjQOdNU8cZwWc-885m36s4fS1YjQ9SeGV7v0Sz27omuAgw3aIYvh4Y4hJsZ3iBTMrSTQsh7O5zKgiGqiC6Zdl05of-wukBFq2x_Q7S2S1wRWvKaoY2sKlmj2zZnD41iWgMadCF4xWvikIHUe25KBPOYsi12Ntc3bD_D6QxngCmGAy74fck1gqOWBhGk2J5rwxSSDVOkLmbYhzF2vR3hghWLY9Lvyx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