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