Skip to content

C2: Reuse cmp condition codes in adjacent basic blocks#45

Draft
raneashay wants to merge 2 commits intomicrosoft:mainfrom
raneashay:JDK-8256951-reuse-cmp-cc
Draft

C2: Reuse cmp condition codes in adjacent basic blocks#45
raneashay wants to merge 2 commits intomicrosoft:mainfrom
raneashay:JDK-8256951-reuse-cmp-cc

Conversation

@raneashay
Copy link

This PR contains two commits:

8256951: C2: Reuse cmp condition codes in adjacent basic blocks

C2 always emits instructions for evaluating the condition prior to the
branch that uses it, but when the code contains a chain of if-else
statements whose predicates compare the same two values or if the code
contains a switch-case statement, the generated assembly often includes
repeated instructions that compare the same two values prior to each
branch, even if the condition codes between the comparison instructions
have not changed.

This patch adds a peephole optimization to the X64 backend to recognize
such redundant instructions and to remove the instruction in the
successor block when the condition codes can be reused. C2 applies this
optimization after conservative checks (single predecessor block, no
intermediate instructions that modify condition codes, and identical
instructions). This patch enables the peephole opimization for signed
and unsigned, and integer and long comparsion instructions.

This optimization should also be applicable to float and double
comparison instructions on X64, but this patch does not enable the
optimization for those instructions, since writing tests for those cases
is made difficult by a different optimization that rewrites one of the
two comparison instruction with swapped operands, causing this patch's
conservative equivalence checks between instructions to fail, thus
skipping the optimization.

and

Extend peephole optimization to AArch64 target

This patch rehouses the peephole optimization from the previous patch so
that it can be enabled on both X64 and AArch64 targets. Specifically,
this patch introduces a new class (WindowOpt) that contains the logic
for the peephole optimization, so that the target-specific Peephole
class implementation can call into the newly-introduced WindowOpt's
method.

Since Peephole optimizations were blanket disabled on the AArch64 target
prior to this patch, this patch makes a few cursory to changes to enable
them. First, this patch updates c2_globals_aarch64.hpp to enable
OptoPeephole and OptoScheduling by default. Second,
peephole_aarch64.{cpp,hpp} introduces a Peephole class that is
compiled only on the AArch64 target. Finally, this newly introduced
Peephole class calls into the method in WindowOpt that works for
both X64 and AArch64 targets.

As a final change, this patch updates the IR tests introduced in the
previous patch to now run on both X64 and AArch64 targets. Local test
runs show that all Tier 1, Tier 2, and Tier 3 HotSpot tests pass in both
Release and FastDebug build configurations.

C2 always emits instructions for evaluating the condition prior to the
branch that uses it, but when the code contains a chain of if-else
statements whose predicates compare the same two values or if the code
contains a switch-case statement, the generated assembly often includes
repeated instructions that compare the same two values prior to each
branch, even if the condition codes between the comparison instructions
have not changed.

This patch adds a peephole optimization to the X64 backend to recognize
such redundant instructions and to remove the instruction in the
successor block when the condition codes can be reused.  C2 applies this
optimization after conservative checks (single predecessor block, no
intermediate instructions that modify condition codes, and identical
instructions).  This patch enables the peephole opimization for signed
and unsigned, and integer and long comparsion instructions.

This optimization should also be applicable to float and double
comparison instructions on X64, but this patch does not enable the
optimization for those instructions, since writing tests for those cases
is made difficult by a different optimization that rewrites one of the
two comparison instruction with swapped operands, causing this patch's
conservative equivalence checks between instructions to fail, thus
skipping the optimization.
This patch rehouses the peephole optimization from the previous patch so
that it can be enabled on both X64 and AArch64 targets.  Specifically,
this patch introduces a new class (`WindowOpt`) that contains the logic
for the peephole optimization, so that the target-specific `Peephole`
class implementation can call into the newly-introduced `WindowOpt`'s
method.

Since Peephole optimizations were blanket disabled on the AArch64 target
prior to this patch, this patch makes a few cursory to changes to enable
them.  First, this patch updates c2_globals_aarch64.hpp to enable
`OptoPeephole` and `OptoScheduling` by default.  Second,
peephole_aarch64.{cpp,hpp} introduces a `Peephole` class that is
compiled only on the AArch64 target.  Finally, this newly introduced
`Peephole` class calls into the method in `WindowOpt` that works for
both X64 and AArch64 targets.

As a final change, this patch updates the IR tests introduced in the
previous patch to now run on both X64 and AArch64 targets.  Local test
runs show that all Tier 1, Tier 2, and Tier 3 HotSpot tests pass in both
Release and FastDebug build configurations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant