public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
@ 2022-06-14 21:37 gabravier at gmail dot com
  2022-06-14 21:39 ` [Bug tree-optimization/105983] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: gabravier at gmail dot com @ 2022-06-14 21:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105983
           Summary: Failure to optimize (b != 0) && (a >= b) as well as
                    the same pattern with binary and
           Product: gcc
           Version: 13.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(unsigned a, unsigned b)
{
    return (b != 0) && (a >= b);
}

This can be optimized to `return (b != 0) & (a >= b);`, which is itself
optimized to `return (b - 1) > a;`. GCC outputs code equivalent to `return (b
!= 0) & (a >= b);` (at least on x86) whereas if that code is compiled it would
output `return (b - 1) > a;`, while LLVM has no trouble directly outputting the
optimal code.

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
@ 2022-06-14 21:39 ` pinskia at gcc dot gnu.org
  2022-06-14 21:41 ` pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-14 21:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
  2022-06-14 21:39 ` [Bug tree-optimization/105983] " pinskia at gcc dot gnu.org
@ 2022-06-14 21:41 ` pinskia at gcc dot gnu.org
  2022-06-14 21:45 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-14 21:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
aarch64 GCC is able to compile it to:
f(unsigned int, unsigned int):
        cmp     w1, 0
        ccmp    w1, w0, 2, ne
        cset    w0, ls
        ret

While aarch64 LLVM does:
        sub     w8, w1, #1
        cmp     w8, w0
        cset    w0, lo
        ret

depending on the pipeline, they might be the same or the ccmp might be better
slightly.

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
  2022-06-14 21:39 ` [Bug tree-optimization/105983] " pinskia at gcc dot gnu.org
  2022-06-14 21:41 ` pinskia at gcc dot gnu.org
@ 2022-06-14 21:45 ` pinskia at gcc dot gnu.org
  2022-06-15 12:26 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-14 21:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
             Blocks|                            |19987
   Last reconfirmed|                            |2022-06-14
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, the issue is GCC does not even handle:
bool f(unsigned a, unsigned b)
{
    bool t = (b != 0);
    bool t1 = (a >= b);
    return t & t1;
}

I suspect this is a fold-const.cc which has not been moved over to match.pd
yet.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19987
[Bug 19987] [meta-bug] fold missing optimizations in general

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2022-06-14 21:45 ` pinskia at gcc dot gnu.org
@ 2022-06-15 12:26 ` jakub at gcc dot gnu.org
  2022-06-15 13:32 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-15 12:26 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
On generic, what opimizes this is:
/* y == XXX_MIN || x < y --> x <= y - 1 */
(simplify
 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
  (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))

/* y != XXX_MIN && x >= y --> x > y - 1 */
(simplify
 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
  (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
in match.pd when & is used instead of &&.

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2022-06-15 12:26 ` jakub at gcc dot gnu.org
@ 2022-06-15 13:32 ` jakub at gcc dot gnu.org
  2022-06-15 14:13 ` rearnsha at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-15 13:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
--- gcc/match.pd.jj     2022-06-15 12:52:04.640981511 +0200
+++ gcc/match.pd        2022-06-15 15:28:55.916225336 +0200
@@ -2379,14 +2379,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)

 /* y == XXX_MIN || x < y --> x <= y - 1 */
 (simplify
- (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
+ (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))

 /* y != XXX_MIN && x >= y --> x > y - 1 */
 (simplify
- (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
+ (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))

fixes this.

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2022-06-15 13:32 ` jakub at gcc dot gnu.org
@ 2022-06-15 14:13 ` rearnsha at gcc dot gnu.org
  2022-06-15 14:20 ` rearnsha at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2022-06-15 14:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Richard Earnshaw <rearnsha at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #4)
> --- gcc/match.pd.jj	2022-06-15 12:52:04.640981511 +0200
> +++ gcc/match.pd	2022-06-15 15:28:55.916225336 +0200
> @@ -2379,14 +2379,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  
>  /* y == XXX_MIN || x < y --> x <= y - 1 */
>  (simplify
> - (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
> + (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
>    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
>         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
>    (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
>  
>  /* y != XXX_MIN && x >= y --> x > y - 1 */
>  (simplify
> - (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
> + (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
>    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
>         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
>    (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
> 
> fixes this.

But doesn't that regress

bool f(unsigned a, unsigned b)
{
    return (b != 0) & (a >= b);
}

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2022-06-15 14:13 ` rearnsha at gcc dot gnu.org
@ 2022-06-15 14:20 ` rearnsha at gcc dot gnu.org
  2022-06-15 14:50 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2022-06-15 14:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Richard Earnshaw <rearnsha at gcc dot gnu.org> ---
(In reply to Richard Earnshaw from comment #5)
> (In reply to Jakub Jelinek from comment #4)
> > --- gcc/match.pd.jj	2022-06-15 12:52:04.640981511 +0200
> > +++ gcc/match.pd	2022-06-15 15:28:55.916225336 +0200
> > @@ -2379,14 +2379,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >  
> >  /* y == XXX_MIN || x < y --> x <= y - 1 */
> >  (simplify
> > - (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
> > + (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
> >    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
> >         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
> >    (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
> >  
> >  /* y != XXX_MIN && x >= y --> x > y - 1 */
> >  (simplify
> > - (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
> > + (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
> >    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
> >         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
> >    (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
> > 
> > fixes this.
> 
> But doesn't that regress
> 
> bool f(unsigned a, unsigned b)
> {
>     return (b != 0) & (a >= b);
> }

Ignore that - I'm confusing reports.

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2022-06-15 14:20 ` rearnsha at gcc dot gnu.org
@ 2022-06-15 14:50 ` jakub at gcc dot gnu.org
  2022-06-16 12:37 ` cvs-commit at gcc dot gnu.org
  2022-06-16 12:39 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-15 14:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 53146
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53146&action=edit
gcc13-pr105983.patch

Full untested patch.

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
                   ` (7 preceding siblings ...)
  2022-06-15 14:50 ` jakub at gcc dot gnu.org
@ 2022-06-16 12:37 ` cvs-commit at gcc dot gnu.org
  2022-06-16 12:39 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-16 12:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 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:9642d07c35f14b9917cd115e8a9f0210fbcdcf4f

commit r13-1134-g9642d07c35f14b9917cd115e8a9f0210fbcdcf4f
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Jun 16 14:37:06 2022 +0200

    match.pd: Improve y == MIN || x < y optimization [PR105983]

    On the following testcase, we only optimize bar where this optimization
    is performed at GENERIC folding time, but on GIMPLE it doesn't trigger
    anymore, as we actually don't see
      (bit_and (ne @1 min_value) (ge @0 @1))
    but
      (bit_and (ne @1 min_value) (le @1 @0))
    genmatch handles :c modifier not just on commutative operations, but
    also comparisons and in that case it means it swaps the comparison.

    2022-06-16  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/105983
            * match.pd (y == XXX_MIN || x < y -> x <= y - 1,
            y != XXX_MIN && x >= y -> x > y - 1): Use :cs instead of :s
            on non-equality comparisons.

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

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

* [Bug tree-optimization/105983] Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and
  2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
                   ` (8 preceding siblings ...)
  2022-06-16 12:37 ` cvs-commit at gcc dot gnu.org
@ 2022-06-16 12:39 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-16 12:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2022-06-16 12:39 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-14 21:37 [Bug tree-optimization/105983] New: Failure to optimize (b != 0) && (a >= b) as well as the same pattern with binary and gabravier at gmail dot com
2022-06-14 21:39 ` [Bug tree-optimization/105983] " pinskia at gcc dot gnu.org
2022-06-14 21:41 ` pinskia at gcc dot gnu.org
2022-06-14 21:45 ` pinskia at gcc dot gnu.org
2022-06-15 12:26 ` jakub at gcc dot gnu.org
2022-06-15 13:32 ` jakub at gcc dot gnu.org
2022-06-15 14:13 ` rearnsha at gcc dot gnu.org
2022-06-15 14:20 ` rearnsha at gcc dot gnu.org
2022-06-15 14:50 ` jakub at gcc dot gnu.org
2022-06-16 12:37 ` cvs-commit at gcc dot gnu.org
2022-06-16 12:39 ` 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).