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