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 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; | 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 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; |