[Patch] x86: LEA/SHL -> LEA optimisation
Original Reporter info from Mantis: CuriousKit @CuriousKit
-
Reporter name: J. Gareth Moreton
Original Reporter info from Mantis: CuriousKit @CuriousKit
- Reporter name: J. Gareth Moreton
Description:
This patch, besides some rearranging to factor out GetNextInstruction calls in OptPass1LEA and OptPass2LEA, introduces a new optimisation that occasionally appears around memory operations:
lea $x(%reg1),%reg2
...
shl $y,%reg2
If y <= 3, abs(x*2^y) doesn't exceed $7FFFFFFF, %reg2 is not used in between (GetNextInstructionUsingReg is called) and the FLAGS register is not in use, this pair is converted into:
lea $x*2^y,%reg2
For example:
leaq 1(%rbx),%rdi
shlq $3,%rdi
Becomes...
leaq 8(%rbx,8),%rdi
Steps to reproduce:
Apply patch and confirm correct compilation. Optimisation appears a handful of times in the compiled Classes unit if an assembly comparison is needed.
Additional information:
Based on the observation that (A + 1) shl 3, for example, is equal to (A + 1) * 8 = (A * 8) + 8. Because multiplication is used, there is no chance of a rounding error. The checking of the FLAGS register ensures that zero or carry conditions aren't checked after the call to SHL.
Patch is quite large because there's a fair few tab changes, and some changing of [0,1] to <= 1 for more efficient code. There should be no other change in functionality.
Mantis conversion info:
- Mantis ID: 37954
- OS: Microsoft Windows`
- OS Build: 10 Home
- Build: r47124
- Platform: i386 and x86_64
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 47143 (#8bbc7d5d)