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

            Bug ID: 87609
           Summary: miscompilation with restrict and loop
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: comexk at gmail dot com
  Target Milestone: ---

Test program below.  Compiled at -O0, it outputs "7 10"; at -O3, it outputs "10
7".  Removing the restrict qualifier on the arguments to copy() avoids the
issue, but copy() is never called with a == b, so there shouldn't be any
undefined behavior.

Tested with revision 7497874053f (current master), host and target
x86_64-apple-darwin18.0.0, installed using Homebrew.

--

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

__attribute__((always_inline))
static inline void copy(int *restrict a, int *restrict b) {
    assert(a != b);
    *b = *a;
    *a = 7;
}

__attribute__((noinline))
void floppy(int mat[static 2], size_t idxs[static 3]) {
    for (int i = 0; i < 3; i++) {
        copy(&mat[i%2], &mat[idxs[i]]);
    }
}

int main() {
    int mat[2] = {10, 20};
    size_t idxs[3] = {1, 0, 1};
    floppy(mat, idxs);
    printf("%d %d\n", mat[0], mat[1]);
}

--

(For reference, I found this while investigating a similar bug in LLVM:
https://bugs.llvm.org/show_bug.cgi?id=39282)

Reply via email to