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

    <tr>
        <th>Summary</th>
        <td>
            Transform 31 - CTLZ into CTLZ ^ 31 on x86
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:X86,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    Compile:

```
// -march=skylake -std=c++20 -O2 -S
#include <bit>
unsigned log2_floor_std(unsigned i) {
  return std::bit_width(i) - 1;
}
```

I get:


```
define dso_local noundef i32 @_Z14log2_floor_stdj(i32 noundef %i) local_unnamed_addr #0 {
entry:
 %0 = tail call i32 @llvm.ctlz.i32(i32 %i, i1 false), !range !5
  %sub = sub nsw i32 31, %0
  ret i32 %sub
}
```

```
f3 0f bd cf lzcnt  %edi,%ecx
b8 1f 00 00 00             mov $0x1f,%eax
29 c8                      sub    %ecx,%eax
```

We could generate:

```
f3 0f bd c7                lzcnt %edi,%eax
83 f0 1f                   xor    $0x1f,%eax
```

This issue is inspired by https://github.com/llvm/llvm-project/issues/57312.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVE2L4zgQ_TXypUiQSvHXwYfuzgQWFvawDbvMJciWnGgiS0GSZ9L96xfJSfd0b2CXEcYuW_XqvVeoLELQB6tUR8pHUm4LMcej891JvM5RnMRRexFF0Tv50j256ayNIvyB0C2ht3tFr9fyijuCO1hNwg9Hwrfh9GLEScEqREn4diD4SPARKaz-QFj9eQNxbQczSwWEP_U6Ev5l2ZltlifBuAPuR-Oc36dK2LztaIItkPpxAQB4FWdvIfM9EP7Q67j_oWU8Emxy7goY4dd0Um_v28j33-Cg4me_99KlGrVVIIPbGzcIA9bNVqoRNEcgG7r_yjYfHXxLaji-JRIss7gM38_WiknJvZDSA0FO3w0qG_3Lm6aEo0D4FqLQBgZhzI3TmO_Teojmda05XtkWlifQDEZhgiLYpleCzAt7UCkob30kWIa5z7XT04YfuTJnC6KkPzUcrsXD3P-fvn76OHKgI_QShhHM62BjJlcySU3BcFny-gbYCJRer5_X5L4DwQ29sPEKElcQtjA0cHclW7BwDZdPsLu6_1IwuNlIOCirvIj_MQvvvurP1IvNDy5vzA2HkSaj_14X5xfB94zeVfx81AF0CLOCFNhw1l5J6F_gGOM5JP15YA86Hud-PbiJ4C6dnOtjdfbumxoiwV2uEgjuypozXC_1C9lx2fJWFKpjVd1WnJWcF8dOjlXd1KxmbGjaDR8UqrFuBEfWimocy0J3SJFTjhu6YU3J11iNDS1lS5GOrG9qsqFqEtqs80F2_lBkBV3FqpoVRvTKhPzXQuzFcFI2zfvfTUUQc2dw0iEouXLnqCf9KqJ2Nu2V28J32Vs_H0IaFB1ieGeJOhrVPXthw-j8BJzBCp6ef_8K2ka3RKT8kr47C5emKmZvul9vZ_bzTwAAAP__yLaEsQ">