Skip to content
Permalink
Browse files
8273021: C2: Improve Add and Xor ideal optimizations
Co-authored-by: yulei <lei.yul@alibaba-inc.com>
Reviewed-by: thartmann, kvn
  • Loading branch information
Yi Yang and yuleil committed Sep 13, 2021
1 parent 9f86082 commit a73c06de2ac47033503189140c0f8ee61fcbceae
Showing 3 changed files with 137 additions and 1 deletion.
@@ -397,6 +397,13 @@ Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
}
}

// Convert (~x+1) into -x. Note there isn't a bitwise not bytecode,
// "~x" would typically represented as "x^(-1)", so (~x+1) will
// be (x^(-1))+1.
if (op1 == Op_XorI && phase->type(in2) == TypeInt::ONE &&
phase->type(in1->in(2)) == TypeInt::MINUS_1) {
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1));
}
return AddNode::Ideal(phase, can_reshape);
}

@@ -554,7 +561,13 @@ Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
}
}


// Convert (~x+1) into -x. Note there isn't a bitwise not bytecode,
// "~x" would typically represented as "x^(-1)", so (~x+1) will
// be (x^(-1))+1
if (op1 == Op_XorL && phase->type(in2) == TypeLong::ONE &&
phase->type(in1->in(2)) == TypeLong::MINUS_1) {
return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(1));
}
return AddNode::Ideal(phase, can_reshape);
}

@@ -967,6 +980,21 @@ const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
}

//=============================================================================
//------------------------------Idealize---------------------------------------
Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
Node* in1 = in(1);
Node* in2 = in(2);
int op1 = in1->Opcode();
// 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));
}
return AddNode::Ideal(phase, can_reshape);
}

const Type* XorINode::Value(PhaseGVN* phase) const {
Node* in1 = in(1);
@@ -1032,6 +1060,21 @@ const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
return TypeLong::make( r0->get_con() ^ r1->get_con() );
}

Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
Node* in1 = in(1);
Node* in2 = in(2);
int op1 = in1->Opcode();
// 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_AddL && phase->type(in2) == TypeLong::MINUS_1 &&
phase->type(in1->in(2)) == TypeLong::MINUS_1) {
return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(1));
}
return AddNode::Ideal(phase, can_reshape);
}

const Type* XorLNode::Value(PhaseGVN* phase) const {
Node* in1 = in(1);
Node* in2 = in(2);
@@ -227,6 +227,7 @@ class XorINode : public AddNode {
public:
XorINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
virtual int Opcode() const;
virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
virtual const Type *add_ring( const Type *, const Type * ) const;
virtual const Type *add_id() const { return TypeInt::ZERO; }
virtual const Type *bottom_type() const { return TypeInt::INT; }
@@ -242,6 +243,7 @@ class XorLNode : public AddNode {
public:
XorLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
virtual int Opcode() const;
virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
virtual const Type *add_ring( const Type *, const Type * ) const;
virtual const Type *add_id() const { return TypeLong::ZERO; }
virtual const Type *bottom_type() const { return TypeLong::LONG; }
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2021, Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

/*
* @test
* @key randomness
* @bug 8273021
* @summary C2: Improve Add and Xor ideal optimizations
* @library /test/lib
* @run main/othervm -XX:-TieredCompilation
* -XX:CompileCommand=dontinline,compiler.c2.TestAddXorIdeal::test*
* compiler.c2.TestAddXorIdeal
*/
package compiler.c2;

import java.util.Random;

import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;

public class TestAddXorIdeal {

public static int test1(int x) {
return ~x + 1;
}

public static int test2(int x) {
return ~(x - 1);
}

public static long test3(long x) {
return ~x + 1L;
}

public static long test4(long x) {
return ~(x - 1L);
}

public static int test5(int x) {
return 1 + ~x;
}

public static int test6(int x) {
return ~(-1 + x);
}

public static long test7(long x) {
return 1L + ~x;
}

public static long test8(long x) {
return ~(-1L + x);
}

public static void main(String... args) {
Random random = Utils.getRandomInstance();
for (int i = 0; i < 50_000; i++) {
int a = random.nextInt();
long b = random.nextLong();
Asserts.assertTrue(test1(a) == -a);
Asserts.assertTrue(test2(a) == -a);
Asserts.assertTrue(test3(b) == -b);
Asserts.assertTrue(test4(b) == -b);
Asserts.assertTrue(test5(a) == -a);
Asserts.assertTrue(test6(a) == -a);
Asserts.assertTrue(test7(b) == -b);
Asserts.assertTrue(test8(b) == -b);
}
}
}

5 comments on commit a73c06d

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on a73c06d Sep 13, 2021

Choose a reason for hiding this comment

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

@kelthuzadx
Copy link
Member

@kelthuzadx kelthuzadx commented on a73c06d Nov 1, 2021

Choose a reason for hiding this comment

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

/backport jdk17u

@openjdk
Copy link

@openjdk openjdk bot commented on a73c06d Nov 1, 2021

Choose a reason for hiding this comment

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

@kelthuzadx @kelthuzadx the backport was successfully created on the branch kelthuzadx-backport-a73c06de in my personal fork of openjdk/jdk17u. To create a pull request with this backport targeting openjdk/jdk17u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

this pull request contains a backport of commit a73c06de from the openjdk/jdk repository.

The commit being backported was authored by Yi Yang on 13 Sep 2021 and was reviewed by Tobias Hartmann and Vladimir Kozlov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u:

$ git fetch https://github.com/openjdk-bots/jdk17u kelthuzadx-backport-a73c06de:kelthuzadx-backport-a73c06de
$ git checkout kelthuzadx-backport-a73c06de
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u kelthuzadx-backport-a73c06de

@kelthuzadx
Copy link
Member

@kelthuzadx kelthuzadx commented on a73c06d Nov 5, 2021

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on a73c06d Nov 5, 2021

Choose a reason for hiding this comment

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

@kelthuzadx Could not automatically backport a73c06de to openjdk/jdk11u-dev due to conflicts in the following files:

  • src/hotspot/share/opto/addnode.cpp

To manually resolve these conflicts run the following commands in your personal fork of openjdk/jdk11u-dev:

$ git checkout -b kelthuzadx-backport-a73c06de
$ git fetch --no-tags https://git.openjdk.java.net/jdk a73c06de2ac47033503189140c0f8ee61fcbceae
$ git cherry-pick --no-commit a73c06de2ac47033503189140c0f8ee61fcbceae
$ # Resolve conflicts
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport a73c06de2ac47033503189140c0f8ee61fcbceae'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk11u-dev with the title Backport a73c06de2ac47033503189140c0f8ee61fcbceae.

Please sign in to comment.