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

            Bug ID: 112094
           Summary: `popcnt(a) == n | a != 0` should be simplified to
                    `a!=0 | n == 0`
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: pinskia at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

```
int f(int a, int n)
{
        int t = __builtin_popcount(a);
        int b = a != 0;
        int c = t == n;
        return b | c;
}

int f_(int a, int n)
{
        int b = a != 0;
        int c = n == 0;
        return b | c;
}

int g(int a, int n)
{
        int t = __builtin_popcount(a);
        int b = a == 0;
        int c = t != n;
        return b & c;  // a == 0
}

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

I Noticed that LLVM handles only if n is a constant. We could do this if
__builtin_popcount is single use if n is not a constant or maybe if `n != 0`
simplifies ...

Reply via email to