Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-03

x86-64: MOVZX peephole optimisations

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

J. Gareth Moreton

Hi everyone,
I'm a tad confused in regards to the best course of action regarding MOVZX.  Many of the peephole optimisations seek to change them to MOV followed by AND (e.g. "movzbl (mem), %eax" to "mov (mem), %al; and $0xff, %eax").  Does MOVZX have a well-documented performance penalty in modern processors that favours the MOV/AND combination?  It seems odd because the combination implies a pipeline stall, which becomes more pronounced if the MOV instruction is reading from memory.
The other issue is that there are some nasty traps in the i386/x86-64 peephole optimisation code because, if you're not careful with conditions, it seems that you can get situations where the peephole optimizer enters an infinite loop as it converts MOVZX to MOV/AND and back again. I bring this up now because it's central to issue #35187. Gareth aka. Kit

Reply by email

J. Gareth Moreton

In that case, I would argue that it should optimise for more modern processors - that is, favour MOVZX, especially as the first few Pentium processors were released in the 90s.  If backwards-compatibility is more of a concern, well, if compiling for x86-64, when it's certain that the processor was released no earlier than the early 2000s (because, for x86_64-win64, SSE2 has to be present) and hence MOVZX is efficient, while i386 and i8086 can favour MOV/AND.

Nevertheless, there is code in parts of the compiler that check for things like "if current_settings.cputype

Reply by email

J. Gareth Moreton

And apologies for my horrendous grammar in that last e-mail!  What I meant to say is that an option could be that x86-64 can favour MOVZX, since the oldest processors are guaranteed to have been first released no earlier than the early 2000s, while i386 and i8086 can favour MOV + AND, unless more conditional checks "if current_settings.cputype

Reply by email

Marģers .
to J. Gareth Moreton: I'm a tad confused in regards to the best course of action regarding MOVZX.  Many of the…
I'm a tad confused in regards to the best course

of action regarding MOVZX.  Many of the peephole
optimisations seek to change them to MOV followed
by AND (e.g. "movzbl (mem), %eax" to "mov (mem),
%al; and $0xff, %eax").  Does MOVZX have a
well-documented performance penalty in modern
processors that favours the MOV/AND combination?  It seems odd because the combination implies a
pipeline stall, which becomes more pronounced if
the MOV instruction is reading from memory.

For intel pentium and earlier processors
combination MOV, AND was better, but now days cpu
handle MOVZX as good as MOV. It's just question
for which cpu to optimize?

Reply by email