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










1203     // Check if memory should be freed before allocation
1204     CodeCache::gc_on_allocation();
1205 
1206     // To prevent compile queue updates.
1207     MutexLocker locker(THREAD, task()->compile_queue()->lock());
1208 
1209     // Prevent InstanceKlass::add_to_hierarchy from running
1210     // and invalidating our dependencies until we install this method.
1211     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1212     MutexLocker ml(Compile_lock);
1213     NoSafepointVerifier nsv;
1214 
1215     if (!is_compilation_valid(THREAD, target, install_code, /*aot_code_entry*/ false, /*preload*/ false)) {







































1216       code_buffer->free_blob();
1217       return;
1218     }
1219 
1220     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1221     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1222 
1223     if (install_code) {
1224       nm =  nmethod::new_nmethod(method,
1225                                  compile_id(),
1226                                  entry_bci,
1227                                  offsets,
1228                                  orig_pc_offset,
1229                                  debug_info(), dependencies(), code_buffer,
1230                                  frame_words, oop_map_set,
1231                                  handler_table, inc_table,
1232                                  compiler, CompLevel(task()->comp_level()));
1233     }
1234     // Free codeBlobs
1235     code_buffer->free_blob();
1236 
1237     if (nm != nullptr) {
1238       nm->set_has_unsafe_access(has_unsafe_access);
1239       nm->set_has_wide_vectors(has_wide_vectors);
1240       nm->set_has_monitors(has_monitors);
1241       nm->set_has_scoped_access(has_scoped_access);
1242       nm->set_preloaded(false);
1243       nm->set_has_clinit_barriers(has_clinit_barriers);
1244       assert(!method->is_synchronized() || nm->has_monitors(), "");
1245 
1246       if (task()->is_precompile()) {
1247         AOTCodeEntry* aot_code_entry = AOTCodeCache::store_nmethod(nm, compiler, for_preload);
1248         if (aot_code_entry != nullptr) {
1249           aot_code_entry->set_inlined_bytecodes(num_inlined_bytecodes());

































1250         }
1251       }
1252       make_code_usable(THREAD, target, /* preload */ false, entry_bci, /* aot_code_entry */ nullptr, nm);
1253     }
1254   }
1255 
1256   NoSafepointVerifier nsv;
1257   if (nm != nullptr) {
1258     // Compilation succeeded, post what we know about it
1259     nm->post_compiled_method(task());
1260     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1261   } else if (install_code) {
1262     // The CodeCache is full.
1263     record_failure("code cache is full");
1264   } else {
1265     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1266   }
1267 
1268   // safepoints are allowed again
1269 }
1270 
1271 // ------------------------------------------------------------------
1272 // ciEnv::find_system_klass
1273 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1274   VM_ENTRY_MARK;
1275   return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1276 }
1277 
1278 // ------------------------------------------------------------------
1279 // ciEnv::comp_level
1280 int ciEnv::comp_level() {
1281   if (task() == nullptr)  return CompilationPolicy::highest_compile_level();
1282   return task()->comp_level();
1283 }
1284 
1285 // ------------------------------------------------------------------

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