[fpc-devel] x86: Efficiency of opposing CMOVs

J. Gareth Moreton gareth at moreton-family.com
Sat Apr 16 06:49:07 CEST 2022

Hi everyone,

In the x86_64 assembly dumps, I frequently come across combinations such 
as the following:

     cmpl    %ebx,%edx
     cmovll    %ebx,%eax
     cmovnll    %edx,%eax

This is essentially the tertiary C operator "x = cond ? trueval : 
falseval", or in Pascal "if (cond) then x := trueval else x := 
falseval;".  However, because the CMOV instructions have exact opposite 
conditions, is it better to optimise it into this?

     movl    %ebx,%eax
     cmpl    %ebx,%edx
     cmovnll    %edx,%eax

It's smaller, but is it actually faster (or the same speed)?  At the 
very least, the two CMOV instructions depend on the CMP instruction 
being completed, but I'm not sure if the second CMOV depends on the 
first one being evaluated (because of %eax).  With the second block of 
code, the MOV and CMP instructions can execute simultaneously.

My educated guess tells me that MOV/CMP/CMOV(~c) is faster than 
CMP/CMOVc/CMOV(~c), but I haven't been able to find an authoritive 
source on this yet.

Gareth aka. Kit

This email has been checked for viruses by Avast antivirus software.

More information about the fpc-devel mailing list