1 /*
   2  * Copyright (c) 2009, 2015, 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.calc;
  24 
  25 import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
  26 import org.graalvm.compiler.core.common.type.ObjectStamp;
  27 import org.graalvm.compiler.core.common.type.Stamp;
  28 import org.graalvm.compiler.graph.NodeClass;
  29 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  30 import org.graalvm.compiler.nodeinfo.NodeCycles;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.LogicConstantNode;
  33 import org.graalvm.compiler.nodes.LogicNode;
  34 import org.graalvm.compiler.nodes.PiNode;
  35 import org.graalvm.compiler.nodes.UnaryOpLogicNode;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  38 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  39 import org.graalvm.compiler.nodes.spi.Virtualizable;
  40 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  41 import org.graalvm.compiler.nodes.type.StampTool;
  42 import org.graalvm.compiler.nodes.util.GraphUtil;
  43 
  44 import jdk.vm.ci.meta.TriState;
  45 
  46 /**
  47  * An IsNullNode will be true if the supplied value is null, and false if it is non-null.
  48  */
  49 @NodeInfo(cycles = NodeCycles.CYCLES_2)
  50 public final class IsNullNode extends UnaryOpLogicNode implements LIRLowerable, Virtualizable {
  51 
  52     public static final NodeClass<IsNullNode> TYPE = NodeClass.create(IsNullNode.class);
  53 
  54     public IsNullNode(ValueNode object) {
  55         super(TYPE, object);
  56         assert object != null;
  57     }
  58 
  59     public static LogicNode create(ValueNode forValue) {
  60         LogicNode result = tryCanonicalize(forValue);
  61         return result == null ? new IsNullNode(GraphUtil.skipPi(forValue)) : result;
  62     }
  63 
  64     public static LogicNode tryCanonicalize(ValueNode forValue) {
  65         if (StampTool.isPointerAlwaysNull(forValue)) {
  66             return LogicConstantNode.tautology();
  67         } else if (StampTool.isPointerNonNull(forValue)) {
  68             return LogicConstantNode.contradiction();
  69         }
  70         return null;
  71     }
  72 
  73     @Override
  74     public void generate(NodeLIRBuilderTool gen) {
  75         // Nothing to do.
  76     }
  77 
  78     @Override
  79     public boolean verify() {
  80         assertTrue(getValue() != null, "is null input must not be null");
  81         assertTrue(getValue().stamp() instanceof AbstractPointerStamp, "input must be a pointer not %s", getValue().stamp());
  82         return super.verify();
  83     }
  84 
  85     @Override
  86     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  87 
  88         LogicNode result = tryCanonicalize(forValue);
  89         if (result != null) {
  90             return result;
  91         }
  92 
  93         if (forValue instanceof PiNode) {
  94             return IsNullNode.create(GraphUtil.skipPi(forValue));
  95         }
  96 
  97         if (forValue instanceof ConvertNode) {
  98             ConvertNode convertNode = (ConvertNode) forValue;
  99             if (convertNode.mayNullCheckSkipConversion()) {
 100                 return IsNullNode.create(convertNode.getValue());
 101             }
 102         }
 103 
 104         return this;
 105     }
 106 
 107     @Override
 108     public void virtualize(VirtualizerTool tool) {
 109         ValueNode alias = tool.getAlias(getValue());
 110         TriState fold = tryFold(alias.stamp());
 111         if (fold != TriState.UNKNOWN) {
 112             tool.replaceWithValue(LogicConstantNode.forBoolean(fold.isTrue(), graph()));
 113         }
 114     }
 115 
 116     @Override
 117     public Stamp getSucceedingStampForValue(boolean negated) {
 118         AbstractPointerStamp pointerStamp = (AbstractPointerStamp) getValue().stamp();
 119         return negated ? pointerStamp.asNonNull() : pointerStamp.asAlwaysNull();
 120     }
 121 
 122     @Override
 123     public TriState tryFold(Stamp valueStamp) {
 124         if (valueStamp instanceof ObjectStamp) {
 125             ObjectStamp objectStamp = (ObjectStamp) valueStamp;
 126             if (objectStamp.alwaysNull()) {
 127                 return TriState.TRUE;
 128             } else if (objectStamp.nonNull()) {
 129                 return TriState.FALSE;
 130             }
 131         }
 132         return TriState.UNKNOWN;
 133     }
 134 }