
jdk.graal.compiler.nodes.calc.SubNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compiler Show documentation
Show all versions of compiler Show documentation
The GraalVM compiler and the Graal-truffle optimizer.
/*
* Copyright (c) 2011, 2022, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 jdk.graal.compiler.nodes.calc;
import jdk.graal.compiler.core.common.type.ArithmeticOpTable;
import jdk.graal.compiler.core.common.type.ArithmeticOpTable.BinaryOp;
import jdk.graal.compiler.core.common.type.ArithmeticOpTable.BinaryOp.Sub;
import jdk.graal.compiler.core.common.type.IntegerStamp;
import jdk.graal.compiler.core.common.type.Stamp;
import jdk.graal.compiler.core.common.type.StampFactory;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.lir.gen.ArithmeticLIRGeneratorTool;
import jdk.graal.compiler.nodeinfo.NodeInfo;
import jdk.graal.compiler.nodes.ConstantNode;
import jdk.graal.compiler.nodes.NodeView;
import jdk.graal.compiler.nodes.ValueNode;
import jdk.graal.compiler.nodes.spi.CanonicalizerTool;
import jdk.graal.compiler.nodes.spi.NodeLIRBuilderTool;
import jdk.graal.compiler.nodes.util.GraphUtil;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.PrimitiveConstant;
@NodeInfo(shortName = "-")
public class SubNode extends BinaryArithmeticNode implements NarrowableArithmeticNode {
public static final NodeClass TYPE = NodeClass.create(SubNode.class);
public SubNode(ValueNode x, ValueNode y) {
this(TYPE, x, y);
}
protected SubNode(NodeClass extends SubNode> c, ValueNode x, ValueNode y) {
super(c, getArithmeticOpTable(x).getSub(), x, y);
}
public static ValueNode create(ValueNode x, ValueNode y, NodeView view) {
BinaryOp op = ArithmeticOpTable.forStamp(x.stamp(view)).getSub();
Stamp stamp = op.foldStamp(x.stamp(view), y.stamp(view));
ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp, view);
if (tryConstantFold != null) {
return tryConstantFold;
}
return canonical(null, op, stamp, x, y, view);
}
@Override
protected BinaryOp getOp(ArithmeticOpTable table) {
return table.getSub();
}
private static ValueNode canonical(SubNode subNode, BinaryOp op, Stamp stamp, ValueNode forX, ValueNode forY, NodeView view) {
SubNode self = subNode;
if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
Constant zero = op.getZero(forX.stamp(view));
if (zero != null) {
return ConstantNode.forPrimitive(stamp, zero);
}
}
boolean mayReassociate = BinaryArithmeticNode.mayReassociate(op, stamp);
if (mayReassociate) {
if (forX instanceof AddNode) {
AddNode x = (AddNode) forX;
if (x.getY() == forY) {
// (a + b) - b
return x.getX();
}
if (x.getX() == forY) {
// (a + b) - a
return x.getY();
}
} else if (forX instanceof SubNode) {
SubNode x = (SubNode) forX;
if (x.getX() == forY) {
// (a - b) - a
return NegateNode.create(x.getY(), view);
}
}
if (forY instanceof AddNode) {
AddNode y = (AddNode) forY;
if (y.getX() == forX) {
// a - (a + b)
return NegateNode.create(y.getY(), view);
}
if (y.getY() == forX) {
// b - (a + b)
return NegateNode.create(y.getX(), view);
}
} else if (forY instanceof SubNode) {
SubNode y = (SubNode) forY;
if (y.getX() == forX) {
// a - (a - b)
return y.getY();
}
}
if (forX instanceof OrNode && forY instanceof XorNode) {
// convert (x|y)-(x^y) to x&y
OrNode or = (OrNode) forX;
XorNode xor = (XorNode) forY;
if ((or.getX() == xor.getX() && or.getY() == xor.getY()) || (or.getX() == xor.getY() && or.getY() == xor.getX())) {
return AndNode.create(or.getX(), or.getY(), view);
}
}
}
if (forY.isConstant()) {
Constant c = forY.asConstant();
if (op.isNeutral(c)) {
return forX;
}
if (mayReassociate && self != null) {
ValueNode reassociated = reassociateMatchedValues(self, ValueNode.isConstantPredicate(), forX, forY, view);
if (reassociated != self) {
return reassociated;
}
}
if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) {
long i = ((PrimitiveConstant) c).asLong();
if (i < 0 || ((IntegerStamp) StampFactory.forKind(forY.getStackKind())).contains(-i)) {
// Adding a negative is more friendly to the backend since adds are
// commutative, so prefer add when it fits.
return BinaryArithmeticNode.add(forX, ConstantNode.forIntegerStamp(stamp, -i), view);
}
}
} else if (forX.isConstant()) {
Constant c = forX.asConstant();
if (ArithmeticOpTable.forStamp(stamp).getAdd().isNeutral(c)) {
/*
* Note that for floating point numbers, + and - have different neutral elements. We
* have to test for the neutral element of +, because we are doing this
* transformation: 0 - x == (-x) + 0 == -x.
*/
return NegateNode.create(forY, view);
}
if (mayReassociate && self != null) {
return reassociateMatchedValues(self, ValueNode.isConstantPredicate(), forX, forY, view);
}
}
if (forY instanceof NegateNode) {
return BinaryArithmeticNode.add(forX, ((NegateNode) forY).getValue(), view);
}
return self != null ? self : new SubNode(forX, forY);
}
@Override
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
NodeView view = NodeView.from(tool);
ValueNode ret = super.canonical(tool, forX, forY);
if (ret != this) {
return ret;
}
BinaryOp op = getOp(forX, forY);
return canonical(this, op, stamp, forX, forY, view);
}
@Override
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
nodeValueMap.setResult(this, gen.emitSub(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()), false));
}
protected boolean isExact() {
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy