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_20;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_20;
  27 
  28 import java.util.Collections;
  29 
  30 import org.graalvm.compiler.core.common.type.StampFactory;
  31 import org.graalvm.compiler.core.common.type.TypeReference;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.Canonicalizable;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.FixedWithNextNode;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.java.MonitorIdNode;
  39 import org.graalvm.compiler.nodes.spi.Lowerable;
  40 import org.graalvm.compiler.nodes.spi.LoweringTool;
  41 import org.graalvm.compiler.nodes.spi.VirtualizableAllocation;
  42 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  43 import org.graalvm.compiler.nodes.type.StampTool;
  44 import org.graalvm.compiler.nodes.virtual.VirtualBoxingNode;
  45 
  46 import jdk.vm.ci.meta.JavaKind;
  47 import jdk.vm.ci.meta.ResolvedJavaType;
  48 
  49 /**
  50  * This node represents the boxing of a primitive value. This corresponds to a call to the valueOf
  51  * methods in Integer, Long, etc.
  52  */
  53 @NodeInfo(cycles = CYCLES_20, size = SIZE_20)
  54 public class BoxNode extends FixedWithNextNode implements VirtualizableAllocation, Lowerable, Canonicalizable.Unary<ValueNode> {
  55 
  56     public static final NodeClass<BoxNode> TYPE = NodeClass.create(BoxNode.class);
  57     @Input private ValueNode value;
  58     protected final JavaKind boxingKind;
  59 
  60     public BoxNode(ValueNode value, ResolvedJavaType resultType, JavaKind boxingKind) {
  61         this(TYPE, value, resultType, boxingKind);
  62     }
  63 
  64     public BoxNode(NodeClass<? extends BoxNode> c, ValueNode value, ResolvedJavaType resultType, JavaKind boxingKind) {
  65         super(c, StampFactory.objectNonNull(TypeReference.createExactTrusted(resultType)));
  66         this.value = value;
  67         this.boxingKind = boxingKind;
  68     }
  69 
  70     public JavaKind getBoxingKind() {
  71         return boxingKind;
  72     }
  73 
  74     @Override
  75     public ValueNode getValue() {
  76         return value;
  77     }
  78 
  79     @Override
  80     public void lower(LoweringTool tool) {
  81         tool.getLowerer().lower(this, tool);
  82     }
  83 
  84     @Override
  85     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  86         if (tool.allUsagesAvailable() && hasNoUsages()) {
  87             return null;
  88         }
  89         return this;
  90     }
  91 
  92     protected VirtualBoxingNode createVirtualBoxingNode() {
  93         return new VirtualBoxingNode(StampTool.typeOrNull(stamp()), boxingKind);
  94     }
  95 
  96     @Override
  97     public void virtualize(VirtualizerTool tool) {
  98         ValueNode alias = tool.getAlias(getValue());
  99 
 100         VirtualBoxingNode newVirtual = createVirtualBoxingNode();
 101         assert newVirtual.getFields().length == 1;
 102 
 103         tool.createVirtualObject(newVirtual, new ValueNode[]{alias}, Collections.<MonitorIdNode> emptyList(), false);
 104         tool.replaceWithVirtual(newVirtual);
 105     }
 106 }