<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/152237>152237</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization: fold (n*x-n)/n to x-1
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
zxt5
</td>
</tr>
</table>
<pre>
```
define i8 @src(i8 %0) {
%2 = mul nsw i8 %0, 40
%3 = add nsw i8 %2, -40
%4 = udiv exact i8 %3, 40
ret i8 %4
}
define i8 @tgt(i8 %0) {
%2 = sub nsw i8 %0, 1
ret i8 %2
}
```
Alive2: https://alive2.llvm.org/ce/z/X3YdJu
Godbolt: https://godbolt.org/z/db1WvTGjc
Found this pattern in https://raw.githubusercontent.com/dtcxzyw/llvm-opt-benchmark/refs/heads/main/bench/llvm/optimized/AsmParser.ll#:~:text=10%20to%20i64-,%25,-.idx%20%3D%20mul%20nuw
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEU0-P2y4Q_TTjC0qEB2zHBx-8G3mln_STeqjU9ogNSdhiiAxOvDn0s1ew3j9ND5VQZsK8mXl6zwjv9dEq1UDxAMU-E3M4uam5LaHIeidfGijpemgr1UFbRfSOAKd-GgB3MceCAtYEqgegLYn_kQDbk3E2xPorecc8Ek7fICxBhJSfIBghmw8MT5hZ6gtRixjCCmOfJk3q7ZYDbaHax990_iAbjuEfZP3c35PN7zbgx4ZPogBtW6MvCoG15BTC2QNrATvATqT7rTGXceumI2A3KMDuBth9Zz_kfzPQ9snJ3pnwd_PxtbA2xh7Z598uX5-eh9etnZutJOGkPTmLENRkibZ3QyZx3R51OM397NU0OBuUDdvBjXFcGJbbyxWwiwQ37hw2vbLDaRTTz9iqDh6wOykhYxyFtoBdQqwtgJ07Bz3qm5KAXevHL2LyatoaA8iAtb-AtUEtAdg-p1FAGlwKuuQbwMeYF4CPm62WSypEd_cpG2eTop2vmWyYrFktMtXkVcE5L3hVZadGFqVkpayYzOuhRFVXyDnr812uFEWOmW6QYkF3tKR5URXVti4FHoaSsyKvhypnwKkahTbvHmXa-1k1eYHIqsyIXhmf3gaiVVeSqoAYn8rUJN36-eiBU6N98B9jgg5GNf9r75Ukq0oiaGej0QdnJAHcWcB22VjAGrCzJDiybPJsnkxz9ykkB1fbVuXT7vPkntUQALtELPq0Mr80-DsAAP__Hp0dDg">