1 /*
   2  * Copyright (c) 2011, 2019, 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.IntegerStamp;
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.graph.Node;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  32 import org.graalvm.compiler.nodeinfo.NodeInfo;
  33 import org.graalvm.compiler.nodes.ConstantNode;
  34 import org.graalvm.compiler.nodes.NodeView;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.extended.GuardingNode;
  37 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  38 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  39 
  40 import jdk.vm.ci.code.CodeUtil;
  41 import jdk.vm.ci.meta.Constant;
  42 import jdk.vm.ci.meta.PrimitiveConstant;
  43 
  44 @NodeInfo(shortName = "%")
  45 public class SignedRemNode extends IntegerDivRemNode implements LIRLowerable {
  46 
  47     public static final NodeClass<SignedRemNode> TYPE = NodeClass.create(SignedRemNode.class);
  48 
  49     public SignedRemNode(ValueNode x, ValueNode y, GuardingNode zeroCheck) {
  50         this(TYPE, x, y, zeroCheck);
  51     }
  52 
  53     protected SignedRemNode(NodeClass<? extends SignedRemNode> c, ValueNode x, ValueNode y, GuardingNode zeroCheck) {
  54         super(c, IntegerStamp.OPS.getRem().foldStamp(x.stamp(NodeView.DEFAULT), y.stamp(NodeView.DEFAULT)), Op.REM, Type.SIGNED, x, y, zeroCheck);
  55     }
  56 
  57     public static ValueNode create(ValueNode x, ValueNode y, GuardingNode zeroCheck, NodeView view) {
  58         Stamp stamp = IntegerStamp.OPS.getRem().foldStamp(x.stamp(view), y.stamp(view));
  59         return canonical(null, x, y, zeroCheck, stamp, view, null);
  60     }
  61 
  62     @Override
  63     public boolean inferStamp() {
  64         return updateStamp(IntegerStamp.OPS.getRem().foldStamp(getX().stamp(NodeView.DEFAULT), getY().stamp(NodeView.DEFAULT)));
  65     }
  66 
  67     @Override
  68     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  69         NodeView view = NodeView.from(tool);
  70         return canonical(this, forX, forY, getZeroCheck(), stamp(view), view, tool);
  71     }
  72 
  73     private static ValueNode canonical(SignedRemNode self, ValueNode forX, ValueNode forY, GuardingNode zeroCheck, Stamp stamp, NodeView view, CanonicalizerTool tool) {
  74         if (forX.isConstant() && forY.isConstant()) {
  75             long y = forY.asJavaConstant().asLong();
  76             if (y == 0) {
  77                 /* This will trap, cannot canonicalize. */
  78                 return self != null ? self : new SignedRemNode(forX, forY, zeroCheck);
  79             }
  80             return ConstantNode.forIntegerStamp(stamp, forX.asJavaConstant().asLong() % y);
  81         } else if (forY.isConstant() && forX.stamp(view) instanceof IntegerStamp && forY.stamp(view) instanceof IntegerStamp) {
  82             long constY = forY.asJavaConstant().asLong();
  83             IntegerStamp xStamp = (IntegerStamp) forX.stamp(view);
  84             IntegerStamp yStamp = (IntegerStamp) forY.stamp(view);
  85             if (constY < 0 && constY != CodeUtil.minValue(yStamp.getBits())) {
  86                 Stamp newStamp = IntegerStamp.OPS.getRem().foldStamp(forX.stamp(view), forY.stamp(view));
  87                 return canonical(null, forX, ConstantNode.forIntegerStamp(yStamp, -constY), zeroCheck, newStamp, view, tool);
  88             }
  89 
  90             if (constY == 1) {
  91                 return ConstantNode.forIntegerStamp(stamp, 0);
  92             } else if (CodeUtil.isPowerOf2(constY) && tool != null && tool.allUsagesAvailable()) {
  93                 if (allUsagesCompareAgainstZero(self)) {
  94                     // x % y == 0 <=> (x & (y-1)) == 0
  95                     return new AndNode(forX, ConstantNode.forIntegerStamp(yStamp, constY - 1));
  96                 } else {
  97                     if (xStamp.isPositive()) {
  98                         // x & (y - 1)
  99                         return new AndNode(forX, ConstantNode.forIntegerStamp(stamp, constY - 1));
 100                     } else if (xStamp.isNegative()) {
 101                         // -((-x) & (y - 1))
 102                         return new NegateNode(new AndNode(new NegateNode(forX), ConstantNode.forIntegerStamp(stamp, constY - 1)));
 103                     }
 104                 }
 105             }
 106         }
 107         return self != null ? self : new SignedRemNode(forX, forY, zeroCheck);
 108     }
 109 
 110     private static boolean allUsagesCompareAgainstZero(SignedRemNode self) {
 111         int compareAgainstZero = 0;
 112         int usageCount = self.getUsageCount();
 113         for (int i = 0; i < usageCount; i++) {
 114             Node usage = self.getUsageAt(i);
 115             if (usage instanceof IntegerEqualsNode) {
 116                 IntegerEqualsNode equalsNode = (IntegerEqualsNode) usage;
 117                 ValueNode node = equalsNode.getY();
 118                 if (node == self) {
 119                     node = equalsNode.getX();
 120                 }
 121                 if (node instanceof ConstantNode) {
 122                     ConstantNode constantNode = (ConstantNode) node;
 123                     Constant constant = constantNode.asConstant();
 124                     if (constant instanceof PrimitiveConstant && ((PrimitiveConstant) constant).asLong() == 0) {
 125                         compareAgainstZero++;
 126                     }
 127                 }
 128             }
 129         }
 130         return compareAgainstZero == usageCount;
 131     }
 132 
 133     @Override
 134     public void generate(NodeLIRBuilderTool gen) {
 135         gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitRem(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
 136     }
 137 }