1 /*
   2  * Copyright (c) 2013, 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 package org.graalvm.compiler.replacements.nodes.arithmetic;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  29 
  30 import org.graalvm.compiler.core.common.type.Stamp;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.graph.spi.Simplifiable;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 import org.graalvm.compiler.nodes.AbstractBeginNode;
  35 import org.graalvm.compiler.nodes.ControlSplitNode;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  38 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  39 
  40 import jdk.vm.ci.meta.Value;
  41 
  42 @NodeInfo(cycles = CYCLES_2, cyclesRationale = "add+cmp", size = SIZE_2)
  43 public abstract class IntegerExactArithmeticSplitNode extends ControlSplitNode implements Simplifiable, LIRLowerable {
  44     public static final NodeClass<IntegerExactArithmeticSplitNode> TYPE = NodeClass.create(IntegerExactArithmeticSplitNode.class);
  45 
  46     @Successor AbstractBeginNode next;
  47     @Successor AbstractBeginNode overflowSuccessor;
  48     @Input ValueNode x;
  49     @Input ValueNode y;
  50 
  51     protected IntegerExactArithmeticSplitNode(NodeClass<? extends IntegerExactArithmeticSplitNode> c, Stamp stamp, ValueNode x, ValueNode y, AbstractBeginNode next,
  52                     AbstractBeginNode overflowSuccessor) {
  53         super(c, stamp);
  54         this.x = x;
  55         this.y = y;
  56         this.overflowSuccessor = overflowSuccessor;
  57         this.next = next;
  58     }
  59 
  60     @Override
  61     public AbstractBeginNode getPrimarySuccessor() {
  62         return next;
  63     }
  64 
  65     @Override
  66     public double probability(AbstractBeginNode successor) {
  67         return successor == next ? 1 : 0;
  68     }
  69 
  70     @Override
  71     public boolean setProbability(AbstractBeginNode successor, double value) {
  72         // Successor probabilities for arithmetic split nodes are fixed.
  73         return false;
  74     }
  75 
  76     public AbstractBeginNode getNext() {
  77         return next;
  78     }
  79 
  80     public AbstractBeginNode getOverflowSuccessor() {
  81         return overflowSuccessor;
  82     }
  83 
  84     public void setNext(AbstractBeginNode next) {
  85         updatePredecessor(this.next, next);
  86         this.next = next;
  87     }
  88 
  89     public void setOverflowSuccessor(AbstractBeginNode overflowSuccessor) {
  90         updatePredecessor(this.overflowSuccessor, overflowSuccessor);
  91         this.overflowSuccessor = overflowSuccessor;
  92     }
  93 
  94     public ValueNode getX() {
  95         return x;
  96     }
  97 
  98     public ValueNode getY() {
  99         return y;
 100     }
 101 
 102     @Override
 103     public void generate(NodeLIRBuilderTool generator) {
 104         generator.setResult(this, generateArithmetic(generator));
 105         generator.emitOverflowCheckBranch(getOverflowSuccessor(), getNext(), stamp, probability(getOverflowSuccessor()));
 106     }
 107 
 108     protected abstract Value generateArithmetic(NodeLIRBuilderTool generator);
 109 
 110     @Override
 111     public int getSuccessorCount() {
 112         return 2;
 113     }
 114 }