RFR: 8312213: Remove unnecessary TEST instructions on x86 when flags reg will already be set [v2]

Quan Anh Mai qamai at openjdk.org
Fri Aug 4 17:26:32 UTC 2023


On Fri, 4 Aug 2023 09:45:40 GMT, Tobias Hotz <duke at openjdk.org> wrote:

>> Tobias Hotz has updated the pull request incrementally with two additional commits since the last revision:
>> 
>>  - Add IR test
>>    
>>    Currently, the peephole only works for branches, not conditional moves.
>>  - Add assert to verify that the machProj and the test operate on the same register.
>>    
>>    Also fix compilation on macos
>
> The peephole only applies the instruction that would be emitted immediately before the test is the input of the test. If that is the case, it must operate on the same register as the result of the above instruction. We then check the flags of the nodes.
> We need all the information cause specific operation require specific flags. For example, if the instruction following the test checks if the value is greater (than zero), the sign and zero flag need to be set and the overflow flag needs to be cleared. Not all instructions (such as add) satisfy this requirement, so in this case we would need to emit the test, but we could omit it if we only check for zero, as that only requires the ZF, which the test instruction sets.
> 
> 
> I also noticed another problem during the construction of the IR tests: Test instructions before conditional moves are currently not removed. This is due to the Matcher thinking that it needs to load a zero into one register when it emits a setb instruction. This is not the case, but the matcher does not know this and 1) emits a pointless register clear and 2) still has the loadConI0 in its graph, which causes the test peephole to bail out as its input is not the preceeding instruction.
> I think the removal of the loadConI0 in this case is a topic for another PR though. Fixing this will result in the peephole also working for setb instructions, which would be a huge win.
> 
> ![idealgraphvisualizer64_c1r2gDJIi7](https://github.com/openjdk/jdk/assets/20151702/937d779e-1090-40e0-9128-dc172199250f)

@ichttt The register clear via `xor` is mandatory since `setb` only sets the lowest byte. We can be more clever and do more complex transformations such as relaxing consecutiveness requirement, reordering nodes, converting a `xor` into a `mov`, etc but I think it can be achieved in another patch and this patch covers the most prominent appearances of redundant `test`. Thanks a lot.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/14172#issuecomment-1665949667


More information about the hotspot-compiler-dev mailing list