Path: blob/master/test/hotspot/jtreg/compiler/stable/TestStableDouble.java
41152 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestStableDouble25* @summary tests on stable fields and arrays26* @library /test/lib /27* @modules java.base/jdk.internal.misc28* @modules java.base/jdk.internal.vm.annotation29* @build sun.hotspot.WhiteBox30*31* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp32* -XX:CompileOnly=::get,::get1,::get2,::get3,::get433* -XX:-TieredCompilation34* -XX:+FoldStableValues35* compiler.stable.TestStableDouble36* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp37* -XX:CompileOnly=::get,::get1,::get2,::get3,::get438* -XX:-TieredCompilation39* -XX:-FoldStableValues40* compiler.stable.TestStableDouble41*42* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp43* -XX:CompileOnly=::get,::get1,::get2,::get3,::get444* -XX:+TieredCompilation -XX:TieredStopAtLevel=145* -XX:+FoldStableValues46* compiler.stable.TestStableDouble47* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp48* -XX:CompileOnly=::get,::get1,::get2,::get3,::get449* -XX:+TieredCompilation -XX:TieredStopAtLevel=150* -XX:-FoldStableValues51* compiler.stable.TestStableDouble52*/5354package compiler.stable;5556import jdk.internal.vm.annotation.Stable;5758import java.lang.reflect.InvocationTargetException;5960public class TestStableDouble {61static final boolean isStableEnabled = StableConfiguration.isStableEnabled;6263public static void main(String[] args) throws Exception {64run(DefaultValue.class);65run(DoubleStable.class);66run(DefaultStaticValue.class);67run(StaticDoubleStable.class);68run(VolatileDoubleStable.class);6970// @Stable arrays: Dim 1-471run(DoubleArrayDim1.class);72run(DoubleArrayDim2.class);73run(DoubleArrayDim3.class);74run(DoubleArrayDim4.class);7576// @Stable Object field: dynamic arrays77run(ObjectArrayLowerDim0.class);78run(ObjectArrayLowerDim1.class);79run(ObjectArrayLowerDim2.class);8081// Nested @Stable fields82run(NestedStableField.class);83run(NestedStableField1.class);84run(NestedStableField2.class);85run(NestedStableField3.class);8687if (failed) {88throw new Error("TEST FAILED");89}90}9192/* ==================================================== */9394static class DefaultValue {95public @Stable double v;9697public static final DefaultValue c = new DefaultValue();98public static double get() { return c.v; }99public static void test() throws Exception {100double val1 = get();101c.v = 1.0; double val2 = get();102assertEquals(val1, 0);103assertEquals(val2, 1.0);104}105}106107/* ==================================================== */108109static class DoubleStable {110public @Stable double v;111112public static final DoubleStable c = new DoubleStable();113public static double get() { return c.v; }114public static void test() throws Exception {115c.v = 1.0; double val1 = get();116c.v = Double.MAX_VALUE; double val2 = get();117assertEquals(val1, 1.0);118assertEquals(val2, (isStableEnabled ? 1.0 : Double.MAX_VALUE));119}120}121122/* ==================================================== */123124static class DefaultStaticValue {125public static @Stable double v;126127public static final DefaultStaticValue c = new DefaultStaticValue();128public static double get() { return c.v; }129public static void test() throws Exception {130double val1 = get();131c.v = 1.0; double val2 = get();132assertEquals(val1, 0);133assertEquals(val2, 1.0);134}135}136137/* ==================================================== */138139static class StaticDoubleStable {140public static @Stable double v;141142public static final StaticDoubleStable c = new StaticDoubleStable();143public static double get() { return c.v; }144public static void test() throws Exception {145c.v = 1.0; double val1 = get();146c.v = Double.MAX_VALUE; double val2 = get();147assertEquals(val1, 1.0);148assertEquals(val2, (isStableEnabled ? 1.0 : Double.MAX_VALUE));149}150}151152/* ==================================================== */153154static class VolatileDoubleStable {155public @Stable double v;156157public static final VolatileDoubleStable c = new VolatileDoubleStable();158public static double get() { return c.v; }159public static void test() throws Exception {160c.v = 1.0; double val1 = get();161c.v = Double.MAX_VALUE; double val2 = get();162assertEquals(val1, 1.0);163assertEquals(val2, (isStableEnabled ? 1.0 : Double.MAX_VALUE));164}165}166167/* ==================================================== */168// @Stable array == field && all components are stable169170static class DoubleArrayDim1 {171public @Stable double[] v;172173public static final DoubleArrayDim1 c = new DoubleArrayDim1();174public static double get() { return c.v[0]; }175public static double get1() { return c.v[10]; }176public static double[] get2() { return c.v; }177public static void test() throws Exception {178{179c.v = new double[1]; c.v[0] = 1.0; double val1 = get();180c.v[0] = 2.0; double val2 = get();181assertEquals(val1, 1.0);182assertEquals(val2, (isStableEnabled ? 1.0 : 2.0));183184c.v = new double[1]; c.v[0] = 3.0; double val3 = get();185assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)186: 3.0));187}188189{190c.v = new double[20]; c.v[10] = 1.0; double val1 = get1();191c.v[10] = 2.0; double val2 = get1();192assertEquals(val1, 1.0);193assertEquals(val2, (isStableEnabled ? 1.0 : 2.0));194195c.v = new double[20]; c.v[10] = 3.0; double val3 = get1();196assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)197: 3.0));198}199200{201c.v = new double[1]; double[] val1 = get2();202c.v = new double[1]; double[] val2 = get2();203assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));204}205}206}207208/* ==================================================== */209210static class DoubleArrayDim2 {211public @Stable double[][] v;212213public static final DoubleArrayDim2 c = new DoubleArrayDim2();214public static double get() { return c.v[0][0]; }215public static double[] get1() { return c.v[0]; }216public static double[][] get2() { return c.v; }217public static void test() throws Exception {218{219c.v = new double[1][1]; c.v[0][0] = 1.0; double val1 = get();220c.v[0][0] = 2.0; double val2 = get();221assertEquals(val1, 1.0);222assertEquals(val2, (isStableEnabled ? 1.0 : 2.0));223224c.v = new double[1][1]; c.v[0][0] = 3.0; double val3 = get();225assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)226: 3.0));227228c.v[0] = new double[1]; c.v[0][0] = 4.0; double val4 = get();229assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)230: 4.0));231}232233{234c.v = new double[1][1]; double[] val1 = get1();235c.v[0] = new double[1]; double[] val2 = get1();236assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));237}238239{240c.v = new double[1][1]; double[][] val1 = get2();241c.v = new double[1][1]; double[][] val2 = get2();242assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));243}244}245}246247/* ==================================================== */248249static class DoubleArrayDim3 {250public @Stable double[][][] v;251252public static final DoubleArrayDim3 c = new DoubleArrayDim3();253public static double get() { return c.v[0][0][0]; }254public static double[] get1() { return c.v[0][0]; }255public static double[][] get2() { return c.v[0]; }256public static double[][][] get3() { return c.v; }257public static void test() throws Exception {258{259c.v = new double[1][1][1]; c.v[0][0][0] = 1.0; double val1 = get();260c.v[0][0][0] = 2.0; double val2 = get();261assertEquals(val1, 1.0);262assertEquals(val2, (isStableEnabled ? 1.0 : 2.0));263264c.v = new double[1][1][1]; c.v[0][0][0] = 3.0; double val3 = get();265assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)266: 3.0));267268c.v[0] = new double[1][1]; c.v[0][0][0] = 4.0; double val4 = get();269assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)270: 4.0));271272c.v[0][0] = new double[1]; c.v[0][0][0] = 5.0; double val5 = get();273assertEquals(val5, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)274: 5.0));275}276277{278c.v = new double[1][1][1]; double[] val1 = get1();279c.v[0][0] = new double[1]; double[] val2 = get1();280assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));281}282283{284c.v = new double[1][1][1]; double[][] val1 = get2();285c.v[0] = new double[1][1]; double[][] val2 = get2();286assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));287}288289{290c.v = new double[1][1][1]; double[][][] val1 = get3();291c.v = new double[1][1][1]; double[][][] val2 = get3();292assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));293}294}295}296297/* ==================================================== */298299static class DoubleArrayDim4 {300public @Stable double[][][][] v;301302public static final DoubleArrayDim4 c = new DoubleArrayDim4();303public static double get() { return c.v[0][0][0][0]; }304public static double[] get1() { return c.v[0][0][0]; }305public static double[][] get2() { return c.v[0][0]; }306public static double[][][] get3() { return c.v[0]; }307public static double[][][][] get4() { return c.v; }308public static void test() throws Exception {309{310c.v = new double[1][1][1][1]; c.v[0][0][0][0] = 1.0; double val1 = get();311c.v[0][0][0][0] = 2.0; double val2 = get();312assertEquals(val1, 1.0);313assertEquals(val2, (isStableEnabled ? 1.0 : 2.0));314315c.v = new double[1][1][1][1]; c.v[0][0][0][0] = 3.0; double val3 = get();316assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)317: 3.0));318319c.v[0] = new double[1][1][1]; c.v[0][0][0][0] = 4.0; double val4 = get();320assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)321: 4.0));322323c.v[0][0] = new double[1][1]; c.v[0][0][0][0] = 5.0; double val5 = get();324assertEquals(val5, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)325: 5.0));326327c.v[0][0][0] = new double[1]; c.v[0][0][0][0] = 6.0; double val6 = get();328assertEquals(val6, (isStableEnabled ? (isStableEnabled ? 1.0 : 2.0)329: 6.0));330}331332{333c.v = new double[1][1][1][1]; double[] val1 = get1();334c.v[0][0][0] = new double[1]; double[] val2 = get1();335assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));336}337338{339c.v = new double[1][1][1][1]; double[][] val1 = get2();340c.v[0][0] = new double[1][1]; double[][] val2 = get2();341assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));342}343344{345c.v = new double[1][1][1][1]; double[][][] val1 = get3();346c.v[0] = new double[1][1][1]; double[][][] val2 = get3();347assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));348}349350{351c.v = new double[1][1][1][1]; double[][][][] val1 = get4();352c.v = new double[1][1][1][1]; double[][][][] val2 = get4();353assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));354}355}356}357358/* ==================================================== */359// Dynamic Dim is higher than static360static class ObjectArrayLowerDim0 {361public @Stable Object v;362363public static final ObjectArrayLowerDim0 c = new ObjectArrayLowerDim0();364public static double get() { return ((double[])c.v)[0]; }365public static double[] get1() { return (double[])c.v; }366367public static void test() throws Exception {368{369c.v = new double[1]; ((double[])c.v)[0] = 1.0; double val1 = get();370((double[])c.v)[0] = 2.0; double val2 = get();371372assertEquals(val1, 1.0);373assertEquals(val2, 2.0);374}375376{377c.v = new double[1]; double[] val1 = get1();378c.v = new double[1]; double[] val2 = get1();379assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));380}381}382}383384/* ==================================================== */385386static class ObjectArrayLowerDim1 {387public @Stable Object[] v;388389public static final ObjectArrayLowerDim1 c = new ObjectArrayLowerDim1();390public static double get() { return ((double[][])c.v)[0][0]; }391public static double[] get1() { return (double[])(c.v[0]); }392public static Object[] get2() { return c.v; }393394public static void test() throws Exception {395{396c.v = new double[1][1]; ((double[][])c.v)[0][0] = 1.0; double val1 = get();397((double[][])c.v)[0][0] = 2.0; double val2 = get();398399assertEquals(val1, 1.0);400assertEquals(val2, 2.0);401}402403{404c.v = new double[1][1]; c.v[0] = new double[0]; double[] val1 = get1();405c.v[0] = new double[0]; double[] val2 = get1();406407assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));408}409410{411c.v = new double[0][0]; Object[] val1 = get2();412c.v = new double[0][0]; Object[] val2 = get2();413414assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));415}416}417}418419/* ==================================================== */420421static class ObjectArrayLowerDim2 {422public @Stable Object[][] v;423424public static final ObjectArrayLowerDim2 c = new ObjectArrayLowerDim2();425public static double get() { return ((double[][][])c.v)[0][0][0]; }426public static double[] get1() { return (double[])(c.v[0][0]); }427public static double[][] get2() { return (double[][])(c.v[0]); }428public static Object[][] get3() { return c.v; }429430public static void test() throws Exception {431{432c.v = new double[1][1][1]; ((double[][][])c.v)[0][0][0] = 1.0; double val1 = get();433((double[][][])c.v)[0][0][0] = 2.0; double val2 = get();434435assertEquals(val1, 1.0);436assertEquals(val2, 2.0);437}438439{440c.v = new double[1][1][1]; c.v[0][0] = new double[0]; double[] val1 = get1();441c.v[0][0] = new double[0]; double[] val2 = get1();442443assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));444}445446{447c.v = new double[1][1][1]; c.v[0] = new double[0][0]; double[][] val1 = get2();448c.v[0] = new double[0][0]; double[][] val2 = get2();449450assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));451}452453{454c.v = new double[0][0][0]; Object[][] val1 = get3();455c.v = new double[0][0][0]; Object[][] val2 = get3();456457assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));458}459}460}461462/* ==================================================== */463464static class NestedStableField {465static class A {466public @Stable double a;467468}469public @Stable A v;470471public static final NestedStableField c = new NestedStableField();472public static A get() { return c.v; }473public static double get1() { return get().a; }474475public static void test() throws Exception {476{477c.v = new A(); c.v.a = 1.0; A val1 = get();478c.v.a = 2.0; A val2 = get();479480assertEquals(val1.a, 2.0);481assertEquals(val2.a, 2.0);482}483484{485c.v = new A(); c.v.a = 1.0; double val1 = get1();486c.v.a = 2.0; double val2 = get1();487c.v = new A(); c.v.a = 3.0; double val3 = get1();488489assertEquals(val1, 1.0);490assertEquals(val2, (isStableEnabled ? 1.0 : 2.0));491assertEquals(val3, (isStableEnabled ? 1.0 : 3.0));492}493}494}495496/* ==================================================== */497498static class NestedStableField1 {499static class A {500public @Stable double a;501public @Stable A next;502}503public @Stable A v;504505public static final NestedStableField1 c = new NestedStableField1();506public static A get() { return c.v.next.next.next.next.next.next.next; }507public static double get1() { return get().a; }508509public static void test() throws Exception {510{511c.v = new A(); c.v.next = new A(); c.v.next.next = c.v;512c.v.a = 1.0; c.v.next.a = 1.0; A val1 = get();513c.v.a = 2.0; c.v.next.a = 2.0; A val2 = get();514515assertEquals(val1.a, 2.0);516assertEquals(val2.a, 2.0);517}518519{520c.v = new A(); c.v.next = c.v;521c.v.a = 1.0; double val1 = get1();522c.v.a = 2.0; double val2 = get1();523c.v = new A(); c.v.next = c.v;524c.v.a = 3.0; double val3 = get1();525526assertEquals(val1, 1.0);527assertEquals(val2, (isStableEnabled ? 1.0 : 2.0));528assertEquals(val3, (isStableEnabled ? 1.0 : 3.0));529}530}531}532/* ==================================================== */533534static class NestedStableField2 {535static class A {536public @Stable double a;537public @Stable A left;538public A right;539}540541public @Stable A v;542543public static final NestedStableField2 c = new NestedStableField2();544public static double get() { return c.v.left.left.left.a; }545public static double get1() { return c.v.left.left.right.left.a; }546547public static void test() throws Exception {548{549c.v = new A(); c.v.left = c.v.right = c.v;550c.v.a = 1.0; double val1 = get(); double val2 = get1();551c.v.a = 2.0; double val3 = get(); double val4 = get1();552553assertEquals(val1, 1.0);554assertEquals(val3, (isStableEnabled ? 1.0 : 2.0));555556assertEquals(val2, 1.0);557assertEquals(val4, 2.0);558}559}560}561562/* ==================================================== */563564static class NestedStableField3 {565static class A {566public @Stable double a;567public @Stable A[] left;568public A[] right;569}570571public @Stable A[] v;572573public static final NestedStableField3 c = new NestedStableField3();574public static double get() { return c.v[0].left[1].left[0].left[1].a; }575public static double get1() { return c.v[1].left[0].left[1].right[0].left[1].a; }576577public static void test() throws Exception {578{579A elem = new A();580c.v = new A[] { elem, elem }; c.v[0].left = c.v[0].right = c.v;581elem.a = 1.0; double val1 = get(); double val2 = get1();582elem.a = 2.0; double val3 = get(); double val4 = get1();583584assertEquals(val1, 1.0);585assertEquals(val3, (isStableEnabled ? 1.0 : 2.0));586587assertEquals(val2, 1.0);588assertEquals(val4, 2.0);589}590}591}592593/* ==================================================== */594// Auxiliary methods595static void assertEquals(double i, double j) { if (i != j) throw new AssertionError(i + " != " + j); }596static void assertTrue(boolean b) { if (!b) throw new AssertionError(); }597598static boolean failed = false;599600public static void run(Class<?> test) {601Throwable ex = null;602System.out.print(test.getName()+": ");603try {604test.getMethod("test").invoke(null);605} catch (InvocationTargetException e) {606ex = e.getCause();607} catch (Throwable e) {608ex = e;609} finally {610if (ex == null) {611System.out.println("PASSED");612} else {613failed = true;614System.out.println("FAILED");615ex.printStackTrace(System.out);616}617}618}619}620621622