Bug 96897 - Failure to optimize sub+not involving constant to add
Summary: Failure to optimize sub+not involving constant to add
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 11.0
: P3 enhancement
Target Milestone: 11.0
Assignee: Andrew Pinski
URL:
Keywords: missed-optimization
Depends on: 37516
Blocks: 19987
  Show dependency treegraph
 
Reported: 2020-09-02 11:17 UTC by Gabriel Ravier
Modified: 2023-08-24 21:26 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-09-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gabriel Ravier 2020-09-02 11:17:54 UTC
int f(int x)
{
    return ~(~x & (~x - 1));
}

This can be optimized to `return (x + 1) | x;`. This transformation is done by LLVM, but not by GCC.
Comment 1 Marc Glisse 2020-09-02 11:37:09 UTC
We already transform to
return ~(-2 - x) | x;

so this is really asking for
~(-2 - x) --> x + 1
Comment 2 Gabriel Ravier 2020-09-02 15:28:38 UTC
Better test case (I'd assume) :

int f(int x)
{
    return ~(constant - x);
}

which should be optimizable to `x + ~constant`.
Comment 3 Andrew Pinski 2021-08-03 22:59:02 UTC
MIne because this is basically PR 37516.
Comment 4 Andrew Pinski 2021-08-03 23:04:20 UTC
Fixed with r11-5958.