1 /* 2 * Copyright (c) 2011, 2020, 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.debug.DebugContext; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 34 import org.graalvm.compiler.nodes.ConstantNode; 35 import org.graalvm.compiler.nodes.FixedNode; 36 import org.graalvm.compiler.nodes.FixedWithNextNode; 37 import org.graalvm.compiler.nodes.NodeView; 38 import org.graalvm.compiler.nodes.ValueNode; 39 import org.graalvm.compiler.nodes.calc.FloatingNode; 40 import org.graalvm.compiler.nodes.calc.UnpackEndianHalfNode; 41 import org.graalvm.compiler.nodes.java.MonitorIdNode; 42 import org.graalvm.compiler.nodes.spi.CoreProviders; 43 import org.graalvm.compiler.nodes.spi.CoreProvidersDelegate; 44 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 45 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode; 46 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode; 47 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 48 import org.graalvm.compiler.options.OptionValues; 49 50 import jdk.vm.ci.meta.Assumptions; 51 import jdk.vm.ci.meta.JavaConstant; 52 import jdk.vm.ci.meta.JavaKind; 53 54 /** 55 * Forwards calls from {@link VirtualizerTool} to the actual {@link PartialEscapeBlockState}. 56 */ 57 class VirtualizerToolImpl extends CoreProvidersDelegate implements VirtualizerTool, CanonicalizerTool { 58 59 private final PartialEscapeClosure<?> closure; 60 private final Assumptions assumptions; 61 private final OptionValues options; 62 private final DebugContext debug; 63 private ConstantNode illegalConstant; 64 VirtualizerToolImpl(CoreProviders providers, PartialEscapeClosure<?> closure, Assumptions assumptions, OptionValues options, DebugContext debug)65 VirtualizerToolImpl(CoreProviders providers, PartialEscapeClosure<?> closure, Assumptions assumptions, OptionValues options, DebugContext debug) { 66 super(providers); 67 this.closure = closure; 68 this.assumptions = assumptions; 69 this.options = options; 70 this.debug = debug; 71 } 72 73 private boolean deleted; 74 private PartialEscapeBlockState<?> state; 75 private ValueNode current; 76 private FixedNode position; 77 private GraphEffectList effects; 78 79 @Override getOptions()80 public OptionValues getOptions() { 81 return options; 82 } 83 84 @Override getDebug()85 public DebugContext getDebug() { 86 return debug; 87 } 88 reset(PartialEscapeBlockState<?> newState, ValueNode newCurrent, FixedNode newPosition, GraphEffectList newEffects)89 public void reset(PartialEscapeBlockState<?> newState, ValueNode newCurrent, FixedNode newPosition, GraphEffectList newEffects) { 90 deleted = false; 91 state = newState; 92 current = newCurrent; 93 position = newPosition; 94 effects = newEffects; 95 } 96 isDeleted()97 public boolean isDeleted() { 98 return deleted; 99 } 100 101 @Override getAlias(ValueNode value)102 public ValueNode getAlias(ValueNode value) { 103 return closure.getAliasAndResolve(state, value); 104 } 105 106 @Override getEntry(VirtualObjectNode virtualObject, int index)107 public ValueNode getEntry(VirtualObjectNode virtualObject, int index) { 108 return state.getObjectState(virtualObject).getEntry(index); 109 } 110 111 @Override setVirtualEntry(VirtualObjectNode virtual, int index, ValueNode value, JavaKind theAccessKind, long offset)112 public boolean setVirtualEntry(VirtualObjectNode virtual, int index, ValueNode value, JavaKind theAccessKind, long offset) { 113 ObjectState obj = state.getObjectState(virtual); 114 assert obj.isVirtual() : "not virtual: " + obj; 115 JavaKind entryKind = virtual.entryKind(this.getMetaAccessExtensionProvider(), index); 116 JavaKind accessKind = theAccessKind != null ? theAccessKind : entryKind; 117 ValueNode newValue = closure.getAliasAndResolve(state, value); 118 getDebug().log(DebugContext.DETAILED_LEVEL, "Setting entry %d in virtual object %s %s results in %s", index, virtual.getObjectId(), virtual, state.getObjectState(virtual.getObjectId())); 119 ValueNode oldValue = getEntry(virtual, index); 120 boolean oldIsIllegal = oldValue.isIllegalConstant(); 121 boolean canVirtualize = entryKind == accessKind || (entryKind == accessKind.getStackKind() && virtual instanceof VirtualInstanceNode); 122 if (!canVirtualize) { 123 assert entryKind != JavaKind.Long || newValue != null; 124 if (entryKind == JavaKind.Long && oldValue.getStackKind() == newValue.getStackKind() && oldValue.getStackKind().isPrimitive()) { 125 /* 126 * Special case: If the entryKind is long, allow arbitrary kinds as long as a value 127 * of the same kind is already there. This can only happen if some other node 128 * initialized the entry with a value of a different kind. One example where this 129 * happens is the Truffle NewFrameNode. 130 */ 131 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s with primitive of kind %s in long entry ", current, oldValue.getStackKind()); 132 canVirtualize = true; 133 } else if (entryKind == JavaKind.Int && (accessKind == JavaKind.Long || accessKind == JavaKind.Double) && offset % 8 == 0) { 134 /* 135 * Special case: Allow storing a single long or double value into two consecutive 136 * int slots. 137 */ 138 int nextIndex = virtual.entryIndexForOffset(getMetaAccess(), offset + 4, JavaKind.Int); 139 if (nextIndex != -1) { 140 canVirtualize = true; 141 assert nextIndex == index + 1 : "expected to be sequential"; 142 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s for double word stored in two ints", current); 143 } 144 } else if (canVirtualizeLargeByteArrayUnsafeWrite(virtual, accessKind, offset)) { 145 /* 146 * Special case: Allow storing any primitive inside a byte array, as long as there 147 * is enough room left, and all accesses and subsequent writes are on the exact 148 * position of the first write, and of the same kind. 149 * 150 * Any other access results in materialization. 151 */ 152 int accessLastIndex = virtual.entryIndexForOffset(getMetaAccess(), offset + accessKind.getByteCount() - 1, JavaKind.Byte); 153 if (accessLastIndex != -1 && !oldIsIllegal && canStoreOverOldValue((VirtualArrayNode) virtual, oldValue, accessKind, index)) { 154 canVirtualize = true; 155 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s for %s word stored in byte array", current, accessKind); 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(getMetaAccessExtensionProvider(), 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 } else if (canVirtualizeLargeByteArrayUnsafeWrite(virtual, accessKind, offset)) { 176 for (int i = index + 1; i < index + accessKind.getByteCount(); i++) { 177 state.setEntry(virtual.getObjectId(), i, getIllegalConstant()); 178 } 179 } 180 if (oldIsIllegal) { 181 if (entryKind == JavaKind.Int) { 182 // Storing into second half of double, so replace previous value 183 ValueNode previous = getEntry(virtual, index - 1); 184 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s producing first half of double word value %s", current, previous); 185 ValueNode firstHalf = UnpackEndianHalfNode.create(previous, true, NodeView.DEFAULT); 186 addNode(firstHalf); 187 state.setEntry(virtual.getObjectId(), index - 1, firstHalf); 188 } 189 } 190 return true; 191 } 192 // Should only occur if there are mismatches between the entry and access kind 193 assert entryKind != accessKind; 194 return false; 195 } 196 canStoreOverOldValue(VirtualArrayNode virtual, ValueNode oldValue, JavaKind accessKind, int index)197 private boolean canStoreOverOldValue(VirtualArrayNode virtual, ValueNode oldValue, JavaKind accessKind, int index) { 198 if (!oldValue.getStackKind().isPrimitive()) { 199 return false; 200 } 201 if (isEntryDefaults(virtual, accessKind.getByteCount(), index)) { 202 return true; 203 } 204 return accessKind.getByteCount() == virtual.byteArrayEntryByteCount(index, this); 205 } 206 canVirtualizeLargeByteArrayUnsafeWrite(VirtualObjectNode virtual, JavaKind accessKind, long offset)207 private boolean canVirtualizeLargeByteArrayUnsafeWrite(VirtualObjectNode virtual, JavaKind accessKind, long offset) { 208 return canVirtualizeLargeByteArrayUnsafeAccess() && virtual.isVirtualByteArrayAccess(this.getMetaAccessExtensionProvider(), accessKind) && 209 /* 210 * Require aligned writes. Some architectures do not support recovering 211 * writes to unaligned offsets. Since most use cases for this optimization 212 * will write to reasonable offsets, disabling the optimization for 213 * unreasonable ones is not that big an issue. 214 */ 215 ((offset % accessKind.getByteCount()) == 0); 216 } 217 getVirtualByteCount(ValueNode[] entries, int startIndex)218 int getVirtualByteCount(ValueNode[] entries, int startIndex) { 219 int pos = startIndex + 1; 220 while (pos < entries.length && entries[pos].getStackKind() == JavaKind.Illegal) { 221 pos++; 222 } 223 return pos - startIndex; 224 } 225 isEntryDefaults(ObjectState object, int byteCount, int index)226 boolean isEntryDefaults(ObjectState object, int byteCount, int index) { 227 for (int i = index; i < index + byteCount; i++) { 228 if (!object.getEntry(i).isDefaultConstant()) { 229 return false; 230 } 231 } 232 return true; 233 } 234 isEntryDefaults(VirtualObjectNode virtual, int byteCount, int index)235 boolean isEntryDefaults(VirtualObjectNode virtual, int byteCount, int index) { 236 return isEntryDefaults(state.getObjectState(virtual), byteCount, index); 237 } 238 getIllegalConstant()239 public ValueNode getIllegalConstant() { 240 if (illegalConstant == null) { 241 /* Try not to spawn a second illegal constant in the graph. */ 242 illegalConstant = ConstantNode.forConstant(JavaConstant.forIllegal(), getMetaAccess(), closure.cfg.graph); 243 } 244 return illegalConstant; 245 } 246 247 @Override setEnsureVirtualized(VirtualObjectNode virtualObject, boolean ensureVirtualized)248 public void setEnsureVirtualized(VirtualObjectNode virtualObject, boolean ensureVirtualized) { 249 int id = virtualObject.getObjectId(); 250 state.setEnsureVirtualized(id, ensureVirtualized); 251 } 252 253 @Override getEnsureVirtualized(VirtualObjectNode virtualObject)254 public boolean getEnsureVirtualized(VirtualObjectNode virtualObject) { 255 return state.getObjectState(virtualObject).getEnsureVirtualized(); 256 } 257 258 @Override replaceWithVirtual(VirtualObjectNode virtual)259 public void replaceWithVirtual(VirtualObjectNode virtual) { 260 closure.addVirtualAlias(virtual, current); 261 effects.deleteNode(current); 262 deleted = true; 263 } 264 265 @Override replaceWithValue(ValueNode replacement)266 public void replaceWithValue(ValueNode replacement) { 267 effects.replaceAtUsages(current, closure.getScalarAlias(replacement), position); 268 closure.addScalarAlias(current, replacement); 269 deleted = true; 270 } 271 272 @Override delete()273 public void delete() { 274 effects.deleteNode(current); 275 deleted = true; 276 } 277 278 @Override replaceFirstInput(Node oldInput, Node replacement)279 public void replaceFirstInput(Node oldInput, Node replacement) { 280 effects.replaceFirstInput(current, oldInput, replacement); 281 } 282 283 @Override addNode(ValueNode node)284 public void addNode(ValueNode node) { 285 if (node instanceof FloatingNode) { 286 effects.addFloatingNode(node, "VirtualizerTool"); 287 } else { 288 effects.addFixedNodeBefore((FixedWithNextNode) node, position); 289 } 290 } 291 292 @Override createVirtualObject(VirtualObjectNode virtualObject, ValueNode[] entryState, List<MonitorIdNode> locks, boolean ensureVirtualized)293 public void createVirtualObject(VirtualObjectNode virtualObject, ValueNode[] entryState, List<MonitorIdNode> locks, boolean ensureVirtualized) { 294 VirtualUtil.trace(options, debug, "{{%s}} ", current); 295 if (!virtualObject.isAlive()) { 296 effects.addFloatingNode(virtualObject, "newVirtualObject"); 297 } 298 for (int i = 0; i < entryState.length; i++) { 299 ValueNode entry = entryState[i]; 300 entryState[i] = entry instanceof VirtualObjectNode ? entry : closure.getAliasAndResolve(state, entry); 301 } 302 int id = virtualObject.getObjectId(); 303 if (id == -1) { 304 id = closure.virtualObjects.size(); 305 closure.virtualObjects.add(virtualObject); 306 virtualObject.setObjectId(id); 307 } 308 state.addObject(id, new ObjectState(entryState, locks, ensureVirtualized)); 309 closure.addVirtualAlias(virtualObject, virtualObject); 310 PartialEscapeClosure.COUNTER_ALLOCATION_REMOVED.increment(debug); 311 effects.addVirtualizationDelta(1); 312 } 313 314 @Override getMaximumEntryCount()315 public int getMaximumEntryCount() { 316 return MaximumEscapeAnalysisArrayLength.getValue(current.getOptions()); 317 } 318 319 @Override replaceWith(ValueNode node)320 public void replaceWith(ValueNode node) { 321 if (node instanceof VirtualObjectNode) { 322 replaceWithVirtual((VirtualObjectNode) node); 323 } else { 324 replaceWithValue(node); 325 } 326 } 327 328 @Override ensureMaterialized(VirtualObjectNode virtualObject)329 public boolean ensureMaterialized(VirtualObjectNode virtualObject) { 330 return closure.ensureMaterialized(state, virtualObject.getObjectId(), position, effects, PartialEscapeClosure.COUNTER_MATERIALIZATIONS_UNHANDLED); 331 } 332 333 @Override addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId)334 public void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId) { 335 int id = virtualObject.getObjectId(); 336 state.addLock(id, monitorId); 337 } 338 339 @Override removeLock(VirtualObjectNode virtualObject)340 public MonitorIdNode removeLock(VirtualObjectNode virtualObject) { 341 int id = virtualObject.getObjectId(); 342 return state.removeLock(id); 343 } 344 345 @Override canVirtualizeLargeByteArrayUnsafeAccess()346 public boolean canVirtualizeLargeByteArrayUnsafeAccess() { 347 if (getPlatformConfigurationProvider() != null) { 348 return getPlatformConfigurationProvider().canVirtualizeLargeByteArrayAccess(); 349 } 350 return false; 351 } 352 353 @Override canonicalizeReads()354 public boolean canonicalizeReads() { 355 return false; 356 } 357 358 @Override allUsagesAvailable()359 public boolean allUsagesAvailable() { 360 return true; 361 } 362 363 @Override getAssumptions()364 public Assumptions getAssumptions() { 365 return assumptions; 366 } 367 368 @Override smallestCompareWidth()369 public Integer smallestCompareWidth() { 370 if (getLowerer() != null) { 371 return getLowerer().smallestCompareWidth(); 372 } else { 373 return null; 374 } 375 } 376 } 377