1 /* 2 * Copyright (c) 2009, 2019, 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.calc; 26 27 import org.graalvm.compiler.core.common.type.AbstractPointerStamp; 28 import org.graalvm.compiler.core.common.type.ObjectStamp; 29 import org.graalvm.compiler.core.common.type.Stamp; 30 import org.graalvm.compiler.graph.NodeClass; 31 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 32 import org.graalvm.compiler.nodeinfo.NodeCycles; 33 import org.graalvm.compiler.nodeinfo.NodeInfo; 34 import org.graalvm.compiler.nodes.CompressionNode; 35 import org.graalvm.compiler.nodes.LogicConstantNode; 36 import org.graalvm.compiler.nodes.LogicNode; 37 import org.graalvm.compiler.nodes.NodeView; 38 import org.graalvm.compiler.nodes.UnaryOpLogicNode; 39 import org.graalvm.compiler.nodes.ValueNode; 40 import org.graalvm.compiler.nodes.spi.LIRLowerable; 41 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 42 import org.graalvm.compiler.nodes.spi.Virtualizable; 43 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 44 import org.graalvm.compiler.nodes.type.NarrowOopStamp; 45 import org.graalvm.compiler.nodes.type.StampTool; 46 47 import jdk.vm.ci.meta.JavaConstant; 48 import jdk.vm.ci.meta.TriState; 49 50 /** 51 * An IsNullNode will be true if the supplied value is null, and false if it is non-null. 52 */ 53 @NodeInfo(cycles = NodeCycles.CYCLES_2) 54 public final class IsNullNode extends UnaryOpLogicNode implements LIRLowerable, Virtualizable { 55 56 public static final NodeClass<IsNullNode> TYPE = NodeClass.create(IsNullNode.class); 57 58 /* 59 * When linear pointer compression is enabled, compressed and uncompressed nulls differ. 60 */ 61 private final JavaConstant nullConstant; 62 63 private IsNullNode(ValueNode object, JavaConstant nullConstant) { 64 super(TYPE, object); 65 this.nullConstant = nullConstant; 66 assert object != null; 67 } 68 69 public IsNullNode(ValueNode object) { 70 this(object, JavaConstant.NULL_POINTER); 71 assertNonNarrow(object); 72 } 73 74 public JavaConstant nullConstant() { 75 return nullConstant; 76 } 77 78 public static LogicNode create(ValueNode forValue) { 79 assertNonNarrow(forValue); 80 return canonicalized(null, forValue, JavaConstant.NULL_POINTER); 81 } 82 83 public static LogicNode create(ValueNode forValue, JavaConstant nullConstant) { 84 assert nullConstant.isNull() : "Null constant is not null: " + nullConstant; 85 return canonicalized(null, forValue, nullConstant); 86 } 87 88 private static void assertNonNarrow(ValueNode object) { 89 assert !(object.stamp(NodeView.DEFAULT) instanceof NarrowOopStamp) : "Value to compare against null is a NarrowOop" + object; 90 } 91 92 @Override 93 public void generate(NodeLIRBuilderTool gen) { 94 // Nothing to do. 95 } 96 97 @Override 98 public boolean verify() { 99 assertTrue(getValue() != null, "is null input must not be null"); 100 assertTrue(getValue().stamp(NodeView.DEFAULT) instanceof AbstractPointerStamp, "input must be a pointer not %s", getValue().stamp(NodeView.DEFAULT)); 101 return super.verify(); 102 } 103 104 @Override 105 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 106 return canonicalized(this, forValue, nullConstant); 107 } 108 109 private static LogicNode canonicalized(IsNullNode node, ValueNode forValue, JavaConstant forNullConstant) { 110 JavaConstant nullConstant = forNullConstant; 111 ValueNode value = forValue; 112 while (true) { 113 if (StampTool.isPointerAlwaysNull(value)) { 114 return LogicConstantNode.tautology(); 115 } else if (StampTool.isPointerNonNull(value)) { 116 return LogicConstantNode.contradiction(); 117 } 118 119 if (value instanceof ConvertNode) { 120 ConvertNode convertNode = (ConvertNode) value; 121 if (convertNode.mayNullCheckSkipConversion()) { 122 value = convertNode.getValue(); 123 continue; 124 } 125 /* 126 * CompressionNode.mayNullCheckSkipConversion returns false when linear pointer 127 * compression is enabled. 128 */ 129 if (value instanceof CompressionNode) { 130 CompressionNode compressionNode = (CompressionNode) value; 131 nullConstant = compressionNode.nullConstant(); 132 value = compressionNode.getValue(); 133 continue; 134 } 135 } 136 /* 137 * If we are at original node, just return it. Otherwise create a new node. 138 */ 139 return (node != null && value == forValue) ? node : new IsNullNode(value, nullConstant); 140 } 141 } 142 143 @Override 144 public void virtualize(VirtualizerTool tool) { 145 ValueNode alias = tool.getAlias(getValue()); 146 TriState fold = tryFold(alias.stamp(NodeView.DEFAULT)); 147 if (fold != TriState.UNKNOWN) { 148 tool.replaceWithValue(LogicConstantNode.forBoolean(fold.isTrue(), graph())); 149 } 150 } 151 152 @Override 153 public Stamp getSucceedingStampForValue(boolean negated) { 154 // Ignore any more precise input stamp since canonicalization will skip through PiNodes 155 AbstractPointerStamp pointerStamp = (AbstractPointerStamp) getValue().stamp(NodeView.DEFAULT).unrestricted(); 156 return negated ? pointerStamp.asNonNull() : pointerStamp.asAlwaysNull(); 157 } 158 159 @Override 160 public TriState tryFold(Stamp valueStamp) { 161 if (valueStamp instanceof ObjectStamp) { 162 ObjectStamp objectStamp = (ObjectStamp) valueStamp; 163 if (objectStamp.alwaysNull()) { 164 return TriState.TRUE; 165 } else if (objectStamp.nonNull()) { 166 return TriState.FALSE; 167 } 168 } 169 return TriState.UNKNOWN; 170 } 171 }