Skip to content

Commit 7c9868c

Browse files
committedSep 18, 2021
8273454: C2: Transform (-a)*(-b) into a*b
Reviewed-by: thartmann, eliu, chagedorn
1 parent bb9d142 commit 7c9868c

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed
 

‎src/hotspot/share/opto/mulnode.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,24 @@ Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) {
6363
const Type *t2 = phase->type( in(2) );
6464
Node *progress = NULL; // Progress flag
6565

66-
// convert "max(a,b) * min(a,b)" into "a*b".
66+
// convert "(-a)*(-b)" into "a*b"
6767
Node *in1 = in(1);
6868
Node *in2 = in(2);
69+
if (in1->is_Sub() && in2->is_Sub()) {
70+
if (phase->type(in1->in(1))->is_zero_type() &&
71+
phase->type(in2->in(1))->is_zero_type()) {
72+
set_req(1, in1->in(2));
73+
set_req(2, in2->in(2));
74+
PhaseIterGVN* igvn = phase->is_IterGVN();
75+
if (igvn) {
76+
igvn->_worklist.push(in1);
77+
igvn->_worklist.push(in2);
78+
}
79+
progress = this;
80+
}
81+
}
82+
83+
// convert "max(a,b) * min(a,b)" into "a*b".
6984
if ((in(1)->Opcode() == max_opcode() && in(2)->Opcode() == min_opcode())
7085
|| (in(1)->Opcode() == min_opcode() && in(2)->Opcode() == max_opcode())) {
7186
Node *in11 = in(1)->in(1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2021, Red Hat, Inc. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @key randomness
27+
* @bug 8273454
28+
* @summary Test transformation (-a)*(-b) = a*b
29+
*
30+
* @library /test/lib
31+
*
32+
* @run main/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:CompileCommand="dontinline,TestNegMultiply::test*" TestNegMultiply
33+
*
34+
*/
35+
36+
import java.util.Random;
37+
import jdk.test.lib.Utils;
38+
import jdk.test.lib.Asserts;
39+
40+
public class TestNegMultiply {
41+
private static final Random random = Utils.getRandomInstance();
42+
// Enough cycles to ensure test methods are JIT-ed
43+
private static final int TEST_COUNT = 20_000;
44+
45+
private static int testInt(int a, int b) {
46+
return (-a) * (-b);
47+
}
48+
private static long testLong(long a, long b) {
49+
return (-a) * (-b);
50+
}
51+
private static float testFloat(float a, float b) {
52+
return (-a) * (-b);
53+
}
54+
private static double testDouble(double a, double b) {
55+
return (-a) * (-b);
56+
}
57+
58+
private static void runIntTests() {
59+
for (int index = 0; index < TEST_COUNT; index ++) {
60+
int a = random.nextInt();
61+
int b = random.nextInt();
62+
int expected = (-a) * (-b);
63+
int res = testInt(a, b);
64+
Asserts.assertEQ(res, expected);
65+
}
66+
}
67+
68+
private static void runLongTests() {
69+
for (int index = 0; index < TEST_COUNT; index ++) {
70+
long a = random.nextLong();
71+
long b = random.nextLong();
72+
long expected = (-a) * (-b);
73+
long res = testLong(a, b);
74+
Asserts.assertEQ(res, expected);
75+
}
76+
}
77+
78+
private static void runFloatTests() {
79+
for (int index = 0; index < TEST_COUNT; index ++) {
80+
float a = random.nextFloat();
81+
float b = random.nextFloat();
82+
float expected = (-a) * (-b);
83+
float res = testFloat(a, b);
84+
Asserts.assertEQ(res, expected);
85+
}
86+
}
87+
88+
private static void runDoubleTests() {
89+
for (int index = 0; index < TEST_COUNT; index ++) {
90+
double a = random.nextDouble();
91+
double b = random.nextDouble();
92+
double expected = (-a) * (-b);
93+
double res = testDouble(a, b);
94+
Asserts.assertEQ(res, expected);
95+
}
96+
}
97+
98+
public static void main(String[] args) {
99+
runIntTests();
100+
runLongTests();
101+
runFloatTests();
102+
runDoubleTests();
103+
}
104+
}

0 commit comments

Comments
 (0)