Skip to content

x86: new optimisation to change add/sub 128,(dest) to sub/add -128,(dest) to reduce binary size

Summary

This merge request introduces a new post-peephole optimisation for x86 processors that converts additions or subtractions by 128 to the inverse operation by -128. 128 is a fairly common constant, but -128 can be stored in a signed byte, whereas 128 cannot, thus shaving 3 bytes (2 bytes if the register is %eax) off the instruction.

System

  • Processor architecture: i386, x86_64

What is the current bug behavior?

N/A

What is the behavior after applying this patch?

32-bit and 64-bit instructions that compile into "add $128,(dest)" or "sub 128,(dest)" (dest can be a register or memory location) will get changed to "sub $-128,(dest)" and "add $-128,(dest)" respectively.

Relevant logs and/or screenshots

A few random saving are made in the compiler itself, while the RTL contains a couple of savings too.

Additional notes

For some reason, 16-bit instructions don't benefit from the saving because the internal assembler doesn't use the encoding that adds or subtracts a signed 8-bit immediate to a register, while 32-bit and 64-bit instructions don't suffer from this issue. This is possibly down to a problem in how 16-bit operands are handled rather than an instruction encoding issue.

Edited by J. Gareth "Kit" Moreton

Merge request reports