Skip to content

[Patch] AND/CMP optimisation

Original Reporter info from Mantis: CuriousKit @CuriousKit
  • Reporter name: J. Gareth Moreton

Description:

This patch performs the following optimisation:

and 2^n,%reg
cmp 2^n,%reg
j(c) ; where c is E, NE, Z or NZ (it actually allows any conditional instruction, not just Jcc)

Change to:

and 2^n,%reg
test %reg,%reg
j(~c)

The test instruction is left in, not immediately removed, as it may then permit other optimisations like MovAndTest2Test on the next pass.

The constant is tested by checking to see if PopCnt is exactly equal to 1, indicating it is a power of two (I wanted it to be as fast as possible, but this check may take a handful of cycles if the source CPU doesn't support PopCnt - an alternative check would be to calculate "x and (x - 1)"; if equal to zero, then x is a power of 2 (has a PopCnt of 1).

This optimisation improves performance for the common condition "if (Value and Flag) = Flag then".

Steps to reproduce:

Apply patch and confirm correct compilation.

Additional information:

The optimisation isn't applied a lot in the RTL, but it appears a lot in the compiler itself. Some specialised optimisations may perform very slightly worse, but I am making more complex developments around this.

Due to the design of OptPass1AND, the patch reorganises some of the other optimisations so the routine is now built around "case taicpu(hp1).opcode of" as a way to see what instruction after AND is.

Mantis conversion info:

  • Mantis ID: 39287
  • OS: Microsoft Windows
  • OS Build: 10 Home
  • Build: r49632
  • Platform: i386 and x86_64
  • Version: 3.3.1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information