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