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
Changes from 2 commits
159f3ca
2f6ceb3
ff3087a
7bfda74
e8a930b
aee3a8b
cceb061
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -397,6 +397,22 @@ 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) { | ||||||||
if (phase->type(in1->in(1)) == TypeInt::MINUS_1) { | ||||||||
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(2)); | ||||||||
} else if (phase->type(in1->in(2)) == TypeInt::MINUS_1) { | ||||||||
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1)); | ||||||||
} | ||||||||
} else if (op2 == Op_XorI && phase->type(in1) == TypeInt::ONE) { | ||||||||
if (phase->type(in2->in(1)) == TypeInt::MINUS_1) { | ||||||||
return new SubINode(phase->makecon(TypeInt::ZERO), in2->in(2)); | ||||||||
} else if (phase->type(in2->in(2)) == TypeInt::MINUS_1) { | ||||||||
return new SubINode(phase->makecon(TypeInt::ZERO), in2->in(1)); | ||||||||
} | ||||||||
} | ||||||||
return AddNode::Ideal(phase, can_reshape); | ||||||||
} | ||||||||
|
||||||||
@@ -554,7 +570,22 @@ 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) { | ||||||||
if (phase->type(in1->in(1)) == TypeLong::MINUS_1) { | ||||||||
return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(2)); | ||||||||
} else if (phase->type(in1->in(2)) == TypeLong::MINUS_1) { | ||||||||
return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(1)); | ||||||||
} | ||||||||
} else if (op2 == Op_XorL && phase->type(in1) == TypeLong::ONE) { | ||||||||
if (phase->type(in2->in(1)) == TypeLong::MINUS_1) { | ||||||||
return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(2)); | ||||||||
} else if (phase->type(in2->in(2)) == TypeLong::MINUS_1) { | ||||||||
return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(1)); | ||||||||
} | ||||||||
} | ||||||||
return AddNode::Ideal(phase, can_reshape); | ||||||||
} | ||||||||
|
||||||||
@@ -967,6 +998,26 @@ const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const { | ||||||||
} | ||||||||
|
||||||||
//============================================================================= | ||||||||
Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) { | ||||||||
Node* in1 = in(1); | ||||||||
Node* in2 = in(2); | ||||||||
int op1 = in1->Opcode(); | ||||||||
int op2 = in2->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 -1^(x+(-1)). | ||||||||
if (op1 == Op_AddI && phase->type(in2) == TypeInt::MINUS_1) { | ||||||||
if (phase->type(in1->in(2)) == TypeInt::MINUS_1) { | ||||||||
return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1)); | ||||||||
} | ||||||||
} else if (op2 == Op_AddI && phase->type(in1) == TypeInt::MINUS_1) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to check both inputs for constant -1? Shouldn't jdk/src/hotspot/share/opto/addnode.hpp Lines 52 to 54 in 599d07c
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, Changed. |
||||||||
if (phase->type(in2->in(2)) == TypeInt::MINUS_1) { | ||||||||
return new SubINode(phase->makecon(TypeInt::ZERO), in2->in(1)); | ||||||||
} | ||||||||
} | ||||||||
return AddNode::Ideal(phase, can_reshape); | ||||||||
} | ||||||||
|
||||||||
const Type* XorINode::Value(PhaseGVN* phase) const { | ||||||||
Node* in1 = in(1); | ||||||||
@@ -1032,6 +1083,27 @@ 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(); | ||||||||
int op2 = in2->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 -1^(x+(-1)). | ||||||||
if (op1 == Op_AddL && phase->type(in2) == TypeLong::MINUS_1) { | ||||||||
if (phase->type(in1->in(2)) == TypeLong::MINUS_1) { | ||||||||
return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(1)); | ||||||||
} | ||||||||
} else if (op2 == Op_AddL && phase->type(in1) == TypeLong::MINUS_1) { | ||||||||
if (phase->type(in2->in(2)) == TypeLong::MINUS_1) { | ||||||||
return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(1)); | ||||||||
} | ||||||||
} | ||||||||
return AddNode::Ideal(phase, can_reshape); | ||||||||
} | ||||||||
|
||||||||
const Type* XorLNode::Value(PhaseGVN* phase) const { | ||||||||
Node* in1 = in(1); | ||||||||
Node* in2 = in(2); | ||||||||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test needs |
||
* @library /test/lib | ||
* @run main/othervm -XX:-Inline -XX:-TieredCompilation -XX:TieredStopAtLevel=4 -XX:CompileCommand=compileonly,compiler.c2.TestAddXorIdeal::* compiler.c2.TestAddXorIdeal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed. Magic number have been substituted by random number. |
||
*/ | ||
package compiler.c2; | ||
|
||
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 void main(String... args) { | ||
for (int i = -5_000; i < 5_000; i++) { | ||
Asserts.assertTrue(test1(i + 5) == -(i + 5)); | ||
Asserts.assertTrue(test2(i - 7) == -(i - 7)); | ||
Asserts.assertTrue(test3(i + 100) == -(i + 100)); | ||
Asserts.assertTrue(test4(i - 1024) == -(i - 1024)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about using random numbers for better coverage? |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two conditions could be combined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I've combined these conditions.