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