1 /*
   2  * Copyright (c) 2009, 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.nodes.calc;
  26 
  27 import org.graalvm.compiler.graph.NodeClass;
  28 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  29 import org.graalvm.compiler.nodeinfo.NodeInfo;
  30 import org.graalvm.compiler.nodes.LogicNode;
  31 import org.graalvm.compiler.nodes.NodeView;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.options.OptionValues;
  34 
  35 import jdk.vm.ci.meta.ConstantReflectionProvider;
  36 import jdk.vm.ci.meta.JavaKind;
  37 import jdk.vm.ci.meta.MetaAccessProvider;
  38 
  39 @NodeInfo
  40 public final class IntegerNormalizeCompareNode extends AbstractNormalizeCompareNode {
  41     public static final NodeClass<IntegerNormalizeCompareNode> TYPE = NodeClass.create(IntegerNormalizeCompareNode.class);
  42     protected final boolean unsigned;
  43 
  44     public IntegerNormalizeCompareNode(ValueNode x, ValueNode y, JavaKind kind, boolean unsigned) {
  45         super(TYPE, kind, x, y);
  46         this.unsigned = unsigned;
  47     }
  48 
  49     public static ValueNode create(ValueNode x, ValueNode y, boolean unsigned, JavaKind kind, ConstantReflectionProvider constantReflection) {
  50         ValueNode result = tryConstantFold(x, y, false, unsigned, kind, constantReflection);
  51         if (result != null) {
  52             return result;
  53         }
  54 
  55         return new IntegerNormalizeCompareNode(x, y, kind, unsigned);
  56     }
  57 
  58     @Override
  59     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  60         NodeView view = NodeView.from(tool);
  61         ValueNode result = tryConstantFold(x, y, false, unsigned, stamp(view).getStackKind(), tool.getConstantReflection());
  62         if (result != null) {
  63             return result;
  64         }
  65         return this;
  66     }
  67 
  68     @Override
  69     public LogicNode createLowerComparison(boolean swapInputs) {
  70         ValueNode a = swapInputs ? y : x;
  71         ValueNode b = swapInputs ? x : y;
  72         if (unsigned) {
  73             return IntegerBelowNode.create(a, b, NodeView.DEFAULT);
  74         } else {
  75             return IntegerLessThanNode.create(a, b, NodeView.DEFAULT);
  76         }
  77     }
  78 
  79     @Override
  80     public LogicNode createLowerComparison(boolean swapInputs, ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
  81                     NodeView view) {
  82         ValueNode a = swapInputs ? y : x;
  83         ValueNode b = swapInputs ? x : y;
  84         if (unsigned) {
  85             return IntegerBelowNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, NodeView.DEFAULT);
  86         } else {
  87             return IntegerLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, NodeView.DEFAULT);
  88         }
  89     }
  90 
  91     @Override
  92     public LogicNode createEqualComparison() {
  93         return IntegerEqualsNode.create(x, y, NodeView.DEFAULT);
  94     }
  95 
  96     @Override
  97     public LogicNode createEqualComparison(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth, NodeView view) {
  98         return IntegerEqualsNode.create(constantReflection, metaAccess, options, smallestCompareWidth, x, y, view);
  99     }
 100 }