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.IntegerStamp;
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.NodeView;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 import org.graalvm.compiler.nodes.extended.GuardingNode;
  36 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  37 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  38 
  39 import jdk.vm.ci.code.CodeUtil;
  40 
  41 @NodeInfo(shortName = "%")
  42 public class SignedRemNode extends IntegerDivRemNode implements LIRLowerable {
  43 
  44     public static final NodeClass<SignedRemNode> TYPE = NodeClass.create(SignedRemNode.class);
  45 
  46     protected SignedRemNode(ValueNode x, ValueNode y, GuardingNode zeroCheck) {
  47         this(TYPE, x, y, zeroCheck);
  48     }
  49 
  50     protected SignedRemNode(NodeClass<? extends SignedRemNode> c, ValueNode x, ValueNode y, GuardingNode zeroCheck) {
  51         super(c, IntegerStamp.OPS.getRem().foldStamp(x.stamp(NodeView.DEFAULT), y.stamp(NodeView.DEFAULT)), Op.REM, Type.SIGNED, x, y, zeroCheck);
  52     }
  53 
  54     public static ValueNode create(ValueNode x, ValueNode y, GuardingNode zeroCheck, NodeView view) {
  55         Stamp stamp = IntegerStamp.OPS.getRem().foldStamp(x.stamp(view), y.stamp(view));
  56         return canonical(null, x, y, zeroCheck, stamp, view);
  57     }
  58 
  59     @Override
  60     public boolean inferStamp() {
  61         return updateStamp(IntegerStamp.OPS.getRem().foldStamp(getX().stamp(NodeView.DEFAULT), getY().stamp(NodeView.DEFAULT)));
  62     }
  63 
  64     @Override
  65     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  66         NodeView view = NodeView.from(tool);
  67         return canonical(this, forX, forY, getZeroCheck(), stamp(view), view);
  68     }
  69 
  70     private static ValueNode canonical(SignedRemNode self, ValueNode forX, ValueNode forY, GuardingNode zeroCheck, Stamp stamp, NodeView view) {
  71         if (forX.isConstant() && forY.isConstant()) {
  72             long y = forY.asJavaConstant().asLong();
  73             if (y == 0) {
  74                 /* This will trap, cannot canonicalize. */
  75                 return self != null ? self : new SignedRemNode(forX, forY, zeroCheck);
  76             }
  77             return ConstantNode.forIntegerStamp(stamp, forX.asJavaConstant().asLong() % y);
  78         } else if (forY.isConstant() && forX.stamp(view) instanceof IntegerStamp && forY.stamp(view) instanceof IntegerStamp) {
  79             long constY = forY.asJavaConstant().asLong();
  80             IntegerStamp xStamp = (IntegerStamp) forX.stamp(view);
  81             IntegerStamp yStamp = (IntegerStamp) forY.stamp(view);
  82             if (constY < 0 && constY != CodeUtil.minValue(yStamp.getBits())) {
  83                 Stamp newStamp = IntegerStamp.OPS.getRem().foldStamp(forX.stamp(view), forY.stamp(view));
  84                 return canonical(null, forX, ConstantNode.forIntegerStamp(yStamp, -constY), zeroCheck, newStamp, view);
  85             }
  86 
  87             if (constY == 1) {
  88                 return ConstantNode.forIntegerStamp(stamp, 0);
  89             } else if (CodeUtil.isPowerOf2(constY)) {
  90                 if (xStamp.isPositive()) {
  91                     // x & (y - 1)
  92                     return new AndNode(forX, ConstantNode.forIntegerStamp(stamp, constY - 1));
  93                 } else if (xStamp.isNegative()) {
  94                     // -((-x) & (y - 1))
  95                     return new NegateNode(new AndNode(new NegateNode(forX), ConstantNode.forIntegerStamp(stamp, constY - 1)));
  96                 } else {
  97                     // x - ((x / y) << log2(y))
  98                     return SubNode.create(forX, LeftShiftNode.create(SignedDivNode.canonical(forX, constY, view), ConstantNode.forInt(CodeUtil.log2(constY)), view), view);
  99                 }
 100             }
 101         }
 102         return self != null ? self : new SignedRemNode(forX, forY, zeroCheck);
 103     }
 104 
 105     @Override
 106     public void generate(NodeLIRBuilderTool gen) {
 107         gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitRem(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
 108     }
 109 }