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

    <tr>
        <th>Summary</th>
        <td>
            Missed optimizations in IP netmask-like code patterns
        </td>
    </tr>

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

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

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

<pre>
    I was playing around with different implementations of IP address netmask checks and noticed what seem to be a bunch of missed optimizations:
https://godbolt.org/z/rT577jGqr

The comments in the godbolt describe what's going on in more detail, but in summary:

* Early returns seem to frustrate Clang (and GCC) in `check_v4_netmask1` and `check_v6_netmask1`. I suspect because it doesn't realize it's allowed to synthesize reads in this context.
* In one function (`check_v4_netmask2`), Clang emits reasonable code except it leaves a clearly unneccessary register spill:
   ```
        mov     dword ptr [rsp - 8], edi
        and     esi, edx
        cmp     esi, edi
 sete    al
        ret
   ```
   GCC does fine here.
* Clang manages to partially vectorize `check_v6_netmask2` but misses an easy vectorization. GCC does fine here.

Missing the 1->2 optimization isn't a big deal because 2 is still simple, portable code. But ideally the compiler would do 2->3 because it requires a bunch of target-specific assumptions.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VEFv2zwM_TXKhUjgyHXiHHxY06XoYcAOuw-yRNtaZckT6aTpr_8gJU3bD8WMQEZEk3x8JJ8isr1HbER1L6qHhZp5CLEx6mhNi37RBnNunuCkCCanztb3oGKYvYGT5QGM7TqM6BnsODkc0bNiGzxB6ODpJyhjIhKBRx4VPYMeUD8TKG_AB7YaDZwGxUCII3CAFkFBO3s9pACjJUIDYWI72tdLYFF-E8WDKL4NzFP-Jw9CHvpg2uB4FWIv5OFVyEP8VW23fx7_xsvnl_PXgKDDmHASWA88IFxdwSDpaFvMiITcEvQh1Rt8-nIMEcEgK-uE3EM7c7qleRxVPN9AXU_5Db6r6M4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