1 /* 2 * Copyright (c) 2011, 2018, 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 24 25 package org.graalvm.compiler.nodes.calc; 26 27 import org.graalvm.compiler.core.common.type.ArithmeticOpTable; 28 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.ShiftOp.Shr; 29 import org.graalvm.compiler.core.common.type.IntegerStamp; 30 import org.graalvm.compiler.core.common.type.Stamp; 31 import org.graalvm.compiler.graph.NodeClass; 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.NodeView; 37 import org.graalvm.compiler.nodes.ValueNode; 38 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 39 40 import jdk.vm.ci.code.CodeUtil; 41 42 @NodeInfo(shortName = ">>") 43 public final class RightShiftNode extends ShiftNode<Shr> { 44 45 public static final NodeClass<RightShiftNode> TYPE = NodeClass.create(RightShiftNode.class); 46 47 public RightShiftNode(ValueNode x, ValueNode y) { 48 super(TYPE, ArithmeticOpTable::getShr, x, y); 49 } 50 51 public static ValueNode create(ValueNode x, int y, NodeView view) { 52 if (y == 0) { 53 return x; 54 } 55 return create(x, ConstantNode.forInt(y), view); 56 } 57 58 public static ValueNode create(ValueNode x, ValueNode y, NodeView view) { 59 ArithmeticOpTable.ShiftOp<Shr> op = ArithmeticOpTable.forStamp(x.stamp(view)).getShr(); 60 Stamp stamp = op.foldStamp(x.stamp(view), (IntegerStamp) y.stamp(view)); 61 ValueNode value = ShiftNode.canonical(op, stamp, x, y, view); 62 if (value != null) { 63 return value; 64 } 65 66 return canonical(null, op, stamp, x, y, view); 67 } 68 69 @Override 70 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 71 NodeView view = NodeView.from(tool); 72 ValueNode ret = super.canonical(tool, forX, forY); 73 if (ret != this) { 74 return ret; 75 } 76 77 return canonical(this, getArithmeticOp(), stamp(view), forX, forY, view); 78 } 79 80 private static ValueNode canonical(RightShiftNode rightShiftNode, ArithmeticOpTable.ShiftOp<Shr> op, Stamp stamp, ValueNode forX, ValueNode forY, NodeView view) { 81 RightShiftNode self = rightShiftNode; 82 if (forX.stamp(view) instanceof IntegerStamp && ((IntegerStamp) forX.stamp(view)).isPositive()) { 83 return new UnsignedRightShiftNode(forX, forY); 84 } 85 86 Stamp xStampGeneric = forX.stamp(view); 87 if (xStampGeneric instanceof IntegerStamp) { 88 IntegerStamp xStamp = (IntegerStamp) xStampGeneric; 89 if (xStamp.lowerBound() >= -1 && xStamp.upperBound() <= 0) { 90 // Right shift by any amount does not change any bit. 91 return forX; 92 } 93 } 94 95 if (forY.isConstant()) { 96 int amount = forY.asJavaConstant().asInt(); 97 int originalAmout = amount; 98 int mask = op.getShiftAmountMask(stamp); 99 amount &= mask; 100 if (amount == 0) { 101 return forX; 102 } 103 104 if (xStampGeneric instanceof IntegerStamp) { 105 IntegerStamp xStamp = (IntegerStamp) xStampGeneric; 106 107 if (xStamp.lowerBound() >> amount == xStamp.upperBound() >> amount) { 108 // Right shift turns the result of the expression into a constant. 109 return ConstantNode.forIntegerKind(stamp.getStackKind(), xStamp.lowerBound() >> amount); 110 } 111 } 112 113 if (forX instanceof ShiftNode) { 114 ShiftNode<?> other = (ShiftNode<?>) forX; 115 if (other.getY().isConstant()) { 116 int otherAmount = other.getY().asJavaConstant().asInt() & mask; 117 if (other instanceof RightShiftNode) { 118 int total = amount + otherAmount; 119 if (total != (total & mask)) { 120 assert other.getX().stamp(view) instanceof IntegerStamp; 121 IntegerStamp istamp = (IntegerStamp) other.getX().stamp(view); 122 123 if (istamp.isPositive()) { 124 return ConstantNode.forIntegerKind(stamp.getStackKind(), 0); 125 } 126 if (istamp.isStrictlyNegative()) { 127 return ConstantNode.forIntegerKind(stamp.getStackKind(), -1L); 128 } 129 130 /* 131 * if we cannot replace both shifts with a constant, replace them by a 132 * full shift for this kind 133 */ 134 assert total >= mask; 135 return new RightShiftNode(other.getX(), ConstantNode.forInt(mask)); 136 } 137 return new RightShiftNode(other.getX(), ConstantNode.forInt(total)); 138 } 139 } 140 } 141 if (originalAmout != amount) { 142 return new RightShiftNode(forX, ConstantNode.forInt(amount)); 143 } 144 } 145 if (self == null) { 146 self = new RightShiftNode(forX, forY); 147 } 148 return self; 149 } 150 151 @Override 152 public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) { 153 nodeValueMap.setResult(this, gen.emitShr(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()))); 154 } 155 156 @Override 157 public boolean isNarrowable(int resultBits) { 158 if (super.isNarrowable(resultBits)) { 159 /* 160 * For signed right shifts, the narrow can be done before the shift if the cut off bits 161 * are all equal to the sign bit of the input. That's equivalent to the condition that 162 * the input is in the signed range of the narrow type. 163 */ 164 IntegerStamp inputStamp = (IntegerStamp) getX().stamp(NodeView.DEFAULT); 165 return CodeUtil.minValue(resultBits) <= inputStamp.lowerBound() && inputStamp.upperBound() <= CodeUtil.maxValue(resultBits); 166 } else { 167 return false; 168 } 169 } 170 }