Common subexpression elimination replaces multiple instances of the same expression with a single instance. This optimization reduces size and increases execution speed.
Controlling common subexpression elimination explains how to control the optimization for common subexpression elimination.
| Turn control this option from here... | use this setting |
|---|---|
| CodeWarrior IDE | Choose an Optimization Level value (2,3,4) from the Properties > C/C++ Build > Settings > Tool Settings > PowerPC Compiler > Optimization panel. |
| source code | #pragma opt_common_subs on | off | reset |
| command line | -opt [no]cse |
For example, in Before common subexepression elimination , 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;
}
}
After common subexpression elimination shows equivalent source code after the compiler applies common subexpression elimination. The compiler generates instructions to compute x * y and store 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;
}
}