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