public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/95923] New: Failure to optimize bool checks into and
@ 2020-06-27  0:49 gabravier at gmail dot com
  2020-06-27 12:54 ` [Bug tree-optimization/95923] " glisse at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: gabravier at gmail dot com @ 2020-06-27  0:49 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95923
           Summary: Failure to optimize bool checks into and
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

bool f(bool a, bool b)
{
    if (!a && !b)
        return 0;
    if (!a && b)
        return 0;
    if (a && !b)
        return 0;
    return 1;
}

This can be optimized to `return a & b;`. This transformation is done by LLVM,
but not by GCC.

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
@ 2020-06-27 12:54 ` glisse at gcc dot gnu.org
  2021-04-26  1:03 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-06-27 12:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> ---
(With one variant I ended up with (a|b)&(a==b), which we don't optimize to a&b)

We don't optimize !(!a && !b) && !(!a && b) && !(a && !b) (we keep several
branches), but we do optimize if I manually replace enough && with &.

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
  2020-06-27 12:54 ` [Bug tree-optimization/95923] " glisse at gcc dot gnu.org
@ 2021-04-26  1:03 ` pinskia at gcc dot gnu.org
  2021-08-07  7:41 ` pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-04-26  1:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
  2020-06-27 12:54 ` [Bug tree-optimization/95923] " glisse at gcc dot gnu.org
  2021-04-26  1:03 ` pinskia at gcc dot gnu.org
@ 2021-08-07  7:41 ` pinskia at gcc dot gnu.org
  2023-06-11 19:27 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-07  7:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-08-07
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Mine.
  _1 = ~a_6(D);
  _3 = _1 & b_7(D);
  if (_3 != 0)
    goto <bb 5>; [34.00%]
  else
    goto <bb 4>; [66.00%]

  <bb 4> [local count: 467721933]:
  _10 = _1 | b_7(D);

  <bb 5> [local count: 1073741824]:
  # _5 = PHI <0(3), _10(4), 0(2)>

This is _3 ? 0 : _10 -> (_10 & ~3)

And then this simplifies more.

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2021-08-07  7:41 ` pinskia at gcc dot gnu.org
@ 2023-06-11 19:27 ` pinskia at gcc dot gnu.org
  2023-07-16  0:02 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-11 19:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
After r14-1597-g64d90d06d2db, we now have:
  _8 = a_6(D) | b_7(D);
  _10 = a_6(D) == b_7(D);
  _1 = _8 & _10;

(a == b) & (a | b)

I am no longer working on this right now. That is a job for reassociate I
think.

Note I noticed that we don't remove some "dead" statements during phiopt3 too:
```
  if (_8 != 0)
    goto <bb 3>; [66.00%]
  else
    goto <bb 4>; [34.00%]

  <bb 3> [local count: 708669601]:
  _1 = ~a_6(D);
  _2 = _1 & b_7(D);
  _10 = a_6(D) == b_7(D);

  <bb 4> [local count: 1073741824]:
  # _5 = PHI <_10(3), 0(2)>
```

I will fix that before unassigning. The problem there is:
We had:
  _1 = ~a_6(D);
  _2 = _1 & b_7(D);
  if (_2 != 0)

But we don't mark _2 as possible unused even though match will produce without
it:
Folded into the sequence:
_3 = ~b_7(D);
_4 = _3 | a_6(D);
_11 = a_6(D) ^ b_7(D);
_10 = a_6(D) == b_7(D);
statement un-sinked:
_12 = _1 | b_7(D);

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2023-06-11 19:27 ` pinskia at gcc dot gnu.org
@ 2023-07-16  0:02 ` pinskia at gcc dot gnu.org
  2023-07-16  0:17 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-16  0:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So there are some patterns for non boolean as for boolean values ~(a^b) is
converted to a==b so we need to support the == case for these:
```
/* (a | b) & ~(a ^ b)  -->  a & b  */
(simplify
 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
 (bit_and @0 @1))

/* a | ~(a ^ b)  -->  a | ~b  */
(simplify
 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
 (bit_ior @0 (bit_not @1)))

/* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
(simplify
 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
 @2)
```

So mine.

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2023-07-16  0:02 ` pinskia at gcc dot gnu.org
@ 2023-07-16  0:17 ` pinskia at gcc dot gnu.org
  2023-07-16 22:57 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-16  0:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #4)
> So there are some patterns for non boolean as for boolean values ~(a^b) is
> converted to a==b so we need to support the == case for these:
The non-boolean ones were originally added by 
r8-4395-ge268a77b59cb78

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2023-07-16  0:17 ` pinskia at gcc dot gnu.org
@ 2023-07-16 22:57 ` pinskia at gcc dot gnu.org
  2023-07-16 22:57 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-16 22:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55558
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55558&action=edit
Patch which I am testing

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2023-07-16 22:57 ` pinskia at gcc dot gnu.org
@ 2023-07-16 22:57 ` pinskia at gcc dot gnu.org
  2023-07-17  2:44 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-16 22:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I will fix the phiopt issue later ...

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (7 preceding siblings ...)
  2023-07-16 22:57 ` pinskia at gcc dot gnu.org
@ 2023-07-17  2:44 ` pinskia at gcc dot gnu.org
  2023-07-17  6:43 ` cvs-commit at gcc dot gnu.org
  2023-07-17  6:45 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-17  2:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2023-July/62
                   |                            |4594.html

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch posted:
https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624594.html

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (8 preceding siblings ...)
  2023-07-17  2:44 ` pinskia at gcc dot gnu.org
@ 2023-07-17  6:43 ` cvs-commit at gcc dot gnu.org
  2023-07-17  6:45 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-17  6:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:0407ae8a7732d90622a65ddf1798c9d51d450e9d

commit r14-2556-g0407ae8a7732d90622a65ddf1798c9d51d450e9d
Author: Andrew Pinski <apinski@marvell.com>
Date:   Sun Jul 16 22:31:59 2023 +0000

    PR 95923: More (boolean) bitop simplifications in match.pd

    This adds the boolean version of some of the simplifications
    that were added with r8-4395-ge268a77b59cb78.

    That are the following:
    (a | b) & (a == b) --> a & b
    a | (a == b)       --> a | (b ^ 1)
    (a & b) | (a == b) --> a == b

    OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

    gcc/ChangeLog:

            PR tree-optimization/95923
            * match.pd ((a|b)&(a==b),a|(a==b),(a&b)|(a==b)): New
transformation.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/95923
            * gcc.dg/tree-ssa/bitops-2.c: New test.
            * gcc.dg/tree-ssa/bool-checks-1.c: New test.

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

* [Bug tree-optimization/95923] Failure to optimize bool checks into and
  2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
                   ` (9 preceding siblings ...)
  2023-07-17  6:43 ` cvs-commit at gcc dot gnu.org
@ 2023-07-17  6:45 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-17  6:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |14.0
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

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

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

end of thread, other threads:[~2023-07-17  6:45 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-27  0:49 [Bug tree-optimization/95923] New: Failure to optimize bool checks into and gabravier at gmail dot com
2020-06-27 12:54 ` [Bug tree-optimization/95923] " glisse at gcc dot gnu.org
2021-04-26  1:03 ` pinskia at gcc dot gnu.org
2021-08-07  7:41 ` pinskia at gcc dot gnu.org
2023-06-11 19:27 ` pinskia at gcc dot gnu.org
2023-07-16  0:02 ` pinskia at gcc dot gnu.org
2023-07-16  0:17 ` pinskia at gcc dot gnu.org
2023-07-16 22:57 ` pinskia at gcc dot gnu.org
2023-07-16 22:57 ` pinskia at gcc dot gnu.org
2023-07-17  2:44 ` pinskia at gcc dot gnu.org
2023-07-17  6:43 ` cvs-commit at gcc dot gnu.org
2023-07-17  6:45 ` pinskia 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).