<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/91754>91754</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Failure to spot bitmask building loop idiom
</td>
</tr>
<tr>
<th>Labels</th>
<td>
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
These two functions (which calculate the mask necessary to extract the n lowest bits from a 64-bit int) are equivalent, but LLVM fails to optimize the first into the second:
[godbolt](https://godbolt.org/z/zdrbaxonT), [alive](https://alive2.llvm.org/ce/z/LuaWSV)
```c
#include <stdint.h>
uint64_t src(uint8_t n) {
uint64_t mask = 0;
for (uint8_t i = 0; i < n; i++) {
mask |= UINT64_C(1) << i;
}
return mask;
}
uint64_t tgt(uint8_t n) {
return (n == 64) ? UINT64_MAX : (UINT64_C(1) << n) - 1;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8U0tvrDYY_TVm82lGxgYGFiwyM0WqmnTTNO0uMvAxuDH21I-8fv2VPY9EUe5FBmy-c46PsY9wTh40YkvKLSn3mQh-Nrb9Y0HxJHXWm_GtvZ_RIfgXA1PQg5dGOyCsfpnlMMMg1BCU8Ah-RliEewKNAzon7Bt4A_jqrRh8qmpQ5gWdh156B5M1CwioilUvPUjtCWtAWAT8P8hnoTB-2UEfPNzePtzBJKRyUdIcvVzk-2nGSVqX2CYNHQ5Gj4TfELon9PIstwcz9kZ5Uu4Jq2fvjy5iWEdYdy6tjT0Q1r3He7S9eDX6nrAmWiDlVij5jN-xU4GtlXpezgoDnmVug_jnr4eo8dlLRU9tOI8Zl3pQYUQgfOf8KLVfz4T_9pkUpPZV8ejB2YGwOg7rRw86_jGy2Z5AAABXYNoIwvdACb_UJ2PhE1te66m7A526hG1T-6Icr5PoZhd5f__-531VPO4Iq_ME5ruoIT-mI5v9B92iD1YnhSviCviySH_wv1zkWYuwWsclRDdVcfLQXXzd3fwLhN9E0E-cJt0V5N_YuWxRNrZ8bHgjMmzzTV7ykvO8yuaWY1P2NKesprjJy2Gop6ZpmNhw1mApiky2jLKCljnNS5qX-bribKIM-URzPlKsSEFxEVJdD04mnQvYNvmmLDIlelQuZZKxRTqH4-p86kXMH2EshtW2kb3qw8GRgirpvPvQ89IrbDshVbAYY-OOJiUv7WIfpBqlPoAy5ghylGbJglXtl2hIP4d-PZiFsC4qn1-rozX_4eAJ65JtR1iXnP8IAAD__4XwRFw">