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