1 /*
   2  * Copyright (c) 2011, 2019, 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.virtual.phases.ea;
  26 
  27 import static org.graalvm.compiler.core.common.GraalOptions.MaximumEscapeAnalysisArrayLength;
  28 
  29 import java.util.List;
  30 
  31 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
  32 import org.graalvm.compiler.debug.DebugContext;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.nodes.ConstantNode;
  36 import org.graalvm.compiler.nodes.FixedNode;
  37 import org.graalvm.compiler.nodes.FixedWithNextNode;
  38 import org.graalvm.compiler.nodes.NodeView;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.calc.FloatingNode;
  41 import org.graalvm.compiler.nodes.calc.UnpackEndianHalfNode;
  42 import org.graalvm.compiler.nodes.java.MonitorIdNode;
  43 import org.graalvm.compiler.nodes.spi.LoweringProvider;
  44 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  45 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
  46 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  47 import org.graalvm.compiler.options.OptionValues;
  48 
  49 import jdk.vm.ci.meta.Assumptions;
  50 import jdk.vm.ci.meta.ConstantReflectionProvider;
  51 import jdk.vm.ci.meta.JavaConstant;
  52 import jdk.vm.ci.meta.JavaKind;
  53 import jdk.vm.ci.meta.MetaAccessProvider;
  54 
  55 /**
  56  * Forwards calls from {@link VirtualizerTool} to the actual {@link PartialEscapeBlockState}.
  57  */
  58 class VirtualizerToolImpl implements VirtualizerTool, CanonicalizerTool {
  59 
  60     private final MetaAccessProvider metaAccess;
  61     private final ConstantReflectionProvider constantReflection;
  62     private final ConstantFieldProvider constantFieldProvider;
  63     private final PartialEscapeClosure<?> closure;
  64     private final Assumptions assumptions;
  65     private final OptionValues options;
  66     private final DebugContext debug;
  67     private final LoweringProvider loweringProvider;
  68     private ConstantNode illegalConstant;
  69 
  70     VirtualizerToolImpl(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider, PartialEscapeClosure<?> closure,
  71                     Assumptions assumptions, OptionValues options, DebugContext debug, LoweringProvider loweringProvider) {
  72         this.metaAccess = metaAccess;
  73         this.constantReflection = constantReflection;
  74         this.constantFieldProvider = constantFieldProvider;
  75         this.closure = closure;
  76         this.assumptions = assumptions;
  77         this.options = options;
  78         this.debug = debug;
  79         this.loweringProvider = loweringProvider;
  80     }
  81 
  82     private boolean deleted;
  83     private PartialEscapeBlockState<?> state;
  84     private ValueNode current;
  85     private FixedNode position;
  86     private GraphEffectList effects;
  87 
  88     @Override
  89     public OptionValues getOptions() {
  90         return options;
  91     }
  92 
  93     @Override
  94     public DebugContext getDebug() {
  95         return debug;
  96     }
  97 
  98     @Override
  99     public ConstantFieldProvider getConstantFieldProvider() {
 100         return constantFieldProvider;
 101     }
 102 
 103     public void reset(PartialEscapeBlockState<?> newState, ValueNode newCurrent, FixedNode newPosition, GraphEffectList newEffects) {
 104         deleted = false;
 105         state = newState;
 106         current = newCurrent;
 107         position = newPosition;
 108         effects = newEffects;
 109     }
 110 
 111     public boolean isDeleted() {
 112         return deleted;
 113     }
 114 
 115     @Override
 116     public ValueNode getAlias(ValueNode value) {
 117         return closure.getAliasAndResolve(state, value);
 118     }
 119 
 120     @Override
 121     public ValueNode getEntry(VirtualObjectNode virtualObject, int index) {
 122         return state.getObjectState(virtualObject).getEntry(index);
 123     }
 124 
 125     @Override
 126     public boolean setVirtualEntry(VirtualObjectNode virtual, int index, ValueNode value, JavaKind theAccessKind, long offset) {
 127         ObjectState obj = state.getObjectState(virtual);
 128         assert obj.isVirtual() : "not virtual: " + obj;
 129         JavaKind entryKind = virtual.entryKind(index);
 130         JavaKind accessKind = theAccessKind != null ? theAccessKind : entryKind;
 131         ValueNode newValue = closure.getAliasAndResolve(state, value);
 132         getDebug().log(DebugContext.DETAILED_LEVEL, "Setting entry %d in virtual object %s %s results in %s", index, virtual.getObjectId(), virtual, state.getObjectState(virtual.getObjectId()));
 133         ValueNode oldValue = getEntry(virtual, index);
 134         boolean canVirtualize = entryKind == accessKind || (entryKind == accessKind.getStackKind() && virtual instanceof VirtualInstanceNode);
 135         if (!canVirtualize) {
 136             assert entryKind != JavaKind.Long || newValue != null;
 137             if (entryKind == JavaKind.Long && oldValue.getStackKind() == newValue.getStackKind() && oldValue.getStackKind().isPrimitive()) {
 138                 /*
 139                  * Special case: If the entryKind is long, allow arbitrary kinds as long as a value
 140                  * of the same kind is already there. This can only happen if some other node
 141                  * initialized the entry with a value of a different kind. One example where this
 142                  * happens is the Truffle NewFrameNode.
 143                  */
 144                 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s with primitive of kind %s in long entry ", current, oldValue.getStackKind());
 145                 canVirtualize = true;
 146             } else if (entryKind == JavaKind.Int && (accessKind == JavaKind.Long || accessKind == JavaKind.Double) && offset % 8 == 0) {
 147                 /*
 148                  * Special case: Allow storing a single long or double value into two consecutive
 149                  * int slots.
 150                  */
 151                 int nextIndex = virtual.entryIndexForOffset(getMetaAccess(), offset + 4, JavaKind.Int);
 152                 if (nextIndex != -1) {
 153                     canVirtualize = true;
 154                     assert nextIndex == index + 1 : "expected to be sequential";
 155                     getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s for double word stored in two ints", current);
 156                 }
 157             }
 158         }
 159 
 160         if (canVirtualize) {
 161             getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s for entryKind %s and access kind %s", current, entryKind, accessKind);
 162             state.setEntry(virtual.getObjectId(), index, newValue);
 163             if (entryKind == JavaKind.Int) {
 164                 if (accessKind.needsTwoSlots()) {
 165                     // Storing double word value two int slots
 166                     assert virtual.entryKind(index + 1) == JavaKind.Int;
 167                     state.setEntry(virtual.getObjectId(), index + 1, getIllegalConstant());
 168                 } else if (oldValue.getStackKind() == JavaKind.Double || oldValue.getStackKind() == JavaKind.Long) {
 169                     // Splitting double word constant by storing over it with an int
 170                     getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s producing second half of double word value %s", current, oldValue);
 171                     ValueNode secondHalf = UnpackEndianHalfNode.create(oldValue, false, NodeView.DEFAULT);
 172                     addNode(secondHalf);
 173                     state.setEntry(virtual.getObjectId(), index + 1, secondHalf);
 174                 }
 175             }
 176             if (oldValue.isConstant() && oldValue.asConstant().equals(JavaConstant.forIllegal())) {
 177                 // Storing into second half of double, so replace previous value
 178                 ValueNode previous = getEntry(virtual, index - 1);
 179                 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s producing first half of double word value %s", current, previous);
 180                 ValueNode firstHalf = UnpackEndianHalfNode.create(previous, true, NodeView.DEFAULT);
 181                 addNode(firstHalf);
 182                 state.setEntry(virtual.getObjectId(), index - 1, firstHalf);
 183             }
 184             return true;
 185         }
 186         // Should only occur if there are mismatches between the entry and access kind
 187         assert entryKind != accessKind;
 188         return false;
 189     }
 190 
 191     private ValueNode getIllegalConstant() {
 192         if (illegalConstant == null) {
 193             illegalConstant = ConstantNode.forConstant(JavaConstant.forIllegal(), getMetaAccess());
 194             addNode(illegalConstant);
 195         }
 196         return illegalConstant;
 197     }
 198 
 199     @Override
 200     public void setEnsureVirtualized(VirtualObjectNode virtualObject, boolean ensureVirtualized) {
 201         int id = virtualObject.getObjectId();
 202         state.setEnsureVirtualized(id, ensureVirtualized);
 203     }
 204 
 205     @Override
 206     public boolean getEnsureVirtualized(VirtualObjectNode virtualObject) {
 207         return state.getObjectState(virtualObject).getEnsureVirtualized();
 208     }
 209 
 210     @Override
 211     public void replaceWithVirtual(VirtualObjectNode virtual) {
 212         closure.addVirtualAlias(virtual, current);
 213         effects.deleteNode(current);
 214         deleted = true;
 215     }
 216 
 217     @Override
 218     public void replaceWithValue(ValueNode replacement) {
 219         effects.replaceAtUsages(current, closure.getScalarAlias(replacement), position);
 220         closure.addScalarAlias(current, replacement);
 221         deleted = true;
 222     }
 223 
 224     @Override
 225     public void delete() {
 226         effects.deleteNode(current);
 227         deleted = true;
 228     }
 229 
 230     @Override
 231     public void replaceFirstInput(Node oldInput, Node replacement) {
 232         effects.replaceFirstInput(current, oldInput, replacement);
 233     }
 234 
 235     @Override
 236     public void addNode(ValueNode node) {
 237         if (node instanceof FloatingNode) {
 238             effects.addFloatingNode(node, "VirtualizerTool");
 239         } else {
 240             effects.addFixedNodeBefore((FixedWithNextNode) node, position);
 241         }
 242     }
 243 
 244     @Override
 245     public void createVirtualObject(VirtualObjectNode virtualObject, ValueNode[] entryState, List<MonitorIdNode> locks, boolean ensureVirtualized) {
 246         VirtualUtil.trace(options, debug, "{{%s}} ", current);
 247         if (!virtualObject.isAlive()) {
 248             effects.addFloatingNode(virtualObject, "newVirtualObject");
 249         }
 250         for (int i = 0; i < entryState.length; i++) {
 251             ValueNode entry = entryState[i];
 252             entryState[i] = entry instanceof VirtualObjectNode ? entry : closure.getAliasAndResolve(state, entry);
 253         }
 254         int id = virtualObject.getObjectId();
 255         if (id == -1) {
 256             id = closure.virtualObjects.size();
 257             closure.virtualObjects.add(virtualObject);
 258             virtualObject.setObjectId(id);
 259         }
 260         state.addObject(id, new ObjectState(entryState, locks, ensureVirtualized));
 261         closure.addVirtualAlias(virtualObject, virtualObject);
 262         PartialEscapeClosure.COUNTER_ALLOCATION_REMOVED.increment(debug);
 263         effects.addVirtualizationDelta(1);
 264     }
 265 
 266     @Override
 267     public int getMaximumEntryCount() {
 268         return MaximumEscapeAnalysisArrayLength.getValue(current.getOptions());
 269     }
 270 
 271     @Override
 272     public void replaceWith(ValueNode node) {
 273         if (node instanceof VirtualObjectNode) {
 274             replaceWithVirtual((VirtualObjectNode) node);
 275         } else {
 276             replaceWithValue(node);
 277         }
 278     }
 279 
 280     @Override
 281     public boolean ensureMaterialized(VirtualObjectNode virtualObject) {
 282         return closure.ensureMaterialized(state, virtualObject.getObjectId(), position, effects, PartialEscapeClosure.COUNTER_MATERIALIZATIONS_UNHANDLED);
 283     }
 284 
 285     @Override
 286     public void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId) {
 287         int id = virtualObject.getObjectId();
 288         state.addLock(id, monitorId);
 289     }
 290 
 291     @Override
 292     public MonitorIdNode removeLock(VirtualObjectNode virtualObject) {
 293         int id = virtualObject.getObjectId();
 294         return state.removeLock(id);
 295     }
 296 
 297     @Override
 298     public MetaAccessProvider getMetaAccess() {
 299         return metaAccess;
 300     }
 301 
 302     @Override
 303     public ConstantReflectionProvider getConstantReflection() {
 304         return constantReflection;
 305     }
 306 
 307     @Override
 308     public boolean canonicalizeReads() {
 309         return false;
 310     }
 311 
 312     @Override
 313     public boolean allUsagesAvailable() {
 314         return true;
 315     }
 316 
 317     @Override
 318     public Assumptions getAssumptions() {
 319         return assumptions;
 320     }
 321 
 322     @Override
 323     public Integer smallestCompareWidth() {
 324         if (loweringProvider != null) {
 325             return loweringProvider.smallestCompareWidth();
 326         } else {
 327             return null;
 328         }
 329     }
 330 }