<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57328>57328</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization / canonicalization for testing if an integer is a power of 2
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jeremy-rifkin
</td>
</tr>
</table>
<pre>
LLVM transforms the following to be identical:
```cpp
bool foo(unsigned n) {
return std::popcount(n) <= 1;
}
bool bar(unsigned n) {
return (n & (n - 1)) == 0;
}
```
However the following two do not generate the same code:
```cpp
bool foo(unsigned n) {
__builtin_assume(n != 0);
return std::popcount(n) == 1;
}
bool bar(unsigned n) {
__builtin_assume(n != 0);
return (n & (n - 1)) == 0;
}
```
https://godbolt.org/z/G4eqeEqjc
https://alive2.llvm.org/ce/z/SHY2mg
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytU8tu2zAQ_BrqQtigSEuWDjokVd0cklOBAj0ZpLSW6VJchaRsJF9fyrKTIO2hjwgUn7uDmeFSYftU3d9_e6DBSet36HpPwx7oDo3Bk7YdDUgVUN2CDbqRhogbwmrCrn3O5tYMw7yjEE1MR8KL0XrdWWipJbykZH07R9D4OQijs9SHdgIUNwMODY42xKw5WHwioqYpEZcksq7f4Cvp_gh_gotdPk8WEY-XM3w9wbNf4V8EvVV5hyc4gnvvzAlpi9RioB1YcDLAOcLLHmiDLXyQV9utGrUJ2m6l92MPF1HprCAKEn9lbP1fxv4LmQ-6hX0Ig59U8U1sHbYKTVii6-LqOf5fVvAInx8Pze_CpdFH4Etjjv0lpYFL3te777zvEqjSPE9FyjJWJm0l2lKUMgk6GKgetPfRDhyC7vWzDBonQRvaSIt2ehbXzfiCaAAfpvrQOyot1TZAF2tHeyrpEAvJUdxRnozOVO8U6bAf1bLBPi4mopdhMTg8QBOvcBNpjODjJFsLXiT7Ki-yIi1kDqwsVrJoeKZWKYgSmkLylWgTIxUYX5HslmR1oivOOGcFXzER-3KZ52tViLwQipWZYg1ZMeilNi9GJa46c1Bj5-Oh0T7418NYA1OhwBVfjmGPrjqAg_5p4fTuh7bJmXR1ZvwT88tKRA">