mirror of
https://github.com/openjdk/jdk.git
synced 2026-01-28 12:09:14 +00:00
8270366: C2: Add associative rule to add/sub node
Reviewed-by: kvn, adinn
This commit is contained in:
parent
1f995e52b2
commit
746fe5dc68
@ -321,6 +321,40 @@ Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
|
||||
if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )
|
||||
return new SubINode( in2, in1->in(2) );
|
||||
|
||||
// Associative
|
||||
if (op1 == Op_MulI && op2 == Op_MulI) {
|
||||
Node* add_in1 = NULL;
|
||||
Node* add_in2 = NULL;
|
||||
Node* mul_in = NULL;
|
||||
|
||||
if (in1->in(1) == in2->in(1)) {
|
||||
// Convert "a*b+a*c into a*(b+c)
|
||||
add_in1 = in1->in(2);
|
||||
add_in2 = in2->in(2);
|
||||
mul_in = in1->in(1);
|
||||
} else if (in1->in(2) == in2->in(1)) {
|
||||
// Convert a*b+b*c into b*(a+c)
|
||||
add_in1 = in1->in(1);
|
||||
add_in2 = in2->in(2);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(2) == in2->in(2)) {
|
||||
// Convert a*c+b*c into (a+b)*c
|
||||
add_in1 = in1->in(1);
|
||||
add_in2 = in2->in(1);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(1) == in2->in(2)) {
|
||||
// Convert a*b+c*a into a*(b+c)
|
||||
add_in1 = in1->in(2);
|
||||
add_in2 = in2->in(1);
|
||||
mul_in = in1->in(1);
|
||||
}
|
||||
|
||||
if (mul_in != NULL) {
|
||||
Node* add = phase->transform(new AddINode(add_in1, add_in2));
|
||||
return new MulINode(mul_in, add);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
|
||||
// Helps with array allocation math constant folding
|
||||
// See 4790063:
|
||||
@ -469,6 +503,40 @@ Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
|
||||
if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeLong::ZERO )
|
||||
return new SubLNode( in2, in1->in(2) );
|
||||
|
||||
// Associative
|
||||
if (op1 == Op_MulL && op2 == Op_MulL) {
|
||||
Node* add_in1 = NULL;
|
||||
Node* add_in2 = NULL;
|
||||
Node* mul_in = NULL;
|
||||
|
||||
if (in1->in(1) == in2->in(1)) {
|
||||
// Convert "a*b+a*c into a*(b+c)
|
||||
add_in1 = in1->in(2);
|
||||
add_in2 = in2->in(2);
|
||||
mul_in = in1->in(1);
|
||||
} else if (in1->in(2) == in2->in(1)) {
|
||||
// Convert a*b+b*c into b*(a+c)
|
||||
add_in1 = in1->in(1);
|
||||
add_in2 = in2->in(2);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(2) == in2->in(2)) {
|
||||
// Convert a*c+b*c into (a+b)*c
|
||||
add_in1 = in1->in(1);
|
||||
add_in2 = in2->in(1);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(1) == in2->in(2)) {
|
||||
// Convert a*b+c*a into a*(b+c)
|
||||
add_in1 = in1->in(2);
|
||||
add_in2 = in2->in(1);
|
||||
mul_in = in1->in(1);
|
||||
}
|
||||
|
||||
if (mul_in != NULL) {
|
||||
Node* add = phase->transform(new AddLNode(add_in1, add_in2));
|
||||
return new MulLNode(mul_in, add);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
|
||||
if (Matcher::match_rule_supported(Op_RotateRight) &&
|
||||
((op1 == Op_URShiftL && op2 == Op_LShiftL) || (op1 == Op_LShiftL && op2 == Op_URShiftL)) &&
|
||||
|
||||
@ -269,6 +269,40 @@ Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
|
||||
return new SubINode( add1, in2->in(1) );
|
||||
}
|
||||
|
||||
// Associative
|
||||
if (op1 == Op_MulI && op2 == Op_MulI) {
|
||||
Node* sub_in1 = NULL;
|
||||
Node* sub_in2 = NULL;
|
||||
Node* mul_in = NULL;
|
||||
|
||||
if (in1->in(1) == in2->in(1)) {
|
||||
// Convert "a*b-a*c into a*(b-c)
|
||||
sub_in1 = in1->in(2);
|
||||
sub_in2 = in2->in(2);
|
||||
mul_in = in1->in(1);
|
||||
} else if (in1->in(2) == in2->in(1)) {
|
||||
// Convert a*b-b*c into b*(a-c)
|
||||
sub_in1 = in1->in(1);
|
||||
sub_in2 = in2->in(2);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(2) == in2->in(2)) {
|
||||
// Convert a*c-b*c into (a-b)*c
|
||||
sub_in1 = in1->in(1);
|
||||
sub_in2 = in2->in(1);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(1) == in2->in(2)) {
|
||||
// Convert a*b-c*a into a*(b-c)
|
||||
sub_in1 = in1->in(2);
|
||||
sub_in2 = in2->in(1);
|
||||
mul_in = in1->in(1);
|
||||
}
|
||||
|
||||
if (mul_in != NULL) {
|
||||
Node* sub = phase->transform(new SubINode(sub_in1, sub_in2));
|
||||
return new MulINode(mul_in, sub);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert "0-(A>>31)" into "(A>>>31)"
|
||||
if ( op2 == Op_RShiftI ) {
|
||||
Node *in21 = in2->in(1);
|
||||
@ -393,6 +427,40 @@ Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
|
||||
return new SubLNode( add1, in2->in(1) );
|
||||
}
|
||||
|
||||
// Associative
|
||||
if (op1 == Op_MulL && op2 == Op_MulL) {
|
||||
Node* sub_in1 = NULL;
|
||||
Node* sub_in2 = NULL;
|
||||
Node* mul_in = NULL;
|
||||
|
||||
if (in1->in(1) == in2->in(1)) {
|
||||
// Convert "a*b-a*c into a*(b+c)
|
||||
sub_in1 = in1->in(2);
|
||||
sub_in2 = in2->in(2);
|
||||
mul_in = in1->in(1);
|
||||
} else if (in1->in(2) == in2->in(1)) {
|
||||
// Convert a*b-b*c into b*(a-c)
|
||||
sub_in1 = in1->in(1);
|
||||
sub_in2 = in2->in(2);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(2) == in2->in(2)) {
|
||||
// Convert a*c-b*c into (a-b)*c
|
||||
sub_in1 = in1->in(1);
|
||||
sub_in2 = in2->in(1);
|
||||
mul_in = in1->in(2);
|
||||
} else if (in1->in(1) == in2->in(2)) {
|
||||
// Convert a*b-c*a into a*(b-c)
|
||||
sub_in1 = in1->in(2);
|
||||
sub_in2 = in2->in(1);
|
||||
mul_in = in1->in(1);
|
||||
}
|
||||
|
||||
if (mul_in != NULL) {
|
||||
Node* sub = phase->transform(new SubLNode(sub_in1, sub_in2));
|
||||
return new MulLNode(mul_in, sub);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert "0L-(A>>63)" into "(A>>>63)"
|
||||
if ( op2 == Op_RShiftL ) {
|
||||
Node *in21 = in2->in(1);
|
||||
|
||||
@ -0,0 +1,238 @@
|
||||
/*
|
||||
* Copyright (c) 2021, Red Hat, Inc. 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 8270366
|
||||
* @summary Test corner cases of integer associative rules
|
||||
*
|
||||
* @run main/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation -XX:-UseOnStackReplacement TestAssociative
|
||||
*
|
||||
*/
|
||||
|
||||
public class TestAssociative {
|
||||
private static class IntParams {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
public IntParams(int a, int b, int c) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LongParams {
|
||||
long a;
|
||||
long b;
|
||||
long c;
|
||||
public LongParams(long a, long b, long c) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
}
|
||||
}
|
||||
|
||||
private static final IntParams[] intParamsArray = {
|
||||
new IntParams(17, 34, 10),
|
||||
new IntParams(Integer.MAX_VALUE - 4, 34, 10),
|
||||
new IntParams(7, Integer.MAX_VALUE, 10),
|
||||
new IntParams(7, Integer.MAX_VALUE, Integer.MAX_VALUE),
|
||||
new IntParams(10, Integer.MIN_VALUE, Integer.MIN_VALUE),
|
||||
new IntParams(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE)
|
||||
};
|
||||
|
||||
private static final LongParams[] longParamsArray = {
|
||||
new LongParams(17, 34, 10),
|
||||
new LongParams(Long.MAX_VALUE - 4, 34, 10),
|
||||
new LongParams(7, Long.MAX_VALUE, 10),
|
||||
new LongParams(7, Long.MAX_VALUE, Long.MAX_VALUE),
|
||||
new LongParams(10, Long.MIN_VALUE, Long.MIN_VALUE),
|
||||
new LongParams(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE)
|
||||
};
|
||||
|
||||
// Integer
|
||||
private static interface IntAssociativeTest {
|
||||
public int test(int a, int b, int c);
|
||||
}
|
||||
|
||||
// Integer Add
|
||||
private static class IntAssociativeTest0 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * b + a * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class IntAssociativeTest1 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * b + b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class IntAssociativeTest2 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * c + b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class IntAssociativeTest3 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * b + c * a;
|
||||
}
|
||||
}
|
||||
|
||||
// Integer Substract
|
||||
private static class IntAssociativeTest4 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * b - a * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class IntAssociativeTest5 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * b - b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class IntAssociativeTest6 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * c - b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class IntAssociativeTest7 implements IntAssociativeTest {
|
||||
public int test(int a, int b, int c) {
|
||||
return a * b - c * a;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Long
|
||||
private static interface LongAssociativeTest {
|
||||
public long test(long a, long b, long c);
|
||||
}
|
||||
|
||||
// Long Add
|
||||
private static class LongAssociativeTest0 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * b + a * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LongAssociativeTest1 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * b + b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LongAssociativeTest2 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * c + b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LongAssociativeTest3 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * b + c * a;
|
||||
}
|
||||
}
|
||||
|
||||
// Long Substract
|
||||
private static class LongAssociativeTest4 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * b - a * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LongAssociativeTest5 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * b - b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LongAssociativeTest6 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * c - b * c;
|
||||
}
|
||||
}
|
||||
|
||||
private static class LongAssociativeTest7 implements LongAssociativeTest {
|
||||
public long test(long a, long b, long c) {
|
||||
return a * b - c * a;
|
||||
}
|
||||
}
|
||||
|
||||
private static void runIntTest(IntAssociativeTest t) {
|
||||
for (IntParams p : intParamsArray) {
|
||||
int result = t.test(p.a, p.b, p.c);
|
||||
for (int i = 0; i < 20_000; i++) {
|
||||
if (result != t.test(p.a, p.b, p.c)) {
|
||||
throw new RuntimeException("incorrect result");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void runLongTest(LongAssociativeTest t) {
|
||||
for (LongParams p : longParamsArray) {
|
||||
long result = t.test(p.a, p.b, p.c);
|
||||
for (int i = 0; i < 20_000; i++) {
|
||||
if (result != t.test(p.a, p.b, p.c)) {
|
||||
throw new RuntimeException("incorrect result");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final IntAssociativeTest[] intTests = {
|
||||
new IntAssociativeTest0(),
|
||||
new IntAssociativeTest1(),
|
||||
new IntAssociativeTest2(),
|
||||
new IntAssociativeTest3(),
|
||||
new IntAssociativeTest4(),
|
||||
new IntAssociativeTest5(),
|
||||
new IntAssociativeTest6(),
|
||||
new IntAssociativeTest7()
|
||||
};
|
||||
|
||||
private static final LongAssociativeTest[] longTests = {
|
||||
new LongAssociativeTest0(),
|
||||
new LongAssociativeTest1(),
|
||||
new LongAssociativeTest2(),
|
||||
new LongAssociativeTest3(),
|
||||
new LongAssociativeTest4(),
|
||||
new LongAssociativeTest5(),
|
||||
new LongAssociativeTest6(),
|
||||
new LongAssociativeTest7()
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (IntAssociativeTest t: intTests) {
|
||||
runIntTest(t);
|
||||
}
|
||||
|
||||
for (LongAssociativeTest t: longTests) {
|
||||
runLongTest(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user