Calling with register indirect reference instead of memory indirect reference.

Hi,

I am working on a small optimization feature to replace the calls with indirect reference using a memory with an indirect reference using register. The purpose of this feature is to improve the performance of calls to functions referred to by function pointers. The motivation behind this work is that gcc does this optimization.

Here is a small test case, that will generate an indirect call with memory reference:

int main()

{

extern void (*foo)();

foo();

return 0;

}

And the corresponding assembly output is:

main: # @main

.cfi_startproc

BB#0: # %entry

pushq %rax

.Ltmp1:

.cfi_def_cfa_offset 16

movl $2, %edi

callq *foo(%rip)

xorl %eax, %eax

popq %rdx

ret

The patch aims to make the memory reference to use a register reference by moving the address in *foo(%rip) into a register and use it for the call. The updated assembly output is:

main: # @main

.cfi_startproc

BB#0: # %entry

pushq %rax

.Ltmp1:

.cfi_def_cfa_offset 16

movq foo(%rip), %rax

movl $0, 4(%rsp)

callq *%rax

xorl %eax, %eax

popq %rdx

ret

However, I am unable to proceed with this fix because it modifies the SelectionDAG to insert a CopyFromReg and a CopyToReg nodes to do this operation. I tried to use a slightly modified test case, using arguments to function instead of void, and it fails. Here is the modified test case:

int main()

{

extern void (*foo)(int);

foo(2);

return 0;

}

And the problem is that I am seeing an assertion failure with respect to the DAG structure.

.file “”

llc: ~/llvm/lib/CodeGen/ScheduleDAG.cpp:510: void llvm::ScheduleDAGTopologicalSort::InitDAGTopologicalSorting(): Assertion `Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] && “Wrong topological sorting”’ failed.

0 llc 0x000000000117ef3a llvm::sys::PrintStackTrace(_IO_FILE*) + 38

I am wondering if the modification made to the DAG is causing a problem, and can it be done at all? If I cannot do this, is there any other place I can look at, to make this work.

call_indirect_reg.patch (3.87 KB)

Hello

I am wondering if the modification made to the DAG is causing a problem, and
can it be done at all? If I cannot do this, is there any other place I can
look at, to make this work.

It's hard to tell w/o seeing the exact code / DAG. Note, however, that
this assertion is seen on simple LLVM IR:
http://llvm.org/bugs/show_bug.cgi?id=15053

So, it might be not your bug after all.

Hi Anton,
I am attaching the Selection DAG before legalize stage (pre and post application of the patch). The test case with one int argument (fn_ptr_int_..) fails and the test case with no argument( fn_ptr_void...) doesn’t error out. I also attached the LLVM IR for the test cases if that might be of some help.

Thanks
Ram

fn_ptr_int_post.ll (846 Bytes)

fn_ptr_void_post.ll (835 Bytes)