43972
|
1 |
/*
|
58533
|
2 |
* Copyright (c) 2014, 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 |
|
58533
|
27 |
import static org.graalvm.compiler.nodes.calc.BinaryArithmeticNode.getArithmeticOpTable;
|
43972
|
28 |
|
|
29 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
|
|
30 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.UnaryOp;
|
|
31 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
32 |
import org.graalvm.compiler.graph.NodeClass;
|
|
33 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
34 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
35 |
import org.graalvm.compiler.nodes.ArithmeticOperation;
|
|
36 |
import org.graalvm.compiler.nodes.ConstantNode;
|
48190
|
37 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
38 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
39 |
import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
|
|
40 |
|
|
41 |
@NodeInfo
|
|
42 |
public abstract class UnaryArithmeticNode<OP> extends UnaryNode implements ArithmeticOperation, ArithmeticLIRLowerable {
|
|
43 |
|
|
44 |
@SuppressWarnings("rawtypes") public static final NodeClass<UnaryArithmeticNode> TYPE = NodeClass.create(UnaryArithmeticNode.class);
|
|
45 |
|
58533
|
46 |
protected UnaryArithmeticNode(NodeClass<? extends UnaryArithmeticNode<OP>> c, UnaryOp<OP> opForStampComputation, ValueNode value) {
|
|
47 |
super(c, opForStampComputation.foldStamp(value.stamp(NodeView.DEFAULT)), value);
|
43972
|
48 |
}
|
|
49 |
|
58533
|
50 |
protected abstract UnaryOp<OP> getOp(ArithmeticOpTable table);
|
43972
|
51 |
|
|
52 |
protected final UnaryOp<OP> getOp(ValueNode forValue) {
|
58533
|
53 |
return getOp(getArithmeticOpTable(forValue));
|
43972
|
54 |
}
|
|
55 |
|
|
56 |
@Override
|
|
57 |
public final UnaryOp<OP> getArithmeticOp() {
|
|
58 |
return getOp(getValue());
|
|
59 |
}
|
|
60 |
|
|
61 |
@Override
|
|
62 |
public Stamp foldStamp(Stamp newStamp) {
|
48190
|
63 |
assert newStamp.isCompatible(getValue().stamp(NodeView.DEFAULT));
|
43972
|
64 |
return getOp(getValue()).foldStamp(newStamp);
|
|
65 |
}
|
|
66 |
|
|
67 |
@Override
|
|
68 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
|
|
69 |
ValueNode synonym = findSynonym(forValue, getOp(forValue));
|
|
70 |
if (synonym != null) {
|
|
71 |
return synonym;
|
|
72 |
}
|
|
73 |
return this;
|
|
74 |
}
|
|
75 |
|
|
76 |
protected static <OP> ValueNode findSynonym(ValueNode forValue, UnaryOp<OP> op) {
|
|
77 |
if (forValue.isConstant()) {
|
48190
|
78 |
return ConstantNode.forPrimitive(op.foldStamp(forValue.stamp(NodeView.DEFAULT)), op.foldConstant(forValue.asConstant()));
|
43972
|
79 |
}
|
|
80 |
return null;
|
|
81 |
}
|
|
82 |
}
|