public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/94718] New: Failure to optimize opposite signs check
@ 2020-04-22 16:51 gabravier at gmail dot com
  2020-04-22 16:53 ` [Bug tree-optimization/94718] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: gabravier at gmail dot com @ 2020-04-22 16:51 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94718
           Summary: Failure to optimize opposite signs check
           Product: gcc
           Version: 10.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(int x, int y)
{
    return (x < 0) != (y < 0); 
}

`(x < 0) != (y < 0)` can be optimized to `(x ^ y) < 0`. 
This transformation is done by clang, but not by GCC, as seen here :
https://godbolt.org/z/AnYPBF.

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
@ 2020-04-22 16:53 ` pinskia at gcc dot gnu.org
  2020-04-23  5:57 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-04-22 16:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |missed-optimization

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
  2020-04-22 16:53 ` [Bug tree-optimization/94718] " pinskia at gcc dot gnu.org
@ 2020-04-23  5:57 ` rguenth at gcc dot gnu.org
  2020-04-23 13:15 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-23  5:57 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-04-23
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |easyhack
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
  2020-04-22 16:53 ` [Bug tree-optimization/94718] " pinskia at gcc dot gnu.org
  2020-04-23  5:57 ` rguenth at gcc dot gnu.org
@ 2020-04-23 13:15 ` jakub at gcc dot gnu.org
  2020-04-23 15:44 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-04-23 13:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I guess:
int
foo (int x, int y)
{
  return (x < 0) != (y < 0);
}

int
bar (int x, int y)
{
  return (x < 0) == (y < 0);
}

int
baz (int x, int y)
{
  return (x >= 0) != (y >= 0);
}

int
qux (int x, int y)
{
  return (x >= 0) == (y <= 0);
}

which we don't optimize ATM is equivalent to:
#define I (-__INT_MAX__ - 1)

int
foo (int x, int y)
{
  return (x & I) != (y & I);
}

int
bar (int x, int y)
{
  return (x & I) == (y & I);
}

int
baz (int x, int y)
{
  return (~x & I) != (~y & I);
}

int
qux (int x, int y)
{
  return (~x & I) == (~y & I);
}

int
quux (int x, int y)
{
  return ((x & I) ^ I) != ((y & I) ^ I);
}

int
corge (int x, int y)
{
  return ((x & I) ^ I) == ((~y & I) ^ I);
}
which we do (already in *.original dump), but then
#define I (-__INT_MAX__ - 1)

int
foo (int x, int y)
{
  int s = (x & I);
  int t = (y & I);
  return s != t;
}

int
bar (int x, int y)
{
  int s = (x & I);
  int t = (y & I);
  return s == t;
}

int
baz (int x, int y)
{
  int s = (~x & I);
  int t = (~y & I);
  return s != t;
}

int
qux (int x, int y)
{
  int s = (~x & I);
  int t = (~y & I);
  return s == t;
}

int
quux (int x, int y)
{
  int s = ((x & I) ^ I);
  int t = ((y & I) ^ I);
  return s != t;
}

int
corge (int x, int y)
{
  int s = ((x & I) ^ I);
  int t = ((~y & I) ^ I);
  return s == t;
}
is not again, which means we optimize this somewhere in fold-const.c or where
and don't in match.pd.

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2020-04-23 13:15 ` jakub at gcc dot gnu.org
@ 2020-04-23 15:44 ` jakub at gcc dot gnu.org
  2020-04-23 17:24 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-04-23 15:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
In fold-const.c this is optimized by
fold_binary case EQ_EXPR: case NE_EXPR:
      /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
Of course, the (x op 0) op2 (y op 0) for op < or >= and op2 == or != could be
added to the same spot, but is there any reason why the above mentioned
optimization shouldn't be moved into match.pd (won't it be even much simpler
there where it can just use :c 3 times)?

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2020-04-23 15:44 ` jakub at gcc dot gnu.org
@ 2020-04-23 17:24 ` jakub at gcc dot gnu.org
  2020-05-04  8:59 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-04-23 17:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
--- gcc/fold-const.c.jj 2020-03-31 11:06:14.063512214 +0200
+++ gcc/fold-const.c    2020-04-23 18:39:15.399738420 +0200
@@ -11631,50 +11631,6 @@ fold_binary_loc (location_t loc, enum tr
          return omit_one_operand_loc (loc, type, res, arg0);
        }

-      /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
-      if (TREE_CODE (arg0) == BIT_AND_EXPR
-         && TREE_CODE (arg1) == BIT_AND_EXPR)
-       {
-         tree arg00 = TREE_OPERAND (arg0, 0);
-         tree arg01 = TREE_OPERAND (arg0, 1);
-         tree arg10 = TREE_OPERAND (arg1, 0);
-         tree arg11 = TREE_OPERAND (arg1, 1);
-         tree itype = TREE_TYPE (arg0);
-
-         if (operand_equal_p (arg01, arg11, 0))
-           {
-             tem = fold_convert_loc (loc, itype, arg10);
-             tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
-             tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
-             return fold_build2_loc (loc, code, type, tem,
-                                     build_zero_cst (itype));
-           }
-         if (operand_equal_p (arg01, arg10, 0))
-           {
-             tem = fold_convert_loc (loc, itype, arg11);
-             tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
-             tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
-             return fold_build2_loc (loc, code, type, tem,
-                                     build_zero_cst (itype));
-           }
-         if (operand_equal_p (arg00, arg11, 0))
-           {
-             tem = fold_convert_loc (loc, itype, arg10);
-             tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
-             tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
-             return fold_build2_loc (loc, code, type, tem,
-                                     build_zero_cst (itype));
-           }
-         if (operand_equal_p (arg00, arg10, 0))
-           {
-             tem = fold_convert_loc (loc, itype, arg11);
-             tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
-             tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
-             return fold_build2_loc (loc, code, type, tem,
-                                     build_zero_cst (itype));
-           }
-       }
-
       if (TREE_CODE (arg0) == BIT_XOR_EXPR
          && TREE_CODE (arg1) == BIT_XOR_EXPR)
        {
--- gcc/match.pd.jj     2020-03-11 18:33:50.341663648 +0100
+++ gcc/match.pd        2020-04-23 19:17:37.954208051 +0200
@@ -4335,7 +4335,12 @@ (define_operator_list COND_TERNARY
  (simplify
   (cmp (convert? addr@0) integer_zerop)
   (if (tree_single_nonzero_warnv_p (@0, NULL))
-   { constant_boolean_node (cmp == NE_EXPR, type); })))
+   { constant_boolean_node (cmp == NE_EXPR, type); }))
+
+ /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
+ (simplify
+  (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
+  (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))

 /* If we have (A & C) == C where C is a power of 2, convert this into
    (A & C) != 0.  Similarly for NE_EXPR.  */

is untested part without testsuite coverage that optimizes the above #c2 third
test the same as #c2 second test.  Unsure about :s.

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2020-04-23 17:24 ` jakub at gcc dot gnu.org
@ 2020-05-04  8:59 ` cvs-commit at gcc dot gnu.org
  2020-05-04  9:01 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-05-04  8:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:73a8043481d24ac86ce8d19459276181dfd9c858

commit r11-34-g73a8043481d24ac86ce8d19459276181dfd9c858
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon May 4 10:57:46 2020 +0200

    match.pd: Move (X & C) eqne (Y & C) -> -> (X ^ Y) & C eqne 0 opt to
match.pd [PR94718]

    This patch moves this optimization from fold-const.c to match.pd where it
    is actually much shorter to do and lets optimize even code not seen
together
    in a single expression in the source, as the first step towards fixing the
    PR.

    2020-05-04  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/94718
            * fold-const.c (fold_binary_loc): Move (X & C) eqne (Y & C)
            -> (X ^ Y) & C eqne 0 optimization to ...
            * match.pd ((X & C) op (Y & C) into (X ^ Y) & C op 0): ... here.

            * gcc.dg/tree-ssa/pr94718-1.c: New test.
            * gcc.dg/tree-ssa/pr94718-2.c: New test.

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2020-05-04  8:59 ` cvs-commit at gcc dot gnu.org
@ 2020-05-04  9:01 ` cvs-commit at gcc dot gnu.org
  2020-05-04  9:04 ` cvs-commit at gcc dot gnu.org
  2020-05-04  9:05 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-05-04  9:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:496f4f884716ae061f771a62e44868a32dbd502f

commit r11-35-g496f4f884716ae061f771a62e44868a32dbd502f
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon May 4 11:01:08 2020 +0200

    match.pd: Decrease number of nop conversions around bitwise ops [PR94718]

    On the following testcase, there are in *.optimized dump 14 nop conversions
    (from signed to unsigned and back), while this patch decreases that number
    to just 4; for bitwise ops it really doesn't matter if they are performed
in
    signed or unsigned, so the patch (in GIMPLE only, there are some comments
    about it being undesirable during GENERIC earlier), if it sees both
    bitop operands nop converted from the same types performs the bitop in
their
    non-converted type and converts the result (i.e. 2 conversions into 1),
    similarly, if a bitop has one operand nop converted from something, the
    other not and the result is converted back to the type of the nop converted
    operand before conversion, it is possible to replace those 2 conversions
    with just a single conversion of the other operand.

    2020-05-04  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/94718
            * match.pd (bitop (convert @0) (convert? @1)): For GIMPLE, if we
can,
            replace two nop conversions on bit_{and,ior,xor} argument
            and result with just one conversion on the result or another
argument.

            * gcc.dg/tree-ssa/pr94718-3.c: New test.

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2020-05-04  9:01 ` cvs-commit at gcc dot gnu.org
@ 2020-05-04  9:04 ` cvs-commit at gcc dot gnu.org
  2020-05-04  9:05 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-05-04  9:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:6b5c7ee0df6b87780f2fd6f2c5759a04e6eed1fe

commit r11-36-g6b5c7ee0df6b87780f2fd6f2c5759a04e6eed1fe
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon May 4 11:03:32 2020 +0200

    match.pd: Optimize (x < 0) != (y < 0) into (x ^ y) < 0 [PR94718]

    The following patch (on top of the two other PR94718 patches) performs the
    actual optimization requested in the PR.

    2020-05-04  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/94718
            * match.pd ((X < 0) != (Y < 0) into (X ^ Y) < 0): New
simplification.

            * gcc.dg/tree-ssa/pr94718-4.c: New test.
            * gcc.dg/tree-ssa/pr94718-5.c: New test.

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

* [Bug tree-optimization/94718] Failure to optimize opposite signs check
  2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
                   ` (7 preceding siblings ...)
  2020-05-04  9:04 ` cvs-commit at gcc dot gnu.org
@ 2020-05-04  9:05 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-05-04  9:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
   Target Milestone|---                         |11.0
             Status|NEW                         |RESOLVED

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for GCC 11.

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

end of thread, other threads:[~2020-05-04  9:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-22 16:51 [Bug tree-optimization/94718] New: Failure to optimize opposite signs check gabravier at gmail dot com
2020-04-22 16:53 ` [Bug tree-optimization/94718] " pinskia at gcc dot gnu.org
2020-04-23  5:57 ` rguenth at gcc dot gnu.org
2020-04-23 13:15 ` jakub at gcc dot gnu.org
2020-04-23 15:44 ` jakub at gcc dot gnu.org
2020-04-23 17:24 ` jakub at gcc dot gnu.org
2020-05-04  8:59 ` cvs-commit at gcc dot gnu.org
2020-05-04  9:01 ` cvs-commit at gcc dot gnu.org
2020-05-04  9:04 ` cvs-commit at gcc dot gnu.org
2020-05-04  9:05 ` 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).