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

    <tr>
        <th>Summary</th>
        <td>
            Redundant equality check when value is known to be 0 or 1 
        </td>
    </tr>

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

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

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

<pre>
    Example:

```ll
declare void @llvm.assume(i1)

define i1 @foo(i8 %a) {
    %lt = icmp ult i8 %a, 2
    tail call void @llvm.assume(i1 %lt)
    %ret = icmp ne i8 %a, 0
    ret i1 %ret
}
```

The `assume` call constrains the value of `%a` to be either 0 or 1, so the function should get compiled to a single `mov al, dil` instruction. Instead, clang produces this redundant instruction sequence:

```asm
foo:
    test    dil, dil
 setne   al
    ret
```

The same thing happens on aarch64, and replacing `icmp ne i8 %a, 0` with `trunc i8 %a to i1` doesn't change the output either.

I discovered this when playing around with inline asm in Rust: https://rust.godbolt.org/z/oafj35dn5 . In this case, the value is set to either 0 or 1 by the `sete` instruction in the asm block, but LLVM inserts two redundant instructions afterwards.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVE2P2zYQ_TX0ZRCDoi3ZPuiQdGsgQHpJi95H5EhiliIVftjd_vpiJO_aCbIoYFgA583Mm_eGxJTs4IlaUX8S9dMGSx5DbP-cpZzHTRfMS_v7PzjNjsTuo5BPQr7-N3L9ObceGNIOI8ElWANiL527TFtMqUwk1NFWQp0e8w311hPYirF9CIw5glA1CnUCcfi0wgCAD10GsXsCq6cZisvwBv0N1B2Y0TrQ6Ny7JNZab1Ru1SM9lGdS9-ryDmTUWiFSvo1yePpJjccR_xoJRCNv7Ru5UtPBpxzR-gR5JLigKwShZ-TStJGQA3QEZPNIESSECBVzSWHJ6IvX2QYPaQzFGRgogw7TbB0ZTkVI1g9u6T2FC6DjZGMdl7bcvCz5W_jsUyY0HNYO_QBzDKZoYmY2QSRTvEGfH7Mg0fdCXr-7D5im9YRNfcUs7lDK_GUmN0JrLFH2BMBEH8X-H2UTTsQ8_QAjzjP5BMEDYtRjs-cG6A1Emh1qxohG_tLeRsLV5pHjORavX6OspK04bAIlL9Qhgx7RD7SYEEqeS755tH3k9hmMTTpcKLIdrON1JA-zwxfmgTEUb9ae1ju-A5gmsB6-lpTF7iOMOc-JlVNnoc6xpLwdgumCy9sQB6HO_wp1Dth_29XG18A2rn00JuKh7mtlE4vLo_ywTNC9LCDRyESZftoL5sJRptW5oJ-5ZlcyfPny9x8MpJgT5Gv49YIkwD5TvGI0absx7c6cdifcUFsdZFNV1Umqzdj2Vd3TkZr-IDViLbEnXRs0vapV35_UxrZKqr2s1L6q66NS2xp3J9XtzbGvpd73UuwlTWjddrnjIQ4bm1Kh9nDaVfXGYUcuLa-aUp6usASFUvzIxZZzPnRlSPxG2JTTvUq22VH79W00-l7Q2fwCeiT9vJr5pu6zD1d_u683bTcluvZHDwebx9JtdZiEOnOj2-fDHMM30lmo80IvCXVe6P8XAAD__-GKuYo">