Path: blob/master/test/hotspot/jtreg/compiler/stable/TestStableLong.java
41149 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 TestStableLong25* @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.TestStableLong36* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp37* -XX:CompileOnly=::get,::get1,::get2,::get3,::get438* -XX:-TieredCompilation39* -XX:-FoldStableValues40* compiler.stable.TestStableLong41*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.TestStableLong47* @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.TestStableLong52*/5354package compiler.stable;5556import jdk.internal.vm.annotation.Stable;5758import java.lang.reflect.InvocationTargetException;5960public class TestStableLong {61static final boolean isStableEnabled = StableConfiguration.isStableEnabled;6263public static void main(String[] args) throws Exception {64run(DefaultValue.class);65run(LongStable.class);66run(DefaultStaticValue.class);67run(StaticLongStable.class);68run(VolatileLongStable.class);6970// @Stable arrays: Dim 1-471run(LongArrayDim1.class);72run(LongArrayDim2.class);73run(LongArrayDim3.class);74run(LongArrayDim4.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 long v;9697public static final DefaultValue c = new DefaultValue();98public static long get() { return c.v; }99public static void test() throws Exception {100long val1 = get();101c.v = 1L; long val2 = get();102assertEquals(val1, 0);103assertEquals(val2, 1L);104}105}106107/* ==================================================== */108109static class LongStable {110public @Stable long v;111112public static final LongStable c = new LongStable();113public static long get() { return c.v; }114public static void test() throws Exception {115c.v = 5; long val1 = get();116c.v = Long.MAX_VALUE; long val2 = get();117assertEquals(val1, 5);118assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE));119}120}121122/* ==================================================== */123124static class DefaultStaticValue {125public static @Stable long v;126127public static final DefaultStaticValue c = new DefaultStaticValue();128public static long get() { return c.v; }129public static void test() throws Exception {130long val1 = get();131c.v = 1L; long val2 = get();132assertEquals(val1, 0);133assertEquals(val2, 1L);134}135}136137/* ==================================================== */138139static class StaticLongStable {140public static @Stable long v;141142public static final StaticLongStable c = new StaticLongStable();143public static long get() { return c.v; }144public static void test() throws Exception {145c.v = 5; long val1 = get();146c.v = Long.MAX_VALUE; long val2 = get();147assertEquals(val1, 5);148assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE));149}150}151152/* ==================================================== */153154static class VolatileLongStable {155public @Stable volatile long v;156157public static final VolatileLongStable c = new VolatileLongStable();158public static long get() { return c.v; }159public static void test() throws Exception {160c.v = 5; long val1 = get();161c.v = Long.MAX_VALUE; long val2 = get();162assertEquals(val1, 5);163assertEquals(val2, (isStableEnabled ? 5 : Long.MAX_VALUE));164}165}166167/* ==================================================== */168// @Stable array == field && all components are stable169170static class LongArrayDim1 {171public @Stable long[] v;172173public static final LongArrayDim1 c = new LongArrayDim1();174public static long get() { return c.v[0]; }175public static long get1() { return c.v[10]; }176public static long[] get2() { return c.v; }177public static void test() throws Exception {178{179c.v = new long[1]; c.v[0] = 1; long val1 = get();180c.v[0] = 2; long val2 = get();181assertEquals(val1, 1);182assertEquals(val2, (isStableEnabled ? 1 : 2));183184c.v = new long[1]; c.v[0] = 3; long val3 = get();185assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)186: 3));187}188189{190c.v = new long[20]; c.v[10] = 1; long val1 = get1();191c.v[10] = 2; long val2 = get1();192assertEquals(val1, 1);193assertEquals(val2, (isStableEnabled ? 1 : 2));194195c.v = new long[20]; c.v[10] = 3; long val3 = get1();196assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)197: 3));198}199200{201c.v = new long[1]; long[] val1 = get2();202c.v = new long[1]; long[] val2 = get2();203assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));204}205}206}207208/* ==================================================== */209210static class LongArrayDim2 {211public @Stable long[][] v;212213public static final LongArrayDim2 c = new LongArrayDim2();214public static long get() { return c.v[0][0]; }215public static long[] get1() { return c.v[0]; }216public static long[][] get2() { return c.v; }217public static void test() throws Exception {218{219c.v = new long[1][1]; c.v[0][0] = 1; long val1 = get();220c.v[0][0] = 2; long val2 = get();221assertEquals(val1, 1);222assertEquals(val2, (isStableEnabled ? 1 : 2));223224c.v = new long[1][1]; c.v[0][0] = 3; long val3 = get();225assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)226: 3));227228c.v[0] = new long[1]; c.v[0][0] = 4; long val4 = get();229assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1 : 2)230: 4));231}232233{234c.v = new long[1][1]; long[] val1 = get1();235c.v[0] = new long[1]; long[] val2 = get1();236assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));237}238239{240c.v = new long[1][1]; long[][] val1 = get2();241c.v = new long[1][1]; long[][] val2 = get2();242assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));243}244}245}246247/* ==================================================== */248249static class LongArrayDim3 {250public @Stable long[][][] v;251252public static final LongArrayDim3 c = new LongArrayDim3();253public static long get() { return c.v[0][0][0]; }254public static long[] get1() { return c.v[0][0]; }255public static long[][] get2() { return c.v[0]; }256public static long[][][] get3() { return c.v; }257public static void test() throws Exception {258{259c.v = new long[1][1][1]; c.v[0][0][0] = 1; long val1 = get();260c.v[0][0][0] = 2; long val2 = get();261assertEquals(val1, 1);262assertEquals(val2, (isStableEnabled ? 1 : 2));263264c.v = new long[1][1][1]; c.v[0][0][0] = 3; long val3 = get();265assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)266: 3));267268c.v[0] = new long[1][1]; c.v[0][0][0] = 4; long val4 = get();269assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1 : 2)270: 4));271272c.v[0][0] = new long[1]; c.v[0][0][0] = 5; long val5 = get();273assertEquals(val5, (isStableEnabled ? (isStableEnabled ? 1 : 2)274: 5));275}276277{278c.v = new long[1][1][1]; long[] val1 = get1();279c.v[0][0] = new long[1]; long[] val2 = get1();280assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));281}282283{284c.v = new long[1][1][1]; long[][] val1 = get2();285c.v[0] = new long[1][1]; long[][] val2 = get2();286assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));287}288289{290c.v = new long[1][1][1]; long[][][] val1 = get3();291c.v = new long[1][1][1]; long[][][] val2 = get3();292assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));293}294}295}296297/* ==================================================== */298299static class LongArrayDim4 {300public @Stable long[][][][] v;301302public static final LongArrayDim4 c = new LongArrayDim4();303public static long get() { return c.v[0][0][0][0]; }304public static long[] get1() { return c.v[0][0][0]; }305public static long[][] get2() { return c.v[0][0]; }306public static long[][][] get3() { return c.v[0]; }307public static long[][][][] get4() { return c.v; }308public static void test() throws Exception {309{310c.v = new long[1][1][1][1]; c.v[0][0][0][0] = 1; long val1 = get();311c.v[0][0][0][0] = 2; long val2 = get();312assertEquals(val1, 1);313assertEquals(val2, (isStableEnabled ? 1 : 2));314315c.v = new long[1][1][1][1]; c.v[0][0][0][0] = 3; long val3 = get();316assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)317: 3));318319c.v[0] = new long[1][1][1]; c.v[0][0][0][0] = 4; long val4 = get();320assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1 : 2)321: 4));322323c.v[0][0] = new long[1][1]; c.v[0][0][0][0] = 5; long val5 = get();324assertEquals(val5, (isStableEnabled ? (isStableEnabled ? 1 : 2)325: 5));326327c.v[0][0][0] = new long[1]; c.v[0][0][0][0] = 6; long val6 = get();328assertEquals(val6, (isStableEnabled ? (isStableEnabled ? 1 : 2)329: 6));330}331332{333c.v = new long[1][1][1][1]; long[] val1 = get1();334c.v[0][0][0] = new long[1]; long[] val2 = get1();335assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));336}337338{339c.v = new long[1][1][1][1]; long[][] val1 = get2();340c.v[0][0] = new long[1][1]; long[][] val2 = get2();341assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));342}343344{345c.v = new long[1][1][1][1]; long[][][] val1 = get3();346c.v[0] = new long[1][1][1]; long[][][] val2 = get3();347assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));348}349350{351c.v = new long[1][1][1][1]; long[][][][] val1 = get4();352c.v = new long[1][1][1][1]; long[][][][] 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 long get() { return ((long[])c.v)[0]; }365public static long[] get1() { return (long[])c.v; }366367public static void test() throws Exception {368{369c.v = new long[1]; ((long[])c.v)[0] = 1; long val1 = get();370((long[])c.v)[0] = 2; long val2 = get();371372assertEquals(val1, 1);373assertEquals(val2, 2);374}375376{377c.v = new long[1]; long[] val1 = get1();378c.v = new long[1]; long[] 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 long get() { return ((long[][])c.v)[0][0]; }391public static long[] get1() { return (long[])(c.v[0]); }392public static Object[] get2() { return c.v; }393394public static void test() throws Exception {395{396c.v = new long[1][1]; ((long[][])c.v)[0][0] = 1; long val1 = get();397((long[][])c.v)[0][0] = 2; long val2 = get();398399assertEquals(val1, 1);400assertEquals(val2, 2);401}402403{404c.v = new long[1][1]; c.v[0] = new long[0]; long[] val1 = get1();405c.v[0] = new long[0]; long[] val2 = get1();406407assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));408}409410{411c.v = new long[0][0]; Object[] val1 = get2();412c.v = new long[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 long get() { return ((long[][][])c.v)[0][0][0]; }426public static long[] get1() { return (long[])(c.v[0][0]); }427public static long[][] get2() { return (long[][])(c.v[0]); }428public static Object[][] get3() { return c.v; }429430public static void test() throws Exception {431{432c.v = new long[1][1][1]; ((long[][][])c.v)[0][0][0] = 1L; long val1 = get();433((long[][][])c.v)[0][0][0] = 2L; long val2 = get();434435assertEquals(val1, 1L);436assertEquals(val2, 2L);437}438439{440c.v = new long[1][1][1]; c.v[0][0] = new long[0]; long[] val1 = get1();441c.v[0][0] = new long[0]; long[] val2 = get1();442443assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));444}445446{447c.v = new long[1][1][1]; c.v[0] = new long[0][0]; long[][] val1 = get2();448c.v[0] = new long[0][0]; long[][] val2 = get2();449450assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));451}452453{454c.v = new long[0][0][0]; Object[][] val1 = get3();455c.v = new long[0][0][0]; Object[][] val2 = get3();456457assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));458}459}460}461462/* ==================================================== */463464static class NestedStableField {465static class A {466public @Stable long a;467468}469public @Stable A v;470471public static final NestedStableField c = new NestedStableField();472public static A get() { return c.v; }473public static long get1() { return get().a; }474475public static void test() throws Exception {476{477c.v = new A(); c.v.a = 1; A val1 = get();478c.v.a = 2; A val2 = get();479480assertEquals(val1.a, 2);481assertEquals(val2.a, 2);482}483484{485c.v = new A(); c.v.a = 1; long val1 = get1();486c.v.a = 2; long val2 = get1();487c.v = new A(); c.v.a = 3; long val3 = get1();488489assertEquals(val1, 1);490assertEquals(val2, (isStableEnabled ? 1 : 2));491assertEquals(val3, (isStableEnabled ? 1 : 3));492}493}494}495496/* ==================================================== */497498static class NestedStableField1 {499static class A {500public @Stable long 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 long 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; c.v.next.a = 1; A val1 = get();513c.v.a = 2; c.v.next.a = 2; A val2 = get();514515assertEquals(val1.a, 2);516assertEquals(val2.a, 2);517}518519{520c.v = new A(); c.v.next = c.v;521c.v.a = 1; long val1 = get1();522c.v.a = 2; long val2 = get1();523c.v = new A(); c.v.next = c.v;524c.v.a = 3; long val3 = get1();525526assertEquals(val1, 1);527assertEquals(val2, (isStableEnabled ? 1 : 2));528assertEquals(val3, (isStableEnabled ? 1 : 3));529}530}531}532/* ==================================================== */533534static class NestedStableField2 {535static class A {536public @Stable long a;537public @Stable A left;538public A right;539}540541public @Stable A v;542543public static final NestedStableField2 c = new NestedStableField2();544public static long get() { return c.v.left.left.left.a; }545public static long 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; long val1 = get(); long val2 = get1();551c.v.a = 2; long val3 = get(); long val4 = get1();552553assertEquals(val1, 1);554assertEquals(val3, (isStableEnabled ? 1 : 2));555556assertEquals(val2, 1);557assertEquals(val4, 2);558}559}560}561562/* ==================================================== */563564static class NestedStableField3 {565static class A {566public @Stable long a;567public @Stable A[] left;568public A[] right;569}570571public @Stable A[] v;572573public static final NestedStableField3 c = new NestedStableField3();574public static long get() { return c.v[0].left[1].left[0].left[1].a; }575public static long 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; long val1 = get(); long val2 = get1();582elem.a = 2; long val3 = get(); long val4 = get1();583584assertEquals(val1, 1);585assertEquals(val3, (isStableEnabled ? 1 : 2));586587assertEquals(val2, 1);588assertEquals(val4, 2);589}590}591}592593/* ==================================================== */594// Auxiliary methods595static void assertEquals(long i, long 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