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

Reviewed-by: kvn
  • Loading branch information
Vladimir Ivanov committed May 27, 2022
1 parent 0df4748 commit d3e781d
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 @@ -4145,20 +4145,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 @@ -1018,22 +1018,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");
}
}

5 comments on commit d3e781d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on d3e781d Jun 23, 2022

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d3e781d Jun 23, 2022

Choose a reason for hiding this comment

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

@GoeLin Could not automatically backport d3e781de to openjdk/jdk11u-dev due to conflicts in the following files:

  • src/hotspot/share/opto/callGenerator.cpp

To manually resolve these conflicts run the following commands in your personal fork of openjdk/jdk11u-dev:

$ git checkout -b GoeLin-backport-d3e781de
$ git fetch --no-tags https://git.openjdk.org/jdk d3e781de086d557a88105da965ff8a7f9126019c
$ git cherry-pick --no-commit d3e781de086d557a88105da965ff8a7f9126019c
$ # Resolve conflicts
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport d3e781de086d557a88105da965ff8a7f9126019c'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk11u-dev with the title Backport d3e781de086d557a88105da965ff8a7f9126019c.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on d3e781d Jun 23, 2022

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d3e781d Jun 23, 2022

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-d3e781de in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d3e781de from the openjdk/jdk repository.

The commit being backported was authored by Vladimir Ivanov on 27 May 2022 and was reviewed by Vladimir Kozlov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-d3e781de:GoeLin-backport-d3e781de
$ git checkout GoeLin-backport-d3e781de
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-d3e781de

Please sign in to comment.