Convert Function Pointer Call to Function Call at the IR Level

Hi,
I want to convert a function pointer call in the IR of MPlayer to a function call. For example, I have the following line:


%10 = tail call i32 %7(%struct.demuxer* nonnull %0, i32 %1, i8* %2) #7, !dbg !863222


I want to set the target which is stored in %7 to a real function called “demux_lavf_control()” with the following definition:

define internal i32 @demux_lavf_control(%struct.demuxer.2657* nocapture readonly, i32, i8* nocapture) #0 !dbg !963916 {


I wrote a function pass and found the mentioned function pointer call (here, stored in “CallInstr”) and set the call target as follows:

Module *theModule = F.getParent();
Function *targetFunc = theModule->getFunction(“demux_lavf_control”);
CalIInstr->setCalledFunction(targetFunc);


“F” is the function containing the call. But it leads to the following error:


Call parameter type does not match function signature!
%struct.demuxer* %0
%struct.demuxer.2657* %10 = tail call i32 @demux_lavf_control(%struct.demuxer* nonnull %0, i32 %1, i8* %2) #7, !dbg !863222
LLVM ERROR: Broken function found, compilation aborted!


The [names:types] of call operands before the change is:

[:%struct.demuxer*]
[:i32]
[:i8*]
[:i32 (%struct.demuxer*, i32, i8*)*]


And after the change is:

[:%struct.demuxer*]
[:i32]
[:i8*]
[demux_lavf_control:i32 (%struct.demuxer.2657*, i32, i8*)*]


How can I solve the problem?
Thanks in advance!

Hi.
It seems that struct.demuxer.2657* is different from struct.demuxer*. Should I add something like a bitcast before the call?

Thanks!

Yep. We encountered similar issue a while ago due to IR linking. A bitcasted function argument would do

Zhang

Thanks.