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 jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.spi.*;
033import com.oracle.graal.nodes.type.*;
034import com.oracle.graal.nodes.virtual.*;
035
036@NodeInfo
037public final class UnboxNode extends FixedWithNextNode implements Virtualizable, Lowerable, Canonicalizable.Unary<ValueNode> {
038
039    public static final NodeClass<UnboxNode> TYPE = NodeClass.create(UnboxNode.class);
040    @Input protected ValueNode value;
041    protected final Kind boxingKind;
042
043    public ValueNode getValue() {
044        return value;
045    }
046
047    protected UnboxNode(ValueNode value, Kind boxingKind) {
048        super(TYPE, StampFactory.forKind(boxingKind.getStackKind()));
049        this.value = value;
050        this.boxingKind = boxingKind;
051    }
052
053    public static ValueNode create(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ValueNode value, Kind boxingKind) {
054        ValueNode synonym = findSynonym(metaAccess, constantReflection, value, boxingKind);
055        if (synonym != null) {
056            return synonym;
057        }
058        return new UnboxNode(value, boxingKind);
059    }
060
061    public Kind getBoxingKind() {
062        return boxingKind;
063    }
064
065    @Override
066    public void lower(LoweringTool tool) {
067        tool.getLowerer().lower(this, tool);
068    }
069
070    @Override
071    public void virtualize(VirtualizerTool tool) {
072        ValueNode alias = tool.getAlias(getValue());
073        if (alias instanceof VirtualObjectNode) {
074            VirtualObjectNode virtual = (VirtualObjectNode) alias;
075            ResolvedJavaType objectType = virtual.type();
076            ResolvedJavaType expectedType = tool.getMetaAccessProvider().lookupJavaType(boxingKind.toBoxedJavaClass());
077            if (objectType.equals(expectedType)) {
078                tool.replaceWithValue(tool.getEntry(virtual, 0));
079            }
080        }
081    }
082
083    @Override
084    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
085        if (tool.allUsagesAvailable() && hasNoUsages() && StampTool.isPointerNonNull(forValue)) {
086            return null;
087        }
088        ValueNode synonym = findSynonym(tool.getMetaAccess(), tool.getConstantReflection(), forValue, boxingKind);
089        if (synonym != null) {
090            return synonym;
091        }
092        return this;
093    }
094
095    private static ValueNode findSynonym(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ValueNode forValue, Kind boxingKind) {
096        if (forValue.isConstant()) {
097            JavaConstant constant = forValue.asJavaConstant();
098            JavaConstant unboxed = constantReflection.unboxPrimitive(constant);
099            if (unboxed != null && unboxed.getKind() == boxingKind) {
100                return ConstantNode.forConstant(unboxed, metaAccess);
101            }
102        } else if (forValue instanceof BoxNode) {
103            BoxNode box = (BoxNode) forValue;
104            if (boxingKind == box.getBoxingKind()) {
105                return box.getValue();
106            }
107        }
108        return null;
109    }
110}