001/*
002 * Copyright (c) 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.loop;
024
025import com.oracle.graal.compiler.common.type.*;
026import com.oracle.graal.nodes.*;
027import com.oracle.graal.nodes.calc.*;
028
029/**
030 * Utility methods to perform integer math with some obvious constant folding first.
031 */
032public class MathUtil {
033    private static boolean isConstantOne(ValueNode v1) {
034        return v1.isConstant() && v1.stamp() instanceof IntegerStamp && v1.asJavaConstant().asLong() == 1;
035    }
036
037    private static boolean isConstantZero(ValueNode v1) {
038        return v1.isConstant() && v1.stamp() instanceof IntegerStamp && v1.asJavaConstant().asLong() == 0;
039    }
040
041    public static ValueNode add(StructuredGraph graph, ValueNode v1, ValueNode v2) {
042        if (isConstantZero(v1)) {
043            return v2;
044        }
045        if (isConstantZero(v2)) {
046            return v1;
047        }
048        return BinaryArithmeticNode.add(graph, v1, v2);
049    }
050
051    public static ValueNode mul(StructuredGraph graph, ValueNode v1, ValueNode v2) {
052        if (isConstantOne(v1)) {
053            return v2;
054        }
055        if (isConstantOne(v2)) {
056            return v1;
057        }
058        return BinaryArithmeticNode.mul(graph, v1, v2);
059    }
060
061    public static ValueNode sub(StructuredGraph graph, ValueNode v1, ValueNode v2) {
062        if (isConstantZero(v2)) {
063            return v1;
064        }
065        return BinaryArithmeticNode.sub(graph, v1, v2);
066    }
067
068    public static ValueNode divBefore(StructuredGraph graph, FixedNode before, ValueNode dividend, ValueNode divisor) {
069        if (isConstantOne(divisor)) {
070            return dividend;
071        }
072        IntegerDivNode div = graph.add(new IntegerDivNode(dividend, divisor));
073        graph.addBeforeFixed(before, div);
074        return div;
075    }
076}