Skip to content

Commit 1515bd7

Browse files
CptGitTobiHartmann
authored andcommittedJan 15, 2024
8322077: Add Ideal transformation: (~a) | (~b) => ~(a & b)
Reviewed-by: thartmann, epeter
1 parent bdee968 commit 1515bd7

File tree

5 files changed

+369
-0
lines changed

5 files changed

+369
-0
lines changed
 

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

+15
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,13 @@ Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
806806
return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
807807
}
808808
}
809+
810+
// Convert "~a | ~b" into "~(a & b)"
811+
if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
812+
Node* and_a_b = new AndINode(in(1)->in(1), in(2)->in(1));
813+
Node* tn = phase->transform(and_a_b);
814+
return AddNode::make_not(phase, tn, T_INT);
815+
}
809816
return nullptr;
810817
}
811818

@@ -872,6 +879,14 @@ Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
872879
return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
873880
}
874881
}
882+
883+
// Convert "~a | ~b" into "~(a & b)"
884+
if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
885+
Node* and_a_b = new AndLNode(in(1)->in(1), in(2)->in(1));
886+
Node* tn = phase->transform(and_a_b);
887+
return AddNode::make_not(phase, tn, T_LONG);
888+
}
889+
875890
return nullptr;
876891
}
877892

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package compiler.c2.irTests;
24+
25+
import jdk.test.lib.Asserts;
26+
import compiler.lib.ir_framework.*;
27+
28+
/*
29+
* @test
30+
* @bug 8322077
31+
* @summary Test that Ideal transformations on the De Morgan's Law perform
32+
as expected for int.
33+
* @library /test/lib /
34+
* @run driver compiler.c2.irTests.DeMorganLawIntTests
35+
*/
36+
public class DeMorganLawIntTests {
37+
38+
public static void main(String[] args) {
39+
TestFramework.run();
40+
}
41+
42+
@Run(test = { "test1", "test2", "test3", "test4" })
43+
public void runMethod() {
44+
int a = RunInfo.getRandom().nextInt();
45+
int b = RunInfo.getRandom().nextInt();
46+
int c = RunInfo.getRandom().nextInt();
47+
int d = RunInfo.getRandom().nextInt();
48+
49+
int min = Integer.MIN_VALUE;
50+
int max = Integer.MAX_VALUE;
51+
52+
assertResult(0, 0, 0, 0);
53+
assertResult(a, b, c, d);
54+
assertResult(min, min, min, min);
55+
assertResult(max, max, max, max);
56+
}
57+
58+
@DontCompile
59+
public void assertResult(int a, int b, int c, int d) {
60+
Asserts.assertEQ((~a | ~b) & (~c | ~d), test1(a, b, c, d));
61+
Asserts.assertEQ((~a & ~b) | (~c & ~d), test2(a, b, c, d));
62+
Asserts.assertEQ((~a | ~b) | (~c | ~d), test3(a, b, c, d));
63+
Asserts.assertEQ((~a & ~b) & (~c & ~d), test4(a, b, c, d));
64+
}
65+
66+
// Checks (~a | ~b) & (~c | ~d)
67+
// => ~(a & b) & ~(c & d)
68+
// => ~((a & b) | (c & d))
69+
@Test
70+
@IR(counts = { IRNode.AND , "2",
71+
IRNode.OR , "1",
72+
IRNode.XOR, "1", })
73+
public int test1(int a, int b, int c, int d) {
74+
return (~a | ~b) & (~c | ~d);
75+
}
76+
77+
// Checks (~a & ~b) | (~c & ~d)
78+
// => ~(a | b) | ~(c | d)
79+
// => ~((a | b) & (c | d))
80+
@Test
81+
@IR(counts = { IRNode.AND , "1",
82+
IRNode.OR , "2",
83+
IRNode.XOR, "1", })
84+
public int test2(int a, int b, int c, int d) {
85+
return (~a & ~b) | (~c & ~d);
86+
}
87+
88+
// Checks (~a | ~b) | (~c | ~d)
89+
// => ~(a & b) | ~(c & d)
90+
// => ~((a & b) & (c & d))
91+
@Test
92+
@IR(failOn = { IRNode.OR })
93+
@IR(counts = { IRNode.AND , "3",
94+
IRNode.XOR, "1", })
95+
public int test3(int a, int b, int c, int d) {
96+
return (~a | ~b) | (~c | ~d);
97+
}
98+
99+
// Checks (~a & ~b) & (~c & ~d)
100+
// => ~(a | b) & ~(c | d)
101+
// => ~((a | b) | (c | d))
102+
@Test
103+
@IR(failOn = { IRNode.AND })
104+
@IR(counts = { IRNode.OR , "3",
105+
IRNode.XOR, "1", })
106+
public int test4(int a, int b, int c, int d) {
107+
return (~a & ~b) & (~c & ~d);
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package compiler.c2.irTests;
24+
25+
import jdk.test.lib.Asserts;
26+
import compiler.lib.ir_framework.*;
27+
28+
/*
29+
* @test
30+
* @bug 8322077
31+
* @summary Test that Ideal transformations on the De Morgan's Law perform
32+
as expected for long.
33+
* @library /test/lib /
34+
* @run driver compiler.c2.irTests.DeMorganLawLongTests
35+
*/
36+
public class DeMorganLawLongTests {
37+
38+
public static void main(String[] args) {
39+
TestFramework.run();
40+
}
41+
42+
@Run(test = { "test1", "test2", "test3", "test4" })
43+
public void runMethod() {
44+
long a = RunInfo.getRandom().nextLong();
45+
long b = RunInfo.getRandom().nextLong();
46+
long c = RunInfo.getRandom().nextLong();
47+
long d = RunInfo.getRandom().nextLong();
48+
49+
long min = Long.MIN_VALUE;
50+
long max = Long.MAX_VALUE;
51+
52+
assertResult(0, 0, 0, 0);
53+
assertResult(a, b, c, d);
54+
assertResult(min, min, min, min);
55+
assertResult(max, max, max, max);
56+
}
57+
58+
@DontCompile
59+
public void assertResult(long a, long b, long c, long d) {
60+
Asserts.assertEQ((~a | ~b) & (~c | ~d), test1(a, b, c, d));
61+
Asserts.assertEQ((~a & ~b) | (~c & ~d), test2(a, b, c, d));
62+
Asserts.assertEQ((~a | ~b) | (~c | ~d), test3(a, b, c, d));
63+
Asserts.assertEQ((~a & ~b) & (~c & ~d), test4(a, b, c, d));
64+
}
65+
66+
// Checks (~a | ~b) & (~c | ~d)
67+
// => ~(a & b) & ~(c & d)
68+
// => ~((a & b) | (c & d))
69+
@Test
70+
@IR(counts = { IRNode.AND , "2",
71+
IRNode.OR , "1",
72+
IRNode.XOR, "1", })
73+
public long test1(long a, long b, long c, long d) {
74+
return (~a | ~b) & (~c | ~d);
75+
}
76+
77+
// Checks (~a & ~b) | (~c & ~d)
78+
// => ~(a | b) | ~(c | d)
79+
// => ~((a | b) & (c | d))
80+
@Test
81+
@IR(counts = { IRNode.AND , "1",
82+
IRNode.OR , "2",
83+
IRNode.XOR, "1", })
84+
public long test2(long a, long b, long c, long d) {
85+
return (~a & ~b) | (~c & ~d);
86+
}
87+
88+
// Checks (~a | ~b) | (~c | ~d)
89+
// => ~(a & b) | ~(c & d)
90+
// => ~((a & b) & (c & d))
91+
@Test
92+
@IR(failOn = { IRNode.OR })
93+
@IR(counts = { IRNode.AND , "3",
94+
IRNode.XOR, "1", })
95+
public long test3(long a, long b, long c, long d) {
96+
return (~a | ~b) | (~c | ~d);
97+
}
98+
99+
// Checks (~a & ~b) & (~c & ~d)
100+
// => ~(a | b) & ~(c | d)
101+
// => ~((a | b) | (c | d))
102+
@Test
103+
@IR(failOn = { IRNode.AND })
104+
@IR(counts = { IRNode.OR , "3",
105+
IRNode.XOR, "1", })
106+
public long test4(long a, long b, long c, long d) {
107+
return (~a & ~b) & (~c & ~d);
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package compiler.c2.irTests;
24+
25+
import jdk.test.lib.Asserts;
26+
import compiler.lib.ir_framework.*;
27+
28+
/*
29+
* @test
30+
* @bug 8322077
31+
* @summary Test that Ideal transformations of OrINode* are being performed as expected.
32+
* @library /test/lib /
33+
* @run driver compiler.c2.irTests.OrINodeIdealizationTests
34+
*/
35+
public class OrINodeIdealizationTests {
36+
37+
public static void main(String[] args) {
38+
TestFramework.run();
39+
}
40+
41+
@Run(test = { "test1" })
42+
public void runMethod() {
43+
int a = RunInfo.getRandom().nextInt();
44+
int b = RunInfo.getRandom().nextInt();
45+
46+
int min = Integer.MIN_VALUE;
47+
int max = Integer.MAX_VALUE;
48+
49+
assertResult(0, 0);
50+
assertResult(a, b);
51+
assertResult(min, min);
52+
assertResult(max, max);
53+
}
54+
55+
@DontCompile
56+
public void assertResult(int a, int b) {
57+
Asserts.assertEQ((~a) | (~b), test1(a, b));
58+
}
59+
60+
// Checks (~a) | (~b) => ~(a & b)
61+
@Test
62+
@IR(failOn = { IRNode.OR })
63+
@IR(counts = { IRNode.AND, "1",
64+
IRNode.XOR, "1" })
65+
public int test1(int a, int b) {
66+
return (~a) | (~b);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package compiler.c2.irTests;
24+
25+
import jdk.test.lib.Asserts;
26+
import compiler.lib.ir_framework.*;
27+
28+
/*
29+
* @test
30+
* @bug 8322077
31+
* @summary Test that Ideal transformations of OrLNode* are being performed as expected.
32+
* @library /test/lib /
33+
* @run driver compiler.c2.irTests.OrLNodeIdealizationTests
34+
*/
35+
public class OrLNodeIdealizationTests {
36+
37+
public static void main(String[] args) {
38+
TestFramework.run();
39+
}
40+
41+
@Run(test = { "test1" })
42+
public void runMethod() {
43+
long a = RunInfo.getRandom().nextLong();
44+
long b = RunInfo.getRandom().nextLong();
45+
46+
long min = Long.MIN_VALUE;
47+
long max = Long.MAX_VALUE;
48+
49+
assertResult(0, 0);
50+
assertResult(a, b);
51+
assertResult(min, min);
52+
assertResult(max, max);
53+
}
54+
55+
@DontCompile
56+
public void assertResult(long a, long b) {
57+
Asserts.assertEQ((~a) | (~b), test1(a, b));
58+
}
59+
60+
// Checks (~a) | (~b) => ~(a & b)
61+
@Test
62+
@IR(failOn = { IRNode.OR })
63+
@IR(counts = { IRNode.AND, "1",
64+
IRNode.XOR, "1" })
65+
public long test1(long a, long b) {
66+
return (~a) | (~b);
67+
}
68+
}

0 commit comments

Comments
 (0)
Please sign in to comment.