Sign in Join

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

Mailing lists / fpc-devel / 2022-06

x86 TEST instruction

6 messages · 4 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 playing around with a peephole optimization (back to my roots!) and I've come across a relatively simple one that aims to reduce instruction size occasionally.  One part involves reducing the opsize if the constant is numeric.  For example:

testl    $17,76(%rcx) -> testb $17,76(%rcx)

Or:

testl    $1,%ebx -> testb    $1,%bl

Obviously I check to make sure the constant fits into the smaller size.  Is there a time when this is NOT recommended?  I'm trying to remember issues with partial register or memory writes, but in this case, it's just a partial read.

Gareth aka. Kit

This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Reply by email

Marc Weustink
to J. Gareth Moreton
J. Gareth Moreton wrote 17 lines
Hi everyone, I'm playing around with a peephole optimization (back to my roots!) and I've come across a relatively simple one that aims to reduce instruction size occasionally.  One part involves reducing the opsize if the constant is numeric.  For example: testl    $17,76(%rcx) -> testb $17,76(%rcx) Or: testl    $1,%ebx -> testb    $1,%bl Obviously I check to make sure the constant fits into the smaller size. Is there a time when this is NOT recommended?  I'm trying to remember issues with partial register or memory writes, but in this case, it's just a partial read.

It's not only the constant size which has to fit, but also the other operand. So you cannot use it for testing an integer for the value of 1

Marc

Reply by email

J. Gareth Moreton
to Thorsten Otto

Yeah, I just realised I was pretty silly to miss that! I must be more tired than I thought!

The reason why I brought it up is because I found an upgrade to the "MovAndTest2Test" optimisation where you might have something like "movl %eax,(memory); andl $1,%eax; testb %al;%al" (Yes, that code sequence does get generated sometimes) and it can be simplified to just "testb $1,(memory)", reading only 1 byte instead of all 4 (the AND instruction would have masked out the upper 3 bytes).

Gareth aka. Kit

Thorsten Otto wrote 15 lines
On Montag, 27. Juni 2022 04:43:46 CEST J. Gareth Moreton via fpc-devel wrote: > testl $1,%ebx -> testb $1,%bl Obviously, thats not the same. The latter will only compare the least-significant byte against 1, and ignore the rest of the register. The former will also compare the remainder of the register against zero. SO you canot do such "optimizations" _______________________________________________ fpc-devel maillist 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

Reply by email

J. Gareth Moreton
to J. Gareth Moreton

This was what I was working on:

https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/252

The same question applies regarding the TEST instruction, but it's a little more restrictive in this instance in that it's determined that the program doesn't care about the upper bytes of the register once the optimisation is made, so "testl $1,%ebx" is indeed the same as "testb $1,%bl" due to previous logic, but I am wondering if a penalty might be incurred due to the partial read or if this is only a concern for partial writes.

Gareth aka. Kit

This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Reply by email

Stefan Glienke
to J. Gareth Moreton

Both clang and gcc seem to do this

Show quoted text 25 lines
On 30/06/2022 17:28 CEST J. Gareth Moreton via fpc-devel wrote: This was what I was working on: https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/252 The same question applies regarding the TEST instruction, but it's a little more restrictive in this instance in that it's determined that the program doesn't care about the upper bytes of the register once the optimisation is made, so "testl $1,%ebx" is indeed the same as "testb $1,%bl" due to previous logic, but I am wondering if a penalty might be incurred due to the partial read or if this is only a concern for partial writes. Gareth aka. Kit -- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus _______________________________________________ fpc-devel maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email