1 /*
  2  * Copyright Amazon.com Inc. 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 
 25 /**
 26  * @test id=C1
 27  * @bug 8280481
 28  * @summary Checks that stubs to the interpreter can be shared for static or final method.
 29  * @library /test/lib
 30  * @requires vm.opt.TieredStopAtLevel == null & vm.opt.TieredCompilation == null
 31  * @requires vm.simpleArch == "x86" | vm.simpleArch == "x64" | vm.simpleArch == "aarch64" | vm.simpleArch == "riscv64"
 32  * @requires vm.debug
 33  * @run driver compiler.sharedstubs.SharedStubToInterpTest -XX:TieredStopAtLevel=1
 34  *
 35  * @test id=C2
 36  * @requires vm.opt.TieredStopAtLevel == null & vm.opt.TieredCompilation == null
 37  * @requires vm.simpleArch == "x86" | vm.simpleArch == "x64" | vm.simpleArch == "aarch64" | vm.simpleArch == "riscv64"
 38  * @requires vm.debug
 39  * @run driver compiler.sharedstubs.SharedStubToInterpTest -XX:-TieredCompilation
 40  *
 41  */
 42 
 43 package compiler.sharedstubs;
 44 
 45 import java.util.ArrayList;
 46 import java.util.List;
 47 import java.util.regex.Pattern;
 48 import jdk.test.lib.process.OutputAnalyzer;
 49 import jdk.test.lib.process.ProcessTools;
 50 
 51 public class SharedStubToInterpTest {
 52     private final static int ITERATIONS_TO_HEAT_LOOP = 20_000;
 53 
 54     private static void runTest(String compiler, String test) throws Exception {
 55         String testClassName = SharedStubToInterpTest.class.getName() + "$" + test;
 56         ArrayList<String> command = new ArrayList<String>();
 57         command.add(compiler);
 58         command.add("-XX:+UnlockDiagnosticVMOptions");
 59         command.add("-Xbatch");
 60         command.add("-XX:+PrintRelocations");
 61         command.add("-XX:CompileCommand=compileonly," + testClassName + "::" + "test");
 62         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "test");
 63         command.add("-XX:CompileCommand=print," + testClassName + "::" + "test");
 64         command.add("-XX:CompileCommand=exclude," + testClassName + "::" + "log01");
 65         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "log01");
 66         command.add("-XX:CompileCommand=exclude," + testClassName + "::" + "log02");
 67         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "log02");
 68         command.add(testClassName);
 69 
 70         ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(command);
 71 
 72         OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
 73 
 74         analyzer.shouldHaveExitValue(0);
 75 
 76         System.out.println(analyzer.getOutput());
 77 
 78         checkOutput(analyzer);
 79     }
 80 
 81     public static void main(String[] args) throws Exception {
 82         String[] methods = new String[] { "StaticMethodTest", "FinalClassTest", "FinalMethodTest"};
 83         for (String methodName : methods) {
 84             runTest(args[0], methodName);
 85         }
 86     }
 87 
 88     private static void checkOutput(OutputAnalyzer output) {
 89         List<String> addrs = Pattern.compile("\\(static_stub\\) addr=(\\w+) .*\\[static_call=")
 90             .matcher(output.getStdout())
 91             .results()
 92             .map(m -> m.group(1))
 93             .toList();
 94         if (addrs.stream().distinct().count() >= addrs.size()) {
 95             throw new RuntimeException("No static stubs reused: distinct " + addrs.stream().distinct().count() + ", in total " + addrs.size());
 96         }
 97     }
 98 
 99     public static class StaticMethodTest {
100         static void log01(int i) {
101         }
102         static void log02(int i) {
103         }
104 
105         static void test(int i) {
106             if (i % 3 == 0) {
107                 log01(i);
108                 log02(i);
109             } else {
110                 log01(i);
111                 log02(i);
112             }
113         }
114 
115         public static void main(String[] args) {
116             for (int i = 1; i < ITERATIONS_TO_HEAT_LOOP; ++i) {
117                 test(i);
118             }
119         }
120     }
121 
122     public static final class FinalClassTest {
123         void log01(int i) {
124         }
125         void log02(int i) {
126         }
127 
128         void test(int i) {
129             if (i % 3 == 0) {
130                 log01(i);
131                 log02(i);
132             } else {
133                 log01(i);
134                 log02(i);
135             }
136         }
137 
138         public static void main(String[] args) {
139             FinalClassTest tFC = new FinalClassTest();
140             for (int i = 1; i < ITERATIONS_TO_HEAT_LOOP; ++i) {
141                 tFC.test(i);
142             }
143         }
144     }
145 
146     public static class FinalMethodTest {
147         final void log01(int i) {
148         }
149         final void log02(int i) {
150         }
151 
152         void test(int i) {
153             if (i % 3 == 0) {
154                 log01(i);
155                 log02(i);
156             } else {
157                 log01(i);
158                 log02(i);
159             }
160         }
161 
162         public static void main(String[] args) {
163             FinalMethodTest tFM = new FinalMethodTest();
164             for (int i = 1; i < ITERATIONS_TO_HEAT_LOOP; ++i) {
165                 tFM.test(i);
166             }
167         }
168     }
169 }