Bug 104378 - (N - x) ^ N should be optimized to x if x <= N (unsigned) and N is a pow2 - 1
Summary: (N - x) ^ N should be optimized to x if x <= N (unsigned) and N is a pow2 - 1
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: 104376
  Show dependency treegraph
 
Reported: 2022-02-04 07:01 UTC by Andrew Pinski
Modified: 2022-02-04 18:36 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-02-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2022-02-04 07:01:04 UTC
Take:
#define n 8
#define N ((1u<<n)-1)

unsigned countLeadingZeros32(unsigned x)
{
   if (x > N) __builtin_unreachable();
   return (N - x) ^ N;
}

This should be optimized to just
return x;

Like it is done by LLVM.