changeset 16828:2834af86f398

Truffle: new condition profile primitives.
author Christian Humer <christian.humer@gmail.com>
date Wed, 13 Aug 2014 20:44:36 +0200
parents 5e647663b77e
children e3724f25056a
files CHANGELOG.md graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/BooleanConditionProfileTest.java graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/IntegerConditionProfileTest.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/BooleanConditionProfile.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ConditionProfile.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/IntegerConditionProfile.java
diffstat 6 files changed, 392 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGELOG.md	Wed Aug 13 21:57:12 2014 +0200
+++ b/CHANGELOG.md	Wed Aug 13 20:44:36 2014 +0200
@@ -22,6 +22,7 @@
 * New flag -G:TruffleSplittingStartCallCount=3 which sets the number of minimal calls until splitting is performed.
 * New flag -G:-TruffleSplittingAggressive if enabled splits every function call.
 * Added isVisited method for BranchProfile.
+* Added new ConditionProfile, BooleanConditionProfile and IntegerConditionProfile utility classes to profile if conditions.
 * ...
 
 ## Version 0.3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/BooleanConditionProfileTest.java	Wed Aug 13 20:44:36 2014 +0200
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2013, 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() {
+        BooleanConditionProfile profile = new BooleanConditionProfile();
+        assertThat(profile.wasTrue(), is(false));
+        assertThat(profile.wasFalse(), is(false));
+    }
+
+    @Theory
+    public void testProfileOne(boolean value) {
+        BooleanConditionProfile profile = new BooleanConditionProfile();
+        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) {
+        BooleanConditionProfile profile = new BooleanConditionProfile();
+        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) {
+        BooleanConditionProfile profile = new BooleanConditionProfile();
+        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));
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/IntegerConditionProfileTest.java	Wed Aug 13 20:44:36 2014 +0200
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2013, 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() {
+        IntegerConditionProfile profile = new IntegerConditionProfile();
+        assertThat(profile.getTrueCount(), is(0));
+        assertThat(profile.getFalseCount(), is(0));
+    }
+
+    @Theory
+    public void testProfileOne(boolean value) {
+        IntegerConditionProfile profile = new IntegerConditionProfile();
+        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) {
+        IntegerConditionProfile profile = new IntegerConditionProfile();
+        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) {
+        IntegerConditionProfile profile = new IntegerConditionProfile();
+        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)));
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/BooleanConditionProfile.java	Wed Aug 13 20:44:36 2014 +0200
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2013, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.utilities;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.CompilerDirectives.*;
+
+/**
+ * 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:
+ *
+ * <pre>
+ * private final ConditionProfile zero = new BooleanConditionProfile();
+ * 
+ * int value = ...;
+ * if (zero.profile(value == 0)) {
+ *   return 0;
+ * } else {
+ *   return value;
+ * }
+ *
+ * </pre>
+ *
+ * @see ConditionProfile
+ * @see IntegerConditionProfile
+ */
+public class BooleanConditionProfile extends ConditionProfile {
+
+    @CompilationFinal private boolean wasTrue;
+    @CompilationFinal private boolean wasFalse;
+
+    @Override
+    public boolean profile(boolean value) {
+        if (value) {
+            if (!wasTrue) {
+                CompilerDirectives.transferToInterpreterAndInvalidate();
+                wasTrue = true;
+            }
+        } else {
+            if (!wasFalse) {
+                CompilerDirectives.transferToInterpreterAndInvalidate();
+                wasFalse = true;
+            }
+        }
+        return value;
+    }
+
+    public boolean wasTrue() {
+        return wasTrue;
+    }
+
+    public boolean wasFalse() {
+        return wasFalse;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%s(wasTrue=%s, wasFalse=%s)@%x", getClass().getSimpleName(), wasTrue, wasFalse, hashCode());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ConditionProfile.java	Wed Aug 13 20:44:36 2014 +0200
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2013, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.utilities;
+
+/**
+ * Abstract utility class to speculate on conditions. Condition profiles are intended to be used as
+ * part of if conditions.
+ *
+ * Example usage:
+ *
+ * <pre>
+ * private final ConditionProfile zero = ...;
+ *
+ * int value = ...;
+ * if (zero.profile(value == 0)) {
+ *   return 0;
+ * } else {
+ *   return value;
+ * }
+ *
+ * </pre>
+ *
+ * @see BooleanConditionProfile
+ * @see IntegerConditionProfile
+ */
+public abstract class ConditionProfile {
+
+    public abstract boolean profile(boolean value);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/IntegerConditionProfile.java	Wed Aug 13 20:44:36 2014 +0200
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2013, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.utilities;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.CompilerDirectives.*;
+
+/**
+ * Utility class to speculate on conditions to be never true or to be never false. Additionally to
+ * {@link BooleanConditionProfile} this implementation of {@link ConditionProfile} 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.
+ *
+ * Example usage:
+ *
+ * <pre>
+ * private final ConditionProfile zero = new IntegerConditionProfile();
+ *
+ * int value = ...;
+ * if (zero.profile(value == 0)) {
+ *   return 0;
+ * } else {
+ *   return value;
+ * }
+ *
+ * </pre>
+ *
+ * @see ConditionProfile
+ * @see IntegerConditionProfile
+ */
+public class IntegerConditionProfile extends ConditionProfile {
+
+    @CompilationFinal private int trueCount;
+    @CompilationFinal private int falseCount;
+
+    @Override
+    public boolean profile(boolean value) {
+        if (value) {
+            if (trueCount == 0) {
+                CompilerDirectives.transferToInterpreterAndInvalidate();
+            }
+            if (CompilerDirectives.inInterpreter()) {
+                trueCount++;
+            }
+        } else {
+            if (falseCount == 0) {
+                CompilerDirectives.transferToInterpreterAndInvalidate();
+            }
+            if (CompilerDirectives.inInterpreter()) {
+                falseCount++;
+            }
+        }
+        return CompilerDirectives.injectBranchProbability((double) trueCount / (double) (trueCount + falseCount), value);
+    }
+
+    public int getTrueCount() {
+        return trueCount;
+    }
+
+    public int getFalseCount() {
+        return falseCount;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%s(trueCount=%s, falseCount=%s)@%x", getClass().getSimpleName(), trueCount, falseCount, hashCode());
+    }
+}