Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8273021: C2: Improve Add and Xor ideal optimizations #5266

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/hotspot/share/opto/addnode.cpp
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/opto/addnode.hpp
Expand Up @@ -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; }
Expand All @@ -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; }
Expand Down
97 changes: 97 additions & 0 deletions test/hotspot/jtreg/compiler/c2/TestAddXorIdeal.java
@@ -0,0 +1,97 @@
/*
* 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
* @bug 8273021
* @summary C2: Improve Add and Xor ideal optimizations
Copy link
Member

Choose a reason for hiding this comment

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

The test needs * @key randomness

* @library /test/lib
* @run main/othervm -XX:-TieredCompilation -XX:TieredStopAtLevel=4
Copy link
Member

Choose a reason for hiding this comment

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

TieredStopAtLevel has no effect if Tiered Compilation is turned off. You can remove it.

* -XX:CompileCommand=dontinline,compiler.c2.TestAddXorIdeal::test*
* compiler.c2.TestAddXorIdeal
*/
package compiler.c2;

import java.util.Random;

import jdk.test.lib.Asserts;

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 = new Random();
Copy link
Member

Choose a reason for hiding this comment

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

You should use Utils.getRandomInstance() from import jdk.test.lib.Utils to ensure that the seed is printed for reproducibility. You can check other tests for an example.

Copy link
Member Author

Choose a reason for hiding this comment

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

That does make sense. Changed.

int n = 0;
long n1 = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Should be declared in the loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean declared within loop body? I've changed but it looks like a perference problem.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, in Java, local variables should be declared as close as possible to the point they are first used at (see, for example, Google's Java Style Guide). The declaration does not affect performance.

Here's how I would write the loop to improve readability:

for (int j = 0; j < 50_000; j++) {
  int i = random.nextInt();
  long l = random.nextLong();
  Asserts.assertTrue(test1(i) == -i);
  Asserts.assertTrue(test2(i) == -i);
  Asserts.assertTrue(test3(l) == -l);
  ...

Summary: No need to use negative initial value for loop induction variable (as it is not even used), increase number of iterations to ensure C2 compilation (you are running without -Xbatch), use same random numbers per loop iteration, use more intuitive variable names.

But these are just code style details, feel free to ignore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for patient. I agree with your comment that using 0 as initial value and increasing iterations. :)

I try to use a and b as variable names since long l looks like long 1

for (int i = -5_000; i < 5_000; i++) {
n = random.nextInt();
Asserts.assertTrue(test1(i + n) == -(i + n));
Copy link
Member

Choose a reason for hiding this comment

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

Now that you are using random numbers, can't you simply check Asserts.assertTrue(test1(n) == -n)? And just loop for a fixed number of iterations.

n = random.nextInt();
Asserts.assertTrue(test2(i - n) == -(i - n));
n1 = random.nextLong();
Asserts.assertTrue(test3(i + n1) == -(i + n1));
n1 = random.nextLong();
Asserts.assertTrue(test4(i - n1) == -(i - n1));
n = random.nextInt();
Asserts.assertTrue(test5(i + n) == -(i + n));
n = random.nextInt();
Asserts.assertTrue(test6(i - n) == -(i - n));
n1 = random.nextLong();
Asserts.assertTrue(test7(i + n1) == -(i + n1));
n1 = random.nextLong();
Asserts.assertTrue(test8(i - n1) == -(i - n1));
}
}
}