1 /*
   2  * Copyright (c) 2015, 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.loop;
  26 
  27 import org.graalvm.compiler.core.common.type.IntegerStamp;
  28 import org.graalvm.compiler.nodes.FixedNode;
  29 import org.graalvm.compiler.nodes.NodeView;
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 import org.graalvm.compiler.nodes.ValueNode;
  32 import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode;
  33 import org.graalvm.compiler.nodes.calc.FixedBinaryNode;
  34 import org.graalvm.compiler.nodes.calc.SignedDivNode;
  35 import org.graalvm.compiler.nodes.calc.UnsignedDivNode;
  36 import org.graalvm.compiler.nodes.extended.GuardingNode;
  37 
  38 import java.util.function.BiFunction;
  39 
  40 /**
  41  * Utility methods to perform integer math with some obvious constant folding first.
  42  */
  43 public class MathUtil {
  44     private static boolean isConstantOne(ValueNode v1) {
  45         return v1.isConstant() && v1.stamp(NodeView.DEFAULT) instanceof IntegerStamp && v1.asJavaConstant().asLong() == 1;
  46     }
  47 
  48     private static boolean isConstantZero(ValueNode v1) {
  49         return v1.isConstant() && v1.stamp(NodeView.DEFAULT) instanceof IntegerStamp && v1.asJavaConstant().asLong() == 0;
  50     }
  51 
  52     public static ValueNode add(StructuredGraph graph, ValueNode v1, ValueNode v2) {
  53         if (isConstantZero(v1)) {
  54             return v2;
  55         }
  56         if (isConstantZero(v2)) {
  57             return v1;
  58         }
  59         return BinaryArithmeticNode.add(graph, v1, v2, NodeView.DEFAULT);
  60     }
  61 
  62     public static ValueNode mul(StructuredGraph graph, ValueNode v1, ValueNode v2) {
  63         if (isConstantOne(v1)) {
  64             return v2;
  65         }
  66         if (isConstantOne(v2)) {
  67             return v1;
  68         }
  69         return BinaryArithmeticNode.mul(graph, v1, v2, NodeView.DEFAULT);
  70     }
  71 
  72     public static ValueNode sub(StructuredGraph graph, ValueNode v1, ValueNode v2) {
  73         if (isConstantZero(v2)) {
  74             return v1;
  75         }
  76         return BinaryArithmeticNode.sub(graph, v1, v2, NodeView.DEFAULT);
  77     }
  78 
  79     public static ValueNode divBefore(StructuredGraph graph, FixedNode before, ValueNode dividend, ValueNode divisor, GuardingNode zeroCheck) {
  80         return fixedDivBefore(graph, before, dividend, divisor, (dend, sor) -> SignedDivNode.create(dend, sor, zeroCheck, NodeView.DEFAULT));
  81     }
  82 
  83     public static ValueNode unsignedDivBefore(StructuredGraph graph, FixedNode before, ValueNode dividend, ValueNode divisor, GuardingNode zeroCheck) {
  84         return fixedDivBefore(graph, before, dividend, divisor, (dend, sor) -> UnsignedDivNode.create(dend, sor, zeroCheck, NodeView.DEFAULT));
  85     }
  86 
  87     private static ValueNode fixedDivBefore(StructuredGraph graph, FixedNode before, ValueNode dividend, ValueNode divisor, BiFunction<ValueNode, ValueNode, ValueNode> createDiv) {
  88         if (isConstantOne(divisor)) {
  89             return dividend;
  90         }
  91         ValueNode div = graph.addOrUniqueWithInputs(createDiv.apply(dividend, divisor));
  92         if (div instanceof FixedBinaryNode) {
  93             FixedBinaryNode fixedDiv = (FixedBinaryNode) div;
  94             if (before.predecessor() instanceof FixedBinaryNode) {
  95                 FixedBinaryNode binaryPredecessor = (FixedBinaryNode) before.predecessor();
  96                 if (fixedDiv.dataFlowEquals(binaryPredecessor)) {
  97                     fixedDiv.safeDelete();
  98                     return binaryPredecessor;
  99                 }
 100             }
 101             graph.addBeforeFixed(before, fixedDiv);
 102         }
 103         return div;
 104     }
 105 }