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