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

    <tr>
        <th>Summary</th>
        <td>
            LLVM does not try to collect common factors in sum of products involving constants
        </td>
    </tr>

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

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

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

<pre>
    ```rs
#[no_mangle]
pub fn src(a: u32, b: u32) -> u32 {
    a * 7 + b * 56
}

#[no_mangle]
pub fn tgt(a: u32, b: u32) -> u32 {
    (a + b * 8) * 7
}
```

https://godbolt.org/z/YEbshG8PY
https://alive2.llvm.org/ce/z/gQJ49Q

This can snowball into a lot of extra instructions/muls in long addition chains in specific cases which can come up, for example, in triangle-shaped code:
```
a +
b * X +
c * X * Y +
d * X * Y * Z
```

Seems unlikely that the transformation is always profitable though.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUU0GL2zwQ_TXyZUhQxnbiHHxIvt18ULbQpaV0eymyLNtqZY2Rxsmmv77YSXeXZSktGDx60ui9eTNSMdrWG1OKfC_ym0SN3FEolbPkJSYV1edSrOXlC1HIGyF3AlOR7z1965VvnRH5zQUfxgoaDzFogYUS6Q7GFAX-B9VTvIWFSG-nGMRmf0kDAFAgcAcbELiHao7z9ZVsc739r7i55X_mnhJeEBfTyVnNawG_fXipp2Meokh3Ag8CDy3VFTleUmgFHn4KPDzcVrH7v_jw8NZx5ezR4NK5Y39N0eaa196_y7b3L5k-dTaCVh6ip1OlnAPrmUCBIwZqwDxyUGB95DBqtuSjwEM_ugjWgyPfgqprO22A7pT1Mx4Ho21jNWgVTYRTZ3U3k2jqDYzD5GBDAcyj6gdnpqX1wMHO7i9ipwZTg6baTEW9ZdNs7SW8-PvlGdBPwA4enuH6FbyDr3_owEdj-gijd_aHcWfgTjFwZ4CD8rGh0Ku5ZhtBuZM6RxgCNZZV5QxwR2PbLZO6TOttulWJKVcb3BSZzFEmXdngdpWumjpr8tqsmrTCfL3e6nTbSLMpdJPYEiVmKylRphlKudRZUVcFrnW2bgpMG5FJ0yvrnpqc2BhHU65WclOsE6cq4-L8-hC9OcG8KxCnxxjKKWlRjW0UmXQ2cny-hi07U97dfX4PNZkInhg4nIEJNDlnNE897MlDozRTuLR77KdRGQLVo-YJOpI7Wt-CJh9ZeY7JGFz5aqotd2O11NQLPEwCrr_FEOi70SzwMMueBu5a17HEXwEAAP__SgdH2A">