public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination.
@ 2021-12-03  1:37 navidr at gcc dot gnu.org
  2021-12-06  8:18 ` [Bug tree-optimization/103536] " rguenth at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: navidr at gcc dot gnu.org @ 2021-12-03  1:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103536
           Summary: Suboptimal codegen for && and || combination.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: navidr at gcc dot gnu.org
  Target Milestone: ---

We do have suboptimal codegen for this pattern:

https://compiler-explorer.com/z/dszaK49WT

bool
src_1 (bool a, bool b)
{
    return (a || b) && (a && b);
}

bool
src_2 (bool a, bool b) // no problem here
{
    return (a && b) && (a || b);
}

bool
tgt (bool a, bool b) // what we want
{
    return (a && b);
}


In the case of src_2, it is the "ethread" pass which will remove the extra
basicblock. But in case of src_1 since the operation is spread over multiple
basicblocks, the match.pd is not able to detect and solve this.

1) In limited situations, it is possible to detect pattern in GENERIC and fix
this:

As proof of concept, I tried with something like this and was able to fix this
in GENERIC:
(for firstAnd (truth_and truth_andif)
 (for secondAnd (truth_and truth_andif)
  (for firstOr (truth_or truth_orif)
   (simplify
    (firstAnd (firstOr @0 @1) (secondAnd truth_valued_p@0 truth_valued_p@1))
    (truth_and @0 @1)))))

Because of GENERIC limitation, this is not going to work on something like
this:
bool src_12 (bool a, bool b)
{
    int z = a || b;
    return z && (a && b);
}



2) A better approach which I haven't looked at, is not to spread this over
multiple basic blocks.

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

* [Bug tree-optimization/103536] Suboptimal codegen for && and || combination.
  2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
@ 2021-12-06  8:18 ` rguenth at gcc dot gnu.org
  2022-03-16  8:37 ` coenraad at wish dot org.za
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-12-06  8:18 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
            Version|unknown                     |12.0
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-12-06

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
In principle the ifcombine pass might be able to do this since it combines CFG
and IL via match.pd.  If threading makes a mess out of this earlier that's of
course a problem.  Maybe we need to move ifcombine early.

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

* [Bug tree-optimization/103536] Suboptimal codegen for && and || combination.
  2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
  2021-12-06  8:18 ` [Bug tree-optimization/103536] " rguenth at gcc dot gnu.org
@ 2022-03-16  8:37 ` coenraad at wish dot org.za
  2023-08-23  3:29 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: coenraad at wish dot org.za @ 2022-03-16  8:37 UTC (permalink / raw)
  To: gcc-bugs

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

dagelf <coenraad at wish dot org.za> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |coenraad at wish dot org.za

--- Comment #2 from dagelf <coenraad at wish dot org.za> ---
&& does not mean logical AND in gcc, it's a label. 

You should just be using &

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

* [Bug tree-optimization/103536] Suboptimal codegen for && and || combination.
  2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
  2021-12-06  8:18 ` [Bug tree-optimization/103536] " rguenth at gcc dot gnu.org
  2022-03-16  8:37 ` coenraad at wish dot org.za
@ 2023-08-23  3:29 ` pinskia at gcc dot gnu.org
  2023-09-03  4:04 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-23  3:29 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
After r14-1597-g64d90d06d2db , we are just missing:
```
int
src_int (int a, int b)
{
    return (a | b) & (a & b);
}
```
into:
```
int
src_int_ (int a, int b)
{
    return (a & b);
}
```
Which is detected at the RTL level though.

This is 

So mine:
```
(for bitop (bit_and bit_ior)
     rbitop (bit_ior bit_and)
  /* (x | y) & (x & z) -> (x & z) */
  /* (x & y) | (x | z) -> (x | z) */
 (simplify
  (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
  @3)
```

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

* [Bug tree-optimization/103536] Suboptimal codegen for && and || combination.
  2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-08-23  3:29 ` pinskia at gcc dot gnu.org
@ 2023-09-03  4:04 ` pinskia at gcc dot gnu.org
  2023-09-03 20:54 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-03  4:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note with `--param=logical-op-non-short-circuit=0` this is optimized earlier
due to getting many bb instead of
```
  _1 = a_4(D) | b_5(D);
  if (_1 != 0)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  _2 = a_4(D) & b_5(D);
  if (_2 != 0)
    goto <bb 5>; [INV]
  else
    goto <bb 4>; [INV]
```

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

* [Bug tree-optimization/103536] Suboptimal codegen for && and || combination.
  2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-09-03  4:04 ` pinskia at gcc dot gnu.org
@ 2023-09-03 20:54 ` pinskia at gcc dot gnu.org
  2023-09-05 21:16 ` cvs-commit at gcc dot gnu.org
  2023-09-05 21:17 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-03 20:54 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|                            |patch

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

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

* [Bug tree-optimization/103536] Suboptimal codegen for && and || combination.
  2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-09-03 20:54 ` pinskia at gcc dot gnu.org
@ 2023-09-05 21:16 ` cvs-commit at gcc dot gnu.org
  2023-09-05 21:17 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-09-05 21:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- 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:8e995e84233661a1a246807a66cc84003426b1df

commit r14-3722-g8e995e84233661a1a246807a66cc84003426b1df
Author: Andrew Pinski <apinski@marvell.com>
Date:   Sun Sep 3 10:17:29 2023 -0700

    MATCH: Add pattern for `(x | y) & (x & z)`

    Like the pattern already there for `(x | y) & x`,
    this adds a simple pattern to optimize `(x | y) & (x & z)`
    to just `x & z`.

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

    gcc/ChangeLog:

            PR tree-optimization/103536
            * match.pd (`(x | y) & (x & z)`,
            `(x & y) | (x | z)`): New patterns.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/103536
            * gcc.dg/tree-ssa/andor-6.c: New test.
            * gcc.dg/tree-ssa/andor-bool-1.c: New test.

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

* [Bug tree-optimization/103536] Suboptimal codegen for && and || combination.
  2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-09-05 21:16 ` cvs-commit at gcc dot gnu.org
@ 2023-09-05 21:17 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-05 21:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |14.0

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.

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

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

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-03  1:37 [Bug tree-optimization/103536] New: Suboptimal codegen for && and || combination navidr at gcc dot gnu.org
2021-12-06  8:18 ` [Bug tree-optimization/103536] " rguenth at gcc dot gnu.org
2022-03-16  8:37 ` coenraad at wish dot org.za
2023-08-23  3:29 ` pinskia at gcc dot gnu.org
2023-09-03  4:04 ` pinskia at gcc dot gnu.org
2023-09-03 20:54 ` pinskia at gcc dot gnu.org
2023-09-05 21:16 ` cvs-commit at gcc dot gnu.org
2023-09-05 21: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).