# HG changeset patch # User Chris Seaton # Date 1414187404 -3600 # Node ID 53afdc71b311397d7c8e796ae843191f76daec80 # Parent c88ab4f1f04aabe399bb47666fe14ba8a2ecf453 Truffle: added PrimitiveValueProfile. diff -r c88ab4f1f04a -r 53afdc71b311 CHANGELOG.md --- a/CHANGELOG.md Fri Oct 24 16:18:10 2014 +0200 +++ b/CHANGELOG.md Fri Oct 24 22:50:04 2014 +0100 @@ -14,6 +14,7 @@ * Renamed DirectCallNode#isSplittable to DirectCallNode#isCallTargetCloningAllowed * Renamed DirectCallNode#split to DirectCallNode#cloneCallTarget * Renamed DirectCallNode#isSplit to DirectCallNode#isCallTargetCloned +* Added PrimitiveValueProfile. * ... ## Version 0.5 diff -r c88ab4f1f04a -r 53afdc71b311 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/PrimitiveValueProfileTest.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/PrimitiveValueProfileTest.java Fri Oct 24 22:50:04 2014 +0100 @@ -0,0 +1,945 @@ +/* + * 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 PrimitiveValueProfileTest { + + @DataPoint public static final String O1 = new String(); + @DataPoint public static final String O2 = O1; + @DataPoint public static final Object O3 = new Object(); + @DataPoint public static final Object O4 = null; + + @DataPoint public static final byte B1 = Byte.MIN_VALUE; + @DataPoint public static final byte B2 = 0; + @DataPoint public static final byte B3 = 14; + @DataPoint public static final byte B4 = Byte.MAX_VALUE; + + @DataPoint public static final short S1 = Short.MIN_VALUE; + @DataPoint public static final short S2 = 0; + @DataPoint public static final short S3 = 14; + @DataPoint public static final short S4 = Short.MAX_VALUE; + + @DataPoint public static final int I1 = Integer.MIN_VALUE; + @DataPoint public static final int I2 = 0; + @DataPoint public static final int I3 = 14; + @DataPoint public static final int I4 = Integer.MAX_VALUE; + + @DataPoint public static final long L1 = Long.MIN_VALUE; + @DataPoint public static final long L2 = 0; + @DataPoint public static final long L3 = 14; + @DataPoint public static final long L4 = Long.MAX_VALUE; + + @DataPoint public static final float F1 = Float.MIN_VALUE; + @DataPoint public static final float F2 = -0.0f; + @DataPoint public static final float F3 = +0.0f; + @DataPoint public static final float F4 = 14.5f; + @DataPoint public static final float F5 = Float.MAX_VALUE; + + @DataPoint public static final double D1 = Double.MIN_VALUE; + @DataPoint public static final double D2 = -0.0; + @DataPoint public static final double D3 = +0.0; + @DataPoint public static final double D4 = 14.5; + @DataPoint public static final double D5 = Double.MAX_VALUE; + + @DataPoint public static final boolean T1 = false; + @DataPoint public static final boolean T2 = true; + + @DataPoint public static final char C1 = Character.MIN_VALUE; + @DataPoint public static final char C2 = 0; + @DataPoint public static final char C3 = 14; + @DataPoint public static final char C4 = Character.MAX_VALUE; + + private static final float FLOAT_DELTA = 0.00001f; + private static final double DOUBLE_DELTA = 0.00001; + + private PrimitiveValueProfile profile; + + @Before + public void create() { + profile = ValueProfile.createPrimitiveProfile(); + } + + @Test + public void testInitial() { + assertThat(profile.isGeneric(), is(false)); + assertThat(profile.isUninitialized(), is(true)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneObject(Object value) { + Object result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoObject(Object value0, Object value1) { + Object result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertThat(result0, is(value0)); + assertThat(result1, is(value1)); + + if (value0 == value1) { + assertThat(profile.getCachedValue(), is(value0)); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeObject(Object value0, Object value1, Object value2) { + Object result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + Object result2 = profile.profile(value2); + + assertThat(result0, is(value0)); + assertThat(result1, is(value1)); + assertThat(result2, is(value2)); + + if (value0 == value1 && value1 == value2) { + assertThat(profile.getCachedValue(), is(value0)); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneByte(byte value) { + byte result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoByte(byte value0, byte value1) { + byte result0 = profile.profile(value0); + byte result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertEquals(result1, value1); + + if (value0 == value1) { + assertTrue(profile.getCachedValue() instanceof Byte); + assertEquals((byte) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeByte(byte value0, byte value1, byte value2) { + byte result0 = profile.profile(value0); + byte result1 = profile.profile(value1); + byte result2 = profile.profile(value2); + + assertEquals(result0, value0); + assertEquals(result1, value1); + assertEquals(result2, value2); + + if (value0 == value1 && value1 == value2) { + assertTrue(profile.getCachedValue() instanceof Byte); + assertEquals((byte) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneShort(short value) { + short result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoShort(short value0, short value1) { + short result0 = profile.profile(value0); + short result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertEquals(result1, value1); + + if (value0 == value1) { + assertTrue(profile.getCachedValue() instanceof Short); + assertEquals((short) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeShort(short value0, short value1, short value2) { + short result0 = profile.profile(value0); + short result1 = profile.profile(value1); + short result2 = profile.profile(value2); + + assertEquals(result0, value0); + assertEquals(result1, value1); + assertEquals(result2, value2); + + if (value0 == value1 && value1 == value2) { + assertTrue(profile.getCachedValue() instanceof Short); + assertEquals((short) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneInteger(int value) { + int result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoInteger(int value0, int value1) { + int result0 = profile.profile(value0); + int result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertEquals(result1, value1); + + if (value0 == value1) { + assertTrue(profile.getCachedValue() instanceof Integer); + assertEquals((int) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeInteger(int value0, int value1, int value2) { + int result0 = profile.profile(value0); + int result1 = profile.profile(value1); + int result2 = profile.profile(value2); + + assertEquals(result0, value0); + assertEquals(result1, value1); + assertEquals(result2, value2); + + if (value0 == value1 && value1 == value2) { + assertTrue(profile.getCachedValue() instanceof Integer); + assertEquals((int) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneLong(long value) { + long result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoLong(long value0, long value1) { + long result0 = profile.profile(value0); + long result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertEquals(result1, value1); + + if (value0 == value1) { + assertTrue(profile.getCachedValue() instanceof Long); + assertEquals((long) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeLong(long value0, long value1, long value2) { + long result0 = profile.profile(value0); + long result1 = profile.profile(value1); + long result2 = profile.profile(value2); + + assertEquals(result0, value0); + assertEquals(result1, value1); + assertEquals(result2, value2); + + if (value0 == value1 && value1 == value2) { + assertTrue(profile.getCachedValue() instanceof Long); + assertEquals((long) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneFloat(float value) { + float result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoFloat(float value0, float value1) { + float result0 = profile.profile(value0); + float result1 = profile.profile(value1); + + assertEquals(result0, value0, FLOAT_DELTA); + assertEquals(result1, value1, FLOAT_DELTA); + + if (PrimitiveValueProfile.exactCompare(value0, value1)) { + assertTrue(profile.getCachedValue() instanceof Float); + assertEquals((float) profile.getCachedValue(), value0, FLOAT_DELTA); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeFloat(float value0, float value1, float value2) { + float result0 = profile.profile(value0); + float result1 = profile.profile(value1); + float result2 = profile.profile(value2); + + assertEquals(result0, value0, FLOAT_DELTA); + assertEquals(result1, value1, FLOAT_DELTA); + assertEquals(result2, value2, FLOAT_DELTA); + + if (PrimitiveValueProfile.exactCompare(value0, value1) && PrimitiveValueProfile.exactCompare(value1, value2)) { + assertTrue(profile.getCachedValue() instanceof Float); + assertEquals((float) profile.getCachedValue(), value0, FLOAT_DELTA); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneDouble(double value) { + double result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoDouble(double value0, double value1) { + double result0 = profile.profile(value0); + double result1 = profile.profile(value1); + + assertEquals(result0, value0, DOUBLE_DELTA); + assertEquals(result1, value1, DOUBLE_DELTA); + + if (PrimitiveValueProfile.exactCompare(value0, value1)) { + assertTrue(profile.getCachedValue() instanceof Double); + assertEquals((double) profile.getCachedValue(), value0, DOUBLE_DELTA); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeDouble(double value0, double value1, double value2) { + double result0 = profile.profile(value0); + double result1 = profile.profile(value1); + double result2 = profile.profile(value2); + + assertEquals(result0, value0, DOUBLE_DELTA); + assertEquals(result1, value1, DOUBLE_DELTA); + assertEquals(result2, value2, DOUBLE_DELTA); + + if (PrimitiveValueProfile.exactCompare(value0, value1) && PrimitiveValueProfile.exactCompare(value1, value2)) { + assertTrue(profile.getCachedValue() instanceof Double); + assertEquals((double) profile.getCachedValue(), value0, DOUBLE_DELTA); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneBoolean(boolean value) { + boolean result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoBoolean(boolean value0, boolean value1) { + boolean result0 = profile.profile(value0); + boolean result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertEquals(result1, value1); + + if (value0 == value1) { + assertTrue(profile.getCachedValue() instanceof Boolean); + assertEquals((boolean) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeBoolean(boolean value0, boolean value1, boolean value2) { + boolean result0 = profile.profile(value0); + boolean result1 = profile.profile(value1); + boolean result2 = profile.profile(value2); + + assertEquals(result0, value0); + assertEquals(result1, value1); + assertEquals(result2, value2); + + if (value0 == value1 && value1 == value2) { + assertTrue(profile.getCachedValue() instanceof Boolean); + assertEquals((boolean) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileOneChar(char value) { + char result = profile.profile(value); + + assertThat(result, is(value)); + assertEquals(profile.getCachedValue(), value); + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileTwoChar(char value0, char value1) { + char result0 = profile.profile(value0); + char result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertEquals(result1, value1); + + if (value0 == value1) { + assertTrue(profile.getCachedValue() instanceof Character); + assertEquals((char) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testProfileThreeChar(char value0, char value1, char value2) { + char result0 = profile.profile(value0); + char result1 = profile.profile(value1); + char result2 = profile.profile(value2); + + assertEquals(result0, value0); + assertEquals(result1, value1); + assertEquals(result2, value2); + + if (value0 == value1 && value1 == value2) { + assertTrue(profile.getCachedValue() instanceof Character); + assertEquals((char) profile.getCachedValue(), value0); + assertThat(profile.isGeneric(), is(false)); + } else { + assertThat(profile.isGeneric(), is(true)); + } + assertThat(profile.isUninitialized(), is(false)); + profile.toString(); // test that it is not crashing + } + + @Theory + public void testWithBoxedBoxedByte(byte value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Byte); + assertEquals((byte) result0, value); + assertTrue(result1 instanceof Byte); + assertEquals((byte) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedByte(byte value) { + byte result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertEquals(result0, value); + assertTrue(result1 instanceof Byte); + assertEquals((byte) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedByte(byte value) { + Object result0 = profile.profile((Object) value); + byte result1 = profile.profile(value); + + assertTrue(result0 instanceof Byte); + assertEquals((byte) result0, value); + assertEquals(result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedBoxedShort(short value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Short); + assertEquals((short) result0, value); + assertTrue(result1 instanceof Short); + assertEquals((short) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedShort(short value) { + short result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertEquals(result0, value); + assertTrue(result1 instanceof Short); + assertEquals((short) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedShort(short value) { + Object result0 = profile.profile((Object) value); + short result1 = profile.profile(value); + + assertTrue(result0 instanceof Short); + assertEquals((short) result0, value); + assertEquals(result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedBoxedInt(int value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Integer); + assertEquals((int) result0, value); + assertTrue(result1 instanceof Integer); + assertEquals((int) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedInt(int value) { + int result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertEquals(result0, value); + assertTrue(result1 instanceof Integer); + assertEquals((int) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedInt(int value) { + Object result0 = profile.profile((Object) value); + int result1 = profile.profile(value); + + assertTrue(result0 instanceof Integer); + assertEquals((int) result0, value); + assertEquals(result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedBoxedLong(long value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Long); + assertEquals((long) result0, value); + assertTrue(result1 instanceof Long); + assertEquals((long) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedLong(long value) { + long result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertEquals(result0, value); + assertTrue(result1 instanceof Long); + assertEquals((long) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedLong(long value) { + Object result0 = profile.profile((Object) value); + long result1 = profile.profile(value); + + assertTrue(result0 instanceof Long); + assertEquals((long) result0, value); + assertEquals(result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedBoxedFloat(float value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Float); + assertTrue(PrimitiveValueProfile.exactCompare((float) result0, value)); + assertTrue(result1 instanceof Float); + assertTrue(PrimitiveValueProfile.exactCompare((float) result1, value)); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedFloat(float value) { + float result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertTrue(PrimitiveValueProfile.exactCompare(result0, value)); + assertTrue(result1 instanceof Float); + assertTrue(PrimitiveValueProfile.exactCompare((float) result1, value)); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedFloat(float value) { + Object result0 = profile.profile((Object) value); + float result1 = profile.profile(value); + + assertTrue(result0 instanceof Float); + assertTrue(PrimitiveValueProfile.exactCompare((float) result0, value)); + assertTrue(PrimitiveValueProfile.exactCompare(result1, value)); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedBoxedDouble(double value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Double); + assertTrue(PrimitiveValueProfile.exactCompare((double) result0, value)); + assertTrue(result1 instanceof Double); + assertTrue(PrimitiveValueProfile.exactCompare((double) result1, value)); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedDouble(double value) { + double result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertTrue(PrimitiveValueProfile.exactCompare(result0, value)); + assertTrue(result1 instanceof Double); + assertTrue(PrimitiveValueProfile.exactCompare((double) result1, value)); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedDouble(double value) { + Object result0 = profile.profile((Object) value); + double result1 = profile.profile(value); + + assertTrue(result0 instanceof Double); + assertTrue(PrimitiveValueProfile.exactCompare((double) result0, value)); + assertTrue(PrimitiveValueProfile.exactCompare(result1, value)); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedBoxedBoolean(boolean value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Boolean); + assertEquals((boolean) result0, value); + assertTrue(result1 instanceof Boolean); + assertEquals((boolean) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedBoolean(boolean value) { + boolean result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertEquals(result0, value); + assertTrue(result1 instanceof Boolean); + assertEquals((boolean) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedBoolean(boolean value) { + Object result0 = profile.profile((Object) value); + boolean result1 = profile.profile(value); + + assertTrue(result0 instanceof Boolean); + assertEquals((boolean) result0, value); + assertEquals(result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedBoxedChar(char value) { + Object result0 = profile.profile((Object) value); + Object result1 = profile.profile((Object) value); + + assertTrue(result0 instanceof Character); + assertEquals((char) result0, value); + assertTrue(result1 instanceof Character); + assertEquals((char) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithUnboxedBoxedChar(char value) { + char result0 = profile.profile(value); + Object result1 = profile.profile((Object) value); + + assertEquals(result0, value); + assertTrue(result1 instanceof Character); + assertEquals((char) result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithBoxedUnboxedCharacter(char value) { + Object result0 = profile.profile((Object) value); + char result1 = profile.profile(value); + + assertTrue(result0 instanceof Character); + assertEquals((char) result0, value); + assertEquals(result1, value); + assertFalse(profile.isUninitialized()); + assertFalse(profile.isGeneric()); + } + + @Theory + public void testWithByteThenObject(byte value0, Object value1) { + byte result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Theory + public void testWithShortThenObject(short value0, Object value1) { + short result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Theory + public void testWithIntThenObject(int value0, Object value1) { + int result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Theory + public void testWithLongThenObject(long value0, Object value1) { + long result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Theory + public void testWithFloatThenObject(float value0, Object value1) { + float result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertTrue(PrimitiveValueProfile.exactCompare(result0, value0)); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Theory + public void testWithDoubleThenObject(double value0, Object value1) { + double result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertTrue(PrimitiveValueProfile.exactCompare(result0, value0)); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Theory + public void testWithBooleanThenObject(boolean value0, Object value1) { + boolean result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Theory + public void testWithCharThenObject(char value0, Object value1) { + char result0 = profile.profile(value0); + Object result1 = profile.profile(value1); + + assertEquals(result0, value0); + assertSame(result1, value1); + assertFalse(profile.isUninitialized()); + assertTrue(profile.isGeneric()); + } + + @Test + public void testNegativeZeroFloat() { + profile.profile(-0.0f); + profile.profile(+0.0f); + assertThat(profile.isGeneric(), is(true)); + } + + @Test + public void testNegativeZeroDouble() { + profile.profile(-0.0); + profile.profile(+0.0); + assertThat(profile.isGeneric(), is(true)); + } + +} diff -r c88ab4f1f04a -r 53afdc71b311 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/PrimitiveValueProfile.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/PrimitiveValueProfile.java Fri Oct 24 22:50:04 2014 +0100 @@ -0,0 +1,224 @@ +/* + * 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. 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 java.util.*; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; + +/** + * Represents a {@link ValueProfile} that speculates on the primitive equality or object identity of + * values. + *

+ * Note that for {code float} and {@code double} values we compare primitive equality via + * {@link Float#floatToRawIntBits} and {@link Double#doubleToRawLongBits}, so that for example + * {@code -0.0} is not considered the same as {@code 0.0}, even though primitive equality would + * normally say that it was. + */ +public class PrimitiveValueProfile extends ValueProfile { + + private static final Object UNINITIALIZED = new Object(); + private static final Object GENERIC = new Object(); + + @CompilationFinal private Object cachedValue = UNINITIALIZED; + + PrimitiveValueProfile() { + } + + @SuppressWarnings("unchecked") + @Override + public Object profile(Object value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Byte && value instanceof Byte && (byte) cachedValue == (byte) value) { + return cachedValue; + } else if (cachedValue instanceof Short && value instanceof Short && (short) cachedValue == (short) value) { + return cachedValue; + } else if (cachedValue instanceof Integer && value instanceof Integer && (int) cachedValue == (int) value) { + return cachedValue; + } else if (cachedValue instanceof Long && value instanceof Long && (long) cachedValue == (long) value) { + return cachedValue; + } else if (cachedValue instanceof Float && value instanceof Float && exactCompare((float) cachedValue, (float) value)) { + return cachedValue; + } else if (cachedValue instanceof Double && value instanceof Double && exactCompare((double) cachedValue, (double) value)) { + return cachedValue; + } else if (cachedValue instanceof Boolean && value instanceof Boolean && (boolean) cachedValue == (boolean) value) { + return cachedValue; + } else if (cachedValue instanceof Character && value instanceof Character && (char) cachedValue == (char) value) { + return cachedValue; + } else if (cachedValue == value) { + return cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public byte profile(byte value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Byte && (byte) cachedValue == value) { + return (byte) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public short profile(short value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Short && (short) cachedValue == value) { + return (short) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public int profile(int value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Integer && (int) cachedValue == value) { + return (int) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public long profile(long value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Long && (long) cachedValue == value) { + return (long) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public float profile(float value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Float && exactCompare((float) cachedValue, value)) { + return (float) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public double profile(double value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Double && exactCompare((double) cachedValue, value)) { + return (double) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public boolean profile(boolean value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Boolean && (boolean) cachedValue == value) { + return (boolean) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public char profile(char value) { + if (cachedValue != GENERIC) { + if (cachedValue instanceof Character && (char) cachedValue == value) { + return (char) cachedValue; + } else { + cacheMiss(value); + } + } + return value; + } + + public boolean isGeneric() { + return getCachedValue() == GENERIC; + } + + public boolean isUninitialized() { + return getCachedValue() == UNINITIALIZED; + } + + public Object getCachedValue() { + return cachedValue; + } + + @Override + public String toString() { + return String.format("%s(%s)@%x", getClass().getSimpleName(), formatValue(), hashCode()); + } + + private void cacheMiss(Object value) { + CompilerDirectives.transferToInterpreterAndInvalidate(); + if (cachedValue == UNINITIALIZED) { + cachedValue = value; + } else { + cachedValue = GENERIC; + } + } + + public static boolean exactCompare(float a, float b) { + /* + * -0.0 == 0.0, but you can tell the difference through other means, so we need to + * differentiate. + */ + return Float.floatToRawIntBits(a) == Float.floatToRawIntBits(b); + } + + public static boolean exactCompare(double a, double b) { + /* + * -0.0 == 0.0, but you can tell the difference through other means, so we need to + * differentiate. + */ + return Double.doubleToRawLongBits(a) == Double.doubleToRawLongBits(b); + } + + private String formatValue() { + if (cachedValue == null) { + return "null"; + } else if (cachedValue == UNINITIALIZED) { + return "uninitialized"; + } else if (cachedValue == GENERIC) { + return "generic"; + } else if (cachedValue instanceof Byte || cachedValue instanceof Short || cachedValue instanceof Integer || cachedValue instanceof Long || cachedValue instanceof Float || + cachedValue instanceof Double || cachedValue instanceof Boolean || cachedValue instanceof Character) { + return String.format("%s=%s", cachedValue.getClass().getSimpleName(), cachedValue); + } else { + return String.format("%s@%x", cachedValue.getClass().getSimpleName(), Objects.hash(cachedValue)); + } + } +} diff -r c88ab4f1f04a -r 53afdc71b311 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ValueProfile.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ValueProfile.java Fri Oct 24 16:18:10 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ValueProfile.java Fri Oct 24 22:50:04 2014 +0100 @@ -31,13 +31,14 @@ * *

  * private final ValueProfile classProfile = ValueProfile.createClassProfile();
- *
+ * 
  * return classProfile.profile(value);
  * 
* * All instances of {@code ValueProfile} (and subclasses) must be held in {@code final} fields for * compiler optimizations to take effect. * + * @see #createPrimitiveProfile() * @see #createIdentityProfile() * @see #createClassProfile() */ @@ -48,6 +49,13 @@ /** * Returns a {@link ValueProfile} that speculates on the exact class of a value. */ + public static PrimitiveValueProfile createPrimitiveProfile() { + return new PrimitiveValueProfile(); + } + + /** + * Returns a {@link ValueProfile} that speculates on the exact class of a value. + */ public static ValueProfile createClassProfile() { return new ExactClassValueProfile(); }