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.Or; 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 OrNode extends BinaryArithmeticNode<Or> implements BinaryCommutative<ValueNode>, NarrowableArithmeticNode { 46 47 public static final NodeClass<OrNode> TYPE = NodeClass.create(OrNode.class); 48 49 public OrNode(ValueNode x, ValueNode y) { 50 super(TYPE, ArithmeticOpTable::getOr, x, y); 51 } 52 53 public static ValueNode create(ValueNode x, ValueNode y) { 54 BinaryOp<Or> op = ArithmeticOpTable.forStamp(x.stamp()).getOr(); 55 Stamp stamp = op.foldStamp(x.stamp(), y.stamp()); 56 ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp); 57 if (tryConstantFold != null) { 58 return tryConstantFold; 59 } else { 60 return new OrNode(x, y).maybeCommuteInputs(); 61 } 62 } 63 64 @Override 65 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 66 ValueNode ret = super.canonical(tool, forX, forY); 67 if (ret != this) { 68 return ret; 69 } 70 71 if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) { 72 return forX; 73 } 74 if (forX.isConstant() && !forY.isConstant()) { 75 return new OrNode(forY, forX); 76 } 77 if (forY.isConstant()) { 78 Constant c = forY.asConstant(); 79 if (getOp(forX, forY).isNeutral(c)) { 80 return forX; 81 } 82 83 if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) { 84 long rawY = ((PrimitiveConstant) c).asLong(); 85 long mask = CodeUtil.mask(PrimitiveStamp.getBits(stamp())); 86 if ((rawY & mask) == mask) { 87 return ConstantNode.forIntegerStamp(stamp(), mask); 88 } 89 } 90 return reassociate(this, ValueNode.isConstantPredicate(), forX, forY); 91 } 92 return this; 93 } 94 95 @Override 96 public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) { 97 nodeValueMap.setResult(this, gen.emitOr(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()))); 98 } 99 }