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, Stamp xStamp, Stamp yStamp) {
  72         return getSucceedingStamp(negated, xStamp, yStamp);
  73     }
  74 
  75     private static Stamp getSucceedingStamp(boolean negated, Stamp xStampGeneric, Stamp otherStampGeneric) {
  76         if (xStampGeneric instanceof IntegerStamp && otherStampGeneric instanceof IntegerStamp) {
  77             IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
  78             IntegerStamp otherStamp = (IntegerStamp) otherStampGeneric;
  79             if (negated) {
  80                 if (Long.bitCount(otherStamp.upMask()) == 1) {
  81                     long newDownMask = xStamp.downMask() | otherStamp.upMask();
  82                     if (xStamp.downMask() != newDownMask) {
  83                         return IntegerStamp.stampForMask(xStamp.getBits(), newDownMask, xStamp.upMask()).join(xStamp);
  84                     }
  85                 }
  86             } else {
  87                 long restrictedUpMask = ((~otherStamp.downMask()) & xStamp.upMask());
  88                 if (xStamp.upMask() != restrictedUpMask) {
  89                     return IntegerStamp.stampForMask(xStamp.getBits(), xStamp.downMask(), restrictedUpMask).join(xStamp);
  90                 }
  91             }
  92         }
  93         return null;
  94     }
  95 
  96     @Override
  97     public Stamp getSucceedingStampForY(boolean negated, Stamp xStamp, Stamp yStamp) {
  98         return getSucceedingStamp(negated, yStamp, xStamp);
  99     }
 100 
 101     @Override
 102     public TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
 103         if (xStampGeneric instanceof IntegerStamp && yStampGeneric instanceof IntegerStamp) {
 104             IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
 105             IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
 106             if ((xStamp.upMask() & yStamp.upMask()) == 0) {
 107                 return TriState.TRUE;
 108             } else if ((xStamp.downMask() & yStamp.downMask()) != 0) {
 109                 return TriState.FALSE;
 110             }
 111         }
 112         return TriState.UNKNOWN;
 113     }
 114 }