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.UShr;
  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.meta.JavaKind;
  37 
  38 @NodeInfo(shortName = ">>>")
  39 public final class UnsignedRightShiftNode extends ShiftNode<UShr> {
  40 
  41     public static final NodeClass<UnsignedRightShiftNode> TYPE = NodeClass.create(UnsignedRightShiftNode.class);
  42 
  43     public UnsignedRightShiftNode(ValueNode x, ValueNode y) {
  44         super(TYPE, ArithmeticOpTable::getUShr, 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 (forY.isConstant()) {
  55             int amount = forY.asJavaConstant().asInt();
  56             int originalAmout = amount;
  57             int mask = getShiftAmountMask();
  58             amount &= mask;
  59             if (amount == 0) {
  60                 return forX;
  61             }
  62             if (forX instanceof ShiftNode) {
  63                 ShiftNode<?> other = (ShiftNode<?>) forX;
  64                 if (other.getY().isConstant()) {
  65                     int otherAmount = other.getY().asJavaConstant().asInt() & mask;
  66                     if (other instanceof UnsignedRightShiftNode) {
  67                         int total = amount + otherAmount;
  68                         if (total != (total & mask)) {
  69                             return ConstantNode.forIntegerKind(getStackKind(), 0);
  70                         }
  71                         return new UnsignedRightShiftNode(other.getX(), ConstantNode.forInt(total));
  72                     } else if (other instanceof LeftShiftNode && otherAmount == amount) {
  73                         if (getStackKind() == JavaKind.Long) {
  74                             return new AndNode(other.getX(), ConstantNode.forLong(-1L >>> amount));
  75                         } else {
  76                             assert getStackKind() == JavaKind.Int;
  77                             return new AndNode(other.getX(), ConstantNode.forInt(-1 >>> amount));
  78                         }
  79                     }
  80                 }
  81             }
  82             if (originalAmout != amount) {
  83                 return new UnsignedRightShiftNode(forX, ConstantNode.forInt(amount));
  84             }
  85         }
  86         return this;
  87     }
  88 
  89     @Override
  90     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
  91         nodeValueMap.setResult(this, gen.emitUShr(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
  92     }
  93 
  94     @Override
  95     public boolean isNarrowable(int resultBits) {
  96         if (super.isNarrowable(resultBits)) {
  97             /*
  98              * For unsigned right shifts, the narrow can be done before the shift if the cut off
  99              * bits are all zero.
 100              */
 101             IntegerStamp inputStamp = (IntegerStamp) getX().stamp();
 102             return (inputStamp.upMask() & ~(resultBits - 1)) == 0;
 103         } else {
 104             return false;
 105         }
 106     }
 107 }