getting GCC to hoist my vtable lookup (in C)?
Josh Haberman
jhaberman@gmail.com
Sun Jul 18 01:30:00 GMT 2010
I'm implementing virtual dispatch in C with an explicit vtable. Is
there any way to coax gcc into hoisting the vtable lookup?
struct my_type;
typedef struct my_type my_type;
typedef struct {
int (*my_virtual_func)(my_type *, int);
} my_vtbl;
struct my_type {
my_vtbl* vtbl;
};
static inline int dispatch_my_virtual_func(my_type *type, int val) {
return type->vtbl->my_virtual_func(type, val);
}
void foo(my_type *type, int count) {
for (int i = 0; i < count; i++) {
dispatch_my_virtual_func(type, 123);
}
}
When I inspect my output, I see that the vtable lookup happens inside
the loop, and I adding "const" in various places doesn't seem to help.
Oddly enough, GCC doesn't seem to hoist vtable lookups in C++ either:
class MyType {
public:
virtual int MyVirtualFunc(int x) const = 0;
};
void foo(MyType *obj, int count) {
for (int i = 0; i < count; i++) {
obj->MyVirtualFunc(123);
}
}
Again, looking at the output of this I see the vtable lookup in the
loop. This is on x86-64, gcc 4.4.3 on Ubuntu Lucid.
Josh
More information about the Gcc-help
mailing list