43972
|
1 |
/*
|
58533
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.nodes.calc;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
|
|
28 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp;
|
|
29 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.Add;
|
52578
|
30 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
43972
|
31 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
32 |
import org.graalvm.compiler.graph.NodeClass;
|
|
33 |
import org.graalvm.compiler.graph.spi.Canonicalizable.BinaryCommutative;
|
|
34 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
35 |
import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
|
|
36 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
37 |
import org.graalvm.compiler.nodes.ConstantNode;
|
48190
|
38 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
39 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
40 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
41 |
|
52578
|
42 |
import jdk.vm.ci.code.CodeUtil;
|
43972
|
43 |
import jdk.vm.ci.meta.Constant;
|
52578
|
44 |
import jdk.vm.ci.meta.JavaConstant;
|
43972
|
45 |
import jdk.vm.ci.meta.Value;
|
|
46 |
|
|
47 |
@NodeInfo(shortName = "+")
|
|
48 |
public class AddNode extends BinaryArithmeticNode<Add> implements NarrowableArithmeticNode, BinaryCommutative<ValueNode> {
|
|
49 |
|
|
50 |
public static final NodeClass<AddNode> TYPE = NodeClass.create(AddNode.class);
|
|
51 |
|
|
52 |
public AddNode(ValueNode x, ValueNode y) {
|
|
53 |
this(TYPE, x, y);
|
|
54 |
}
|
|
55 |
|
|
56 |
protected AddNode(NodeClass<? extends AddNode> c, ValueNode x, ValueNode y) {
|
58533
|
57 |
super(c, getArithmeticOpTable(x).getAdd(), x, y);
|
43972
|
58 |
}
|
|
59 |
|
48190
|
60 |
public static ValueNode create(ValueNode x, ValueNode y, NodeView view) {
|
|
61 |
BinaryOp<Add> op = ArithmeticOpTable.forStamp(x.stamp(view)).getAdd();
|
|
62 |
Stamp stamp = op.foldStamp(x.stamp(view), y.stamp(view));
|
|
63 |
ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp, view);
|
43972
|
64 |
if (tryConstantFold != null) {
|
|
65 |
return tryConstantFold;
|
46371
|
66 |
}
|
|
67 |
if (x.isConstant() && !y.isConstant()) {
|
48190
|
68 |
return canonical(null, op, y, x, view);
|
43972
|
69 |
} else {
|
48190
|
70 |
return canonical(null, op, x, y, view);
|
43972
|
71 |
}
|
|
72 |
}
|
|
73 |
|
58533
|
74 |
@Override
|
|
75 |
protected BinaryOp<Add> getOp(ArithmeticOpTable table) {
|
|
76 |
return table.getAdd();
|
|
77 |
}
|
|
78 |
|
48190
|
79 |
private static ValueNode canonical(AddNode addNode, BinaryOp<Add> op, ValueNode forX, ValueNode forY, NodeView view) {
|
46371
|
80 |
AddNode self = addNode;
|
43972
|
81 |
boolean associative = op.isAssociative();
|
|
82 |
if (associative) {
|
|
83 |
if (forX instanceof SubNode) {
|
|
84 |
SubNode sub = (SubNode) forX;
|
|
85 |
if (sub.getY() == forY) {
|
|
86 |
// (a - b) + b
|
|
87 |
return sub.getX();
|
|
88 |
}
|
|
89 |
}
|
|
90 |
if (forY instanceof SubNode) {
|
|
91 |
SubNode sub = (SubNode) forY;
|
|
92 |
if (sub.getY() == forX) {
|
|
93 |
// b + (a - b)
|
|
94 |
return sub.getX();
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
if (forY.isConstant()) {
|
|
99 |
Constant c = forY.asConstant();
|
|
100 |
if (op.isNeutral(c)) {
|
|
101 |
return forX;
|
|
102 |
}
|
46371
|
103 |
if (associative && self != null) {
|
43972
|
104 |
// canonicalize expressions like "(a + 1) + 2"
|
48190
|
105 |
ValueNode reassociated = reassociate(self, ValueNode.isConstantPredicate(), forX, forY, view);
|
46371
|
106 |
if (reassociated != self) {
|
43972
|
107 |
return reassociated;
|
|
108 |
}
|
|
109 |
}
|
52578
|
110 |
|
|
111 |
// Attempt to optimize the pattern of an extend node between two add nodes.
|
|
112 |
if (c instanceof JavaConstant && (forX instanceof SignExtendNode || forX instanceof ZeroExtendNode)) {
|
|
113 |
IntegerConvertNode<?, ?> integerConvertNode = (IntegerConvertNode<?, ?>) forX;
|
|
114 |
ValueNode valueNode = integerConvertNode.getValue();
|
|
115 |
long constant = ((JavaConstant) c).asLong();
|
|
116 |
if (valueNode instanceof AddNode) {
|
|
117 |
AddNode addBeforeExtend = (AddNode) valueNode;
|
|
118 |
if (addBeforeExtend.getY().isConstant()) {
|
|
119 |
// There is a second add before the extend node that also has a constant as
|
|
120 |
// second operand. Therefore there will be canonicalizations triggered if we
|
|
121 |
// can move the add above the extension. For this we need to check whether
|
|
122 |
// the result of the addition is the same before the extension (which can be
|
|
123 |
// either zero extend or sign extend).
|
|
124 |
IntegerStamp beforeExtendStamp = (IntegerStamp) addBeforeExtend.stamp(view);
|
|
125 |
int bits = beforeExtendStamp.getBits();
|
|
126 |
if (constant >= CodeUtil.minValue(bits) && constant <= CodeUtil.maxValue(bits)) {
|
|
127 |
IntegerStamp narrowConstantStamp = IntegerStamp.create(bits, constant, constant);
|
|
128 |
|
|
129 |
if (!IntegerStamp.addCanOverflow(narrowConstantStamp, beforeExtendStamp)) {
|
|
130 |
ConstantNode constantNode = ConstantNode.forIntegerStamp(narrowConstantStamp, constant);
|
|
131 |
if (forX instanceof SignExtendNode) {
|
|
132 |
return SignExtendNode.create(AddNode.create(addBeforeExtend, constantNode, view), integerConvertNode.getResultBits(), view);
|
|
133 |
} else {
|
|
134 |
assert forX instanceof ZeroExtendNode;
|
|
135 |
|
|
136 |
// Must check to not cross zero with the new add.
|
|
137 |
boolean crossesZeroPoint = true;
|
|
138 |
if (constant > 0) {
|
|
139 |
if (beforeExtendStamp.lowerBound() >= 0 || beforeExtendStamp.upperBound() < -constant) {
|
|
140 |
// We are good here.
|
|
141 |
crossesZeroPoint = false;
|
|
142 |
}
|
|
143 |
} else {
|
|
144 |
if (beforeExtendStamp.lowerBound() >= -constant || beforeExtendStamp.upperBound() < 0) {
|
|
145 |
// We are good here as well.
|
|
146 |
crossesZeroPoint = false;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
if (!crossesZeroPoint) {
|
|
150 |
return ZeroExtendNode.create(AddNode.create(addBeforeExtend, constantNode, view), integerConvertNode.getResultBits(), view);
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
43972
|
158 |
}
|
|
159 |
if (forX instanceof NegateNode) {
|
48190
|
160 |
return BinaryArithmeticNode.sub(forY, ((NegateNode) forX).getValue(), view);
|
43972
|
161 |
} else if (forY instanceof NegateNode) {
|
48190
|
162 |
return BinaryArithmeticNode.sub(forX, ((NegateNode) forY).getValue(), view);
|
43972
|
163 |
}
|
46371
|
164 |
if (self == null) {
|
|
165 |
self = (AddNode) new AddNode(forX, forY).maybeCommuteInputs();
|
|
166 |
}
|
|
167 |
return self;
|
|
168 |
}
|
|
169 |
|
|
170 |
@Override
|
|
171 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
|
172 |
ValueNode ret = super.canonical(tool, forX, forY);
|
|
173 |
if (ret != this) {
|
|
174 |
return ret;
|
|
175 |
}
|
|
176 |
|
|
177 |
if (forX.isConstant() && !forY.isConstant()) {
|
|
178 |
// we try to swap and canonicalize
|
|
179 |
ValueNode improvement = canonical(tool, forY, forX);
|
|
180 |
if (improvement != this) {
|
|
181 |
return improvement;
|
|
182 |
}
|
|
183 |
// if this fails we only swap
|
|
184 |
return new AddNode(forY, forX);
|
|
185 |
}
|
|
186 |
BinaryOp<Add> op = getOp(forX, forY);
|
48190
|
187 |
NodeView view = NodeView.from(tool);
|
|
188 |
return canonical(this, op, forX, forY, view);
|
43972
|
189 |
}
|
|
190 |
|
|
191 |
@Override
|
|
192 |
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
|
|
193 |
Value op1 = nodeValueMap.operand(getX());
|
|
194 |
assert op1 != null : getX() + ", this=" + this;
|
|
195 |
Value op2 = nodeValueMap.operand(getY());
|
|
196 |
if (shouldSwapInputs(nodeValueMap)) {
|
|
197 |
Value tmp = op1;
|
|
198 |
op1 = op2;
|
|
199 |
op2 = tmp;
|
|
200 |
}
|
|
201 |
nodeValueMap.setResult(this, gen.emitAdd(op1, op2, false));
|
|
202 |
}
|
|
203 |
}
|