This is an archive of the discontinued LLVM Phabricator instance.

[llvm] Bug fix: Loop interchange for multiple loops with conditional statements
Needs ReviewPublic

Authored by masakazu.ueno on Feb 15 2021, 5:51 AM.

Details

Summary

Loop interchange for a multiple loop containing if-statement may be performed incorrectly.
Therefore, I modified the loop interchange so that it does not optimize loops that contain conditional statements.

For example, in the following example, "-1, -1" is correct. However, if the loop is interchanged, "1, 1" is output.

#include <stdio.h>

void bar(int x[restrict 2], int a[restrict 2][2]) {
    for (int i=0; i<2; ++i) {
        for (int j=0; j<2; ++j) {
            for (int k=0; k<2; ++k) {
                if (x[j] == a[k][i]) {
                    x[j] = i - k;
                }
            }
        }
    }
}

int main(void) {
    int x[2] = {2, 2};
    int a[2][2] = {{0, 2}, {2, 0}};
    bar(x, a);
    printf("%d, %d\n", x[0], x[1]);
    return 0;
}

Specified options are "-Ofast -mllvm -enable-loopinterchange=true -flegacy-pass-manager".

Diff Detail