1 /*
   2  * Copyright (c) 2019, 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 
  26 package org.graalvm.compiler.replacements.nodes.arithmetic;
  27 
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.spi.Canonicalizable;
  31 import org.graalvm.compiler.graph.spi.Simplifiable;
  32 import org.graalvm.compiler.graph.spi.SimplifierTool;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 import org.graalvm.compiler.nodes.AbstractBeginNode;
  35 import org.graalvm.compiler.nodes.IfNode;
  36 import org.graalvm.compiler.nodes.LogicNode;
  37 import org.graalvm.compiler.nodes.NodeView;
  38 import org.graalvm.compiler.nodes.ValueNode;
  39 import org.graalvm.compiler.nodes.calc.BinaryNode;
  40 
  41 import java.util.List;
  42 
  43 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  44 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  45 
  46 @NodeInfo(cycles = CYCLES_2, size = SIZE_2)
  47 public abstract class IntegerExactOverflowNode extends LogicNode implements Canonicalizable.Binary<ValueNode>, Simplifiable {
  48     public static final NodeClass<IntegerExactOverflowNode> TYPE = NodeClass.create(IntegerExactOverflowNode.class);
  49     @Input protected ValueNode x;
  50     @Input protected ValueNode y;
  51 
  52     public IntegerExactOverflowNode(NodeClass<? extends IntegerExactOverflowNode> c, ValueNode x, ValueNode y) {
  53         super(c);
  54         assert x != null && y != null;
  55         this.x = x;
  56         this.y = y;
  57     }
  58 
  59     @Override
  60     public ValueNode getX() {
  61         return x;
  62     }
  63 
  64     @Override
  65     public ValueNode getY() {
  66         return y;
  67     }
  68 
  69     /**
  70      * Make sure the overflow detection nodes have the same order of inputs as the exact arithmetic
  71      * nodes.
  72      *
  73      * @return the original node or another node with the same inputs, ignoring ordering.
  74      */
  75     @SuppressWarnings("deprecation")
  76     public LogicNode maybeCommuteInputs() {
  77         assert this instanceof BinaryCommutative;
  78         if (!y.isConstant() && (x.isConstant() || x.getId() > y.getId())) {
  79             ValueNode tmp = x;
  80             x = y;
  81             y = tmp;
  82             if (graph() != null) {
  83                 // See if this node already exists
  84                 LogicNode duplicate = graph().findDuplicate(this);
  85                 if (duplicate != null) {
  86                     return duplicate;
  87                 }
  88             }
  89         }
  90         return this;
  91     }
  92 
  93     protected abstract IntegerExactArithmeticSplitNode createSplit(Stamp splitStamp, AbstractBeginNode next, AbstractBeginNode overflow);
  94 
  95     protected abstract Class<? extends BinaryNode> getCoupledType();
  96 
  97     @Override
  98     public void simplify(SimplifierTool tool) {
  99         // Find all ifs that this node feeds into
 100         for (IfNode ifNode : usages().filter(IfNode.class).snapshot()) {
 101             // Replace the if with exact split
 102             AbstractBeginNode next = ifNode.falseSuccessor();
 103             AbstractBeginNode overflow = ifNode.trueSuccessor();
 104             ifNode.clearSuccessors();
 105 
 106             // Try to find corresponding exact nodes that could be combined with the split. They
 107             // would be directly
 108             // linked to the BeginNode of the false branch.
 109             List<? extends BinaryNode> coupledNodes = next.usages().filter(getCoupledType()).filter(n -> {
 110                 BinaryNode exact = (BinaryNode) n;
 111                 return exact.getX() == getX() && exact.getY() == getY();
 112             }).snapshot();
 113 
 114             Stamp splitStamp = x.stamp(NodeView.DEFAULT).unrestricted();
 115             if (!coupledNodes.isEmpty()) {
 116                 splitStamp = coupledNodes.iterator().next().stamp(NodeView.DEFAULT);
 117             }
 118             IntegerExactArithmeticSplitNode split = graph().add(createSplit(splitStamp, next, overflow));
 119             ifNode.replaceAndDelete(split);
 120 
 121             coupledNodes.forEach(n -> n.replaceAndDelete(split));
 122         }
 123     }
 124 }