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 */ 23package org.graalvm.compiler.nodes.calc; 24 25import org.graalvm.compiler.core.common.type.ArithmeticOpTable; 26import org.graalvm.compiler.core.common.type.ArithmeticOpTable.ShiftOp.Shl; 27import org.graalvm.compiler.core.common.type.IntegerStamp; 28import org.graalvm.compiler.core.common.type.Stamp; 29import org.graalvm.compiler.graph.NodeClass; 30import org.graalvm.compiler.graph.spi.CanonicalizerTool; 31import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool; 32import org.graalvm.compiler.nodeinfo.NodeInfo; 33import org.graalvm.compiler.nodes.ConstantNode; 34import org.graalvm.compiler.nodes.ValueNode; 35import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 36 37import jdk.vm.ci.meta.JavaKind; 38 39@NodeInfo(shortName = "<<") 40public final class LeftShiftNode extends ShiftNode<Shl> { 41 42 public static final NodeClass<LeftShiftNode> TYPE = NodeClass.create(LeftShiftNode.class); 43 44 public LeftShiftNode(ValueNode x, ValueNode y) { 45 super(TYPE, ArithmeticOpTable::getShl, x, y); 46 } 47 48 public static ValueNode create(ValueNode x, ValueNode y) { 49 ArithmeticOpTable.ShiftOp<Shl> op = ArithmeticOpTable.forStamp(x.stamp()).getShl(); 50 Stamp stamp = op.foldStamp(x.stamp(), (IntegerStamp) y.stamp()); 51 ValueNode value = ShiftNode.canonical(op, stamp, x, y); 52 if (value != null) { 53 return value; 54 } 55 56 return canonical(null, op, stamp, x, y); 57 } 58 59 @Override 60 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 61 ValueNode ret = super.canonical(tool, forX, forY); 62 if (ret != this) { 63 return ret; 64 } 65 66 return canonical(this, getArithmeticOp(), stamp(), forX, forY); 67 } 68 69 private static ValueNode canonical(LeftShiftNode leftShiftNode, ArithmeticOpTable.ShiftOp<Shl> op, Stamp stamp, ValueNode forX, ValueNode forY) { 70 LeftShiftNode self = leftShiftNode; 71 if (forY.isConstant()) { 72 int amount = forY.asJavaConstant().asInt(); 73 int originalAmount = amount; 74 int mask = op.getShiftAmountMask(stamp); 75 amount &= mask; 76 if (amount == 0) { 77 return forX; 78 } 79 if (forX instanceof ShiftNode) { 80 ShiftNode<?> other = (ShiftNode<?>) forX; 81 if (other.getY().isConstant()) { 82 int otherAmount = other.getY().asJavaConstant().asInt() & mask; 83 if (other instanceof LeftShiftNode) { 84 int total = amount + otherAmount; 85 if (total != (total & mask)) { 86 return ConstantNode.forIntegerKind(stamp.getStackKind(), 0); 87 } 88 return new LeftShiftNode(other.getX(), ConstantNode.forInt(total)); 89 } else if ((other instanceof RightShiftNode || other instanceof UnsignedRightShiftNode) && otherAmount == amount) { 90 if (stamp.getStackKind() == JavaKind.Long) { 91 return new AndNode(other.getX(), ConstantNode.forLong(-1L << amount)); 92 } else { 93 assert stamp.getStackKind() == JavaKind.Int; 94 return new AndNode(other.getX(), ConstantNode.forInt(-1 << amount)); 95 } 96 } 97 } 98 } 99 if (originalAmount != amount) { 100 return new LeftShiftNode(forX, ConstantNode.forInt(amount)); 101 } 102 } 103 if (self == null) { 104 self = new LeftShiftNode(forX, forY); 105 } 106 return self; 107 } 108 109 @Override 110 public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) { 111 nodeValueMap.setResult(this, gen.emitShl(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()))); 112 } 113} 114