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

    <tr>
        <th>Summary</th>
        <td>
            [Missed optimization] `has_single_bit` when the value is known to be non-zero
        </td>
    </tr>

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

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

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

<pre>
    ```c++
bool test1(unsigned n) {
 __builtin_assume(n);
    return __builtin_popcount(n) == 1;
}

bool test2(unsigned n) {
    __builtin_assume(n);
 return (n != 0) && ((n & (n-1u)) == 0);
}
```

Where `test1` looks like libstdc++'s `std::has_single_bit` and `test2` looks like libc++'s `std::has_single_bit`.  
These are optimized differently with the non-zero assumption: https://godbolt.org/z/b8jTae91x

```asm
test1: # @test1
        popcnt  eax, edi
        cmp eax, 1
        sete    al
        ret
test2: # @test2
        blsr    eax, edi
        sete    al
 ret
```

(without the assumption, they are both optimized to use `popcnt`)

`return __builtin_popcount(n) <= 1;` *is* optimized to `blsr`
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVMtu4zoM_Rp6QzSQ6ffCC0-D7GY3wF0GssXEmipSYMnttF9_Iefd3kcNAxIo8vDwkJL0Xu8tcwvFDyjWiZzD6Kb2pw7SdH50L0nv1HsLpTj9A9CP-Is1iK53zmBgH1KgerYLkkIL1CBUZx_cbvtZm6DtVno_Hxiojh6QXRwQceIwT_bO9eiOg5ttODsjZGvI1pheo6BanzePTOg_mCD-P5kzk2hHoDQmFQsKlUBltJ-Plr19SucYfmMo7tFuJC_y3XP-a-SJEUpxUrAUaJx78Wj0C6PRvQ_qojZVPjr6oCDrIOtG6bde273hba9DDJVWXaDoK9S3cVaIJ3K_RvaMcmJ0x6AP-oMVKr3b8cQ2mHd802HEMDJaZ58-eHK4CHoM2lnIOhxDOPqYhDZAm71TvTNh5aY90OYDaNPXv39JbtI_94pcZZL-cLKcpMk6BMoQ8rNU13bGL46KDYgs_wA9Iyv9eD4cjpezT5GeA8dVmkf7xOGWnT5lp0ff3vgprv-W_UuOK_g_zgRQHZV1c1jEvdOUnqPlfWlJ78J415fgcPbLJJ20iJjUfBL2G1fs-XrFSoFAnfZA3WMeKEWsGEqRqDZTTdbIhNu0SmsSVBaUjG1R7dRO5juq66YaiIeK8iytVUEqzSuZJbolQbkoU0rzTBRilcu6ULtMiqEoZFPvIBd8kNqsjHk9xJlJtPczt01BRZEY2bPxy3tFZPkNl0Mgis_X1MaYp37ee8iF0T74G0rQwSwP3U_tPatLZXLRt1jH4r5erLeR7dKLV2lmRu3xxbo3G8Xob9OfzJNpP828DuPcrwZ3ANpEDufl6Ti53zwEoM3C3ANtTpW9tvR3AAAA__-Rxp8C">