001/* 002 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.nodes.calc; 024 025import jdk.internal.jvmci.meta.*; 026 027import com.oracle.graal.compiler.common.calc.*; 028import com.oracle.graal.compiler.common.type.*; 029import com.oracle.graal.graph.*; 030import com.oracle.graal.graph.spi.*; 031import com.oracle.graal.nodeinfo.*; 032import com.oracle.graal.nodes.*; 033import com.oracle.graal.nodes.spi.*; 034 035/** 036 * Returns -1, 0, or 1 if either x < y, x == y, or x > y. If the comparison is undecided (one 037 * of the inputs is NaN), the result is 1 if isUnorderedLess is false and -1 if isUnorderedLess is 038 * true. 039 */ 040@NodeInfo 041public final class NormalizeCompareNode extends BinaryNode implements Lowerable { 042 043 public static final NodeClass<NormalizeCompareNode> TYPE = NodeClass.create(NormalizeCompareNode.class); 044 protected final boolean isUnorderedLess; 045 046 public NormalizeCompareNode(ValueNode x, ValueNode y, boolean isUnorderedLess) { 047 super(TYPE, StampFactory.forKind(Kind.Int), x, y); 048 this.isUnorderedLess = isUnorderedLess; 049 } 050 051 public static ValueNode create(ValueNode x, ValueNode y, boolean isUnorderedLess, ConstantReflectionProvider constantReflection) { 052 LogicNode result = CompareNode.tryConstantFold(Condition.EQ, x, y, constantReflection, false); 053 if (result instanceof LogicConstantNode) { 054 LogicConstantNode logicConstantNode = (LogicConstantNode) result; 055 LogicNode resultLT = CompareNode.tryConstantFold(Condition.LT, x, y, constantReflection, isUnorderedLess); 056 if (resultLT instanceof LogicConstantNode) { 057 LogicConstantNode logicConstantNodeLT = (LogicConstantNode) resultLT; 058 if (logicConstantNodeLT.getValue()) { 059 return ConstantNode.forInt(-1); 060 } else if (logicConstantNode.getValue()) { 061 return ConstantNode.forInt(0); 062 } else { 063 return ConstantNode.forInt(1); 064 } 065 } 066 } 067 068 return new NormalizeCompareNode(x, y, isUnorderedLess); 069 } 070 071 @Override 072 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 073 // nothing to do 074 return this; 075 } 076 077 @Override 078 public void lower(LoweringTool tool) { 079 LogicNode equalComp; 080 LogicNode lessComp; 081 if (getX().stamp() instanceof FloatStamp) { 082 equalComp = graph().unique(FloatEqualsNode.create(getX(), getY(), tool.getConstantReflection())); 083 lessComp = graph().unique(FloatLessThanNode.create(getX(), getY(), isUnorderedLess, tool.getConstantReflection())); 084 } else { 085 equalComp = graph().unique(IntegerEqualsNode.create(getX(), getY(), tool.getConstantReflection())); 086 lessComp = graph().unique(IntegerLessThanNode.create(getX(), getY(), tool.getConstantReflection())); 087 } 088 089 ConditionalNode equalValue = graph().unique(new ConditionalNode(equalComp, ConstantNode.forInt(0, graph()), ConstantNode.forInt(1, graph()))); 090 ConditionalNode value = graph().unique(new ConditionalNode(lessComp, ConstantNode.forInt(-1, graph()), equalValue)); 091 092 graph().replaceFloating(this, value); 093 } 094}