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

            Bug ID: 104106
           Summary: Fail to remove some useless loop
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: denis.campredon at gmail dot com
  Target Milestone: ---

In the following snippet none of the loops are removed when compiled with -O2
or -O3.

In f and g the optimizers shoulds detect that tmp_a is only written and never
read.

In h, only one index of tmp_a is read, so it should be the only one computed.

Ideally, if not too complex for gcc, the first two loops should be removed and
the computations, if any, done on the last loop.

-------------
int f(char *a, unsigned n) {
    char tmp_a[n];

    for (unsigned i = 1; i != n; i++) tmp_a[i] = a[i];
    return a[0];
}

int g(char *a, int n) {
    char tmp_a[n];

    for (int i = 1; i < n; i++) tmp_a[i] = a[i] - a[i - 1];
    return a[0];
}

int h(char *a, int n) {
    char tmp_a[n];

    for (int i = 0; i < n; i++) tmp_a[i] = a[i];
    return tmp_a[1];
}

int i(char *a, char *b, int n) {
    char tmp_a[n];
    char tmp_b[n];

    for (int i = 1; i < n; i++) tmp_a[i] = a[i] - a[i - 1];
    for (int i = 1; i < n; i++) tmp_b[i] = b[i] - b[i - 1];

    int result = 0;
    for (int i = 1; i < n; i++) result += tmp_a[i] + tmp_b[i];

    return result;
}
---------------------

Reply via email to