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

            Bug ID: 125774
           Summary: [13 Regression] wrong code generated for range check
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ell_se at yahoo dot com
  Target Milestone: ---

int f(const int* a, int n) {
        int lower = a[0];
        int upper = a[0];

        int i;

        for (i = 1; i < n; ++i) {
            if (a[i] < lower || a[i] > upper) break;
            lower = 0;
        }

        return i;
    }

With -O1 or higher the loop is eliminated entirely:

    f:
            mov     eax, 1
            ret

This happens if either `lower` or `upper` is modified inside the loop, and if
their types match the element type of `a`. Only seems to affect GCC 13.x (last
tested on 13.4), not older or newer series.

Test:

    int main() {
        const int a[] = {0, 0};
        assert(f(a, 2) == 2); // returns 1
    }

Reply via email to