Skip to content

Commit 1383fec

Browse files
committed
8327381: Refactor type-improving transformations in BoolNode::Ideal to BoolNode::Value
Reviewed-by: chagedorn, thartmann, jkarthikeyan, epeter
1 parent eb7ead5 commit 1383fec

File tree

4 files changed

+169
-20
lines changed

4 files changed

+169
-20
lines changed

src/hotspot/share/opto/subnode.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1623,25 +1623,8 @@ Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
16231623
return new BoolNode( ncmp, _test.negate() );
16241624
}
16251625

1626-
// Change ((x & m) u<= m) or ((m & x) u<= m) to always true
1627-
// Same with ((x & m) u< m+1) and ((m & x) u< m+1)
1628-
if (cop == Op_CmpU &&
1629-
cmp1_op == Op_AndI) {
1630-
Node* bound = nullptr;
1631-
if (_test._test == BoolTest::le) {
1632-
bound = cmp2;
1633-
} else if (_test._test == BoolTest::lt &&
1634-
cmp2->Opcode() == Op_AddI &&
1635-
cmp2->in(2)->find_int_con(0) == 1) {
1636-
bound = cmp2->in(1);
1637-
}
1638-
if (cmp1->in(2) == bound || cmp1->in(1) == bound) {
1639-
return ConINode::make(1);
1640-
}
1641-
}
1642-
16431626
// Change ((x & (m - 1)) u< m) into (m > 0)
1644-
// This is the off-by-one variant of the above
1627+
// This is the off-by-one variant of ((x & m) u<= m)
16451628
if (cop == Op_CmpU &&
16461629
_test._test == BoolTest::lt &&
16471630
cmp1_op == Op_AndI) {
@@ -1827,9 +1810,39 @@ Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
18271810
}
18281811

18291812
//------------------------------Value------------------------------------------
1813+
// Change ((x & m) u<= m) or ((m & x) u<= m) to always true
1814+
// Same with ((x & m) u< m+1) and ((m & x) u< m+1)
1815+
const Type* BoolNode::Value_cmpu_and_mask(PhaseValues* phase) const {
1816+
Node* cmp = in(1);
1817+
if (cmp != nullptr && cmp->Opcode() == Op_CmpU) {
1818+
Node* cmp1 = cmp->in(1);
1819+
Node* cmp2 = cmp->in(2);
1820+
1821+
if (cmp1->Opcode() == Op_AndI) {
1822+
Node* bound = nullptr;
1823+
if (_test._test == BoolTest::le) {
1824+
bound = cmp2;
1825+
} else if (_test._test == BoolTest::lt && cmp2->Opcode() == Op_AddI && cmp2->in(2)->find_int_con(0) == 1) {
1826+
bound = cmp2->in(1);
1827+
}
1828+
1829+
if (cmp1->in(2) == bound || cmp1->in(1) == bound) {
1830+
return TypeInt::ONE;
1831+
}
1832+
}
1833+
}
1834+
1835+
return nullptr;
1836+
}
1837+
18301838
// Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
18311839
// based on local information. If the input is constant, do it.
18321840
const Type* BoolNode::Value(PhaseGVN* phase) const {
1841+
const Type* t = Value_cmpu_and_mask(phase);
1842+
if (t != nullptr) {
1843+
return t;
1844+
}
1845+
18331846
return _test.cc2logical( phase->type( in(1) ) );
18341847
}
18351848

src/hotspot/share/opto/subnode.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ class BoolNode : public Node {
347347
BoolNode* negate(PhaseGVN* phase);
348348
virtual int Opcode() const;
349349
virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
350+
const Type* Value_cmpu_and_mask(PhaseValues* phase) const;
350351
virtual const Type* Value(PhaseGVN* phase) const;
351352
virtual const Type *bottom_type() const { return TypeInt::BOOL; }
352353
uint match_edge(uint idx) const { return 0; }
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) 2024 Red Hat 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.gvn;
25+
26+
import compiler.lib.ir_framework.*;
27+
28+
import java.util.Random;
29+
30+
/**
31+
* @test
32+
* @bug 8327381
33+
* @summary Refactor boolean node tautology transformations
34+
* @library /test/lib /
35+
* @run driver compiler.c2.gvn.TestBoolNodeGVN
36+
*/
37+
public class TestBoolNodeGVN {
38+
public static void main(String[] args) {
39+
TestFramework.run();
40+
testCorrectness();
41+
}
42+
43+
/**
44+
* Test changing ((x & m) u<= m) or ((m & x) u<= m) to always true, same with ((x & m) u< m+1) and ((m & x) u< m+1)
45+
* The test is only applicable to x64, aarch64 and riscv64 for having <code>Integer.compareUnsigned</code>
46+
* intrinsified.
47+
*/
48+
@Test
49+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
50+
@IR(failOn = IRNode.CMP_U,
51+
phase = CompilePhase.AFTER_PARSING,
52+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
53+
public static boolean testShouldReplaceCpmUCase1(int x, int m) {
54+
return !(Integer.compareUnsigned((x & m), m) > 0); // assert in inversions to generates the pattern looking for
55+
}
56+
@Test
57+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
58+
@IR(failOn = IRNode.CMP_U,
59+
phase = CompilePhase.AFTER_PARSING,
60+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
61+
public static boolean testShouldReplaceCpmUCase2(int x, int m) {
62+
return !(Integer.compareUnsigned((m & x), m) > 0);
63+
}
64+
65+
@Test
66+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
67+
@IR(failOn = IRNode.CMP_U,
68+
phase = CompilePhase.AFTER_PARSING,
69+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
70+
public static boolean testShouldReplaceCpmUCase3(int x, int m) {
71+
return Integer.compareUnsigned((x & m), m + 1) < 0;
72+
}
73+
74+
@Test
75+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
76+
@IR(failOn = IRNode.CMP_U,
77+
phase = CompilePhase.AFTER_PARSING,
78+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
79+
public static boolean testShouldReplaceCpmUCase4(int x, int m) {
80+
return Integer.compareUnsigned((m & x), m + 1) < 0;
81+
}
82+
83+
@Test
84+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
85+
@IR(counts = {IRNode.CMP_U, "1"},
86+
phase = CompilePhase.AFTER_PARSING,
87+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
88+
public static boolean testShouldHaveCpmUCase1(int x, int m) {
89+
return !(Integer.compareUnsigned((x & m), m - 1) > 0);
90+
}
91+
92+
@Test
93+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
94+
@IR(counts = {IRNode.CMP_U, "1"},
95+
phase = CompilePhase.AFTER_PARSING,
96+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
97+
public static boolean testShouldHaveCpmUCase2(int x, int m) {
98+
return !(Integer.compareUnsigned((m & x), m - 1) > 0);
99+
}
100+
101+
@Test
102+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
103+
@IR(counts = {IRNode.CMP_U, "1"},
104+
phase = CompilePhase.AFTER_PARSING,
105+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
106+
public static boolean testShouldHaveCpmUCase3(int x, int m) {
107+
return Integer.compareUnsigned((x & m), m + 2) < 0;
108+
}
109+
110+
@Test
111+
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
112+
@IR(counts = {IRNode.CMP_U, "1"},
113+
phase = CompilePhase.AFTER_PARSING,
114+
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
115+
public static boolean testShouldHaveCpmUCase4(int x, int m) {
116+
return Integer.compareUnsigned((m & x), m + 2) < 0;
117+
}
118+
119+
private static void testCorrectness() {
120+
int[] values = {
121+
0, 1, 5, 8, 16, 42, 100, new Random().nextInt(0, Integer.MAX_VALUE), Integer.MAX_VALUE
122+
};
123+
124+
for (int x : values) {
125+
for (int m : values) {
126+
if (!testShouldReplaceCpmUCase1(x, m) |
127+
!testShouldReplaceCpmUCase2(x, m) |
128+
!testShouldReplaceCpmUCase3(x, m) |
129+
!testShouldReplaceCpmUCase4(x, m)) {
130+
throw new RuntimeException("Bad result for x = " + x + " and m = " + m + ", expected always true");
131+
}
132+
}
133+
}
134+
}
135+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public class IRNode {
439439

440440
public static final String CMP_U = PREFIX + "CMP_U" + POSTFIX;
441441
static {
442-
beforeMatchingNameRegex(CMP_U, "CmpU");
442+
beforeMatchingNameRegex(CMP_U, "CmpU\\b");
443443
}
444444

445445
public static final String CMP_U3 = PREFIX + "CMP_U3" + POSTFIX;

0 commit comments

Comments
 (0)