1/*
2 * Copyright (c) 2011, 2015, 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.spi;
24
25import java.util.List;
26
27import org.graalvm.compiler.debug.DebugContext;
28import org.graalvm.compiler.graph.Node;
29import org.graalvm.compiler.nodes.ValueNode;
30import org.graalvm.compiler.nodes.java.MonitorIdNode;
31import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
32import org.graalvm.compiler.options.OptionValues;
33
34import jdk.vm.ci.meta.ConstantReflectionProvider;
35import jdk.vm.ci.meta.JavaConstant;
36import jdk.vm.ci.meta.JavaKind;
37import jdk.vm.ci.meta.MetaAccessProvider;
38
39/**
40 * This tool can be used to query the current state (normal/virtualized/re-materialized) of values
41 * and to describe the actions that would be taken for this state.
42 *
43 * See also {@link Virtualizable}.
44 */
45public interface VirtualizerTool {
46
47    /**
48     * @return the {@link MetaAccessProvider} associated with the current compilation.
49     */
50    MetaAccessProvider getMetaAccessProvider();
51
52    /**
53     * @return the {@link ConstantReflectionProvider} associated with the current compilation, which
54     *         can be used to access {@link JavaConstant}s.
55     */
56    ConstantReflectionProvider getConstantReflectionProvider();
57
58    /**
59     * This method should be used to query the maximum size of virtualized objects before attempting
60     * virtualization.
61     *
62     * @return the maximum number of entries for virtualized objects.
63     */
64    int getMaximumEntryCount();
65
66    // methods working on virtualized/materialized objects
67
68    /**
69     * Introduces a new virtual object to the current state.
70     *
71     * @param virtualObject the new virtual object.
72     * @param entryState the initial state of the virtual object's fields.
73     * @param locks the initial locking depths.
74     * @param ensureVirtualized true if this object needs to stay virtual
75     */
76    void createVirtualObject(VirtualObjectNode virtualObject, ValueNode[] entryState, List<MonitorIdNode> locks, boolean ensureVirtualized);
77
78    /**
79     * Returns a VirtualObjectNode if the given value is aliased with a virtual object that is still
80     * virtual, the materialized value of the given value is aliased with a virtual object that was
81     * materialized, the replacement if the give value was replaced, otherwise the given value.
82     *
83     * Replacements via {@link #replaceWithValue(ValueNode)} are not immediately committed. This
84     * method can be used to determine if a value was replaced by another one (e.g., a load field by
85     * the loaded value).
86     */
87    ValueNode getAlias(ValueNode value);
88
89    /**
90     * Sets the entry (field or array element) with the given index in the virtualized object.
91     *
92     * @param index the index to be set.
93     * @param value the new value for the given index.
94     * @param unsafe if true, then mismatching value {@link JavaKind}s will be accepted.
95     */
96    void setVirtualEntry(VirtualObjectNode virtualObject, int index, ValueNode value, boolean unsafe);
97
98    ValueNode getEntry(VirtualObjectNode virtualObject, int index);
99
100    void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId);
101
102    MonitorIdNode removeLock(VirtualObjectNode virtualObject);
103
104    void setEnsureVirtualized(VirtualObjectNode virtualObject, boolean ensureVirtualized);
105
106    boolean getEnsureVirtualized(VirtualObjectNode virtualObject);
107
108    // operations on the current node
109
110    /**
111     * Deletes the current node and replaces it with the given virtualized object.
112     *
113     * @param virtualObject the virtualized object that should replace the current node.
114     */
115    void replaceWithVirtual(VirtualObjectNode virtualObject);
116
117    /**
118     * Deletes the current node and replaces it with the given value.
119     *
120     * @param replacement the value that should replace the current node.
121     */
122    void replaceWithValue(ValueNode replacement);
123
124    /**
125     * Deletes the current node.
126     */
127    void delete();
128
129    /**
130     * Replaces an input of the current node.
131     *
132     * @param oldInput the old input value.
133     * @param replacement the new input value.
134     */
135    void replaceFirstInput(Node oldInput, Node replacement);
136
137    /**
138     * Adds the given node to the graph.This action will only be performed when, and if, the changes
139     * are committed.
140     *
141     * @param node the node to add.
142     */
143    void addNode(ValueNode node);
144
145    /**
146     * This method performs either {@link #replaceWithValue(ValueNode)} or
147     * {@link #replaceWithVirtual(VirtualObjectNode)}, depending on the given value.
148     *
149     * @param value the replacement value
150     */
151    void replaceWith(ValueNode value);
152
153    /**
154     *
155     * If state is virtual, materialization is performed for the given state.
156     *
157     * @return true if materialization happened, false if not.
158     */
159    boolean ensureMaterialized(VirtualObjectNode virtualObject);
160
161    OptionValues getOptions();
162
163    DebugContext getDebug();
164}
165