UnsafeAccessNode.java revision 13175:3df8ef613001
1/*
2 * Copyright (c) 2012, 2016, 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.NodeCycles.CYCLES_2;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
27
28import org.graalvm.compiler.core.common.type.Stamp;
29import org.graalvm.compiler.graph.Node;
30import org.graalvm.compiler.graph.NodeClass;
31import org.graalvm.compiler.graph.spi.Canonicalizable;
32import org.graalvm.compiler.graph.spi.CanonicalizerTool;
33import org.graalvm.compiler.nodeinfo.NodeInfo;
34import org.graalvm.compiler.nodes.FixedWithNextNode;
35import org.graalvm.compiler.nodes.NamedLocationIdentity;
36import org.graalvm.compiler.nodes.ValueNode;
37import org.graalvm.compiler.nodes.type.StampTool;
38import org.graalvm.word.LocationIdentity;
39
40import jdk.vm.ci.meta.Assumptions;
41import jdk.vm.ci.meta.JavaKind;
42import jdk.vm.ci.meta.ResolvedJavaField;
43import jdk.vm.ci.meta.ResolvedJavaType;
44
45@NodeInfo(cycles = CYCLES_2, size = SIZE_1)
46public abstract class UnsafeAccessNode extends FixedWithNextNode implements Canonicalizable {
47
48    public static final NodeClass<UnsafeAccessNode> TYPE = NodeClass.create(UnsafeAccessNode.class);
49    @Input ValueNode object;
50    @Input ValueNode offset;
51    protected final JavaKind accessKind;
52    protected final LocationIdentity locationIdentity;
53    protected final boolean forceAnyLocation;
54
55    protected UnsafeAccessNode(NodeClass<? extends UnsafeAccessNode> c, Stamp stamp, ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity,
56                    boolean forceAnyLocation) {
57        super(c, stamp);
58        this.forceAnyLocation = forceAnyLocation;
59        assert accessKind != null;
60        assert locationIdentity != null;
61        this.object = object;
62        this.offset = offset;
63        this.accessKind = accessKind;
64        this.locationIdentity = locationIdentity;
65    }
66
67    public LocationIdentity getLocationIdentity() {
68        return locationIdentity;
69    }
70
71    public boolean isAnyLocationForced() {
72        return forceAnyLocation;
73    }
74
75    public ValueNode object() {
76        return object;
77    }
78
79    public ValueNode offset() {
80        return offset;
81    }
82
83    public JavaKind accessKind() {
84        return accessKind;
85    }
86
87    @Override
88    public Node canonical(CanonicalizerTool tool) {
89        if (!isAnyLocationForced() && getLocationIdentity().isAny()) {
90            if (offset().isConstant()) {
91                long constantOffset = offset().asJavaConstant().asLong();
92
93                // Try to canonicalize to a field access.
94                ResolvedJavaType receiverType = StampTool.typeOrNull(object());
95                if (receiverType != null) {
96                    ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(constantOffset, accessKind());
97                    // No need for checking that the receiver is non-null. The field access includes
98                    // the null check and if a field is found, the offset is so small that this is
99                    // never a valid access of an arbitrary address.
100                    if (field != null && field.getJavaKind() == this.accessKind()) {
101                        assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase";
102                        return cloneAsFieldAccess(graph().getAssumptions(), field);
103                    }
104                }
105            }
106            ResolvedJavaType receiverType = StampTool.typeOrNull(object());
107            // Try to build a better location identity.
108            if (receiverType != null && receiverType.isArray()) {
109                LocationIdentity identity = NamedLocationIdentity.getArrayLocation(receiverType.getComponentType().getJavaKind());
110                assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase";
111                return cloneAsArrayAccess(offset(), identity);
112            }
113        }
114
115        return this;
116    }
117
118    protected abstract ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field);
119
120    protected abstract ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity);
121}
122