[LLVMdev] Work with CallSites

Duncan Sands baldrick at free.fr
Tue Mar 6 06:39:29 PST 2012


Hi Михаил,

> I have a test program:
>
>     class A {
>     int A;
>     public:
>     virtual void test ( int x ) = 0;
>     };
>
>     class B : public A {
>     int B;
>     public:
>     void test ( int x ) {};
>     };
>
>     int main() {
>     A *a = new B();
>     a->test(1);
>     }
>
>
> We have call site CS: "a->test(1);". CS.getCalledFunction() - return NULL,

LLVM is already capable of devirtualizing this.  For example, I added
   extern void foo(int);
to your testcase, and changed
   void test ( int x ) {};
to
   void test ( int x ) { foo(x); };

Compiling with "clang -S -O4 -o -" gives:
   define i32 @main() uwtable {
   entry:
     tail call void @_Z3fooi(i32 1)
     ret i32 0
   }

If you want to enhance LLVM's devirtualization, I suggest you start by studying
how the optimizers manage to work things like this out, and build on that.

Ciao, Duncan.



More information about the llvm-dev mailing list