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 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 ValueNode newValue; 130 JavaKind entryKind = virtual.entryKind(index); 131 JavaKind accessKind = theAccessKind != null ? theAccessKind : entryKind; 132 if (value == null) { 133 newValue = null; 134 } else { 135 newValue = closure.getAliasAndResolve(state, value); 136 } 137 getDebug().log(DebugContext.DETAILED_LEVEL, "Setting entry %d in virtual object %s %s results in %s", index, virtual.getObjectId(), virtual, state.getObjectState(virtual.getObjectId())); 138 ValueNode oldValue = getEntry(virtual, index); 139 boolean canVirtualize = entryKind == accessKind || (entryKind == accessKind.getStackKind() && virtual instanceof VirtualInstanceNode); 140 if (!canVirtualize) { 141 if (entryKind == JavaKind.Long && oldValue.getStackKind() == newValue.getStackKind() && oldValue.getStackKind().isPrimitive()) { 142 /* 143 * Special case: If the entryKind is long, allow arbitrary kinds as long as a value 144 * of the same kind is already there. This can only happen if some other node 145 * initialized the entry with a value of a different kind. One example where this 146 * happens is the Truffle NewFrameNode. 147 */ 148 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s with primitive of kind %s in long entry ", current, oldValue.getStackKind()); 149 canVirtualize = true; 150 } else if (entryKind == JavaKind.Int && (accessKind == JavaKind.Long || accessKind == JavaKind.Double) && offset % 8 == 0) { 151 /* 152 * Special case: Allow storing a single long or double value into two consecutive 153 * int slots. 154 */ 155 int nextIndex = virtual.entryIndexForOffset(getMetaAccess(), offset + 4, JavaKind.Int); 156 if (nextIndex != -1) { 157 canVirtualize = true; 158 assert nextIndex == index + 1 : "expected to be sequential"; 159 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s for double word stored in two ints", current); 160 } 161 } 162 } 163 164 if (canVirtualize) { 165 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s for entryKind %s and access kind %s", current, entryKind, accessKind); 166 state.setEntry(virtual.getObjectId(), index, newValue); 167 if (entryKind == JavaKind.Int) { 168 if (accessKind.needsTwoSlots()) { 169 // Storing double word value two int slots 170 assert virtual.entryKind(index + 1) == JavaKind.Int; 171 state.setEntry(virtual.getObjectId(), index + 1, getIllegalConstant()); 172 } else if (oldValue.getStackKind() == JavaKind.Double || oldValue.getStackKind() == JavaKind.Long) { 173 // Splitting double word constant by storing over it with an int 174 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s producing second half of double word value %s", current, oldValue); 175 ValueNode secondHalf = UnpackEndianHalfNode.create(oldValue, false, NodeView.DEFAULT); 176 addNode(secondHalf); 177 state.setEntry(virtual.getObjectId(), index + 1, secondHalf); 178 } 179 } 180 if (oldValue.isConstant() && oldValue.asConstant().equals(JavaConstant.forIllegal())) { 181 // Storing into second half of double, so replace previous value 182 ValueNode previous = getEntry(virtual, index - 1); 183 getDebug().log(DebugContext.DETAILED_LEVEL, "virtualizing %s producing first half of double word value %s", current, previous); 184 ValueNode firstHalf = UnpackEndianHalfNode.create(previous, true, NodeView.DEFAULT); 185 addNode(firstHalf); 186 state.setEntry(virtual.getObjectId(), index - 1, firstHalf); 187 } 188 return true; 189 } 190 // Should only occur if there are mismatches between the entry and access kind 191 assert entryKind != accessKind; 192 return false; 193 } 194 195 private ValueNode getIllegalConstant() { 196 if (illegalConstant == null) { 197 illegalConstant = ConstantNode.forConstant(JavaConstant.forIllegal(), getMetaAccess()); 198 addNode(illegalConstant); 199 } 200 return illegalConstant; 201 } 202 203 @Override 204 public void setEnsureVirtualized(VirtualObjectNode virtualObject, boolean ensureVirtualized) { 205 int id = virtualObject.getObjectId(); 206 state.setEnsureVirtualized(id, ensureVirtualized); 207 } 208 209 @Override 210 public boolean getEnsureVirtualized(VirtualObjectNode virtualObject) { 211 return state.getObjectState(virtualObject).getEnsureVirtualized(); 212 } 213 214 @Override 215 public void replaceWithVirtual(VirtualObjectNode virtual) { 216 closure.addVirtualAlias(virtual, current); 217 effects.deleteNode(current); 218 deleted = true; 219 } 220 221 @Override 222 public void replaceWithValue(ValueNode replacement) { 223 effects.replaceAtUsages(current, closure.getScalarAlias(replacement), position); 224 closure.addScalarAlias(current, replacement); 225 deleted = true; 226 } 227 228 @Override 229 public void delete() { 230 effects.deleteNode(current); 231 deleted = true; 232 } 233 234 @Override 235 public void replaceFirstInput(Node oldInput, Node replacement) { 236 effects.replaceFirstInput(current, oldInput, replacement); 237 } 238 239 @Override 240 public void addNode(ValueNode node) { 241 if (node instanceof FloatingNode) { 242 effects.addFloatingNode(node, "VirtualizerTool"); 243 } else { 244 effects.addFixedNodeBefore((FixedWithNextNode) node, position); 245 } 246 } 247 248 @Override 249 public void createVirtualObject(VirtualObjectNode virtualObject, ValueNode[] entryState, List<MonitorIdNode> locks, boolean ensureVirtualized) { 250 VirtualUtil.trace(options, debug, "{{%s}} ", current); 251 if (!virtualObject.isAlive()) { 252 effects.addFloatingNode(virtualObject, "newVirtualObject"); 253 } 254 for (int i = 0; i < entryState.length; i++) { 255 ValueNode entry = entryState[i]; 256 entryState[i] = entry instanceof VirtualObjectNode ? entry : closure.getAliasAndResolve(state, entry); 257 } 258 int id = virtualObject.getObjectId(); 259 if (id == -1) { 260 id = closure.virtualObjects.size(); 261 closure.virtualObjects.add(virtualObject); 262 virtualObject.setObjectId(id); 263 } 264 state.addObject(id, new ObjectState(entryState, locks, ensureVirtualized)); 265 closure.addVirtualAlias(virtualObject, virtualObject); 266 PartialEscapeClosure.COUNTER_ALLOCATION_REMOVED.increment(debug); 267 effects.addVirtualizationDelta(1); 268 } 269 270 @Override 271 public int getMaximumEntryCount() { 272 return MaximumEscapeAnalysisArrayLength.getValue(current.getOptions()); 273 } 274 275 @Override 276 public void replaceWith(ValueNode node) { 277 if (node instanceof VirtualObjectNode) { 278 replaceWithVirtual((VirtualObjectNode) node); 279 } else { 280 replaceWithValue(node); 281 } 282 } 283 284 @Override 285 public boolean ensureMaterialized(VirtualObjectNode virtualObject) { 286 return closure.ensureMaterialized(state, virtualObject.getObjectId(), position, effects, PartialEscapeClosure.COUNTER_MATERIALIZATIONS_UNHANDLED); 287 } 288 289 @Override 290 public void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId) { 291 int id = virtualObject.getObjectId(); 292 state.addLock(id, monitorId); 293 } 294 295 @Override 296 public MonitorIdNode removeLock(VirtualObjectNode virtualObject) { 297 int id = virtualObject.getObjectId(); 298 return state.removeLock(id); 299 } 300 301 @Override 302 public MetaAccessProvider getMetaAccess() { 303 return metaAccess; 304 } 305 306 @Override 307 public ConstantReflectionProvider getConstantReflection() { 308 return constantReflection; 309 } 310 311 @Override 312 public boolean canonicalizeReads() { 313 return false; 314 } 315 316 @Override 317 public boolean allUsagesAvailable() { 318 return true; 319 } 320 321 @Override 322 public Assumptions getAssumptions() { 323 return assumptions; 324 } 325 326 @Override 327 public Integer smallestCompareWidth() { 328 if (loweringProvider != null) { 329 return loweringProvider.smallestCompareWidth(); 330 } else { 331 return null; 332 } 333 } 334 }