public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0
@ 2023-09-16  2:41 pinskia at gcc dot gnu.org
  2023-09-16  2:42 ` [Bug tree-optimization/111431] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-16  2:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111431
           Summary: a & (a == 0) is not optimized to 0
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization, xfail
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
int
foo (int a)
{
  int b = !a;
  return (a & b);
}
```
The general rule is:
(a == CST) & a
transform into:
(CST & 1) ? (a == CST) : 0

So
(simplify
 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) @0)
 (if ((wi::to_wide (@1) & 1) != 0)
  @2
  { build_zero_cst (type); }))

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
@ 2023-09-16  2:42 ` pinskia at gcc dot gnu.org
  2023-09-16  2:53 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-16  2:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The following testcases are xfailed because of this missing optimization:
gcc.dg/binop-notand1a.c
gcc.dg/binop-notand4a.c

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
  2023-09-16  2:42 ` [Bug tree-optimization/111431] " pinskia at gcc dot gnu.org
@ 2023-09-16  2:53 ` pinskia at gcc dot gnu.org
  2023-09-16  3:27 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-16  2:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The pattern should be:

(simplify
 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
 (if ((wi::to_wide (@1) & 1) != 0)
  @2
  { build_zero_cst (type); }))

Since 0/1 of the eq, the convert on the @0 inside the bit_and does not matter.

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
  2023-09-16  2:42 ` [Bug tree-optimization/111431] " pinskia at gcc dot gnu.org
  2023-09-16  2:53 ` pinskia at gcc dot gnu.org
@ 2023-09-16  3:27 ` pinskia at gcc dot gnu.org
  2023-09-16  3:33 ` 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-09-16  3:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note this is an extension of the pattern:
/* X & !X -> 0.  */
(simplify
 (bit_and:c @0 (logical_inverted_value @0))

Which was moved to match via r5-4683-g5609420fbab5ca .

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-09-16  3:27 ` pinskia at gcc dot gnu.org
@ 2023-09-16  3:33 ` pinskia at gcc dot gnu.org
  2023-09-16  3:39 ` 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-09-16  3:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note I found this while looking into the patch that was originally posted for
PR 57755 which removed the xfail for binop-notand1a.c/binop-notand4a.c but
found this was a better way of fixing this rather than that old patch. Plus
there can be more benifit out of it too.

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-09-16  3:33 ` pinskia at gcc dot gnu.org
@ 2023-09-16  3:39 ` pinskia at gcc dot gnu.org
  2023-09-16 16:00 ` 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-09-16  3:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55907
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55907&action=edit
Patch which I am testing

I should note that LLVM does NOT do this simple pattern matching either.

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-09-16  3:39 ` pinskia at gcc dot gnu.org
@ 2023-09-16 16:00 ` pinskia at gcc dot gnu.org
  2023-09-18 16:13 ` cvs-commit at gcc dot gnu.org
  2023-09-18 16:17 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-16 16:00 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-09-16 16:00 ` pinskia at gcc dot gnu.org
@ 2023-09-18 16:13 ` cvs-commit at gcc dot gnu.org
  2023-09-18 16:17 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-09-18 16:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- 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:0fb828af75732d39c644fd145c29d41ca14b8cb1

commit r14-4105-g0fb828af75732d39c644fd145c29d41ca14b8cb1
Author: Andrew Pinski <apinski@marvell.com>
Date:   Sat Sep 16 03:27:26 2023 +0000

    MATCH: Add simplifications of `(a == CST) & a`

    `(a == CST) & a` can be either simplified to simplying `a == CST`
    or 0 depending on the first bit of the CST.
    This is an extension of the already pattern of `X & !X` and allows
    us to remove the 2 xfails on gcc.dg/binop-notand1a.c and
gcc.dg/binop-notand4a.c.

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

            PR tree-optimization/111431

    gcc/ChangeLog:

            * match.pd (`(a == CST) & a`): New pattern.

    gcc/testsuite/ChangeLog:

            * gcc.dg/binop-notand1a.c: Remove xfail.
            * gcc.dg/binop-notand4a.c: Likewise.
            * gcc.c-torture/execute/pr111431-1.c: New test.
            * gcc.dg/binop-andeq1.c: New test.
            * gcc.dg/binop-andeq2.c: New test.
            * gcc.dg/binop-notand7.c: New test.
            * gcc.dg/binop-notand7a.c: New test.

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

* [Bug tree-optimization/111431] a & (a == 0) is not optimized to 0
  2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2023-09-18 16:13 ` cvs-commit at gcc dot gnu.org
@ 2023-09-18 16:17 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-18 16:17 UTC (permalink / raw)
  To: gcc-bugs

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

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.

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

end of thread, other threads:[~2023-09-18 16:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-16  2:41 [Bug tree-optimization/111431] New: a & (a == 0) is not optimized to 0 pinskia at gcc dot gnu.org
2023-09-16  2:42 ` [Bug tree-optimization/111431] " pinskia at gcc dot gnu.org
2023-09-16  2:53 ` pinskia at gcc dot gnu.org
2023-09-16  3:27 ` pinskia at gcc dot gnu.org
2023-09-16  3:33 ` pinskia at gcc dot gnu.org
2023-09-16  3:39 ` pinskia at gcc dot gnu.org
2023-09-16 16:00 ` pinskia at gcc dot gnu.org
2023-09-18 16:13 ` cvs-commit at gcc dot gnu.org
2023-09-18 16:17 ` 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).