8252058: [JVMCI] Rework setting is_method_handle_invoke flag in jvmciCodeInstaller

Reviewed-by: kvn, dlong
diff --git a/src/hotspot/share/code/compiledMethod.hpp b/src/hotspot/share/code/compiledMethod.hpp
index f43e7ee..02cbd4c 100644
--- a/src/hotspot/share/code/compiledMethod.hpp
+++ b/src/hotspot/share/code/compiledMethod.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2020, 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
@@ -330,7 +330,7 @@
   // Deopt
   // Return true is the PC is one would expect if the frame is being deopted.
   inline bool is_deopt_pc(address pc);
-  bool is_deopt_mh_entry(address pc) { return pc == deopt_mh_handler_begin(); }
+  inline bool is_deopt_mh_entry(address pc);
   inline bool is_deopt_entry(address pc);
 
   virtual bool can_convert_to_zombie() = 0;
diff --git a/src/hotspot/share/code/compiledMethod.inline.hpp b/src/hotspot/share/code/compiledMethod.inline.hpp
index 99c120a..670a2ae 100644
--- a/src/hotspot/share/code/compiledMethod.inline.hpp
+++ b/src/hotspot/share/code/compiledMethod.inline.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2020, 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
@@ -41,6 +41,14 @@
     ;
 }
 
+inline bool CompiledMethod::is_deopt_mh_entry(address pc) {
+  return pc == deopt_mh_handler_begin()
+#if INCLUDE_JVMCI
+    || (is_compiled_by_jvmci() && pc == (deopt_mh_handler_begin() + NativeCall::instruction_size))
+#endif
+    ;
+}
+
 // -----------------------------------------------------------------------------
 // CompiledMethod::get_deopt_original_pc
 //
diff --git a/src/hotspot/share/jvmci/jvmciCodeInstaller.cpp b/src/hotspot/share/jvmci/jvmciCodeInstaller.cpp
index f44a5b8..4f6ad79 100644
--- a/src/hotspot/share/jvmci/jvmciCodeInstaller.cpp
+++ b/src/hotspot/share/jvmci/jvmciCodeInstaller.cpp
@@ -1043,7 +1043,7 @@
   return objects;
 }
 
-void CodeInstaller::record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool return_oop, JVMCI_TRAPS) {
+void CodeInstaller::record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {
   JVMCIObject position = jvmci_env()->get_DebugInfo_bytecodePosition(debug_info);
   if (position.is_null()) {
     // Stubs do not record scope info, just oop maps
@@ -1056,7 +1056,7 @@
   } else {
     objectMapping = NULL;
   }
-  record_scope(pc_offset, position, scope_mode, objectMapping, return_oop, JVMCI_CHECK);
+  record_scope(pc_offset, position, scope_mode, objectMapping, is_mh_invoke, return_oop, JVMCI_CHECK);
 }
 
 int CodeInstaller::map_jvmci_bci(int bci) {
@@ -1079,7 +1079,7 @@
   return bci;
 }
 
-void CodeInstaller::record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool return_oop, JVMCI_TRAPS) {
+void CodeInstaller::record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {
   JVMCIObject frame;
   if (scope_mode == CodeInstaller::FullFrame) {
     if (!jvmci_env()->isa_BytecodeFrame(position)) {
@@ -1089,7 +1089,7 @@
   }
   JVMCIObject caller_frame = jvmci_env()->get_BytecodePosition_caller(position);
   if (caller_frame.is_non_null()) {
-    record_scope(pc_offset, caller_frame, scope_mode, objects, return_oop, JVMCI_CHECK);
+    record_scope(pc_offset, caller_frame, scope_mode, objects, is_mh_invoke, return_oop, JVMCI_CHECK);
   }
 
   JVMCIObject hotspot_method = jvmci_env()->get_BytecodePosition_method(position);
@@ -1181,7 +1181,7 @@
     throw_exception = jvmci_env()->get_BytecodeFrame_rethrowException(frame) == JNI_TRUE;
   }
 
-  _debug_recorder->describe_scope(pc_offset, method, NULL, bci, reexecute, throw_exception, false, return_oop,
+  _debug_recorder->describe_scope(pc_offset, method, NULL, bci, reexecute, throw_exception, is_mh_invoke, return_oop,
                                   locals_token, expressions_token, monitors_token);
 }
 
@@ -1236,9 +1236,19 @@
     OopMap *map = create_oop_map(debug_info, JVMCI_CHECK);
     _debug_recorder->add_safepoint(next_pc_offset, map);
 
-    bool return_oop = hotspot_method.is_non_null() && jvmci_env()->asMethod(hotspot_method)->is_returning_oop();
-
-    record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, return_oop, JVMCI_CHECK);
+    if (hotspot_method.is_non_null()) {
+      Method *method = jvmci_env()->asMethod(hotspot_method);
+      vmIntrinsics::ID iid = method->intrinsic_id();
+      bool is_mh_invoke = false;
+      if (jvmci_env()->get_site_Call_direct(site)) {
+        is_mh_invoke = !method->is_static() && (iid == vmIntrinsics::_compiledLambdaForm ||
+                (MethodHandles::is_signature_polymorphic(iid) && MethodHandles::is_signature_polymorphic_intrinsic(iid)));
+      }
+      bool return_oop = method->is_returning_oop();
+      record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, is_mh_invoke, return_oop, JVMCI_CHECK);
+    } else {
+      record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, JVMCI_CHECK);
+    }
   }
 
   if (foreign_call.is_non_null()) {
@@ -1347,6 +1357,9 @@
       case DEOPT_HANDLER_ENTRY:
         _offsets.set_value(CodeOffsets::Deopt, pc_offset);
         break;
+      case DEOPT_MH_HANDLER_ENTRY:
+        _offsets.set_value(CodeOffsets::DeoptMH, pc_offset);
+        break;
       case FRAME_COMPLETE:
         _offsets.set_value(CodeOffsets::Frame_Complete, pc_offset);
         break;
diff --git a/src/hotspot/share/jvmci/jvmciCodeInstaller.hpp b/src/hotspot/share/jvmci/jvmciCodeInstaller.hpp
index 45f9232..0bbaf3f 100644
--- a/src/hotspot/share/jvmci/jvmciCodeInstaller.hpp
+++ b/src/hotspot/share/jvmci/jvmciCodeInstaller.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2020, 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
@@ -159,6 +159,7 @@
     CRC_TABLE_ADDRESS,
     LOG_OF_HEAP_REGION_GRAIN_BYTES,
     INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED,
+    DEOPT_MH_HANDLER_ENTRY,
     INVOKE_INVALID                         = -1
   };
 
@@ -297,11 +298,11 @@
 
   int map_jvmci_bci(int bci);
 
-  void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool return_oop, JVMCI_TRAPS);
+  void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS);
   void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, JVMCI_TRAPS) {
-    record_scope(pc_offset, debug_info, scope_mode, false /* return_oop */, JVMCIENV);
+    record_scope(pc_offset, debug_info, scope_mode, false /* is_mh_invoke */, false /* return_oop */, JVMCIENV);
   }
-  void record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool return_oop, JVMCI_TRAPS);
+  void record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS);
   void record_object_value(ObjectValue* sv, JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS);
 
   GrowableArray<ScopeValue*>* record_virtual_objects(JVMCIObject debug_info, JVMCI_TRAPS);
diff --git a/src/hotspot/share/jvmci/jvmciJavaClasses.hpp b/src/hotspot/share/jvmci/jvmciJavaClasses.hpp
index fae5480..91c9302 100644
--- a/src/hotspot/share/jvmci/jvmciJavaClasses.hpp
+++ b/src/hotspot/share/jvmci/jvmciJavaClasses.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2020, 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
@@ -175,6 +175,7 @@
   end_class                                                                                                   \
   start_class(site_Call, jdk_vm_ci_code_site_Call)                                                            \
     object_field(site_Call, target, "Ljdk/vm/ci/meta/InvokeTarget;")                                          \
+    boolean_field(site_Call, direct)                                                                          \
   end_class                                                                                                   \
   start_class(site_DataPatch, jdk_vm_ci_code_site_DataPatch)                                                  \
     object_field(site_DataPatch, reference, "Ljdk/vm/ci/code/site/Reference;")                                \
diff --git a/src/hotspot/share/jvmci/vmStructs_jvmci.cpp b/src/hotspot/share/jvmci/vmStructs_jvmci.cpp
index f6aec1f..e12eca3 100644
--- a/src/hotspot/share/jvmci/vmStructs_jvmci.cpp
+++ b/src/hotspot/share/jvmci/vmStructs_jvmci.cpp
@@ -471,8 +471,14 @@
   declare_constant(CodeInstaller::CRC_TABLE_ADDRESS)                      \
   declare_constant(CodeInstaller::LOG_OF_HEAP_REGION_GRAIN_BYTES)         \
   declare_constant(CodeInstaller::INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED) \
+  declare_constant(CodeInstaller::DEOPT_MH_HANDLER_ENTRY)                 \
   declare_constant(CodeInstaller::INVOKE_INVALID)                         \
                                                                           \
+  declare_constant(vmIntrinsics::FIRST_MH_SIG_POLY)                       \
+  declare_constant(vmIntrinsics::LAST_MH_SIG_POLY)                        \
+  declare_constant(vmIntrinsics::_invokeGeneric)                          \
+  declare_constant(vmIntrinsics::_compiledLambdaForm)                     \
+                                                                          \
   declare_constant(CollectedHeap::Serial)                                 \
   declare_constant(CollectedHeap::Parallel)                               \
   declare_constant(CollectedHeap::G1)                                     \
@@ -966,4 +972,3 @@
   JVMCIVMStructs::init();
 }
 #endif // ASSERT
-