[fpc-devel] x86: Efficiency of opposing CMOVs

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


That answers that.  MOV/CMP/CMOV is faster than CMP/CMOV/CMOV. Thanks.

I've been finalising another peephole optimisation.  What started as a 
means to permit constants in CMOV instructions using a bit of trickery 
became almost an entire rewrite of the optimisation if only because I 
was struggling to remember which temporary variable was set to what 
(where have we heard this before?!).  I'm just running my tests then 
will make a merge request explaining all of the details.  But these are 
the following additions:

* Aligns are now properly removed with the labels around the JccMOV -> 
CMOV optimisations.
* JccMOV -> CMOV will now work with constants in some circumstances 
(although this won't be done under -Os as it adds extra instructions).
* If CMP/JE/MOV/@lbl is found, everything except the MOV is removed if 
CMP and MOV contain the same operands (even if in reverse order), since 
regardless of the result of the condition, the end state is for the 2 
operands (which might be registers, references or constants) to be 
identical (MOV is only called if they're not equal, and if MOV is not 
called, well, they're already equal!), so we might as well just call MOV 
(the optimisation uses MatchOperand to compare the operands, so if 
volatile references are used, the optimisation is not made).

Gareth aka. Kit

P.S. Thanks for the godbolt link Florian.  I just tried "return (a>b) ? 
a : b;" and it compiles to the same thing, using a MOV/CMP/CMOV 
triplet.  In other words, it tries to convert a CMOV to a MOV if it can.

On 16/04/2022 20:03, Florian Klämpfl via fpc-devel wrote:
>
>
>> Am 16.04.2022 um 12:31 schrieb Thorsten Otto via fpc-devel 
>> <fpc-devel at lists.freepascal.org>:
>>
>> On Samstag, 16. April 2022 06:49:07 CEST J. Gareth Moreton via 
>> fpc-devel wrote:
>> > but I haven't been able to find an authoritive
>> > source on this yet.
>>
>> Did you check what for example GCC generates for similar C 
>> constructs? I don't think it uses cmov at all.
>>
>
> Why shouldn’t it? clang does the same as my quick tests recommends: 
> https://godbolt.org/z/eWTvfcM8E
>
>
> _______________________________________________
> fpc-devel maillist  -fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20220416/abd0658f/attachment.htm>


More information about the fpc-devel mailing list