<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/89430>89430</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AArch64] Suboptimal code for multiplication by certain constants
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:AArch64,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
For some constants, GCC is able to generate sequences of `add` where LLVM generates `mul`. I have checked all constants between 1 and 100 (https://godbolt.org/z/rxej44fGj):
For all of the examples below(11, 13, 19, 21, 25, 27, 35, 37, 41, 49, 51, 69, 73, 81, 85), LLVM generates
```asm
mulK:
mov w8, K
mul w0, w0, w8
ret
```
```asm
mul11:
add w1, w0, w0, lsl 2
add w0, w0, w1, lsl 1
ret
```
```asm
mul13:
add w1, w0, w0, lsl 1
add w0, w0, w1, lsl 2
ret
```
```asm
mul19:
add w1, w0, w0, lsl 3
add w0, w0, w1, lsl 1
ret
```
```asm
mul21:
add w1, w0, w0, lsl 2
add w0, w0, w1, lsl 2
ret
```
```asm
mul25:
add w0, w0, w0, lsl 2
add w0, w0, w0, lsl 2
ret
```
```asm
mul27:
add w0, w0, w0, lsl 1
add w0, w0, w0, lsl 3
ret
```
```asm
mul35:
add w1, w0, w0, lsl 4
add w0, w0, w1, lsl 1
ret
```
```asm
mul37:
add w1, w0, w0, lsl 3
add w0, w0, w1, lsl 2
ret
```
```asm
mul41:
add w1, w0, w0, lsl 2
add w0, w0, w1, lsl 3
ret
```
```asm
mul49:
add w1, w0, w0, lsl 1
add w0, w0, w1, lsl 4
ret
```
```asm
mul51:
add w0, w0, w0, lsl 1
add w0, w0, w0, lsl 4
ret
```
```asm
mul69:
add w1, w0, w0, lsl 4
add w0, w0, w1, lsl 2
ret
```
```asm
mul73:
add w1, w0, w0, lsl 3
add w0, w0, w1, lsl 3
ret
```
```asm
mul81:
add w0, w0, w0, lsl 3
add w0, w0, w0, lsl 3
ret
```
```asm
mul85:
add w0, w0, w0, lsl 2
add w0, w0, w0, lsl 4
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0lktv4zgMgH-NfBEa6OXXwYdsAg8WnT0tsHfZZmK1spW15GZmfv1CavrYJHXjpBMEtGAS5CeSYSitVdseoEDxHyheR3J0rRmK-w7ko-qjyjQ_i9IM2JoOcG1662TvLGIr_G21wspiWWnAzuAt9DBIB9jCvyP0NVhsNhglRDYNSgjetzAA_v79n79eTa1Xd6NGCVngP3ErnwDXLdSP0GCp9Vs4XIHbA_SYYtk3mBKCEcta53YW8SViJWLl1jSV0W5hhi1i5S_EyuEHPAix-faAWO7NyBqRg_Q38hHMBrsWMPyQ3U6Dj6PNHrGMUn9DyoPMvWThDYuDTL3k4czDWQStCJZxOCfhnAYPWXiTxZ6DrY5y8B4LJeT5K233_KYb9f0rOz58OvMUnvvMu7s_Uo4a74lXHGT2f_0A7ijYpwSUniDIpsF7-i5KkNpqzE4NA-t7JPpiTA_G10Dxs1Ah1lmwl1jTROwGonwmEb-I6MIcfczFzpfvY65ZJbwhYSz-GIxMgV1mdQ1ROpNosqlOCn0FEZ_I0dniia9sqrNEEzm6oc1vqJq4tsGniU6rNpdr7kCgc3544ta5EE-kbbLZLzK-EO8cWDI3b5f1_A0dls79s7ms52-YC9nc4k0SfcWkyn7PNBefrS9RU_Am57mMoKAp5XFMRJxGbSFFktGKxEmScZ7Imkhe5SmkeV7nPKZZpApGmCCC5jSjgqYLyUTWbKqYZmSTUkiQINBJpRdaP3V-s4yUtSMUWS44ibSsQNuwODNWyfoR-gbx5XI51G0iEGOIrRBjnbIWmjuzc6pTv6RTpve6eB0Nhfd7V41biwTRyjr7Fskpp8NW_uIvXuO_xyq4kX43bgBvzOD3Pad2WtXBM65-4hoGJ1X_tj5H46CLo21ZuXasFrXpECt9zMPjbjeYB6gdYmW4qkWsDLf9LwAA__9ZhgYp">