Skip to content

Commit

Permalink
8287223: C1: Inlining attempt through MH::invokeBasic() with null rec…
Browse files Browse the repository at this point in the history
…eiver

Backport-of: d3e781de086d557a88105da965ff8a7f9126019c
  • Loading branch information
GoeLin committed Jun 24, 2022
1 parent 25059b2 commit 831b3b8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 23 deletions.
28 changes: 18 additions & 10 deletions src/hotspot/share/c1/c1_GraphBuilder.cpp
Expand Up @@ -4051,20 +4051,28 @@ bool GraphBuilder::try_method_handle_inline(ciMethod* callee, bool ignore_return
const int args_base = state()->stack_size() - callee->arg_size();
ValueType* type = state()->stack_at(args_base)->type();
if (type->is_constant()) {
ciMethod* target = type->as_ObjectType()->constant_value()->as_method_handle()->get_vmtarget();
// We don't do CHA here so only inline static and statically bindable methods.
if (target->is_static() || target->can_be_statically_bound()) {
if (ciMethod::is_consistent_info(callee, target)) {
Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual;
ignore_return = ignore_return || (callee->return_type()->is_void() && !target->return_type()->is_void());
if (try_inline(target, /*holder_known*/ !callee->is_static(), ignore_return, bc)) {
return true;
ciObject* mh = type->as_ObjectType()->constant_value();
if (mh->is_method_handle()) {
ciMethod* target = mh->as_method_handle()->get_vmtarget();

// We don't do CHA here so only inline static and statically bindable methods.
if (target->is_static() || target->can_be_statically_bound()) {
if (ciMethod::is_consistent_info(callee, target)) {
Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual;
ignore_return = ignore_return || (callee->return_type()->is_void() && !target->return_type()->is_void());
if (try_inline(target, /*holder_known*/ !callee->is_static(), ignore_return, bc)) {
return true;
}
} else {
print_inlining(target, "signatures mismatch", /*success*/ false);
}
} else {
print_inlining(target, "signatures mismatch", /*success*/ false);
assert(false, "no inlining through MH::invokeBasic"); // missing optimization opportunity due to suboptimal LF shape
print_inlining(target, "not static or statically bindable", /*success*/ false);
}
} else {
print_inlining(target, "not static or statically bindable", /*success*/ false);
assert(mh->is_null_object(), "not a null");
print_inlining(callee, "receiver is always null", /*success*/ false);
}
} else {
print_inlining(callee, "receiver not constant", /*success*/ false);
Expand Down
33 changes: 20 additions & 13 deletions src/hotspot/share/opto/callGenerator.cpp
Expand Up @@ -1115,22 +1115,29 @@ CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod*
Node* receiver = kit.argument(0);
if (receiver->Opcode() == Op_ConP) {
input_not_const = false;
const TypeOopPtr* oop_ptr = receiver->bottom_type()->is_oopptr();
ciMethod* target = oop_ptr->const_oop()->as_method_handle()->get_vmtarget();
const int vtable_index = Method::invalid_vtable_index;
const TypeOopPtr* recv_toop = receiver->bottom_type()->isa_oopptr();
if (recv_toop != NULL) {
ciMethod* target = recv_toop->const_oop()->as_method_handle()->get_vmtarget();
const int vtable_index = Method::invalid_vtable_index;

if (!ciMethod::is_consistent_info(callee, target)) {
print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
"signatures mismatch");
return NULL;
}

if (!ciMethod::is_consistent_info(callee, target)) {
CallGenerator *cg = C->call_generator(target, vtable_index,
false /* call_does_dispatch */,
jvms,
allow_inline,
PROB_ALWAYS);
return cg;
} else {
assert(receiver->bottom_type() == TypePtr::NULL_PTR, "not a null: %s",
Type::str(receiver->bottom_type()));
print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
"signatures mismatch");
return NULL;
"receiver is always null");
}

CallGenerator* cg = C->call_generator(target, vtable_index,
false /* call_does_dispatch */,
jvms,
allow_inline,
PROB_ALWAYS);
return cg;
} else {
print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
"receiver not constant");
Expand Down
55 changes: 55 additions & 0 deletions test/hotspot/jtreg/compiler/jsr292/NullConstantMHReceiver.java
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* @test
* @bug 8287223
* @library /test/lib / patches
*
* @build java.base/java.lang.invoke.MethodHandleHelper
* @run main/bootclasspath/othervm -Xbatch -XX:CompileCommand=compileonly,*::test -XX:-TieredCompilation compiler.jsr292.NullConstantMHReceiver
* @run main/bootclasspath/othervm -Xbatch -XX:CompileCommand=compileonly,*::test -XX:+TieredCompilation -XX:TieredStopAtLevel=1 compiler.jsr292.NullConstantMHReceiver
*/

package compiler.jsr292;

import java.lang.invoke.MethodHandleHelper;

public class NullConstantMHReceiver {
static void test() throws Throwable {
MethodHandleHelper.invokeBasicL(null);
}

public static void main(String[] args) throws Throwable {
for (int i = 0; i < 15000; i++) {
try {
test();
} catch (NullPointerException e) {
// expected
continue;
}
throw new AssertionError("NPE wasn't thrown");
}
System.out.println("TEST PASSED");
}
}

1 comment on commit 831b3b8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.