[llvm-commits] Trampoline changes

Chris Lattner clattner at apple.com
Fri Sep 28 12:16:09 PDT 2007


On Sep 17, 2007, at 3:45 AM, Duncan Sands wrote:
>>> My hope was that some of these trampoline calls could be eliminated
>>> using this optimization.  But for...
>>
>> It would be pretty straight-forward to implement an IPO pass that
>> turns trampoline pointers into fat pointers in cases like this.
>
> that's a very interesting idea.  It sounds hard to an LLVM minnow like
> myself though :)
>
>> It sounds like it could be a big win for the container library,  
>> because
>> the container (if the routines get marked internal) would
>> automatically be converted to take fat pointers, even if there are
>> multiple different trampolines being passed in.
>
> Yes, it would be pretty nice.  But how feasible is it?  Surely it will
> only work well if we can find out all or at least most of the  
> places that
> the trampoline function pointer gets passed to.

It is very feasible and would be pretty straight-forward.  Consider  
this code in C:

static void foo(funcptr_t P) {
   P();
}

void a() {
   tmp = maketrampoline(somefunc1, somedata);
   foo(tmp);
}
void b() {
   tmp = maketrampoline(somefunc2, somedata);
   foo(tmp);
}
void c() {
   tmp = maketrampoline(somefunc3, somedata);
   foo(tmp);
}



The idea is to transform it into:

static void foo(funcptr2_t P1, void *Data) {
   P1(Data);
}

void a() {
   tmp = maketrampoline(somefunc1, somedata);  // probably dead now.
   foo(somefunc1, somedata);
}
void b() {
   tmp = maketrampoline(somefunc2, somedata);  // probably dead now.
   foo(somefunc2, somedata);
}
void c() {
   tmp = maketrampoline(somefunc3, somedata);  // probably dead now
   foo(somefunc3, somedata);
}

The checks involved would be:

1. The function (foo) has to have internal linkage.
2. The function is only called by direct calls, no taking the address  
of foo etc.
3. It takes some function pointer as an argument.
4. all call sites (which you know are direct) pass in a function  
pointer obtained from llvm.trampoline

In this case, you can handle it, transforming it as above.  The nice  
thing about this is that the xform doesn't increase code size, so  
it's always a win, regardless of how large foo is.


If you wanted to do this, this should go into the arg promotion pass,  
which already does much of this analysis.

-Chris




More information about the llvm-commits mailing list