https://bugs.llvm.org/show_bug.cgi?id=50190

            Bug ID: 50190
           Summary: Possible Issue with Whole Program Devirtualization
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected],
                    [email protected]

class A {
public:
  virtual int foo() const = 0;
};

class B : public A {
public:
  virtual int foo() const { return 2; }
};

class C : public A {
public:
  virtual int foo() const { return 3; }
};

A *get_A(int num) {
  if (num > 1) {
    printf("returning new B\n");
    return new B; 
  }
  printf("returning new C\n");
  return new C;
}

__attribute__((noinline))
void call_foo(int argc) {
  C *c = (C *)get_A(argc);
  printf("%d\n", c->foo());
}

int main(int argc, char *argv[]) {
  call_foo(argc);
}

trunk clang with -O3 -flto -whole-program-vtables -fvisibility=hidden outputs
./a.out 1
returning new B
3

gcc 10.2 with -O3 -flto -fwhole-program -fvisibility=hidden outputs
./a.out 1
returning new B
2

It looks like the c->foo is being devirtualized to a direct C::foo() call.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to