1 /* 2 * Copyright (c) 2017, 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. 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 package compiler.valhalla.valuetypes; 25 26 import jdk.test.lib.Asserts; 27 28 /* 29 * @test 30 * @summary Test on stack replacement (OSR) with value types 31 * @library /testlibrary /test/lib /compiler/whitebox / 32 * @requires os.simpleArch == "x64" 33 * @compile -XDallowWithFieldOperator TestOnStackReplacement.java 34 * @run driver ClassFileInstaller sun.hotspot.WhiteBox jdk.test.lib.Platform 35 * @run main/othervm/timeout=120 -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions 36 * -XX:+UnlockExperimentalVMOptions -XX:+WhiteBoxAPI -XX:+EnableValhalla 37 * compiler.valhalla.valuetypes.ValueTypeTest 38 * compiler.valhalla.valuetypes.TestOnStackReplacement 39 */ 40 public class TestOnStackReplacement extends ValueTypeTest { 41 // Extra VM parameters for some test scenarios. See ValueTypeTest.getVMParameters() 42 @Override 43 public String[] getExtraVMParameters(int scenario) { 44 switch (scenario) { 45 case 3: return new String[] {"-XX:-ValueArrayFlatten"}; 46 } 47 return null; 48 } 49 50 public static void main(String[] args) throws Throwable { 51 TestOnStackReplacement test = new TestOnStackReplacement(); 52 test.run(args, MyValue1.class, MyValue2.class, MyValue2Inline.class); 53 } 54 55 // Helper methods 56 57 protected long hash() { 58 return hash(rI, rL); 59 } 60 61 protected long hash(int x, long y) { 62 return MyValue1.createWithFieldsInline(x, y).hash(); 63 } 64 65 // Test OSR compilation 66 @Test() 67 public long test1() { 68 MyValue1 v = MyValue1.createWithFieldsInline(rI, rL); 69 MyValue1[] va = new MyValue1[Math.abs(rI) % 3]; 70 for (int i = 0; i < va.length; ++i) { 71 va[i] = MyValue1.createWithFieldsInline(rI, rL); 72 } 73 long result = 0; 74 // Long loop to trigger OSR compilation 75 for (int i = 0 ; i < 50_000; ++i) { 76 // Reference local value type in interpreter state 77 result = v.hash(); 78 for (int j = 0; j < va.length; ++j) { 79 result += va[j].hash(); 80 } 81 } 82 return result; 83 } 84 85 @DontCompile 86 public void test1_verifier(boolean warmup) { 87 long result = test1(); 88 Asserts.assertEQ(result, ((Math.abs(rI) % 3) + 1) * hash()); 89 } 90 91 // Test loop peeling 92 @Test(failOn = ALLOC + LOAD + STORE) 93 public void test2() { 94 MyValue1 v = MyValue1.createWithFieldsInline(0, 1); 95 // Trigger OSR compilation and loop peeling 96 for (int i = 0; i < 50_000; ++i) { 97 if (v.x != i || v.y != i + 1) { 98 // Uncommon trap 99 throw new RuntimeException("test2 failed"); 100 } 101 v = MyValue1.createWithFieldsInline(i + 1, i + 2); 102 } 103 } 104 105 @DontCompile 106 public void test2_verifier(boolean warmup) { 107 test2(); 108 } 109 110 // Test loop peeling and unrolling 111 @Test() 112 public void test3() { 113 MyValue1 v1 = MyValue1.createWithFieldsInline(0, 0); 114 MyValue1 v2 = MyValue1.createWithFieldsInline(1, 1); 115 // Trigger OSR compilation and loop peeling 116 for (int i = 0; i < 50_000; ++i) { 117 if (v1.x != 2*i || v2.x != i+1 || v2.y != i+1) { 118 // Uncommon trap 119 throw new RuntimeException("test3 failed"); 120 } 121 v1 = MyValue1.createWithFieldsInline(2*(i+1), 0); 122 v2 = MyValue1.createWithFieldsInline(i+2, i+2); 123 } 124 } 125 126 @DontCompile 127 public void test3_verifier(boolean warmup) { 128 test3(); 129 } 130 131 // OSR compilation with Object local 132 @DontCompile 133 public Object test4_init() { 134 return MyValue1.createWithFieldsInline(rI, rL); 135 } 136 137 @DontCompile 138 public Object test4_body() { 139 return MyValue1.createWithFieldsInline(rI, rL); 140 } 141 142 @Test() 143 public Object test4() { 144 Object vt = test4_init(); 145 for (int i = 0; i < 50_000; i++) { 146 if (i % 2 == 1) { 147 vt = test4_body(); 148 } 149 } 150 return vt; 151 } 152 153 @DontCompile 154 public void test4_verifier(boolean warmup) { 155 test4(); 156 } 157 158 // OSR compilation with null value type local 159 160 MyValue1.box nullField; 161 162 @Test() 163 public void test5() { 164 MyValue1.box vt = nullField; 165 for (int i = 0; i < 50_000; i++) { 166 if ((Object)vt != null) { 167 throw new RuntimeException("test5 failed: No NPE thrown"); 168 } 169 } 170 } 171 172 @DontCompile 173 public void test5_verifier(boolean warmup) { 174 test5(); 175 } 176 }