<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61237>61237</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[X86] Failed to optimize local jump table address math
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:X86
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
RKSimon
</td>
</tr>
</table>
<pre>
Noticed in one of the code examples from #61104
https://gcc.godbolt.org/z/v97ErYY43
```cpp
#include <x86intrin.h>
uint8_t f(__m256i a, int n, __m256i i0, __m256i i1, __m256i i2, __m256i i3, __m256i i4, __m256i i5, __m256i i6, __m256i i7) {
__m256i r = _mm256_cmpeq_epi32(a, i0);
switch(n) { default: __builtin_unreachable();
case 8: r|= _mm256_cmpeq_epi32(a, i7);
case 7: r|= _mm256_cmpeq_epi32(a, i6);
case 6: r|= _mm256_cmpeq_epi32(a, i5);
case 5: r|= _mm256_cmpeq_epi32(a, i4);
case 4: r|= _mm256_cmpeq_epi32(a, i3);
case 3: r|= _mm256_cmpeq_epi32(a, i2);
case 2: r|= _mm256_cmpeq_epi32(a, i1);
}
return _mm256_movemask_ps((__m256) r);
}
```
Clang ends up with
```asm
f:
pushq %rbp
movq %rsp, %rbp
andq $-32, %rsp
subq $32, %rsp
vpcmpeqd %ymm1, %ymm0, %ymm8
addl $-2, %edi
leaq .LJTI0_0(%rip), %rax
movslq (%rax,%rdi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
...
.LJTI0_0:
.long .LBB0_7-.LJTI0_0
.long .LBB0_6-.LJTI0_0
.long .LBB0_5-.LJTI0_0
.long .LBB0_4-.LJTI0_0
.long .LBB0_3-.LJTI0_0
.long .LBB0_2-.LJTI0_0
.long .LBB0_1-.LJTI0_0
```
while GCC manages:
```asm
f:
vpcmpeqd %ymm0, %ymm1, %ymm1
movl %edi, %edi
vmovdqa %ymm0, %ymm8
vmovdqa %ymm1, %ymm0
jmp *.L4(,%rdi,8)
.L4:
.quad .L3
.quad .L3
.quad .L10
.quad .L9
.quad .L8
.quad .L7
.quad .L6
.quad .L5
.quad .L3
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVk1z6jYU_TVio4lHvvIXCxYJeXTaZrpou-hbMbIlsPL0YSyZJO_Xd2Rsgh3ygocBXd1zzr2Sj42Yc3JvhFih9AGljwvW-dq2q7___Edqaxal5W-rv6yXleBYGmyNwHaHfS1wZbnA4pXpRgmHd63VGAHN4pgkiDwicn_6rr1vHKL3CDYINvuqivaWl1b5yLZ7BJufCDbHZf6t_f49oQMxI6dP1TTDDFBpKtVxgRFdvxaZNL6VJqoR_XZZrJPGF1uPdwiK7VZDmknMEKyxNB6bMBhnJZlE8SSCSUQnUTKJ0kmUTaIcwRKj_OHU2nm-xYg-4q0O0bbSjThsRSMpIChOrRIES0RHGnYv0lc1gsIMepiLHeuURzSIlp1UXpptZ1rBqpqVSiAophIYV8wJXARGi_L1Fw3kE3ZPzW-kZlcLZzey06vs9EZ28rHt5EYqvVqY3siGq2y4kR1P2Ch_HHVa4bvWjFRtj0Iz92PbuP7-Dv4OnmgvFc4C56foFK4VM3ssDHe4a_CL9PUMxpw-zezC03peS7iaztUHjDGCtC2baU7bY0j1OdeEFV1DMcMHVHJHYUS5Gcp15Yi6Ajo2_fbxEY0gfdM6HnBvWpP3YTErz7kay4_CgssBpATry0ZPf_z7O9mSfn_TVjZhX4cu2OuHZTt1CIrFkIZ1GHCJYJ1cEKuRyDg_b1QPn6aH61k3A-r-Mh1F0TAYezzfo0hZs--7f3gg2_zujJjoTlHZL1EnTHqTUnKDEr1JCW5QimeYmcdfaqkE_m29xpoZthfuvEu_dPl1Y124Kb4czm0wGKv309xZR22P_MC-9OcUN7H03B3BGdFT0vvu3XH9C38wSPLh-Y0OHeP9RtOvEu8z8fw-nDPLzxLFtUT0lH-Gzz5LpNeF5ueDBV9RvqRLthCrOMuLuChIRhb1KgUgKRd5ni-XBasymuwKTkWyE2xHkjJZyBUQoISSPI4hJiQSYsmqMk0AeELyXYoSIjSTKlLqqMNRZSGd68Qqi4HmC8VKoVx_ZgIoWfVDGI7o_X9FhgDCMapdBd5d2e0dSoiSzrt3JS-96g9cAZ8-4g2TSnDsLbaNl1r-FFjZiin83OkG-_CXHt4erXAOa-brRdeq1excJX3dlVFlNYJNKDT83DWtfRaVR7Dp-3cINv0S_g8AAP__scyfYw">