[llvm-dev] Proposal: virtual constant propagation

Peter Collingbourne via llvm-dev llvm-dev at lists.llvm.org
Thu Jan 28 10:25:37 PST 2016


On Thu, Jan 28, 2016 at 10:11:41AM -0800, Pete Cooper wrote:
> Hi Peter
> 
> > On Jan 27, 2016, at 7:57 PM, Peter Collingbourne via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> > 
> > For example, the vtable for Base and Derived will look like this:
> > 
> > struct Base_VTable {
> >  bool isDerived : 1, isOfTypeBase : 1, isOfTypeDerived : 1;
> >  size_t padding : 61;
> >  size_t offset_to_top;
> >  size_t rtti;
> >  bool (*isDerived)(const Base *this);
> >  bool (*isOfType)(const Base *this, Type t);
> > };
> > Base_VTable base_vtable{
> >  false, true, false, 0, 0, 0, &Base::isDerived, &Base::IsOfType
> > };
> > Base_VTable derived_vtable{
> >  True, false, true, 0, 0, 0, &Derived::isDerived, &Derived::IsOfType
> > };
> 
> Given the above vtable, you still need to carry around the new booleans as well as the old pointers.  Have you considered cases where the set of possible candidates is small and comparing the vtable pointer itself is sufficient?
> 
> For example,
> 
> class Base { virtual bool foo() = 0; }
> class A : public Base { virtual bool foo() { return false; } }
> class B : public Base { virtual bool foo() { return true; } }
> class C : public Base { virtual bool foo() { return true; } }
> 
> Base *p;
>> p->foo();
> 
> Here, p->foo() is the same as ‘p->vtable != vtable_A’.  This would reduce both the number of elements you’d need to add to your vtable, as well as reducing the code size as now you only need to load the value of the vtable and not dereference it to get the boolean from inside.

Yes, this is one possible optimization I have considered.

Thanks,
-- 
Peter


More information about the llvm-dev mailing list