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 org.graalvm.compiler.core.common.type.Stamp; 29 import org.graalvm.compiler.core.common.type.StampFactory; 30 import org.graalvm.compiler.core.common.type.TypeReference; 31 import org.graalvm.compiler.graph.IterableNodeType; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.graph.NodeClass; 34 import org.graalvm.compiler.graph.spi.Canonicalizable; 35 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 36 import org.graalvm.compiler.nodeinfo.NodeInfo; 37 import org.graalvm.compiler.nodes.extended.GuardingNode; 38 import org.graalvm.compiler.nodes.extended.UnsafeLoadNode; 39 import org.graalvm.compiler.nodes.java.LoadFieldNode; 40 import org.graalvm.compiler.nodes.spi.LIRLowerable; 41 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 42 import org.graalvm.compiler.nodes.spi.ValueProxy; 43 import org.graalvm.compiler.nodes.spi.Virtualizable; 44 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 45 import org.graalvm.compiler.nodes.type.StampTool; 46 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 47 48 //JaCoCo Exclude 49 50 import jdk.vm.ci.meta.JavaKind; 51 import jdk.vm.ci.meta.ResolvedJavaType; 52 53 /** 54 * A node that changes the type of its input, usually narrowing it. For example, a {@link PiNode} 55 * refines the type of a receiver during type-guarded inlining to be the type tested by the guard. 56 * 57 * In contrast to a {@link GuardedValueNode}, a {@link PiNode} is useless as soon as the type of its 58 * input is as narrow or narrower than the {@link PiNode}'s type. The {@link PiNode}, and therefore 59 * also the scheduling restriction enforced by the anchor, will go away. 60 */ 61 @NodeInfo(cycles = CYCLES_0, size = SIZE_0) 62 public class PiNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy { 63 64 public static final NodeClass<PiNode> TYPE = NodeClass.create(PiNode.class); 65 @Input ValueNode object; 66 protected final Stamp piStamp; 67 68 public ValueNode object() { 69 return object; 70 } 71 72 protected PiNode(NodeClass<? extends PiNode> c, ValueNode object, Stamp stamp, GuardingNode guard) { 73 super(c, stamp, guard); 74 this.object = object; 75 this.piStamp = stamp; 76 inferStamp(); 77 } 78 79 public PiNode(ValueNode object, Stamp stamp) { 80 this(object, stamp, null); 81 } 82 83 public PiNode(ValueNode object, Stamp stamp, ValueNode anchor) { 84 this(TYPE, object, stamp, (GuardingNode) anchor); 85 } 86 87 public PiNode(ValueNode object, ValueNode anchor) { 88 this(object, object.stamp().join(StampFactory.objectNonNull()), anchor); 89 } 90 91 public PiNode(ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) { 92 this(object, StampFactory.object(exactType ? TypeReference.createExactTrusted(toType) : TypeReference.createWithoutAssumptions(toType), nonNull || StampTool.isPointerNonNull(object.stamp()))); 93 } 94 95 @Override 96 public void generate(NodeLIRBuilderTool generator) { 97 if (object.getStackKind() != JavaKind.Void && object.getStackKind() != JavaKind.Illegal) { 98 generator.setResult(this, generator.operand(object)); 99 } 100 } 101 102 @Override 103 public boolean inferStamp() { 104 return updateStamp(computeStamp()); 105 } 106 107 private Stamp computeStamp() { 108 // When piStamp == StampFactory.forNodeIntrinsic() then stamp is either 109 // StampFactory.forNodeIntrinsic() or it has been updated during snippet 110 // lowering to be the stamp of the node being replaced by the snippet. 111 if (piStamp == StampFactory.forNodeIntrinsic()) { 112 return stamp; 113 } 114 return piStamp.improveWith(object().stamp()); 115 } 116 117 @Override 118 public void virtualize(VirtualizerTool tool) { 119 ValueNode alias = tool.getAlias(object()); 120 if (alias instanceof VirtualObjectNode) { 121 VirtualObjectNode virtual = (VirtualObjectNode) alias; 122 if (StampTool.typeOrNull(this) != null && StampTool.typeOrNull(this).isAssignableFrom(virtual.type())) { 123 tool.replaceWithVirtual(virtual); 124 } 125 } 126 } 127 128 @Override 129 public Node canonical(CanonicalizerTool tool) { 130 if (stamp() == StampFactory.forNodeIntrinsic()) { 131 /* The actual stamp has not been set yet. */ 132 return this; 133 } 134 // Use most up to date stamp. 135 Stamp computedStamp = computeStamp(); 136 137 ValueNode o = object(); 138 139 // The pi node does not give any additional information => skip it. 140 if (computedStamp.equals(o.stamp())) { 141 return o; 142 } 143 144 GuardingNode g = getGuard(); 145 if (g == null) { 146 147 // Try to merge the pi node with a load node. 148 if (o instanceof LoadFieldNode) { 149 LoadFieldNode loadFieldNode = (LoadFieldNode) o; 150 loadFieldNode.setStamp(loadFieldNode.stamp().improveWith(this.piStamp)); 151 return loadFieldNode; 152 } else if (o instanceof UnsafeLoadNode) { 153 UnsafeLoadNode unsafeLoadNode = (UnsafeLoadNode) o; 154 unsafeLoadNode.setStamp(unsafeLoadNode.stamp().improveWith(this.piStamp)); 155 return unsafeLoadNode; 156 } 157 } else { 158 for (Node n : g.asNode().usages()) { 159 if (n instanceof PiNode) { 160 PiNode otherPi = (PiNode) n; 161 if (o == otherPi.object() && computedStamp.equals(otherPi.stamp())) { 162 /* 163 * Two PiNodes with the same guard and same result, so return the one with 164 * the more precise piStamp. 165 */ 166 Stamp newStamp = piStamp.join(otherPi.piStamp); 167 if (newStamp.equals(otherPi.piStamp)) { 168 return otherPi; 169 } 170 } 171 } 172 } 173 } 174 return this; 175 } 176 177 @Override 178 public ValueNode getOriginalNode() { 179 return object; 180 } 181 182 /** 183 * Casts an object to have an exact, non-null stamp representing {@link Class}. 184 */ 185 public static Class<?> asNonNullClass(Object object) { 186 return asNonNullClassIntrinsic(object, Class.class, true, true); 187 } 188 189 /** 190 * Casts an object to have an exact, non-null stamp representing {@link Class}. 191 */ 192 public static Class<?> asNonNullObject(Object object) { 193 return asNonNullClassIntrinsic(object, Object.class, false, true); 194 } 195 196 @NodeIntrinsic(PiNode.class) 197 private static native Class<?> asNonNullClassIntrinsic(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull); 198 199 /** 200 * Changes the stamp of an object. 201 */ 202 @NodeIntrinsic 203 public static native Object piCast(Object object, @ConstantNodeParameter Stamp stamp); 204 205 /** 206 * Changes the stamp of an object and ensures the newly stamped value does not float above a 207 * given anchor. 208 */ 209 @NodeIntrinsic 210 public static native Object piCast(Object object, @ConstantNodeParameter Stamp stamp, GuardingNode anchor); 211 212 /** 213 * Changes the stamp of an object and ensures the newly stamped value is non-null and does not 214 * float above a given anchor. 215 */ 216 @NodeIntrinsic 217 public static native Object piCastNonNull(Object object, GuardingNode anchor); 218 219 /** 220 * Changes the stamp of an object to represent a given type and to indicate that the object is 221 * not null. 222 */ 223 public static Object piCastNonNull(Object object, @ConstantNodeParameter Class<?> toType) { 224 return piCast(object, toType, false, true); 225 } 226 227 @NodeIntrinsic 228 public static native Object piCast(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull); 229 }