[fpc-devel] Optimizing unused return values of inline functions

Benito van der Zander benito at benibela.de
Sun Aug 20 11:11:50 CEST 2017


Hi,

why does fpc not remove the calculation of the return value of inline 
functions, when the return value is unused?

For example

type TUtility = class
   function doSomething: TUtility; inline;
end;

function TUtility.dosomething: TUtility;
begin
    writeln();
    result := self;
end;

adds a mov    rax,rdi in the caller function, even if rax is then never 
used.

{
26      begin
    0x00000000004001c0 <+0>:     push   rbx

27        ut := TUtility.Create;
    0x00000000004001c1 <+1>:     movabs rsi,0x1
    0x00000000004001cb <+11>:    lea rdi,[rip+0x2b2fde]        # 
0x6b31b0 <VMT_$WTFINLINE_$$_TUTILITY>
    0x00000000004001d2 <+18>:    call   0x411b60 
<SYSTEM$_$TOBJECT_$__$$_CREATE$$TOBJECT>
    0x00000000004001d7 <+23>:    mov    rbx,rax

28        ut.dosomething;
    0x00000000004001da <+26>:    call   0x41bf20 <fpc_get_output>
    0x00000000004001df <+31>:    mov    rdi,rax
    0x00000000004001e2 <+34>:    call   0x41c100 <fpc_writeln_end>
    0x00000000004001e7 <+39>:    call   0x416170 <fpc_iocheck>
    0x00000000004001ec <+44>:    mov    rdi,rbx
    0x00000000004001ef <+47>:    mov    rax,rdi 
//<----------------------------------------------- here

29        ut.free;
    0x00000000004001f2 <+50>:    call   0x411cd0 
<SYSTEM$_$TOBJECT_$__$$_FREE>

30      end;
    0x00000000004001f7 <+55>:    pop    rbx
    0x00000000004001f8 <+56>:    ret
}


It is a popular pattern to add result := self; to _every_ method (or 
result := @self in objects/records), so it can be chained as 
dosomething().dosomething().dosomething(),.....
but one cannot use it with fpc efficiently, when it inserts too many 
unnecessary instructions



Best,

Benito


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20170820/f4c4ad4f/attachment.html>


More information about the fpc-devel mailing list