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

            Bug ID: 109878
           Summary: missed simplifications of MAX<a&CST0,a&CST1> and
                    MIN<a&CST0,a&CST1>
           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 max_and(int a, int b)
{
        b = a & 3;
        a = a & 1;
        if (b > a)
          return b;
        else
          return a;
}
int min_and(int a, int b)
{
        b = a & 3;
        a = a & 1;
        if (b < a)
          return b;
        else
          return a;
}
```
max_and should just be optimized to `a&3` while min_and should be just
optimized to `a&1` The general rule is:
MAX<a&CST0, a & CST1> -> a & CST0 IFF CST0 &CST1 == CST1, that is CST1 is a
true subset of CST0.

I found this on accident while thinking about some generated code in
insn-automata.cc on x86_64.
MIN<a&CST0, a & CST1> -> a & CST0 IFF CST0&CST1 == CST0, that is CST0 is a true
subset of CST1.

Reply via email to