1 /*
   2  * Copyright (c) 2011, 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 static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  27 
  28 import org.graalvm.compiler.core.common.type.IntegerStamp;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.graph.spi.Canonicalizable.BinaryCommutative;
  32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 import org.graalvm.compiler.nodes.BinaryOpLogicNode;
  35 import org.graalvm.compiler.nodes.LogicConstantNode;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 
  38 import jdk.vm.ci.meta.TriState;
  39 
  40 /**
  41  * This node will perform a "test" operation on its arguments. Its result is equivalent to the
  42  * expression "(x & y) == 0", meaning that it will return true if (and only if) no bit is set in
  43  * both x and y.
  44  */
  45 @NodeInfo(cycles = CYCLES_2, size = SIZE_2)
  46 public final class IntegerTestNode extends BinaryOpLogicNode implements BinaryCommutative<ValueNode> {
  47     public static final NodeClass<IntegerTestNode> TYPE = NodeClass.create(IntegerTestNode.class);
  48 
  49     public IntegerTestNode(ValueNode x, ValueNode y) {
  50         super(TYPE, x, y);
  51     }
  52 
  53     @Override
  54     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  55         if (forX.isConstant() && forY.isConstant()) {
  56             return LogicConstantNode.forBoolean((forX.asJavaConstant().asLong() & forY.asJavaConstant().asLong()) == 0);
  57         }
  58         if (forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) {
  59             IntegerStamp xStamp = (IntegerStamp) forX.stamp();
  60             IntegerStamp yStamp = (IntegerStamp) forY.stamp();
  61             if ((xStamp.upMask() & yStamp.upMask()) == 0) {
  62                 return LogicConstantNode.tautology();
  63             } else if ((xStamp.downMask() & yStamp.downMask()) != 0) {
  64                 return LogicConstantNode.contradiction();
  65             }
  66         }
  67         return this;
  68     }
  69 
  70     @Override
  71     public Stamp getSucceedingStampForX(boolean negated) {
  72         Stamp xStampGeneric = this.getX().stamp();
  73         Stamp yStampGeneric = this.getY().stamp();
  74         return getSucceedingStamp(negated, xStampGeneric, yStampGeneric);
  75     }
  76 
  77     private static Stamp getSucceedingStamp(boolean negated, Stamp xStampGeneric, Stamp otherStampGeneric) {
  78         if (xStampGeneric instanceof IntegerStamp && otherStampGeneric instanceof IntegerStamp) {
  79             IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
  80             IntegerStamp otherStamp = (IntegerStamp) otherStampGeneric;
  81             if (negated) {
  82                 if (Long.bitCount(otherStamp.upMask()) == 1) {
  83                     long newDownMask = xStamp.downMask() | otherStamp.upMask();
  84                     if (xStamp.downMask() != newDownMask) {
  85                         return IntegerStamp.stampForMask(xStamp.getBits(), newDownMask, xStamp.upMask()).join(xStamp);
  86                     }
  87                 }
  88             } else {
  89                 long restrictedUpMask = ((~otherStamp.downMask()) & xStamp.upMask());
  90                 if (xStamp.upMask() != restrictedUpMask) {
  91                     return IntegerStamp.stampForMask(xStamp.getBits(), xStamp.downMask(), restrictedUpMask).join(xStamp);
  92                 }
  93             }
  94         }
  95         return null;
  96     }
  97 
  98     @Override
  99     public Stamp getSucceedingStampForY(boolean negated) {
 100         Stamp xStampGeneric = this.getX().stamp();
 101         Stamp yStampGeneric = this.getY().stamp();
 102         return getSucceedingStamp(negated, yStampGeneric, xStampGeneric);
 103     }
 104 
 105     @Override
 106     public TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
 107         if (xStampGeneric instanceof IntegerStamp && yStampGeneric instanceof IntegerStamp) {
 108             IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
 109             IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
 110             if ((xStamp.upMask() & yStamp.upMask()) == 0) {
 111                 return TriState.TRUE;
 112             } else if ((xStamp.downMask() & yStamp.downMask()) != 0) {
 113                 return TriState.FALSE;
 114             }
 115         }
 116         return TriState.UNKNOWN;
 117     }
 118 }