changeset 4550:e065aa86d077

Lower NormalizeCompareNode in the front end to avoid LIR operation.
author Christian Wimmer <Christian.Wimmer@Oracle.com>
date Thu, 09 Feb 2012 17:37:53 -0800
parents 2bab0e314b5c
children daba89671d29
files graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/gen/LIRGenerator.java graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/target/amd64/AMD64LIRGenerator.java graal/com.oracle.max.graal.hotspot/src/com/oracle/max/graal/hotspot/ri/HotSpotXirGenerator.java graal/com.oracle.max.graal.lir.amd64/src/com/oracle/max/graal/lir/amd64/AMD64CompareToIntOpcode.java graal/com.oracle.max.graal.lir.amd64/src/com/oracle/max/graal/lir/amd64/AMD64ControlFlow.java graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/MaterializeNode.java graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/CompareNode.java graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/Condition.java graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/NormalizeCompareNode.java graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/spi/LIRGeneratorTool.java
diffstat 10 files changed, 20 insertions(+), 181 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/gen/LIRGenerator.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/gen/LIRGenerator.java	Thu Feb 09 17:37:53 2012 -0800
@@ -820,9 +820,6 @@
     }
 
     public Variable emitConditional(BooleanNode node, CiValue trueValue, CiValue falseValue) {
-        assert trueValue instanceof CiConstant && (trueValue.kind.stackKind() == CiKind.Int || trueValue.kind == CiKind.Long);
-        assert falseValue instanceof CiConstant && (falseValue.kind.stackKind() == CiKind.Int || trueValue.kind == CiKind.Long);
-
         if (node instanceof NullCheckNode) {
             return emitNullCheckConditional((NullCheckNode) node, trueValue, falseValue);
         } else if (node instanceof CompareNode) {
--- a/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/target/amd64/AMD64LIRGenerator.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/target/amd64/AMD64LIRGenerator.java	Thu Feb 09 17:37:53 2012 -0800
@@ -26,7 +26,6 @@
 import static com.oracle.max.cri.ci.CiValueUtil.*;
 import static com.oracle.max.graal.lir.amd64.AMD64Arithmetic.*;
 import static com.oracle.max.graal.lir.amd64.AMD64Compare.*;
-import static com.oracle.max.graal.lir.amd64.AMD64CompareToIntOpcode.*;
 
 import java.util.*;
 
@@ -599,27 +598,4 @@
             postGCWriteBarrier(address.base, newValue);
         }
     }
-
-    // TODO The class NormalizeCompareNode should be lowered away in the front end, since the code generated is long and uses branches anyway.
-    @Override
-    public void visitNormalizeCompare(NormalizeCompareNode x) {
-        emitCompare(operand(x.x()), operand(x.y()));
-        Variable result = newVariable(x.kind());
-        switch (x.x().kind()){
-            case Float:
-            case Double:
-                if (x.isUnorderedLess) {
-                    append(CMP2INT_UL.create(result));
-                } else {
-                    append(CMP2INT_UG.create(result));
-                }
-                break;
-            case Long:
-                append(CMP2INT.create(result));
-                break;
-            default:
-                throw GraalInternalError.shouldNotReachHere();
-        }
-        setResult(x, result);
-    }
 }
--- a/graal/com.oracle.max.graal.hotspot/src/com/oracle/max/graal/hotspot/ri/HotSpotXirGenerator.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.hotspot/src/com/oracle/max/graal/hotspot/ri/HotSpotXirGenerator.java	Thu Feb 09 17:37:53 2012 -0800
@@ -762,8 +762,8 @@
             XirOperand result = asm.restart(CiKind.Int);
             XirParameter object = asm.createInputParameter("object", CiKind.Object);
             final XirOperand hub = is(EXACT_HINTS, flags) ? null : asm.createConstantInputParameter("hub", CiKind.Object);
-            XirOperand trueValue = asm.createConstantInputParameter("trueValue", CiKind.Int);
-            XirOperand falseValue = asm.createConstantInputParameter("falseValue", CiKind.Int);
+            XirOperand trueValue = asm.createInputParameter("trueValue", CiKind.Int);
+            XirOperand falseValue = asm.createInputParameter("falseValue", CiKind.Int);
 
             XirOperand objHub = asm.createTemp("objHub", CiKind.Object);
 
--- a/graal/com.oracle.max.graal.lir.amd64/src/com/oracle/max/graal/lir/amd64/AMD64CompareToIntOpcode.java	Thu Feb 09 23:54:33 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,107 +0,0 @@
-/*
- * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.max.graal.lir.amd64;
-
-import static com.oracle.max.cri.ci.CiValueUtil.*;
-
-import java.util.*;
-
-import com.oracle.max.asm.*;
-import com.oracle.max.asm.target.amd64.AMD64Assembler.ConditionFlag;
-import com.oracle.max.asm.target.amd64.*;
-import com.oracle.max.cri.ci.*;
-import com.oracle.max.graal.graph.*;
-import com.oracle.max.graal.lir.*;
-import com.oracle.max.graal.lir.asm.*;
-
-/**
- * Implementation of the Java bytecodes that compare a long, float, or double value and produce the
- * integer constants -1, 0, 1 on less, equal, or greater, respectively.  For floating point compares,
- * unordered can be either greater {@link #CMP2INT_UG} or less {@link #CMP2INT_UL}.
- */
-public enum AMD64CompareToIntOpcode {
-    CMP2INT, CMP2INT_UG, CMP2INT_UL;
-
-    public LIRInstruction create(CiValue result) {
-        CiValue[] outputs = new CiValue[] {result};
-
-        return new AMD64LIRInstruction(this, outputs, null, LIRInstruction.NO_OPERANDS, LIRInstruction.NO_OPERANDS, LIRInstruction.NO_OPERANDS) {
-            @Override
-            public void emitCode(TargetMethodAssembler tasm, AMD64MacroAssembler masm) {
-                emit(masm, output(0));
-            }
-
-            @Override
-            protected EnumSet<OperandFlag> flagsFor(OperandMode mode, int index) {
-                if (mode == OperandMode.Output && index == 0) {
-                    return EnumSet.of(OperandFlag.Register);
-                }
-                throw GraalInternalError.shouldNotReachHere();
-            }
-        };
-    }
-
-    private void emit(AMD64MacroAssembler masm, CiValue result) {
-        CiRegister dest = asIntReg(result);
-        Label high = new Label();
-        Label low = new Label();
-        Label done = new Label();
-
-        // comparison is done by a separate LIR instruction before
-        switch (this) {
-            case CMP2INT:
-                masm.jcc(ConditionFlag.greater, high);
-                masm.jcc(ConditionFlag.less, low);
-                break;
-            case CMP2INT_UG:
-                masm.jcc(ConditionFlag.parity, high);
-                masm.jcc(ConditionFlag.above, high);
-                masm.jcc(ConditionFlag.below, low);
-                break;
-            case CMP2INT_UL:
-                masm.jcc(ConditionFlag.parity, low);
-                masm.jcc(ConditionFlag.above, high);
-                masm.jcc(ConditionFlag.below, low);
-                break;
-            default:
-                throw GraalInternalError.shouldNotReachHere();
-        }
-
-        // equal -> 0
-        masm.xorptr(dest, dest);
-        masm.jmp(done);
-
-        // greater -> 1
-        masm.bind(high);
-        masm.xorptr(dest, dest);
-        masm.incrementl(dest, 1);
-        masm.jmp(done);
-
-        // less -> -1
-        masm.bind(low);
-        masm.xorptr(dest, dest);
-        masm.decrementl(dest, 1);
-
-        masm.bind(done);
-    }
-}
--- a/graal/com.oracle.max.graal.lir.amd64/src/com/oracle/max/graal/lir/amd64/AMD64ControlFlow.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.lir.amd64/src/com/oracle/max/graal/lir/amd64/AMD64ControlFlow.java	Thu Feb 09 17:37:53 2012 -0800
@@ -354,10 +354,10 @@
         switch (cond) {
             case EQ: return ConditionFlag.equal;
             case NE: return ConditionFlag.notEqual;
-            case BT: return ConditionFlag.below;
-            case BE: return ConditionFlag.belowEqual;
-            case AE: return ConditionFlag.aboveEqual;
-            case AT: return ConditionFlag.above;
+            case LT: return ConditionFlag.below;
+            case LE: return ConditionFlag.belowEqual;
+            case GE: return ConditionFlag.aboveEqual;
+            case GT: return ConditionFlag.above;
             default: throw GraalInternalError.shouldNotReachHere();
         }
     }
--- a/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/MaterializeNode.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/MaterializeNode.java	Thu Feb 09 17:37:53 2012 -0800
@@ -31,7 +31,7 @@
         super(condition, trueValue, falseValue);
     }
 
-    public static MaterializeNode create(BooleanNode condition, Graph graph, ConstantNode trueValue, ConstantNode falseValue) {
+    public static MaterializeNode create(BooleanNode condition, Graph graph, ValueNode trueValue, ValueNode falseValue) {
         MaterializeNode result = new MaterializeNode(condition, trueValue, falseValue);
         return graph.unique(result);
 
--- a/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/CompareNode.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/CompareNode.java	Thu Feb 09 17:37:53 2012 -0800
@@ -146,17 +146,6 @@
     private ValueNode optimizeNormalizeCmp(CiConstant constant, NormalizeCompareNode normalizeNode) {
         if (constant.kind == CiKind.Int && constant.asInt() == 0) {
             Condition cond = condition();
-            if (normalizeNode.x().kind().isFloatOrDouble()) {
-                switch (cond) {
-                    case LT: cond = Condition.BT; break;
-                    case LE: cond = Condition.BE; break;
-                    case GE: cond = Condition.AE; break;
-                    case GT: cond = Condition.AT; break;
-                }
-            }
-            if (normalizeNode == y()) {
-                cond = cond.mirror();
-            }
             boolean isLess = cond == Condition.LE || cond == Condition.LT || cond == Condition.BE || cond == Condition.BT;
             boolean canonUnorderedIsTrue = cond != Condition.EQ && (cond == Condition.NE || !(isLess ^ normalizeNode.isUnorderedLess));
             CompareNode result = graph().unique(new CompareNode(normalizeNode.x(), cond, canonUnorderedIsTrue, normalizeNode.y()));
--- a/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/Condition.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/Condition.java	Thu Feb 09 17:37:53 2012 -0800
@@ -227,13 +227,9 @@
                 switch (this) {
                     case EQ: return x == y;
                     case NE: return x != y;
-                    case BT:
                     case LT: return x < y;
-                    case BE:
                     case LE: return x <= y;
-                    case AT:
                     case GT: return x > y;
-                    case AE:
                     case GE: return x >= y;
                 }
             }
@@ -246,13 +242,9 @@
                 switch (this) {
                     case EQ: return x == y;
                     case NE: return x != y;
-                    case BT:
                     case LT: return x < y;
-                    case BE:
                     case LE: return x <= y;
-                    case AT:
                     case GT: return x > y;
-                    case AE:
                     case GE: return x >= y;
                 }
             }
--- a/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/NormalizeCompareNode.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/calc/NormalizeCompareNode.java	Thu Feb 09 17:37:53 2012 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,21 +22,15 @@
  */
 package com.oracle.max.graal.nodes.calc;
 
-import java.util.*;
-
 import com.oracle.max.cri.ci.*;
+import com.oracle.max.graal.cri.*;
 import com.oracle.max.graal.nodes.*;
 import com.oracle.max.graal.nodes.spi.*;
 
 /**
- * Returns -1, 0, or 1 if either x > y, x == y, or x < y.
+ * Returns -1, 0, or 1 if either x < y, x == y, or x > y.
  */
-// TODO(cwi): Since the machine code representation of this node uses branches and individual constant assignemnts anyway,
-// it should be lowered to explicit control flow nodes.  This removes AMD64CompareToIntOpcode.
-// For code generated by javac, the opcodes are followed by a branch instruction immediately, so in all cases that matter
-// this node is canonicalized away.
-// Consider moving this node and the code that does the lowering / canonicalization in a Java-specific project.
-public final class NormalizeCompareNode extends BinaryNode implements LIRLowerable {
+public final class NormalizeCompareNode extends BinaryNode implements Lowerable {
     @Data public final boolean isUnorderedLess;
 
     /**
@@ -51,14 +45,15 @@
     }
 
     @Override
-    public void generate(LIRGeneratorTool gen) {
-        gen.visitNormalizeCompare(this);
-    }
+    public void lower(CiLoweringTool tool) {
+        StructuredGraph graph = (StructuredGraph) graph();
 
-    @Override
-    public Map<Object, Object> getDebugProperties() {
-        Map<Object, Object> properties = super.getDebugProperties();
-        properties.put("isUnorderedLess", isUnorderedLess);
-        return properties;
+        CompareNode equalComp = graph.unique(new CompareNode(x(), Condition.EQ, false, y()));
+        MaterializeNode equalValue = MaterializeNode.create(equalComp, graph, ConstantNode.forInt(0, graph), ConstantNode.forInt(1, graph));
+
+        CompareNode lessComp = graph.unique(new CompareNode(x(), Condition.LT, isUnorderedLess, y()));
+        MaterializeNode value =  MaterializeNode.create(lessComp, graph, ConstantNode.forInt(-1, graph), equalValue);
+
+        graph.replaceFloating(this, value);
     }
 }
--- a/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/spi/LIRGeneratorTool.java	Thu Feb 09 23:54:33 2012 +0100
+++ b/graal/com.oracle.max.graal.nodes/src/com/oracle/max/graal/nodes/spi/LIRGeneratorTool.java	Thu Feb 09 17:37:53 2012 -0800
@@ -103,9 +103,6 @@
     // * The actual compare-and-swap
     public abstract void visitCompareAndSwap(CompareAndSwapNode i);
 
-    // The class NormalizeCompareNode should be lowered away in the front end, since the code generated is long and uses branches anyway.
-    public abstract void visitNormalizeCompare(NormalizeCompareNode i);
-
     // Functionality that is currently implemented in XIR.
     // These methods will go away eventually when lowering is done via snippets in the front end.
     public abstract void visitArrayLength(ArrayLengthNode i);