1 /*
   2  * Copyright (c) 2011, 2016, 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.IntegerStamp;
  26 import org.graalvm.compiler.graph.NodeClass;
  27 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  28 import org.graalvm.compiler.nodeinfo.NodeInfo;
  29 import org.graalvm.compiler.nodes.ConstantNode;
  30 import org.graalvm.compiler.nodes.ValueNode;
  31 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  32 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  33 
  34 import jdk.vm.ci.code.CodeUtil;
  35 
  36 @NodeInfo(shortName = "%")
  37 public class SignedRemNode extends IntegerDivRemNode implements LIRLowerable {
  38 
  39     public static final NodeClass<SignedRemNode> TYPE = NodeClass.create(SignedRemNode.class);
  40 
  41     public SignedRemNode(ValueNode x, ValueNode y) {
  42         this(TYPE, x, y);
  43     }
  44 
  45     protected SignedRemNode(NodeClass<? extends SignedRemNode> c, ValueNode x, ValueNode y) {
  46         super(c, IntegerStamp.OPS.getRem().foldStamp(x.stamp(), y.stamp()), Op.REM, Type.SIGNED, x, y);
  47     }
  48 
  49     @Override
  50     public boolean inferStamp() {
  51         return updateStamp(IntegerStamp.OPS.getRem().foldStamp(getX().stamp(), getY().stamp()));
  52     }
  53 
  54     @Override
  55     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  56         if (forX.isConstant() && forY.isConstant()) {
  57             @SuppressWarnings("hiding")
  58             long y = forY.asJavaConstant().asLong();
  59             if (y == 0) {
  60                 return this; // this will trap, can not canonicalize
  61             }
  62             return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() % y);
  63         } else if (forY.isConstant() && forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) {
  64             long constY = forY.asJavaConstant().asLong();
  65             IntegerStamp xStamp = (IntegerStamp) forX.stamp();
  66             IntegerStamp yStamp = (IntegerStamp) forY.stamp();
  67             if (constY < 0 && constY != CodeUtil.minValue(yStamp.getBits())) {
  68                 return new SignedRemNode(forX, ConstantNode.forIntegerStamp(yStamp, -constY)).canonical(tool);
  69             }
  70 
  71             if (constY == 1) {
  72                 return ConstantNode.forIntegerStamp(stamp(), 0);
  73             } else if (CodeUtil.isPowerOf2(constY)) {
  74                 if (xStamp.isPositive()) {
  75                     return new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), constY - 1));
  76                 } else if (xStamp.isNegative()) {
  77                     return new NegateNode(new AndNode(new NegateNode(forX), ConstantNode.forIntegerStamp(stamp(), constY - 1)));
  78                 } else {
  79                     return new ConditionalNode(IntegerLessThanNode.create(forX, ConstantNode.forIntegerStamp(forX.stamp(), 0)),
  80                                     new NegateNode(new AndNode(new NegateNode(forX), ConstantNode.forIntegerStamp(stamp(), constY - 1))),
  81                                     new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), constY - 1)));
  82                 }
  83             }
  84         }
  85         return this;
  86     }
  87 
  88     @Override
  89     public void generate(NodeLIRBuilderTool gen) {
  90         gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitRem(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
  91     }
  92 }