How to supress a specific kind of ansi-aliasing rules?

Sergei Organov osv@javad.com
Wed Jun 20 11:52:00 GMT 2007


"Bokhanko, Andrey S" <andrey.s.bokhanko@intel.com> writes:

> Hi,
>
> As I learned from experience, gcc always assume independence between
> memory references in the following program:
>
> typedef struct {
>     int m1;
>     int m2;
> } S1;
>
> void foo(S1 *p1, S1 *p2) {
>     ... = p1->m1;
>     ... = p2->m2;
> }
>
> ...even if -fno-strict-aliasing (an option disabling ansi-aliasing
> rules) supplied.
>
> I wonder, is there a way to force gcc not to assume independence in
> the example shown above?

A dirty trick using volatile may work:

int foo(S1 *p1, S1 *p2) {
   ((S1 volatile*)p1)->m2 = 10;
   return ((S1 volatile*)p2)->m1;
}

However, provided that GCC guarantees (unlike C standard) that type
punning through unions will work, it seems that GCC should not assume
p1->m2 and p2->m1 never alias:

typedef struct {
  int pad;
  S1 s;
} X;

typedef union {
  S1 s;
  X  x;
} U;

U u;

If it is guaranteed that

int boo() {
  u.s.m2 = 10;
  return u.x.s.m1;
}

works, then

f(&u.s, &u.x.s);

where

int f(S1 *p1, S1 *p2) {
   p1->m2 = 10;
   return p2->m1;
}

should better work the same way, isn't it?

-- Sergei.



More information about the Gcc mailing list