public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/103354] New: missed optimization with & and | and compares
@ 2021-11-22  2:17 pinskia at gcc dot gnu.org
  2021-11-22  2:23 ` [Bug tree-optimization/103354] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-22  2:17 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103354
           Summary: missed optimization with & and | and compares
           Product: gcc
           Version: 12.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: ---

Take:
int h(int a, int b, int c, int d)
{
  return (c & -(a==b)) | (d & -(a!=b));
}

---- CUT ---
We currently don't optimize the above down to just:
int h(int a, int b, int c, int d)
{
  return (a==b) ? c : d;
}

This is from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92342#c4

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

* [Bug tree-optimization/103354] missed optimization with & and | and compares
  2021-11-22  2:17 [Bug tree-optimization/103354] New: missed optimization with & and | and compares pinskia at gcc dot gnu.org
@ 2021-11-22  2:23 ` pinskia at gcc dot gnu.org
  2023-03-13  8:53 ` roger at nextmovesoftware dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-22  2:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2021-11-22
           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> ---
After my patch for PR 92342, we get:


  _4 = a_9(D) == b_10(D) ? c_11(D) : 0;
  _8 = a_9(D) != b_10(D) ? d_12(D) : 0;
  _13 = _4 | _8;

A few patterns are needed here really:

One pattern simple is:
(simplify
 (bit_ior:c
  (cond @0 @1 integer_zero_p)
  (cond @0 integer_zero_p @2))
 (cond @0 @1 @2))

But the next one is harder and requires a double for loop. I will handle it
soon.

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

* [Bug tree-optimization/103354] missed optimization with & and | and compares
  2021-11-22  2:17 [Bug tree-optimization/103354] New: missed optimization with & and | and compares pinskia at gcc dot gnu.org
  2021-11-22  2:23 ` [Bug tree-optimization/103354] " pinskia at gcc dot gnu.org
@ 2023-03-13  8:53 ` roger at nextmovesoftware dot com
  2023-03-13 17:52 ` pinskia at gcc dot gnu.org
  2023-03-13 17:53 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: roger at nextmovesoftware dot com @ 2023-03-13  8:53 UTC (permalink / raw)
  To: gcc-bugs

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

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |roger at nextmovesoftware dot com

--- Comment #2 from Roger Sayle <roger at nextmovesoftware dot com> ---
Hi Andrew,
I believe this missed-optimization is now implemented, and GCC generates the
same (or equivalent) code for all of:

int h1(int a, int b, int c, int d) {
  return (c & -(a==b)) | (d & -(a!=b));
}

int h2(int a, int b, int c, int d) {
  return (a==b) ? c : d;
}

int h3(int a, int b, int c, int d) {
  int t1 = a==b ? c : 0;
  int t2 = a==b ? 0 : d;
  return t1 | t2;
}

int h4(int a, int b, int c, int d) {
  int t1 = a==b ? c : 0;
  int t2 = a!=b ? d : 0;
  return t1 | t2;
}

If you agree we can close this PR.  I don't think this needs a new test case in
the testsuite, but I'll leave that up to you.

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

* [Bug tree-optimization/103354] missed optimization with & and | and compares
  2021-11-22  2:17 [Bug tree-optimization/103354] New: missed optimization with & and | and compares pinskia at gcc dot gnu.org
  2021-11-22  2:23 ` [Bug tree-optimization/103354] " pinskia at gcc dot gnu.org
  2023-03-13  8:53 ` roger at nextmovesoftware dot com
@ 2023-03-13 17:52 ` pinskia at gcc dot gnu.org
  2023-03-13 17:53 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-13 17:52 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
h in comment #0 was fixed by r13-4620-g4d9db4bdd458a4 which has a testcase for
this: gcc.target/aarch64/if-compare_1.c (not a generic one though but I think
that is ok for now) .

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

* [Bug tree-optimization/103354] missed optimization with & and | and compares
  2021-11-22  2:17 [Bug tree-optimization/103354] New: missed optimization with & and | and compares pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-03-13 17:52 ` pinskia at gcc dot gnu.org
@ 2023-03-13 17:53 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-13 17:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0

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

end of thread, other threads:[~2023-03-13 17:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-22  2:17 [Bug tree-optimization/103354] New: missed optimization with & and | and compares pinskia at gcc dot gnu.org
2021-11-22  2:23 ` [Bug tree-optimization/103354] " pinskia at gcc dot gnu.org
2023-03-13  8:53 ` roger at nextmovesoftware dot com
2023-03-13 17:52 ` pinskia at gcc dot gnu.org
2023-03-13 17:53 ` 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).