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.Or;
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 OrNode extends BinaryArithmeticNode<Or> implements BinaryCommutative<ValueNode>, NarrowableArithmeticNode {
042
043    public static final NodeClass<OrNode> TYPE = NodeClass.create(OrNode.class);
044
045    public OrNode(ValueNode x, ValueNode y) {
046        super(TYPE, ArithmeticOpTable::getOr, x, y);
047    }
048
049    public static ValueNode create(ValueNode x, ValueNode y) {
050        BinaryOp<Or> op = ArithmeticOpTable.forStamp(x.stamp()).getOr();
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 OrNode(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 OrNode(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) == mask) {
083                    return ConstantNode.forIntegerStamp(stamp(), mask);
084                }
085            }
086            return reassociate(this, ValueNode.isConstantPredicate(), forX, forY);
087        }
088        return this;
089    }
090
091    @Override
092    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
093        nodeValueMap.setResult(this, gen.emitOr(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
094    }
095}