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.graph.spi.CanonicalizerTool; 35 import org.graalvm.compiler.nodeinfo.NodeCycles; 36 import org.graalvm.compiler.nodeinfo.NodeInfo; 37 import org.graalvm.compiler.nodes.ConstantNode; 38 import org.graalvm.compiler.nodes.LogicConstantNode; 39 import org.graalvm.compiler.nodes.LogicNode; 40 import org.graalvm.compiler.nodes.NodeView; 41 import org.graalvm.compiler.nodes.ValueNode; 42 43 import jdk.vm.ci.meta.ConstantReflectionProvider; 44 import jdk.vm.ci.meta.JavaKind; 45 46 /** 47 * Returns -1, 0, or 1 if either x < y, x == y, or x > y. If the comparison is undecided (one 48 * of the inputs is NaN), the result is 1 if isUnorderedLess is false and -1 if isUnorderedLess is 49 * true. 50 */ 51 @NodeInfo(cycles = NodeCycles.CYCLES_2, size = SIZE_2) 52 public final class NormalizeCompareNode extends BinaryNode implements IterableNodeType { 53 54 public static final NodeClass<NormalizeCompareNode> TYPE = NodeClass.create(NormalizeCompareNode.class); 55 protected final boolean isUnorderedLess; 56 57 public NormalizeCompareNode(ValueNode x, ValueNode y, JavaKind kind, boolean isUnorderedLess) { 58 super(TYPE, StampFactory.forInteger(kind, -1, 1), x, y); 59 this.isUnorderedLess = isUnorderedLess; 60 } 61 62 public static ValueNode create(ValueNode x, ValueNode y, boolean isUnorderedLess, JavaKind kind, ConstantReflectionProvider constantReflection) { 63 ValueNode result = tryConstantFold(x, y, isUnorderedLess, kind, constantReflection); 64 if (result != null) { 65 return result; 66 } 67 68 return new NormalizeCompareNode(x, y, kind, isUnorderedLess); 69 } 70 71 protected static ValueNode tryConstantFold(ValueNode x, ValueNode y, boolean isUnorderedLess, JavaKind kind, ConstantReflectionProvider constantReflection) { 72 LogicNode result = CompareNode.tryConstantFold(CanonicalCondition.EQ, x, y, null, false); 73 if (result instanceof LogicConstantNode) { 74 LogicConstantNode logicConstantNode = (LogicConstantNode) result; 75 LogicNode resultLT = CompareNode.tryConstantFold(CanonicalCondition.LT, x, y, constantReflection, isUnorderedLess); 76 if (resultLT instanceof LogicConstantNode) { 77 LogicConstantNode logicConstantNodeLT = (LogicConstantNode) resultLT; 78 if (logicConstantNodeLT.getValue()) { 79 return ConstantNode.forIntegerKind(kind, -1); 80 } else if (logicConstantNode.getValue()) { 81 return ConstantNode.forIntegerKind(kind, 0); 82 } else { 83 return ConstantNode.forIntegerKind(kind, 1); 84 } 85 } 86 } 87 return null; 88 } 89 90 @Override 91 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 92 NodeView view = NodeView.from(tool); 93 ValueNode result = tryConstantFold(x, y, isUnorderedLess, stamp(view).getStackKind(), tool.getConstantReflection()); 94 if (result != null) { 95 return result; 96 } 97 return this; 98 } 99 100 @Override 101 public boolean inferStamp() { 102 return false; 103 } 104 105 @Override 106 public Stamp foldStamp(Stamp stampX, Stamp stampY) { 107 return stamp(NodeView.DEFAULT); 108 } 109 110 public boolean isUnorderedLess() { 111 return isUnorderedLess; 112 } 113 }