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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] Missed optimization within bitshift on bools
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    C code:
func1 only differs by using another bool from comparison of (ptr[n] == '_').
Otherwise, they should behave exactly the same.
```
#include <stdbool.h>
#include <stddef.h>

unsigned long func0(unsigned char * restrict ptr){
        unsigned long ret_val = 0;
        bool tmp_bool = false;
        for(size_t n = 0; n < 64; n++){
                tmp_bool = (ptr[n] == '_') || (ptr[n] >= 'a' && ptr[n] <= 'z') || (ptr[n] >= 'A' && ptr[n] <= 'Z') || (ptr[n] >= '0' && ptr[n] <= '9');
                ret_val |= ((unsigned long)tmp_bool << n);
                tmp_bool = false;
 }
        return ret_val;
}

unsigned long func1(unsigned char * restrict ptr){
        unsigned long ret_val = 0;
        bool tmp_bool = false;
        bool tmp_bool1 = false;
        for(size_t n = 0; n < 64; n++){
                tmp_bool1 = ptr[n] == '_';
                tmp_bool = tmp_bool || (ptr[n] >= 'a' && ptr[n] <= 'z') || (ptr[n] >= 'A' && ptr[n] <= 'Z') || (ptr[n] >= '0' && ptr[n] <= '9');
                ret_val |= ((unsigned long)(tmp_bool || tmp_bool1) << n);
                tmp_bool = false;
                tmp_bool1 = false;
        }
        return ret_val;
}
```
[Godbolt](https://godbolt.org/z/xx6q4abjs) show that func1 reduces instructions by 101 compared to func0.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVU2P6yYU_TXXm6sXAf5eeJE4L11V3XTVzQgbHPOEIQU8M5lfX-Fk8pJ0Rhl10W6KkELC4XDvuYdc7r3aGykbyDeQbxM-h9G6ZuMkF7_bKemsODYt9lZISNdA1sNseorW6CMKNQzSeeyOOHtl9siNDaN02FmrcXB2wt5OB-6UtwbtgMCqQ3CQbwzkW4R0C-kWgZVPwEpg9QrI-rdI8KK8BNZiGOUR_WhnLbCTI3-WKF95H_QxbqHnk4xnoCDnSdbAUmV6PQuJkLY-iBjLaoT0-0ebQg6XPbKezaKFQG3NHmOiBFh1-bUfuUNga3TSB6f6gDEZVkO5AbLG87glcTI8PXMdc0UC6TVwESlMh6dlEQED117eggbrgFVevcmngOZCsyxbLLJlDWyzzLtI3sfNHQ9KgFC2ULZ_g30_wziwEoEVwAq8AbRnwNvXeNaPeP74Gg95xFOfeG5VfR-X6pRneHVd71hAYPWVfG0U3XxK92kxodxewZ0MszPvl58gJ8SHHqT_tQdvQPTfcOrpks99-lj9n1_-9_Odn4FV9-pcdF8i_Kc2B1IDqR8a5cuP4fpfPd_8YkVndYB8C6waQzj42I_YDthuf9paWbcHtnsDtnt9Lf7MePfDx4T8aF8wjDycnhM6KeZeelTGBzf3QVmz9DBK6LlfSYHBnhrAKhFNKuq05olsaJnRKs0ozZKxKQpSd53kVUYJpbLM04xxOgyiIGlPO5KohhGWk5SmhGVVmq3quqp7nlLRy1LURQEZkRNXeqX18xSjT5T3s2xoSkleJJp3UvulMTPWax6rx2KPdk088K2b9x4yopUP_idFUEEv3bxdTuRb_FV5LwXaQ1CTeuMxX3xRYVQGOxX8qIaA1izP3Cez082duiqMc7fq7QRsF685f3w7OPtD9gHYbgnbA9udI39u2F8BAAD__1j4TsA">