[fpc-devel] Producing assembly with less branches?

Florian Klämpfl florian at freepascal.org
Sun Jul 19 21:20:09 CEST 2020


Am 19.07.20 um 19:24 schrieb Stefan Glienke:
> Hi,
> 
> not sure if anything significantly changed in trunk compared to 3.2 wrt 
> to optimized code being generated but I am quite disappointed that fpc 
> (checked win64 with -O3 and -O4) does not use cmovxx instructions and 
> alike for the most basic things and produces terrible code like this:

cmov does not support constant operands, so FPC does not use it.


> 
> unit1.pas:49                              if left < right then
> 000000010002E3C0 39ca                     cmp    edx,ecx
> 000000010002E3C2 7e06                     jle    0x10002e3ca 
> <COMPAREINT+10>
> unit1.pas:50                              Result := -1
> 000000010002E3C4 b8ffffffff               mov    eax,0xffffffff
> 000000010002E3C9 c3                       ret
> unit1.pas:51                              else if left > right then
> 000000010002E3CA 39ca                     cmp    edx,ecx
> 000000010002E3CC 7d06                     jge    0x10002e3d4 
> <COMPAREINT+20>
> unit1.pas:52                              Result := 1
> 000000010002E3CE b801000000c3             mov    eax,0x1
> unit1.pas:54                              Result := 0;
> 000000010002E3D4 31c0                     xor    eax,eax
> unit1.pas:55                              end;
> 000000010002E3D6 c3                       ret
> 
> Similar for even simpler things:
> 
> unit1.pas:43                              if i < 0 then
> 000000010002E3A1 85c0                     test   eax,eax
> 000000010002E3A3 7d03                     jge    0x10002e3a8 
> <BUTTON1CLICK+72>
> unit1.pas:44                              i := 0;
> 000000010002E3A5 31c0                     xor    eax,eax
> 000000010002E3A7 90                       nop
> 
> Imo someone should work at that and make the compiler produce less 
> branches. Not sure if that is on your list but it should be looked at.

It might be in the list which contains enough work for >10 years :)


More information about the fpc-devel mailing list