001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.calc;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.Canonicalizable.BinaryCommutative;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033
034/**
035 * This node will perform a "test" operation on its arguments. Its result is equivalent to the
036 * expression "(x & y) == 0", meaning that it will return true if (and only if) no bit is set in
037 * both x and y.
038 */
039@NodeInfo
040public final class IntegerTestNode extends BinaryOpLogicNode implements BinaryCommutative<ValueNode> {
041    public static final NodeClass<IntegerTestNode> TYPE = NodeClass.create(IntegerTestNode.class);
042
043    public IntegerTestNode(ValueNode x, ValueNode y) {
044        super(TYPE, x, y);
045    }
046
047    @Override
048    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
049        if (forX.isConstant() && forY.isConstant()) {
050            return LogicConstantNode.forBoolean((forX.asJavaConstant().asLong() & forY.asJavaConstant().asLong()) == 0);
051        }
052        if (forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) {
053            IntegerStamp xStamp = (IntegerStamp) forX.stamp();
054            IntegerStamp yStamp = (IntegerStamp) forY.stamp();
055            if ((xStamp.upMask() & yStamp.upMask()) == 0) {
056                return LogicConstantNode.tautology();
057            } else if ((xStamp.downMask() & yStamp.downMask()) != 0) {
058                return LogicConstantNode.contradiction();
059            }
060        }
061        return this;
062    }
063
064    @Override
065    public Stamp getSucceedingStampForX(boolean negated) {
066        Stamp xStampGeneric = this.getX().stamp();
067        Stamp yStampGeneric = this.getY().stamp();
068        return getSucceedingStamp(negated, xStampGeneric, yStampGeneric);
069    }
070
071    private static Stamp getSucceedingStamp(boolean negated, Stamp xStampGeneric, Stamp otherStampGeneric) {
072        if (xStampGeneric instanceof IntegerStamp && otherStampGeneric instanceof IntegerStamp) {
073            IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
074            IntegerStamp otherStamp = (IntegerStamp) otherStampGeneric;
075            if (negated) {
076                if (Long.bitCount(otherStamp.upMask()) == 1) {
077                    long newDownMask = xStamp.downMask() | otherStamp.upMask();
078                    if (xStamp.downMask() != newDownMask) {
079                        return IntegerStamp.stampForMask(xStamp.getBits(), newDownMask, xStamp.upMask()).join(xStamp);
080                    }
081                }
082            } else {
083                long restrictedUpMask = ((~otherStamp.downMask()) & xStamp.upMask());
084                if (xStamp.upMask() != restrictedUpMask) {
085                    return IntegerStamp.stampForMask(xStamp.getBits(), xStamp.downMask(), restrictedUpMask).join(xStamp);
086                }
087            }
088        }
089        return null;
090    }
091
092    @Override
093    public Stamp getSucceedingStampForY(boolean negated) {
094        Stamp xStampGeneric = this.getX().stamp();
095        Stamp yStampGeneric = this.getY().stamp();
096        return getSucceedingStamp(negated, yStampGeneric, xStampGeneric);
097    }
098
099    @Override
100    public TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
101        if (xStampGeneric instanceof IntegerStamp && yStampGeneric instanceof IntegerStamp) {
102            IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
103            IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
104            if ((xStamp.upMask() & yStamp.upMask()) == 0) {
105                return TriState.TRUE;
106            } else if ((xStamp.downMask() & yStamp.downMask()) != 0) {
107                return TriState.FALSE;
108            }
109        }
110        return TriState.UNKNOWN;
111    }
112}