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 test) throws Exception {
 55         String testClassName = SharedStubToInterpTest.class.getName() + "$" + test;
 56         ArrayList<String> command = new ArrayList<String>();
 57         command.add("-XX:+UnlockDiagnosticVMOptions");
 58         command.add("-Xbatch");
 59         command.add("-XX:+PrintRelocations");
 60         command.add("-XX:CompileCommand=compileonly," + testClassName + "::" + "test");
 61         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "test");
 62         command.add("-XX:CompileCommand=print," + testClassName + "::" + "test");
 63         command.add("-XX:CompileCommand=exclude," + testClassName + "::" + "log01");
 64         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "log01");
 65         command.add("-XX:CompileCommand=exclude," + testClassName + "::" + "log02");
 66         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "log02");
 67         command.add(testClassName);
 68 
 69         ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(command);
 70 
 71         OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
 72 
 73         analyzer.shouldHaveExitValue(0);
 74 
 75         System.out.println(analyzer.getOutput());
 76 
 77         checkOutput(analyzer);
 78     }
 79 
 80     public static void main(String[] args) throws Exception {
 81         String[] methods = new String[] { "StaticMethodTest", "FinalClassTest", "FinalMethodTest"};
 82         for (String methodName : methods) {
 83             runTest(methodName);
 84         }
 85     }
 86 
 87     private static void checkOutput(OutputAnalyzer output) {
 88         List<String> addrs = Pattern.compile("\\(static_stub\\) addr=(\\w+) .*\\[static_call=")
 89             .matcher(output.getStdout())
 90             .results()
 91             .map(m -> m.group(1))
 92             .toList();
 93         if (addrs.stream().distinct().count() >= addrs.size()) {
 94             throw new RuntimeException("No static stubs reused: distinct " + addrs.stream().distinct().count() + ", in total " + addrs.size());
 95         }
 96     }
 97 
 98     public static class StaticMethodTest {
 99         static void log01(int i) {
100         }
101         static void log02(int i) {
102         }
103 
104         static void test(int i) {
105             if (i % 3 == 0) {
106                 log01(i);
107                 log02(i);
108             } else {
109                 log01(i);
110                 log02(i);
111             }
112         }
113 
114         public static void main(String[] args) {
115             for (int i = 1; i < ITERATIONS_TO_HEAT_LOOP; ++i) {
116                 test(i);
117             }
118         }
119     }
120 
121     public static final class FinalClassTest {
122         void log01(int i) {
123         }
124         void log02(int i) {
125         }
126 
127         void test(int i) {
128             if (i % 3 == 0) {
129                 log01(i);
130                 log02(i);
131             } else {
132                 log01(i);
133                 log02(i);
134             }
135         }
136 
137         public static void main(String[] args) {
138             FinalClassTest tFC = new FinalClassTest();
139             for (int i = 1; i < ITERATIONS_TO_HEAT_LOOP; ++i) {
140                 tFC.test(i);
141             }
142         }
143     }
144 
145     public static class FinalMethodTest {
146         final void log01(int i) {
147         }
148         final void log02(int i) {
149         }
150 
151         void test(int i) {
152             if (i % 3 == 0) {
153                 log01(i);
154                 log02(i);
155             } else {
156                 log01(i);
157                 log02(i);
158             }
159         }
160 
161         public static void main(String[] args) {
162             FinalMethodTest tFM = new FinalMethodTest();
163             for (int i = 1; i < ITERATIONS_TO_HEAT_LOOP; ++i) {
164                 tFM.test(i);
165             }
166         }
167     }
168 }