<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56328>56328</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimisation in jump tables (arm64)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
uncleasm
</td>
</tr>
</table>
<pre>
In https://godbolt.org/z/EqTaa1sqG
the produced code for a (short jump table <= 255 instructions) could be slightly optimised by using the same
base address for table loading and jumping; This saves 1 register and 2 instructions compared to the original approach, which can encode jump table to 256 instructions.
```
cmp x0, #5
b.hi .LBB0_3
adr x8, .LJTI0_0
ldrb x9, [x8, x0]
sub x8, x8, x9, lsl #2
br x8
.LBB0_2:
b void impl<0>()
.LBB0_3:
ret
.LBB0_4:
b void impl<1>()
.LBB0_5:
b void impl<2>()
.LBB0_6:
b void impl<3>()
.LBB0_7:
b void impl<4>()
.LBB0_8:
b void impl<5>()
.LJTI0_0:
.byte (LJTI0_0-.LBB0_2)>>2
.byte (LJTI0_0-.LBB0_4)>>2
.byte (LJTI0_0-.LBB0_5)>>2
.byte (LJTI0_0-.LBB0_6)>>2
.byte (LJTI0_0-.LBB0_7)>>2
.byte (LJTI0_0-.LBB0_8)>>2
```
vs.
```
cmp x0, #5
b.hi .LBB0_3
adrp x8, .LJTI0_0
add x8, x8, :lo12:.LJTI0_0
adr x9, .LBB0_2
ldrb w10, [x8, x0]
add x9, x9, x10, lsl #2
br x9
.LBB0_2:
b void impl<0>()
.LBB0_3:
ret
.LBB0_4:
b void impl<1>()
.LBB0_5:
b void impl<2>()
.LBB0_6:
b void impl<3>()
.LBB0_7:
b void impl<4>()
.LBB0_8:
b void impl<5>()
.LJTI0_0:
.byte (.LBB0_2-.LBB0_2)>>2
.byte (.LBB0_4-.LBB0_2)>>2
.byte (.LBB0_5-.LBB0_2)>>2
.byte (.LBB0_6-.LBB0_2)>>2
.byte (.LBB0_7-.LBB0_2)>>2
.byte (.LBB0_8-.LBB0_2)>>2
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztlk1vozAQhn8NXKwiY8LXgUPTj1VX3VvvlcEuuDI4tU3a7q_fsaHbJE234bLSSouIjeN55h2wPZpasdfqZkCdtRsTJOcBuYa7VaxW0kZKtzD6Cb-rpztKY_P0LcCXAT6fWttxtNGKjQ1nqFGMowelEUUBKUyntEWPY79BltaSoyC5CJJLRNIUicFYPTZWqMEEpARylAzVHBkp2s7KV6Q2VvTCgNf6FY1GDC1yWob2fFKuqeGIMqa5MV50EpGKMmdMB-a14TlI1uiuEwbgLTcoRpq3wliuvRHZCwYi6TdUg6xVXlBp0YqBSkQ38J606QJygZ470XSooQPig3_pnbcEjqTZntNo94sFGZ5vP0Tz1YADd71gJxCQJN2fr6NOuD66Xa_xfbI_SZme4MLB0e33uxt8fyAgma69TekF0vVkDHrp5b6lGesdb3PrKWmkC40chDaJO0v__xQicVtp327ut0owJPqNhA2Bg-QK9grsgV02-cBqbncNVic5j486T09iyVE2O4lNjrL5SezqKFucxKYf2HkjHMJR_Wo59GA8m5y9LRqw4CS5IicSq8VEupjIFhP5YqI4JA7O6favnGJP_-kUQ8JD7zZTC8srVezO22fMnB3Kye-00EeTw3OMv8oOvyMo3_PCy4R9kR72dvT_9PBPpId5sRakh3kFFhPpYiJbTOSLieIz4ncCCFmVsDIpaWiFlbz6IYyrWub6hboCAIqBnQrBOO9U95lLneGoZXVQeQnbjXUEhQgMpNy-dWdQfzzyxsIQJEYOhdN1miWkCLuqKNgDXhHCaYI5LdIyjxmOy4JlK7xK8iyUtObSVHCwA0IG_oy8C3iG8x2KimBCcI5jnJGY4KgsH3JeM1rnaRnHNA5WmPdUyMjF4UrCUFc-pHpsDUxKKKfM-yQ1RrQD514O_NPRQi1YjUMjOTV96LUrH_svBgykqQ">