Copy Propagation

Copy propagation replaces variables with their original values if the variables do not change. This optimization reduces runtime stack size and improves execution speed.

Controlling copy propagation explains how to control the optimization for copy propagation.

Table 1. Controlling copy propagation
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_propagation on | off | reset
command line -opt [no]prop[agation]

For example, in Before copy propagation , the variable j is assigned the value of x . But j 's value is never changed, so the compiler replaces later instances of j with x , as shown in After copy propagation .

By propagating x , the compiler is able to reduce the number of registers it uses to hold variable values, allowing more variables to be stored in registers instead of slower memory. Also, this optimization reduces the amount of stack memory used during function calls.

Figure 1. Before copy propagation
void func_from(int* a, int x)
{
    int i;
    int j;
    j = x;
    for (i = 0; i < j; i++)
    {
        a[i] = j; 
    }  
}
Figure 2. After copy propagation
void func_to(int* a, int x)
{
    int i;
    int j;
    j = x;
    for (i = 0; i < x; i++)
    {
        a[i] = x;
    }  
}