1 /* 2 * Copyright (c) 2009, 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 24 25 package org.graalvm.compiler.nodes; 26 27 import java.util.function.Predicate; 28 29 import org.graalvm.compiler.core.common.type.Stamp; 30 import org.graalvm.compiler.graph.Node; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.graph.iterators.NodePredicate; 33 import org.graalvm.compiler.nodeinfo.InputType; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodeinfo.Verbosity; 36 import org.graalvm.compiler.nodes.spi.NodeValueMap; 37 38 import jdk.vm.ci.meta.Constant; 39 import jdk.vm.ci.meta.JavaConstant; 40 import jdk.vm.ci.meta.JavaKind; 41 42 /** 43 * This class represents a value within the graph, including local variables, phis, and all other 44 * instructions. 45 */ 46 @NodeInfo 47 public abstract class ValueNode extends org.graalvm.compiler.graph.Node implements ValueNodeInterface { 48 49 public static final NodeClass<ValueNode> TYPE = NodeClass.create(ValueNode.class); 50 /** 51 * The kind of this value. This is {@link JavaKind#Void} for instructions that produce no value. 52 * This kind is guaranteed to be a {@linkplain JavaKind#getStackKind() stack kind}. 53 */ 54 protected Stamp stamp; 55 56 public ValueNode(NodeClass<? extends ValueNode> c, Stamp stamp) { 57 super(c); 58 this.stamp = stamp; 59 } 60 61 public final Stamp stamp(NodeView view) { 62 return view.stamp(this); 63 } 64 65 public final void setStamp(Stamp stamp) { 66 this.stamp = stamp; 67 assert !isAlive() || !inferStamp() : "setStamp called on a node that overrides inferStamp: " + this; 68 } 69 70 @Override 71 public final StructuredGraph graph() { 72 return (StructuredGraph) super.graph(); 73 } 74 75 /** 76 * Checks if the given stamp is different than the current one ( 77 * {@code newStamp.equals(oldStamp) == false}). If it is different then the new stamp will 78 * become the current stamp for this node. 79 * 80 * @return true if the stamp has changed, false otherwise. 81 */ 82 protected final boolean updateStamp(Stamp newStamp) { 83 if (newStamp == null || newStamp.equals(stamp)) { 84 return false; 85 } else { 86 stamp = newStamp; 87 return true; 88 } 89 } 90 91 /** 92 * This method can be overridden by subclasses of {@link ValueNode} if they need to recompute 93 * their stamp if their inputs change. A typical implementation will compute the stamp and pass 94 * it to {@link #updateStamp(Stamp)}, whose return value can be used as the result of this 95 * method. 96 * 97 * @return true if the stamp has changed, false otherwise. 98 */ 99 public boolean inferStamp() { 100 return false; 101 } 102 103 public final JavaKind getStackKind() { 104 return stamp(NodeView.DEFAULT).getStackKind(); 105 } 106 107 /** 108 * Checks whether this value is a constant (i.e. it is of type {@link ConstantNode}. 109 * 110 * @return {@code true} if this value is a constant 111 */ 112 public final boolean isConstant() { 113 return this instanceof ConstantNode; 114 } 115 116 private static final NodePredicate IS_CONSTANT = new NodePredicate() { 117 @Override 118 public boolean apply(Node n) { 119 return n instanceof ConstantNode; 120 } 121 }; 122 123 public static NodePredicate isConstantPredicate() { 124 return IS_CONSTANT; 125 } 126 127 /** 128 * Checks whether this value represents the null constant. 129 * 130 * @return {@code true} if this value represents the null constant 131 */ 132 public final boolean isNullConstant() { 133 JavaConstant value = asJavaConstant(); 134 return value != null && value.isNull(); 135 } 136 137 public final boolean isDefaultConstant() { 138 Constant value = asConstant(); 139 return value != null && value.isDefaultForKind(); 140 } 141 142 /** 143 * Convert this value to a constant if it is a constant, otherwise return null. 144 * 145 * @return the {@link JavaConstant} represented by this value if it is a constant; {@code null} 146 * otherwise 147 */ 148 public final Constant asConstant() { 149 if (this instanceof ConstantNode) { 150 return ((ConstantNode) this).getValue(); 151 } else { 152 return null; 153 } 154 } 155 156 public final boolean isJavaConstant() { 157 return isConstant() && asConstant() instanceof JavaConstant; 158 } 159 160 public final JavaConstant asJavaConstant() { 161 Constant value = asConstant(); 162 if (value instanceof JavaConstant) { 163 return (JavaConstant) value; 164 } else { 165 return null; 166 } 167 } 168 169 @Override 170 public ValueNode asNode() { 171 return this; 172 } 173 174 @Override 175 public boolean isAllowedUsageType(InputType type) { 176 if (getStackKind() != JavaKind.Void && type == InputType.Value) { 177 return true; 178 } else { 179 return super.isAllowedUsageType(type); 180 } 181 } 182 183 /** 184 * Checks if this node has usages other than the given node {@code node}. 185 * 186 * @param node node which is ignored when searching for usages 187 * @param nodeValueMap 188 * @return true if this node has other usages, false otherwise 189 */ 190 public boolean hasUsagesOtherThan(ValueNode node, NodeValueMap nodeValueMap) { 191 for (Node usage : usages()) { 192 if (usage != node && usage instanceof ValueNode && nodeValueMap.hasOperand(usage)) { 193 return true; 194 } 195 } 196 return false; 197 } 198 199 @Override 200 protected void replaceAtUsages(Node other, Predicate<Node> filter, Node toBeDeleted) { 201 super.replaceAtUsages(other, filter, toBeDeleted); 202 assert checkReplaceAtUsagesInvariants(other); 203 } 204 205 private boolean checkReplaceAtUsagesInvariants(Node other) { 206 assert other == null || other instanceof ValueNode; 207 if (this.hasUsages() && !this.stamp(NodeView.DEFAULT).isEmpty() && !(other instanceof PhiNode) && other != null) { 208 assert ((ValueNode) other).stamp(NodeView.DEFAULT).getClass() == stamp(NodeView.DEFAULT).getClass() : "stamp have to be of same class"; 209 boolean morePrecise = ((ValueNode) other).stamp(NodeView.DEFAULT).join(stamp(NodeView.DEFAULT)).equals(((ValueNode) other).stamp(NodeView.DEFAULT)); 210 assert morePrecise : "stamp can only get more precise " + toString(Verbosity.All) + " " + 211 other.toString(Verbosity.All); 212 } 213 return true; 214 } 215 }