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 public static long test1Snippet(double a) { 50 final Object m = Memory; 51 if (a > 0) { 52 UNSAFE.putDouble(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET, a); 53 } else { 54 SideEffectL = UNSAFE.getLong(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET); 55 } 56 return UNSAFE.getLong(m, (long) Unsafe.ARRAY_LONG_BASE_OFFSET); 57 } 58 59 public static class A { 60 long[][] o; 61 long[][] p; 62 } 63 64 public static Object test2Snippet(A a, int c) { 65 Object phi = null; 66 if (c != 0) { 67 long[][] r = a.o; 68 phi = r; 69 UNSAFE.putDouble(r, (long) Unsafe.ARRAY_LONG_BASE_OFFSET, 12d); 70 } else { 71 long[][] r = a.p; 72 phi = r; 73 UNSAFE.putLong(r, (long) Unsafe.ARRAY_LONG_BASE_OFFSET, 123); 74 } 75 GraalDirectives.controlFlowAnchor(); 76 SideEffectD = UNSAFE.getDouble(phi, (long) Unsafe.ARRAY_LONG_BASE_OFFSET); 77 return phi; 78 } 79 80 @Test 81 public void test01() { 82 StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.NO); 83 testEarlyReadElimination(graph, 3, 2); 84 } 85 86 @Test 87 public void test02() { 88 StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.NO); 89 testPartialEscapeReadElimination(graph, 3, 2); 90 } 91 92 @Test 93 public void test03() { 94 StructuredGraph graph = parseEager("test2Snippet", AllowAssumptions.NO); 95 testEarlyReadElimination(graph, 3, 3); 96 } 97 98 @Test 99 public void test04() { 100 StructuredGraph graph = parseEager("test2Snippet", AllowAssumptions.NO); 101 testEarlyReadElimination(graph, 3, 3); 102 } 103 104 public void testEarlyReadElimination(StructuredGraph graph, int reads, int writes) { 105 PhaseContext context = getDefaultHighTierContext(); 106 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 107 canonicalizer.apply(graph, context); 108 new EarlyReadEliminationPhase(canonicalizer).apply(graph, context); 109 Assert.assertEquals(3, graph.getNodes().filter(UnsafeAccessNode.class).count()); 110 // after lowering the same applies for reads and writes 111 new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 112 canonicalizer.apply(graph, context); 113 new EarlyReadEliminationPhase(canonicalizer).apply(graph, context); 114 Assert.assertEquals(reads, graph.getNodes().filter(ReadNode.class).count()); 115 Assert.assertEquals(writes, graph.getNodes().filter(WriteNode.class).count()); 116 } 117 118 public void testPartialEscapeReadElimination(StructuredGraph graph, int reads, int writes) { 119 OptionValues options = graph.getOptions(); 120 PhaseContext context = getDefaultHighTierContext(); 121 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 122 canonicalizer.apply(graph, context); 123 new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context); 124 Assert.assertEquals(3, graph.getNodes().filter(UnsafeAccessNode.class).count()); 125 // after lowering the same applies for reads and writes 126 new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 127 canonicalizer.apply(graph, context); 128 new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context); 129 Assert.assertEquals(reads, graph.getNodes().filter(ReadNode.class).count()); 130 Assert.assertEquals(writes, graph.getNodes().filter(WriteNode.class).count()); 131 } 132 133 }