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.

The following table 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 Level 2, Level 3, or Level 4 in the Global Optimizations settings panel.
source code #pragma opt_propagation on | off | reset
command line -opt [no]prop[agation]

For example, in the following listing, the variable j is assigned the value of x.

Listing: 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; 

    }  

}

But j's value is never changed, so the compiler replaces later instances of j with x, as shown in the following listing.

Listing: 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;

    }  

}

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.