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