Skip to content
Permalink
Browse files
8274983: C1 optimizes the invocation of private interface methods
Reviewed-by: xliu
Backport-of: 21d9ca6cd942ac98a3be2577ded8eaf92dac7d46
  • Loading branch information
TheRealMDoerr committed Mar 3, 2022
1 parent 23b7f7e commit 977e773663d5fad07cb37471e00381718f62c889
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();
}
}
}

1 comment on commit 977e773

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 977e773 Mar 3, 2022

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.