Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit ad34f03

Browse files
author
Vladimir Ivanov
committed
8279515: C1: No inlining through invokedynamic and invokestatic call sites when resolved class is not linked
Reviewed-by: kvn, dlong
1 parent 928e347 commit ad34f03

File tree

4 files changed

+166
-4
lines changed

4 files changed

+166
-4
lines changed

src/hotspot/share/c1/c1_GraphBuilder.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,9 +2072,11 @@ void GraphBuilder::invoke(Bytecodes::Code code) {
20722072
}
20732073

20742074
// check if we could do inlining
2075-
if (!PatchALot && Inline && target->is_loaded() && callee_holder->is_linked() && !patch_for_appendix) {
2075+
if (!PatchALot && Inline && target->is_loaded() && !patch_for_appendix &&
2076+
callee_holder->is_loaded()) { // the effect of symbolic reference resolution
2077+
20762078
// callee is known => check if we have static binding
2077-
if ((code == Bytecodes::_invokestatic && callee_holder->is_initialized()) || // invokestatic involves an initialization barrier on resolved klass
2079+
if ((code == Bytecodes::_invokestatic && klass->is_initialized()) || // invokestatic involves an initialization barrier on declaring class
20782080
code == Bytecodes::_invokespecial ||
20792081
(code == Bytecodes::_invokevirtual && target->is_final_method()) ||
20802082
code == Bytecodes::_invokedynamic) {

src/hotspot/share/ci/ciStreams.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,9 @@ ciKlass* ciBytecodeStream::get_declared_method_holder() {
476476
constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
477477
bool ignore;
478478
// report as MethodHandle for invokedynamic, which is syntactically classless
479-
if (cur_bc() == Bytecodes::_invokedynamic)
480-
return CURRENT_ENV->get_klass_by_name(_holder, ciSymbols::java_lang_invoke_MethodHandle(), false);
479+
if (cur_bc() == Bytecodes::_invokedynamic) {
480+
return CURRENT_ENV->MethodHandle_klass();
481+
}
481482
return CURRENT_ENV->get_klass_by_index(cpool, get_method_holder_index(), ignore, _holder);
482483
}
483484

src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ private String getInternalName(Class<?> c) {
683683
}
684684

685685
private static MemberName resolveFrom(String name, MethodType type, Class<?> holder) {
686+
assert(!UNSAFE.shouldBeInitialized(holder)) : holder + "not initialized";
686687
MemberName member = new MemberName(holder, name, type, REF_invokeStatic);
687688
MemberName resolvedMember = MemberName.getFactory().resolveOrNull(REF_invokeStatic, member, holder, LM_TRUSTED);
688689
traceLambdaForm(name, type, holder, resolvedMember);
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2022, 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+
/**
25+
* @test
26+
* @bug 8279515
27+
*
28+
* @requires vm.flagless
29+
* @modules java.base/jdk.internal.misc
30+
* @library /test/lib /
31+
*
32+
* @run driver compiler.jsr292.ResolvedClassTest
33+
*/
34+
35+
package compiler.jsr292;
36+
37+
import jdk.test.lib.process.OutputAnalyzer;
38+
import jdk.test.lib.process.ProcessTools;
39+
40+
import java.io.IOException;
41+
42+
public class ResolvedClassTest {
43+
/* ======================================================================== */
44+
static void testStatic() throws IOException {
45+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
46+
"-XX:+IgnoreUnrecognizedVMOptions", "-showversion",
47+
"-XX:+PrintCompilation", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining",
48+
"-Xbatch", "-XX:CompileCommand=quiet", "-XX:CompileCommand=compileonly," + TestStatic.class.getName() + "::test",
49+
TestStatic.class.getName());
50+
51+
OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
52+
53+
analyzer.shouldHaveExitValue(0);
54+
55+
analyzer.shouldNotContain("TestStatic$A::m (1 bytes) not inlineable");
56+
analyzer.shouldNotContain("TestStatic$A::m (1 bytes) no static binding");
57+
58+
analyzer.shouldContain("TestStatic$A::m (1 bytes) inline");
59+
}
60+
61+
static class TestStatic {
62+
static class A {
63+
static void m() {}
64+
}
65+
static class B extends A {}
66+
67+
// @DontInline
68+
static void test() {
69+
B.m(); // invokestatic B "m" => A::m
70+
}
71+
72+
public static void main(String[] args) {
73+
for (int i = 0; i < 20_000; i++) {
74+
test();
75+
}
76+
}
77+
}
78+
79+
/* ======================================================================== */
80+
static void testStaticInit() throws IOException {
81+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
82+
"-XX:+IgnoreUnrecognizedVMOptions", "-showversion",
83+
"-XX:+PrintCompilation", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining",
84+
"-Xbatch", "-XX:CompileCommand=quiet", "-XX:CompileCommand=compileonly," + TestStaticInit.class.getName() + "::test",
85+
TestStaticInit.class.getName());
86+
87+
OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
88+
89+
analyzer.shouldHaveExitValue(0);
90+
91+
analyzer.shouldContain("TestStaticInit$A::m (1 bytes) no static binding");
92+
}
93+
94+
static class TestStaticInit {
95+
static class A {
96+
static {
97+
for (int i = 0; i < 20_000; i++) {
98+
TestStaticInit.test();
99+
}
100+
}
101+
102+
static void m() {}
103+
}
104+
static class B extends A {}
105+
106+
// @DontInline
107+
static void test() {
108+
B.m(); // A::<clinit> => test() => A::m()
109+
}
110+
111+
public static void main(String[] args) {
112+
A.m(); // trigger initialization of A
113+
}
114+
}
115+
116+
/* ======================================================================== */
117+
static void testIndy() throws IOException {
118+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
119+
"-XX:+IgnoreUnrecognizedVMOptions", "-showversion",
120+
"-XX:+PrintCompilation", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining",
121+
"-Xbatch", "-XX:CompileCommand=quiet", "-XX:CompileCommand=compileonly," + TestIndy.class.getName() + "::test",
122+
TestIndy.class.getName());
123+
124+
OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
125+
126+
analyzer.shouldHaveExitValue(0);
127+
128+
analyzer.shouldNotContain("java.lang.invoke.Invokers$Holder::linkToTargetMethod (9 bytes) not inlineable");
129+
130+
analyzer.shouldContain("java.lang.invoke.Invokers$Holder::linkToTargetMethod (9 bytes) force inline by annotation");
131+
analyzer.shouldContain("java/lang/invoke/MethodHandle::invokeBasic (not loaded) not inlineable");
132+
}
133+
134+
static class TestIndy {
135+
static String str = "";
136+
137+
// @DontInline
138+
static void test() {
139+
String s1 = "" + str; // indy (linked)
140+
141+
for (int i = 0; i < 200_000; i++) {} // trigger OSR compilation
142+
143+
String s2 = "" + str; // indy (not linked)
144+
}
145+
146+
public static void main(String[] args) {
147+
test();
148+
}
149+
}
150+
151+
/* ======================================================================== */
152+
153+
public static void main(String[] args) throws IOException {
154+
testStatic();
155+
testStaticInit();
156+
testIndy();
157+
}
158+
}

0 commit comments

Comments
 (0)