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

    <tr>
        <th>Summary</th>
        <td>
            Cannot constant fold functions from <cwchar>
        </td>
    </tr>

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

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

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

<pre>
    Clang has builtins for a lot of the `<cwchar>` functions and can constant evaluate them when using the `__builtin_` variants, but the backend can't constant fold them. There doesn't seem to be much of a technical reason, since their `<cstring>` counterparts can often be constant folded, and some even have passes dedicated to them.

For example,
```c++
#include <cstddef>

auto test(wchar_t *ptr, size_t count) {
  wchar_t buffer[] = L"Banane";
  return __builtin_wmemchr(buffer, L'a', __builtin_wcslen(buffer)) != nullptr;
}

static_assert([] {
  wchar_t buffer[] = L"Banane";
  return __builtin_wmemchr(buffer, L'a', __builtin_wcslen(buffer)) != nullptr;
}());
```
compiles just fine, but `test` generates horrible code compared to what should be possible: https://godbolt.org/z/xP3vM9jPn
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVM2u4jgTfRpnU2pkKvcmsMjiXvhYfSP1YvaoYleIbzt25B_o6acfOYGB6TcYyTJgnzp1jqsoitFcHHMn3j_F-7GinEYfunk01pkfbdtWvdd_dQdL7gIjReizscm4CIMPQGB9Aj9AGhlEI0V9UDc1UhD1_0QjYchOJeNdBHIaFDlQ3sVELgFfyWZKXEInuI3sIEfjLg-q8_me6VyIrhQMuRQFHqDPaQH1pH7wSiuwTU_qwVu90G7gz5EDg_YcV0xkniB56BmmrMYinSCxGp1RZCEwRe9KkmicWrSZ8DAWUzDucnemfHaJw0whxcWYHxK7wvsvGawLWTEf_cTAV3Yw0pVhphg5gmZtFCXWRdQiWcijkB_rfvIB-CdNs2WBh_tNI9elBH6WtZ5ibZyyWTOsUrXmoUh9YaNccnBMAndLkc4JBH7MKayGf_E5rb4E7kG0d2qAB7jPw8BhbRQQ9RH-LxA_yZFjgSjqfwICpxwcPEt4m3hSYxC4u3PgoQS3JLAt31-QKlp2L8D9Iga3JZ_L1ha5j0yiPb4ajImSUefysqGYfCj9jzjB3Yp5Hj5Kvf5UfpqN5QhfOSYYTNG6_htEI5e6NhIu7DhQ4gijD8H0tjSkLts0U1j77DZSgjj6bHVp2NnHWICi_oAxpTmK-kPgSeDp4nXvbdr4cBF4-iXw9PN7ff1j__XdVbqr9b7eU8XdtkXctlLKpho73MtGIql232Ct-92u5pa2u0axRIVElelQ4ptscbfFusZmgzVpPbQoh1pi22rxJnkiYzfWXqeSuzIxZu62Uu5xX1nq2cZlXiE6vsFyW8r2fqxCV4K-9fkSxZu0Jqb4pEkmWe4O5Jz_fVo8B9UQ_ASvc6zKwXa_vYtJY-43yk8CT4X-_vFtDv6LVRJ4WkRFgae76muHfwcAAP__yPu2Kw">