< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/UnsafeReadEliminationTest.java

Print this page




  29 import org.graalvm.compiler.nodes.memory.ReadNode;
  30 import org.graalvm.compiler.nodes.memory.WriteNode;
  31 import org.graalvm.compiler.nodes.spi.LoweringTool;
  32 import org.graalvm.compiler.options.OptionValues;
  33 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  34 import org.graalvm.compiler.phases.common.LoweringPhase;
  35 import org.graalvm.compiler.phases.tiers.PhaseContext;
  36 import org.graalvm.compiler.virtual.phases.ea.EarlyReadEliminationPhase;
  37 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  38 import org.junit.Assert;
  39 import org.junit.Test;
  40 
  41 import sun.misc.Unsafe;
  42 
  43 public class UnsafeReadEliminationTest extends GraalCompilerTest {
  44 
  45     public static long[] Memory = new long[]{1, 2};
  46     public static double SideEffectD;
  47     public static double SideEffectL;
  48 

  49     private static final long byteArrayBaseOffset;
  50     private static final long intArrayBaseOffset;
  51     private static final long longArrayBaseOffset;
  52 
  53     static {

  54         byteArrayBaseOffset = UNSAFE.arrayBaseOffset(byte[].class);
  55         intArrayBaseOffset = UNSAFE.arrayBaseOffset(int[].class);
  56         longArrayBaseOffset = UNSAFE.arrayBaseOffset(long[].class);
  57     }
  58 
  59     public static long test1Snippet(double a) {
  60         final Object m = Memory;
  61         if (a > 0) {
  62             UNSAFE.putDouble(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET, a);
  63         } else {
  64             SideEffectL = UNSAFE.getLong(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET);
  65         }
  66         return UNSAFE.getLong(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET);
  67     }
  68 
  69     public static class A {
  70         long[][] o;
  71         long[][] p;
  72     }
  73 


 193         long[] array = new long[1];
 194         array[0] = 0x0102030405060708L;
 195         UNSAFE.putInt(array, longArrayBaseOffset, 0x04030201);
 196         return array[0];
 197     }
 198 
 199     @Test
 200     public void testWriteIntToLongArray() {
 201         test("testWriteIntToLongArraySnippet");
 202     }
 203 
 204     public static float testWriteFloatToIntArraySnippet() {
 205         float[] array = new float[1];
 206         UNSAFE.putInt(array, intArrayBaseOffset, Float.floatToRawIntBits(0.5f));
 207         return array[0];
 208     }
 209 
 210     @Test
 211     public void testWriteFloatToIntArray() {
 212         test("testWriteFloatToIntArraySnippet");









































































 213     }
 214 
 215 }


  29 import org.graalvm.compiler.nodes.memory.ReadNode;
  30 import org.graalvm.compiler.nodes.memory.WriteNode;
  31 import org.graalvm.compiler.nodes.spi.LoweringTool;
  32 import org.graalvm.compiler.options.OptionValues;
  33 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  34 import org.graalvm.compiler.phases.common.LoweringPhase;
  35 import org.graalvm.compiler.phases.tiers.PhaseContext;
  36 import org.graalvm.compiler.virtual.phases.ea.EarlyReadEliminationPhase;
  37 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  38 import org.junit.Assert;
  39 import org.junit.Test;
  40 
  41 import sun.misc.Unsafe;
  42 
  43 public class UnsafeReadEliminationTest extends GraalCompilerTest {
  44 
  45     public static long[] Memory = new long[]{1, 2};
  46     public static double SideEffectD;
  47     public static double SideEffectL;
  48 
  49     private static final long booleanArrayBaseOffset;
  50     private static final long byteArrayBaseOffset;
  51     private static final long intArrayBaseOffset;
  52     private static final long longArrayBaseOffset;
  53 
  54     static {
  55         booleanArrayBaseOffset = UNSAFE.arrayBaseOffset(boolean[].class);
  56         byteArrayBaseOffset = UNSAFE.arrayBaseOffset(byte[].class);
  57         intArrayBaseOffset = UNSAFE.arrayBaseOffset(int[].class);
  58         longArrayBaseOffset = UNSAFE.arrayBaseOffset(long[].class);
  59     }
  60 
  61     public static long test1Snippet(double a) {
  62         final Object m = Memory;
  63         if (a > 0) {
  64             UNSAFE.putDouble(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET, a);
  65         } else {
  66             SideEffectL = UNSAFE.getLong(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET);
  67         }
  68         return UNSAFE.getLong(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET);
  69     }
  70 
  71     public static class A {
  72         long[][] o;
  73         long[][] p;
  74     }
  75 


 195         long[] array = new long[1];
 196         array[0] = 0x0102030405060708L;
 197         UNSAFE.putInt(array, longArrayBaseOffset, 0x04030201);
 198         return array[0];
 199     }
 200 
 201     @Test
 202     public void testWriteIntToLongArray() {
 203         test("testWriteIntToLongArraySnippet");
 204     }
 205 
 206     public static float testWriteFloatToIntArraySnippet() {
 207         float[] array = new float[1];
 208         UNSAFE.putInt(array, intArrayBaseOffset, Float.floatToRawIntBits(0.5f));
 209         return array[0];
 210     }
 211 
 212     @Test
 213     public void testWriteFloatToIntArray() {
 214         test("testWriteFloatToIntArraySnippet");
 215     }
 216 
 217     public static final byte[] FINAL_BYTE_ARRAY = new byte[16];
 218 
 219     public static boolean alignedKill() {
 220         int beforeKill = UNSAFE.getInt(FINAL_BYTE_ARRAY, byteArrayBaseOffset);
 221         FINAL_BYTE_ARRAY[0] = 1;
 222         int afterKill = UNSAFE.getInt(FINAL_BYTE_ARRAY, byteArrayBaseOffset);
 223 
 224         FINAL_BYTE_ARRAY[0] = 0; // reset
 225         return beforeKill == afterKill;
 226     }
 227 
 228     @Test
 229     public void testAlignedKill() {
 230         test("alignedKill");
 231     }
 232 
 233     public static boolean unalignedKill() {
 234         int beforeKill = UNSAFE.getInt(FINAL_BYTE_ARRAY, byteArrayBaseOffset);
 235         FINAL_BYTE_ARRAY[1] = 1;
 236         int afterKill = UNSAFE.getInt(FINAL_BYTE_ARRAY, byteArrayBaseOffset);
 237 
 238         FINAL_BYTE_ARRAY[1] = 0; // reset
 239         return beforeKill == afterKill;
 240     }
 241 
 242     @Test
 243     public void testUnalignedKill() {
 244         test("unalignedKill");
 245     }
 246 
 247     public static final boolean[] FINAL_BOOLEAN_ARRAY = new boolean[16];
 248 
 249     public static boolean killBooleanAccessToBooleanArrayViaBASTORE() {
 250         boolean beforeKill = UNSAFE.getBoolean(FINAL_BOOLEAN_ARRAY, booleanArrayBaseOffset);
 251         FINAL_BOOLEAN_ARRAY[0] = true;
 252         boolean afterKill = UNSAFE.getBoolean(FINAL_BOOLEAN_ARRAY, booleanArrayBaseOffset);
 253 
 254         FINAL_BOOLEAN_ARRAY[0] = false; // reset
 255         return beforeKill == afterKill;
 256     }
 257 
 258     @Test
 259     public void testKillBooleanAccessToBooleanArrayViaBASTORE() {
 260         test("killBooleanAccessToBooleanArrayViaBASTORE");
 261     }
 262 
 263     public static boolean killByteAccessToBooleanArrayViaBASTORE() {
 264         byte beforeKill = UNSAFE.getByte(FINAL_BOOLEAN_ARRAY, booleanArrayBaseOffset);
 265         FINAL_BOOLEAN_ARRAY[0] = true;
 266         byte afterKill = UNSAFE.getByte(FINAL_BOOLEAN_ARRAY, booleanArrayBaseOffset);
 267 
 268         FINAL_BOOLEAN_ARRAY[0] = false; // reset
 269         return beforeKill == afterKill;
 270     }
 271 
 272     @Test
 273     public void testKillByteAccessToBooleanArrayViaBASTORE() {
 274         test("killByteAccessToBooleanArrayViaBASTORE");
 275     }
 276 
 277     public static boolean unsafeWriteToBooleanArray() {
 278         UNSAFE.putByte(FINAL_BOOLEAN_ARRAY, booleanArrayBaseOffset, (byte) 2);
 279         boolean result = UNSAFE.getBoolean(FINAL_BOOLEAN_ARRAY, booleanArrayBaseOffset);
 280 
 281         FINAL_BOOLEAN_ARRAY[0] = false; // reset
 282         return result;
 283     }
 284 
 285     @Test
 286     public void testUnsafeWriteToBooleanArray() {
 287         test("unsafeWriteToBooleanArray");
 288     }
 289 
 290 }
< prev index next >