# HG changeset patch # User Tom Rodriguez # Date 1396417157 25200 # Node ID 159b21f4191502b4b76eb5ecf7c0595ff2c39c9e # Parent 1b96d1a7451467297ed8ff81355852d00d62b81b add redundant compare test and fix breakage diff -r 1b96d1a74514 -r 159b21f41915 graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/ConditionalEliminationTest.java --- 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() { diff -r 1b96d1a74514 -r 159b21f41915 graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/ConditionalEliminationPhase.java --- 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); } }