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