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 */
23package org.graalvm.compiler.nodes.extended;
24
25import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
26
27import java.util.Collections;
28
29import org.graalvm.compiler.core.common.type.StampFactory;
30import org.graalvm.compiler.core.common.type.TypeReference;
31import org.graalvm.compiler.graph.NodeClass;
32import org.graalvm.compiler.graph.spi.Canonicalizable;
33import org.graalvm.compiler.graph.spi.CanonicalizerTool;
34import org.graalvm.compiler.nodeinfo.NodeCycles;
35import org.graalvm.compiler.nodeinfo.NodeInfo;
36import org.graalvm.compiler.nodes.FixedWithNextNode;
37import org.graalvm.compiler.nodes.ValueNode;
38import org.graalvm.compiler.nodes.java.MonitorIdNode;
39import org.graalvm.compiler.nodes.spi.Lowerable;
40import org.graalvm.compiler.nodes.spi.LoweringTool;
41import org.graalvm.compiler.nodes.spi.VirtualizableAllocation;
42import org.graalvm.compiler.nodes.spi.VirtualizerTool;
43import org.graalvm.compiler.nodes.type.StampTool;
44import org.graalvm.compiler.nodes.virtual.VirtualBoxingNode;
45
46import jdk.vm.ci.meta.JavaKind;
47import 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 = NodeCycles.CYCLES_8, size = SIZE_8)
54public 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}
107