<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61296>61296</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] reduce expanded form of integer square-of-sums
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rotateright
</td>
</tr>
</table>
<pre>
```ll
; square-of-sum may be expanded in source/IR:
; x^2 + 2xy + y^2
; (x * x) + ((2 * x) + y) * y
define i6 @src(i6 %x, i6 %y) {
%xx = mul i6 %x, %x
%x2 = shl i6 %x, 1
%x2y = add i6 %x2, %y
%x2yy = mul i6 %x2y, %y
%r = add i6 %x2yy, %xx
ret i6 %r
}
; This can be reduced
; (unless all of the intermediate values have extra uses?):
; (x + y)^2
define i6 @tgt(i6 %x, i6 %y) {
%xy = add i6 %y, %x
%r = mul i6 %xy, %xy
ret i6 %r
}
```
https://alive2.llvm.org/ce/z/TkwEKJ
Note that no-wrap (nsw, nuw) can be propagated to the resulting math if (some subset of?) the original code has no-wrap. Also, there are probably 8 commuted variations of the last 3 math ops.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVMGO4zYM_Rr5QiRw6MRJDj5kNmtgW6CHYn9AthhbrWylopTE_frCsjMZT_cwgAER1iOf-B5ByaybnqgQuzexOycy-Na6wlkvPTndtD6prBoKkafTZ4xIzyI9iewN-J8gHa3sZcWhg04OUBHQ4yp7RQp0D2yDq0lg-eNPkZ1eiQ-x-44g8A3wMcRzGP-8AAIPDxB4gofAYwQIPAg84PLnMAUnGKZURRfdE-gcxDZlVws8jDHuHgK_wRROOfu3KQPi7QNEdoYuGPgAj-cHEEYQtwvQZkZEwBARUqknAuc6w6LOMHymw-EXQPe_asMT9nh_mCM_X7tZvf15Dt7F_Nlqhlr2ozuOVKhJwULq0BtiBmkM2Av4lkD3nlxHSktPcJMmEEMrb6O73kkITCyyUuBxYevs2uTLy9CFK77xX3Xls5zDL1xxn6V8Bw1fkOg51B8Va72_8tgWlgJLafSNcG3MrVtb1wgs4zz_K7D8-ff9---_fUz9w3oC30oPvV3dnbyOivR8H5_Uh_vY4mzD1dmrbKQnBd5GxR1xMF73DXTSt6AvYy7bjoBDxeTBXibFI9o63eheGqitImglPxnXcDJsR0LfkiOQLpJVsjIDHKC2XRdG1pt0Wnpte35abiR7yCZ2e-V1oopMHbOjTKjY5PtDts_3eZ60hdxmKttv6rpK6ZCpC2KV5pu8TinLVa7yRBeYYpZm6XGz2-Zpvj7WdMlUne6zVFb7nRLblDqpzbusiWYOVOQbPOaJkRUZjgsJsac7xEuBOO4nV4w5qyo0LLap0ez5VcVrb-Im-9Gz_2a7Svckdud56F-b6WJdN7Y9TnlDbrnHOAnOFMspaLRvQ7WubSewHOnmY3V19i-qvcAyPpIFlrGJ_wIAAP__rlKJww">