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 java.lang.reflect.Field;
  26 
  27 import org.graalvm.compiler.nodes.StructuredGraph;
  28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  29 import org.graalvm.compiler.options.OptionValues;
  30 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  31 import org.graalvm.compiler.phases.tiers.PhaseContext;
  32 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  33 import org.junit.Test;
  34 
  35 public class UnsafeVirtualizationTest extends GraalCompilerTest {
  36 
  37     public static class A {
  38         int f1;
  39         int f2;
  40     }
  41 
  42     private static final long AF1Offset;
  43     private static final long AF2Offset;
  44     static {
  45         long o1 = -1;
  46         long o2 = -1;
  47         try {
  48             Field f1 = A.class.getDeclaredField("f1");
  49             Field f2 = A.class.getDeclaredField("f2");
  50             o1 = UNSAFE.objectFieldOffset(f1);
  51             o2 = UNSAFE.objectFieldOffset(f2);
  52         } catch (NoSuchFieldException | SecurityException e) {
  53             throw new AssertionError(e);
  54         }
  55         AF1Offset = o1;
  56         AF2Offset = o2;
  57     }
  58 
  59     public static int unsafeSnippet0(int i1, int i2) {
  60         A a = new A();
  61         UNSAFE.putDouble(a, AF1Offset, i1 + i2);
  62         return UNSAFE.getInt(a, AF1Offset) + UNSAFE.getInt(a, AF2Offset);
  63     }
  64 
  65     public static int unsafeSnippet1(int i1, int i2) {
  66         A a = new A();
  67         UNSAFE.putDouble(a, AF1Offset, i1 + i2);
  68         a.f2 = i1;
  69         return (int) UNSAFE.getDouble(a, AF1Offset);
  70     }
  71 
  72     @Test
  73     public void testUnsafePEA01() {
  74         testPartialEscapeReadElimination(parseEager("unsafeSnippet0", AllowAssumptions.NO), false);
  75         testPartialEscapeReadElimination(parseEager("unsafeSnippet0", AllowAssumptions.NO), true);
  76     }
  77 
  78     @Test
  79     public void testUnsafePEA02() {
  80         testPartialEscapeReadElimination(parseEager("unsafeSnippet1", AllowAssumptions.NO), false);
  81         testPartialEscapeReadElimination(parseEager("unsafeSnippet1", AllowAssumptions.NO), true);
  82     }
  83 
  84     public void testPartialEscapeReadElimination(StructuredGraph graph, boolean canonicalizeBefore) {
  85         OptionValues options = graph.getOptions();
  86         PhaseContext context = getDefaultHighTierContext();
  87         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  88         if (canonicalizeBefore) {
  89             canonicalizer.apply(graph, context);
  90         }
  91         new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context);
  92     }
  93 
  94 }