1 /* 2 * Copyright (c) 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.vm.ci.runtime.test; 26 27 import org.junit.Assert; 28 import org.junit.Test; 29 30 import jdk.vm.ci.meta.JavaConstant; 31 import jdk.vm.ci.meta.SpeculationLog; 32 33 public class TestSpeculationLog extends MethodUniverse { 34 35 static class Dummy implements SpeculationLog.SpeculationReason { 36 37 } 38 39 @Test 40 public void testSpeculationIdentity() { 41 Dummy spec = new Dummy(); 42 SpeculationLog log = methods.entrySet().iterator().next().getValue().getSpeculationLog(); 43 SpeculationLog.Speculation s1 = log.speculate(spec); 44 SpeculationLog.Speculation s2 = log.speculate(spec); 45 Assert.assertTrue("Speculation should maintain identity", s1.equals(s2)); 46 JavaConstant e1 = metaAccess.encodeSpeculation(s1); 47 JavaConstant e2 = metaAccess.encodeSpeculation(s2); 48 Assert.assertTrue("speculation encoding should maintain identity", e1.equals(e2)); 49 } 50 } | 1 /* 2 * Copyright (c) 2018, 2019, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.vm.ci.runtime.test; 26 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.Collection; 30 import java.util.function.Supplier; 31 32 import org.junit.Assert; 33 import org.junit.Test; 34 35 import jdk.vm.ci.code.CodeCacheProvider; 36 import jdk.vm.ci.meta.JavaConstant; 37 import jdk.vm.ci.meta.ResolvedJavaMethod; 38 import jdk.vm.ci.meta.ResolvedJavaType; 39 import jdk.vm.ci.meta.SpeculationLog; 40 import jdk.vm.ci.meta.SpeculationLog.SpeculationReasonEncoding; 41 import jdk.vm.ci.runtime.JVMCI; 42 43 public class TestSpeculationLog extends MethodUniverse { 44 45 static final class Dummy implements SpeculationLog.SpeculationReason { 46 47 final int[] ints = {Integer.MIN_VALUE, -42, -1, 0, 1, 42, Integer.MAX_VALUE}; 48 final long[] longs = {Long.MIN_VALUE, -42, -1, 0, 1, 42, Long.MAX_VALUE}; 49 final String[] strings = {null, "non-empty string", ""}; 50 final Collection<ResolvedJavaMethod> methods = new ArrayList<>(MethodUniverse.methods.values()).subList(0, 10); 51 final Collection<ResolvedJavaMethod> constructors = new ArrayList<>(MethodUniverse.constructors.values()).subList(0, 10); 52 final Collection<ResolvedJavaType> types = new ArrayList<>(TypeUniverse.javaTypes).subList(0, 10); 53 54 private final boolean useCache; 55 private SpeculationReasonEncoding cachedEncoding; 56 57 Dummy(boolean useCache) { 58 this.useCache = useCache; 59 } 60 61 @Override 62 public SpeculationReasonEncoding encode(Supplier<SpeculationReasonEncoding> encodingSupplier) { 63 SpeculationReasonEncoding encoding = cachedEncoding; 64 if (encoding == null) { 65 encoding = encodingSupplier.get(); 66 for (int i : ints) { 67 encoding.addInt(i); 68 } 69 for (long l : longs) { 70 encoding.addLong(l); 71 } 72 for (String s : strings) { 73 encoding.addString(s); 74 } 75 for (ResolvedJavaMethod m : methods) { 76 encoding.addMethod(m); 77 } 78 for (ResolvedJavaMethod c : constructors) { 79 encoding.addMethod(c); 80 } 81 for (ResolvedJavaType t : types) { 82 encoding.addType(t); 83 } 84 encoding.addMethod(null); 85 encoding.addType(null); 86 } 87 if (useCache) { 88 cachedEncoding = encoding; 89 } 90 return encoding; 91 } 92 93 @Override 94 public boolean equals(Object obj) { 95 if (obj instanceof Dummy) { 96 Dummy that = (Dummy) obj; 97 return Arrays.equals(this.ints, that.ints) && 98 Arrays.equals(this.longs, that.longs) && 99 Arrays.equals(this.strings, that.strings) && 100 this.methods.equals(that.methods) && 101 this.constructors.equals(that.constructors) && 102 this.types.equals(that.types); 103 } 104 return super.equals(obj); 105 } 106 107 @Override 108 public int hashCode() { 109 return 31 * Arrays.hashCode(ints) ^ 110 Arrays.hashCode(longs) ^ 111 Arrays.hashCode(strings) ^ 112 methods.hashCode() ^ 113 constructors.hashCode() ^ 114 types.hashCode(); 115 } 116 } 117 118 @Test 119 public synchronized void testSpeculationIdentity() { 120 CodeCacheProvider codeCache = JVMCI.getRuntime().getHostJVMCIBackend().getCodeCache(); 121 SpeculationLog log = codeCache.createSpeculationLog(); 122 Dummy spec1 = new Dummy(true); 123 Dummy spec2 = new Dummy(false); 124 Assert.assertTrue(log.maySpeculate(spec1)); 125 Assert.assertTrue(log.maySpeculate(spec2)); 126 SpeculationLog.Speculation s1 = log.speculate(spec1); 127 SpeculationLog.Speculation s2 = log.speculate(spec2); 128 Assert.assertTrue("Speculation should maintain identity", s1.equals(s2)); 129 JavaConstant e1 = metaAccess.encodeSpeculation(s1); 130 JavaConstant e2 = metaAccess.encodeSpeculation(s2); 131 Assert.assertTrue("speculation encoding should maintain identity", e1.equals(e2)); 132 } 133 } |