Skip to content

8322589: Add Ideal transformation: (~a) & (~b) => ~(a | b) #16333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/hotspot/share/opto/addnode.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -253,6 +253,22 @@ AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) {
return nullptr;
}

bool AddNode::is_not(PhaseGVN* phase, Node* n, BasicType bt) {
return n->Opcode() == Op_Xor(bt) && phase->type(n->in(2)) == TypeInteger::minus_1(bt);
}

AddNode* AddNode::make_not(PhaseGVN* phase, Node* n, BasicType bt) {
switch (bt) {
case T_INT:
return new XorINode(n, phase->intcon(-1));
case T_LONG:
return new XorLNode(n, phase->longcon(-1L));
default:
fatal("Not implemented for %s", type2name(bt));
}
return nullptr;
}

//=============================================================================
//------------------------------Idealize---------------------------------------
Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
Expand Down
9 changes: 8 additions & 1 deletion src/hotspot/share/opto/addnode.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -78,6 +78,13 @@ class AddNode : public Node {
virtual int min_opcode() const = 0;

static AddNode* make(Node* in1, Node* in2, BasicType bt);

// Utility function to check if the given node is a NOT operation,
// i.e., n == m ^ (-1).
static bool is_not(PhaseGVN* phase, Node* n, BasicType bt);

// Utility function to make a NOT operation, i.e., returning n ^ (-1).
static AddNode* make_not(PhaseGVN* phase, Node* n, BasicType bt);
};

//------------------------------AddINode---------------------------------------
Expand Down
16 changes: 15 additions & 1 deletion src/hotspot/share/opto/mulnode.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -610,6 +610,13 @@ Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) {
return progress;
}

// Convert "(~a) & (~b)" into "~(a | b)"
if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
Node* or_a_b = new OrINode(in(1)->in(1), in(2)->in(1));
Node* tn = phase->transform(or_a_b);
return AddNode::make_not(phase, tn, T_INT);
}

// Special case constant AND mask
const TypeInt *t2 = phase->type( in(2) )->isa_int();
if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
Expand Down Expand Up @@ -750,6 +757,13 @@ Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
return progress;
}

// Convert "(~a) & (~b)" into "~(a | b)"
if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
Node* or_a_b = new OrLNode(in(1)->in(1), in(2)->in(1));
Node* tn = phase->transform(or_a_b);
return AddNode::make_not(phase, tn, T_LONG);
}

// Special case constant AND mask
const TypeLong *t2 = phase->type( in(2) )->isa_long();
if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 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
Expand Down Expand Up @@ -38,22 +38,24 @@ public static void main(String[] args) {
TestFramework.run();
}

@Run(test = { "test1" })
@Run(test = { "test1", "test2" })
public void runMethod() {
int a = RunInfo.getRandom().nextInt();
int b = RunInfo.getRandom().nextInt();

int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;

assertResult(0);
assertResult(a);
assertResult(min);
assertResult(max);
assertResult(0, 0);
assertResult(a, b);
assertResult(min, min);
assertResult(max, max);
}

@DontCompile
public void assertResult(int a) {
public void assertResult(int a, int b) {
Asserts.assertEQ((0 - a) & 1, test1(a));
Asserts.assertEQ((~a) & (~b), test2(a, b));
}

@Test
Expand All @@ -63,4 +65,13 @@ public void assertResult(int a) {
public int test1(int x) {
return (0 - x) & 1;
}

@Test
@IR(failOn = { IRNode.AND })
@IR(counts = { IRNode.OR, "1",
IRNode.XOR, "1" })
// Checks (~a) & (~b) => ~(a | b)
public int test2(int a, int b) {
return (~a) & (~b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 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
* 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.irTests;

import jdk.test.lib.Asserts;
import compiler.lib.ir_framework.*;

/*
* @test
* @bug 8322589
* @summary Test that Ideal transformations of AndLNode* are being performed as expected.
* @library /test/lib /
* @run driver compiler.c2.irTests.AndLNodeIdealizationTests
*/
public class AndLNodeIdealizationTests {

public static void main(String[] args) {
TestFramework.run();
}

@Run(test = { "test1" })
public void runMethod() {
long a = RunInfo.getRandom().nextLong();
long b = RunInfo.getRandom().nextLong();

long min = Long.MIN_VALUE;
long max = Long.MAX_VALUE;

assertResult(0, 0);
assertResult(a, b);
assertResult(min, min);
assertResult(max, max);
}

@DontCompile
public void assertResult(long a, long b) {
Asserts.assertEQ((~a) & (~b), test1(a, b));
}

@Test
@IR(failOn = { IRNode.AND })
@IR(counts = { IRNode.OR, "1",
IRNode.XOR, "1" })
// Checks (~a) & (~b) => ~(a | b)
public long test1(long a, long b) {
return (~a) & (~b);
}
}
7 changes: 6 additions & 1 deletion test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
Expand Down Expand Up @@ -298,6 +298,11 @@ public class IRNode {
optoOnly(ALLOC_ARRAY_OF, regex);
}

public static final String OR = PREFIX + "OR" + POSTFIX;
static {
beforeMatchingNameRegex(OR, "Or(I|L)");
}

public static final String AND = PREFIX + "AND" + POSTFIX;
static {
beforeMatchingNameRegex(AND, "And(I|L)");
Expand Down