This is the original code
for (j = 0; j <= 30; j++) {
for (k = 0; k <= 30; k++) {
x[j][k] = y[j][k] + z[j][k];
}
}
And the code below is after applying a common sub-expression elimination optimization
for (j = 0; j <= 30; j++) {
for (k = 0; k <= 30; k++) {
t2 = C1 * j + W * k;
x[t2] = y[t2] + z[t2];
}
}
How does the common sub-expression elimination helps to improve performance in this case ? Its only array indices that is optimized ?
Any half-decent modern compiler will already be performing this optimisation.
Furthermore part of the index calculation should be hoisted into the outer loop: