Oracle® Solaris Studio 12.4: C User's Guide

Exit Print View

Updated: March 2015
 
 

5.4.1 Example: Levels of Aliasing

Consider the following code. It can be compiled with different levels of aliasing to demonstrate the aliasing relationship of the shown types.

struct foo {
    int f1;
    short f2;
    short f3;
    int f4;
} *fp;

struct bar {
    int b1;
    int b2;
    int b3;
} *bp;

int *ip;
short *sp;

If this example is compiled with the -xalias_level=any option, the compiler considers the following indirect accesses as aliases to each other:

*ip, *sp, *fp, *bp, fp->f1, fp->f2, fp->f3, fp->f4, bp->b1, bp->b2, bp->b3

If this example is compiled with the -xalias_level=basic option, the compiler considers the following indirect accesses as aliases to each other:

*ip, *bp, fp->f1, fp->f4, bp->b1, bp->b2, bp->b3

Additionally, *sp, fp->f2, and fp->f3 can alias each other, and *sp and *fp can alias each other.

However, under -xalias_level=basic, the compiler assumes the following:

  • *ip does not alias *sp.

  • *ip does not alias fp->f2 and fp->f3.

  • *sp does not alias fp->f1, fp->f4, bp->b1, bp->b2, and bp->b3.

The compiler makes these assumptions because the access types of the two indirect accesses are different basic types.

If this example is compiled with the -xalias_level=weak option, the compiler assumes the following alias information:

  • *ip can alias *fp, fp->f1, fp->f4, *bp, bp->b1, bp->b2, and bp->b3.

  • *sp can alias *fp, fp->f2 and fp->f3.

  • fp->f1 can alias bp->b1.

  • fp->f4 can alias bp->b3.

The compiler assumes that fp->fp1 does not alias bp->b2 because f1 is a field with offset 0 in a structure, whereas b2 is a field with a 4-byte offset in a structure. Similarly, the compiler assumes that fp->f1 does not alias bp->b3, and fp->f4 does not alias either bp->b1 or bp->b2.

If this example is compiled with the -xalias_level=layout option, the compiler assumes the following information:

  • *ip can alias *fp, *bp, fp->f1, fp->f4, bp->b1, bp->b2, and bp->b3.

  • *sp can alias *fp, fp->f2, and fp->f3.

  • fp->f1 can alias bp->b1 and *bp.

  • *fp and *bp can alias each other.

fp->f4 does not alias bp->b3 because f4 and b3 are not corresponding fields in the common initial sequence of foo and bar.

If this example is compiled with the -xalias_level=strict option, the compiler assumes the following alias information:

  • *ip can alias *fp, fp->f1, fp->f4, *bp, bp->b1, bp->b2, and bp->b3.

  • *sp can alias *fp, fp->f2, and fp->f3.

With -xalias_level=strict, the compiler assumes that *fp, *bp, fp->f1, fp->f2, fp->f3, fp->f4, bp->b1, bp->b2, and bp->b3 do not alias each other because foo and bar are not the same when field names are ignored. However, fp aliases fp->f1 and bp aliases bp->b1.

If this example is compiled with the -xalias_level=std option, the compiler assumes the following alias information:

  • *ip can alias *fp, fp->f1, fp->f4, *bp, bp->b1, bp->b2, and bp->b3.

  • *sp can alias *fp, fp->f2, and fp->f3.

However, fp->f1 does not alias bp->b1, bp->b2, or bp->b3 because foo and bar are not the same when field names are considered.

If this example is compiled with the -xalias_level=strong option, the compiler assumes the following alias information:

  • *ip does not alias fp->f1, fp->f4, bp->b1, bp->b2, and bp->b3 because a pointer, such as *ip, should not point to the interior of a structure.

  • Similarly, *sp does not alias fp->f1 or fp->f3.

  • *ip does not alias *fp, *bp, and *sp due to differing types.

  • *sp does not alias *fp, *bp, and *ip due to differing types.