Common subexpression elimination replaces multiple instances of the same expression with a single instance. This optimization reduces size and increases execution speed.
| Turn control this option from here... | use this setting |
|---|---|
| CodeWarrior IDE | Choose Level 2 , Level 3 , or Level 4 in the Optimization Level settings panel. |
| source code | #pragma opt_common_subs on | off | reset |
| command line | -opt [no]cse |
For example, in the following listing, the subexpression x * y occurs twice.
void func_from(int* vec, int size, int x, int y, int value) { if (x * y < size) { vec[x * y - 1] = value; } }
The following listing shows equivalent source code after the compiler applies common subexpression elimination. The compiler generates instructions to compute x * y and stores it in a hidden, temporary variable. The compiler then replaces each instance of the subexpression with this variable.
void func_to(int* vec, int size, int x, int y, int value) { int temp = x * y; if (temp < size) { vec[temp - 1] = value; } }