[LLVMdev] Calling with register indirect reference instead of memory indirect reference.

Murali, Sriram sriram.murali at intel.com
Thu Feb 28 10:26:29 PST 2013


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         "<stdin>"
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.
--
Sriram Murali
SSG/DPD/ECDL/DMP
+1 (519) 772 - 2579

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130228/426de877/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: call_indirect_reg.patch
Type: application/octet-stream
Size: 3966 bytes
Desc: call_indirect_reg.patch
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130228/426de877/attachment.obj>


More information about the llvm-dev mailing list