<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/117133>117133</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[indvars] Miscompile when loop body has an operation with Undefined Behaviour
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Nirhar
</td>
</tr>
</table>
<pre>
Here is the problematic IR:
```
define i32 @widget() {
bb:
br label %bb1
bb1: ; preds = %bb5, %bb
%phi = phi i32 [ 0, %bb ], [ %udiv6, %bb5 ]
%phi2 = phi i32 [ 1, %bb ], [ %add, %bb5 ]
%icmp = icmp eq i32 %phi, 0
br i1 %icmp, label %bb3, label %bb8
bb3: ; preds = %bb1
%udiv = udiv i32 10, %phi2
%urem = urem i32 %udiv, 10
%icmp4 = icmp eq i32 %urem, 0
br i1 %icmp4, label %bb7, label %bb5
bb5: ; preds = %bb3
%udiv6 = udiv i32 %phi2, 0
%add = add i32 %phi2, 1
br label %bb1
bb7: ; preds = %bb3
ret i32 5
bb8: ; preds = %bb1
ret i32 7
}
```
produces incorrect IR when `indvars` pass is run. I suspect this is because of the `%udiv6 = udiv i32 %phi2, 0` divide by zero operation.
Look at the indvars transformation here: https://godbolt.org/z/cz1r5178h
The original IR must return 5, while the transformed IR returns 7
Proof of wrong transformation: https://alive2.llvm.org/ce/z/vPFhzg
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVWGP4yYQ_TX4y-gig01sf_CHTaOoK7XV6dT-AGwmgdY2LmBHt7--Aju5rG-jrbRawLx5894AE-GcvgyINeEHwo-JmLwytv5DWyVs0hj5vf4VLYJ24BXCaE3TYS-8buH1G8leSHok6QvZp-tfXEo86wFBZwxInl61vKAnrCSsAlIcFkzT3KMBGgudaLADwnjT0JV0xVGSvQDJDjBalA5IdlxgnLBfltmNhjA-Kh0RYYz5-QHSOxAIP8YFP4QPk9Tz_r7J4-47KvYTF33GJaR8yqTbfoxMcYL_LmwxQ4hJH8qg6S0g7DwUJdusy_dFyp4UiT7ICHbjTpwEDfRWmuD1EWmxX5BhsqoNUQFO0421_CNvIfK5uXzjptis-Xt3_Im7bONu_97ezdiDjOWkIiyMGxT9Xxey-EyNRR-ZNy7Kz87oFlesccXxw-c1WiOnFh3ooTXWYuvh9RtcFQ5A9qke5CysI_sURuFceLl2GnbwCm5yYwB7pePnBlsxOQRzjm878H9exn0KUs9aIjTf4Q2tATOiFV6bYQeLvt-M-QeEj6SrGvBWDO5sbB-RoNBiqIbyfnShEbATYaeLkY3p_M7YC2GnN8JO7Ru1nBalWpj_VAjG6oseRBc895PzoWqTHSC2g6vSHcbE94QoA3IBuXtp4_-v1phzsH-1ZrhsNP4sT3R6RrbrurlfJba46py_ntTbJZF1JqusEgnWtMgYLdOC0kTV52afn8u0zMtzmeVtLvJWyqpqRS5oVRQ80TVLWU4po2lFy4zuuOCCVqI6M0GRtpTkKfZCd_fsiXZuwprSgmZZEi-ri02csQGvEHcJY6Gn2zoEfWmmiyN52mnn3Q8ar30Xu__t3vAj_K5da_oxlDLeqs6YEcJvASjhQAw_jhyu2iv4a1g6voQDKjFrM9lksl29OV3t1dTsWtMTdgr51-HLaM3f2HrCTlG1I-y02ppr9l8AAAD__zFX5ak">