This is the mail archive of the gcc-help@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]

cast lvalues optimizing and strict-aliasing


Hello,

from my understanding of the type-punning aliasing rule, the following code is correct, and indeed compiles fine with gcc/trunk :

typedef struct {
  short a : 16;
  short b : 16;
} c;

foo(c t1)
{
  char *pt1 = (char*)&t1;

  if (*(int*)(pt1))
    return 0;

  return 1;
}

However if I try to rewrite it using an intermediate cast :

foo(c t1)
{
  if (*((int*)((char*)&t1)))
    return 0;

  return 1;
}

the compiler emits the "dereferencing type-punned..." warning message.

Indeed the gimple tree doesn't show the (char *) cast:

;; Function foo (null)
;; enabled by -tree-original


if (*(int *) &t1 != 0) { return 0; } return 1; }

And the alias analyser see the types as not-compatible. So was the parser allowed to ignore the (char *) cast ? I think this would change the aliasing semantic, but before suspecting a bug, I would very much like to have other opinions.

many thanks

Christian



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