1 /*
   2  * Copyright (c) 2011, 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 org.graalvm.compiler.core.common.calc.CanonicalCondition;
  28 import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
  29 import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
  30 import org.graalvm.compiler.core.common.type.ObjectStamp;
  31 import org.graalvm.compiler.core.common.type.TypeReference;
  32 import org.graalvm.compiler.debug.GraalError;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.ConstantNode;
  37 import org.graalvm.compiler.nodes.LogicConstantNode;
  38 import org.graalvm.compiler.nodes.LogicNode;
  39 import org.graalvm.compiler.nodes.NodeView;
  40 import org.graalvm.compiler.nodes.StructuredGraph;
  41 import org.graalvm.compiler.nodes.ValueNode;
  42 import org.graalvm.compiler.nodes.extended.GetClassNode;
  43 import org.graalvm.compiler.nodes.java.InstanceOfNode;
  44 import org.graalvm.compiler.nodes.spi.Virtualizable;
  45 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  46 import org.graalvm.compiler.nodes.virtual.VirtualBoxingNode;
  47 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  48 import org.graalvm.compiler.options.OptionValues;
  49 
  50 import jdk.vm.ci.meta.Constant;
  51 import jdk.vm.ci.meta.ConstantReflectionProvider;
  52 import jdk.vm.ci.meta.JavaConstant;
  53 import jdk.vm.ci.meta.JavaKind;
  54 import jdk.vm.ci.meta.MetaAccessProvider;
  55 import jdk.vm.ci.meta.ResolvedJavaType;
  56 
  57 @NodeInfo(shortName = "==")
  58 public final class ObjectEqualsNode extends PointerEqualsNode implements Virtualizable {
  59 
  60     public static final NodeClass<ObjectEqualsNode> TYPE = NodeClass.create(ObjectEqualsNode.class);
  61     private static final ObjectEqualsOp OP = new ObjectEqualsOp();
  62 
  63     public ObjectEqualsNode(ValueNode x, ValueNode y) {
  64         super(TYPE, x, y);
  65         assert x.stamp(NodeView.DEFAULT) instanceof AbstractObjectStamp;
  66         assert y.stamp(NodeView.DEFAULT) instanceof AbstractObjectStamp;
  67     }
  68 
  69     public static LogicNode create(ValueNode x, ValueNode y, ConstantReflectionProvider constantReflection, NodeView view) {
  70         LogicNode result = CompareNode.tryConstantFold(CanonicalCondition.EQ, x, y, constantReflection, false);
  71         if (result != null) {
  72             return result;
  73         } else {
  74             result = findSynonym(x, y, view);
  75             if (result != null) {
  76                 return result;
  77             }
  78             return new ObjectEqualsNode(x, y);
  79         }
  80     }
  81 
  82     public static LogicNode create(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, ValueNode x, ValueNode y, NodeView view) {
  83         LogicNode result = OP.canonical(constantReflection, metaAccess, options, null, CanonicalCondition.EQ, false, x, y, view);
  84         if (result != null) {
  85             return result;
  86         }
  87         return create(x, y, constantReflection, view);
  88     }
  89 
  90     @Override
  91     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  92         NodeView view = NodeView.from(tool);
  93         ValueNode value = OP.canonical(tool.getConstantReflection(), tool.getMetaAccess(), tool.getOptions(), tool.smallestCompareWidth(), CanonicalCondition.EQ, false, forX, forY, view);
  94         if (value != null) {
  95             return value;
  96         }
  97         return this;
  98     }
  99 
 100     public static class ObjectEqualsOp extends PointerEqualsOp {
 101 
 102         @Override
 103         protected LogicNode canonicalizeSymmetricConstant(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
 104                         CanonicalCondition condition, Constant constant, ValueNode nonConstant, boolean mirrored, boolean unorderedIsTrue, NodeView view) {
 105             ResolvedJavaType type = constantReflection.asJavaType(constant);
 106             if (type != null && nonConstant instanceof GetClassNode) {
 107                 GetClassNode getClassNode = (GetClassNode) nonConstant;
 108                 ValueNode object = getClassNode.getObject();
 109                 assert ((ObjectStamp) object.stamp(view)).nonNull();
 110                 if (!type.isPrimitive() && (type.isConcrete() || type.isArray())) {
 111                     return InstanceOfNode.create(TypeReference.createExactTrusted(type), object);
 112                 }
 113                 return LogicConstantNode.forBoolean(false);
 114             }
 115             return super.canonicalizeSymmetricConstant(constantReflection, metaAccess, options, smallestCompareWidth, condition, constant, nonConstant, mirrored, unorderedIsTrue, view);
 116         }
 117 
 118         @Override
 119         protected CompareNode duplicateModified(ValueNode newX, ValueNode newY, boolean unorderedIsTrue, NodeView view) {
 120             if (newX.stamp(view) instanceof ObjectStamp && newY.stamp(view) instanceof ObjectStamp) {
 121                 return new ObjectEqualsNode(newX, newY);
 122             } else if (newX.stamp(view) instanceof AbstractPointerStamp && newY.stamp(view) instanceof AbstractPointerStamp) {
 123                 return new PointerEqualsNode(newX, newY);
 124             }
 125             throw GraalError.shouldNotReachHere();
 126         }
 127     }
 128 
 129     private static LogicNode virtualizeNonVirtualComparison(VirtualObjectNode virtual, ValueNode other, StructuredGraph graph, VirtualizerTool tool) {
 130         if (virtual instanceof VirtualBoxingNode && other.isConstant()) {
 131             VirtualBoxingNode virtualBoxingNode = (VirtualBoxingNode) virtual;
 132             if (virtualBoxingNode.getBoxingKind() == JavaKind.Boolean) {
 133                 JavaConstant otherUnboxed = tool.getConstantReflection().unboxPrimitive(other.asJavaConstant());
 134                 if (otherUnboxed != null && otherUnboxed.getJavaKind() == JavaKind.Boolean) {
 135                     int expectedValue = otherUnboxed.asBoolean() ? 1 : 0;
 136                     return new IntegerEqualsNode(virtualBoxingNode.getBoxedValue(tool), ConstantNode.forInt(expectedValue, graph));
 137                 } else {
 138                     return LogicConstantNode.contradiction(graph);
 139                 }
 140             }
 141         }
 142         if (virtual.hasIdentity()) {
 143             // one of them is virtual: they can never be the same objects
 144             return LogicConstantNode.contradiction(graph);
 145         }
 146         return null;
 147     }
 148 
 149     public static LogicNode virtualizeComparison(ValueNode x, ValueNode y, StructuredGraph graph, VirtualizerTool tool) {
 150         ValueNode xAlias = tool.getAlias(x);
 151         ValueNode yAlias = tool.getAlias(y);
 152 
 153         VirtualObjectNode xVirtual = xAlias instanceof VirtualObjectNode ? (VirtualObjectNode) xAlias : null;
 154         VirtualObjectNode yVirtual = yAlias instanceof VirtualObjectNode ? (VirtualObjectNode) yAlias : null;
 155 
 156         if (xVirtual != null && yVirtual == null) {
 157             return virtualizeNonVirtualComparison(xVirtual, yAlias, graph, tool);
 158         } else if (xVirtual == null && yVirtual != null) {
 159             return virtualizeNonVirtualComparison(yVirtual, xAlias, graph, tool);
 160         } else if (xVirtual != null && yVirtual != null) {
 161             if (xVirtual.hasIdentity() ^ yVirtual.hasIdentity()) {
 162                 /*
 163                  * One of the two objects has identity, the other doesn't. In code, this looks like
 164                  * "Integer.valueOf(a) == new Integer(b)", which is always false.
 165                  *
 166                  * In other words: an object created via valueOf can never be equal to one created
 167                  * by new in the same compilation unit.
 168                  */
 169                 return LogicConstantNode.contradiction(graph);
 170             } else if (!xVirtual.hasIdentity() && !yVirtual.hasIdentity()) {
 171                 ResolvedJavaType type = xVirtual.type();
 172                 if (type.equals(yVirtual.type())) {
 173                     MetaAccessProvider metaAccess = tool.getMetaAccess();
 174                     if (type.equals(metaAccess.lookupJavaType(Integer.class)) || type.equals(metaAccess.lookupJavaType(Long.class))) {
 175                         // both are virtual without identity: check contents
 176                         assert xVirtual.entryCount() == 1 && yVirtual.entryCount() == 1;
 177                         assert xVirtual.entryKind(0).getStackKind() == JavaKind.Int || xVirtual.entryKind(0) == JavaKind.Long;
 178                         return new IntegerEqualsNode(tool.getEntry(xVirtual, 0), tool.getEntry(yVirtual, 0));
 179                     }
 180                 }
 181             } else {
 182                 // both are virtual with identity: check if they refer to the same object
 183                 return LogicConstantNode.forBoolean(xVirtual == yVirtual, graph);
 184             }
 185         }
 186         return null;
 187     }
 188 
 189     @Override
 190     public void virtualize(VirtualizerTool tool) {
 191         ValueNode node = virtualizeComparison(getX(), getY(), graph(), tool);
 192         if (node == null) {
 193             return;
 194         }
 195         if (!node.isAlive()) {
 196             tool.addNode(node);
 197         }
 198         tool.replaceWithValue(node);
 199     }
 200 }