Vyacheslav Egorov <vegorov@xxxxxxxxxxxx> writes: > I will try to read places in GCC source where actual alias analysis > happens. This should give me a better understanding of mechanics and > underlying assumptions. I think it would be better to focus on understanding the language semantics as described in the appropriate standards. The GCC developers will always try to implement the language semantics. The specific aliasing implementation will change--in fact, every release since GCC 4.0 has substantially changed the aliasing implementation, in the ongoing quest for the appropriate mix between compile time efficiency and runtime performance. That said, see gcc/tree-ssa-alias.c and gcc/alias.c. >> Again what matters is that all accesses to a particular object use the >> same type. ÂCasting to other pointers doesn't matter as long as you >> don't actually use those pointers. ÂIn this case I don't know whether >> you use them or not. ÂI assume there are no virtual functions here. ÂIf >> you have an Object* po, and you cast it to a Foo* pf, and you then say >> pf->x, that should be OK with regard to the aliasing rules. > > Let me provide you a bit more detail: we have this hierarchy of > classes with Object class as a root and all other (e.g. GlobalObject, > Context) inheriting from it. This classes have no members, no virtual > functions, no constructors, no destructors. They are never constructed > as values. We only have variables of pointer types like Context* or > Object**. On this variables we call methods: > > Context* ctx = foo(); > ctx->set_bar(ctx); > > Inside setters/getters we treat this pointer kinda like an Object** pointer: > > #define FIELD_ADDR(p, offset) \ > (reinterpret_cast<byte*>(p) + offset - kHeapObjectTag) > > #define WRITE_FIELD(p, offset, value) \ > (*reinterpret_cast<Object**>(FIELD_ADDR(p, offset)) = value) > > So set_bar() method would be implemented like > > void Context::set_bar(Context* ctx) { > WRITE_FIELD(this, kBarFieldOffset, ctx); > } > > We only load/store to/from object "fields" through this kind of casts. > > Does it look OK to you? This looks OK if you modify fields using WRITE_FIELD and you only access fields using the corresponding READ_FIELD macro. Well, also, you need to allocate Object* values using malloc or placement new, not, say, new char[]. If those are all true, then I don't see any aliasing violation here. Ian