public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
@ 2024-04-30  6:18 pinskia at gcc dot gnu.org
  2024-04-30  6:18 ` [Bug tree-optimization/114894] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-30  6:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114894
           Summary: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
           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: ---

Take:
```
int fmul(int a, int b)
{
        int c = a * b;
        int d = a != 0;
        return c & -d;
}

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

```

These should just optimize to: `return a * b;` and `return a & b;`.

Currently we get:
```
  c_4 = a_2(D) * b_3(D);
  _1 = a_2(D) != 0;
  _6 = _1 ? c_4 : 0;
...

  c_4 = a_2(D) & b_3(D);
  _1 = a_2(D) != 0;
  _6 = _1 ? c_4 : 0;
```

For:
```

int fmul1(int a, int b)
{
        int c = a * b;
        if (a != 0)
          return c;
        return 0;
}


int fand1(int a, int b)
{
        int c = a & b;
        if (a != 0)
          return c;
        return 0;
}
```

It is not until phiopt4 we are able to remove the conditional.

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

* [Bug tree-optimization/114894] `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
  2024-04-30  6:18 [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b` pinskia at gcc dot gnu.org
@ 2024-04-30  6:18 ` pinskia at gcc dot gnu.org
  2024-04-30  6:23 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-30  6:18 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-04-30
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Mine. Found while moving value_replacement over to use match-and-simplify.

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

* [Bug tree-optimization/114894] `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
  2024-04-30  6:18 [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b` pinskia at gcc dot gnu.org
  2024-04-30  6:18 ` [Bug tree-optimization/114894] " pinskia at gcc dot gnu.org
@ 2024-04-30  6:23 ` pinskia at gcc dot gnu.org
  2024-04-30 18:04 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-30  6:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=59100

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
gcc.dg/tree-ssa/phi-opt-12.c is the testcase .

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

* [Bug tree-optimization/114894] `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
  2024-04-30  6:18 [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b` pinskia at gcc dot gnu.org
  2024-04-30  6:18 ` [Bug tree-optimization/114894] " pinskia at gcc dot gnu.org
  2024-04-30  6:23 ` pinskia at gcc dot gnu.org
@ 2024-04-30 18:04 ` pinskia at gcc dot gnu.org
  2024-04-30 18:18 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-30 18:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note we should catch this too:
```
int fmul2(unsigned a, int b)
{
        int c = a * b;
        int d = a != 0;
        return c & -d;
}

```

Which is more complex ...

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

* [Bug tree-optimization/114894] `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
  2024-04-30  6:18 [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b` pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-04-30 18:04 ` pinskia at gcc dot gnu.org
@ 2024-04-30 18:18 ` pinskia at gcc dot gnu.org
  2024-05-07 21:42 ` cvs-commit at gcc dot gnu.org
  2024-05-07 21:43 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-30 18:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
And divides should happen if we know if the divisor is non-zero:
eg:
```
int fdiv(int a, int b)
{
        if (b == 0) __builtin_unreachable();
        int c = a / b;
        int d = a != 0;
        return c & -d;
}

```

Phi-OPT value-replacement does handle it for ifs right now:
```
int fmul3(int a, int b)
{
        if (b == 0) __builtin_unreachable();
        int c = a / b;
        if (a == 0)  return 0;
        return c;
        int d = a != 0;
        return c & -d;
}
```

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

* [Bug tree-optimization/114894] `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
  2024-04-30  6:18 [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b` pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-04-30 18:18 ` pinskia at gcc dot gnu.org
@ 2024-05-07 21:42 ` cvs-commit at gcc dot gnu.org
  2024-05-07 21:43 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-07 21:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from GCC 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:e472527c7b45d23e8dfd0fb767a6e663b4bc136e

commit r15-307-ge472527c7b45d23e8dfd0fb767a6e663b4bc136e
Author: Andrew Pinski <quic_apinski@quicinc.com>
Date:   Tue Apr 30 14:45:26 2024 -0700

    MATCH: Add some more value_replacement simplifications (a != 0 ? expr : 0)
to match

    This adds a few more of what is currently done in phiopt's
value_replacement
    to match. I noticed this when I was hooking up phiopt's value_replacement
    code to use match and disabling the old code. But this can be done
    independently from the hooking up phiopt's value_replacement as phiopt
    is already hooked up for simplified versions already.

    /* a != 0 ? a / b : 0  -> a / b iff b is nonzero. */
    /* a != 0 ? a * b : 0 -> a * b */
    /* a != 0 ? a & b : 0 -> a & b */

    We prefer the `cond ? a : 0` forms to allow optimization of `a * cond`
which
    uses that form.

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

            PR tree-optimization/114894

    gcc/ChangeLog:

            * match.pd (`a != 0 ? a / b : 0`): New pattern.
            (`a != 0 ? a * b : 0`): New pattern.
            (`a != 0 ? a & b : 0`): New pattern.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/phi-opt-value-5.c: New test.

    Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>

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

* [Bug tree-optimization/114894] `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b`
  2024-04-30  6:18 [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b` pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-05-07 21:42 ` cvs-commit at gcc dot gnu.org
@ 2024-05-07 21:43 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-07 21:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

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

end of thread, other threads:[~2024-05-07 21:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-30  6:18 [Bug tree-optimization/114894] New: `a == 0 ? 0 : a * b` -> `a * b` likewise for `a & b` pinskia at gcc dot gnu.org
2024-04-30  6:18 ` [Bug tree-optimization/114894] " pinskia at gcc dot gnu.org
2024-04-30  6:23 ` pinskia at gcc dot gnu.org
2024-04-30 18:04 ` pinskia at gcc dot gnu.org
2024-04-30 18:18 ` pinskia at gcc dot gnu.org
2024-05-07 21:42 ` cvs-commit at gcc dot gnu.org
2024-05-07 21:43 ` 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).