Bug 96923 - Failure to optimize a select-related bool pattern to or+not
Summary: Failure to optimize a select-related bool pattern to or+not
Status: RESOLVED DUPLICATE of bug 89263
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 11.0
: P3 enhancement
Target Milestone: ---
Assignee: Andrew Pinski
URL:
Keywords: missed-optimization
Depends on: 25290 100864
Blocks: 19987 20083 94898 105903
  Show dependency treegraph
 
Reported: 2020-09-03 14:06 UTC by Gabriel Ravier
Modified: 2023-08-02 14:01 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-06-01 00:00:00


Attachments
Patch which is in testing (needs testcases) (922 bytes, patch)
2021-06-01 21:40 UTC, Andrew Pinski
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gabriel Ravier 2020-09-03 14:06:08 UTC
_Bool f2(_Bool a, _Bool b)
{
    return !a ? !b : 0;
}

This can be optimized to `return !(a | b);`. This transformation is done by LLVM, but not by GCC.
Comment 1 Gabriel Ravier 2020-09-03 14:49:21 UTC
_Bool f2(_Bool a, _Bool b)
{
    return a ? !b : 1;
}

This similar pattern can be optimized to `return !(a & b);`. This transformation is done by LLVM, but not by GCC.
Comment 2 Andrew Pinski 2020-09-06 23:47:26 UTC
Note if we spell out the ?:, this would require PR 25290 too.
Comment 3 Andrew Pinski 2021-06-01 21:02:44 UTC
Mine, I have a patch which implements this.
It needs https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571616.html first.
I will be posting this once I write some testcases and do a bootstrap/test cycle.
Comment 4 Andrew Pinski 2021-06-01 21:40:22 UTC
Created attachment 50905 [details]
Patch which is in testing (needs testcases)

As I said for the case in this PR, it needs https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571616.html too.

If you change !b to b; that is "!a ? b : 0", this patch will optimize it.  The other patch is needed to allow to move !b out of the conditional.
Comment 5 Andrew Pinski 2021-06-01 23:19:05 UTC
(In reply to Andrew Pinski from comment #4)
> Created attachment 50905 [details]
> Patch which is in testing (needs testcases)
> 
> As I said for the case in this PR, it needs
> https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571616.html too.
> 
> If you change !b to b; that is "!a ? b : 0", this patch will optimize it. 
> The other patch is needed to allow to move !b out of the conditional.

This patch has one bug in it where we need a convert added.
BUT then we run into a different missed optimization issue.
  _51 = p2_22 <= prephitmp_122;
  _44 = (logical(kind=4)) _51;
  _37 = p2_22 > prephitmp_122;
  _49 = (logical(kind=4)) _37;
  _38 = _49 & found_p_63;
  _46 = _38 | _44;

This is really just:
_51 = p2_22 <= prephitmp_122;
_44 = (logical(kind=4)) _51;
_46 = found_p_63| _44;

That is we don't optimize:
(a & ~b) | b into a | b if ~b has been converted already.

The other thing I noticed is the cast should not be really needed but nothing removes it; I will look at that later.

Note I could rewrite the pattern to do the simplification of the constants manually but I want to try to avoid that.
Comment 6 Andrew Pinski 2021-06-01 23:49:10 UTC
This depends on PR 100864 if I don't want to write out the 4 patterns.
Comment 7 Andrew Pinski 2023-05-24 00:01:24 UTC
Ok, this in the end is a dup of bug 89263.

*** This bug has been marked as a duplicate of bug 89263 ***