[cfe-dev] "load groups" IR feature to improve C++ devirtualization
John McCall
rjmccall at apple.com
Thu May 9 19:13:48 PDT 2013
On May 9, 2013, at 6:34 PM, Nick Lewycky <nlewycky at google.com> wrote:
> I'm looking into how we can improve devirtualization in clang, and there a language in C++ feature I'd like to take advantage of which would let us perform elimination of more vptr loads. In this code:
>
> Cls *p = new Cls;
> p->virtual_method1();
> p->method_changing_vptr(); // uses placement new to legally change the vptr
> p->virtual_method2(); // invalid!
> Cls *q = p;
> q->virtual_method2(); // this must get a new vptr lookup.
This is not how I understand the [basic.life] rules. The question is whether a pointer value, reference, or name is formally forwarded to point to the new object. Because the dynamic type is different, the pointer value held in 'p' is not updated. Copying that value into 'q' does not change the fact that the pointer value still refers to a non-existent object.
It is unclear what, exactly, under the rules constitutes forming a valid pointer to the newly-constructed object except using the result of the new-expression itself. I think an explicit cast might, ignoring all "object-ness" of the source pointer and simply treating it formally as a pointer to some storage that you are casting to the type of an object stored there?
John.
More information about the cfe-dev
mailing list