This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Any way to locally apply no-strict-alias?
John Fine wrote:
> Andrew Haley wrote:
>> You are allowed to take a pointer, copy it as a void *, and copy it
>> back to the same type and dereference it. If the compiler removes
>> the copy step, then the compiler is broken. If your code accesses
>> the object via an incompatible type, then your code is broken.
>
> I think the difference between that and what I'm doing is the extra
> level of indirection.
>
> I write a Datatype* indirectly through a Datatype**. Then I read a
> void* indirectly through a void** that happens to be the same pointer as
> that Datatype**.
> Next I do the reverse (write through void** to read through Datatype**).
By my reckoning this should still work. Writing through the void**
will be fine. Casting &(void*) to Datatype** is the bit that won't
work. But you don't need to do that...
> You're saying I can copy a Datatype* to a void* then back to a Datatype*
> and use it. That wouldn't be a violation of strict aliasing, because it
> never dereferences two different type pointers to the same address.
OK, I now understand.
> I know my code is "broken". I didn't write it and I have no practical
> way to rewrite it. I'm looking for a local work around to the problem.
>
> I've looked at the generated assembler code and the copy step is
> optimized out when compiled without -fno-strict-alias and isn't
> optimized out when compiled with -fno-strict-alias.
OK, but you still haven't said what "the copy step" is.
We obviously don't want to start having a no-strict-alias attribute for
pointers. I think it's pointless because it would only apply to new
code, and the whole point of no-strict-alias is to make old code work.
Andrew.