📝Loop unswitching

Loop unswitching is a compiler optimization, where a loop with a condition inside is replaced by outside condition and two version of the loop. This has a great benefit because (1) the condition is not checked inside hot path, (2) it allows additional vectorization of the code.

As an example:

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

the code above can be transformed into:

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

See also

Backlinks