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         return canonicalized(null, forValue);
  61     }
  62 
  63     public static LogicNode tryCanonicalize(ValueNode forValue) {
  64         if (StampTool.isPointerAlwaysNull(forValue)) {
  65             return LogicConstantNode.tautology();
  66         } else if (StampTool.isPointerNonNull(forValue)) {
  67             return LogicConstantNode.contradiction();
  68         }
  69         return null;
  70     }
  71 
  72     @Override
  73     public void generate(NodeLIRBuilderTool gen) {
  74         // Nothing to do.
  75     }
  76 
  77     @Override
  78     public boolean verify() {
  79         assertTrue(getValue() != null, "is null input must not be null");
  80         assertTrue(getValue().stamp() instanceof AbstractPointerStamp, "input must be a pointer not %s", getValue().stamp());
  81         return super.verify();
  82     }
  83 
  84     @Override
  85     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  86         return canonicalized(this, forValue);
  87     }
  88 
  89     private static LogicNode canonicalized(IsNullNode isNullNode, ValueNode forValue) {
  90         IsNullNode self = isNullNode;
  91         LogicNode result = tryCanonicalize(forValue);
  92         if (result != null) {
  93             return result;
  94         }
  95 
  96         if (forValue instanceof PiNode) {
  97             return IsNullNode.create(GraphUtil.skipPi(forValue));
  98         }
  99 
 100         if (forValue instanceof ConvertNode) {
 101             ConvertNode convertNode = (ConvertNode) forValue;
 102             if (convertNode.mayNullCheckSkipConversion()) {
 103                 return IsNullNode.create(convertNode.getValue());
 104             }
 105         }
 106 
 107         if (self == null) {
 108             self = new IsNullNode(GraphUtil.skipPi(forValue));
 109         }
 110         return self;
 111     }
 112 
 113     @Override
 114     public void virtualize(VirtualizerTool tool) {
 115         ValueNode alias = tool.getAlias(getValue());
 116         TriState fold = tryFold(alias.stamp());
 117         if (fold != TriState.UNKNOWN) {
 118             tool.replaceWithValue(LogicConstantNode.forBoolean(fold.isTrue(), graph()));
 119         }
 120     }
 121 
 122     @Override
 123     public Stamp getSucceedingStampForValue(boolean negated) {
 124         AbstractPointerStamp pointerStamp = (AbstractPointerStamp) getValue().stamp();
 125         return negated ? pointerStamp.asNonNull() : pointerStamp.asAlwaysNull();
 126     }
 127 
 128     @Override
 129     public TriState tryFold(Stamp valueStamp) {
 130         if (valueStamp instanceof ObjectStamp) {
 131             ObjectStamp objectStamp = (ObjectStamp) valueStamp;
 132             if (objectStamp.alwaysNull()) {
 133                 return TriState.TRUE;
 134             } else if (objectStamp.nonNull()) {
 135                 return TriState.FALSE;
 136             }
 137         }
 138         return TriState.UNKNOWN;
 139     }
 140 }