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.UShr; 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.meta.JavaKind; 41 42 @NodeInfo(shortName = ">>>") 43 public final class UnsignedRightShiftNode extends ShiftNode<UShr> { 44 45 public static final NodeClass<UnsignedRightShiftNode> TYPE = NodeClass.create(UnsignedRightShiftNode.class); 46 47 public UnsignedRightShiftNode(ValueNode x, ValueNode y) { 48 super(TYPE, ArithmeticOpTable::getUShr, x, y); 49 } 50 51 public static ValueNode create(ValueNode x, ValueNode y, NodeView view) { 52 ArithmeticOpTable.ShiftOp<UShr> op = ArithmeticOpTable.forStamp(x.stamp(view)).getUShr(); 53 Stamp stamp = op.foldStamp(x.stamp(view), (IntegerStamp) y.stamp(view)); 54 ValueNode value = ShiftNode.canonical(op, stamp, x, y, view); 55 if (value != null) { 56 return value; 57 } 58 59 return canonical(null, op, stamp, x, y, view); 60 } 61 62 @Override 63 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 64 NodeView view = NodeView.from(tool); 65 ValueNode ret = super.canonical(tool, forX, forY); 66 if (ret != this) { 67 return ret; 68 } 69 70 return canonical(this, this.getArithmeticOp(), this.stamp(view), forX, forY, view); 71 } 72 73 @SuppressWarnings("unused") 74 private static ValueNode canonical(UnsignedRightShiftNode node, ArithmeticOpTable.ShiftOp<UShr> op, Stamp stamp, ValueNode forX, ValueNode forY, NodeView view) { 75 if (forY.isConstant()) { 76 int amount = forY.asJavaConstant().asInt(); 77 int originalAmout = amount; 78 int mask = op.getShiftAmountMask(stamp); 79 amount &= mask; 80 if (amount == 0) { 81 return forX; 82 } 83 if (forX instanceof ShiftNode) { 84 ShiftNode<?> other = (ShiftNode<?>) forX; 85 if (other.getY().isConstant()) { 86 int otherAmount = other.getY().asJavaConstant().asInt() & mask; 87 if (other instanceof UnsignedRightShiftNode) { 88 int total = amount + otherAmount; 89 if (total != (total & mask)) { 90 return ConstantNode.forIntegerKind(stamp.getStackKind(), 0); 91 } 92 return new UnsignedRightShiftNode(other.getX(), ConstantNode.forInt(total)); 93 } else if (other instanceof LeftShiftNode && otherAmount == amount) { 94 if (stamp.getStackKind() == JavaKind.Long) { 95 return new AndNode(other.getX(), ConstantNode.forLong(-1L >>> amount)); 96 } else { 97 assert stamp.getStackKind() == JavaKind.Int; 98 return new AndNode(other.getX(), ConstantNode.forInt(-1 >>> amount)); 99 } 100 } 101 } 102 } 103 if (originalAmout != amount) { 104 return new UnsignedRightShiftNode(forX, ConstantNode.forInt(amount)); 105 } 106 } 107 108 if (node != null) { 109 return node; 110 } 111 return new UnsignedRightShiftNode(forX, forY); 112 } 113 114 @Override 115 public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) { 116 nodeValueMap.setResult(this, gen.emitUShr(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()))); 117 } 118 119 @Override 120 public boolean isNarrowable(int resultBits) { 121 if (super.isNarrowable(resultBits)) { 122 /* 123 * For unsigned right shifts, the narrow can be done before the shift if the cut off 124 * bits are all zero. 125 */ 126 IntegerStamp inputStamp = (IntegerStamp) getX().stamp(NodeView.DEFAULT); 127 return (inputStamp.upMask() & ~(resultBits - 1)) == 0; 128 } else { 129 return false; 130 } 131 } 132 }