Open
Description
For now loop invariand motion is weakend in order to avoid some unsafe transformations.
Consider the following snippet:
i = 0;
while (i < n) {
if (a != null) {
print(a.data[i]);
}
}
Loop invariant motion without constraints makes the following:
i = 0;
b = a.data; // may throw NPE
while (i < n) {
if (a != null) {
print(b[i]);
}
}
A better approach to avoid such transformations is to check whether loop invariant dominates all loop exits. However, it will have no use in most while
loops, since loop exit won't be dominated by any invariant. We need to transform while
loops into do
... while
loops by applying loop inversion. For example, our snippet must be transformed to:
if (i < n) {
do {
if (a != null) {
print(b[i]);
}
} while (i < n);
}