public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types
@ 2023-02-19 13:37 lh_mouse at 126 dot com
  2023-02-20 18:43 ` [Bug middle-end/108847] " pinskia at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: lh_mouse at 126 dot com @ 2023-02-19 13:37 UTC (permalink / raw)
  To: gcc-bugs

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
```

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] unnecessary bitwise AND on boolean types
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
@ 2023-02-20 18:43 ` pinskia at gcc dot gnu.org
  2023-05-20  1:14 ` [Bug middle-end/108847] unnecessary bitwise AND on boolean types and shifting of the "sign" bit pinskia at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-20 18:43 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
           Keywords|ABI                         |
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2023-02-20

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The workaround in this case is just to do:
((__INT32_TYPE__)value) < 0

instead of value>>31.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
  2023-02-20 18:43 ` [Bug middle-end/108847] " pinskia at gcc dot gnu.org
@ 2023-05-20  1:14 ` pinskia at gcc dot gnu.org
  2023-05-20 23:31 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-20  1:14 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|x86_64-*-*                  |x86_64-*-* aarch64-*-*
             Status|NEW                         |ASSIGNED

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I am messing around in this area ....

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
  2023-02-20 18:43 ` [Bug middle-end/108847] " pinskia at gcc dot gnu.org
  2023-05-20  1:14 ` [Bug middle-end/108847] unnecessary bitwise AND on boolean types and shifting of the "sign" bit pinskia at gcc dot gnu.org
@ 2023-05-20 23:31 ` pinskia at gcc dot gnu.org
  2023-05-20 23:34 ` [Bug middle-end/108847] [10/11/12/13/14 Regression] " pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-20 23:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So looking into this a little futher.
The problem is VRP turns !=0 into (bool) and then expand comes along and
decides that a cast to bool needs &1 because well it just. I am going to look
into see if I can remove the &1 there ...

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [10/11/12/13/14 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (2 preceding siblings ...)
  2023-05-20 23:31 ` pinskia at gcc dot gnu.org
@ 2023-05-20 23:34 ` pinskia at gcc dot gnu.org
  2023-05-20 23:34 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-20 23:34 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|enhancement                 |trivial

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [10/11/12/13/14 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (3 preceding siblings ...)
  2023-05-20 23:34 ` [Bug middle-end/108847] [10/11/12/13/14 Regression] " pinskia at gcc dot gnu.org
@ 2023-05-20 23:34 ` pinskia at gcc dot gnu.org
  2023-05-20 23:36 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-20 23:34 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |14.0
           Keywords|                            |deferred

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [10/11/12/13/14 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (4 preceding siblings ...)
  2023-05-20 23:34 ` pinskia at gcc dot gnu.org
@ 2023-05-20 23:36 ` pinskia at gcc dot gnu.org
  2023-05-27  6:50 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-20 23:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
VRP starting doing this in GCC 5:
Folding statement: _5 = _4 != 0;
Folded into: _5 = (bool) _4;

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [10/11/12/13/14 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (5 preceding siblings ...)
  2023-05-20 23:36 ` pinskia at gcc dot gnu.org
@ 2023-05-27  6:50 ` pinskia at gcc dot gnu.org
  2023-05-27  7:02 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-27  6:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---

Oh simple way to solve this to convert:
  t2_7 = (unsigned int) t_4;
  _1 = t1_6 | t2_7;
  t_8 = _1 != 0;

Into:
  t3_8 = t1_7 != 0;
  _1 = t_5 | t3_8;

Which is smaller even.

(for bit_op (bit_ior bit_and bit_xor)
 (simplify
  (ne (bit_op (convert @0) @1) integer_zerop@2)
   (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
        && types_match (type, @0))
    (bit_op @0 (ne @1 @2))))

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [10/11/12/13/14 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (6 preceding siblings ...)
  2023-05-27  6:50 ` pinskia at gcc dot gnu.org
@ 2023-05-27  7:02 ` pinskia at gcc dot gnu.org
  2023-09-17  1:49 ` [Bug middle-end/108847] [11/12/13/14 " pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-27  7:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Just for reference here are the functions I tried to see which way was the best
(set_bool0 and set_bool0_ produce the best for x86):
void
set_bool(bool& fl, __UINT32_TYPE__ value)
  {
    bool t = fl;
    __UINT32_TYPE__ t1 = value>>31;
    __UINT32_TYPE__ t2 = t;
    t = t2 | t1;
    fl = t;
  }

void
set_bool0(bool& fl, __UINT32_TYPE__ value)
  {
    bool t = fl;
    __UINT32_TYPE__ t1 = value>>31;
    bool t3 = t1;
    t = t | t3;
    fl = t;
  }  

void
set_bool0_(bool& fl, __INT32_TYPE__ value)
  {
    bool t = fl;
    __UINT32_TYPE__ t1 = value<0;
    bool t3 = t1;
    t = t | t3;
    fl = t;
  }

  void
set_bool1(bool& fl, __UINT32_TYPE__ value)
  {

    fl |= value >> 31;
  }

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [11/12/13/14 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (7 preceding siblings ...)
  2023-05-27  7:02 ` pinskia at gcc dot gnu.org
@ 2023-09-17  1:49 ` pinskia at gcc dot gnu.org
  2024-03-08 15:38 ` law at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-17  1:49 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=52345

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Funny, the match pattern in comment #5 is similar to the one which I was
working on for PR 52345 .

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [11/12/13/14 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (8 preceding siblings ...)
  2023-09-17  1:49 ` [Bug middle-end/108847] [11/12/13/14 " pinskia at gcc dot gnu.org
@ 2024-03-08 15:38 ` law at gcc dot gnu.org
  2024-05-07  7:40 ` [Bug middle-end/108847] [11/12/13/14/15 " rguenth at gcc dot gnu.org
  2024-08-01  9:31 ` [Bug middle-end/108847] [12/13/14/15 " jakub at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-08 15:38 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |law at gcc dot gnu.org
           Priority|P3                          |P2

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [11/12/13/14/15 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (9 preceding siblings ...)
  2024-03-08 15:38 ` law at gcc dot gnu.org
@ 2024-05-07  7:40 ` rguenth at gcc dot gnu.org
  2024-08-01  9:31 ` [Bug middle-end/108847] [12/13/14/15 " jakub at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-07  7:40 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|14.0                        |14.2

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 14.1 is being released, retargeting bugs to GCC 14.2.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug middle-end/108847] [12/13/14/15 Regression] unnecessary bitwise AND on boolean types and shifting of the "sign" bit
  2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
                   ` (10 preceding siblings ...)
  2024-05-07  7:40 ` [Bug middle-end/108847] [11/12/13/14/15 " rguenth at gcc dot gnu.org
@ 2024-08-01  9:31 ` jakub at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-08-01  9:31 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|14.2                        |14.3

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 14.2 is being released, retargeting bugs to GCC 14.3.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-08-01  9:31 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-19 13:37 [Bug middle-end/108847] New: unnecessary bitwise AND on boolean types lh_mouse at 126 dot com
2023-02-20 18:43 ` [Bug middle-end/108847] " pinskia at gcc dot gnu.org
2023-05-20  1:14 ` [Bug middle-end/108847] unnecessary bitwise AND on boolean types and shifting of the "sign" bit pinskia at gcc dot gnu.org
2023-05-20 23:31 ` pinskia at gcc dot gnu.org
2023-05-20 23:34 ` [Bug middle-end/108847] [10/11/12/13/14 Regression] " pinskia at gcc dot gnu.org
2023-05-20 23:34 ` pinskia at gcc dot gnu.org
2023-05-20 23:36 ` pinskia at gcc dot gnu.org
2023-05-27  6:50 ` pinskia at gcc dot gnu.org
2023-05-27  7:02 ` pinskia at gcc dot gnu.org
2023-09-17  1:49 ` [Bug middle-end/108847] [11/12/13/14 " pinskia at gcc dot gnu.org
2024-03-08 15:38 ` law at gcc dot gnu.org
2024-05-07  7:40 ` [Bug middle-end/108847] [11/12/13/14/15 " rguenth at gcc dot gnu.org
2024-08-01  9:31 ` [Bug middle-end/108847] [12/13/14/15 " jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).