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.common.*; 026import jdk.internal.jvmci.meta.*; 027 028import com.oracle.graal.compiler.common.calc.*; 029import com.oracle.graal.compiler.common.type.*; 030import com.oracle.graal.graph.*; 031import com.oracle.graal.graph.spi.*; 032import com.oracle.graal.nodeinfo.*; 033import com.oracle.graal.nodes.*; 034import com.oracle.graal.nodes.extended.*; 035import com.oracle.graal.nodes.java.*; 036import com.oracle.graal.nodes.spi.*; 037import com.oracle.graal.nodes.virtual.*; 038 039@NodeInfo(shortName = "==") 040public final class ObjectEqualsNode extends PointerEqualsNode implements Virtualizable { 041 042 public static final NodeClass<ObjectEqualsNode> TYPE = NodeClass.create(ObjectEqualsNode.class); 043 044 public ObjectEqualsNode(ValueNode x, ValueNode y) { 045 super(TYPE, x, y); 046 assert x.stamp() instanceof AbstractObjectStamp; 047 assert y.stamp() instanceof AbstractObjectStamp; 048 } 049 050 public static LogicNode create(ValueNode x, ValueNode y, ConstantReflectionProvider constantReflection) { 051 LogicNode result = CompareNode.tryConstantFold(Condition.EQ, x, y, constantReflection, false); 052 if (result != null) { 053 return result; 054 } else { 055 result = findSynonym(x, y); 056 if (result != null) { 057 return result; 058 } 059 return new ObjectEqualsNode(x, y); 060 } 061 } 062 063 @Override 064 protected ValueNode canonicalizeSymmetricConstant(CanonicalizerTool tool, Constant constant, ValueNode nonConstant, boolean mirrored) { 065 ResolvedJavaType type = tool.getConstantReflection().asJavaType(constant); 066 if (type != null && nonConstant instanceof GetClassNode) { 067 if (!type.isPrimitive() && (type.isConcrete() || type.isArray())) { 068 return TypeCheckNode.create(type, ((GetClassNode) nonConstant).getObject()); 069 } 070 return LogicConstantNode.forBoolean(false); 071 } 072 return super.canonicalizeSymmetricConstant(tool, constant, nonConstant, mirrored); 073 } 074 075 private void virtualizeNonVirtualComparison(VirtualObjectNode virtual, ValueNode other, VirtualizerTool tool) { 076 if (!virtual.hasIdentity() && virtual.entryKind(0) == Kind.Boolean) { 077 if (other.isConstant()) { 078 JavaConstant otherUnboxed = tool.getConstantReflectionProvider().unboxPrimitive(other.asJavaConstant()); 079 if (otherUnboxed != null && otherUnboxed.getKind() == Kind.Boolean) { 080 int expectedValue = otherUnboxed.asBoolean() ? 1 : 0; 081 IntegerEqualsNode equals = new IntegerEqualsNode(tool.getEntry(virtual, 0), ConstantNode.forInt(expectedValue, graph())); 082 tool.addNode(equals); 083 tool.replaceWithValue(equals); 084 } else { 085 tool.replaceWithValue(LogicConstantNode.contradiction(graph())); 086 } 087 } 088 } else { 089 // one of them is virtual: they can never be the same objects 090 tool.replaceWithValue(LogicConstantNode.contradiction(graph())); 091 } 092 } 093 094 @Override 095 public void virtualize(VirtualizerTool tool) { 096 ValueNode xAlias = tool.getAlias(getX()); 097 ValueNode yAlias = tool.getAlias(getY()); 098 099 VirtualObjectNode xVirtual = xAlias instanceof VirtualObjectNode ? (VirtualObjectNode) xAlias : null; 100 VirtualObjectNode yVirtual = yAlias instanceof VirtualObjectNode ? (VirtualObjectNode) yAlias : null; 101 102 if (xVirtual != null && yVirtual == null) { 103 virtualizeNonVirtualComparison(xVirtual, yAlias, tool); 104 } else if (xVirtual == null && yVirtual != null) { 105 virtualizeNonVirtualComparison(yVirtual, xAlias, tool); 106 } else if (xVirtual != null && yVirtual != null) { 107 if (xVirtual.hasIdentity() ^ yVirtual.hasIdentity()) { 108 /* 109 * One of the two objects has identity, the other doesn't. In code, this looks like 110 * "Integer.valueOf(a) == new Integer(b)", which is always false. 111 * 112 * In other words: an object created via valueOf can never be equal to one created 113 * by new in the same compilation unit. 114 */ 115 tool.replaceWithValue(LogicConstantNode.contradiction(graph())); 116 } else if (!xVirtual.hasIdentity() && !yVirtual.hasIdentity()) { 117 ResolvedJavaType type = xVirtual.type(); 118 if (type.equals(yVirtual.type())) { 119 MetaAccessProvider metaAccess = tool.getMetaAccessProvider(); 120 if (type.equals(metaAccess.lookupJavaType(Integer.class)) || type.equals(metaAccess.lookupJavaType(Long.class))) { 121 // both are virtual without identity: check contents 122 assert xVirtual.entryCount() == 1 && yVirtual.entryCount() == 1; 123 assert xVirtual.entryKind(0).getStackKind() == Kind.Int || xVirtual.entryKind(0) == Kind.Long; 124 IntegerEqualsNode equals = new IntegerEqualsNode(tool.getEntry(xVirtual, 0), tool.getEntry(yVirtual, 0)); 125 tool.addNode(equals); 126 tool.replaceWithValue(equals); 127 } 128 } 129 } else { 130 // both are virtual with identity: check if they refer to the same object 131 tool.replaceWithValue(LogicConstantNode.forBoolean(xVirtual == yVirtual, graph())); 132 } 133 } 134 } 135 136 @Override 137 protected CompareNode duplicateModified(ValueNode newX, ValueNode newY) { 138 if (newX.stamp() instanceof ObjectStamp && newY.stamp() instanceof ObjectStamp) { 139 return new ObjectEqualsNode(newX, newY); 140 } else if (newX.stamp() instanceof AbstractPointerStamp && newY.stamp() instanceof AbstractPointerStamp) { 141 return new PointerEqualsNode(newX, newY); 142 } 143 throw JVMCIError.shouldNotReachHere(); 144 } 145}