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;
  26 
  27 import jdk.vm.ci.amd64.AMD64;
  28 import jdk.vm.ci.meta.ResolvedJavaMethod;
  29 
  30 import org.graalvm.compiler.core.common.CompilationIdentifier;
  31 import org.graalvm.compiler.nodes.ConstantNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.options.OptionValues;
  34 import org.junit.Assume;
  35 import org.junit.Test;
  36 
  37 public class StableArrayReadFoldingTest extends GraalCompilerTest {
  38 
  39     static final boolean[] STABLE_BOOLEAN_ARRAY = new boolean[16];
  40     static final int[] STABLE_INT_ARRAY = new int[16];
  41 
  42     static final long BOOLEAN_ARRAY_BASE_OFFSET;
  43     static final long INT_ARRAY_BASE_OFFSET;
  44 
  45     static {
  46         BOOLEAN_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(boolean[].class);
  47         INT_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(int[].class);
  48     }
  49 
  50     @Override
  51     protected StructuredGraph parseForCompile(ResolvedJavaMethod method, CompilationIdentifier compilationId, OptionValues options) {
  52         StructuredGraph graph = super.parseForCompile(method, compilationId, options);
  53         // Mimic @Stable array constants.
  54         for (ConstantNode constantNode : graph.getNodes().filter(ConstantNode.class).snapshot()) {
  55             if (getConstantReflection().readArrayLength(constantNode.asJavaConstant()) != null) {
  56                 ConstantNode newConstantNode = graph.unique(ConstantNode.forConstant(constantNode.asJavaConstant(), 1, true, getMetaAccess()));
  57                 constantNode.replaceAndDelete(newConstantNode);
  58             }
  59         }
  60         return graph;
  61     }
  62 
  63     public static boolean killWithSameType() {
  64         boolean beforeKill = UNSAFE.getBoolean(STABLE_BOOLEAN_ARRAY, BOOLEAN_ARRAY_BASE_OFFSET);
  65         STABLE_BOOLEAN_ARRAY[0] = true;
  66         boolean afterKill = UNSAFE.getBoolean(STABLE_BOOLEAN_ARRAY, BOOLEAN_ARRAY_BASE_OFFSET);
  67 
  68         STABLE_BOOLEAN_ARRAY[0] = false;
  69         return beforeKill == afterKill;
  70     }
  71 
  72     @Test
  73     public void testKillWithSameType() {
  74         ResolvedJavaMethod method = getResolvedJavaMethod("killWithSameType");
  75         testAgainstExpected(method, new Result(true, null), null);
  76     }
  77 
  78     public static boolean killWithDifferentType() {
  79         byte beforeKill = UNSAFE.getByte(STABLE_BOOLEAN_ARRAY, BOOLEAN_ARRAY_BASE_OFFSET);
  80         STABLE_BOOLEAN_ARRAY[0] = true;
  81         byte afterKill = UNSAFE.getByte(STABLE_BOOLEAN_ARRAY, BOOLEAN_ARRAY_BASE_OFFSET);
  82 
  83         STABLE_BOOLEAN_ARRAY[0] = false;
  84         return beforeKill == afterKill;
  85     }
  86 
  87     @Test
  88     public void testKillWithDifferentType() {
  89         ResolvedJavaMethod method = getResolvedJavaMethod("killWithDifferentType");
  90         testAgainstExpected(method, new Result(true, null), null);
  91     }
  92 
  93     public static boolean killWithSameTypeUnaligned() {
  94         int beforeKill = UNSAFE.getInt(STABLE_INT_ARRAY, INT_ARRAY_BASE_OFFSET + 1);
  95         STABLE_INT_ARRAY[0] = 0x01020304;
  96         int afterKill = UNSAFE.getInt(STABLE_INT_ARRAY, INT_ARRAY_BASE_OFFSET + 1);
  97 
  98         STABLE_INT_ARRAY[0] = 0;
  99         return beforeKill == afterKill;
 100     }
 101 
 102     @Test
 103     public void testKillWithSameTypeUnaligned() {
 104         Assume.assumeTrue("Only test unaligned access on AMD64", getTarget().arch instanceof AMD64);
 105         ResolvedJavaMethod method = getResolvedJavaMethod("killWithSameTypeUnaligned");
 106         testAgainstExpected(method, new Result(true, null), null);
 107     }
 108 
 109     public static boolean killWithDifferentTypeUnaligned() {
 110         byte beforeKill = UNSAFE.getByte(STABLE_INT_ARRAY, INT_ARRAY_BASE_OFFSET + 1);
 111         STABLE_INT_ARRAY[0] = 0x01020304;
 112         byte afterKill = UNSAFE.getByte(STABLE_INT_ARRAY, INT_ARRAY_BASE_OFFSET + 1);
 113 
 114         STABLE_INT_ARRAY[0] = 0;
 115         return beforeKill == afterKill;
 116     }
 117 
 118     @Test
 119     public void testKillWithDifferentTypeUnaligned() {
 120         Assume.assumeTrue("Only test unaligned access on AMD64", getTarget().arch instanceof AMD64);
 121         ResolvedJavaMethod method = getResolvedJavaMethod("killWithDifferentTypeUnaligned");
 122         testAgainstExpected(method, new Result(true, null), null);
 123     }
 124 }