< 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 "cds/heapShared.hpp"
  29 #include "ci/ciConstant.hpp"
  30 #include "ci/ciEnv.hpp"
  31 #include "ci/ciField.hpp"
  32 #include "ci/ciInstance.hpp"
  33 #include "ci/ciInstanceKlass.hpp"
  34 #include "ci/ciMethod.hpp"
  35 #include "ci/ciNullObject.hpp"
  36 #include "ci/ciReplay.hpp"
  37 #include "ci/ciSymbols.hpp"
  38 #include "ci/ciUtilities.inline.hpp"
  39 #include "classfile/javaClasses.hpp"
  40 #include "classfile/javaClasses.inline.hpp"
  41 #include "classfile/systemDictionary.hpp"
  42 #include "classfile/vmClasses.hpp"
  43 #include "classfile/vmSymbols.hpp"
  44 #include "code/aotCodeCache.hpp"
  45 #include "code/codeCache.hpp"
  46 #include "code/scopeDesc.hpp"
  47 #include "compiler/compilationLog.hpp"
  48 #include "compiler/compilationPolicy.hpp"
  49 #include "compiler/compileBroker.hpp"
  50 #include "compiler/compileLog.hpp"
  51 #include "compiler/compilerEvent.hpp"
  52 #include "compiler/compileTask.hpp"
  53 #include "compiler/disassembler.hpp"
  54 #include "gc/shared/barrierSetNMethod.hpp"
  55 #include "gc/shared/collectedHeap.inline.hpp"
  56 #include "interpreter/bytecodeStream.hpp"
  57 #include "interpreter/linkResolver.hpp"
  58 #include "jfr/jfrEvents.hpp"
  59 #include "jvm.h"
  60 #include "logging/log.hpp"
  61 #include "memory/allocation.inline.hpp"
  62 #include "memory/oopFactory.hpp"
  63 #include "memory/resourceArea.hpp"
  64 #include "memory/universe.hpp"
  65 #include "oops/constantPool.inline.hpp"
  66 #include "oops/cpCache.inline.hpp"
  67 #include "oops/method.inline.hpp"
  68 #include "oops/methodData.hpp"
  69 #include "oops/objArrayKlass.hpp"
  70 #include "oops/objArrayOop.inline.hpp"
  71 #include "oops/oop.inline.hpp"
  72 #include "oops/resolvedIndyEntry.hpp"
  73 #include "oops/symbolHandle.hpp"
  74 #include "oops/trainingData.hpp"
  75 #include "prims/jvmtiExport.hpp"
  76 #include "prims/methodHandles.hpp"
  77 #include "runtime/fieldDescriptor.inline.hpp"
  78 #include "runtime/flags/flagSetting.hpp"
  79 #include "runtime/handles.inline.hpp"
  80 #include "runtime/init.hpp"
  81 #include "runtime/javaThread.hpp"
  82 #include "runtime/jniHandles.inline.hpp"
  83 #include "runtime/reflection.hpp"
  84 #include "runtime/safepointVerifiers.hpp"
  85 #include "runtime/sharedRuntime.hpp"
  86 #include "utilities/dtrace.hpp"
  87 #include "utilities/macros.hpp"
  88 #ifdef COMPILER1
  89 #include "c1/c1_Runtime1.hpp"
  90 #endif
  91 #ifdef COMPILER2
  92 #include "opto/runtime.hpp"
  93 #endif
  94 
  95 // ciEnv
  96 //
  97 // This class is the top level broker for requests from the compiler
  98 // to the VM.

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

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

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

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










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







































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

































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

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