8327381: Refactor type-improving transformations in BoolNode::Ideal to BoolNode::Value

Reviewed-by: chagedorn, thartmann, jkarthikeyan, epeter
This commit is contained in:
Kangcheng Xu 2024-08-29 05:34:08 +00:00
parent eb7ead58fd
commit 1383fec417
4 changed files with 169 additions and 20 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. 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
@ -1623,25 +1623,8 @@ Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
return new BoolNode( ncmp, _test.negate() );
}
// Change ((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)
if (cop == Op_CmpU &&
cmp1_op == Op_AndI) {
Node* bound = nullptr;
if (_test._test == BoolTest::le) {
bound = cmp2;
} else if (_test._test == BoolTest::lt &&
cmp2->Opcode() == Op_AddI &&
cmp2->in(2)->find_int_con(0) == 1) {
bound = cmp2->in(1);
}
if (cmp1->in(2) == bound || cmp1->in(1) == bound) {
return ConINode::make(1);
}
}
// Change ((x & (m - 1)) u< m) into (m > 0)
// This is the off-by-one variant of the above
// This is the off-by-one variant of ((x & m) u<= m)
if (cop == Op_CmpU &&
_test._test == BoolTest::lt &&
cmp1_op == Op_AndI) {
@ -1827,9 +1810,39 @@ Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
}
//------------------------------Value------------------------------------------
// Change ((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)
const Type* BoolNode::Value_cmpu_and_mask(PhaseValues* phase) const {
Node* cmp = in(1);
if (cmp != nullptr && cmp->Opcode() == Op_CmpU) {
Node* cmp1 = cmp->in(1);
Node* cmp2 = cmp->in(2);
if (cmp1->Opcode() == Op_AndI) {
Node* bound = nullptr;
if (_test._test == BoolTest::le) {
bound = cmp2;
} else if (_test._test == BoolTest::lt && cmp2->Opcode() == Op_AddI && cmp2->in(2)->find_int_con(0) == 1) {
bound = cmp2->in(1);
}
if (cmp1->in(2) == bound || cmp1->in(1) == bound) {
return TypeInt::ONE;
}
}
}
return nullptr;
}
// Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
// based on local information. If the input is constant, do it.
const Type* BoolNode::Value(PhaseGVN* phase) const {
const Type* t = Value_cmpu_and_mask(phase);
if (t != nullptr) {
return t;
}
return _test.cc2logical( phase->type( in(1) ) );
}

View File

@ -347,6 +347,7 @@ public:
BoolNode* negate(PhaseGVN* phase);
virtual int Opcode() const;
virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
const Type* Value_cmpu_and_mask(PhaseValues* phase) const;
virtual const Type* Value(PhaseGVN* phase) const;
virtual const Type *bottom_type() const { return TypeInt::BOOL; }
uint match_edge(uint idx) const { return 0; }

View File

@ -0,0 +1,135 @@
/*
* Copyright (c) 2024 Red Hat and/or its affiliates. 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.
*/
package compiler.c2.gvn;
import compiler.lib.ir_framework.*;
import java.util.Random;
/**
* @test
* @bug 8327381
* @summary Refactor boolean node tautology transformations
* @library /test/lib /
* @run driver compiler.c2.gvn.TestBoolNodeGVN
*/
public class TestBoolNodeGVN {
public static void main(String[] args) {
TestFramework.run();
testCorrectness();
}
/**
* 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)
* The test is only applicable to x64, aarch64 and riscv64 for having <code>Integer.compareUnsigned</code>
* intrinsified.
*/
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(failOn = IRNode.CMP_U,
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldReplaceCpmUCase1(int x, int m) {
return !(Integer.compareUnsigned((x & m), m) > 0); // assert in inversions to generates the pattern looking for
}
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(failOn = IRNode.CMP_U,
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldReplaceCpmUCase2(int x, int m) {
return !(Integer.compareUnsigned((m & x), m) > 0);
}
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(failOn = IRNode.CMP_U,
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldReplaceCpmUCase3(int x, int m) {
return Integer.compareUnsigned((x & m), m + 1) < 0;
}
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(failOn = IRNode.CMP_U,
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldReplaceCpmUCase4(int x, int m) {
return Integer.compareUnsigned((m & x), m + 1) < 0;
}
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(counts = {IRNode.CMP_U, "1"},
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldHaveCpmUCase1(int x, int m) {
return !(Integer.compareUnsigned((x & m), m - 1) > 0);
}
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(counts = {IRNode.CMP_U, "1"},
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldHaveCpmUCase2(int x, int m) {
return !(Integer.compareUnsigned((m & x), m - 1) > 0);
}
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(counts = {IRNode.CMP_U, "1"},
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldHaveCpmUCase3(int x, int m) {
return Integer.compareUnsigned((x & m), m + 2) < 0;
}
@Test
@Arguments(values = {Argument.DEFAULT, Argument.DEFAULT})
@IR(counts = {IRNode.CMP_U, "1"},
phase = CompilePhase.AFTER_PARSING,
applyIfPlatformOr = {"x64", "true", "aarch64", "true", "riscv64", "true"})
public static boolean testShouldHaveCpmUCase4(int x, int m) {
return Integer.compareUnsigned((m & x), m + 2) < 0;
}
private static void testCorrectness() {
int[] values = {
0, 1, 5, 8, 16, 42, 100, new Random().nextInt(0, Integer.MAX_VALUE), Integer.MAX_VALUE
};
for (int x : values) {
for (int m : values) {
if (!testShouldReplaceCpmUCase1(x, m) |
!testShouldReplaceCpmUCase2(x, m) |
!testShouldReplaceCpmUCase3(x, m) |
!testShouldReplaceCpmUCase4(x, m)) {
throw new RuntimeException("Bad result for x = " + x + " and m = " + m + ", expected always true");
}
}
}
}
}

View File

@ -439,7 +439,7 @@ public class IRNode {
public static final String CMP_U = PREFIX + "CMP_U" + POSTFIX;
static {
beforeMatchingNameRegex(CMP_U, "CmpU");
beforeMatchingNameRegex(CMP_U, "CmpU\\b");
}
public static final String CMP_U3 = PREFIX + "CMP_U3" + POSTFIX;