Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

8288564: C2: LShiftLNode::Ideal produces wrong result after JDK-8278114 #29

Closed
wants to merge 3 commits into from

Conversation

chhagedorn
Copy link
Member

@chhagedorn chhagedorn commented Jun 16, 2022

JDK-8278114 added the following transformation for integer and long left shifts:

 "(x + x) << c0" into "x << (c0 + 1)"

However, in the long shift case, this transformation is not correct if c0 is 63:

(x + x) << 63 = 2x << 63

while

 (x + x) << 63 --transform--> x << 64 = x << 0 = x

which is not the same. For example, if x = 1:

2x << 63 = 2 << 63 = 0 != 1

This optimization does not account for the fact that x << 64 is the same as x << 0 = x. According to the Java spec, chapter 15.19, we only consider the six lowest-order bits of the right-hand operand (i.e. "right-hand operand" & 0b111111). Therefore, x << 64 is the same as x << 0 (64 = 0b10000000 & 0b0111111 = 0).

Integer shifts are not affected because we do not apply this transformation if c0 >= 16:

// Transform is legal, but check for profit. Avoid breaking 'i2s'
// and 'i2b' patterns which typically fold into 'StoreC/StoreB'.
if( con < 16 ) {
// Left input is an add of the same number?
if (add1->in(1) == add1->in(2)) {
// Convert "(x + x) << c0" into "x << (c0 + 1)"
return new LShiftINode(add1->in(1), phase->intcon(con + 1));
}

The fix I propose is to not apply this optimization for long left shifts if c0 == 63. I've added an additional sanity assertion for integer left shifts just in case this optimization is moved at some point and ending up outside the check for con < 16.

Thanks,
Christian


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8288564: C2: LShiftLNode::Ideal produces wrong result after JDK-8278114

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk19 pull/29/head:pull/29
$ git checkout pull/29

Update a local copy of the PR:
$ git checkout pull/29
$ git pull https://git.openjdk.org/jdk19 pull/29/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 29

View PR using the GUI difftool:
$ git pr show -t 29

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk19/pull/29.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 16, 2022

👋 Welcome back chagedorn! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 16, 2022
@openjdk
Copy link

openjdk bot commented Jun 16, 2022

@chhagedorn The following label will be automatically applied to this pull request:

  • hotspot-compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Jun 16, 2022
@mlbridge
Copy link

mlbridge bot commented Jun 16, 2022

Webrevs

Copy link

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. Just one comment.

@@ -813,6 +813,7 @@ Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
// Left input is an add of the same number?
if (add1->in(1) == add1->in(2)) {
// Convert "(x + x) << c0" into "x << (c0 + 1)"
assert(con != BitsPerJavaInteger - 1, "sanity check, optimization cannot be applied for con == 31");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert is useless because there is check at line 812 `(con < 16).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true. I just wanted to make sure that we do not forget about the fact that we cannot apply this optimization for con == 31 if this optimization is moved at some point or we decide to remove the con < 16 restriction. But I guess I can also just add a comment instead.

@openjdk
Copy link

openjdk bot commented Jun 16, 2022

@chhagedorn This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8288564: C2: LShiftLNode::Ideal produces wrong result after JDK-8278114

Reviewed-by: kvn, iveresov, thartmann

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 7 new commits pushed to the master branch:

  • ae030bc: 8288397: AArch64: Fix register issues in SVE backend match rules
  • f12d044: 8288692: jdk/javadoc/doclet/testTagMisuse/TestTagMisuse.java fails after JDK-8288545
  • 97544be: 8268398: 15% increase in JFR footprint in Noop-Base
  • 983f75c: 8288545: Missing space in error message
  • 53bf1bf: 8286176: Add JNI_VERSION_19 to jni.h and JNI spec
  • c254c9d: 8287401: jpackage tests failing on Windows due to powershell issue
  • ff3db52: 8288534: Out of bound errors for memory segment access mentions wrong values

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch.
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 16, 2022
Copy link

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

@chhagedorn
Copy link
Member Author

Thanks Vladimir and Igor for your reviews!

Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice analysis. Looks good.

@chhagedorn
Copy link
Member Author

Thanks Tobias for your review!

/integrate

@openjdk
Copy link

openjdk bot commented Jun 20, 2022

Going to push as commit ed714af.
Since your change was applied there have been 7 commits pushed to the master branch:

  • ae030bc: 8288397: AArch64: Fix register issues in SVE backend match rules
  • f12d044: 8288692: jdk/javadoc/doclet/testTagMisuse/TestTagMisuse.java fails after JDK-8288545
  • 97544be: 8268398: 15% increase in JFR footprint in Noop-Base
  • 983f75c: 8288545: Missing space in error message
  • 53bf1bf: 8286176: Add JNI_VERSION_19 to jni.h and JNI spec
  • c254c9d: 8287401: jpackage tests failing on Windows due to powershell issue
  • ff3db52: 8288534: Out of bound errors for memory segment access mentions wrong values

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 20, 2022
@openjdk openjdk bot closed this Jun 20, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 20, 2022
@openjdk
Copy link

openjdk bot commented Jun 20, 2022

@chhagedorn Pushed as commit ed714af.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated
4 participants