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