getting GCC to hoist my vtable lookup (in C)?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux