1 /* 2 * Copyright (c) 2012, 2016, 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.nodes; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0; 26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0; 27 28 import jdk.vm.ci.meta.JavaKind; 29 import jdk.vm.ci.meta.ResolvedJavaMethod; 30 import org.graalvm.compiler.core.common.type.AbstractPointerStamp; 31 import org.graalvm.compiler.core.common.type.ObjectStamp; 32 import org.graalvm.compiler.core.common.type.Stamp; 33 import org.graalvm.compiler.core.common.type.StampFactory; 34 import org.graalvm.compiler.core.common.type.TypeReference; 35 import org.graalvm.compiler.graph.IterableNodeType; 36 import org.graalvm.compiler.graph.Node; 37 import org.graalvm.compiler.graph.NodeClass; 38 import org.graalvm.compiler.graph.spi.Canonicalizable; 39 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 40 import org.graalvm.compiler.nodeinfo.NodeInfo; 41 import org.graalvm.compiler.nodes.extended.GuardingNode; 42 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 43 import org.graalvm.compiler.nodes.memory.ReadNode; 44 import org.graalvm.compiler.nodes.spi.LIRLowerable; 45 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 46 import org.graalvm.compiler.nodes.spi.ValueProxy; 47 import org.graalvm.compiler.nodes.spi.Virtualizable; 48 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 49 import org.graalvm.compiler.nodes.type.StampTool; 50 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 51 52 //JaCoCo Exclude 53 54 import jdk.vm.ci.meta.ResolvedJavaType; 55 56 /** 57 * A node that changes the type of its input, usually narrowing it. For example, a {@link PiNode} 58 * refines the type of a receiver during type-guarded inlining to be the type tested by the guard. 59 * 60 * In contrast to a {@link GuardedValueNode}, a {@link PiNode} is useless as soon as the type of its 61 * input is as narrow or narrower than the {@link PiNode}'s type. The {@link PiNode}, and therefore 62 * also the scheduling restriction enforced by the guard, will go away. 63 */ 64 @NodeInfo(cycles = CYCLES_0, size = SIZE_0) 65 public class PiNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy { 66 67 public static final NodeClass<PiNode> TYPE = NodeClass.create(PiNode.class); 68 @Input ValueNode object; 69 protected Stamp piStamp; 70 71 public ValueNode object() { 72 return object; 73 } 74 75 protected PiNode(NodeClass<? extends PiNode> c, ValueNode object, Stamp stamp, GuardingNode guard) { 76 super(c, stamp, guard); 77 this.object = object; 78 this.piStamp = stamp; 79 assert piStamp.isCompatible(object.stamp()) : "Object stamp not compatible to piStamp"; 80 inferStamp(); 81 } 82 83 public PiNode(ValueNode object, Stamp stamp) { 84 this(object, stamp, null); 85 } 86 87 public PiNode(ValueNode object, Stamp stamp, ValueNode guard) { 88 this(TYPE, object, stamp, (GuardingNode) guard); 89 } 90 91 public PiNode(ValueNode object, ValueNode guard) { 92 this(object, AbstractPointerStamp.pointerNonNull(object.stamp()), guard); 93 } 94 95 public PiNode(ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) { 96 this(object, StampFactory.object(exactType ? TypeReference.createExactTrusted(toType) : TypeReference.createWithoutAssumptions(toType), nonNull || StampTool.isPointerNonNull(object.stamp()))); 97 } 98 99 public static ValueNode create(ValueNode object, Stamp stamp) { 100 ValueNode value = canonical(object, stamp, null); 101 if (value != null) { 102 return value; 103 } 104 return new PiNode(object, stamp); 105 } 106 107 public static ValueNode create(ValueNode object, Stamp stamp, ValueNode guard) { 108 ValueNode value = canonical(object, stamp, (GuardingNode) guard); 109 if (value != null) { 110 return value; 111 } 112 return new PiNode(object, stamp, guard); 113 } 114 115 public static ValueNode create(ValueNode object, ValueNode guard) { 116 Stamp stamp = AbstractPointerStamp.pointerNonNull(object.stamp()); 117 ValueNode value = canonical(object, stamp, (GuardingNode) guard); 118 if (value != null) { 119 return value; 120 } 121 return new PiNode(object, stamp, guard); 122 } 123 124 @SuppressWarnings("unused") 125 public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode object, ValueNode guard) { 126 Stamp stamp = AbstractPointerStamp.pointerNonNull(object.stamp()); 127 ValueNode value = canonical(object, stamp, (GuardingNode) guard); 128 if (value == null) { 129 value = new PiNode(object, stamp, guard); 130 } 131 b.push(JavaKind.Object, b.append(value)); 132 return true; 133 } 134 135 @SuppressWarnings("unused") 136 public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) { 137 Stamp stamp = StampFactory.object(exactType ? TypeReference.createExactTrusted(toType) : TypeReference.createWithoutAssumptions(toType), nonNull || StampTool.isPointerNonNull(object.stamp())); 138 ValueNode value = canonical(object, stamp, null); 139 if (value == null) { 140 value = new PiNode(object, stamp); 141 } 142 b.push(JavaKind.Object, b.append(value)); 143 return true; 144 } 145 146 public final Stamp piStamp() { 147 return piStamp; 148 } 149 150 public void strengthenPiStamp(Stamp newPiStamp) { 151 assert this.piStamp.join(newPiStamp).equals(newPiStamp) : "stamp can only improve"; 152 this.piStamp = newPiStamp; 153 } 154 155 @Override 156 public void generate(NodeLIRBuilderTool generator) { 157 if (generator.hasOperand(object)) { 158 generator.setResult(this, generator.operand(object)); 159 } 160 } 161 162 @Override 163 public boolean inferStamp() { 164 return updateStamp(computeStamp()); 165 } 166 167 private Stamp computeStamp() { 168 return piStamp.improveWith(object().stamp()); 169 } 170 171 @Override 172 public void virtualize(VirtualizerTool tool) { 173 ValueNode alias = tool.getAlias(object()); 174 if (alias instanceof VirtualObjectNode) { 175 VirtualObjectNode virtual = (VirtualObjectNode) alias; 176 if (StampTool.typeOrNull(this) != null && StampTool.typeOrNull(this).isAssignableFrom(virtual.type())) { 177 tool.replaceWithVirtual(virtual); 178 } 179 } 180 } 181 182 public static ValueNode canonical(ValueNode object, Stamp stamp, GuardingNode guard) { 183 // Use most up to date stamp. 184 Stamp computedStamp = stamp.improveWith(object.stamp()); 185 186 // The pi node does not give any additional information => skip it. 187 if (computedStamp.equals(object.stamp())) { 188 return object; 189 } 190 191 if (guard == null) { 192 // Try to merge the pi node with a load node. 193 if (object instanceof ReadNode) { 194 ReadNode readNode = (ReadNode) object; 195 readNode.setStamp(readNode.stamp().improveWith(stamp)); 196 return readNode; 197 } 198 } else { 199 for (Node n : guard.asNode().usages()) { 200 if (n instanceof PiNode) { 201 PiNode otherPi = (PiNode) n; 202 if (object == otherPi.object() && computedStamp.equals(otherPi.stamp())) { 203 /* 204 * Two PiNodes with the same guard and same result, so return the one with 205 * the more precise piStamp. 206 */ 207 Stamp newStamp = stamp.join(otherPi.piStamp); 208 if (newStamp.equals(otherPi.piStamp)) { 209 return otherPi; 210 } 211 } 212 } 213 } 214 } 215 return null; 216 } 217 218 @Override 219 public Node canonical(CanonicalizerTool tool) { 220 Node value = canonical(object(), stamp(), getGuard()); 221 if (value != null) { 222 return value; 223 } 224 return this; 225 } 226 227 @Override 228 public ValueNode getOriginalNode() { 229 return object; 230 } 231 232 public void setOriginalNode(ValueNode newNode) { 233 this.updateUsages(object, newNode); 234 this.object = newNode; 235 assert piStamp.isCompatible(object.stamp()) : "New object stamp not compatible to piStamp"; 236 } 237 238 /** 239 * Casts an object to have an exact, non-null stamp representing {@link Class}. 240 */ 241 public static Class<?> asNonNullClass(Object object) { 242 return asNonNullClassIntrinsic(object, Class.class, true, true); 243 } 244 245 /** 246 * Casts an object to have an exact, non-null stamp representing {@link Class}. 247 */ 248 public static Class<?> asNonNullObject(Object object) { 249 return asNonNullClassIntrinsic(object, Object.class, false, true); 250 } 251 252 @NodeIntrinsic(PiNode.class) 253 private static native Class<?> asNonNullClassIntrinsic(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull); 254 255 /** 256 * Changes the stamp of an object inside a snippet to be the stamp of the node replaced by the 257 * snippet. 258 */ 259 @NodeIntrinsic(PiNode.Placeholder.class) 260 public static native Object piCastToSnippetReplaceeStamp(Object object); 261 262 /** 263 * Changes the stamp of an object and ensures the newly stamped value is non-null and does not 264 * float above a given guard. 265 */ 266 @NodeIntrinsic 267 public static native Object piCastNonNull(Object object, GuardingNode guard); 268 269 /** 270 * Changes the stamp of an object and ensures the newly stamped value is non-null and does not 271 * float above a given guard. 272 */ 273 @NodeIntrinsic 274 public static native Class<?> piCastNonNullClass(Class<?> type, GuardingNode guard); 275 276 /** 277 * Changes the stamp of an object to represent a given type and to indicate that the object is 278 * not null. 279 */ 280 public static Object piCastNonNull(Object object, @ConstantNodeParameter Class<?> toType) { 281 return piCast(object, toType, false, true); 282 } 283 284 @NodeIntrinsic 285 public static native Object piCast(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull); 286 287 /** 288 * A placeholder node in a snippet that will be replaced with a {@link PiNode} when the snippet 289 * is instantiated. 290 */ 291 @NodeInfo(cycles = CYCLES_0, size = SIZE_0) 292 public static class Placeholder extends FloatingGuardedNode { 293 294 public static final NodeClass<Placeholder> TYPE = NodeClass.create(Placeholder.class); 295 @Input ValueNode object; 296 297 public ValueNode object() { 298 return object; 299 } 300 301 protected Placeholder(NodeClass<? extends Placeholder> c, ValueNode object) { 302 super(c, PlaceholderStamp.SINGLETON, null); 303 this.object = object; 304 } 305 306 public Placeholder(ValueNode object) { 307 this(TYPE, object); 308 } 309 310 /** 311 * Replaces this node with a {@link PiNode} during snippet instantiation. 312 * 313 * @param snippetReplaceeStamp the stamp of the node being replace by the snippet 314 */ 315 public void makeReplacement(Stamp snippetReplaceeStamp) { 316 ValueNode value = graph().maybeAddOrUnique(PiNode.create(object(), snippetReplaceeStamp, null)); 317 replaceAndDelete(value); 318 } 319 } 320 321 /** 322 * A stamp for {@link Placeholder} nodes which are only used in snippets. It is replaced by an 323 * actual stamp when the snippet is instantiated. 324 */ 325 public static final class PlaceholderStamp extends ObjectStamp { 326 private static final PlaceholderStamp SINGLETON = new PlaceholderStamp(); 327 328 public static PlaceholderStamp singleton() { 329 return SINGLETON; 330 } 331 332 private PlaceholderStamp() { 333 super(null, false, false, false); 334 } 335 336 @Override 337 public int hashCode() { 338 return System.identityHashCode(this); 339 } 340 341 @Override 342 public boolean equals(Object obj) { 343 return this == obj; 344 } 345 346 @Override 347 public String toString() { 348 return "PlaceholderStamp"; 349 } 350 } 351 }