# HG changeset patch # User Tom Rodriguez # Date 1431462421 25200 # Node ID 1b3270c6d87cf774241233a711bf91c0f16f3340 # Parent f85738837dae3e7392d91b6c9a617427f0b6a5ff Fix materialization of instanceof when sharing instantiation diff -r f85738837dae -r 1b3270c6d87c graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/InstanceOfTest.java --- a/graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/InstanceOfTest.java Tue May 12 21:36:57 2015 +0200 +++ b/graal/com.oracle.graal.replacements.test/src/com/oracle/graal/replacements/test/InstanceOfTest.java Tue May 12 13:27:01 2015 -0700 @@ -412,4 +412,23 @@ test("arrayCopyTypeName", (Object) new Object[]{"one", "two", "three"}); test("arrayCopyTypeName", (Object) new String[]{"one", "two", "three"}); } + + public int conditionalInstantiation(Object o) { + int total = 0; + if (o instanceof CharSequence) { + if (o instanceof StringBuilder || o instanceof String) { + total = 9; + } + total += (o instanceof String ? 2 : 1); + } + + return total; + } + + @Test + public void testInstantiation() { + test("conditionalInstantiation", "foo"); + test("conditionalInstantiation", new StringBuilder()); + test("conditionalInstantiation", 1); + } } diff -r f85738837dae -r 1b3270c6d87c graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/InstanceOfSnippetsTemplates.java --- a/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/InstanceOfSnippetsTemplates.java Tue May 12 21:36:57 2015 +0200 +++ b/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/InstanceOfSnippetsTemplates.java Tue May 12 13:27:01 2015 -0700 @@ -96,7 +96,17 @@ protected InstanceOfUsageReplacer createReplacer(FloatingNode instanceOf, Instantiation instantiation, Node usage, final StructuredGraph graph) { InstanceOfUsageReplacer replacer; if (usage instanceof IfNode || usage instanceof FixedGuardNode || usage instanceof ShortCircuitOrNode || usage instanceof GuardingPiNode || usage instanceof ConditionAnchorNode) { - replacer = new NonMaterializationUsageReplacer(instantiation, ConstantNode.forInt(1, graph), ConstantNode.forInt(0, graph), instanceOf, usage); + ValueNode trueValue = ConstantNode.forInt(1, graph); + ValueNode falseValue = ConstantNode.forInt(0, graph); + if (instantiation.isInitialized() && (trueValue != instantiation.trueValue || falseValue != instantiation.falseValue)) { + /* + * This code doesn't really care what values are used so adopt the values from the + * previous instantiation. + */ + trueValue = instantiation.trueValue; + falseValue = instantiation.falseValue; + } + replacer = new NonMaterializationUsageReplacer(instantiation, trueValue, falseValue, instanceOf, usage); } else { assert usage instanceof ConditionalNode : "unexpected usage of " + instanceOf + ": " + usage; ConditionalNode c = (ConditionalNode) usage;