# HG changeset patch # User Doug Simon # Date 1427967932 -7200 # Node ID 987b4c42aef9f1cafd5f9230d01706378c15d235 # Parent 9dcf9f8779c228c533fba7a1b651e3dbdc5606d4 add GetClassNode and use it in a non-HotSpot specific InvocationPlugin for Object.getClass() diff -r 9dcf9f8779c2 -r 987b4c42aef9 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/DefaultHotSpotLoweringProvider.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/DefaultHotSpotLoweringProvider.java Thu Apr 02 10:00:21 2015 +0200 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/DefaultHotSpotLoweringProvider.java Thu Apr 02 11:45:32 2015 +0200 @@ -100,6 +100,8 @@ lowerInvoke((Invoke) n, tool, graph); } else if (n instanceof LoadMethodNode) { lowerLoadMethodNode((LoadMethodNode) n); + } else if (n instanceof GetClassNode) { + lowerGetClassNode((GetClassNode) n, tool, graph); } else if (n instanceof StoreHubNode) { lowerStoreHubNode((StoreHubNode) n, graph); } else if (n instanceof OSRStartNode) { @@ -326,6 +328,15 @@ graph.replaceFixed(loadMethodNode, metaspaceMethod); } + private static void lowerGetClassNode(GetClassNode getClass, LoweringTool tool, StructuredGraph graph) { + StampProvider stampProvider = tool.getStampProvider(); + LoadHubNode hub = graph.unique(new LoadHubNode(stampProvider, getClass.getObject())); + HubGetClassNode hubGetClass = graph.unique(new HubGetClassNode(tool.getMetaAccess(), hub)); + graph.replaceFloating(getClass, hubGetClass); + hub.lower(tool); + hubGetClass.lower(tool); + } + private void lowerStoreHubNode(StoreHubNode storeHub, StructuredGraph graph) { WriteNode hub = createWriteHub(graph, storeHub.getObject(), storeHub.getValue()); graph.replaceFixed(storeHub, hub); diff -r 9dcf9f8779c2 -r 987b4c42aef9 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotGraphBuilderPlugins.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotGraphBuilderPlugins.java Thu Apr 02 10:00:21 2015 +0200 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotGraphBuilderPlugins.java Thu Apr 02 11:45:32 2015 +0200 @@ -31,7 +31,6 @@ import com.oracle.graal.api.code.*; import com.oracle.graal.api.meta.*; import com.oracle.graal.api.replacements.*; -import com.oracle.graal.compiler.common.*; import com.oracle.graal.compiler.common.type.*; import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins; import com.oracle.graal.graphbuilderconf.*; @@ -93,22 +92,6 @@ private static void registerObjectPlugins(InvocationPlugins plugins) { Registration r = new Registration(plugins, Object.class); - r.register1("getClass", Receiver.class, new InvocationPlugin() { - public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { - ValueNode rcvr = receiver.get(); - ObjectStamp objectStamp = (ObjectStamp) rcvr.stamp(); - ValueNode mirror; - if (objectStamp.isExactType() && objectStamp.nonNull() && !GraalOptions.ImmutableCode.getValue()) { - mirror = ConstantNode.forConstant(objectStamp.type().getJavaClass(), b.getMetaAccess()); - } else { - StampProvider stampProvider = b.getStampProvider(); - LoadHubNode hub = b.add(new LoadHubNode(stampProvider, rcvr)); - mirror = new HubGetClassNode(b.getMetaAccess(), hub); - } - b.addPush(Kind.Object, mirror); - return true; - } - }); r.register1("clone", Receiver.class, new InvocationPlugin() { public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { ValueNode object = receiver.get(); diff -r 9dcf9f8779c2 -r 987b4c42aef9 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ClassGetHubNode.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ClassGetHubNode.java Thu Apr 02 10:00:21 2015 +0200 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ClassGetHubNode.java Thu Apr 02 11:45:32 2015 +0200 @@ -73,6 +73,10 @@ } } } + if (clazz instanceof GetClassNode) { + GetClassNode getClass = (GetClassNode) clazz; + return new LoadHubNode(KlassPointerStamp.klass(), getClass.getObject(), null); + } if (clazz instanceof HubGetClassNode) { // replace _klass._java_mirror._klass -> _klass return ((HubGetClassNode) clazz).getHub(); diff -r 9dcf9f8779c2 -r 987b4c42aef9 graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/GetClassNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/GetClassNode.java Thu Apr 02 11:45:32 2015 +0200 @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.nodes.extended; + +import com.oracle.graal.api.meta.Assumptions.AssumptionResult; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.compiler.common.type.*; +import com.oracle.graal.graph.*; +import com.oracle.graal.graph.spi.*; +import com.oracle.graal.nodeinfo.*; +import com.oracle.graal.nodes.*; +import com.oracle.graal.nodes.calc.*; +import com.oracle.graal.nodes.spi.*; + +/** + * Loads an object's class (i.e., this node can be created for {@code object.getClass()}). + */ +@NodeInfo +public final class GetClassNode extends FloatingNode implements Lowerable, Canonicalizable, Virtualizable { + + public static final NodeClass TYPE = NodeClass.create(GetClassNode.class); + @Input ValueNode object; + + public ValueNode getObject() { + return object; + } + + public GetClassNode(Stamp stamp, ValueNode object) { + super(TYPE, stamp); + this.object = object; + } + + @Override + public void lower(LoweringTool tool) { + tool.getLowerer().lower(this, tool); + } + + public static ValueNode tryFold(MetaAccessProvider metaAccess, ValueNode object) { + if (metaAccess != null && object != null && object.stamp() instanceof ObjectStamp) { + ObjectStamp objectStamp = (ObjectStamp) object.stamp(); + + ResolvedJavaType exactType = null; + if (objectStamp.isExactType()) { + exactType = objectStamp.type(); + } else if (objectStamp.type() != null && object.graph().getAssumptions() != null) { + AssumptionResult leafConcreteSubtype = objectStamp.type().findLeafConcreteSubtype(); + if (leafConcreteSubtype != null) { + exactType = leafConcreteSubtype.getResult(); + object.graph().getAssumptions().record(leafConcreteSubtype); + } + } + + if (exactType != null) { + return ConstantNode.forConstant(exactType.getJavaClass(), metaAccess); + } + } + return null; + } + + @Override + public ValueNode canonical(CanonicalizerTool tool) { + ValueNode folded = tryFold(tool.getMetaAccess(), getObject()); + return folded == null ? this : folded; + } + + @Override + public void virtualize(VirtualizerTool tool) { + State state = tool.getObjectState(object); + if (state != null) { + Constant javaClass = state.getVirtualObject().type().getJavaClass(); + tool.replaceWithValue(ConstantNode.forConstant(stamp(), javaClass, tool.getMetaAccessProvider(), graph())); + } + } +} diff -r 9dcf9f8779c2 -r 987b4c42aef9 graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/LoadHubNode.java --- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/LoadHubNode.java Thu Apr 02 10:00:21 2015 +0200 +++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/LoadHubNode.java Thu Apr 02 11:45:32 2015 +0200 @@ -54,7 +54,11 @@ } public LoadHubNode(@InjectedNodeParameter StampProvider stampProvider, ValueNode value, ValueNode guard) { - super(TYPE, hubStamp(stampProvider, value), (GuardingNode) guard); + this(hubStamp(stampProvider, value), value, guard); + } + + public LoadHubNode(Stamp stamp, ValueNode value, ValueNode guard) { + super(TYPE, stamp, (GuardingNode) guard); assert value != guard; this.value = value; } diff -r 9dcf9f8779c2 -r 987b4c42aef9 graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/StandardGraphBuilderPlugins.java --- a/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/StandardGraphBuilderPlugins.java Thu Apr 02 10:00:21 2015 +0200 +++ b/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/StandardGraphBuilderPlugins.java Thu Apr 02 11:45:32 2015 +0200 @@ -30,6 +30,7 @@ import com.oracle.graal.api.directives.*; import com.oracle.graal.api.meta.*; import com.oracle.graal.compiler.common.calc.*; +import com.oracle.graal.compiler.common.type.*; import com.oracle.graal.graph.*; import com.oracle.graal.graphbuilderconf.*; import com.oracle.graal.graphbuilderconf.InvocationPlugins.Receiver; @@ -39,6 +40,7 @@ import com.oracle.graal.nodes.debug.*; import com.oracle.graal.nodes.extended.*; import com.oracle.graal.nodes.java.*; +import com.oracle.graal.nodes.util.*; import com.oracle.graal.options.*; import com.oracle.graal.replacements.nodes.*; @@ -368,6 +370,19 @@ return true; } }); + r.register1("getClass", Receiver.class, new InvocationPlugin() { + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) { + ValueNode object = receiver.get(); + ValueNode folded = GetClassNode.tryFold(b.getMetaAccess(), GraphUtil.originalValue(object)); + if (folded != null) { + b.addPush(Kind.Object, folded); + } else { + Stamp stamp = StampFactory.declaredNonNull(b.getMetaAccess().lookupJavaType(Class.class)); + b.addPush(Kind.Object, new GetClassNode(stamp, object)); + } + return true; + } + }); } private static void registerClassPlugins(InvocationPlugins plugins) {