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.ShiftOp.Shr;
  27 import org.graalvm.compiler.core.common.type.IntegerStamp;
  28 import org.graalvm.compiler.graph.NodeClass;
  29 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  30 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.ValueNode;
  34 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  35 
  36 import jdk.vm.ci.code.CodeUtil;
  37 
  38 @NodeInfo(shortName = ">>")
  39 public final class RightShiftNode extends ShiftNode<Shr> {
  40 
  41     public static final NodeClass<RightShiftNode> TYPE = NodeClass.create(RightShiftNode.class);
  42 
  43     public RightShiftNode(ValueNode x, ValueNode y) {
  44         super(TYPE, ArithmeticOpTable::getShr, x, y);
  45     }
  46 
  47     @Override
  48     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  49         ValueNode ret = super.canonical(tool, forX, forY);
  50         if (ret != this) {
  51             return ret;
  52         }
  53 
  54         if (forX.stamp() instanceof IntegerStamp && ((IntegerStamp) forX.stamp()).isPositive()) {
  55             return new UnsignedRightShiftNode(forX, forY);
  56         }
  57 
  58         if (forY.isConstant()) {
  59             int amount = forY.asJavaConstant().asInt();
  60             int originalAmout = amount;
  61             int mask = getShiftAmountMask();
  62             amount &= mask;
  63             if (amount == 0) {
  64                 return forX;
  65             }
  66             if (forX instanceof ShiftNode) {
  67                 ShiftNode<?> other = (ShiftNode<?>) forX;
  68                 if (other.getY().isConstant()) {
  69                     int otherAmount = other.getY().asJavaConstant().asInt() & mask;
  70                     if (other instanceof RightShiftNode) {
  71                         int total = amount + otherAmount;
  72                         if (total != (total & mask)) {
  73                             assert other.getX().stamp() instanceof IntegerStamp;
  74                             IntegerStamp istamp = (IntegerStamp) other.getX().stamp();
  75 
  76                             if (istamp.isPositive()) {
  77                                 return ConstantNode.forIntegerKind(getStackKind(), 0);
  78                             }
  79                             if (istamp.isStrictlyNegative()) {
  80                                 return ConstantNode.forIntegerKind(getStackKind(), -1L);
  81                             }
  82 
  83                             /*
  84                              * if we cannot replace both shifts with a constant, replace them by a
  85                              * full shift for this kind
  86                              */
  87                             assert total >= mask;
  88                             return new RightShiftNode(other.getX(), ConstantNode.forInt(mask));
  89                         }
  90                         return new RightShiftNode(other.getX(), ConstantNode.forInt(total));
  91                     }
  92                 }
  93             }
  94             if (originalAmout != amount) {
  95                 return new RightShiftNode(forX, ConstantNode.forInt(amount));
  96             }
  97         }
  98         return this;
  99     }
 100 
 101     @Override
 102     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
 103         nodeValueMap.setResult(this, gen.emitShr(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
 104     }
 105 
 106     @Override
 107     public boolean isNarrowable(int resultBits) {
 108         if (super.isNarrowable(resultBits)) {
 109             /*
 110              * For signed right shifts, the narrow can be done before the shift if the cut off bits
 111              * are all equal to the sign bit of the input. That's equivalent to the condition that
 112              * the input is in the signed range of the narrow type.
 113              */
 114             IntegerStamp inputStamp = (IntegerStamp) getX().stamp();
 115             return CodeUtil.minValue(resultBits) <= inputStamp.lowerBound() && inputStamp.upperBound() <= CodeUtil.maxValue(resultBits);
 116         } else {
 117             return false;
 118         }
 119     }
 120 }