changeset 14936:159b21f41915

add redundant compare test and fix breakage
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Tue, 01 Apr 2014 22:39:17 -0700
parents 1b96d1a74514
children 3de340bdb8d3
files graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/ConditionalEliminationTest.java graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/ConditionalEliminationPhase.java
diffstat 2 files changed, 65 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/ConditionalEliminationTest.java	Tue Apr 01 19:45:34 2014 -0700
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/ConditionalEliminationTest.java	Tue Apr 01 22:39:17 2014 -0700
@@ -28,6 +28,7 @@
 
 import org.junit.*;
 
+import com.oracle.graal.debug.internal.*;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.calc.*;
 import com.oracle.graal.nodes.java.*;
@@ -256,6 +257,58 @@
         assertEquals(0, graph.getNodes().filter(CheckCastNode.class).count());
     }
 
+    public static int testRedundantComparesSnippet(int[] array) {
+        if (array == null) {
+            return 0;
+        }
+        return array[0] + array[1] + array[2] + array[3];
+    }
+
+    @Test
+    public void testRedundantCompares() {
+        StructuredGraph graph = parse("testRedundantComparesSnippet");
+        CanonicalizerPhase canonicalizer = new CanonicalizerPhase(true);
+        PhaseContext context = new PhaseContext(getProviders(), null);
+
+        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
+        canonicalizer.apply(graph, context);
+        new FloatingReadPhase().apply(graph);
+        new ConditionalEliminationPhase(getMetaAccess()).apply(graph);
+        canonicalizer.apply(graph, context);
+
+        assertEquals(1, graph.getNodes().filter(GuardNode.class).count());
+    }
+
+    public static int testDuplicateNullChecksSnippet(Object a) {
+        if (a == null) {
+            return 2;
+        }
+        try {
+            return ((Integer) a).intValue();
+        } catch (ClassCastException e) {
+            return 0;
+        }
+    }
+
+    @Test
+    @Ignore
+    public void testDuplicateNullChecks() {
+        // This tests whether explicit null checks properly eliminate later null guards. Currently
+        // it's failing.
+        StructuredGraph graph = parse("testDuplicateNullChecksSnippet");
+        CanonicalizerPhase canonicalizer = new CanonicalizerPhase(true);
+        PhaseContext context = new PhaseContext(getProviders(), null);
+
+        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
+        canonicalizer.apply(graph, context);
+        new FloatingReadPhase().apply(graph);
+        new ConditionalEliminationPhase(getMetaAccess()).apply(graph);
+        canonicalizer.apply(graph, context);
+        DebugScope.forceDump(graph, "dup guards");
+
+        assertEquals(1, graph.getNodes().filter(GuardNode.class).count());
+    }
+
     @Test
     @Ignore
     public void testInstanceOfCheckCastLowered() {
--- a/graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/ConditionalEliminationPhase.java	Tue Apr 01 19:45:34 2014 -0700
+++ b/graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/ConditionalEliminationPhase.java	Tue Apr 01 22:39:17 2014 -0700
@@ -548,6 +548,17 @@
                         }
                     }
                 }
+            } else if (guard.condition() instanceof IntegerEqualsNode && guard.negated()) {
+                IntegerEqualsNode equals = (IntegerEqualsNode) guard.condition();
+                GuardedStamp cstamp = state.valueConstraints.get(equals.y());
+                if (cstamp != null && equals.x().isConstant()) {
+                    IntegerStamp stamp = (IntegerStamp) cstamp.getStamp();
+                    if (!stamp.contains(equals.x().asConstant().asLong())) {
+                        // x != n is true if n is outside the range of the stamp
+                        existingGuard = cstamp.getGuard();
+                        Debug.log("existing guard %s %1s proves !%1s", existingGuard, existingGuard.condition(), guard.condition());
+                    }
+                }
             }
 
             if (existingGuard != null) {
@@ -670,7 +681,7 @@
                 // just be registered since they aren't trivially deleteable. Test the other guards
                 // to see if they can be deleted using type constraints.
                 for (GuardNode guard : begin.guards().snapshot()) {
-                    if (provers.contains(guard) || !testExistingGuard(guard) || !testImpliedGuard(guard)) {
+                    if (provers.contains(guard) || !(testExistingGuard(guard) || testImpliedGuard(guard))) {
                         registerCondition(!guard.negated(), guard.condition(), guard);
                     }
                 }