< prev index next >

src/hotspot/share/ci/ciEnv.cpp

Print this page

   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 



  25 #include "ci/ciConstant.hpp"
  26 #include "ci/ciEnv.hpp"
  27 #include "ci/ciField.hpp"
  28 #include "ci/ciInstance.hpp"
  29 #include "ci/ciInstanceKlass.hpp"
  30 #include "ci/ciMethod.hpp"
  31 #include "ci/ciNullObject.hpp"
  32 #include "ci/ciReplay.hpp"
  33 #include "ci/ciSymbols.hpp"
  34 #include "ci/ciUtilities.inline.hpp"
  35 #include "classfile/javaClasses.hpp"
  36 #include "classfile/javaClasses.inline.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/vmClasses.hpp"
  39 #include "classfile/vmSymbols.hpp"

  40 #include "code/codeCache.hpp"
  41 #include "code/scopeDesc.hpp"
  42 #include "compiler/compilationLog.hpp"
  43 #include "compiler/compilationPolicy.hpp"
  44 #include "compiler/compileBroker.hpp"
  45 #include "compiler/compileLog.hpp"
  46 #include "compiler/compilerEvent.hpp"
  47 #include "compiler/compileTask.hpp"
  48 #include "compiler/disassembler.hpp"

  49 #include "gc/shared/collectedHeap.inline.hpp"
  50 #include "interpreter/bytecodeStream.hpp"
  51 #include "interpreter/linkResolver.hpp"
  52 #include "jfr/jfrEvents.hpp"
  53 #include "jvm.h"
  54 #include "logging/log.hpp"
  55 #include "memory/allocation.inline.hpp"
  56 #include "memory/oopFactory.hpp"
  57 #include "memory/resourceArea.hpp"
  58 #include "memory/universe.hpp"
  59 #include "oops/constantPool.inline.hpp"
  60 #include "oops/cpCache.inline.hpp"
  61 #include "oops/method.inline.hpp"
  62 #include "oops/methodData.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "oops/resolvedIndyEntry.hpp"
  67 #include "oops/symbolHandle.hpp"

  68 #include "prims/jvmtiExport.hpp"
  69 #include "prims/methodHandles.hpp"
  70 #include "runtime/fieldDescriptor.inline.hpp"

  71 #include "runtime/handles.inline.hpp"
  72 #include "runtime/init.hpp"
  73 #include "runtime/javaThread.hpp"
  74 #include "runtime/jniHandles.inline.hpp"
  75 #include "runtime/reflection.hpp"
  76 #include "runtime/safepointVerifiers.hpp"
  77 #include "runtime/sharedRuntime.hpp"
  78 #include "utilities/dtrace.hpp"
  79 #include "utilities/macros.hpp"
  80 #ifdef COMPILER1
  81 #include "c1/c1_Runtime1.hpp"
  82 #endif
  83 #ifdef COMPILER2
  84 #include "opto/runtime.hpp"
  85 #endif
  86 
  87 // ciEnv
  88 //
  89 // This class is the top level broker for requests from the compiler
  90 // to the VM.

 650       if (is_java_primitive(bt)) {
 651         assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
 652         return unbox_primitive_value(ciobj, bt);
 653       } else {
 654         assert(ciobj->is_instance(), "should be an instance");
 655         return ciConstant(T_OBJECT, ciobj);
 656       }
 657     }
 658   }
 659 }
 660 
 661 // ------------------------------------------------------------------
 662 // ciEnv::get_constant_by_index_impl
 663 //
 664 // Implementation of get_constant_by_index().
 665 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 666                                              int index, int obj_index,
 667                                              ciInstanceKlass* accessor) {
 668   if (obj_index >= 0) {
 669     ciConstant con = get_resolved_constant(cpool, obj_index);
 670     if (con.is_valid()) {
 671       return con;
 672     }
 673   }
 674   constantTag tag = cpool->tag_at(index);
 675   if (tag.is_int()) {
 676     return ciConstant(T_INT, (jint)cpool->int_at(index));
 677   } else if (tag.is_long()) {
 678     return ciConstant((jlong)cpool->long_at(index));
 679   } else if (tag.is_float()) {
 680     return ciConstant((jfloat)cpool->float_at(index));
 681   } else if (tag.is_double()) {
 682     return ciConstant((jdouble)cpool->double_at(index));
 683   } else if (tag.is_string()) {
 684     EXCEPTION_CONTEXT;
 685     assert(obj_index >= 0, "should have an object index");
 686     oop string = cpool->string_at(index, obj_index, THREAD);
 687     if (HAS_PENDING_EXCEPTION) {
 688       CLEAR_PENDING_EXCEPTION;
 689       record_out_of_memory_failure();
 690       return ciConstant();

 804   }
 805 }
 806 
 807 
 808 // ------------------------------------------------------------------
 809 // ciEnv::get_method_by_index_impl
 810 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool,
 811                                           int index, Bytecodes::Code bc,
 812                                           ciInstanceKlass* accessor) {
 813   assert(cpool.not_null(), "need constant pool");
 814   assert(accessor != nullptr, "need origin of access");
 815   if (bc == Bytecodes::_invokedynamic) {
 816     // FIXME: code generation could allow for null (unlinked) call site
 817     // The call site could be made patchable as follows:
 818     // Load the appendix argument from the constant pool.
 819     // Test the appendix argument and jump to a known deopt routine if it is null.
 820     // Jump through a patchable call site, which is initially a deopt routine.
 821     // Patch the call site to the nmethod entry point of the static compiled lambda form.
 822     // As with other two-component call sites, both values must be independently verified.
 823     assert(index < cpool->cache()->resolved_indy_entries_length(), "impossible");
 824     Method* adapter = cpool->resolved_indy_entry_at(index)->method();








 825     // Resolved if the adapter is non null.
 826     if (adapter != nullptr) {
 827       return get_method(adapter);
 828     }
 829 
 830     // Fake a method that is equivalent to a declared method.
 831     ciInstanceKlass* holder    = get_instance_klass(vmClasses::MethodHandle_klass());
 832     ciSymbol*        name      = ciSymbols::invokeBasic_name();
 833     ciSymbol*        signature = get_symbol(cpool->signature_ref_at(index, bc));
 834     return get_unloaded_method(holder, name, signature, accessor);
 835   } else {
 836     const int holder_index = cpool->klass_ref_index_at(index, bc);
 837     bool holder_is_accessible;
 838     ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
 839 
 840     // Get the method's name and signature.
 841     Symbol* name_sym = cpool->name_ref_at(index, bc);
 842     Symbol* sig_sym  = cpool->signature_ref_at(index, bc);
 843 
 844     if (cpool->has_preresolution()

 851       case Bytecodes::_invokevirtual:
 852       case Bytecodes::_invokeinterface:
 853       case Bytecodes::_invokespecial:
 854       case Bytecodes::_invokestatic:
 855         {
 856           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 857           if (m != nullptr) {
 858             return get_method(m);
 859           }
 860         }
 861         break;
 862       default:
 863         break;
 864       }
 865     }
 866 
 867     if (holder_is_accessible) {  // Our declared holder is loaded.
 868       constantTag tag = cpool->tag_ref_at(index, bc);
 869       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 870       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 871       if (m != nullptr &&
 872           (bc == Bytecodes::_invokestatic
 873            ?  m->method_holder()->is_not_initialized()
 874            : !m->method_holder()->is_loaded())) {
 875         m = nullptr;
 876       }
 877       if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
 878         m = nullptr;
 879       }
 880       if (m != nullptr) {
 881         // We found the method.
 882         return get_method(m);
 883       }
 884     }
 885 
 886     // Either the declared holder was not loaded, or the method could
 887     // not be found.  Create a dummy ciMethod to represent the failed
 888     // lookup.
 889     ciSymbol* name      = get_symbol(name_sym);
 890     ciSymbol* signature = get_symbol(sig_sym);
 891     return get_unloaded_method(holder, name, signature, accessor);
 892   }
 893 }
 894 
 895 

 936       _name_buffer =
 937         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 938       _name_buffer_len = req_len;
 939     }
 940   }
 941   return _name_buffer;
 942 }
 943 
 944 // ------------------------------------------------------------------
 945 // ciEnv::is_in_vm
 946 bool ciEnv::is_in_vm() {
 947   return JavaThread::current()->thread_state() == _thread_in_vm;
 948 }
 949 
 950 // ------------------------------------------------------------------
 951 // ciEnv::validate_compile_task_dependencies
 952 //
 953 // Check for changes during compilation (e.g. class loads, evolution,
 954 // breakpoints, call site invalidation).
 955 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 956   if (failing())  return;  // no need for further checks
 957 
 958   Dependencies::DepType result = dependencies()->validate_dependencies(_task);
 959   if (result != Dependencies::end_marker) {
 960     if (result == Dependencies::call_site_target_value) {
 961       _inc_decompile_count_on_failure = false;
 962       record_failure("call site target change");
 963     } else if (Dependencies::is_klass_type(result)) {
 964       record_failure("concurrent class loading");
 965     } else {
 966       record_failure("invalid non-klass dependency");
 967     }
 968   }
 969 }
 970 











































































































































































































 971 // ------------------------------------------------------------------
 972 // ciEnv::register_method
 973 void ciEnv::register_method(ciMethod* target,
 974                             int entry_bci,
 975                             CodeOffsets* offsets,
 976                             int orig_pc_offset,
 977                             CodeBuffer* code_buffer,
 978                             int frame_words,
 979                             OopMapSet* oop_map_set,
 980                             ExceptionHandlerTable* handler_table,
 981                             ImplicitExceptionTable* inc_table,
 982                             AbstractCompiler* compiler,


 983                             bool has_unsafe_access,
 984                             bool has_wide_vectors,
 985                             bool has_monitors,
 986                             bool has_scoped_access,
 987                             int immediate_oops_patched) {


 988   VM_ENTRY_MARK;
 989   nmethod* nm = nullptr;
 990   {
 991     methodHandle method(THREAD, target->get_Method());
 992 
 993     // We require method counters to store some method state (max compilation levels) required by the compilation policy.
 994     if (method->get_method_counters(THREAD) == nullptr) {
 995       record_failure("can't create method counters");
 996       // All buffers in the CodeBuffer are allocated in the CodeCache.
 997       // If the code buffer is created on each compile attempt
 998       // as in C2, then it must be freed.
 999       code_buffer->free_blob();
1000       return;
1001     }
1002 
1003     // Check if memory should be freed before allocation
1004     CodeCache::gc_on_allocation();
1005 
1006     // To prevent compile queue updates.
1007     MutexLocker locker(THREAD, MethodCompileQueue_lock);
1008 
1009     // Prevent InstanceKlass::add_to_hierarchy from running
1010     // and invalidating our dependencies until we install this method.
1011     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1012     MutexLocker ml(Compile_lock);
1013     NoSafepointVerifier nsv;
1014 
1015     // Change in Jvmti state may invalidate compilation.
1016     if (!failing() && jvmti_state_changed()) {
1017       record_failure("Jvmti state change invalidated dependencies");
1018     }
1019 
1020     // Change in DTrace flags may invalidate compilation.
1021     if (!failing() &&
1022         ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1023           (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1024       record_failure("DTrace flags change invalidated dependencies");
1025     }
1026 
1027     if (!failing() && target->needs_clinit_barrier() &&
1028         target->holder()->is_in_error_state()) {
1029       record_failure("method holder is in error state");
1030     }
1031 
1032     if (!failing()) {
1033       if (log() != nullptr) {
1034         // Log the dependencies which this compilation declares.
1035         dependencies()->log_all_dependencies();
1036       }
1037 
1038       // Encode the dependencies now, so we can check them right away.
1039       dependencies()->encode_content_bytes();
1040 
1041       // Check for {class loads, evolution, breakpoints, ...} during compilation
1042       validate_compile_task_dependencies(target);
1043     }
1044 
1045     if (failing()) {
1046       // While not a true deoptimization, it is a preemptive decompile.
1047       MethodData* mdo = method()->method_data();
1048       if (mdo != nullptr && _inc_decompile_count_on_failure) {
1049         mdo->inc_decompile_count();
1050       }
1051 
1052       // All buffers in the CodeBuffer are allocated in the CodeCache.
1053       // If the code buffer is created on each compile attempt
1054       // as in C2, then it must be freed.
1055       code_buffer->free_blob();
1056       return;
1057     }
1058 
1059     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1060     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1061 
1062     nm =  nmethod::new_nmethod(method,
1063                                compile_id(),
1064                                entry_bci,
1065                                offsets,
1066                                orig_pc_offset,
1067                                debug_info(), dependencies(), code_buffer,
1068                                frame_words, oop_map_set,
1069                                handler_table, inc_table,
1070                                compiler, CompLevel(task()->comp_level()));
1071 


1072     // Free codeBlobs
1073     code_buffer->free_blob();
1074 
1075     if (nm != nullptr) {
1076       nm->set_has_unsafe_access(has_unsafe_access);
1077       nm->set_has_wide_vectors(has_wide_vectors);
1078       nm->set_has_monitors(has_monitors);
1079       nm->set_has_scoped_access(has_scoped_access);


1080       assert(!method->is_synchronized() || nm->has_monitors(), "");
1081 
1082       if (entry_bci == InvocationEntryBci) {
1083         if (TieredCompilation) {
1084           // If there is an old version we're done with it
1085           nmethod* old = method->code();
1086           if (TraceMethodReplacement && old != nullptr) {
1087             ResourceMark rm;
1088             char *method_name = method->name_and_sig_as_C_string();
1089             tty->print_cr("Replacing method %s", method_name);
1090           }
1091           if (old != nullptr) {
1092             old->make_not_used();
1093           }
1094         }
1095 
1096         LogTarget(Info, nmethod, install) lt;
1097         if (lt.is_enabled()) {
1098           ResourceMark rm;
1099           char *method_name = method->name_and_sig_as_C_string();
1100           lt.print("Installing method (%d) %s ",
1101                     task()->comp_level(), method_name);
1102         }
1103         // Allow the code to be executed
1104         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1105         if (nm->make_in_use()) {
1106           method->set_code(method, nm);
1107         }
1108       } else {
1109         LogTarget(Info, nmethod, install) lt;
1110         if (lt.is_enabled()) {
1111           ResourceMark rm;
1112           char *method_name = method->name_and_sig_as_C_string();
1113           lt.print("Installing osr method (%d) %s @ %d",
1114                     task()->comp_level(), method_name, entry_bci);
1115         }
1116         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1117         if (nm->make_in_use()) {
1118           method->method_holder()->add_osr_nmethod(nm);
1119         }
1120       }

1121     }
1122   }
1123 
1124   NoSafepointVerifier nsv;
1125   if (nm != nullptr) {
1126     // Compilation succeeded, post what we know about it
1127     nm->post_compiled_method(task());
1128     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1129   } else {
1130     // The CodeCache is full.
1131     record_failure("code cache is full");


1132   }
1133 
1134   // safepoints are allowed again
1135 }
1136 
1137 // ------------------------------------------------------------------
1138 // ciEnv::find_system_klass
1139 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1140   VM_ENTRY_MARK;
1141   return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1142 }
1143 
1144 // ------------------------------------------------------------------
1145 // ciEnv::comp_level
1146 int ciEnv::comp_level() {
1147   if (task() == nullptr)  return CompilationPolicy::highest_compile_level();
1148   return task()->comp_level();
1149 }
1150 
1151 // ------------------------------------------------------------------

1681         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1682         GUARDED_VM_ENTRY(
1683           MutexLocker ml(Compile_lock);
1684           dump_replay_data_version(&replay_data_stream);
1685           dump_compile_data(&replay_data_stream);
1686         )
1687         replay_data_stream.flush();
1688         tty->print("# Compiler inline data is saved as: ");
1689         tty->print_cr("%s", buffer);
1690       } else {
1691         tty->print_cr("# Can't open file to dump inline data.");
1692         close(fd);
1693       }
1694     }
1695   }
1696 }
1697 
1698 void ciEnv::dump_replay_data_version(outputStream* out) {
1699   out->print_cr("version %d", REPLAY_VERSION);
1700 }






















































   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/aotCacheAccess.hpp"
  26 #include "cds/aotConstantPoolResolver.hpp"
  27 #include "cds/cdsConfig.hpp"
  28 #include "ci/ciConstant.hpp"
  29 #include "ci/ciEnv.hpp"
  30 #include "ci/ciField.hpp"
  31 #include "ci/ciInstance.hpp"
  32 #include "ci/ciInstanceKlass.hpp"
  33 #include "ci/ciMethod.hpp"
  34 #include "ci/ciNullObject.hpp"
  35 #include "ci/ciReplay.hpp"
  36 #include "ci/ciSymbols.hpp"
  37 #include "ci/ciUtilities.inline.hpp"
  38 #include "classfile/javaClasses.hpp"
  39 #include "classfile/javaClasses.inline.hpp"
  40 #include "classfile/systemDictionary.hpp"
  41 #include "classfile/vmClasses.hpp"
  42 #include "classfile/vmSymbols.hpp"
  43 #include "code/aotCodeCache.hpp"
  44 #include "code/codeCache.hpp"
  45 #include "code/scopeDesc.hpp"
  46 #include "compiler/compilationLog.hpp"
  47 #include "compiler/compilationPolicy.hpp"
  48 #include "compiler/compileBroker.hpp"
  49 #include "compiler/compileLog.hpp"
  50 #include "compiler/compilerEvent.hpp"
  51 #include "compiler/compileTask.hpp"
  52 #include "compiler/disassembler.hpp"
  53 #include "gc/shared/barrierSetNMethod.hpp"
  54 #include "gc/shared/collectedHeap.inline.hpp"
  55 #include "interpreter/bytecodeStream.hpp"
  56 #include "interpreter/linkResolver.hpp"
  57 #include "jfr/jfrEvents.hpp"
  58 #include "jvm.h"
  59 #include "logging/log.hpp"
  60 #include "memory/allocation.inline.hpp"
  61 #include "memory/oopFactory.hpp"
  62 #include "memory/resourceArea.hpp"
  63 #include "memory/universe.hpp"
  64 #include "oops/constantPool.inline.hpp"
  65 #include "oops/cpCache.inline.hpp"
  66 #include "oops/method.inline.hpp"
  67 #include "oops/methodData.hpp"
  68 #include "oops/objArrayKlass.hpp"
  69 #include "oops/objArrayOop.inline.hpp"
  70 #include "oops/oop.inline.hpp"
  71 #include "oops/resolvedIndyEntry.hpp"
  72 #include "oops/symbolHandle.hpp"
  73 #include "oops/trainingData.hpp"
  74 #include "prims/jvmtiExport.hpp"
  75 #include "prims/methodHandles.hpp"
  76 #include "runtime/fieldDescriptor.inline.hpp"
  77 #include "runtime/flags/flagSetting.hpp"
  78 #include "runtime/handles.inline.hpp"
  79 #include "runtime/init.hpp"
  80 #include "runtime/javaThread.hpp"
  81 #include "runtime/jniHandles.inline.hpp"
  82 #include "runtime/reflection.hpp"
  83 #include "runtime/safepointVerifiers.hpp"
  84 #include "runtime/sharedRuntime.hpp"
  85 #include "utilities/dtrace.hpp"
  86 #include "utilities/macros.hpp"
  87 #ifdef COMPILER1
  88 #include "c1/c1_Runtime1.hpp"
  89 #endif
  90 #ifdef COMPILER2
  91 #include "opto/runtime.hpp"
  92 #endif
  93 
  94 // ciEnv
  95 //
  96 // This class is the top level broker for requests from the compiler
  97 // to the VM.

 657       if (is_java_primitive(bt)) {
 658         assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
 659         return unbox_primitive_value(ciobj, bt);
 660       } else {
 661         assert(ciobj->is_instance(), "should be an instance");
 662         return ciConstant(T_OBJECT, ciobj);
 663       }
 664     }
 665   }
 666 }
 667 
 668 // ------------------------------------------------------------------
 669 // ciEnv::get_constant_by_index_impl
 670 //
 671 // Implementation of get_constant_by_index().
 672 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 673                                              int index, int obj_index,
 674                                              ciInstanceKlass* accessor) {
 675   if (obj_index >= 0) {
 676     ciConstant con = get_resolved_constant(cpool, obj_index);
 677     if (con.should_be_constant()) {
 678       return con;
 679     }
 680   }
 681   constantTag tag = cpool->tag_at(index);
 682   if (tag.is_int()) {
 683     return ciConstant(T_INT, (jint)cpool->int_at(index));
 684   } else if (tag.is_long()) {
 685     return ciConstant((jlong)cpool->long_at(index));
 686   } else if (tag.is_float()) {
 687     return ciConstant((jfloat)cpool->float_at(index));
 688   } else if (tag.is_double()) {
 689     return ciConstant((jdouble)cpool->double_at(index));
 690   } else if (tag.is_string()) {
 691     EXCEPTION_CONTEXT;
 692     assert(obj_index >= 0, "should have an object index");
 693     oop string = cpool->string_at(index, obj_index, THREAD);
 694     if (HAS_PENDING_EXCEPTION) {
 695       CLEAR_PENDING_EXCEPTION;
 696       record_out_of_memory_failure();
 697       return ciConstant();

 811   }
 812 }
 813 
 814 
 815 // ------------------------------------------------------------------
 816 // ciEnv::get_method_by_index_impl
 817 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool,
 818                                           int index, Bytecodes::Code bc,
 819                                           ciInstanceKlass* accessor) {
 820   assert(cpool.not_null(), "need constant pool");
 821   assert(accessor != nullptr, "need origin of access");
 822   if (bc == Bytecodes::_invokedynamic) {
 823     // FIXME: code generation could allow for null (unlinked) call site
 824     // The call site could be made patchable as follows:
 825     // Load the appendix argument from the constant pool.
 826     // Test the appendix argument and jump to a known deopt routine if it is null.
 827     // Jump through a patchable call site, which is initially a deopt routine.
 828     // Patch the call site to the nmethod entry point of the static compiled lambda form.
 829     // As with other two-component call sites, both values must be independently verified.
 830     assert(index < cpool->cache()->resolved_indy_entries_length(), "impossible");
 831     ResolvedIndyEntry* indy_info = cpool->resolved_indy_entry_at(index);
 832     Method* adapter = indy_info->method();
 833 #if INCLUDE_CDS
 834     if (is_precompile() && !AOTConstantPoolResolver::is_resolution_deterministic(cpool(), indy_info->constant_pool_index())) {
 835       // This is an indy callsite that was resolved as a side effect of VM bootstrap, but
 836       // it cannot be cached in the resolved state, so AOT code should not reference it.
 837       adapter = nullptr;
 838     }
 839 #endif
 840     // Resolved if the adapter is non null.
 841     if (adapter != nullptr) {
 842       return get_method(adapter);
 843     }
 844 
 845     // Fake a method that is equivalent to a declared method.
 846     ciInstanceKlass* holder    = get_instance_klass(vmClasses::MethodHandle_klass());
 847     ciSymbol*        name      = ciSymbols::invokeBasic_name();
 848     ciSymbol*        signature = get_symbol(cpool->signature_ref_at(index, bc));
 849     return get_unloaded_method(holder, name, signature, accessor);
 850   } else {
 851     const int holder_index = cpool->klass_ref_index_at(index, bc);
 852     bool holder_is_accessible;
 853     ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
 854 
 855     // Get the method's name and signature.
 856     Symbol* name_sym = cpool->name_ref_at(index, bc);
 857     Symbol* sig_sym  = cpool->signature_ref_at(index, bc);
 858 
 859     if (cpool->has_preresolution()

 866       case Bytecodes::_invokevirtual:
 867       case Bytecodes::_invokeinterface:
 868       case Bytecodes::_invokespecial:
 869       case Bytecodes::_invokestatic:
 870         {
 871           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 872           if (m != nullptr) {
 873             return get_method(m);
 874           }
 875         }
 876         break;
 877       default:
 878         break;
 879       }
 880     }
 881 
 882     if (holder_is_accessible) {  // Our declared holder is loaded.
 883       constantTag tag = cpool->tag_ref_at(index, bc);
 884       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 885       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 886       if (m != nullptr) {
 887         ciInstanceKlass* cik = get_instance_klass(m->method_holder());
 888         if ((bc == Bytecodes::_invokestatic && cik->is_not_initialized()) || !cik->is_loaded()) {
 889           m = nullptr;
 890         }
 891       }
 892       if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
 893         m = nullptr;
 894       }
 895       if (m != nullptr) {
 896         // We found the method.
 897         return get_method(m);
 898       }
 899     }
 900 
 901     // Either the declared holder was not loaded, or the method could
 902     // not be found.  Create a dummy ciMethod to represent the failed
 903     // lookup.
 904     ciSymbol* name      = get_symbol(name_sym);
 905     ciSymbol* signature = get_symbol(sig_sym);
 906     return get_unloaded_method(holder, name, signature, accessor);
 907   }
 908 }
 909 
 910 

 951       _name_buffer =
 952         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 953       _name_buffer_len = req_len;
 954     }
 955   }
 956   return _name_buffer;
 957 }
 958 
 959 // ------------------------------------------------------------------
 960 // ciEnv::is_in_vm
 961 bool ciEnv::is_in_vm() {
 962   return JavaThread::current()->thread_state() == _thread_in_vm;
 963 }
 964 
 965 // ------------------------------------------------------------------
 966 // ciEnv::validate_compile_task_dependencies
 967 //
 968 // Check for changes during compilation (e.g. class loads, evolution,
 969 // breakpoints, call site invalidation).
 970 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 971   assert(!failing(), "should not call this when failing");
 972 
 973   Dependencies::DepType result = dependencies()->validate_dependencies(_task);
 974   if (result != Dependencies::end_marker) {
 975     if (result == Dependencies::call_site_target_value) {
 976       _inc_decompile_count_on_failure = false;
 977       record_failure("call site target change");
 978     } else if (Dependencies::is_klass_type(result)) {
 979       record_failure("concurrent class loading");
 980     } else {
 981       record_failure("invalid non-klass dependency");
 982     }
 983   }
 984 }
 985 
 986 // aot_code_entry != nullptr implies loading compiled code from AOT code cache
 987 bool ciEnv::is_compilation_valid(JavaThread* thread, ciMethod* target, bool preload, bool install_code, CodeBuffer* code_buffer, AOTCodeEntry* aot_code_entry) {
 988   methodHandle method(thread, target->get_Method());
 989 
 990   // We require method counters to store some method state (max compilation levels) required by the compilation policy.
 991   if (!preload && method->get_method_counters(thread) == nullptr) {
 992     record_failure("can't create method counters");
 993     if (code_buffer != nullptr) {
 994       code_buffer->free_blob();
 995     }
 996     return false;
 997   }
 998 
 999   if (aot_code_entry != nullptr) {
1000     // Invalid compilation states:
1001     //  - AOTCodeCache is closed, AOTCode entry is garbage.
1002     //  - AOTCode entry indicates this shared code was marked invalid while it was loaded.
1003     if (!AOTCodeCache::is_on() || aot_code_entry->not_entrant()) {
1004       return false;
1005     }
1006   }
1007 
1008   // Change in Jvmti state may invalidate compilation.
1009   if (!failing() && jvmti_state_changed()) {
1010     record_failure("Jvmti state change invalidated dependencies");
1011   }
1012 
1013   // Change in DTrace flags may invalidate compilation.
1014   if (!failing() &&
1015       ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1016         (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1017     record_failure("DTrace flags change invalidated dependencies");
1018   }
1019 
1020   if (!preload && !failing() && target->needs_clinit_barrier() &&
1021       target->holder()->is_in_error_state()) {
1022     record_failure("method holder is in error state");
1023   }
1024 
1025   if (!failing() && (aot_code_entry == nullptr)) {
1026     if (log() != nullptr) {
1027       // Log the dependencies which this compilation declares.
1028       dependencies()->log_all_dependencies();
1029     }
1030 
1031     // Encode the dependencies now, so we can check them right away.
1032     dependencies()->encode_content_bytes();
1033   }
1034   // Check for {class loads, evolution, breakpoints, ...} during compilation
1035   if (!failing() && install_code) {
1036     // Check for {class loads, evolution, breakpoints, ...} during compilation
1037     validate_compile_task_dependencies(target);
1038     if (failing() && preload) {
1039       ResourceMark rm;
1040       char *method_name = method->name_and_sig_as_C_string();
1041       log_info(aot, codecache)("preload code for '%s' failed dependency check", method_name);
1042     }
1043   }
1044 
1045   if (failing()) {
1046     // While not a true deoptimization, it is a preemptive decompile.
1047     MethodData* mdo = method()->method_data();
1048     if (mdo != nullptr && _inc_decompile_count_on_failure) {
1049       mdo->inc_decompile_count();
1050     }
1051 
1052     if (code_buffer != nullptr) {
1053       code_buffer->free_blob();
1054     }
1055     return false;
1056   }
1057   return true;
1058 }
1059 
1060 void ciEnv::make_code_usable(JavaThread* thread, ciMethod* target, bool preload, int entry_bci, AOTCodeEntry* aot_code_entry, nmethod* nm) {
1061   methodHandle method(thread, target->get_Method());
1062 
1063   if (entry_bci == InvocationEntryBci) {
1064     if (TieredCompilation) {
1065       // If there is an old version we're done with it
1066       nmethod* old = method->code();
1067       if (TraceMethodReplacement && old != nullptr) {
1068         ResourceMark rm;
1069         char *method_name = method->name_and_sig_as_C_string();
1070         tty->print_cr("Replacing method %s", method_name);
1071       }
1072       if (old != nullptr) {
1073         old->make_not_used();
1074       }
1075     }
1076 
1077     LogTarget(Info, nmethod, install) lt;
1078     if (lt.is_enabled()) {
1079       ResourceMark rm;
1080       char *method_name = method->name_and_sig_as_C_string();
1081       lt.print("Installing method (L%d) %s id=%d aot=%s%s%u",
1082                task()->comp_level(), method_name, compile_id(),
1083                task()->is_aot_load() ? "A" : "", preload ? "P" : "",
1084                (aot_code_entry != nullptr ? aot_code_entry->offset() : 0));
1085     }
1086     // Allow the code to be executed
1087     MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1088     if (nm->make_in_use()) {
1089 #ifdef ASSERT
1090       BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1091       if (bs_nm != nullptr && bs_nm->supports_entry_barrier(nm)) {
1092         if (!bs_nm->is_armed(nm)) {
1093           log_info(init)("nmethod %d %d not armed", nm->compile_id(), nm->comp_level());
1094         }
1095       }
1096 #endif // ASSERT
1097       if (preload) {
1098         nm->set_preloaded(true);
1099         method->set_preload_code(nm);
1100       }
1101       if (!preload || target->holder()->is_linked()) {
1102         method->set_code(method, nm);
1103       }
1104     }
1105   } else {
1106     LogTarget(Info, nmethod, install) lt;
1107     if (lt.is_enabled()) {
1108       assert(aot_code_entry == nullptr && !task()->is_aot_load(), "OSR nmethods are not AOT compiled");
1109       ResourceMark rm;
1110       char *method_name = method->name_and_sig_as_C_string();
1111       lt.print("Installing osr method (L%d) %s @ %d id=%u",
1112                task()->comp_level(), method_name, entry_bci, compile_id());
1113     }
1114     MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1115     if (nm->make_in_use()) {
1116       method->method_holder()->add_osr_nmethod(nm);
1117     }
1118   }
1119 }
1120 
1121 nmethod* ciEnv::register_aot_method(JavaThread* thread,
1122                                 ciMethod* target,
1123                                 AbstractCompiler* compiler,
1124                                 nmethod* archived_nm,
1125                                 address reloc_data,
1126                                 GrowableArray<Handle>& oop_list,
1127                                 GrowableArray<Metadata*>& metadata_list,
1128                                 ImmutableOopMapSet* oopmaps,
1129                                 address immutable_data,
1130                                 GrowableArray<Handle>& reloc_imm_oop_list,
1131                                 GrowableArray<Metadata*>& reloc_imm_metadata_list,
1132                                 AOTCodeReader* aot_code_reader)
1133 {
1134   AOTCodeEntry* aot_code_entry = task()->aot_code_entry();
1135   assert(aot_code_entry != nullptr, "must be");
1136   nmethod* nm = nullptr;
1137   {
1138     methodHandle method(thread, target->get_Method());
1139     bool preload = task()->preload(); // Code is preloaded before Java method execution
1140 
1141     // Check if memory should be freed before allocation
1142     CodeCache::gc_on_allocation();
1143 
1144     // To prevent compile queue updates.
1145     MutexLocker locker(thread, task()->compile_queue()->lock());
1146 
1147     // Prevent InstanceKlass::add_to_hierarchy from running
1148     // and invalidating our dependencies until we install this method.
1149     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1150     MutexLocker ml(Compile_lock);
1151     NoSafepointVerifier nsv;
1152 
1153     if (!is_compilation_valid(thread, target, preload, true /*install_code*/, nullptr /*code_buffer*/, aot_code_entry)) {
1154       return nullptr;
1155     }
1156 
1157     nm = nmethod::new_nmethod(archived_nm,
1158                               method,
1159                               compiler,
1160                               compile_id(),
1161                               reloc_data,
1162                               oop_list,
1163                               metadata_list,
1164                               oopmaps,
1165                               immutable_data,
1166                               reloc_imm_oop_list,
1167                               reloc_imm_metadata_list,
1168                               aot_code_reader);
1169 
1170     if (nm != nullptr) {
1171       aot_code_entry->set_loaded();
1172       make_code_usable(thread, target, preload, InvocationEntryBci, aot_code_entry, nm);
1173     }
1174   }
1175 
1176   NoSafepointVerifier nsv;
1177   if (nm != nullptr) {
1178     // Compilation succeeded, post what we know about it
1179     nm->post_compiled_method(task());
1180     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1181   } else {
1182     // The CodeCache is full.
1183     record_failure("code cache is full");
1184   }
1185   return nm;
1186   // safepoints are allowed again
1187 }
1188 
1189 // ------------------------------------------------------------------
1190 // ciEnv::register_method
1191 void ciEnv::register_method(ciMethod* target,
1192                             int entry_bci,
1193                             CodeOffsets* offsets,
1194                             int orig_pc_offset,
1195                             CodeBuffer* code_buffer,
1196                             int frame_words,
1197                             OopMapSet* oop_map_set,
1198                             ExceptionHandlerTable* handler_table,
1199                             ImplicitExceptionTable* inc_table,
1200                             AbstractCompiler* compiler,
1201                             bool has_clinit_barriers,
1202                             bool for_preload,
1203                             bool has_unsafe_access,
1204                             bool has_wide_vectors,
1205                             bool has_monitors,
1206                             bool has_scoped_access,
1207                             int immediate_oops_patched,
1208                             bool install_code,
1209                             AOTCodeEntry* aot_code_entry) {
1210   VM_ENTRY_MARK;
1211   nmethod* nm = nullptr;
1212   {
1213     methodHandle method(THREAD, target->get_Method());
1214     bool preload = task()->preload(); // Code is preloaded before Java method execution









1215 
1216     // Check if memory should be freed before allocation
1217     CodeCache::gc_on_allocation();
1218 
1219     // To prevent compile queue updates.
1220     MutexLocker locker(THREAD, task()->compile_queue()->lock());
1221 
1222     // Prevent InstanceKlass::add_to_hierarchy from running
1223     // and invalidating our dependencies until we install this method.
1224     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1225     MutexLocker ml(Compile_lock);
1226     NoSafepointVerifier nsv;
1227 
1228     if (!is_compilation_valid(THREAD, target, preload, install_code, code_buffer, aot_code_entry)) {








































1229       return;
1230     }
1231 
1232     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1233     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1234 
1235     if (install_code) {
1236       nm =  nmethod::new_nmethod(method,
1237                                  compile_id(),
1238                                  entry_bci,
1239                                  offsets,
1240                                  orig_pc_offset,
1241                                  debug_info(), dependencies(), code_buffer,
1242                                  frame_words, oop_map_set,
1243                                  handler_table, inc_table,
1244                                  compiler, CompLevel(task()->comp_level()),
1245                                  aot_code_entry);
1246     }
1247     // Free codeBlobs
1248     code_buffer->free_blob();
1249 
1250     if (nm != nullptr) {
1251       nm->set_has_unsafe_access(has_unsafe_access);
1252       nm->set_has_wide_vectors(has_wide_vectors);
1253       nm->set_has_monitors(has_monitors);
1254       nm->set_has_scoped_access(has_scoped_access);
1255       nm->set_preloaded(preload);
1256       nm->set_has_clinit_barriers(has_clinit_barriers);
1257       assert(!method->is_synchronized() || nm->has_monitors(), "");
1258 
1259       if (task()->is_precompile()) {
1260         aot_code_entry = AOTCodeCache::store_nmethod(nm, compiler, for_preload);
1261         if (aot_code_entry != nullptr) {
1262           aot_code_entry->set_inlined_bytecodes(num_inlined_bytecodes());

































1263         }
1264       }
1265       make_code_usable(THREAD, target, preload, entry_bci, aot_code_entry, nm);
1266     }
1267   }
1268 
1269   NoSafepointVerifier nsv;
1270   if (nm != nullptr) {
1271     // Compilation succeeded, post what we know about it
1272     nm->post_compiled_method(task());
1273     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1274   } else if (install_code) {
1275     // The CodeCache is full.
1276     record_failure("code cache is full");
1277   } else {
1278     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1279   }
1280 
1281   // safepoints are allowed again
1282 }
1283 
1284 // ------------------------------------------------------------------
1285 // ciEnv::find_system_klass
1286 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1287   VM_ENTRY_MARK;
1288   return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1289 }
1290 
1291 // ------------------------------------------------------------------
1292 // ciEnv::comp_level
1293 int ciEnv::comp_level() {
1294   if (task() == nullptr)  return CompilationPolicy::highest_compile_level();
1295   return task()->comp_level();
1296 }
1297 
1298 // ------------------------------------------------------------------

1828         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1829         GUARDED_VM_ENTRY(
1830           MutexLocker ml(Compile_lock);
1831           dump_replay_data_version(&replay_data_stream);
1832           dump_compile_data(&replay_data_stream);
1833         )
1834         replay_data_stream.flush();
1835         tty->print("# Compiler inline data is saved as: ");
1836         tty->print_cr("%s", buffer);
1837       } else {
1838         tty->print_cr("# Can't open file to dump inline data.");
1839         close(fd);
1840       }
1841     }
1842   }
1843 }
1844 
1845 void ciEnv::dump_replay_data_version(outputStream* out) {
1846   out->print_cr("version %d", REPLAY_VERSION);
1847 }
1848 
1849 bool ciEnv::is_precompile() {
1850   return (task() != nullptr) && task()->is_precompile();
1851 }
1852 
1853 InstanceKlass::ClassState ciEnv::compute_init_state_for_precompiled(InstanceKlass* ik) {
1854   ASSERT_IN_VM;
1855   assert(is_precompile(), "should be called only in assembly phase");
1856   assert(AOTCacheAccess::can_generate_aot_code_for(ik), "klass should be archived for AOT compilation");
1857   assert(!ik->is_in_error_state(), "there should not be any probelm with this klass");
1858   ResourceMark rm;
1859 
1860   if (task()->method()->method_holder() == ik) {
1861     if (task()->method()->is_static_initializer()) { // Happens with -Xcomp
1862        return InstanceKlass::ClassState::being_initialized;
1863     } else {
1864        return InstanceKlass::ClassState::fully_initialized;
1865     }
1866   }
1867 
1868   switch (task()->compile_reason()) {
1869     case CompileTask::Reason_Precompile: {
1870       // check init dependencies
1871       MethodTrainingData* mtd = nullptr;
1872       GUARDED_VM_ENTRY(mtd = MethodTrainingData::find(methodHandle(Thread::current(), task()->method())); )
1873       if (mtd != nullptr) {
1874         CompileTrainingData* ctd = mtd->last_toplevel_compile(task()->comp_level());
1875         if (ctd != nullptr) {
1876           for (int i = 0; i < ctd->init_dep_count(); i++) {
1877             KlassTrainingData* ktd = ctd->init_dep(i);
1878             if (ktd->has_holder() && (ktd->holder() == ik)) {
1879               log_trace(precompile)("%d: init_dependency: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1880               return InstanceKlass::ClassState::fully_initialized;; // init dependency present
1881             }
1882           }
1883         }
1884       }
1885       // Class may be not present during TD creation for this method.
1886       // It could happen when profiled data for inlined method
1887       // was updated after this method was compiled during training.
1888       break;
1889     }
1890     case CompileTask::Reason_PrecompileForPreload: {
1891       // Preload AOT code does not depend on Training Data,
1892       // it has class init barriers to initialize class by
1893       // going into interpreter or directly calling runtime.
1894       return InstanceKlass::ClassState::fully_initialized;
1895     }
1896     default: fatal("%s", CompileTask::reason_name(task()->compile_reason()));
1897   }
1898   // Skip this class
1899   return InstanceKlass::ClassState::initialization_error;
1900 }
< prev index next >