Alias Optimization (by type)

The ANSI C standard specifies the following regarding the circumstances in which an object may or may not be aliased.

An object shall have its stored value accessed only by an lvalue that has one of the following types:

This means, for example, that a compiler can assume that a pointer to an object of type short is not an alias for a pointer to an object of type int, and perform optimizations based on this assumption.

Example:

In the code fragment below, the lvalues ps and pi have different types, and the compiler can assume they are not aliased.

void f (short *ps, int *pi)
{
  int i, j;
  i = *pi;
  *ps = 3;
  j = *pi;
  g (i, j);
}
  

Since ps and pi can not be aliased, the store through ps can not change the object pointed to by pi, and the second reference to the object pointed to by pi can be eliminated, as shown below.

void f (short *ps, int *pi)
{
  int i, j;
  i = *pi;
  *ps = 3;
  g (i, i);
}
  

Notes:

Although specified in the ANSI C standard as undefined behavior, some programs use aliasing. To support these programs, some compilers avoid alias optimization by type, or provide user-selectable options to inhibit this optimization.