<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/129659>129659</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Destination register for x86 `bsf` and `bsr` should always be initialized
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
CatsAreFluffy
</td>
</tr>
</table>
<pre>
The x86 instructions `bsf` and `bsr` (which are used to implement `countl_zero` and `countr_zero` respectively on targets not supporting BMI1) have a dependency on their output register. Currently, LLVM does not do anything to break this dependency, which can lead to strange slowdowns depending on the whims of register allocation. For example, in [this Rust program](https://godbolt.org/z/45axenMjd), on the baseline target "New isqrt" is much slower than "Stdlib isqrt", while on a modern target "New isqrt" is much faster than "Stdlib isqrt", since on the baseline target "New isqrt" has a damaging `bsr`-induced dependency. This dependency should be broken by initializing the output register before `bsr` or `bsf`. (LLVM already does this on 64-bit targets when the argument is not known to be nonzero as of #123623, but the dependency still exists even in other cases.)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVE1v4zYQ_TXUZRBDpixHOuiwSWCgwKaHdtFrMRJHIjcUqZJDO86vLyi7jrtAP06CCL7HefPeDMZoJkfUifpJ1C8FJtY-dM_I8Uugg03jeC56r87dN03w3uzBuMghDWy8iyD2ZR9HsS8Bnbr8hfwnZHPSZtCAgSBFUsAezLxYmslxvjj45Nj-_kHB38HX03A7DRQXGtgcyZ7BO2AME3EE5xliWhYf2LgJnl5_2grZgsYjAYKihZwiN1wwmkwAn3hJDIEmE5nCBp5TCOTYnoV8hq9ff3sF5enCrDygO7PO1OyhD4RvwNrEO-aMuigc0IElXBVGDugmgmj9SfmT-wuRmS6lZNAcwY-3UgCt9QPmfm7g4APQO-ZG5ReMA1E_rU__kiLDEvwUcBb1i5CNZl6iqL4IeRDyMHnVe8sbHyYhDx9CHnY1vpN7_a6EbDPZtYAeI1nj6NpMEFL-TCcw8Y_AQkowEeY06FUDBWCNLt_5lZU1_e3aVb-lTIswe0XB_RfliKvgf6GMxg30fyvVGLPbOOOUG3xL34NxKg2k7uzawLe_-wdR-2QV9AR98G_koD-DcYYNWvOxOq_px9RAT6MPdJdzHz5HYJNTvyYJbSBU50uiVve8g_3uoTd8i_BJ00UlhimtQ2Eu6Xtz_uTW3BE47_IoAK6JEbLaymovq9ypPvEKv5fExlqgdxM5Ah3J5fx41hRgwEhxI2RbqK5SbdViQd32cbctH6VsqkJ39X5osaof5UjNrh4fm7HajeNQ7xtsmxZlYTpZyrqsyl35uN3KarPr90hN3cuGhlKOKHYlzWjsxtrjnHNYmBgTdVvZ7uu2sNiTjeuekdKtPsZE2ff6pQhdBj30aYpiV9qs4JOGDVvqXiiyceucfBoy-rAupX_aQ1eX0Z7wHHNHbxaTKlKw3Q9DZFinfjP4WchDfv_6eViC_04DC3lYq45CHq6yjp38MwAA___N_sgq">