1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.core.test;
  24 
  25 import org.graalvm.compiler.api.directives.GraalDirectives;
  26 import org.graalvm.compiler.nodes.StructuredGraph;
  27 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  28 import org.graalvm.compiler.nodes.extended.UnsafeAccessNode;
  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 
  74     public static Object test2Snippet(A a, int c) {
  75         Object phi = null;
  76         if (c != 0) {
  77             long[][] r = a.o;
  78             phi = r;
  79             UNSAFE.putDouble(r, (long) Unsafe.ARRAY_LONG_BASE_OFFSET, 12d);
  80         } else {
  81             long[][] r = a.p;
  82             phi = r;
  83             UNSAFE.putLong(r, (long) Unsafe.ARRAY_LONG_BASE_OFFSET, 123);
  84         }
  85         GraalDirectives.controlFlowAnchor();
  86         SideEffectD = UNSAFE.getDouble(phi, (long) Unsafe.ARRAY_LONG_BASE_OFFSET);
  87         return phi;
  88     }
  89 
  90     @Test
  91     public void test01() {
  92         StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.NO);
  93         testEarlyReadElimination(graph, 3, 2);
  94     }
  95 
  96     @Test
  97     public void test02() {
  98         StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.NO);
  99         testPartialEscapeReadElimination(graph, 3, 2);
 100     }
 101 
 102     @Test
 103     public void test03() {
 104         StructuredGraph graph = parseEager("test2Snippet", AllowAssumptions.NO);
 105         testEarlyReadElimination(graph, 3, 3);
 106     }
 107 
 108     @Test
 109     public void test04() {
 110         StructuredGraph graph = parseEager("test2Snippet", AllowAssumptions.NO);
 111         testEarlyReadElimination(graph, 3, 3);
 112     }
 113 
 114     public void testEarlyReadElimination(StructuredGraph graph, int reads, int writes) {
 115         PhaseContext context = getDefaultHighTierContext();
 116         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 117         canonicalizer.apply(graph, context);
 118         new EarlyReadEliminationPhase(canonicalizer).apply(graph, context);
 119         Assert.assertEquals(3, graph.getNodes().filter(UnsafeAccessNode.class).count());
 120         // after lowering the same applies for reads and writes
 121         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 122         canonicalizer.apply(graph, context);
 123         new EarlyReadEliminationPhase(canonicalizer).apply(graph, context);
 124         Assert.assertEquals(reads, graph.getNodes().filter(ReadNode.class).count());
 125         Assert.assertEquals(writes, graph.getNodes().filter(WriteNode.class).count());
 126     }
 127 
 128     public void testPartialEscapeReadElimination(StructuredGraph graph, int reads, int writes) {
 129         OptionValues options = graph.getOptions();
 130         PhaseContext context = getDefaultHighTierContext();
 131         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 132         canonicalizer.apply(graph, context);
 133         new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context);
 134         Assert.assertEquals(3, graph.getNodes().filter(UnsafeAccessNode.class).count());
 135         // after lowering the same applies for reads and writes
 136         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 137         canonicalizer.apply(graph, context);
 138         new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context);
 139         Assert.assertEquals(reads, graph.getNodes().filter(ReadNode.class).count());
 140         Assert.assertEquals(writes, graph.getNodes().filter(WriteNode.class).count());
 141     }
 142 
 143     public static int testWriteIntToByteArraySnippet() {
 144         byte[] array = new byte[4];
 145         UNSAFE.putInt(array, byteArrayBaseOffset, 0x01020304);
 146         return array[0];
 147     }
 148 
 149     @Test
 150     public void testWriteIntToByteArray() {
 151         test("testWriteIntToByteArraySnippet");
 152     }
 153 
 154     public static byte testWriteSignedExtendedByteToByteArraySnippet(byte b) {
 155         byte[] array = new byte[4];
 156         array[0] = 0x01;
 157         array[1] = 0x02;
 158         array[2] = 0x03;
 159         array[3] = 0x04;
 160         UNSAFE.putInt(array, byteArrayBaseOffset, b);
 161         return array[3];
 162     }
 163 
 164     @Test
 165     public void testWriteSignedExtendedByteToByteArray() {
 166         test("testWriteSignedExtendedByteToByteArraySnippet", (byte) 0);
 167     }
 168 
 169     public static int testWriteLongToIntArraySnippet() {
 170         int[] array = new int[2];
 171         UNSAFE.putLong(array, intArrayBaseOffset, 0x0102030405060708L);
 172         return array[0];
 173     }
 174 
 175     @Test
 176     public void testWriteLongToIntArray() {
 177         test("testWriteLongToIntArraySnippet");
 178     }
 179 
 180     public static int testWriteByteToIntArraySnippet() {
 181         int[] array = new int[1];
 182         array[0] = 0x01020304;
 183         UNSAFE.putByte(array, intArrayBaseOffset, (byte) 0x05);
 184         return array[0];
 185     }
 186 
 187     @Test
 188     public void testWriteByteToIntArray() {
 189         test("testWriteByteToIntArraySnippet");
 190     }
 191 
 192     public static long testWriteIntToLongArraySnippet() {
 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 }