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 */
23package org.graalvm.compiler.nodes.calc;
24
25import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
26import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp;
27import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.And;
28import org.graalvm.compiler.core.common.type.IntegerStamp;
29import org.graalvm.compiler.core.common.type.PrimitiveStamp;
30import org.graalvm.compiler.core.common.type.Stamp;
31import org.graalvm.compiler.graph.NodeClass;
32import org.graalvm.compiler.graph.spi.Canonicalizable.BinaryCommutative;
33import org.graalvm.compiler.graph.spi.CanonicalizerTool;
34import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
35import org.graalvm.compiler.nodeinfo.NodeInfo;
36import org.graalvm.compiler.nodes.ConstantNode;
37import org.graalvm.compiler.nodes.ValueNode;
38import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
39import org.graalvm.compiler.nodes.util.GraphUtil;
40
41import jdk.vm.ci.code.CodeUtil;
42import jdk.vm.ci.meta.Constant;
43import jdk.vm.ci.meta.PrimitiveConstant;
44
45@NodeInfo(shortName = "&")
46public final class AndNode extends BinaryArithmeticNode<And> implements NarrowableArithmeticNode, BinaryCommutative<ValueNode> {
47
48    public static final NodeClass<AndNode> TYPE = NodeClass.create(AndNode.class);
49
50    public AndNode(ValueNode x, ValueNode y) {
51        super(TYPE, ArithmeticOpTable::getAnd, x, y);
52    }
53
54    public static ValueNode create(ValueNode x, ValueNode y) {
55        BinaryOp<And> op = ArithmeticOpTable.forStamp(x.stamp()).getAnd();
56        Stamp stamp = op.foldStamp(x.stamp(), y.stamp());
57        ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp);
58        if (tryConstantFold != null) {
59            return tryConstantFold;
60        } else {
61            return new AndNode(x, y).maybeCommuteInputs();
62        }
63    }
64
65    @Override
66    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
67        ValueNode ret = super.canonical(tool, forX, forY);
68        if (ret != this) {
69            return ret;
70        }
71
72        if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
73            return forX;
74        }
75        if (forX.isConstant() && !forY.isConstant()) {
76            return new AndNode(forY, forX);
77        }
78        if (forY.isConstant()) {
79            Constant c = forY.asConstant();
80            if (getOp(forX, forY).isNeutral(c)) {
81                return forX;
82            }
83
84            if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) {
85                long rawY = ((PrimitiveConstant) c).asLong();
86                long mask = CodeUtil.mask(PrimitiveStamp.getBits(stamp()));
87                if ((rawY & mask) == 0) {
88                    return ConstantNode.forIntegerStamp(stamp(), 0);
89                }
90                if (forX instanceof SignExtendNode) {
91                    SignExtendNode ext = (SignExtendNode) forX;
92                    if (rawY == ((1L << ext.getInputBits()) - 1)) {
93                        return new ZeroExtendNode(ext.getValue(), ext.getResultBits());
94                    }
95                }
96                IntegerStamp xStamp = (IntegerStamp) forX.stamp();
97                if (((xStamp.upMask() | xStamp.downMask()) & ~rawY) == 0) {
98                    // No bits are set which are outside the mask, so the mask will have no effect.
99                    return forX;
100                }
101            }
102
103            return reassociate(this, ValueNode.isConstantPredicate(), forX, forY);
104        }
105        return this;
106    }
107
108    @Override
109    public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
110        nodeValueMap.setResult(this, gen.emitAnd(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
111    }
112}
113