# HG changeset patch # User Christian Humer # Date 1408453169 -7200 # Node ID 2270852ee1ef2205bb226faf2bc52eb0056ac8f4 # Parent fa5e6262059382c95ddfa4db75155681d18ae582# Parent 1051d6e4b61bc6ee701b55d6c4ee54c19056713a Merge. diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/BinaryConditionProfileTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/BinaryConditionProfileTest.java Tue Aug 19 14:59:29 2014 +0200 @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2014, 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.truffle.api.test.utilities; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.*; +import org.junit.experimental.theories.*; +import org.junit.runner.*; + +import com.oracle.truffle.api.utilities.*; + +@RunWith(Theories.class) +public class BinaryConditionProfileTest { + + @DataPoints public static boolean[] data = new boolean[]{true, false}; + + @Test + public void testInitial() { + BinaryConditionProfile profile = ConditionProfile.createBinaryProfile(); + assertThat(profile.wasTrue(), is(false)); + assertThat(profile.wasFalse(), is(false)); + } + + @Theory + public void testProfileOne(boolean value) { + BinaryConditionProfile profile = ConditionProfile.createBinaryProfile(); + boolean result = profile.profile(value); + + assertThat(result, is(value)); + assertThat(profile.wasTrue(), is(value)); + assertThat(profile.wasFalse(), is(!value)); + } + + @Theory + public void testProfileTwo(boolean value0, boolean value1) { + BinaryConditionProfile profile = ConditionProfile.createBinaryProfile(); + boolean result0 = profile.profile(value0); + boolean result1 = profile.profile(value1); + + assertThat(result0, is(value0)); + assertThat(result1, is(value1)); + assertThat(profile.wasTrue(), is(value0 || value1)); + assertThat(profile.wasFalse(), is(!value0 || !value1)); + } + + @Theory + public void testProfileThree(boolean value0, boolean value1, boolean value2) { + BinaryConditionProfile profile = ConditionProfile.createBinaryProfile(); + boolean result0 = profile.profile(value0); + boolean result1 = profile.profile(value1); + boolean result2 = profile.profile(value2); + + assertThat(result0, is(value0)); + assertThat(result1, is(value1)); + assertThat(result2, is(value2)); + assertThat(profile.wasTrue(), is(value0 || value1 || value2)); + assertThat(profile.wasFalse(), is(!value0 || !value1 || !value2)); + } + +} diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/BooleanConditionProfileTest.java --- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/BooleanConditionProfileTest.java Tue Aug 19 13:51:14 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2014, 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.truffle.api.test.utilities; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import org.junit.*; -import org.junit.experimental.theories.*; -import org.junit.runner.*; - -import com.oracle.truffle.api.utilities.*; - -@RunWith(Theories.class) -public class BooleanConditionProfileTest { - - @DataPoints public static boolean[] data = new boolean[]{true, false}; - - @Test - public void testInitial() { - BinaryConditionProfile profile = new BinaryConditionProfile(); - assertThat(profile.wasTrue(), is(false)); - assertThat(profile.wasFalse(), is(false)); - } - - @Theory - public void testProfileOne(boolean value) { - BinaryConditionProfile profile = new BinaryConditionProfile(); - boolean result = profile.profile(value); - - assertThat(result, is(value)); - assertThat(profile.wasTrue(), is(value)); - assertThat(profile.wasFalse(), is(!value)); - } - - @Theory - public void testProfileTwo(boolean value0, boolean value1) { - BinaryConditionProfile profile = new BinaryConditionProfile(); - boolean result0 = profile.profile(value0); - boolean result1 = profile.profile(value1); - - assertThat(result0, is(value0)); - assertThat(result1, is(value1)); - assertThat(profile.wasTrue(), is(value0 || value1)); - assertThat(profile.wasFalse(), is(!value0 || !value1)); - } - - @Theory - public void testProfileThree(boolean value0, boolean value1, boolean value2) { - BinaryConditionProfile profile = new BinaryConditionProfile(); - boolean result0 = profile.profile(value0); - boolean result1 = profile.profile(value1); - boolean result2 = profile.profile(value2); - - assertThat(result0, is(value0)); - assertThat(result1, is(value1)); - assertThat(result2, is(value2)); - assertThat(profile.wasTrue(), is(value0 || value1 || value2)); - assertThat(profile.wasFalse(), is(!value0 || !value1 || !value2)); - } - -} diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/CountingConditionProfileTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/CountingConditionProfileTest.java Tue Aug 19 14:59:29 2014 +0200 @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2014, 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.truffle.api.test.utilities; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.*; +import org.junit.experimental.theories.*; +import org.junit.runner.*; + +import com.oracle.truffle.api.utilities.*; + +@RunWith(Theories.class) +public class CountingConditionProfileTest { + + @DataPoints public static boolean[] data = new boolean[]{true, false}; + + @Test + public void testInitial() { + CountingConditionProfile profile = ConditionProfile.createCountingProfile(); + assertThat(profile.getTrueCount(), is(0)); + assertThat(profile.getFalseCount(), is(0)); + } + + @Theory + public void testProfileOne(boolean value) { + CountingConditionProfile profile = ConditionProfile.createCountingProfile(); + boolean result = profile.profile(value); + + assertThat(result, is(value)); + assertThat(profile.getTrueCount(), is(value ? 1 : 0)); + assertThat(profile.getFalseCount(), is(!value ? 1 : 0)); + } + + @Theory + public void testProfileTwo(boolean value0, boolean value1) { + CountingConditionProfile profile = ConditionProfile.createCountingProfile(); + boolean result0 = profile.profile(value0); + boolean result1 = profile.profile(value1); + + assertThat(result0, is(value0)); + assertThat(result1, is(value1)); + assertThat(profile.getTrueCount(), is((value0 ? 1 : 0) + (value1 ? 1 : 0))); + assertThat(profile.getFalseCount(), is((!value0 ? 1 : 0) + (!value1 ? 1 : 0))); + } + + @Theory + public void testProfileThree(boolean value0, boolean value1, boolean value2) { + CountingConditionProfile profile = ConditionProfile.createCountingProfile(); + boolean result0 = profile.profile(value0); + boolean result1 = profile.profile(value1); + boolean result2 = profile.profile(value2); + + assertThat(result0, is(value0)); + assertThat(result1, is(value1)); + assertThat(result2, is(value2)); + assertThat(profile.getTrueCount(), is((value0 ? 1 : 0) + (value1 ? 1 : 0) + (value2 ? 1 : 0))); + assertThat(profile.getFalseCount(), is((!value0 ? 1 : 0) + (!value1 ? 1 : 0) + (!value2 ? 1 : 0))); + } + +} diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/IntegerConditionProfileTest.java --- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/IntegerConditionProfileTest.java Tue Aug 19 13:51:14 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2014, 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.truffle.api.test.utilities; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import org.junit.*; -import org.junit.experimental.theories.*; -import org.junit.runner.*; - -import com.oracle.truffle.api.utilities.*; - -@RunWith(Theories.class) -public class IntegerConditionProfileTest { - - @DataPoints public static boolean[] data = new boolean[]{true, false}; - - @Test - public void testInitial() { - CountingConditionProfile profile = new CountingConditionProfile(); - assertThat(profile.getTrueCount(), is(0)); - assertThat(profile.getFalseCount(), is(0)); - } - - @Theory - public void testProfileOne(boolean value) { - CountingConditionProfile profile = new CountingConditionProfile(); - boolean result = profile.profile(value); - - assertThat(result, is(value)); - assertThat(profile.getTrueCount(), is(value ? 1 : 0)); - assertThat(profile.getFalseCount(), is(!value ? 1 : 0)); - } - - @Theory - public void testProfileTwo(boolean value0, boolean value1) { - CountingConditionProfile profile = new CountingConditionProfile(); - boolean result0 = profile.profile(value0); - boolean result1 = profile.profile(value1); - - assertThat(result0, is(value0)); - assertThat(result1, is(value1)); - assertThat(profile.getTrueCount(), is((value0 ? 1 : 0) + (value1 ? 1 : 0))); - assertThat(profile.getFalseCount(), is((!value0 ? 1 : 0) + (!value1 ? 1 : 0))); - } - - @Theory - public void testProfileThree(boolean value0, boolean value1, boolean value2) { - CountingConditionProfile profile = new CountingConditionProfile(); - boolean result0 = profile.profile(value0); - boolean result1 = profile.profile(value1); - boolean result2 = profile.profile(value2); - - assertThat(result0, is(value0)); - assertThat(result1, is(value1)); - assertThat(result2, is(value2)); - assertThat(profile.getTrueCount(), is((value0 ? 1 : 0) + (value1 ? 1 : 0) + (value2 ? 1 : 0))); - assertThat(profile.getFalseCount(), is((!value0 ? 1 : 0) + (!value1 ? 1 : 0) + (!value2 ? 1 : 0))); - } - -} diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/BinaryConditionProfile.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/BinaryConditionProfile.java Tue Aug 19 13:51:14 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/BinaryConditionProfile.java Tue Aug 19 14:59:29 2014 +0200 @@ -31,28 +31,17 @@ * Utility class to speculate on conditions to be never true or to be never false. Condition * profiles are intended to be used as part of if conditions. * - * Example usage: - * - *
- * private final ConditionProfile zero = new BooleanConditionProfile();
- *
- * int value = ...;
- * if (zero.profile(value == 0)) {
- *   return 0;
- * } else {
- *   return value;
- * }
- *
- * 
- * - * @see ConditionProfile - * @see CountingConditionProfile + * @see ConditionProfile#createBinaryProfile() */ -public class BinaryConditionProfile extends ConditionProfile { +public final class BinaryConditionProfile extends ConditionProfile { @CompilationFinal private boolean wasTrue; @CompilationFinal private boolean wasFalse; + BinaryConditionProfile() { + /* package protected constructor */ + } + @Override public boolean profile(boolean value) { if (value) { diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ConditionProfile.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ConditionProfile.java Tue Aug 19 13:51:14 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ConditionProfile.java Tue Aug 19 14:59:29 2014 +0200 @@ -24,6 +24,8 @@ */ package com.oracle.truffle.api.utilities; +import com.oracle.truffle.api.*; + /** * Abstract utility class to speculate on conditions. Condition profiles are intended to be used as * part of if conditions. @@ -31,8 +33,8 @@ * Example usage: * *
- * private final ConditionProfile zero = ...;
- * 
+ * private final ConditionProfile zero = ConditionProfile.createBinaryProfile();
+ *
  * int value = ...;
  * if (zero.profile(value == 0)) {
  *   return 0;
@@ -42,11 +44,37 @@
  *
  * 
* - * @see BinaryConditionProfile - * @see CountingConditionProfile + * @see #createCountingProfile() + * @see #createBinaryProfile() */ public abstract class ConditionProfile { public abstract boolean profile(boolean value); + /** + * Returns a {@link ConditionProfile} that speculates on conditions to be never + * true or to be never false. Additionally to a binary profile this + * method returns a condition profile that also counts the number of times the condition was + * true and false. This information is reported to the underlying optimization system using + * {@link CompilerDirectives#injectBranchProbability(double, boolean)}. Condition profiles are + * intended to be used as part of if conditions. + * + * @see ConditionProfile + * @see #createBinaryProfile() + */ + public static CountingConditionProfile createCountingProfile() { + return new CountingConditionProfile(); + } + + /** + * REturns a {@link ConditionProfile} that speculates on conditions to be never true or to be + * never false. Condition profiles are intended to be used as part of if conditions. + * + * @see ConditionProfile + * @see ConditionProfile#createBinaryProfile() + */ + public static BinaryConditionProfile createBinaryProfile() { + return new BinaryConditionProfile(); + } + } diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/CountingConditionProfile.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/CountingConditionProfile.java Tue Aug 19 13:51:14 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/CountingConditionProfile.java Tue Aug 19 14:59:29 2014 +0200 @@ -34,28 +34,17 @@ * optimization system using {@link CompilerDirectives#injectBranchProbability(double, boolean)}. * Condition profiles are intended to be used as part of if conditions. * - * Example usage: - * - *
- * private final ConditionProfile zero = new IntegerConditionProfile();
- * 
- * int value = ...;
- * if (zero.profile(value == 0)) {
- *   return 0;
- * } else {
- *   return value;
- * }
- *
- * 
- * - * @see ConditionProfile - * @see CountingConditionProfile + * @see ConditionProfile#createCountingProfile() */ -public class CountingConditionProfile extends ConditionProfile { +public final class CountingConditionProfile extends ConditionProfile { @CompilationFinal private int trueCount; @CompilationFinal private int falseCount; + CountingConditionProfile() { + /* package protected constructor */ + } + @Override public boolean profile(boolean value) { if (value) { diff -r 1051d6e4b61b -r 2270852ee1ef graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLIfNode.java --- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLIfNode.java Tue Aug 19 13:51:14 2014 +0200 +++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLIfNode.java Tue Aug 19 14:59:29 2014 +0200 @@ -52,7 +52,7 @@ * (as opposed to {@link BinaryConditionProfile} implementation) transmits the probability of * the condition to be true to the compiler. */ - private final ConditionProfile condition = new CountingConditionProfile(); + private final ConditionProfile condition = ConditionProfile.createCountingProfile(); public SLIfNode(SourceSection src, SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) { super(src);