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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V] Bitwise NOT is computed twice
        </td>
    </tr>

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

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

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

<pre>
    I have this Zig code:

```zig
export fn count_zero_groups(x: u64) u64 {
    return @popCount(~x & ~(~x << 1));
}
```

For the sifive-x280 CPU, we get this emit ([Godbolt](https://godbolt.org/z/1cr57MbbG)):

```asm
count_zero_groups:
 not     a1, a0
        slli    a1, a1, 1
        not     a1, a1
        andn    a0, a1, a0
        cpop    a0, a0
 ret
```

As you can see, we are doing a NOT on `a0` in the first instruction, as well as in the `andn`. In this case, this is a problem because we could have folded the `not a1, a1` into the `andn` instead:

```asm
count_zero_groups:
        not     a1, a0
 slli    a2, a1, 1
        andn    a0, a1, a2
        cpop    a0, a0
        ret
```

Also worth noting that we can change all the `a2`'s to `a0` in the above code. This is because we don't need `x` anymore, once we have `~x`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVE1v4zYQ_TXUZRCDoixLPuiQ2PAih36g3e6hlwVFjiS2FCmQVOzkkN9ekLIT15t-ACsIoq2ZN_P0Hjnce9UbxIaUD6TcZ3wOg3XNF66V5O7PrLXyuXmEgT8hhEF5-F31IKxEUtwTuif08tzQ5X5R_fIGT5N1AToDws4mfH1BZ7_2zs6TJ6w-keIe5s2asG1cgFQPCwwAwGGYnQGyppOddhFNWP16AsI28Hr-WexIsYOcsG28izOaVPsbPtckD9ZBGBC86tQT3p1YTWH382-E7eCI0GNYPhFHFYCwmpQPn6xsrQ6k3BNWDyFMPn43OxB26JfQyrqesMMLYYdcuLL6oW0_XUh9rBD34_LmW10uCDA2RCGA55Ecp-_axMtrra6i6Zn_PeWmwE2UG2nSSt8L3DYRk52uci5Rh-FfFL738GxnENyARzwLyx2CtMr0wOHHnz6DNRBliFhQJjnSKecDKOODm0VQ1qSeHo6odVzPaRFmpCEbuoJHs7gluE-N0h_lgcPkbKtxhBYFnz1GCsLOWi67uLNaoryUizq9aZT4BHvTKtFCLr_Hz49duWj65if7Rz8_dIz9L8fO138Yp72Fo3VhiCSjV2HgIUnHDYiBmx6Ba_2mDYsFWOUh2G_c5K19wjQlVvD5bMuVGTLaWwUwiDJiTxHKzfNoXXLSGpHykl1kQ19jwiqTTSG3xZZn2ORVTuuq3qyrbGjaqt20W-xEJYo833SdLMtOdIJ2HEtJRaYaRtmaFvmWUbqmxaoVHeO5qGvW8arrCrKmOHKlV1o_jfFAZ8r7GZu6rMsq07xF7dOAZMzgEVKQMBbnpWsi5q6de0_WVCsf_HuVoIJOk_WXx193d19IuYcHFY7KYzoHcfPacZpD3I5HJTCbnW5upowKw9yuhB0JO8TC5-VucvYPFIGwQ6LjCTskun8FAAD__5GAqns">