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:
- the declared type of the object,
- a qualified version of the declared type of the object,
- a type that is the signed or unsigned type corresponding to the declared type of the object,
- a type that is the signed or unsigned type corresponding to a qualified version of the declared type of the object,
- an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a sub-aggregate or contained union), or
- a character type.
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); }