Skip to content
Permalink
Browse files
8274983: C1 optimizes the invocation of private interface methods
Reviewed-by: dlong, iveresov
  • Loading branch information
Xin Liu committed Nov 30, 2021
1 parent 98a9f03 commit 21d9ca6cd942ac98a3be2577ded8eaf92dac7d46
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 12 deletions.
@@ -1865,22 +1865,17 @@ void GraphBuilder::invoke(Bytecodes::Code code) {
log->identify(target),
Bytecodes::name(code));

// invoke-special-super
if (bc_raw == Bytecodes::_invokespecial && !target->is_object_initializer()) {
ciInstanceKlass* sender_klass = calling_klass;
if (sender_klass->is_interface()) {
int index = state()->stack_size() - (target->arg_size_no_receiver() + 1);
Value receiver = state()->stack_at(index);
CheckCast* c = new CheckCast(sender_klass, receiver, copy_state_before());
c->set_invokespecial_receiver_check();
state()->stack_at_put(index, append_split(c));
}
}

// Some methods are obviously bindable without any type checks so
// convert them directly to an invokespecial or invokestatic.
if (target->is_loaded() && !target->is_abstract() && target->can_be_statically_bound()) {
switch (bc_raw) {
case Bytecodes::_invokeinterface:
// convert to invokespecial if the target is the private interface method.
if (target->is_private()) {
assert(holder->is_interface(), "How did we get a non-interface method here!");
code = Bytecodes::_invokespecial;
}
break;
case Bytecodes::_invokevirtual:
code = Bytecodes::_invokespecial;
break;
@@ -1897,6 +1892,26 @@ void GraphBuilder::invoke(Bytecodes::Code code) {
}
}

if (code == Bytecodes::_invokespecial) {
// Additional receiver subtype checks for interface calls via invokespecial or invokeinterface.
ciKlass* receiver_constraint = nullptr;

if (bc_raw == Bytecodes::_invokeinterface) {
receiver_constraint = holder;
} else if (bc_raw == Bytecodes::_invokespecial && !target->is_object_initializer() && calling_klass->is_interface()) {
receiver_constraint = calling_klass;
}

if (receiver_constraint != nullptr) {
int index = state()->stack_size() - (target->arg_size_no_receiver() + 1);
Value receiver = state()->stack_at(index);
CheckCast* c = new CheckCast(receiver_constraint, receiver, copy_state_before());
// go to uncommon_trap when checkcast fails
c->set_invokespecial_receiver_check();
state()->stack_at_put(index, append_split(c));
}
}

// Push appendix argument (MethodType, CallSite, etc.), if one.
bool patch_for_appendix = false;
int patching_appendix_arg = 0;
@@ -0,0 +1,80 @@
/*
* Copyright Amazon.com Inc. 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.
*/
package org.openjdk.bench.vm.compiler;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
public class InterfacePrivateCalls {
interface I {
private int bar() { return 0; }
default int foo() {
return bar();
}
}

static class C1 implements I {}
static class C2 implements I {}
static class C3 implements I {}

private I[] objs;

@Setup(Level.Trial)
public void setupTrial() {
objs = new I[3];
objs[0] = new C1();
objs[1] = new C2();
objs[2] = new C3();
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value=1, jvmArgsAppend={"-XX:TieredStopAtLevel=1"})
public void invokePrivateInterfaceMethodC1() {
for (int i = 0; i < objs.length; ++i) {
objs[i].foo();
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value=1)
public void invokePrivateInterfaceMethodC2() {
for (int i = 0; i < objs.length; ++i) {
objs[i].foo();
}
}
}

3 comments on commit 21d9ca6

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 21d9ca6 Nov 30, 2021

Choose a reason for hiding this comment

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

@TheRealMDoerr
Copy link
Contributor

@TheRealMDoerr TheRealMDoerr commented on 21d9ca6 Mar 2, 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 21d9ca6 Mar 2, 2022

Choose a reason for hiding this comment

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

@TheRealMDoerr the backport was successfully created on the branch TheRealMDoerr-backport-21d9ca6c 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 21d9ca6c from the openjdk/jdk repository.

The commit being backported was authored by Xin Liu on 30 Nov 2021 and was reviewed by Dean Long and Igor Veresov.

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 TheRealMDoerr-backport-21d9ca6c:TheRealMDoerr-backport-21d9ca6c
$ git checkout TheRealMDoerr-backport-21d9ca6c
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev TheRealMDoerr-backport-21d9ca6c

Please sign in to comment.