Skip to content

Commit f6fbb5a

Browse files
CptGitDamonFool
authored andcommitted
8278471: Remove unreached rules in AddNode::IdealIL
Reviewed-by: jiefu, kvn
1 parent 0dbe4c5 commit f6fbb5a

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

src/hotspot/share/opto/addnode.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,6 @@ Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
302302
assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
303303
return AddNode::make(in1->in(1), in2->in(1), bt);
304304
}
305-
// Convert "(a-b)+(b-c)" into "(a-c)"
306-
if (op2 == Op_Sub(bt) && in1->in(2) == in2->in(1)) {
307-
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
308-
return SubNode::make(in1->in(1), in2->in(2), bt);
309-
}
310-
// Convert "(a-b)+(c-a)" into "(c-b)"
311-
if (op2 == Op_Sub(bt) && in1->in(1) == in2->in(2)) {
312-
assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
313-
return SubNode::make(in2->in(1), in1->in(2), bt);
314-
}
315305
}
316306

317307
// Convert "x+(0-y)" into "(x-y)"

src/hotspot/share/opto/subnode.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
435435
if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
436436
return new SubLNode( in1->in(1), in2->in(1) );
437437

438+
// Convert "(A+X) - (X+B)" into "A - B"
439+
if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(1) )
440+
return new SubLNode( in1->in(1), in2->in(2) );
441+
442+
// Convert "(X+A) - (B+X)" into "A - B"
443+
if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(2) )
444+
return new SubLNode( in1->in(2), in2->in(1) );
445+
438446
// Convert "A-(B-C)" into (A+C)-B"
439447
if( op2 == Op_SubL && in2->outcnt() == 1) {
440448
Node *add1 = phase->transform( new AddLNode( in1, in2->in(2) ) );
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. 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+
package compiler.c2.irTests;
25+
26+
import jdk.test.lib.Asserts;
27+
import compiler.lib.ir_framework.*;
28+
29+
/*
30+
* @test
31+
* @bug 8278471
32+
* @summary Remove unreached rules in AddNode::IdealIL
33+
* @library /test/lib /
34+
* @run driver compiler.c2.irTests.TestSpecialCasesOf_AMinusB_Plus_CMinusD_InAddIdeal
35+
*/
36+
/* Test conversion from (a - b) + (b - c) to (a - c) and conversion
37+
* from (a - b) + (c - a) to (c - b) have really happened so we can
38+
* safely remove both. */
39+
public class TestSpecialCasesOf_AMinusB_Plus_CMinusD_InAddIdeal {
40+
41+
public static void main(String[] args) {
42+
TestFramework.run();
43+
}
44+
45+
@Test
46+
@Arguments({Argument.RANDOM_ONCE, Argument.RANDOM_ONCE, Argument.RANDOM_ONCE})
47+
@IR(failOn = {IRNode.ADD_I})
48+
@IR(counts = {IRNode.SUB_I, "1"})
49+
public int test1Int(int a, int b, int c) {
50+
return (a - b) + (b - c); // transformed to a - c rather than (a + b) - (b + c)
51+
}
52+
53+
@Test
54+
@Arguments({Argument.RANDOM_ONCE, Argument.RANDOM_ONCE, Argument.RANDOM_ONCE})
55+
@IR(failOn = {IRNode.ADD_L})
56+
@IR(counts = {IRNode.SUB_L, "1"})
57+
public long test1Long(long a, long b, long c) {
58+
return (a - b) + (b - c); // transformed to a - c rather than (a + b) - (b + c)
59+
}
60+
61+
@Test
62+
@Arguments({Argument.RANDOM_ONCE, Argument.RANDOM_ONCE, Argument.RANDOM_ONCE})
63+
@IR(failOn = {IRNode.ADD_I})
64+
@IR(counts = {IRNode.SUB_I, "1"})
65+
public int test2Int(int b, int a, int c) { // make sure inputs sorted
66+
return (a - b) + (c - a); // transformed to c - b rather than (a + c) - (b + a)
67+
}
68+
69+
@Test
70+
@Arguments({Argument.RANDOM_ONCE, Argument.RANDOM_ONCE, Argument.RANDOM_ONCE})
71+
@IR(failOn = {IRNode.ADD_L})
72+
@IR(counts = {IRNode.SUB_L, "1"})
73+
public long test2Long(long b, long a, long c) { // make sure inputs sorted
74+
return (a - b) + (c - a); // transformed to return c - b rather than (a + c) - (b + a)
75+
}
76+
}

test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public class IRNode {
139139
public static final String LSHIFT_L = START + "LShiftL" + MID + END;
140140
public static final String ADD_I = START + "AddI" + MID + END;
141141
public static final String ADD_L = START + "AddL" + MID + END;
142+
public static final String SUB_I = START + "SubI" + MID + END;
143+
public static final String SUB_L = START + "SubL" + MID + END;
142144
public static final String CONV_I2L = START + "ConvI2L" + MID + END;
143145

144146
public static final String VECTOR_CAST_B2X = START + "VectorCastB2X" + MID + END;

0 commit comments

Comments
 (0)