LogicNegationNode.java revision 12651:6ef01bd40ce2
1356290Sjkim/*
2110010Smarkm * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3110010Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4142429Snectar *
5110010Smarkm * This code is free software; you can redistribute it and/or modify it
6110010Smarkm * under the terms of the GNU General Public License version 2 only, as
7110010Smarkm * published by the Free Software Foundation.
8110010Smarkm *
9110010Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT
10110010Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11110010Smarkm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12110010Smarkm * version 2 for more details (a copy is included in the LICENSE file that
13110010Smarkm * accompanied this code).
14110010Smarkm *
15110010Smarkm * You should have received a copy of the GNU General Public License version
16110010Smarkm * 2 along with this work; if not, write to the Free Software Foundation,
17110010Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18110010Smarkm *
19110010Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20215698Ssimon * or visit www.oracle.com if you need additional information or have any
21215698Ssimon * questions.
22215698Ssimon */
23215698Ssimonpackage org.graalvm.compiler.nodes;
24215698Ssimon
25110010Smarkmimport static org.graalvm.compiler.nodeinfo.InputType.Condition;
26110010Smarkmimport static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
27110010Smarkmimport static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
28110010Smarkm
29110010Smarkmimport org.graalvm.compiler.graph.NodeClass;
30110010Smarkmimport org.graalvm.compiler.graph.spi.Canonicalizable;
31110010Smarkmimport org.graalvm.compiler.graph.spi.CanonicalizerTool;
32110010Smarkmimport org.graalvm.compiler.nodeinfo.NodeInfo;
33110010Smarkm
34110010Smarkm/**
35110010Smarkm * Logic node that negates its argument.
36110010Smarkm */
37110010Smarkm@NodeInfo(cycles = CYCLES_0, size = SIZE_0)
38110010Smarkmpublic final class LogicNegationNode extends LogicNode implements Canonicalizable.Unary<LogicNode> {
39110010Smarkm
40110010Smarkm    public static final NodeClass<LogicNegationNode> TYPE = NodeClass.create(LogicNegationNode.class);
41276861Sjkim    @Input(Condition) LogicNode value;
42276861Sjkim
43110010Smarkm    public LogicNegationNode(LogicNode value) {
44110010Smarkm        super(TYPE);
45215698Ssimon        this.value = value;
46215698Ssimon    }
47215698Ssimon
48215698Ssimon    public static LogicNode create(LogicNode value) {
49312826Sjkim        LogicNode synonym = findSynonym(value);
50215698Ssimon        if (synonym != null) {
51142429Snectar            return synonym;
52142429Snectar        }
53276861Sjkim        return new LogicNegationNode(value);
54276861Sjkim    }
55276861Sjkim
56110010Smarkm    private static LogicNode findSynonym(LogicNode value) {
57344604Sjkim        if (value instanceof LogicConstantNode) {
58344604Sjkim            LogicConstantNode logicConstantNode = (LogicConstantNode) value;
59344604Sjkim            return LogicConstantNode.forBoolean(!logicConstantNode.getValue());
60344604Sjkim        } else if (value instanceof LogicNegationNode) {
61344604Sjkim            return ((LogicNegationNode) value).getValue();
62344604Sjkim        }
63215698Ssimon        return null;
64344604Sjkim    }
65344604Sjkim
66344604Sjkim    @Override
67344604Sjkim    public LogicNode getValue() {
68276861Sjkim        return value;
69215698Ssimon    }
70344604Sjkim
71110010Smarkm    @Override
72110010Smarkm    public LogicNode canonical(CanonicalizerTool tool, LogicNode forValue) {
73110010Smarkm        LogicNode synonym = findSynonym(forValue);
74110010Smarkm        if (synonym != null) {
75110010Smarkm            return synonym;
76110010Smarkm        }
77110010Smarkm        return this;
78110010Smarkm    }
79110010Smarkm
80110010Smarkm}
81110010Smarkm