llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Indirect call of phi/select not converted into direct calls

Open nikic opened this issue 4 years ago • 3 comments

When calling a phi/select of function pointers, we do not attempt to convert this into a branch over direct calls: https://llvm.godbolt.org/z/6s8PxKcj6

This precludes further optimizations, such as inlining.

nikic avatar Apr 28 '22 08:04 nikic

@nikic, I've looked into this a bit, but before sending patches I'd like to find out your opinion. Any suggestions / recommendations would be appreciated.

Option 1. Similarly to the pass CallSiteSplitting when we have call uint64 %phi() in some basic block BB, we can create new basic blocks SplitBB_1, SplitBB_2, SplitBB_3, SplitBB_4, duplicate the instructions from the prefix of BB up until the call and remove them from BB, create a new Phi node inside BB to "collect" the results of @fn1, @fn2, @fn3, @fn4 (from SplitBB_1, ... , SplitBB_4). One downside of this approach - need a threshold to control the aforementioned duplication.

Option 2. Instead of duplicating the prefix of BB one can introduce an indirect branch (switch) to the newly created basic blocks SplitBB_1, SplitBB_2, SplitBB_3, SplitBB_4.

P.S. What would be the best place for this transformation ?

cc: @fhahn

alexander-shaposhnikov avatar May 07 '22 00:05 alexander-shaposhnikov

cc @aeubanks @teresajohnson

davidbolvansky avatar Jun 22 '22 10:06 davidbolvansky

I suppose I got cc'ed because of my WPD work, but this seems quite different - at least from the examples in the godbolt link. I'm not very familiar with CallSiteSplitting, but can it be expanded to handle this case? @alexander-shaposhnikov can you show a short example for each of your 2 options?

teresajohnson avatar Jun 22 '22 14:06 teresajohnson