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

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization for C ? BSWAP(X) : 0
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    ```
unsigned select_bswap(bool c, unsigned x)
{
  return c ? __builtin_bswap32(x) : 0;
  // but this form is optimized:
  // return x ? __builtin_bswap32(x) : 0;
} // cttz, ctlz, ...
```

Current -O3:
select_bswap(unsigned int, unsigned int):                     # @select_bswap(unsigned int, unsigned int)
        mov     eax, esi
 test    edi, edi
        bswap   eax
        cmove   eax, edi
 ret


We can fully drop extra select / cmove as bswap of 0 is 0, so:

select_bswap(unsigned int, unsigned int):
        mov     eax, esi
        bswap   eax
 ret


https://godbolt.org/z/8MbaqefW3

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycU09vnD4Q_TTDxQry2rALBw6bRNyi30_qIb1F_seuW4O39rDd5NNXGEKzUSo1tZANmnlvZh5-IkZ7GIxpoLyF8j4TIx59aLQ4Wy29O4shfn_OpNfPDWzp8tB7oPtxSEhNonFG4ZOMP8UJWCW9d0QBuyNrxgVYPYNgdzu_EBIMjmEgigBvydOTHK1DO8w0nAGrJhQBvicU-IoC1gJriRyR4NFG0vnQExuJP6Ht7YvRwPfvcpdCl88Ugt39K1whvkzTKHTpzPN8ybnWY97vxhDMgOTmP7528k6gVRY74JVM6bueGvloAeMECvpJskWKefX-nE4jLlOuiXaJo4mYAtqmgLbXwFRtAV4FVO_P5g3jCgwG38oy74-GKDGQbnTumejgT8RcMIjlBpGkdiIUcSnpO0Kn30sn9uhXSf9R2L-U449TfzjVEfEUJ_J0Xw5-8g3mPhyAtS_A2upBih-me-RzeqYbrmtei8w0m23FN5TWvMiOzXarN6YqVC07XopSbndyV6gd3ZmCaSVoZhtGGacl5bSibFPkneok7fhG1iXnsjJQUNML63Lnzv3UQWZjHE2zZeWmyJyQxsVXo4dmSrqR4yFCQZ2NGH_D0KIzzYON0ehXawm0fpj8Ru6Sk26_PO7_B1Z9Xd2TjcE179SweBxlrnwPrJ3ol-PmFPw3oxBYm1qMwNrU5a8AAAD__3yYVPY">