πŸ“Loop unswitching must pay attention to possibly zero-counted loops (in presence of uninitialized values)

top

Loop unswitching

source

Chisnall2018

Consider the following example:

int i, j, w, x[1000], y[1000];
for (i = 0; i < j; i++) {
  x[i] += y[i];
  if (w)
    y[i] = 0;
 }
int i, j, w, x[1000], y[1000];
if (w) {
  for (i = 0; i < j; i++) {
    x[i] += y[i];
    y[i] = 0;
  }
 } else {
  for (i = 0; i < j; i++) {
    x[i] += y[i];
  }
 }

Because j might be 0, the original loop might execute 0 times and never access w (which might be uninitialized). The β€œoptimized” version always accesses w, and thus is invalid optimization.

This is not an issue for languages that do not allow uninitialized values.

Backlinks