public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction
@ 2020-08-19 10:07 gabravier at gmail dot com
  2021-04-26  1:02 ` [Bug tree-optimization/96702] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: gabravier at gmail dot com @ 2020-08-19 10:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96702
           Summary: Failure to optimize comparisons involving result of
                    subtraction
           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(int a, int b)
{
    int c = (a - b);
    return c >= a && c != 0;
}

This can be optimized to `return (b <= 0) && (a != b);`. This transformation is
done by LLVM, but not by GCC.

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

* [Bug tree-optimization/96702] Failure to optimize comparisons involving result of subtraction
  2020-08-19 10:07 [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction gabravier at gmail dot com
@ 2021-04-26  1:02 ` pinskia at gcc dot gnu.org
  2021-08-17  5:39 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-04-26  1:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/96702] Failure to optimize comparisons involving result of subtraction
  2020-08-19 10:07 [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction gabravier at gmail dot com
  2021-04-26  1:02 ` [Bug tree-optimization/96702] " pinskia at gcc dot gnu.org
@ 2021-08-17  5:39 ` pinskia at gcc dot gnu.org
  2023-09-04  3:32 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-17  5:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-08-17
     Ever confirmed|0                           |1

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

The problem is c is used twice.
we handle both optimizations just fine alone, just not when together.
There might be either a :s that maybe should not be there in match.pd or an
explict check for used more than once.

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

* [Bug tree-optimization/96702] Failure to optimize comparisons involving result of subtraction
  2020-08-19 10:07 [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction gabravier at gmail dot com
  2021-04-26  1:02 ` [Bug tree-optimization/96702] " pinskia at gcc dot gnu.org
  2021-08-17  5:39 ` pinskia at gcc dot gnu.org
@ 2023-09-04  3:32 ` pinskia at gcc dot gnu.org
  2023-09-04  3:33 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-04  3:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The comment says:
/* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
   However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
   expects the long form, so we restrict the transformation for now.  */
(for cmp (gt le)
 (simplify
  (cmp:c (minus@2 @0 @1) @0)
  (if (single_use (@2)

I am curious if that is still true ...

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

* [Bug tree-optimization/96702] Failure to optimize comparisons involving result of subtraction
  2020-08-19 10:07 [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2023-09-04  3:32 ` pinskia at gcc dot gnu.org
@ 2023-09-04  3:33 ` pinskia at gcc dot gnu.org
  2023-09-04  3:42 ` pinskia at gcc dot gnu.org
  2023-09-04  3:50 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-04  3:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
wait that is for unsigned types and we don't have an unsigned type here ..

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

* [Bug tree-optimization/96702] Failure to optimize comparisons involving result of subtraction
  2020-08-19 10:07 [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2023-09-04  3:33 ` pinskia at gcc dot gnu.org
@ 2023-09-04  3:42 ` pinskia at gcc dot gnu.org
  2023-09-04  3:50 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-04  3:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The pattern:
/* X - Y < X is the same as Y > 0 when there is no overflow.
   For equality, this is also true with wrapping overflow.  */
(for op (simple_comparison)
 (simplify
  (op:c @0 (minus@2 @0 @1))
  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
           || ((op == EQ_EXPR || op == NE_EXPR)
               && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
       && (CONSTANT_CLASS_P (@1) || single_use (@2)))
   (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))

https://gcc.gnu.org/pipermail/gcc-patches/2017-October/484606.html describes
why there was a single use but I wonder if ranger now does not cause the need
for the single_use ....

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

* [Bug tree-optimization/96702] Failure to optimize comparisons involving result of subtraction
  2020-08-19 10:07 [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2023-09-04  3:42 ` pinskia at gcc dot gnu.org
@ 2023-09-04  3:50 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-04  3:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #4)
> The pattern:
> /* X - Y < X is the same as Y > 0 when there is no overflow.
>    For equality, this is also true with wrapping overflow.  */
> (for op (simple_comparison)
>  (simplify
>   (op:c @0 (minus@2 @0 @1))
>   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
>        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
> 	   || ((op == EQ_EXPR || op == NE_EXPR)
> 	       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
>        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
>    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
> 
> https://gcc.gnu.org/pipermail/gcc-patches/2017-October/484606.html describes
> why there was a single use but I wonder if ranger now does not cause the
> need for the single_use ....

Looks like that issue was fixed in GCC 9 so maybe the 2 uses of single_use can
be removed ...

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

end of thread, other threads:[~2023-09-04  3:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-19 10:07 [Bug tree-optimization/96702] New: Failure to optimize comparisons involving result of subtraction gabravier at gmail dot com
2021-04-26  1:02 ` [Bug tree-optimization/96702] " pinskia at gcc dot gnu.org
2021-08-17  5:39 ` pinskia at gcc dot gnu.org
2023-09-04  3:32 ` pinskia at gcc dot gnu.org
2023-09-04  3:33 ` pinskia at gcc dot gnu.org
2023-09-04  3:42 ` pinskia at gcc dot gnu.org
2023-09-04  3:50 ` 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).