001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.extended;
024
025import java.util.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.java.*;
035import com.oracle.graal.nodes.spi.*;
036import com.oracle.graal.nodes.type.*;
037import com.oracle.graal.nodes.virtual.*;
038
039/**
040 * This node represents the boxing of a primitive value. This corresponds to a call to the valueOf
041 * methods in Integer, Long, etc.
042 */
043@NodeInfo
044public final class BoxNode extends FixedWithNextNode implements VirtualizableAllocation, Lowerable, Canonicalizable.Unary<ValueNode> {
045
046    public static final NodeClass<BoxNode> TYPE = NodeClass.create(BoxNode.class);
047    @Input private ValueNode value;
048    private final Kind boxingKind;
049
050    public BoxNode(ValueNode value, ResolvedJavaType resultType, Kind boxingKind) {
051        super(TYPE, StampFactory.exactNonNull(resultType));
052        this.value = value;
053        this.boxingKind = boxingKind;
054    }
055
056    public Kind getBoxingKind() {
057        return boxingKind;
058    }
059
060    public ValueNode getValue() {
061        return value;
062    }
063
064    @Override
065    public void lower(LoweringTool tool) {
066        tool.getLowerer().lower(this, tool);
067    }
068
069    @Override
070    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
071        if (tool.allUsagesAvailable() && hasNoUsages()) {
072            return null;
073        }
074        return this;
075    }
076
077    @Override
078    public void virtualize(VirtualizerTool tool) {
079        ValueNode alias = tool.getAlias(getValue());
080        ResolvedJavaType type = StampTool.typeOrNull(stamp());
081
082        VirtualBoxingNode newVirtual = new VirtualBoxingNode(type, boxingKind);
083        assert newVirtual.getFields().length == 1;
084
085        tool.createVirtualObject(newVirtual, new ValueNode[]{alias}, Collections.<MonitorIdNode> emptyList(), false);
086        tool.replaceWithVirtual(newVirtual);
087    }
088}