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.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp;
030import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp.And;
031import com.oracle.graal.graph.*;
032import com.oracle.graal.graph.spi.Canonicalizable.BinaryCommutative;
033import com.oracle.graal.graph.spi.*;
034import com.oracle.graal.lir.gen.*;
035import com.oracle.graal.nodeinfo.*;
036import com.oracle.graal.nodes.*;
037import com.oracle.graal.nodes.spi.*;
038import com.oracle.graal.nodes.util.*;
039
040@NodeInfo(shortName = "&")
041public final class AndNode extends BinaryArithmeticNode<And> implements NarrowableArithmeticNode, BinaryCommutative<ValueNode> {
042
043    public static final NodeClass<AndNode> TYPE = NodeClass.create(AndNode.class);
044
045    public AndNode(ValueNode x, ValueNode y) {
046        super(TYPE, ArithmeticOpTable::getAnd, x, y);
047    }
048
049    public static ValueNode create(ValueNode x, ValueNode y) {
050        BinaryOp<And> op = ArithmeticOpTable.forStamp(x.stamp()).getAnd();
051        Stamp stamp = op.foldStamp(x.stamp(), y.stamp());
052        ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp);
053        if (tryConstantFold != null) {
054            return tryConstantFold;
055        } else {
056            return new AndNode(x, y).maybeCommuteInputs();
057        }
058    }
059
060    @Override
061    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
062        ValueNode ret = super.canonical(tool, forX, forY);
063        if (ret != this) {
064            return ret;
065        }
066
067        if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
068            return forX;
069        }
070        if (forX.isConstant() && !forY.isConstant()) {
071            return new AndNode(forY, forX);
072        }
073        if (forY.isConstant()) {
074            Constant c = forY.asConstant();
075            if (getOp(forX, forY).isNeutral(c)) {
076                return forX;
077            }
078
079            if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getKind().isNumericInteger()) {
080                long rawY = ((PrimitiveConstant) c).asLong();
081                long mask = CodeUtil.mask(PrimitiveStamp.getBits(stamp()));
082                if ((rawY & mask) == 0) {
083                    return ConstantNode.forIntegerStamp(stamp(), 0);
084                }
085                if (forX instanceof SignExtendNode) {
086                    SignExtendNode ext = (SignExtendNode) forX;
087                    if (rawY == ((1L << ext.getInputBits()) - 1)) {
088                        return new ZeroExtendNode(ext.getValue(), ext.getResultBits());
089                    }
090                }
091                IntegerStamp xStamp = (IntegerStamp) forX.stamp();
092                if (((xStamp.upMask() | xStamp.downMask()) & ~rawY) == 0) {
093                    // No bits are set which are outside the mask, so the mask will have no effect.
094                    return forX;
095                }
096            }
097
098            return reassociate(this, ValueNode.isConstantPredicate(), forX, forY);
099        }
100        return this;
101    }
102
103    @Override
104    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
105        nodeValueMap.setResult(this, gen.emitAnd(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
106    }
107}