<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/102734>102734</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
compiler-rt: Seemingly unused workaround in clzdi2.c/ctzdi2.c
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mintsuki
</td>
</tr>
</table>
<pre>
In `compiler-rt/lib/builtins/{clzdi2.c,ctzdi2.c}`, the following snippet of code is present:
(In clzdi2.c)
```c
#if !defined(__clang__) && \
((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) || \
(defined(__riscv) && __SIZEOF_POINTER__ >= 8))
// On 64-bit architectures with neither a native clz instruction nor a native
// ctz instruction, gcc resolves __builtin_clz to __clzdi2 rather than
// __clzsi2, leading to infinite recursion.
#define __builtin_clz(a) __clzsi2(a)
extern int __clzsi2(si_int);
#endif
```
(In ctzdi2.c)
```c
#if !defined(__clang__) && \
((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) || \
(defined(__riscv) && __SIZEOF_POINTER__ >= 8))
// On 64-bit architectures with neither a native clz instruction nor a native
// ctz instruction, gcc resolves __builtin_ctz to __ctzdi2 rather than
// __ctzsi2, leading to infinite recursion.
#define __builtin_ctz(a) __ctzsi2(a)
extern int __ctzsi2(si_int);
#endif
```
This code seems to define `__builtin_{clz,ctz}` to `__{clz,ctz}si2` on some architectures to work around an issue on GCC.
The issue is that these files were later modified to *not* use `__builtin_{clz,ctz}` anymore, but instead they call `clzsi()/ctzsi()` from `int_lib.h`.
`int_lib.h` then has this code to define `clzsi()/ctzsi()` to either `__builtin_{clz,ctz}` or `__builtin_{ctz,clz}l`:
```c
#if UINT_MAX == 0xFFFFFFFF
#define clzsi __builtin_clz
#define ctzsi __builtin_ctz
#elif ULONG_MAX == 0xFFFFFFFF
#define clzsi __builtin_clzl
#define ctzsi __builtin_ctzl
#else
#error could not determine appropriate clzsi macro for this system
#endif
```
This means that the workaround from the `clzdi2.c/ctzdi2.c` files doesn't seem to be applied anymore. So either this workaround is no longer useful, and the dead code can be removed, or this is a bug and it should be fixed.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcVk9v27gT_TT0ZRBDpmxZPuiQOnUR4PdrFtsusNiLQEkja7YUaZAjp9lPvyAlx39QNLvbW4UgiTSP84Yzb4ZU3tPeIBZi9U6sHmZq4M66oifDfvhCs8o2L8WjAZElte0PpNHdORZyp6kSclcNpJmMF3In1u9q_VdDcl4Lua15-nf9ILJEyC1wh9Bare0zmT14Q4cDMtgWatsgkIeDQ4-GRXovkgeRnH7L_NHA2fNm-pwl4099gqXUgpCLBlsy2AiZl2WtldmXpZAbEDITMgOx2o54AAAhcyHzywX-oFx9teDSqlzdZctojoj1Vqy3V4ieDj5bXhjH58x6w-fI18cLtrL89PjH-6dd-cvT48fP738tSxDpe5E-QD6Snja7E3IHTway5V1FDCEyYqx5cOjhmbgDg8QdOlBgFNMRQwqBjGc31EzWgLFn45Xbmq-AoXb7ugaH3uojeijLqeplcMkWQqJDecCpSMmdMlceI8CTDK40qiYIgC2QackQIzisB-fJmvlrMccsXXMJmauQrLO7-D6uwa-MzgAZvrR7KslwAKXvXn2jaai9kdE3JMc_h-Qm2p9JdnySHb8lO_5h2fGl7PgN2fGPyO5zR34chh6x9yHUKRyRJeeIxjk7jthxugZkhNyYQixZAtaAtz3eVIstPFv3BZSzg2lAGSDvBwzoD9vt_DownIzkQ5Y5zHKP0JIOZUeHoBWjg9421BI2MSB5bywLeQ-Df3sHyrz01mGoVDVw1AGqJvC8QK20judPaOrYQBshdzHX01uWQOtsH0BkuNRUzTuRJVebuLEF1wY6FTZ0SvtVwr_LxhYmnb-1MfsNCEeIDhAdNJDef2_A_Pb48XP5__vfQaQPoSWTr7vpuVVtDPlmZN5C-AbCZwjqwPa_p48f_jOd_gd8-oLQ4_nFOeugtoNuwFiGBhldH3yow8HZg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