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