Constant propagation and strength reduction

David Edelsohn dje@watson.ibm.com
Tue Jan 21 17:58:00 GMT 2003


	Some of my colleagues at IBM noticed a surprising case where GCC
does not propagate a constant to allow optimization of a divide
illustrated by the following example:


int
foo1 (int x)
{
  return x / 3;
}

int
foo2 (int x)
{
  int y = 3;

  return x / y;
}

int
foo3 (int x)
{
  const int y = 3;

  return x / y;
}

In my tests, the GCC converts the divides in foo1 and foo3 to multiplies
and shifts while foo2 remains an explicit divide.  "y" is local in scope
to the function foo2 and cannot change, so why doesn't GCC propagate the
constant when compiling with optimization?  Is this a C language
requirement?  The constant needs to be propagated in the tree
representation so that expand_divmod chooses the optimized algorithm.

Thanks, David



More information about the Gcc-patches mailing list