public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one
@ 2020-06-27  3:07 gabravier at gmail dot com
  2020-06-27 12:42 ` [Bug tree-optimization/95929] " glisse at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: gabravier at gmail dot com @ 2020-06-27  3:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95929
           Summary: Failure to optimize tautological comparisons of
                    comparisons to a single one
           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: ---

int f(int a, int b)
{
    return (((b != 0) & (a == 0)) | ((a != 0) & (b == 0)));
}

This can be optimized to `(a != 0) ^ (b != 0)`. I originally found this while
compiling this code :

inline bool nand(bool a, bool b)
{
    return !(a && b);
}

int f(int a, int b)
{
    return nand(nand(b, nand(a, a)), nand(a, nand(b, b)));
}

Which GCC compiles to the above example, and that LLVM optimizes with the
transformation I gave (strangely, LLVM does not seem to optimize the example at
the top of this bug report to the transformed version if directly given the top
example, which is why I'm giving these details as I'm thinking there could be
some kind of UB weirdness or something like that with the transformations here)

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
@ 2020-06-27 12:42 ` glisse at gcc dot gnu.org
  2021-08-15  0:09 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-06-27 12:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> ---
Here gcc does optimize the first f to (a != 0) ^ (b != 0). However, for the
second f, it does indeed generate something that looks like the first f before
optimization... The optimization for the first f is probably "(X && !Y) || (!X
&& Y) is X ^ Y" in fold-const.c, which may not have an equivalent in match.pd
yet.

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
  2020-06-27 12:42 ` [Bug tree-optimization/95929] " glisse at gcc dot gnu.org
@ 2021-08-15  0:09 ` pinskia at gcc dot gnu.org
  2023-06-18 18:28 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-15  0:09 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-08-15
           Severity|normal                      |enhancement
     Ever confirmed|0                           |1

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

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
  2020-06-27 12:42 ` [Bug tree-optimization/95929] " glisse at gcc dot gnu.org
  2021-08-15  0:09 ` pinskia at gcc dot gnu.org
@ 2023-06-18 18:28 ` pinskia at gcc dot gnu.org
  2023-08-22 16:53 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-18 18:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
On the GCC 13+ we get:
  _1 = a_5(D) != 0;
  _8 = a_5(D) == 0; // _1^1
  _2 = b_3(D) != 0;
  _13 = _2 ? _8 : _1;
  _6 = (int) _13;

or rather
_13 = _2 ? _1 ^ 1 : _1

But we don't match that.

We could dosoemthing like:
```
(for cmp
(for cmpN
(simplify
 (cond
  @0 
  (cmpN   @1 @2)
  (cmp@3  @1 @2))
 (if (inverseof(cmp, TREE_TYPE (@1)) == cmpN)
  (bit_xor (convert @0) @3)
 )
)
)
)
```
And that would fix this I think.

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2023-06-18 18:28 ` pinskia at gcc dot gnu.org
@ 2023-08-22 16:53 ` pinskia at gcc dot gnu.org
  2023-08-22 17:02 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-22 16:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
r14-3110-g7fb65f102851 (handling of a?~t:t -> (-(a))^t) basically fixes this.

The only thing left is removing of `bool = -bool`:
In:
  _1 = a_5(D) != 0;
  _2 = b_3(D) != 0;
  _13 = -_2;
  _12 = _1 ^ _13;

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2023-08-22 16:53 ` pinskia at gcc dot gnu.org
@ 2023-08-22 17:02 ` pinskia at gcc dot gnu.org
  2023-08-24  2:39 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-22 17:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
/* -bool is always bool, likewise for abs and absu. */
(for op (negate abs absu)
 (simplify
  (op @0)
  (if (INTEGER_TYPE_P (type) && TYPE_PRECISION (type) == 1)
   (non_lvalue @0))))

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2023-08-22 17:02 ` pinskia at gcc dot gnu.org
@ 2023-08-24  2:39 ` pinskia at gcc dot gnu.org
  2023-08-24  6:52 ` cvs-commit at gcc dot gnu.org
  2023-08-24  6:53 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-24  2:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2023-08-24  2:39 ` pinskia at gcc dot gnu.org
@ 2023-08-24  6:52 ` cvs-commit at gcc dot gnu.org
  2023-08-24  6:53 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-08-24  6:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 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:ddd64a6ec3b38e18aefb9fcba50c0d9297e5e711

commit r14-3432-gddd64a6ec3b38e18aefb9fcba50c0d9297e5e711
Author: Andrew Pinski <apinski@marvell.com>
Date:   Tue Aug 22 18:41:56 2023 -0700

    MATCH: remove negate for 1bit types

    For 1bit types, negate is either undefined or don't change the value.
    In either cases we want to remove them.
    This patch adds a match pattern to do that.
    Also converting to a 1bit type we can remove the negate just like we
already do
    for `&1` so this patch adds that too.

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

    Notes on the testcases:
    This patch is the last part to fix PR 95929; cond-bool-2.c testcase.
    bit1neg-1.c is a 1bit-field testcase where we could remove the assignment
    all the way in one case (which happened on the RTL level for some targets
but not all).
    cond-bool-2.c is the reduced testcase of PR 95929.

            PR tree-optimization/95929

    gcc/ChangeLog:

            * match.pd (convert?(-a)): New pattern
            for 1bit integer types.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/bit1neg-1.c: New test.
            * gcc.dg/tree-ssa/cond-bool-1.c: New test.
            * gcc.dg/tree-ssa/cond-bool-2.c: New test.

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

* [Bug tree-optimization/95929] Failure to optimize tautological comparisons of comparisons to a single one
  2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2023-08-24  6:52 ` cvs-commit at gcc dot gnu.org
@ 2023-08-24  6:53 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-24  6:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed. cond-bool-2.c is the testcase.

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

end of thread, other threads:[~2023-08-24  6:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-27  3:07 [Bug tree-optimization/95929] New: Failure to optimize tautological comparisons of comparisons to a single one gabravier at gmail dot com
2020-06-27 12:42 ` [Bug tree-optimization/95929] " glisse at gcc dot gnu.org
2021-08-15  0:09 ` pinskia at gcc dot gnu.org
2023-06-18 18:28 ` pinskia at gcc dot gnu.org
2023-08-22 16:53 ` pinskia at gcc dot gnu.org
2023-08-22 17:02 ` pinskia at gcc dot gnu.org
2023-08-24  2:39 ` pinskia at gcc dot gnu.org
2023-08-24  6:52 ` cvs-commit at gcc dot gnu.org
2023-08-24  6:53 ` 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).