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.Xor;
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 XorNode extends BinaryArithmeticNode<Xor> implements BinaryCommutative<ValueNode>, NarrowableArithmeticNode {
042
043    public static final NodeClass<XorNode> TYPE = NodeClass.create(XorNode.class);
044
045    public XorNode(ValueNode x, ValueNode y) {
046        super(TYPE, ArithmeticOpTable::getXor, x, y);
047        assert x.stamp().isCompatible(y.stamp());
048    }
049
050    public static ValueNode create(ValueNode x, ValueNode y) {
051        BinaryOp<Xor> op = ArithmeticOpTable.forStamp(x.stamp()).getXor();
052        Stamp stamp = op.foldStamp(x.stamp(), y.stamp());
053        ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp);
054        if (tryConstantFold != null) {
055            return tryConstantFold;
056        } else {
057            return new XorNode(x, y).maybeCommuteInputs();
058        }
059    }
060
061    @Override
062    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
063        ValueNode ret = super.canonical(tool, forX, forY);
064        if (ret != this) {
065            return ret;
066        }
067
068        if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
069            return ConstantNode.forPrimitive(stamp(), getOp(forX, forY).getZero(forX.stamp()));
070        }
071        if (forX.isConstant() && !forY.isConstant()) {
072            return new XorNode(forY, forX);
073        }
074        if (forY.isConstant()) {
075            Constant c = forY.asConstant();
076            if (getOp(forX, forY).isNeutral(c)) {
077                return forX;
078            }
079
080            if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getKind().isNumericInteger()) {
081                long rawY = ((PrimitiveConstant) c).asLong();
082                long mask = CodeUtil.mask(PrimitiveStamp.getBits(stamp()));
083                if ((rawY & mask) == mask) {
084                    return new NotNode(forX);
085                }
086            }
087            return reassociate(this, ValueNode.isConstantPredicate(), forX, forY);
088        }
089        return this;
090    }
091
092    @Override
093    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
094        nodeValueMap.setResult(this, gen.emitXor(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
095    }
096}