1 /* 2 * Copyright (c) 2009, 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 static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1; 26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1; 27 28 import java.io.Serializable; 29 import java.util.function.Function; 30 31 import org.graalvm.compiler.core.common.type.ArithmeticOpTable; 32 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.ShiftOp; 33 import org.graalvm.compiler.core.common.type.IntegerStamp; 34 import org.graalvm.compiler.core.common.type.Stamp; 35 import org.graalvm.compiler.graph.NodeClass; 36 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 37 import org.graalvm.compiler.nodeinfo.NodeInfo; 38 import org.graalvm.compiler.nodes.ArithmeticOperation; 39 import org.graalvm.compiler.nodes.ConstantNode; 40 import org.graalvm.compiler.nodes.ValueNode; 41 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable; 42 43 import jdk.vm.ci.code.CodeUtil; 44 import jdk.vm.ci.meta.JavaConstant; 45 import jdk.vm.ci.meta.JavaKind; 46 47 /** 48 * The {@code ShiftOp} class represents shift operations. 49 */ 50 @NodeInfo(cycles = CYCLES_1, size = SIZE_1) 51 public abstract class ShiftNode<OP> extends BinaryNode implements ArithmeticOperation, ArithmeticLIRLowerable, NarrowableArithmeticNode { 52 53 @SuppressWarnings("rawtypes") public static final NodeClass<ShiftNode> TYPE = NodeClass.create(ShiftNode.class); 54 55 protected interface SerializableShiftFunction<T> extends Function<ArithmeticOpTable, ShiftOp<T>>, Serializable { 56 } 57 58 protected final SerializableShiftFunction<OP> getOp; 59 60 /** 61 * Creates a new shift operation. 62 * 63 * @param x the first input value 64 * @param s the second input value 65 */ 66 protected ShiftNode(NodeClass<? extends ShiftNode<OP>> c, SerializableShiftFunction<OP> getOp, ValueNode x, ValueNode s) { 67 super(c, getOp.apply(ArithmeticOpTable.forStamp(x.stamp())).foldStamp(x.stamp(), (IntegerStamp) s.stamp()), x, s); 68 assert ((IntegerStamp) s.stamp()).getBits() == 32; 69 this.getOp = getOp; 70 } 71 72 protected final ShiftOp<OP> getOp(ValueNode forValue) { 73 return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp())); 74 } 75 76 @Override 77 public final ShiftOp<OP> getArithmeticOp() { 78 return getOp(getX()); 79 } 80 81 @Override 82 public Stamp foldStamp(Stamp stampX, Stamp stampY) { 83 return getArithmeticOp().foldStamp(stampX, (IntegerStamp) stampY); 84 } 85 86 @Override 87 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 88 if (forX.isConstant() && forY.isConstant()) { 89 JavaConstant amount = forY.asJavaConstant(); 90 assert amount.getJavaKind() == JavaKind.Int; 91 return ConstantNode.forPrimitive(stamp(), getOp(forX).foldConstant(forX.asConstant(), amount.asInt())); 92 } 93 return this; 94 } 95 96 public int getShiftAmountMask() { 97 return getArithmeticOp().getShiftAmountMask(stamp()); 98 } 99 100 @Override 101 public boolean isNarrowable(int resultBits) { 102 assert CodeUtil.isPowerOf2(resultBits); 103 int narrowMask = resultBits - 1; 104 int wideMask = getShiftAmountMask(); 105 assert (wideMask & narrowMask) == narrowMask : String.format("wideMask %x should be wider than narrowMask %x", wideMask, narrowMask); 106 107 /* 108 * Shifts are special because narrowing them also changes the implicit mask of the shift 109 * amount. We can narrow only if (y & wideMask) == (y & narrowMask) for all possible values 110 * of y. 111 */ 112 IntegerStamp yStamp = (IntegerStamp) getY().stamp(); 113 return (yStamp.upMask() & (wideMask & ~narrowMask)) == 0; 114 } 115 }