1 /* 2 * Copyright (c) 2013, 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.extended; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_100; 26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_50; 27 28 import org.graalvm.compiler.core.common.type.StampFactory; 29 import org.graalvm.compiler.graph.NodeClass; 30 import org.graalvm.compiler.graph.spi.Canonicalizable; 31 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 32 import org.graalvm.compiler.nodeinfo.NodeInfo; 33 import org.graalvm.compiler.nodes.ConstantNode; 34 import org.graalvm.compiler.nodes.FixedWithNextNode; 35 import org.graalvm.compiler.nodes.ValueNode; 36 import org.graalvm.compiler.nodes.spi.Lowerable; 37 import org.graalvm.compiler.nodes.spi.LoweringTool; 38 import org.graalvm.compiler.nodes.spi.Virtualizable; 39 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 40 import org.graalvm.compiler.nodes.type.StampTool; 41 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 42 43 import jdk.vm.ci.meta.ConstantReflectionProvider; 44 import jdk.vm.ci.meta.JavaConstant; 45 import jdk.vm.ci.meta.JavaKind; 46 import jdk.vm.ci.meta.MetaAccessProvider; 47 import jdk.vm.ci.meta.ResolvedJavaType; 48 49 @NodeInfo(cycles = CYCLES_100, size = SIZE_50) 50 public final class UnboxNode extends FixedWithNextNode implements Virtualizable, Lowerable, Canonicalizable.Unary<ValueNode> { 51 52 public static final NodeClass<UnboxNode> TYPE = NodeClass.create(UnboxNode.class); 53 @Input protected ValueNode value; 54 protected final JavaKind boxingKind; 55 56 @Override 57 public ValueNode getValue() { 58 return value; 59 } 60 61 public UnboxNode(ValueNode value, JavaKind boxingKind) { 62 super(TYPE, StampFactory.forKind(boxingKind.getStackKind())); 63 this.value = value; 64 this.boxingKind = boxingKind; 65 } 66 67 public static ValueNode create(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ValueNode value, JavaKind boxingKind) { 68 ValueNode synonym = findSynonym(metaAccess, constantReflection, value, boxingKind); 69 if (synonym != null) { 70 return synonym; 71 } 72 return new UnboxNode(value, boxingKind); 73 } 74 75 public JavaKind getBoxingKind() { 76 return boxingKind; 77 } 78 79 @Override 80 public void lower(LoweringTool tool) { 81 tool.getLowerer().lower(this, tool); 82 } 83 84 @Override 85 public void virtualize(VirtualizerTool tool) { 86 ValueNode alias = tool.getAlias(getValue()); 87 if (alias instanceof VirtualObjectNode) { 88 VirtualObjectNode virtual = (VirtualObjectNode) alias; 89 ResolvedJavaType objectType = virtual.type(); 90 ResolvedJavaType expectedType = tool.getMetaAccessProvider().lookupJavaType(boxingKind.toBoxedJavaClass()); 91 if (objectType.equals(expectedType)) { 92 tool.replaceWithValue(tool.getEntry(virtual, 0)); 93 } 94 } 95 } 96 97 @Override 98 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 99 if (tool.allUsagesAvailable() && hasNoUsages() && StampTool.isPointerNonNull(forValue)) { 100 return null; 101 } 102 ValueNode synonym = findSynonym(tool.getMetaAccess(), tool.getConstantReflection(), forValue, boxingKind); 103 if (synonym != null) { 104 return synonym; 105 } 106 return this; 107 } 108 109 private static ValueNode findSynonym(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ValueNode forValue, JavaKind boxingKind) { 110 if (forValue.isConstant()) { 111 JavaConstant constant = forValue.asJavaConstant(); 112 JavaConstant unboxed = constantReflection.unboxPrimitive(constant); 113 if (unboxed != null && unboxed.getJavaKind() == boxingKind) { 114 return ConstantNode.forConstant(unboxed, metaAccess); 115 } 116 } else if (forValue instanceof BoxNode) { 117 BoxNode box = (BoxNode) forValue; 118 if (boxingKind == box.getBoxingKind()) { 119 return box.getValue(); 120 } 121 } 122 return null; 123 } 124 }