001/*
002 * Copyright (c) 2011, 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.spi;
024
025import java.util.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.graph.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.java.*;
032import com.oracle.graal.nodes.virtual.*;
033
034/**
035 * This tool can be used to query the current state (normal/virtualized/re-materialized) of values
036 * and to describe the actions that would be taken for this state.
037 *
038 * See also {@link Virtualizable}.
039 */
040public interface VirtualizerTool {
041
042    /**
043     * @return the {@link MetaAccessProvider} associated with the current compilation.
044     */
045    MetaAccessProvider getMetaAccessProvider();
046
047    /**
048     * @return the {@link ConstantReflectionProvider} associated with the current compilation, which
049     *         can be used to access {@link JavaConstant}s.
050     */
051    ConstantReflectionProvider getConstantReflectionProvider();
052
053    /**
054     * This method should be used to query the maximum size of virtualized objects before attempting
055     * virtualization.
056     *
057     * @return the maximum number of entries for virtualized objects.
058     */
059    int getMaximumEntryCount();
060
061    // methods working on virtualized/materialized objects
062
063    /**
064     * Introduces a new virtual object to the current state.
065     *
066     * @param virtualObject the new virtual object.
067     * @param entryState the initial state of the virtual object's fields.
068     * @param locks the initial locking depths.
069     * @param ensureVirtualized true if this object needs to stay virtual
070     */
071    void createVirtualObject(VirtualObjectNode virtualObject, ValueNode[] entryState, List<MonitorIdNode> locks, boolean ensureVirtualized);
072
073    /**
074     * Returns a VirtualObjectNode if the given value is aliased with a virtual object that is still
075     * virtual, the materialized value of the given value is aliased with a virtual object that was
076     * materialized, the replacement if the give value was replaced, otherwise the given value.
077     *
078     * Replacements via {@link #replaceWithValue(ValueNode)} are not immediately committed. This
079     * method can be used to determine if a value was replaced by another one (e.g., a load field by
080     * the loaded value).
081     */
082    ValueNode getAlias(ValueNode value);
083
084    /**
085     * Sets the entry (field or array element) with the given index in the virtualized object.
086     *
087     * @param index the index to be set.
088     * @param value the new value for the given index.
089     * @param unsafe if true, then mismatching value {@link Kind}s will be accepted.
090     */
091    void setVirtualEntry(VirtualObjectNode virtualObject, int index, ValueNode value, boolean unsafe);
092
093    ValueNode getEntry(VirtualObjectNode virtualObject, int index);
094
095    void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId);
096
097    MonitorIdNode removeLock(VirtualObjectNode virtualObject);
098
099    void setEnsureVirtualized(VirtualObjectNode virtualObject, boolean ensureVirtualized);
100
101    boolean getEnsureVirtualized(VirtualObjectNode virtualObject);
102
103    // operations on the current node
104
105    /**
106     * Deletes the current node and replaces it with the given virtualized object.
107     *
108     * @param virtualObject the virtualized object that should replace the current node.
109     */
110    void replaceWithVirtual(VirtualObjectNode virtualObject);
111
112    /**
113     * Deletes the current node and replaces it with the given value.
114     *
115     * @param replacement the value that should replace the current node.
116     */
117    void replaceWithValue(ValueNode replacement);
118
119    /**
120     * Deletes the current node.
121     */
122    void delete();
123
124    /**
125     * Replaces an input of the current node.
126     *
127     * @param oldInput the old input value.
128     * @param replacement the new input value.
129     */
130    void replaceFirstInput(Node oldInput, Node replacement);
131
132    /**
133     * Adds the given node to the graph.This action will only be performed when, and if, the changes
134     * are committed.
135     *
136     * @param node the node to add.
137     */
138    void addNode(ValueNode node);
139
140    /**
141     * This method performs either {@link #replaceWithValue(ValueNode)} or
142     * {@link #replaceWithVirtual(VirtualObjectNode)}, depending on the given value.
143     *
144     * @param value the replacement value
145     */
146    void replaceWith(ValueNode value);
147
148}