This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
getting GCC to hoist my vtable lookup (in C)?
- From: Josh Haberman <jhaberman at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 17 Jul 2010 18:57:24 +0000 (UTC)
- Subject: getting GCC to hoist my vtable lookup (in C)?
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