001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.calc;
024
025import jdk.internal.jvmci.code.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.spi.*;
033
034@NodeInfo(shortName = "%")
035public class IntegerRemNode extends FixedBinaryNode implements Lowerable, LIRLowerable {
036    public static final NodeClass<IntegerRemNode> TYPE = NodeClass.create(IntegerRemNode.class);
037
038    public IntegerRemNode(ValueNode x, ValueNode y) {
039        this(TYPE, x, y);
040    }
041
042    protected IntegerRemNode(NodeClass<? extends IntegerRemNode> c, ValueNode x, ValueNode y) {
043        super(c, IntegerStamp.OPS.getRem().foldStamp(x.stamp(), y.stamp()), x, y);
044    }
045
046    @Override
047    public boolean inferStamp() {
048        return updateStamp(IntegerStamp.OPS.getRem().foldStamp(getX().stamp(), getY().stamp()));
049    }
050
051    @Override
052    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
053        if (forX.isConstant() && forY.isConstant()) {
054            @SuppressWarnings("hiding")
055            long y = forY.asJavaConstant().asLong();
056            if (y == 0) {
057                return this; // this will trap, can not canonicalize
058            }
059            return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() % y);
060        } else if (forY.isConstant()) {
061            long c = forY.asJavaConstant().asLong();
062            if (c == 1 || c == -1) {
063                return ConstantNode.forIntegerStamp(stamp(), 0);
064            } else if (c > 0 && CodeUtil.isPowerOf2(c) && forX.stamp() instanceof IntegerStamp && ((IntegerStamp) forX.stamp()).isPositive()) {
065                return new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), c - 1));
066            }
067        }
068        return this;
069    }
070
071    @Override
072    public void lower(LoweringTool tool) {
073        tool.getLowerer().lower(this, tool);
074    }
075
076    @Override
077    public void generate(NodeLIRBuilderTool gen) {
078        gen.setResult(this, gen.getLIRGeneratorTool().emitRem(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
079    }
080
081    @Override
082    public boolean canDeoptimize() {
083        return !(getY().stamp() instanceof IntegerStamp) || ((IntegerStamp) getY().stamp()).contains(0);
084    }
085}