Skip to content

Conversation

CptGit
Copy link
Contributor

@CptGit CptGit commented Dec 16, 2021

Hi all,

Existing optimization ~x+1 -> -x can be generalized to ~x+c -> (c-1)-x. I included both microbenchmark and jtreg tests.

// Convert (~x+c) into (c-1)-x. Note there isn't a bitwise not
// bytecode, "~x" would typically represented as "x^(-1)", so (~x+c)
// will be (x^(-1))+c.
if (op1 == Op_Xor(bt) &&
    (in2->Opcode() == Op_ConI || in2->Opcode() == Op_ConL) &&
    phase->type(in1->in(2)) == TypeInteger::minus_1(bt)) {
  Node* c_minus_one = phase->makecon(add_ring(phase->type(in(2)), TypeInteger::minus_1(bt)));
  return SubNode::make(c_minus_one, in1->in(1), bt);
}

Thank you for reviewing.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8279607: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x".

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6858/head:pull/6858
$ git checkout pull/6858

Update a local copy of the PR:
$ git checkout pull/6858
$ git pull https://git.openjdk.java.net/jdk pull/6858/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 6858

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6858.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 16, 2021

👋 Welcome back CptGit! 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
Copy link

openjdk bot commented Dec 16, 2021

@CptGit 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.

@CptGit CptGit changed the title Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x" [TBD]: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x" Dec 16, 2021
@CptGit CptGit marked this pull request as draft December 18, 2021 03:26
@CptGit CptGit marked this pull request as ready for review December 19, 2021 00:37
@CptGit
Copy link
Contributor Author

CptGit commented Dec 19, 2021

Attach benchmark results:

Baseline:

Benchmark                       Mode  Cnt  Score   Error  Units
AddIdealNotXPlusC.baselineInt   avgt   60  1.479 ± 0.004  ns/op
AddIdealNotXPlusC.baselineLong  avgt   60  1.479 ± 0.006  ns/op
AddIdealNotXPlusC.testInt       avgt   60  3.142 ± 0.019  ns/op
AddIdealNotXPlusC.testLong      avgt   60  3.150 ± 0.019  ns/op

Patch:

Benchmark                       Mode  Cnt  Score   Error  Units
AddIdealNotXPlusC.baselineInt   avgt   60  1.489 ± 0.008  ns/op
AddIdealNotXPlusC.baselineLong  avgt   60  1.488 ± 0.007  ns/op
AddIdealNotXPlusC.testInt       avgt   60  0.343 ± 0.001  ns/op
AddIdealNotXPlusC.testLong      avgt   60  0.343 ± 0.001  ns/op

@CptGit CptGit changed the title [TBD]: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x" 8279607: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x" Jan 7, 2022
@openjdk openjdk bot changed the title 8279607: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x" 8279607: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x". Jan 7, 2022
@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 7, 2022
@mlbridge
Copy link

mlbridge bot commented Jan 7, 2022

Webrevs

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.

Isn't the same applicable to the corresponding XoINode and XorLNode optimizations?

// Convert ~(x-1) into -x. Note there isn't a bitwise not bytecode,
// "~x" would typically represented as "x^(-1)", and "x-c0" would
// convert into "x+ -c0" in SubXNode::Ideal. So ~(x-1) will eventually
// be (x+(-1))^-1.
if (op1 == Op_AddI && phase->type(in2) == TypeInt::MINUS_1 &&
phase->type(in1->in(2)) == TypeInt::MINUS_1) {
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1));

@CptGit
Copy link
Contributor Author

CptGit commented Jan 15, 2022

Isn't the same applicable to the corresponding XoINode and XorLNode optimizations?

// Convert ~(x-1) into -x. Note there isn't a bitwise not bytecode,
// "~x" would typically represented as "x^(-1)", and "x-c0" would
// convert into "x+ -c0" in SubXNode::Ideal. So ~(x-1) will eventually
// be (x+(-1))^-1.
if (op1 == Op_AddI && phase->type(in2) == TypeInt::MINUS_1 &&
phase->type(in1->in(2)) == TypeInt::MINUS_1) {
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1));

Great catch, thanks. I included similar transformation for XorINode and XorLNode.

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.

That looks good to me and all testing passed.

You need a second review.

@TobiHartmann
Copy link
Member

/reviewers 2

@openjdk
Copy link

openjdk bot commented Jan 19, 2022

@CptGit 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:

8279607: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x".

Reviewed-by: thartmann, kvn

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 155 new commits pushed to the master branch:

  • 84fa0d8: 8190264: JScrollBar ignores its border when using macOS Mac OS X Aqua look and feel
  • 610a129: 8268831: Improve javadoc tool handling of streams.
  • e20c6bf: 8280189: JFR: TestPrintXML should print mismatching XML
  • b20b11c: 8258240: make vscode-project on Windows generates jdk.code-workspace file with unescaped '' in paths
  • 9611431: 8279936: Change shared code to use os:: system API's
  • cc2f474: 8280024: Parallel: Remove unnecessary region resizing methods in PSCardTable
  • 8931c12: 8280157: wrong texts Falied in a couple of tests
  • 68b40ec: 8273139: C2: assert(f <= 1 && f >= 0) failed: Incorrect frequency
  • 39b1d75: 8277822: Remove debug-only heap overrun checks in os::malloc and friends
  • 5af7f25: 8274811: Remove superfluous use of boxing in java.base
  • ... and 145 more: https://git.openjdk.java.net/jdk/compare/4243f4c998344e77dccd4d5605e56e869bc8af89...master

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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@TobiHartmann, @vnkozlov) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

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

openjdk bot commented Jan 19, 2022

@TobiHartmann
The number of required reviews for this PR is now set to 2 (with at least 1 of role reviewers).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jan 19, 2022
@CptGit
Copy link
Contributor Author

CptGit commented Jan 19, 2022

That looks good to me and all testing passed.

You need a second review.

Thanks for reviewing.

Copy link
Contributor

@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.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 19, 2022
@CptGit
Copy link
Contributor Author

CptGit commented Jan 19, 2022

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jan 19, 2022
@openjdk
Copy link

openjdk bot commented Jan 19, 2022

@CptGit
Your change (at version 4285a23) is now ready to be sponsored by a Committer.

@TobiHartmann
Copy link
Member

/sponsor

@openjdk
Copy link

openjdk bot commented Jan 20, 2022

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

  • e683d4a: 8279921: Dump the .class file in jlink debug mode for any failure during transform() of a plugin
  • 1022cbd: 8280047: Broken link to Swing Connection document from javax.swing package docs
  • c4a624d: 8279894: javax/swing/JInternalFrame/8020708/bug8020708.java timeouts on Windows 11
  • 4616c13: Merge
  • 03680be: 8280233: Temporarily disable Unix domain sockets in Windows PipeImpl
  • be0538d: 8278834: Error "Cannot read field "sym" because "this.lvar[od]" is null" when compiling
  • f5de6fa: 8272058: 25 Null pointer dereference defect groups in 4 files
  • 28e02fa: 8280234: AArch64 "core" variant does not build after JDK-8270947
  • f37bfea: 8280155: [PPC64, s390] frame size checks are not yet correct
  • 69cfa9c: 8273383: vmTestbase/vm/gc/containers/Combination05/TestDescription.java crashes verifying length of DCQS
  • ... and 193 more: https://git.openjdk.java.net/jdk/compare/4243f4c998344e77dccd4d5605e56e869bc8af89...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jan 20, 2022

@TobiHartmann @CptGit Pushed as commit 0bf95a1.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-compiler [email protected] integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

3 participants