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