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 org.graalvm.compiler.core.common.type.Stamp; 26 import org.graalvm.compiler.graph.Node; 27 import org.graalvm.compiler.graph.NodeClass; 28 import org.graalvm.compiler.graph.iterators.NodePredicate; 29 import org.graalvm.compiler.nodeinfo.InputType; 30 import org.graalvm.compiler.nodeinfo.NodeInfo; 31 import org.graalvm.compiler.nodes.spi.NodeValueMap; 32 33 import jdk.vm.ci.meta.Constant; 34 import jdk.vm.ci.meta.JavaConstant; 35 import jdk.vm.ci.meta.JavaKind; 36 37 /** 38 * This class represents a value within the graph, including local variables, phis, and all other 39 * instructions. 40 */ 41 @NodeInfo 42 public abstract class ValueNode extends org.graalvm.compiler.graph.Node implements ValueNodeInterface { 43 44 public static final NodeClass<ValueNode> TYPE = NodeClass.create(ValueNode.class); 45 /** 46 * The kind of this value. This is {@link JavaKind#Void} for instructions that produce no value. 47 * This kind is guaranteed to be a {@linkplain JavaKind#getStackKind() stack kind}. 48 */ 49 protected Stamp stamp; 50 126 */ 127 public final boolean isNullConstant() { 128 JavaConstant value = asJavaConstant(); 129 return value != null && value.isNull(); 130 } 131 132 /** 133 * Convert this value to a constant if it is a constant, otherwise return null. 134 * 135 * @return the {@link JavaConstant} represented by this value if it is a constant; {@code null} 136 * otherwise 137 */ 138 public final Constant asConstant() { 139 if (this instanceof ConstantNode) { 140 return ((ConstantNode) this).getValue(); 141 } else { 142 return null; 143 } 144 } 145 146 public final JavaConstant asJavaConstant() { 147 Constant value = asConstant(); 148 if (value instanceof JavaConstant) { 149 return (JavaConstant) value; 150 } else { 151 return null; 152 } 153 } 154 155 @Override 156 public ValueNode asNode() { 157 return this; 158 } 159 160 @Override 161 public boolean isAllowedUsageType(InputType type) { 162 if (getStackKind() != JavaKind.Void && type == InputType.Value) { 163 return true; 164 } else { 165 return super.isAllowedUsageType(type); 166 } 167 } 168 169 /** 170 * Checks if this node has usages other than the given node {@code node}. 171 * 172 * @param node node which is ignored when searching for usages 173 * @param nodeValueMap 174 * @return true if this node has other usages, false otherwise 175 */ 176 public boolean hasUsagesOtherThan(ValueNode node, NodeValueMap nodeValueMap) { 177 for (Node usage : usages()) { 178 if (usage != node && usage instanceof ValueNode && nodeValueMap.hasOperand(usage)) { 179 return true; 180 } 181 } 182 return false; 183 } 184 185 } | 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 java.util.function.Predicate; 26 27 import org.graalvm.compiler.core.common.type.Stamp; 28 import org.graalvm.compiler.graph.Node; 29 import org.graalvm.compiler.graph.NodeClass; 30 import org.graalvm.compiler.graph.iterators.NodePredicate; 31 import org.graalvm.compiler.nodeinfo.InputType; 32 import org.graalvm.compiler.nodeinfo.NodeInfo; 33 import org.graalvm.compiler.nodeinfo.Verbosity; 34 import org.graalvm.compiler.nodes.spi.NodeValueMap; 35 36 import jdk.vm.ci.meta.Constant; 37 import jdk.vm.ci.meta.JavaConstant; 38 import jdk.vm.ci.meta.JavaKind; 39 40 /** 41 * This class represents a value within the graph, including local variables, phis, and all other 42 * instructions. 43 */ 44 @NodeInfo 45 public abstract class ValueNode extends org.graalvm.compiler.graph.Node implements ValueNodeInterface { 46 47 public static final NodeClass<ValueNode> TYPE = NodeClass.create(ValueNode.class); 48 /** 49 * The kind of this value. This is {@link JavaKind#Void} for instructions that produce no value. 50 * This kind is guaranteed to be a {@linkplain JavaKind#getStackKind() stack kind}. 51 */ 52 protected Stamp stamp; 53 129 */ 130 public final boolean isNullConstant() { 131 JavaConstant value = asJavaConstant(); 132 return value != null && value.isNull(); 133 } 134 135 /** 136 * Convert this value to a constant if it is a constant, otherwise return null. 137 * 138 * @return the {@link JavaConstant} represented by this value if it is a constant; {@code null} 139 * otherwise 140 */ 141 public final Constant asConstant() { 142 if (this instanceof ConstantNode) { 143 return ((ConstantNode) this).getValue(); 144 } else { 145 return null; 146 } 147 } 148 149 public final boolean isJavaConstant() { 150 return isConstant() && asConstant() instanceof JavaConstant; 151 } 152 153 public final JavaConstant asJavaConstant() { 154 Constant value = asConstant(); 155 if (value instanceof JavaConstant) { 156 return (JavaConstant) value; 157 } else { 158 return null; 159 } 160 } 161 162 @Override 163 public ValueNode asNode() { 164 return this; 165 } 166 167 @Override 168 public boolean isAllowedUsageType(InputType type) { 169 if (getStackKind() != JavaKind.Void && type == InputType.Value) { 170 return true; 171 } else { 172 return super.isAllowedUsageType(type); 173 } 174 } 175 176 /** 177 * Checks if this node has usages other than the given node {@code node}. 178 * 179 * @param node node which is ignored when searching for usages 180 * @param nodeValueMap 181 * @return true if this node has other usages, false otherwise 182 */ 183 public boolean hasUsagesOtherThan(ValueNode node, NodeValueMap nodeValueMap) { 184 for (Node usage : usages()) { 185 if (usage != node && usage instanceof ValueNode && nodeValueMap.hasOperand(usage)) { 186 return true; 187 } 188 } 189 return false; 190 } 191 192 @Override 193 protected void replaceAtUsages(Node other, Predicate<Node> filter, Node toBeDeleted) { 194 super.replaceAtUsages(other, filter, toBeDeleted); 195 assert checkReplaceAtUsagesInvariants(other); 196 } 197 198 private boolean checkReplaceAtUsagesInvariants(Node other) { 199 assert other == null || other instanceof ValueNode; 200 if (this.hasUsages() && !this.stamp().isEmpty() && !(other instanceof PhiNode) && other != null) { 201 assert ((ValueNode) other).stamp().getClass() == stamp().getClass() : "stamp have to be of same class"; 202 boolean morePrecise = ((ValueNode) other).stamp().join(stamp()).equals(((ValueNode) other).stamp()); 203 assert morePrecise : "stamp can only get more precise " + toString(Verbosity.All) + " " + 204 other.toString(Verbosity.All); 205 } 206 return true; 207 } 208 } |