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

            Bug ID: 103803
           Summary: i++; i = i % constant -> i >= constant-1 ? 0 : i+1
           Product: gcc
           Version: 12.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 next_trivial(int i) {
    if (i < 0 || i >= 10) __builtin_unreachable ();
    ++i;
    i = i % 10;
    return i;
}
int next_trivial1(int i) {
    if (i < 0 || i >= 10) __builtin_unreachable ();
    ++i;
    if (i >= 10) i = 0;
    return i;
}

These two should produce the same assembly code, the next_trivial1 is better
than next_trivial

Reply via email to