More space efficient virtual function calls

Wink Saville wink@saville.com
Sun Jul 7 08:45:00 GMT 2002


Hello,

I was wondering if there was switch in gcc that would cause it to generate
smaller code when invoking virtual functions. I'm using GCC 3.1 ARM cross
compiler with -O3 and -fvtable-thunks=3, and it does the following when
calling a virtual function with no parameters:

    LDR    R1,[R4]        /* Fetch the vtable pointer */
    MOV    R0,R4        /* R0 = this */
    MOV LR, PC          /* Set of the link register */
    LDR PC,[R1, #4]  /* Call virtual function 1, the second entry */

This is good code but it takes 20 bytes, what I was hoping for when I
enabled -fvtable-thunks=3 the compiler would use intermediate "thunks" to
invoke functions such as:

    LDR    R0,R4    /* R0 = this */
    BL    __VTFunc1     /* Branch and Link to virtual thunk function 1  */

...

__VTFunc0:
    MOV    R5,[R0]        /* Get vtable pointer */
    LDR    PC,[R5,#0]    /* Jump to the function */
__VTFunc1:
    MOV    R5,[R0]        /* Get vtable pointer */
    LDR    PC,[R5,#4]    /* Jump to the function */
__VTFunc2:
    MOV    R5,[R0]        /* Get vtable pointer */
    LDR    PC,[R5,#8]    /* Jump to the function */


This only costs 8 bytes per call instead of 20 (plus the size of the
_VTFuncX code). Of course this has at least two consequences, 1) A register,
in this example R5, would have to be reserved for use by the thunking code.
2) There is a performance hit because of two jumps. For space constrained
applications the trade off would be well worth it and the programmer should
be able to use a pragma or compiler switch to select which type of code to
emit.

Is this already possible with an existing switch/pragma?
Is this practical?
Would most of the modifications be need to needed to the front end, back end
or both?
Could someone point me to the places I would need to change to try this out?


Thanks,

Wink Saville





More information about the Gcc mailing list