Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Last active April 8, 2018 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rednaxelafx/8107647 to your computer and use it in GitHub Desktop.
Save rednaxelafx/8107647 to your computer and use it in GitHub Desktop.
Example of HotSpot 25's C1 compiling a MethodHandle (Part II): inlines the MH graph, because root of compilation is the driver() which contains the MH.invokeExact() call site, and sees the MH is a "constant". Part I is at https://gist.github.com/rednaxelafx/8107598
diff -r a632dd6ef1f9 src/share/vm/c1/c1_GraphBuilder.cpp
--- a/src/share/vm/c1/c1_GraphBuilder.cpp Mon Dec 16 00:44:46 2013 -0800
+++ b/src/share/vm/c1/c1_GraphBuilder.cpp Mon Dec 23 18:02:23 2013 -0800
@@ -4015,6 +4015,14 @@
// get MethodHandle receiver
const int args_base = state()->stack_size() - callee->arg_size();
ValueType* type = state()->stack_at(args_base)->type();
+ {
+ tty->print(__FILE__ ":%d: type = ", __LINE__);
+ type->print();
+ tty->print_cr(" %s", type->is_constant() ? "is constant" : "not constant");
+ Value receiver = state()->stack_at(args_base);
+ receiver->print();
+ tty->cr();
+ }
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.
import java.lang.invoke.*;
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
public class MHTest {
private static final Lookup LK = lookup();
private static final MethodHandle MH0;
private static final MethodHandle MH1;
static {
MethodHandle mh0 = null;
MethodHandle mh1 = null;
try {
mh0 = LK.findStatic(LK.lookupClass(),
"foo",
methodType(int.class,
int.class, int.class));
mh1 = insertArguments(mh0, 1, 42);
} catch (Throwable t) {
t.printStackTrace();
}
MH0 = mh0;
MH1 = mh1;
}
public static int foo(int x, int y) {
// System.out.println("x: " + x + ", y: " + y);
return x + y;
}
public static void driver() throws Throwable {
int dummy = (int) MH1.invokeExact(0);
if (dummy != 42) {
throw new RuntimeException("wrong behavior");
}
}
public static void main(String[] args) throws Throwable {
for (int i = 0; i < 1200; i++) {
driver();
}
}
}
512 132 b 1 MHTest::driver (25 bytes)
@ 4 java.lang.invoke.LambdaForm$MH/1414644648::invokeExact_MT (14 bytes) force inline by annotation
@ 2 java.lang.invoke.Invokers::checkExactType (30 bytes) force inline by annotation
@ 11 java.lang.invoke.MethodHandle::type (5 bytes)
/Users/krismo/code/hotspot-comp/hotspot/src/share/vm/c1/c1_GraphBuilder.cpp:4019: type = object is constant
__bci__use__tid____instr____________________________________
0 0 a4 <object 0x00007f9869450220 klass=java/lang/invoke/BoundMethodHandle$Species_LI>
@ 10 java.lang.invoke.LambdaForm$BMH/1072591677::reinvoke (26 bytes) force inline by annotation
@ 12 java.lang.invoke.BoundMethodHandle$Species_LI::reinvokerTarget (8 bytes)
/Users/krismo/code/hotspot-comp/hotspot/src/share/vm/c1/c1_GraphBuilder.cpp:4019: type = object is constant
__bci__use__tid____instr____________________________________
1 0 a30 <object 0x00007f9869450238 klass=java/lang/invoke/DirectMethodHandle>
@ 22 java.lang.invoke.LambdaForm$DMH/621009875::invokeStatic_II_I (15 bytes) force inline by annotation
@ 1 java.lang.invoke.DirectMethodHandle::internalMemberName (8 bytes) force inline by annotation
@ 11 MHTest::foo (4 bytes)
514 133 b 1 java.lang.invoke.LambdaForm$MH/1414644648::invokeExact_MT (14 bytes)
@ 2 java.lang.invoke.Invokers::checkExactType (30 bytes) force inline by annotation
@ 11 java.lang.invoke.MethodHandle::type (5 bytes)
@ 25 java.lang.invoke.Invokers::newWrongMethodTypeException (36 bytes) callee is too large
/Users/krismo/code/hotspot-comp/hotspot/src/share/vm/c1/c1_GraphBuilder.cpp:4019: type = object not constant
__bci__use__tid____instr____________________________________
. 6 0 a19 checkcast(a1) java/lang/invoke/MethodHandle
@ 10 java.lang.invoke.MethodHandle::invokeBasic(I)I (0 bytes) receiver not constant
515 134 b 1 java.lang.invoke.Invokers::checkExactType (30 bytes)
@ 11 java.lang.invoke.MethodHandle::type (5 bytes)
@ 25 java.lang.invoke.Invokers::newWrongMethodTypeException (36 bytes) callee is too large
516 135 b 1 java.lang.invoke.BoundMethodHandle$Species_LI::reinvokerTarget (8 bytes)
517 136 b 1 MHTest::foo (4 bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment