https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108847

            Bug ID: 108847
           Summary: unnecessary bitwise AND on boolean types
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

Godbolt: https://gcc.godbolt.org/z/fsavMzMo7

```
void
set_bool(bool& fl, __UINT32_TYPE__ value)
  {
    fl |= value >> 31;
  }
```

This code shifts a `uint32` to the right by 31 bits, so the result will only be
0 or 1.

Clang outputs:

```
set_bool(bool&, unsigned int):                         # @set_bool(bool&,
unsigned int)
        shr     esi, 31
        or      byte ptr [rdi], sil
        ret
```

but GCC emits an additional unnecessary bitwise AND operation on the
destination operand:

```
set_bool(bool&, unsigned int):
        shr     esi, 31
        or      BYTE PTR [rdi], sil
        and     BYTE PTR [rdi], 1
        ret
```

Reply via email to