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