public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases
@ 2023-06-16 23:22 pinskia at gcc dot gnu.org
  2023-06-16 23:57 ` [Bug tree-optimization/110293] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-16 23:22 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110293
           Summary: Some `A CMP (A NEEQ 0)` is not simplified in some
                    cases
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

The full set is:
`x != (typeof x)(x == 0)` is always true (PR 110278)
`x == (typeof x)(x == 0)` is always false.
`x == (typeof x)(x != 0)` is `(unsigned_type)x <= 1`
`x != (typeof x)(x != 0)` is `(unsigned_type)x > 1`


`uns < (typeof uns)(uns != 0)` is always false (PR 110278)
`uns >= (typeof uns)(uns != 0)` is always true
`uns <= (typeof uns)(uns != 0)` is `uns <= 1`
`uns > (typeof uns)(uns != 0)` is `uns > 1`

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
@ 2023-06-16 23:57 ` pinskia at gcc dot gnu.org
  2023-06-17  1:58 ` pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-16 23:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Here are the signed versions of </<=/>/>=:
`s <  (s != 0)` is `s <  0`
`s <= (s != 0)` is `s <= 1`
`s >  (s != 0)` is `s >  1`
`s >= (s != 0)` is `s >= 0`

`s <  (s == 0)` is `s <= 0`
`s <= (s == 0)` is `s <= 0`
`s >  (s == 0)` is `s >  0`
`s >= (s == 0)` is `s >  0`


Here are the unsigned versions with `uns == 0` since I had missed those before:

`uns <  (uns == 0)` is `uns == 0`
`uns <= (uns == 0)` is `uns == 0`
`uns >= (uns == 0)` is `uns != 0`
`uns >  (uns == 0)` is `uns != 0`

I noticed LLVM does not catch these either.

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
  2023-06-16 23:57 ` [Bug tree-optimization/110293] " pinskia at gcc dot gnu.org
@ 2023-06-17  1:58 ` pinskia at gcc dot gnu.org
  2023-07-11 23:13 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-17  1:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Some more generally for the ==/!= cases for CSTs:
x == (x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x != 0)
x == (x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x != 1)
x != (x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0)
x != (x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1)

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
  2023-06-16 23:57 ` [Bug tree-optimization/110293] " pinskia at gcc dot gnu.org
  2023-06-17  1:58 ` pinskia at gcc dot gnu.org
@ 2023-07-11 23:13 ` pinskia at gcc dot gnu.org
  2023-07-12  6:29 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-11 23:13 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
MIne, going to fix this ...

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-07-11 23:13 ` pinskia at gcc dot gnu.org
@ 2023-07-12  6:29 ` pinskia at gcc dot gnu.org
  2023-07-12 17:31 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-12  6:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55527
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55527&action=edit
the eq/ne based functions

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-07-12  6:29 ` pinskia at gcc dot gnu.org
@ 2023-07-12 17:31 ` pinskia at gcc dot gnu.org
  2023-07-13 14:55 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-12 17:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Part 1 of the patch to fix this:
https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624293.html

This weekend I will handle the outer != NE/EQ cases.

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-07-12 17:31 ` pinskia at gcc dot gnu.org
@ 2023-07-13 14:55 ` cvs-commit at gcc dot gnu.org
  2023-07-13 14:56 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-13 14:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 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:285c9d042e90a7425b37697edc9ec93a1b03b486

commit r14-2501-g285c9d042e90a7425b37697edc9ec93a1b03b486
Author: Andrew Pinski <apinski@marvell.com>
Date:   Wed Jul 12 00:33:14 2023 -0700

    Fix part of PR 110293: `A NEEQ (A NEEQ CST)` part

    This fixes part of PR 110293, for the outer comparison case
    being `!=` or `==`.  In turn PR 110539 is able to be optimized
    again as the if statement for `(a&1) == ((a & 1) != 0)` gets optimized
    to `false` early enough to allow FRE/DOM to do a CSE for memory store/load.

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

    gcc/ChangeLog:

            PR tree-optimization/110293
            PR tree-optimization/110539
            * match.pd: Expand the `x != (typeof x)(x == 0)`
            pattern to handle where the inner and outer comparsions
            are either `!=` or `==` and handle other constants
            than 0.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/pr110293-1.c: New test.
            * gcc.dg/tree-ssa/pr110539-1.c: New test.
            * gcc.dg/tree-ssa/pr110539-2.c: New test.
            * gcc.dg/tree-ssa/pr110539-3.c: New test.
            * gcc.dg/tree-ssa/pr110539-4.c: New test.

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-07-13 14:55 ` cvs-commit at gcc dot gnu.org
@ 2023-07-13 14:56 ` pinskia at gcc dot gnu.org
  2023-09-12 22:00 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-13 14:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Half of this is fixed now.

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2023-07-13 14:56 ` pinskia at gcc dot gnu.org
@ 2023-09-12 22:00 ` pinskia at gcc dot gnu.org
  2023-09-12 23:42 ` pinskia at gcc dot gnu.org
  2024-03-09  5:55 ` pinskia at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-12 22:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55889
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55889&action=edit
Runtime test

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2023-09-12 22:00 ` pinskia at gcc dot gnu.org
@ 2023-09-12 23:42 ` pinskia at gcc dot gnu.org
  2024-03-09  5:55 ` pinskia at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-12 23:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Actually here is the rest for the non-zero comparisons.

Note for the below case, s can be swapped around with unsigned and the CST
comparisons become unsigned comparisons too.

s < (s == CST) ->  CST == 0 ? s <= 0 : CST < 0
s >= (s == CST) -> CST == 0 ? s > 0  : CST > 0
s > (s == CST) ->  CST == 0 ? s <= 0 : CST > 1
s <= (s == CST) -> CST == 0 ? s > 0  : CST <= 1

s <  (s != CST) -> CST == 0 ? s < 0  : (CST > 0 ? s < 1 : (s < 1 & s != CST))
s >= (s != CST) -> CST == 0 ? s >= 0 : (CST == 1 ? s > 1 : (CST > 1 ? s != CST
& s >= 1 : s >= 1))
s >  (s != CST) -> CST == 0 ? s <= 1 : 
s <= (s != CST) -> CST == 0 ? s >  1 :

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

* [Bug tree-optimization/110293] Some `A CMP (A NEEQ 0)` is not simplified in some cases
  2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2023-09-12 23:42 ` pinskia at gcc dot gnu.org
@ 2024-03-09  5:55 ` pinskia at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-09  5:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Here is some more optimizations where the inner CMP is not EQ/NE:
signed:
x == (x <= CST) -> CST == 0 ? false : CST < 0 ? x == 0 : x == 1
x == (x  < CST) -> CST == 1 ? false : CST <= 0 ? x == 0 : x == 1

unsigned:
x == (x u<= CST) -> CST == 0 ? false : x == 1 (note 0 should already be
handled)
x == (x u<= CST) -> CST == 0 ? x == 0 : CST == 1 ? false : x == 1 (note 0/1
should be handled already)

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

end of thread, other threads:[~2024-03-09  5:55 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-16 23:22 [Bug tree-optimization/110293] New: Some `A CMP (A NEEQ 0)` is not simplified in some cases pinskia at gcc dot gnu.org
2023-06-16 23:57 ` [Bug tree-optimization/110293] " pinskia at gcc dot gnu.org
2023-06-17  1:58 ` pinskia at gcc dot gnu.org
2023-07-11 23:13 ` pinskia at gcc dot gnu.org
2023-07-12  6:29 ` pinskia at gcc dot gnu.org
2023-07-12 17:31 ` pinskia at gcc dot gnu.org
2023-07-13 14:55 ` cvs-commit at gcc dot gnu.org
2023-07-13 14:56 ` pinskia at gcc dot gnu.org
2023-09-12 22:00 ` pinskia at gcc dot gnu.org
2023-09-12 23:42 ` pinskia at gcc dot gnu.org
2024-03-09  5:55 ` 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).