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 static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  28 
  29 import org.graalvm.compiler.core.common.calc.CanonicalCondition;
  30 import org.graalvm.compiler.core.common.type.Stamp;
  31 import org.graalvm.compiler.core.common.type.StampFactory;
  32 import org.graalvm.compiler.graph.IterableNodeType;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.nodeinfo.NodeCycles;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.ConstantNode;
  37 import org.graalvm.compiler.nodes.LogicConstantNode;
  38 import org.graalvm.compiler.nodes.LogicNode;
  39 import org.graalvm.compiler.nodes.NodeView;
  40 import org.graalvm.compiler.nodes.ValueNode;
  41 import org.graalvm.compiler.options.OptionValues;
  42 
  43 import jdk.vm.ci.meta.ConstantReflectionProvider;
  44 import jdk.vm.ci.meta.JavaKind;
  45 import jdk.vm.ci.meta.MetaAccessProvider;
  46 
  47 /**
  48  * Returns -1, 0, or 1 if either x < y, x == y, or x > y.
  49  */
  50 @NodeInfo(cycles = NodeCycles.CYCLES_2, size = SIZE_2)
  51 public abstract class AbstractNormalizeCompareNode extends BinaryNode implements IterableNodeType {
  52     public static final NodeClass<AbstractNormalizeCompareNode> TYPE = NodeClass.create(AbstractNormalizeCompareNode.class);
  53 
  54     public AbstractNormalizeCompareNode(NodeClass<? extends BinaryNode> c, JavaKind kind, ValueNode x, ValueNode y) {
  55         super(c, StampFactory.forInteger(kind, -1, 1), x, y);
  56     }
  57 
  58     @Override
  59     public boolean inferStamp() {
  60         return false;
  61     }
  62 
  63     @Override
  64     public Stamp foldStamp(Stamp stampX, Stamp stampY) {
  65         return stamp(NodeView.DEFAULT);
  66     }
  67 
  68     protected static ValueNode tryConstantFold(ValueNode x, ValueNode y, boolean isUnorderedLess, boolean unsigned, JavaKind kind, ConstantReflectionProvider constantReflection) {
  69         LogicNode result = CompareNode.tryConstantFold(CanonicalCondition.EQ, x, y, null, false);
  70         if (result instanceof LogicConstantNode) {
  71             LogicConstantNode logicConstantNode = (LogicConstantNode) result;
  72             LogicNode resultLT = CompareNode.tryConstantFold(unsigned ? CanonicalCondition.BT : CanonicalCondition.LT, x, y, constantReflection, isUnorderedLess);
  73             if (resultLT instanceof LogicConstantNode) {
  74                 LogicConstantNode logicConstantNodeLT = (LogicConstantNode) resultLT;
  75                 if (logicConstantNodeLT.getValue()) {
  76                     return ConstantNode.forIntegerKind(kind, -1);
  77                 } else if (logicConstantNode.getValue()) {
  78                     return ConstantNode.forIntegerKind(kind, 0);
  79                 } else {
  80                     return ConstantNode.forIntegerKind(kind, 1);
  81                 }
  82             }
  83         }
  84         return null;
  85     }
  86 
  87     public abstract LogicNode createEqualComparison();
  88 
  89     public abstract LogicNode createEqualComparison(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth, NodeView view);
  90 
  91     public LogicNode createLowerComparison() {
  92         return createLowerComparison(false);
  93     }
  94 
  95     public LogicNode createLowerComparison(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth, NodeView view) {
  96         return createLowerComparison(false, constantReflection, metaAccess, options, smallestCompareWidth, view);
  97     }
  98 
  99     public abstract LogicNode createLowerComparison(boolean swapInputs);
 100 
 101     public abstract LogicNode createLowerComparison(boolean swapInputs, ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options,
 102                     Integer smallestCompareWidth, NodeView view);
 103 }