001/*
002 * Copyright (c) 2012, 2014, 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.type.*;
033
034@NodeInfo
035public abstract class UnsafeAccessNode extends FixedWithNextNode implements Canonicalizable {
036
037    public static final NodeClass<UnsafeAccessNode> TYPE = NodeClass.create(UnsafeAccessNode.class);
038    @Input ValueNode object;
039    @Input ValueNode offset;
040    protected final Kind accessKind;
041    protected final LocationIdentity locationIdentity;
042
043    protected UnsafeAccessNode(NodeClass<? extends UnsafeAccessNode> c, Stamp stamp, ValueNode object, ValueNode offset, Kind accessKind, LocationIdentity locationIdentity) {
044        super(c, stamp);
045        assert accessKind != null;
046        this.object = object;
047        this.offset = offset;
048        this.accessKind = accessKind;
049        this.locationIdentity = locationIdentity;
050    }
051
052    public LocationIdentity getLocationIdentity() {
053        return locationIdentity;
054    }
055
056    public ValueNode object() {
057        return object;
058    }
059
060    public ValueNode offset() {
061        return offset;
062    }
063
064    public Kind accessKind() {
065        return accessKind;
066    }
067
068    @Override
069    public Node canonical(CanonicalizerTool tool) {
070        if (this.getLocationIdentity().isAny() && offset().isConstant()) {
071            long constantOffset = offset().asJavaConstant().asLong();
072
073            // Try to canonicalize to a field access.
074            ResolvedJavaType receiverType = StampTool.typeOrNull(object());
075            if (receiverType != null) {
076                ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(constantOffset, accessKind());
077                // No need for checking that the receiver is non-null. The field access includes
078                // the null check and if a field is found, the offset is so small that this is
079                // never a valid access of an arbitrary address.
080                if (field != null && field.getKind() == this.accessKind()) {
081                    assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase";
082                    return cloneAsFieldAccess(field);
083                }
084            }
085        }
086        if (this.getLocationIdentity().isAny()) {
087            ResolvedJavaType receiverType = StampTool.typeOrNull(object());
088            // Try to build a better location identity.
089            if (receiverType != null && receiverType.isArray()) {
090                LocationIdentity identity = NamedLocationIdentity.getArrayLocation(receiverType.getComponentType().getKind());
091                assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase";
092                return cloneAsArrayAccess(offset(), identity);
093            }
094        }
095
096        return this;
097    }
098
099    protected abstract ValueNode cloneAsFieldAccess(ResolvedJavaField field);
100
101    protected abstract ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity);
102}