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

    <tr>
        <th>Summary</th>
        <td>
            SimplifyCFG or InstCombine incorrect in the presence of overflow
        </td>
    </tr>

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

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

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

<pre>
    [overflow_simplifycfg.ll.log](https://github.com/llvm/llvm-project/files/8217868/overflow_simplifycfg.ll.log)

Running: `opt -simplifycfg overflow_simplifycfg.ll` shows the issue.
The branching is removed and the difference is done unconditionally. In the original test, the difference overflows.

This was found following the change in https://reviews.llvm.org/D98152. The test does not overflow following the SimplifyCFG transform but does overflow after Instcombine. I haven't understood why this is yet. 

Pertinent diffs from original test:

Before SimplifyCFG:
```
40:                                               ; preds = %38, %13
  %41 = load i64, i64* %4, align 8
  %42 = getelementptr inbounds %"c442", %"c442"* %0, i64 0, i32 1
  %43 = load i64, i64* %42, align 8
  %44 = icmp slt i64 %41, %43
  br i1 %44, label %47, label %45

45:                                               ; preds = %40
  %46 = sub nsw i64 %41, %43
  br label %47

47:                                               ; preds = %45, %40
  %48 = phi i64 [ %46, %45 ], [ 0, %40 ]
  call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %9)
  ret i64 %48
```

After SimplifyCFG:

```
32:                                               ; preds = %29, %12
  %33 = load i64, i64* %4, align 8
  %34 = getelementptr inbounds %"c442", %"c442"* %0, i64 0, i32 1
  %35 = load i64, i64* %34, align 8
  %36 = icmp slt i64 %33, %35
  %37 = sub nsw i64 %33, %35
  %spec.select = select i1 %36, i64 0, i64 %37
  call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %7)
  ret i64 %spec.select
```


Before instcombine:
```
%112 = icmp slt i64 %.0, %111
%113 = sub nsw i64 %.0, %111
%spec.select = select i1 %112, i64 0, i64 %113
ret i64 %spec.select
```
After instcombine:
```
%112 = sub nsw i64 %.0, %111
%113 = call i64 @llvm.smax.i64(i64 %112, i64 0)
ret i64 %113
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VkuP4ygQ_jXOBY1lg5-HHNId9Whuq925r7DBMSsMFuBk-99vgZ200-OOtrW9kRXA1OP7iipcjWav-yh_0mduOqkvf1oxjFJ0r213iqWMpT5F-THCVe_caCNyiPALPCfh-qmJWz3AQsrzdfg2Gv0Xbx0sOyG5hbHCaVkVFcwe-cB1lByj5DD__z4pJdQJ3KGoSPTo0LeVDvrAEIgi2-uLRa7nSFg78Xi29xPWjaGq7cEq7CDDBzDCEFUsCDPRddxw1Xo9xLTiaFKtVkw4oRWV8jVGP1QQ1UacBLxCjlvg-fxe_wrOxmtGP3uwe6EWdXoCn52WIOLBeO22p-oEnhW6j7LhZ8HBkI9srA1E6eVYV2mOY-QZeQCAlVuktLv5fWf7jyVEzy_fkYMQ2E6bATXTonnTop3jBjhaB4faCMWBMOrpmasIlw6iwbixTmuGLv0rWAY68LxyF6M1z9-4caCsXAgJ0DV6eBcyclgrPHEAdAfzTaBIlicss8Tnw-d-EXlCo-HMwuyIIpyTyh8ZTFIyW0V-kaVhX2rKkCgyLxKGQ9j0SyrFSaFqrYODzok7LvkAjEdn4Agbf77WC0QYt1mGYVhcrl8Ey8niCM0TglG6dkAegcIfoMqCkmiHEVnpgvVAcMGQ3Wg3gDadVfyepA2XYVneL_P1acHqv59BlqwBF-G1nRqk7OUx3jXGNajyK0DlV4936KqwP_ZiRpY_zZivsjkKt-Nz2EluFsLbxUoLtwc6a8FQlCWhkiHPuRMDj7li8ZgISMnKWw-p6VcHKGilJhm41re7EcG19Xai1WaRzP-HUMybNbWlRPAXBBBQLpWFVwEkD5N4O4dJ9j9XFskfgCIfoSo2K4uQBQPJ18LlVlZvy9qRt7EFqq2blebpXJykuGeyGCq_KrvK7exaYXqQZnc3uHj7dHx0g_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