Forward Store

Stores to global variables in loops can be moved out of the loop to reduce memory bandwidth requirements.

Example:

In the code fragment below, the load and store to the global variable sum can be moved out of the loop by computing the summation in a register and then storing the result to sum outside the loop.

int sum;

void f (void)
{
  int i;

  sum = 0;
  for (i = 0; i < 100; i++)
    sum += a[i];
}
  

Below is the code fragment after forward store optimization (we assume that t is a compiler generated temporary that is kept in a register).

int sum;

void f (void)
{
  int i;
  register int t;

  t = 0;
  for (i = 0; i < 100; i++)
    t += a[i];
  sum = t;
}