001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.calc;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.calc.*;
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.Canonicalizable.BinaryCommutative;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.util.*;
035
036@NodeInfo(shortName = "==")
037public class PointerEqualsNode extends CompareNode implements BinaryCommutative<ValueNode> {
038
039    public static final NodeClass<PointerEqualsNode> TYPE = NodeClass.create(PointerEqualsNode.class);
040
041    public PointerEqualsNode(ValueNode x, ValueNode y) {
042        this(TYPE, x, y);
043    }
044
045    public static LogicNode create(ValueNode x, ValueNode y) {
046        LogicNode result = findSynonym(x, y);
047        if (result != null) {
048            return result;
049        }
050        return new PointerEqualsNode(x, y);
051    }
052
053    protected PointerEqualsNode(NodeClass<? extends PointerEqualsNode> c, ValueNode x, ValueNode y) {
054        super(c, Condition.EQ, false, x, y);
055        assert x.stamp() instanceof AbstractPointerStamp;
056        assert y.stamp() instanceof AbstractPointerStamp;
057    }
058
059    @Override
060    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
061        LogicNode result = findSynonym(forX, forY);
062        if (result != null) {
063            return result;
064        }
065        return super.canonical(tool, forX, forY);
066    }
067
068    public static LogicNode findSynonym(ValueNode forX, ValueNode forY) {
069        if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
070            return LogicConstantNode.tautology();
071        } else if (forX.stamp().alwaysDistinct(forY.stamp())) {
072            return LogicConstantNode.contradiction();
073        } else if (((AbstractPointerStamp) forX.stamp()).alwaysNull()) {
074            return new IsNullNode(forY);
075        } else if (((AbstractPointerStamp) forY.stamp()).alwaysNull()) {
076            return new IsNullNode(forX);
077        } else {
078            return null;
079        }
080    }
081
082    @Override
083    protected CompareNode duplicateModified(ValueNode newX, ValueNode newY) {
084        return new PointerEqualsNode(newX, newY);
085    }
086
087    @Override
088    public Stamp getSucceedingStampForX(boolean negated) {
089        if (!negated) {
090            Stamp xStamp = getX().stamp();
091            Stamp newStamp = xStamp.join(getY().stamp());
092            if (!newStamp.equals(xStamp)) {
093                return newStamp;
094            }
095        }
096        return null;
097    }
098
099    @Override
100    public Stamp getSucceedingStampForY(boolean negated) {
101        if (!negated) {
102            Stamp yStamp = getY().stamp();
103            Stamp newStamp = yStamp.join(getX().stamp());
104            if (!newStamp.equals(yStamp)) {
105                return newStamp;
106            }
107        }
108        return null;
109    }
110
111    @Override
112    public TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
113        if (xStampGeneric instanceof ObjectStamp && yStampGeneric instanceof ObjectStamp) {
114            ObjectStamp xStamp = (ObjectStamp) xStampGeneric;
115            ObjectStamp yStamp = (ObjectStamp) yStampGeneric;
116            if (xStamp.alwaysDistinct(yStamp)) {
117                return TriState.FALSE;
118            } else if (xStamp.neverDistinct(yStamp)) {
119                return TriState.TRUE;
120            }
121        }
122        return TriState.UNKNOWN;
123    }
124}