1 /*
   2  * Copyright (c) 2017, 2018, 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 
  24 
  25 package org.graalvm.compiler.core.test.ea;
  26 
  27 import java.lang.reflect.Field;
  28 
  29 import org.graalvm.compiler.core.common.GraalOptions;
  30 import org.graalvm.compiler.core.test.GraalCompilerTest;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.extended.RawLoadNode;
  33 import org.graalvm.compiler.nodes.extended.RawStoreNode;
  34 import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
  35 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  36 import org.graalvm.compiler.phases.tiers.HighTierContext;
  37 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  38 import org.junit.Test;
  39 
  40 import sun.misc.Unsafe;
  41 
  42 public class TrufflePEATest extends GraalCompilerTest {
  43 
  44     /**
  45      * This class mimics the behavior of {@code FrameWithoutBoxing}.
  46      */
  47     static class Frame {
  48         long[] primitiveLocals;
  49 
  50         Frame(int size) {
  51             primitiveLocals = new long[size];
  52         }
  53     }
  54 
  55     /**
  56      * This class mimics the behavior of {@code DynamicObjectL6I6}.
  57      */
  58     static class DynamicObject {
  59         int primitiveField0;
  60         int primitiveField1;
  61         int primitiveField2;
  62     }
  63 
  64     private static final long offsetLong1 = Unsafe.ARRAY_LONG_BASE_OFFSET + Unsafe.ARRAY_LONG_INDEX_SCALE * 1;
  65     private static final long offsetLong2 = Unsafe.ARRAY_LONG_BASE_OFFSET + Unsafe.ARRAY_LONG_INDEX_SCALE * 2;
  66 
  67     private static final long primitiveField0Offset;
  68 
  69     static {
  70         try {
  71             Field primitiveField0 = DynamicObject.class.getDeclaredField("primitiveField0");
  72             long offset = UNSAFE.objectFieldOffset(primitiveField0);
  73             if (offset % 8 == 0) {
  74                 primitiveField0Offset = offset;
  75             } else {
  76                 Field primitiveField1 = DynamicObject.class.getDeclaredField("primitiveField1");
  77                 offset = UNSAFE.objectFieldOffset(primitiveField1);
  78                 assert offset % 8 == 0;
  79                 primitiveField0Offset = offset;
  80             }
  81         } catch (NoSuchFieldException | SecurityException e) {
  82             throw new AssertionError(e);
  83         }
  84     }
  85 
  86     public static int unsafeAccessToLongArray(int v, Frame frame) {
  87         long[] array = frame.primitiveLocals;
  88         int s = UNSAFE.getInt(array, offsetLong1);
  89         UNSAFE.putInt(array, offsetLong1, v);
  90         UNSAFE.putInt(array, offsetLong2, v);
  91         return s + UNSAFE.getInt(array, offsetLong1) + UNSAFE.getInt(array, offsetLong2);
  92     }
  93 
  94     @Test
  95     public void testUnsafeAccessToLongArray() {
  96         StructuredGraph graph = processMethod("unsafeAccessToLongArray");
  97         assertDeepEquals(1, graph.getNodes().filter(RawLoadNode.class).count());
  98     }
  99 
 100     /**
 101      * The following value should be less than the default value of
 102      * {@link GraalOptions#MaximumEscapeAnalysisArrayLength}.
 103      */
 104     private static final int FRAME_SIZE = 16;
 105 
 106     public static long newFrame(long v) {
 107         Frame frame = new Frame(FRAME_SIZE);
 108         // Testing unsafe accesses with other kinds requires special handling of the initialized
 109         // entry kind.
 110         UNSAFE.putLong(frame.primitiveLocals, offsetLong1, v);
 111         return UNSAFE.getLong(frame.primitiveLocals, offsetLong1);
 112     }
 113 
 114     @Test
 115     public void testNewFrame() {
 116         StructuredGraph graph = processMethod("newFrame");
 117         assertDeepEquals(0, graph.getNodes().filter(CommitAllocationNode.class).count());
 118         assertDeepEquals(0, graph.getNodes().filter(RawLoadNode.class).count());
 119         assertDeepEquals(0, graph.getNodes().filter(RawStoreNode.class).count());
 120     }
 121 
 122     protected StructuredGraph processMethod(final String snippet) {
 123         StructuredGraph graph = parseEager(snippet, StructuredGraph.AllowAssumptions.NO);
 124         HighTierContext context = getDefaultHighTierContext();
 125         createInliningPhase().apply(graph, context);
 126         new PartialEscapePhase(true, true, new CanonicalizerPhase(), null, graph.getOptions()).apply(graph, context);
 127         return graph;
 128     }
 129 
 130     public static double accessDynamicObject(double v) {
 131         DynamicObject obj = new DynamicObject();
 132         UNSAFE.putDouble(obj, primitiveField0Offset, v);
 133         return UNSAFE.getDouble(obj, primitiveField0Offset);
 134     }
 135 
 136     @Test
 137     public void testAccessDynamicObject() {
 138         StructuredGraph graph = processMethod("accessDynamicObject");
 139         assertDeepEquals(0, graph.getNodes().filter(CommitAllocationNode.class).count());
 140         assertDeepEquals(0, graph.getNodes().filter(RawLoadNode.class).count());
 141         assertDeepEquals(0, graph.getNodes().filter(RawStoreNode.class).count());
 142     }
 143 
 144 }