<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/79630>79630</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[llvm] add libcall optimizations for C23 stdbit.h routines
</td>
</tr>
<tr>
<th>Labels</th>
<td>
c23,
llvm:optimizations,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nickdesaulniers
</td>
</tr>
</table>
<pre>
C23 added a bunch of type generic bitwise functions like:
- `stdc_leading_zeros`
- `stdc_leading_ones`
- `stdc_trailing_zeros`
- `stdc_trailing_ones`
- `stdc_first_leading_zero`
- `stdc_first_leading_one` (smells like `ffs` but using proper signedness)
- `stdc_first_trailing_zero`
- `stdc_first_trailing_one`
- `stdc_count_zeros`
- `stdc_count_ones`
- `stdc_has_single_bit`
- `stdc_bit_width`
- `stdc_bit_floor`
- `stdc_bit_ceil`
Refer to section 7.18 of the latest C23 draft (n3096 is what I'm looking at).
All of the above are type generic, so I expect libc implementations to use `_Generic` to dispatch to the proper variant. There are type specific variants of all of the above with suffixes `uc`, `us`, `ui`, `ul`, `ull` for `unsigned char`, `unsigned short`, `unsigned int`, `unsigned long`, and `unsigned long long` respectively.
We should recognize calls to all of the above (14 type-generic functions cross 5 specific types == 70 functions) and ensure that calling them with a constant value is recognized (in hosted mode) and folded properly, thereby eliminating the library dispatch.
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp should recognize these calls in IR and ensure we can elide the libcalls.
cc @efriedma-quic @davidbolvansky
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVU2T4jYQ_TXi0jWULQ9fBw6ToUhtVU6bTeVIyVIbd0aWHHWbWfbXp2RgBrKQXEDWa3e_fv0sGWbaB8S1mv2iZpuJGaSNaR3IvjlkM_hAmHhSR3dcv-oKjHPowEA9BNtCbECOPcIeAyayUJO8EyM0Q7BCMTB4ekNVvahio4qXJ1DzgsXZ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