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