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  */
  23 package org.graalvm.compiler.nodes.calc;
  24 
  25 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
  26 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp;
  27 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.Xor;
  28 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.graph.spi.Canonicalizable.BinaryCommutative;
  32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  33 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodes.ConstantNode;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  38 import org.graalvm.compiler.nodes.util.GraphUtil;
  39 
  40 import jdk.vm.ci.code.CodeUtil;
  41 import jdk.vm.ci.meta.Constant;
  42 import jdk.vm.ci.meta.PrimitiveConstant;
  43 
  44 @NodeInfo(shortName = "^")
  45 public final class XorNode extends BinaryArithmeticNode<Xor> implements BinaryCommutative<ValueNode>, NarrowableArithmeticNode {
  46 
  47     public static final NodeClass<XorNode> TYPE = NodeClass.create(XorNode.class);
  48 
  49     public XorNode(ValueNode x, ValueNode y) {
  50         super(TYPE, ArithmeticOpTable::getXor, x, y);
  51         assert x.stamp().isCompatible(y.stamp());
  52     }
  53 
  54     public static ValueNode create(ValueNode x, ValueNode y) {
  55         BinaryOp<Xor> op = ArithmeticOpTable.forStamp(x.stamp()).getXor();
  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 XorNode(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 ConstantNode.forPrimitive(stamp(), getOp(forX, forY).getZero(forX.stamp()));
  74         }
  75         if (forX.isConstant() && !forY.isConstant()) {
  76             return new XorNode(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) == mask) {
  88                     return new NotNode(forX);
  89                 }
  90             }
  91             return reassociate(this, ValueNode.isConstantPredicate(), forX, forY);
  92         }
  93         return this;
  94     }
  95 
  96     @Override
  97     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
  98         nodeValueMap.setResult(this, gen.emitXor(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
  99     }
 100 }