001/*
002 * Copyright (c) 2011, 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.nodeinfo.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.java.*;
032import com.oracle.graal.nodes.spi.*;
033import com.oracle.graal.nodes.virtual.*;
034
035/**
036 * Load of a value from a location specified as an offset relative to an object. No null check is
037 * performed before the load.
038 */
039@NodeInfo
040public final class UnsafeLoadNode extends UnsafeAccessNode implements Lowerable, Virtualizable {
041    public static final NodeClass<UnsafeLoadNode> TYPE = NodeClass.create(UnsafeLoadNode.class);
042    @OptionalInput(InputType.Condition) LogicNode guardingCondition;
043
044    public UnsafeLoadNode(ValueNode object, ValueNode offset, Kind accessKind, LocationIdentity locationIdentity) {
045        this(object, offset, accessKind, locationIdentity, null);
046    }
047
048    public UnsafeLoadNode(ValueNode object, ValueNode offset, Kind accessKind, LocationIdentity locationIdentity, LogicNode condition) {
049        super(TYPE, StampFactory.forKind(accessKind.getStackKind()), object, offset, accessKind, locationIdentity);
050        this.guardingCondition = condition;
051    }
052
053    public LogicNode getGuardingCondition() {
054        return guardingCondition;
055    }
056
057    @Override
058    public void lower(LoweringTool tool) {
059        tool.getLowerer().lower(this, tool);
060    }
061
062    @Override
063    public void virtualize(VirtualizerTool tool) {
064        ValueNode alias = tool.getAlias(object());
065        if (alias instanceof VirtualObjectNode) {
066            VirtualObjectNode virtual = (VirtualObjectNode) alias;
067            ValueNode offsetValue = tool.getAlias(offset());
068            if (offsetValue.isConstant()) {
069                long off = offsetValue.asJavaConstant().asLong();
070                int entryIndex = virtual.entryIndexForOffset(off, accessKind());
071
072                if (entryIndex != -1) {
073                    ValueNode entry = tool.getEntry(virtual, entryIndex);
074                    Kind entryKind = virtual.entryKind(entryIndex);
075                    if (entry.getKind() == getKind() || entryKind == accessKind()) {
076                        tool.replaceWith(entry);
077                    }
078                }
079            }
080        }
081    }
082
083    @Override
084    protected ValueNode cloneAsFieldAccess(ResolvedJavaField field) {
085        return new LoadFieldNode(object(), field);
086    }
087
088    @Override
089    protected ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity) {
090        return new UnsafeLoadNode(object(), location, accessKind(), identity, guardingCondition);
091    }
092
093    @NodeIntrinsic
094    public static native Object load(Object object, long offset, @ConstantNodeParameter Kind kind, @ConstantNodeParameter LocationIdentity locationIdentity);
095}