43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 2015, 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 org.graalvm.compiler.nodes.calc;
|
|
24 |
|
|
25 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
|
|
26 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp;
|
|
27 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.Sub;
|
|
28 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
29 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
30 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
31 |
import org.graalvm.compiler.graph.NodeClass;
|
|
32 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
33 |
import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
|
|
34 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
35 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
36 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
37 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
38 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
|
39 |
|
|
40 |
import jdk.vm.ci.meta.Constant;
|
|
41 |
import jdk.vm.ci.meta.PrimitiveConstant;
|
|
42 |
|
|
43 |
@NodeInfo(shortName = "-")
|
|
44 |
public class SubNode extends BinaryArithmeticNode<Sub> implements NarrowableArithmeticNode {
|
|
45 |
|
|
46 |
public static final NodeClass<SubNode> TYPE = NodeClass.create(SubNode.class);
|
|
47 |
|
|
48 |
public SubNode(ValueNode x, ValueNode y) {
|
|
49 |
this(TYPE, x, y);
|
|
50 |
}
|
|
51 |
|
|
52 |
protected SubNode(NodeClass<? extends SubNode> c, ValueNode x, ValueNode y) {
|
|
53 |
super(c, ArithmeticOpTable::getSub, x, y);
|
|
54 |
}
|
|
55 |
|
|
56 |
public static ValueNode create(ValueNode x, ValueNode y) {
|
|
57 |
BinaryOp<Sub> op = ArithmeticOpTable.forStamp(x.stamp()).getSub();
|
|
58 |
Stamp stamp = op.foldStamp(x.stamp(), y.stamp());
|
|
59 |
ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp);
|
|
60 |
if (tryConstantFold != null) {
|
|
61 |
return tryConstantFold;
|
|
62 |
}
|
46371
|
63 |
return canonical(null, op, stamp, x, y);
|
43972
|
64 |
}
|
|
65 |
|
46371
|
66 |
private static ValueNode canonical(SubNode subNode, BinaryOp<Sub> op, Stamp stamp, ValueNode forX, ValueNode forY) {
|
|
67 |
SubNode self = subNode;
|
43972
|
68 |
if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
|
|
69 |
Constant zero = op.getZero(forX.stamp());
|
|
70 |
if (zero != null) {
|
46371
|
71 |
return ConstantNode.forPrimitive(stamp, zero);
|
43972
|
72 |
}
|
|
73 |
}
|
|
74 |
boolean associative = op.isAssociative();
|
|
75 |
if (associative) {
|
|
76 |
if (forX instanceof AddNode) {
|
|
77 |
AddNode x = (AddNode) forX;
|
|
78 |
if (x.getY() == forY) {
|
|
79 |
// (a + b) - b
|
|
80 |
return x.getX();
|
|
81 |
}
|
|
82 |
if (x.getX() == forY) {
|
|
83 |
// (a + b) - a
|
|
84 |
return x.getY();
|
|
85 |
}
|
|
86 |
} else if (forX instanceof SubNode) {
|
|
87 |
SubNode x = (SubNode) forX;
|
|
88 |
if (x.getX() == forY) {
|
|
89 |
// (a - b) - a
|
46371
|
90 |
return NegateNode.create(x.getY());
|
43972
|
91 |
}
|
|
92 |
}
|
|
93 |
if (forY instanceof AddNode) {
|
|
94 |
AddNode y = (AddNode) forY;
|
|
95 |
if (y.getX() == forX) {
|
|
96 |
// a - (a + b)
|
46371
|
97 |
return NegateNode.create(y.getY());
|
43972
|
98 |
}
|
|
99 |
if (y.getY() == forX) {
|
|
100 |
// b - (a + b)
|
46371
|
101 |
return NegateNode.create(y.getX());
|
43972
|
102 |
}
|
|
103 |
} else if (forY instanceof SubNode) {
|
|
104 |
SubNode y = (SubNode) forY;
|
|
105 |
if (y.getX() == forX) {
|
|
106 |
// a - (a - b)
|
|
107 |
return y.getY();
|
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|
|
111 |
if (forY.isConstant()) {
|
|
112 |
Constant c = forY.asConstant();
|
|
113 |
if (op.isNeutral(c)) {
|
|
114 |
return forX;
|
|
115 |
}
|
46371
|
116 |
if (associative && self != null) {
|
|
117 |
ValueNode reassociated = reassociate(self, ValueNode.isConstantPredicate(), forX, forY);
|
|
118 |
if (reassociated != self) {
|
43972
|
119 |
return reassociated;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) {
|
|
123 |
long i = ((PrimitiveConstant) c).asLong();
|
|
124 |
if (i < 0 || ((IntegerStamp) StampFactory.forKind(forY.getStackKind())).contains(-i)) {
|
|
125 |
// Adding a negative is more friendly to the backend since adds are
|
|
126 |
// commutative, so prefer add when it fits.
|
46371
|
127 |
return BinaryArithmeticNode.add(forX, ConstantNode.forIntegerStamp(stamp, -i));
|
43972
|
128 |
}
|
|
129 |
}
|
|
130 |
} else if (forX.isConstant()) {
|
|
131 |
Constant c = forX.asConstant();
|
46371
|
132 |
if (ArithmeticOpTable.forStamp(stamp).getAdd().isNeutral(c)) {
|
43972
|
133 |
/*
|
|
134 |
* Note that for floating point numbers, + and - have different neutral elements. We
|
|
135 |
* have to test for the neutral element of +, because we are doing this
|
|
136 |
* transformation: 0 - x == (-x) + 0 == -x.
|
|
137 |
*/
|
46371
|
138 |
return NegateNode.create(forY);
|
43972
|
139 |
}
|
46371
|
140 |
if (associative && self != null) {
|
|
141 |
return reassociate(self, ValueNode.isConstantPredicate(), forX, forY);
|
43972
|
142 |
}
|
|
143 |
}
|
|
144 |
if (forY instanceof NegateNode) {
|
|
145 |
return BinaryArithmeticNode.add(forX, ((NegateNode) forY).getValue());
|
|
146 |
}
|
46371
|
147 |
if (self == null) {
|
|
148 |
self = new SubNode(forX, forY);
|
|
149 |
}
|
|
150 |
return self;
|
|
151 |
}
|
|
152 |
|
|
153 |
@Override
|
|
154 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
|
155 |
ValueNode ret = super.canonical(tool, forX, forY);
|
|
156 |
if (ret != this) {
|
|
157 |
return ret;
|
|
158 |
}
|
|
159 |
|
|
160 |
BinaryOp<Sub> op = getOp(forX, forY);
|
|
161 |
return canonical(this, op, stamp, forX, forY);
|
43972
|
162 |
}
|
|
163 |
|
|
164 |
@Override
|
|
165 |
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
|
|
166 |
nodeValueMap.setResult(this, gen.emitSub(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()), false));
|
|
167 |
}
|
|
168 |
}
|