1 /*
   2  * Copyright (c) 2011, 2017, 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.virtual.phases.ea;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.MaximumEscapeAnalysisArrayLength;
  26 
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
  30 import org.graalvm.compiler.debug.DebugContext;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  33 import org.graalvm.compiler.nodes.FixedNode;
  34 import org.graalvm.compiler.nodes.FixedWithNextNode;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.calc.FloatingNode;
  37 import org.graalvm.compiler.nodes.java.MonitorIdNode;
  38 import org.graalvm.compiler.nodes.spi.LoweringProvider;
  39 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  40 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  41 import org.graalvm.compiler.options.OptionValues;
  42 
  43 import jdk.vm.ci.meta.Assumptions;
  44 import jdk.vm.ci.meta.ConstantReflectionProvider;
  45 import jdk.vm.ci.meta.JavaKind;
  46 import jdk.vm.ci.meta.MetaAccessProvider;
  47 
  48 /**
  49  * Forwards calls from {@link VirtualizerTool} to the actual {@link PartialEscapeBlockState}.
  50  */
  51 class VirtualizerToolImpl implements VirtualizerTool, CanonicalizerTool {
  52 
  53     private final MetaAccessProvider metaAccess;
  54     private final ConstantReflectionProvider constantReflection;
  55     private final ConstantFieldProvider constantFieldProvider;
  56     private final PartialEscapeClosure<?> closure;
  57     private final Assumptions assumptions;
  58     private final OptionValues options;
  59     private final DebugContext debug;
  60     private final LoweringProvider loweringProvider;
  61 
  62     VirtualizerToolImpl(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider, PartialEscapeClosure<?> closure,
  63                     Assumptions assumptions, OptionValues options, DebugContext debug, LoweringProvider loweringProvider) {
  64         this.metaAccess = metaAccess;
  65         this.constantReflection = constantReflection;
  66         this.constantFieldProvider = constantFieldProvider;
  67         this.closure = closure;
  68         this.assumptions = assumptions;
  69         this.options = options;
  70         this.debug = debug;
  71         this.loweringProvider = loweringProvider;
  72     }
  73 
  74     private boolean deleted;
  75     private PartialEscapeBlockState<?> state;
  76     private ValueNode current;
  77     private FixedNode position;
  78     private GraphEffectList effects;
  79 
  80     @Override
  81     public OptionValues getOptions() {
  82         return options;
  83     }
  84 
  85     @Override
  86     public DebugContext getDebug() {
  87         return debug;
  88     }
  89 
  90     @Override
  91     public MetaAccessProvider getMetaAccessProvider() {
  92         return metaAccess;
  93     }
  94 
  95     @Override
  96     public ConstantReflectionProvider getConstantReflectionProvider() {
  97         return constantReflection;
  98     }
  99 
 100     @Override
 101     public ConstantFieldProvider getConstantFieldProvider() {
 102         return constantFieldProvider;
 103     }
 104 
 105     public void reset(PartialEscapeBlockState<?> newState, ValueNode newCurrent, FixedNode newPosition, GraphEffectList newEffects) {
 106         deleted = false;
 107         state = newState;
 108         current = newCurrent;
 109         position = newPosition;
 110         effects = newEffects;
 111     }
 112 
 113     public boolean isDeleted() {
 114         return deleted;
 115     }
 116 
 117     @Override
 118     public ValueNode getAlias(ValueNode value) {
 119         return closure.getAliasAndResolve(state, value);
 120     }
 121 
 122     @Override
 123     public ValueNode getEntry(VirtualObjectNode virtualObject, int index) {
 124         return state.getObjectState(virtualObject).getEntry(index);
 125     }
 126 
 127     @Override
 128     public void setVirtualEntry(VirtualObjectNode virtual, int index, ValueNode value, boolean unsafe) {
 129         ObjectState obj = state.getObjectState(virtual);
 130         assert obj.isVirtual() : "not virtual: " + obj;
 131         ValueNode newValue;
 132         if (value == null) {
 133             newValue = null;
 134         } else {
 135             newValue = closure.getAliasAndResolve(state, value);
 136             assert unsafe || obj.getEntry(index) == null || obj.getEntry(index).getStackKind() == newValue.getStackKind() || (isObjectEntry(obj.getEntry(index)) && isObjectEntry(newValue));
 137         }
 138         state.setEntry(virtual.getObjectId(), index, newValue);
 139     }
 140 
 141     @Override
 142     public void setEnsureVirtualized(VirtualObjectNode virtualObject, boolean ensureVirtualized) {
 143         int id = virtualObject.getObjectId();
 144         state.setEnsureVirtualized(id, ensureVirtualized);
 145     }
 146 
 147     @Override
 148     public boolean getEnsureVirtualized(VirtualObjectNode virtualObject) {
 149         return state.getObjectState(virtualObject).getEnsureVirtualized();
 150     }
 151 
 152     private static boolean isObjectEntry(ValueNode value) {
 153         return value.getStackKind() == JavaKind.Object || value instanceof VirtualObjectNode;
 154     }
 155 
 156     @Override
 157     public void replaceWithVirtual(VirtualObjectNode virtual) {
 158         closure.addVirtualAlias(virtual, current);
 159         effects.deleteNode(current);
 160         deleted = true;
 161     }
 162 
 163     @Override
 164     public void replaceWithValue(ValueNode replacement) {
 165         effects.replaceAtUsages(current, closure.getScalarAlias(replacement), position);
 166         closure.addScalarAlias(current, replacement);
 167         deleted = true;
 168     }
 169 
 170     @Override
 171     public void delete() {
 172         effects.deleteNode(current);
 173         deleted = true;
 174     }
 175 
 176     @Override
 177     public void replaceFirstInput(Node oldInput, Node replacement) {
 178         effects.replaceFirstInput(current, oldInput, replacement);
 179     }
 180 
 181     @Override
 182     public void addNode(ValueNode node) {
 183         if (node instanceof FloatingNode) {
 184             effects.addFloatingNode(node, "VirtualizerTool");
 185         } else {
 186             effects.addFixedNodeBefore((FixedWithNextNode) node, position);
 187         }
 188     }
 189 
 190     @Override
 191     public void createVirtualObject(VirtualObjectNode virtualObject, ValueNode[] entryState, List<MonitorIdNode> locks, boolean ensureVirtualized) {
 192         VirtualUtil.trace(options, debug, "{{%s}} ", current);
 193         if (!virtualObject.isAlive()) {
 194             effects.addFloatingNode(virtualObject, "newVirtualObject");
 195         }
 196         for (int i = 0; i < entryState.length; i++) {
 197             ValueNode entry = entryState[i];
 198             entryState[i] = entry instanceof VirtualObjectNode ? entry : closure.getAliasAndResolve(state, entry);
 199         }
 200         int id = virtualObject.getObjectId();
 201         if (id == -1) {
 202             id = closure.virtualObjects.size();
 203             closure.virtualObjects.add(virtualObject);
 204             virtualObject.setObjectId(id);
 205         }
 206         state.addObject(id, new ObjectState(entryState, locks, ensureVirtualized));
 207         closure.addVirtualAlias(virtualObject, virtualObject);
 208         PartialEscapeClosure.COUNTER_ALLOCATION_REMOVED.increment(debug);
 209         effects.addVirtualizationDelta(1);
 210     }
 211 
 212     @Override
 213     public int getMaximumEntryCount() {
 214         return MaximumEscapeAnalysisArrayLength.getValue(current.getOptions());
 215     }
 216 
 217     @Override
 218     public void replaceWith(ValueNode node) {
 219         if (node instanceof VirtualObjectNode) {
 220             replaceWithVirtual((VirtualObjectNode) node);
 221         } else {
 222             replaceWithValue(node);
 223         }
 224     }
 225 
 226     @Override
 227     public boolean ensureMaterialized(VirtualObjectNode virtualObject) {
 228         return closure.ensureMaterialized(state, virtualObject.getObjectId(), position, effects, PartialEscapeClosure.COUNTER_MATERIALIZATIONS_UNHANDLED);
 229     }
 230 
 231     @Override
 232     public void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId) {
 233         int id = virtualObject.getObjectId();
 234         state.addLock(id, monitorId);
 235     }
 236 
 237     @Override
 238     public MonitorIdNode removeLock(VirtualObjectNode virtualObject) {
 239         int id = virtualObject.getObjectId();
 240         return state.removeLock(id);
 241     }
 242 
 243     @Override
 244     public MetaAccessProvider getMetaAccess() {
 245         return metaAccess;
 246     }
 247 
 248     @Override
 249     public ConstantReflectionProvider getConstantReflection() {
 250         return constantReflection;
 251     }
 252 
 253     @Override
 254     public boolean canonicalizeReads() {
 255         return false;
 256     }
 257 
 258     @Override
 259     public boolean allUsagesAvailable() {
 260         return true;
 261     }
 262 
 263     @Override
 264     public Assumptions getAssumptions() {
 265         return assumptions;
 266     }
 267 
 268     @Override
 269     public Integer smallestCompareWidth() {
 270         if (loweringProvider != null) {
 271             return loweringProvider.smallestCompareWidth();
 272         } else {
 273             return null;
 274         }
 275     }
 276 }