<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/85457>85457</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LICM] Mul association incorrectly combines no-wrap flags
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
aleks-tmb
</td>
</tr>
</table>
<pre>
After the https://github.com/llvm/llvm-project/pull/67736 LICM turns the following IR
```llvm
define i32 @test() {
entry:
br label %loop
loop:
%iv = phi i32 [ 1, %entry ], [ %iv.next, %loop ]
%iv.next = add nuw nsw i32 %iv, 1
%mul0 = mul nuw nsw i32 30000, %iv
%mul1 = mul nuw i32 %mul0, 30000
%cmp = icmp slt i32 %mul1, 1
br i1 %cmp, label %exit, label %loop
exit:
ret i32 0
}
```
into the
```llvm
define i32 @test() {
entry:
br label %loop
loop: ; preds = %loop, %entry
%iv = phi i32 [ 1, %entry ], [ %iv.next, %loop ]
%iv.next = add nuw nsw i32 %iv, 1
%mul0 = mul nuw nsw i32 900000000, %iv
%cmp = icmp slt i32 %mul0, 1
br i1 %cmp, label %exit, label %loop
exit: ; preds = %loop
ret i32 0
}
```
https://godbolt.org/z/G5zvEzYEv
The resulting `mul` instruction has 2 flags `nuw nsw`, while in the source code it has `nuw` only. That transformation seems to be incorrect.
If we run `instcombine` for the case, it would provide `mul nuw i32 %iv, 900000000`:
https://godbolt.org/z/6oWrPbEMn
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VU1v4zYQ_TXUZRCDor4POsRxXARogKJYoOiRksYWW4oU-GFt8usLUtrYyWUbICsYtim-eTN6fJrh1oqzQmxJsSfFIeHejdq0XOK_9s5NXdLp4aW9Pzk04EaE0bnZkuyesCNhx7Nwo-92vZ4IO0p5-fFzNxv9D_aOsOPspSTsWFZVVsLvTw_P4LxRNpKdtJR6EeoMT38SeiD0npR0_USyeGvAk1AIImNAcurQOsJqwhog1X5FoHLmJdQUVwCdAck7lEBYIbWeN-r4HddXKGGFuADJDjCPYs1R7CEl7CFsRWIgxSGui_0K3yn87jZEoIuAle-6Hzn5MIDyCyi7rNxhO0SmN_knL2lET16-Q2eUUrrlEZdrhsnL9F3ARh2IAnyNuybopznCRfhjpbvBp--K6QyIdIsIG28i4nfh3t34qGoEXFU1uCbZqiDV4cPprkuhnA4--Lqj_x8nD5-5SLaH2eBgo4A_SG_M8Yt89LVGauh2fTTTT9xBv9YdX6D85_z1oVfpodPS7bQ5E3Z8Jez4W_F6eXz9-_FyW-y3EcGg9dKFvkRKOnlJSgpCWWd874RWMHILDE6Sn21AbFqHvOwBllFIBKFih7Pamx6h1wOCcDFwDQiUWsmXHXwbuQNnuLInbSYeE1jEyYLT0AWmXhuDvdvBbZ1PJ1gQjFeBMBTX66kTCgPxSa_NuucWQ0nCwaK9HGA2-iIG3B7rtnmsdro6paRvb9XPZCz1X-aP7vFZJUObDU3W8ATbtEppkbG6qpOx5UWJVZan5cDrYciaJqsrXud5iflwqnOaiJZRltMsLRilaVrtWNXXZcPyJq0axIaRnOLEhdyF5hByJ8Jaj21d5EWVROfZOMIYU7hA3CSMhYlm2jiROn-2JKdSWGevLE44GWdfmEykOMCzl8Ct1b1YT-JNffkCm8IWlL5bDJ9XByTeyPbTYzFWaAk7xif4LwAA__9aRh-E">