[Patch] OptPass2Jcc Refactor
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 seeks to clean up and improve the OptPass2Jcc routine to minimise repeated calls to GetNextInstruction and shrink overall code size. This is a pure refactor and generated code should not change.
Steps to reproduce:
Apply patch and confirm identical compilation of RTL and other projects on i386 and x86_64 machines.
Additional information:
List of changes:
- All relevant optimisations now within an if-block that cheks that GetNextInstruction(p, hp1) returns True and hp1 is an instruction, thus removing the need to call them for each optimisation.
- The JccAdd/Inc/Dec2CmcAdc/Sbb, JccAdd/Inc/Dec2Adc/Sbb and JccAdd2SetccAdd optimisations are now more efficient in how they check the relevant instructions (namely it doesn't check the operand types and values multiple times).
- On i386, and additionally i8086 (although i8086 doesn't use Pass 2 yet), JccAdd2SetccAdd checks to see if the register is EAX, EBX, ECX or EDX first, since this is a cheaper check and avoids calling GetUsedRegs and paramanager.get_volatile_registers_int for registers that can't have their lowest byte set (ESI, EDI, ESP, EBP).
- JccAdd2SetccAdd now returns True and exits, since p changes from Jcc to SETcc.
- The JccMov2CMov removes an initial call to GetNextInstruction because it already knows what hp1 is.
Possible future improvements:
- A separate utility method for getting a free register, possibly allowing for smarter behaviour in order to permit more optimisations (e.g. allowing non-volatile registers that have been preserved on the stack in the function prologue).
- Improving the cross-platform jump optimisations to recognise RET operations and equivalent on other platforms, seeing them effectively as unconditional jumps. This would allow the removal of the JccJmpRet2J!ccRet optimisation which otherwise follows very simialr principles to the cross-platform jump optimisations (the code path for this optimisation also seems to be very rarely taken).
Miscellaneous:
- Stack size for OptPass2Jcc increases by 4 bytes on i386-win32 and x86_64-win64, but overall code base is smaller.
Mantis conversion info:
- Mantis ID: 38343
- OS: Microsoft Windows
- OS Build: 10 Home
- Build: r48130
- Platform: i386 and x86_64
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 48156 (#9003114a)