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

    <tr>
        <th>Summary</th>
        <td>
            [AArch64] GlobalIsel cannot select G_EXTRACT_VECTOR_ELT with non-constant index
        </td>
    </tr>

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

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

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

<pre>
    The following code crashes `aarch64` backend in 'Instruction Selection' when compiled with GlobalISel.
The problem doesn't exist with SelectionDAG.
## Minimal Reproduction
https://godbolt.org/z/1bzf134Mr
### Code
```ll
define i64  @f(i64 %i) {
  %w = extractelement <2 x i64 > <i64  1, i64  1>, i64  %i
  ret i64  %w
}
```
### Stack Trace
<pre>
LLVM ERROR: cannot select: %1:fpr(s64) = G_EXTRACT_VECTOR_ELT %2:fpr(<2 x s64>), %0:gpr(s64) (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: /opt/compiler-explorer/clang-trunk/bin/llc -o /app/output.s -x86-asm-syntax=intel -mtriple=aarch64 -global-isel -print-after-all <source>
1.      Running pass 'Function Pass Manager' on module '<source>'.
2.      Running pass 'InstructionSelect' on function '@f'
 #0 0x0000000003062b08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/llc+0x3062b08)
 #1 0x00000000030606cc SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f4bec084420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f4bebb4700b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f4bebb26859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x000000000069e862 llvm::RISCVISAInfo::updateImplication() (.cold) RISCVISAInfo.cpp:0:0
 #6 0x00000000034c7c72 llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, char const*, llvm::StringRef, llvm::MachineInstr const&) (/opt/compiler-explorer/clang-trunk/bin/llc+0x34c7c72)
 #7 0x0000000003455579 llvm::InstructionSelect::runOnMachineFunction(llvm::MachineFunction&) (.part.0) InstructionSelect.cpp:0:0
 #8 0x00000000024464f0 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
 #9 0x00000000029200f2 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-trunk/bin/llc+0x29200f2)
#10 0x0000000002920271 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/llc+0x2920271)
#11 0x0000000002921de0 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/llc+0x2921de0)
#12 0x0000000000768830 compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#13 0x00000000006a95c2 main (/opt/compiler-explorer/clang-trunk/bin/llc+0x6a95c2)
#14 0x00007f4bebb28083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#15 0x000000000076077e _start (/opt/compiler-explorer/clang-trunk/bin/llc+0x76077e)
Program terminated with signal: SIGSEGV
Compiler returned: 139
</pre>
### Results of the Last Successful Pass
<pre>
# *** IR Dump After RegBankSelect (regbankselect) ***:
# Machine code for function f: IsSSA, TracksLiveness, Legalized, RegBankSelected
Function Live Ins: $x0
bb.1 (%ir-block.0):
  liveins: $x0
  %0:gpr(s64) = COPY $x0
  %3:gpr(s64) = G_CONSTANT i64 1
  %2:fpr(<2 x s64>) = G_DUP %3:gpr(s64)
 %1:fpr(s64) = G_EXTRACT_VECTOR_ELT %2:fpr(<2 x s64>), %0:gpr(s64)
  $x0 = COPY %1:fpr(s64)
  RET_ReallyLR implicit $x0
# End machine code for function f.
</pre>
## Analysis
The following lines failed to find the index to exract from the vector:
https://github.com/llvm/llvm-project/blob/f2b150b4501ff8978ce19b0727225a6817be1dc7/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp#L4073-L4077
In the example, the value of the index operand register is copied from function argument, but it is expected to be a constant in the code above and thus return false.
Our question is whether it is by design to expect the index to be a constant or a bug. We suspect it might be a bug since it compiles successfully with SelectionDAG.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0WFtz27gV_jXwC0YaELzqwQ-KLq5n7LVHctP2SQOShxRqEGABMJbz6zsAqQtt7TabbXY2CQUC3_nOleeAGcNrCXCL4i8oXt6wzu6Vvu24kKx752b5fJOr8v32ZQ-4UkKoNy5rXKgScKGZ2YPBKCGM6WKfRCghOGfFK8gSc4kRTe-lsborLFcSb0GAf0I0xW97kLhQTcsFlPiN2z2-Eypn4n4LYorIEpG5k9lqlQtocKnAuIMWw4Eb2584IS7nd8MZRENEQ_zIJW-YwBtotSp7Av2GvbWtQeEc0TWi61qVuRJ2qnSN6Po7ousg_14FYfSoL_Ec5EKVMKwlpP9fiH6hhIpLwDyJMEYRqRDN3DOiMUd0hlH6pd-H3dIbRuESw8FqVlgQ0IC0GIULig8eAYUr99ODBYgu8PAUrk4_PPCAqMGeFt8GfunyA9GPumwtK17xi2bFUaVw0WpwMvzPh4evj3i12TxtUDjHBZNSWWy8td0ConGAwnnVakQzk0ReyXCJ73arf75s5ouX3dfV4uVps1s9vLjN9LR50NOdcfrMnEqIxgSF83qERjMucdXJ3nPhHFdutyf3_LCab1fYdHnDLWY472qsoVXaYqvwB_9yu-_yaaEaRNdCfDv-M2m1-rfThq65MR0YRNeY-bAtRFcCtvshwH1AO1_BEGG97cquaZ0Uv0SmiMyetao1azDTded8anpLrVXrpAyhridwaIXSoN2aYLKeWN3JV0TXOZeeXIEnyp1jbetOd7bt7NTgySFLJsw0E_MuLTugcMmlBYEnjdW8FYDC5ZCFeFL7TJpw4963mks7YZUFPW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