This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


"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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]