1 /*
2  * Copyright (c) 2009, 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 
24 
25 package org.graalvm.compiler.nodes.calc;
26 
27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
29 
30 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
31 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.UnaryOp.Neg;
32 import org.graalvm.compiler.core.common.type.FloatStamp;
33 import org.graalvm.compiler.core.common.type.Stamp;
34 import org.graalvm.compiler.graph.NodeClass;
35 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
36 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
37 import org.graalvm.compiler.nodeinfo.NodeInfo;
38 import org.graalvm.compiler.nodes.NodeView;
39 import org.graalvm.compiler.nodes.ValueNode;
40 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
41 import org.graalvm.compiler.nodes.spi.StampInverter;
42 
43 /**
44  * The {@code NegateNode} node negates its operand.
45  */
46 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
47 public final class NegateNode extends UnaryArithmeticNode<Neg> implements NarrowableArithmeticNode, StampInverter {
48 
49     public static final NodeClass<NegateNode> TYPE = NodeClass.create(NegateNode.class);
50 
NegateNode(ValueNode value)51     public NegateNode(ValueNode value) {
52         super(TYPE, ArithmeticOpTable::getNeg, value);
53     }
54 
create(ValueNode value, NodeView view)55     public static ValueNode create(ValueNode value, NodeView view) {
56         ValueNode synonym = findSynonym(value, view);
57         if (synonym != null) {
58             return synonym;
59         }
60         return new NegateNode(value);
61     }
62 
63     @Override
canonical(CanonicalizerTool tool, ValueNode forValue)64     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
65         ValueNode synonym = findSynonym(forValue, getOp(forValue));
66         if (synonym != null) {
67             return synonym;
68         }
69         return this;
70     }
71 
findSynonym(ValueNode forValue, NodeView view)72     protected static ValueNode findSynonym(ValueNode forValue, NodeView view) {
73         ArithmeticOpTable.UnaryOp<Neg> negOp = ArithmeticOpTable.forStamp(forValue.stamp(view)).getNeg();
74         ValueNode synonym = UnaryArithmeticNode.findSynonym(forValue, negOp);
75         if (synonym != null) {
76             return synonym;
77         }
78         if (forValue instanceof NegateNode) {
79             return ((NegateNode) forValue).getValue();
80         }
81         if (forValue instanceof SubNode && !(forValue.stamp(view) instanceof FloatStamp)) {
82             SubNode sub = (SubNode) forValue;
83             return SubNode.create(sub.getY(), sub.getX(), view);
84         }
85         return null;
86     }
87 
88     @Override
generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen)89     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
90         nodeValueMap.setResult(this, gen.emitNegate(nodeValueMap.operand(getValue())));
91     }
92 
93     @Override
invertStamp(Stamp outStamp)94     public Stamp invertStamp(Stamp outStamp) {
95         return getArithmeticOp().foldStamp(outStamp);
96     }
97 }
98