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 "precompiled.hpp"
26 #include "ci/ciConstant.hpp"
27 #include "ci/ciEnv.hpp"
28 #include "ci/ciField.hpp"
29 #include "ci/ciInstance.hpp"
30 #include "ci/ciInstanceKlass.hpp"
31 #include "ci/ciMethod.hpp"
32 #include "ci/ciNullObject.hpp"
33 #include "ci/ciReplay.hpp"
34 #include "ci/ciSymbols.hpp"
35 #include "ci/ciUtilities.inline.hpp"
36 #include "classfile/javaClasses.hpp"
37 #include "classfile/javaClasses.inline.hpp"
38 #include "classfile/systemDictionary.hpp"
39 #include "classfile/vmClasses.hpp"
40 #include "classfile/vmSymbols.hpp"
41 #include "code/codeCache.hpp"
42 #include "code/scopeDesc.hpp"
43 #include "compiler/compilationLog.hpp"
44 #include "compiler/compilationPolicy.hpp"
45 #include "compiler/compileBroker.hpp"
46 #include "compiler/compilerEvent.hpp"
47 #include "compiler/compileLog.hpp"
48 #include "compiler/compileTask.hpp"
49 #include "compiler/disassembler.hpp"
50 #include "gc/shared/collectedHeap.inline.hpp"
51 #include "interpreter/bytecodeStream.hpp"
52 #include "interpreter/linkResolver.hpp"
53 #include "jfr/jfrEvents.hpp"
54 #include "jvm.h"
55 #include "logging/log.hpp"
56 #include "memory/allocation.inline.hpp"
57 #include "memory/oopFactory.hpp"
58 #include "memory/resourceArea.hpp"
59 #include "memory/universe.hpp"
60 #include "oops/constantPool.inline.hpp"
61 #include "oops/cpCache.inline.hpp"
62 #include "oops/method.inline.hpp"
63 #include "oops/methodData.hpp"
64 #include "oops/objArrayKlass.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "oops/resolvedIndyEntry.hpp"
68 #include "oops/symbolHandle.hpp"
69 #include "prims/jvmtiExport.hpp"
70 #include "prims/methodHandles.hpp"
71 #include "runtime/fieldDescriptor.inline.hpp"
72 #include "runtime/handles.inline.hpp"
73 #include "runtime/init.hpp"
74 #include "runtime/javaThread.hpp"
75 #include "runtime/jniHandles.inline.hpp"
76 #include "runtime/reflection.hpp"
77 #include "runtime/safepointVerifiers.hpp"
78 #include "runtime/sharedRuntime.hpp"
79 #include "utilities/dtrace.hpp"
80 #include "utilities/macros.hpp"
81 #ifdef COMPILER1
82 #include "c1/c1_Runtime1.hpp"
83 #endif
84 #ifdef COMPILER2
85 #include "opto/runtime.hpp"
86 #endif
87
88 // ciEnv
89 //
90 // This class is the top level broker for requests from the compiler
91 // to the VM.
156 _ArithmeticException_instance = get_object(o)->as_instance();
157 o = Universe::array_index_out_of_bounds_exception_instance();
158 assert(o != nullptr, "should have been initialized");
159 _ArrayIndexOutOfBoundsException_instance = get_object(o)->as_instance();
160 o = Universe::array_store_exception_instance();
161 assert(o != nullptr, "should have been initialized");
162 _ArrayStoreException_instance = get_object(o)->as_instance();
163 o = Universe::class_cast_exception_instance();
164 assert(o != nullptr, "should have been initialized");
165 _ClassCastException_instance = get_object(o)->as_instance();
166
167 _the_null_string = nullptr;
168 _the_min_jint_string = nullptr;
169
170 _jvmti_redefinition_count = 0;
171 _jvmti_can_hotswap_or_post_breakpoint = false;
172 _jvmti_can_access_local_variables = false;
173 _jvmti_can_post_on_exceptions = false;
174 _jvmti_can_pop_frame = false;
175
176 _dyno_klasses = nullptr;
177 _dyno_locs = nullptr;
178 _dyno_name[0] = '\0';
179 }
180
181 // Record components of a location descriptor string. Components are appended by the constructor and
182 // removed by the destructor, like a stack, so scope matters. These location descriptors are used to
183 // locate dynamic classes, and terminate at a Method* or oop field associated with dynamic/hidden class.
184 //
185 // Example use:
186 //
187 // {
188 // RecordLocation fp(this, "field1");
189 // // location: "field1"
190 // { RecordLocation fp(this, " field2"); // location: "field1 field2" }
191 // // location: "field1"
192 // { RecordLocation fp(this, " field3"); // location: "field1 field3" }
193 // // location: "field1"
194 // }
195 // // location: ""
276 // During VM initialization, these instances have not yet been created.
277 // Assertions ensure that these instances are not accessed before
278 // their initialization.
279
280 assert(Universe::is_fully_initialized(), "must be");
281
282 _NullPointerException_instance = nullptr;
283 _ArithmeticException_instance = nullptr;
284 _ArrayIndexOutOfBoundsException_instance = nullptr;
285 _ArrayStoreException_instance = nullptr;
286 _ClassCastException_instance = nullptr;
287 _the_null_string = nullptr;
288 _the_min_jint_string = nullptr;
289
290 _jvmti_redefinition_count = 0;
291 _jvmti_can_hotswap_or_post_breakpoint = false;
292 _jvmti_can_access_local_variables = false;
293 _jvmti_can_post_on_exceptions = false;
294 _jvmti_can_pop_frame = false;
295
296 _dyno_klasses = nullptr;
297 _dyno_locs = nullptr;
298 }
299
300 ciEnv::~ciEnv() {
301 GUARDED_VM_ENTRY(
302 CompilerThread* current_thread = CompilerThread::current();
303 _factory->remove_symbols();
304 // Need safepoint to clear the env on the thread. RedefineClasses might
305 // be reading it.
306 current_thread->set_env(nullptr);
307 )
308 }
309
310 // ------------------------------------------------------------------
311 // Cache Jvmti state
312 bool ciEnv::cache_jvmti_state() {
313 VM_ENTRY_MARK;
314 // Get Jvmti capabilities under lock to get consistent values.
315 MutexLocker mu(JvmtiThreadState_lock);
653 if (is_java_primitive(bt)) {
654 assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
655 return unbox_primitive_value(ciobj, bt);
656 } else {
657 assert(ciobj->is_instance(), "should be an instance");
658 return ciConstant(T_OBJECT, ciobj);
659 }
660 }
661 }
662 }
663
664 // ------------------------------------------------------------------
665 // ciEnv::get_constant_by_index_impl
666 //
667 // Implementation of get_constant_by_index().
668 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
669 int index, int obj_index,
670 ciInstanceKlass* accessor) {
671 if (obj_index >= 0) {
672 ciConstant con = get_resolved_constant(cpool, obj_index);
673 if (con.is_valid()) {
674 return con;
675 }
676 }
677 constantTag tag = cpool->tag_at(index);
678 if (tag.is_int()) {
679 return ciConstant(T_INT, (jint)cpool->int_at(index));
680 } else if (tag.is_long()) {
681 return ciConstant((jlong)cpool->long_at(index));
682 } else if (tag.is_float()) {
683 return ciConstant((jfloat)cpool->float_at(index));
684 } else if (tag.is_double()) {
685 return ciConstant((jdouble)cpool->double_at(index));
686 } else if (tag.is_string()) {
687 EXCEPTION_CONTEXT;
688 assert(obj_index >= 0, "should have an object index");
689 oop string = cpool->string_at(index, obj_index, THREAD);
690 if (HAS_PENDING_EXCEPTION) {
691 CLEAR_PENDING_EXCEPTION;
692 record_out_of_memory_failure();
693 return ciConstant();
854 case Bytecodes::_invokevirtual:
855 case Bytecodes::_invokeinterface:
856 case Bytecodes::_invokespecial:
857 case Bytecodes::_invokestatic:
858 {
859 Method* m = ConstantPool::method_at_if_loaded(cpool, index);
860 if (m != nullptr) {
861 return get_method(m);
862 }
863 }
864 break;
865 default:
866 break;
867 }
868 }
869
870 if (holder_is_accessible) { // Our declared holder is loaded.
871 constantTag tag = cpool->tag_ref_at(index, bc);
872 assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
873 Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
874 if (m != nullptr &&
875 (bc == Bytecodes::_invokestatic
876 ? m->method_holder()->is_not_initialized()
877 : !m->method_holder()->is_loaded())) {
878 m = nullptr;
879 }
880 if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
881 m = nullptr;
882 }
883 if (m != nullptr) {
884 // We found the method.
885 return get_method(m);
886 }
887 }
888
889 // Either the declared holder was not loaded, or the method could
890 // not be found. Create a dummy ciMethod to represent the failed
891 // lookup.
892 ciSymbol* name = get_symbol(name_sym);
893 ciSymbol* signature = get_symbol(sig_sym);
894 return get_unloaded_method(holder, name, signature, accessor);
895 }
896 }
897
898
939 _name_buffer =
940 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
941 _name_buffer_len = req_len;
942 }
943 }
944 return _name_buffer;
945 }
946
947 // ------------------------------------------------------------------
948 // ciEnv::is_in_vm
949 bool ciEnv::is_in_vm() {
950 return JavaThread::current()->thread_state() == _thread_in_vm;
951 }
952
953 // ------------------------------------------------------------------
954 // ciEnv::validate_compile_task_dependencies
955 //
956 // Check for changes during compilation (e.g. class loads, evolution,
957 // breakpoints, call site invalidation).
958 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
959 if (failing()) return; // no need for further checks
960
961 Dependencies::DepType result = dependencies()->validate_dependencies(_task);
962 if (result != Dependencies::end_marker) {
963 if (result == Dependencies::call_site_target_value) {
964 _inc_decompile_count_on_failure = false;
965 record_failure("call site target change");
966 } else if (Dependencies::is_klass_type(result)) {
967 record_failure("concurrent class loading");
968 } else {
969 record_failure("invalid non-klass dependency");
970 }
971 }
972 }
973
974 // ------------------------------------------------------------------
975 // ciEnv::register_method
976 void ciEnv::register_method(ciMethod* target,
977 int entry_bci,
978 CodeOffsets* offsets,
979 int orig_pc_offset,
980 CodeBuffer* code_buffer,
981 int frame_words,
982 OopMapSet* oop_map_set,
983 ExceptionHandlerTable* handler_table,
984 ImplicitExceptionTable* inc_table,
985 AbstractCompiler* compiler,
986 bool has_unsafe_access,
987 bool has_wide_vectors,
988 bool has_monitors,
989 bool has_scoped_access,
990 int immediate_oops_patched) {
991 VM_ENTRY_MARK;
992 nmethod* nm = nullptr;
993 {
994 methodHandle method(THREAD, target->get_Method());
995
996 // We require method counters to store some method state (max compilation levels) required by the compilation policy.
997 if (method->get_method_counters(THREAD) == nullptr) {
998 record_failure("can't create method counters");
999 // All buffers in the CodeBuffer are allocated in the CodeCache.
1000 // If the code buffer is created on each compile attempt
1001 // as in C2, then it must be freed.
1002 code_buffer->free_blob();
1003 return;
1004 }
1005
1006 // Check if memory should be freed before allocation
1007 CodeCache::gc_on_allocation();
1008
1009 // To prevent compile queue updates.
1010 MutexLocker locker(THREAD, MethodCompileQueue_lock);
1011
1012 // Prevent InstanceKlass::add_to_hierarchy from running
1013 // and invalidating our dependencies until we install this method.
1014 // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1015 MutexLocker ml(Compile_lock);
1016 NoSafepointVerifier nsv;
1017
1018 // Change in Jvmti state may invalidate compilation.
1019 if (!failing() && jvmti_state_changed()) {
1020 record_failure("Jvmti state change invalidated dependencies");
1021 }
1022
1023 // Change in DTrace flags may invalidate compilation.
1024 if (!failing() &&
1025 ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1026 (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1027 record_failure("DTrace flags change invalidated dependencies");
1028 }
1029
1030 if (!failing() && target->needs_clinit_barrier() &&
1031 target->holder()->is_in_error_state()) {
1032 record_failure("method holder is in error state");
1033 }
1034
1035 if (!failing()) {
1036 if (log() != nullptr) {
1037 // Log the dependencies which this compilation declares.
1038 dependencies()->log_all_dependencies();
1039 }
1040
1041 // Encode the dependencies now, so we can check them right away.
1042 dependencies()->encode_content_bytes();
1043
1044 // Check for {class loads, evolution, breakpoints, ...} during compilation
1045 validate_compile_task_dependencies(target);
1046 }
1047
1048 if (failing()) {
1049 // While not a true deoptimization, it is a preemptive decompile.
1050 MethodData* mdo = method()->method_data();
1051 if (mdo != nullptr && _inc_decompile_count_on_failure) {
1052 mdo->inc_decompile_count();
1053 }
1054
1055 // All buffers in the CodeBuffer are allocated in the CodeCache.
1056 // If the code buffer is created on each compile attempt
1057 // as in C2, then it must be freed.
1058 code_buffer->free_blob();
1059 return;
1060 }
1061
1062 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1063 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1064
1065 nm = nmethod::new_nmethod(method,
1066 compile_id(),
1067 entry_bci,
1068 offsets,
1069 orig_pc_offset,
1070 debug_info(), dependencies(), code_buffer,
1071 frame_words, oop_map_set,
1072 handler_table, inc_table,
1073 compiler, CompLevel(task()->comp_level()));
1074
1075 // Free codeBlobs
1076 code_buffer->free_blob();
1077
1078 if (nm != nullptr) {
1079 nm->set_has_unsafe_access(has_unsafe_access);
1080 nm->set_has_wide_vectors(has_wide_vectors);
1081 nm->set_has_monitors(has_monitors);
1082 nm->set_has_scoped_access(has_scoped_access);
1083 assert(!method->is_synchronized() || nm->has_monitors(), "");
1084
1085 if (entry_bci == InvocationEntryBci) {
1086 if (TieredCompilation) {
1087 // If there is an old version we're done with it
1088 nmethod* old = method->code();
1089 if (TraceMethodReplacement && old != nullptr) {
1090 ResourceMark rm;
1091 char *method_name = method->name_and_sig_as_C_string();
1092 tty->print_cr("Replacing method %s", method_name);
1093 }
1094 if (old != nullptr) {
1095 old->make_not_used();
1096 }
1097 }
1098
1099 LogTarget(Info, nmethod, install) lt;
1100 if (lt.is_enabled()) {
1101 ResourceMark rm;
1102 char *method_name = method->name_and_sig_as_C_string();
1103 lt.print("Installing method (%d) %s ",
1104 task()->comp_level(), method_name);
1105 }
1106 // Allow the code to be executed
1107 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1108 if (nm->make_in_use()) {
1109 method->set_code(method, nm);
1110 }
1111 } else {
1112 LogTarget(Info, nmethod, install) lt;
1113 if (lt.is_enabled()) {
1114 ResourceMark rm;
1115 char *method_name = method->name_and_sig_as_C_string();
1116 lt.print("Installing osr method (%d) %s @ %d",
1117 task()->comp_level(), method_name, entry_bci);
1118 }
1119 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1120 if (nm->make_in_use()) {
1121 method->method_holder()->add_osr_nmethod(nm);
1122 }
1123 }
1124 }
1125 }
1126
1127 NoSafepointVerifier nsv;
1128 if (nm != nullptr) {
1129 // Compilation succeeded, post what we know about it
1130 nm->post_compiled_method(task());
1131 task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1132 } else {
1133 // The CodeCache is full.
1134 record_failure("code cache is full");
1135 }
1136
1137 // safepoints are allowed again
1138 }
1139
1140 // ------------------------------------------------------------------
1141 // ciEnv::find_system_klass
1142 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1143 VM_ENTRY_MARK;
1144 return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1145 }
1146
1147 // ------------------------------------------------------------------
1148 // ciEnv::comp_level
1149 int ciEnv::comp_level() {
1150 if (task() == nullptr) return CompilationPolicy::highest_compile_level();
1151 return task()->comp_level();
1152 }
1153
1154 // ------------------------------------------------------------------
1155 // ciEnv::compile_id
1156 int ciEnv::compile_id() {
1157 if (task() == nullptr) return 0;
1158 return task()->compile_id();
1159 }
1160
1161 // ------------------------------------------------------------------
1162 // ciEnv::notice_inlined_method()
1163 void ciEnv::notice_inlined_method(ciMethod* method) {
1164 _num_inlined_bytecodes += method->code_size_for_inlining();
1165 }
1166
1167 // ------------------------------------------------------------------
1168 // ciEnv::num_inlined_bytecodes()
1169 int ciEnv::num_inlined_bytecodes() const {
1170 return _num_inlined_bytecodes;
1171 }
1172
1173 // ------------------------------------------------------------------
1174 // ciEnv::record_failure()
1175 void ciEnv::record_failure(const char* reason) {
1176 if (_failure_reason.get() == nullptr) {
1177 // Record the first failure reason.
1178 _failure_reason.set(reason);
1179 }
1180 }
1181
1182 void ciEnv::report_failure(const char* reason) {
1183 EventCompilationFailure event;
1184 if (event.should_commit()) {
1669 fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1670 GUARDED_VM_ENTRY(
1671 MutexLocker ml(Compile_lock);
1672 dump_replay_data_version(&replay_data_stream);
1673 dump_compile_data(&replay_data_stream);
1674 )
1675 replay_data_stream.flush();
1676 tty->print("# Compiler inline data is saved as: ");
1677 tty->print_cr("%s", buffer);
1678 } else {
1679 tty->print_cr("# Can't open file to dump inline data.");
1680 close(fd);
1681 }
1682 }
1683 }
1684 }
1685
1686 void ciEnv::dump_replay_data_version(outputStream* out) {
1687 out->print_cr("version %d", REPLAY_VERSION);
1688 }
|
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 "precompiled.hpp"
26 #include "cds/archiveBuilder.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/codeCache.hpp"
44 #include "code/scopeDesc.hpp"
45 #include "code/SCCache.hpp"
46 #include "compiler/compilationLog.hpp"
47 #include "compiler/compilationPolicy.hpp"
48 #include "compiler/compileBroker.hpp"
49 #include "compiler/compilerEvent.hpp"
50 #include "compiler/compileLog.hpp"
51 #include "compiler/compileTask.hpp"
52 #include "compiler/disassembler.hpp"
53 #include "gc/shared/collectedHeap.inline.hpp"
54 #include "gc/shared/barrierSetNMethod.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.
162 _ArithmeticException_instance = get_object(o)->as_instance();
163 o = Universe::array_index_out_of_bounds_exception_instance();
164 assert(o != nullptr, "should have been initialized");
165 _ArrayIndexOutOfBoundsException_instance = get_object(o)->as_instance();
166 o = Universe::array_store_exception_instance();
167 assert(o != nullptr, "should have been initialized");
168 _ArrayStoreException_instance = get_object(o)->as_instance();
169 o = Universe::class_cast_exception_instance();
170 assert(o != nullptr, "should have been initialized");
171 _ClassCastException_instance = get_object(o)->as_instance();
172
173 _the_null_string = nullptr;
174 _the_min_jint_string = nullptr;
175
176 _jvmti_redefinition_count = 0;
177 _jvmti_can_hotswap_or_post_breakpoint = false;
178 _jvmti_can_access_local_variables = false;
179 _jvmti_can_post_on_exceptions = false;
180 _jvmti_can_pop_frame = false;
181
182 _scc_clinit_barriers_entry = nullptr;
183
184 _dyno_klasses = nullptr;
185 _dyno_locs = nullptr;
186 _dyno_name[0] = '\0';
187 }
188
189 // Record components of a location descriptor string. Components are appended by the constructor and
190 // removed by the destructor, like a stack, so scope matters. These location descriptors are used to
191 // locate dynamic classes, and terminate at a Method* or oop field associated with dynamic/hidden class.
192 //
193 // Example use:
194 //
195 // {
196 // RecordLocation fp(this, "field1");
197 // // location: "field1"
198 // { RecordLocation fp(this, " field2"); // location: "field1 field2" }
199 // // location: "field1"
200 // { RecordLocation fp(this, " field3"); // location: "field1 field3" }
201 // // location: "field1"
202 // }
203 // // location: ""
284 // During VM initialization, these instances have not yet been created.
285 // Assertions ensure that these instances are not accessed before
286 // their initialization.
287
288 assert(Universe::is_fully_initialized(), "must be");
289
290 _NullPointerException_instance = nullptr;
291 _ArithmeticException_instance = nullptr;
292 _ArrayIndexOutOfBoundsException_instance = nullptr;
293 _ArrayStoreException_instance = nullptr;
294 _ClassCastException_instance = nullptr;
295 _the_null_string = nullptr;
296 _the_min_jint_string = nullptr;
297
298 _jvmti_redefinition_count = 0;
299 _jvmti_can_hotswap_or_post_breakpoint = false;
300 _jvmti_can_access_local_variables = false;
301 _jvmti_can_post_on_exceptions = false;
302 _jvmti_can_pop_frame = false;
303
304 _scc_clinit_barriers_entry = nullptr;
305
306 _dyno_klasses = nullptr;
307 _dyno_locs = nullptr;
308 }
309
310 ciEnv::~ciEnv() {
311 GUARDED_VM_ENTRY(
312 CompilerThread* current_thread = CompilerThread::current();
313 _factory->remove_symbols();
314 // Need safepoint to clear the env on the thread. RedefineClasses might
315 // be reading it.
316 current_thread->set_env(nullptr);
317 )
318 }
319
320 // ------------------------------------------------------------------
321 // Cache Jvmti state
322 bool ciEnv::cache_jvmti_state() {
323 VM_ENTRY_MARK;
324 // Get Jvmti capabilities under lock to get consistent values.
325 MutexLocker mu(JvmtiThreadState_lock);
663 if (is_java_primitive(bt)) {
664 assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
665 return unbox_primitive_value(ciobj, bt);
666 } else {
667 assert(ciobj->is_instance(), "should be an instance");
668 return ciConstant(T_OBJECT, ciobj);
669 }
670 }
671 }
672 }
673
674 // ------------------------------------------------------------------
675 // ciEnv::get_constant_by_index_impl
676 //
677 // Implementation of get_constant_by_index().
678 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
679 int index, int obj_index,
680 ciInstanceKlass* accessor) {
681 if (obj_index >= 0) {
682 ciConstant con = get_resolved_constant(cpool, obj_index);
683 if (con.should_be_constant()) {
684 return con;
685 }
686 }
687 constantTag tag = cpool->tag_at(index);
688 if (tag.is_int()) {
689 return ciConstant(T_INT, (jint)cpool->int_at(index));
690 } else if (tag.is_long()) {
691 return ciConstant((jlong)cpool->long_at(index));
692 } else if (tag.is_float()) {
693 return ciConstant((jfloat)cpool->float_at(index));
694 } else if (tag.is_double()) {
695 return ciConstant((jdouble)cpool->double_at(index));
696 } else if (tag.is_string()) {
697 EXCEPTION_CONTEXT;
698 assert(obj_index >= 0, "should have an object index");
699 oop string = cpool->string_at(index, obj_index, THREAD);
700 if (HAS_PENDING_EXCEPTION) {
701 CLEAR_PENDING_EXCEPTION;
702 record_out_of_memory_failure();
703 return ciConstant();
864 case Bytecodes::_invokevirtual:
865 case Bytecodes::_invokeinterface:
866 case Bytecodes::_invokespecial:
867 case Bytecodes::_invokestatic:
868 {
869 Method* m = ConstantPool::method_at_if_loaded(cpool, index);
870 if (m != nullptr) {
871 return get_method(m);
872 }
873 }
874 break;
875 default:
876 break;
877 }
878 }
879
880 if (holder_is_accessible) { // Our declared holder is loaded.
881 constantTag tag = cpool->tag_ref_at(index, bc);
882 assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
883 Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
884 if (m != nullptr) {
885 ciInstanceKlass* cik = get_instance_klass(m->method_holder());
886 if ((bc == Bytecodes::_invokestatic && cik->is_not_initialized()) || !cik->is_loaded()) {
887 m = nullptr;
888 }
889 }
890 if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
891 m = nullptr;
892 }
893 if (m != nullptr) {
894 // We found the method.
895 return get_method(m);
896 }
897 }
898
899 // Either the declared holder was not loaded, or the method could
900 // not be found. Create a dummy ciMethod to represent the failed
901 // lookup.
902 ciSymbol* name = get_symbol(name_sym);
903 ciSymbol* signature = get_symbol(sig_sym);
904 return get_unloaded_method(holder, name, signature, accessor);
905 }
906 }
907
908
949 _name_buffer =
950 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
951 _name_buffer_len = req_len;
952 }
953 }
954 return _name_buffer;
955 }
956
957 // ------------------------------------------------------------------
958 // ciEnv::is_in_vm
959 bool ciEnv::is_in_vm() {
960 return JavaThread::current()->thread_state() == _thread_in_vm;
961 }
962
963 // ------------------------------------------------------------------
964 // ciEnv::validate_compile_task_dependencies
965 //
966 // Check for changes during compilation (e.g. class loads, evolution,
967 // breakpoints, call site invalidation).
968 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
969 assert(!failing(), "should not call this when failing");
970
971 Dependencies::DepType result = dependencies()->validate_dependencies(_task);
972 if (result != Dependencies::end_marker) {
973 if (result == Dependencies::call_site_target_value) {
974 _inc_decompile_count_on_failure = false;
975 record_failure("call site target change");
976 } else if (Dependencies::is_klass_type(result)) {
977 record_failure("concurrent class loading");
978 } else {
979 record_failure("invalid non-klass dependency");
980 }
981 }
982 }
983
984 // ------------------------------------------------------------------
985 // ciEnv::register_method
986 void ciEnv::register_method(ciMethod* target,
987 int entry_bci,
988 CodeOffsets* offsets,
989 int orig_pc_offset,
990 CodeBuffer* code_buffer,
991 int frame_words,
992 OopMapSet* oop_map_set,
993 ExceptionHandlerTable* handler_table,
994 ImplicitExceptionTable* inc_table,
995 AbstractCompiler* compiler,
996 bool has_clinit_barriers,
997 bool for_preload,
998 bool has_unsafe_access,
999 bool has_wide_vectors,
1000 bool has_monitors,
1001 bool has_scoped_access,
1002 int immediate_oops_patched,
1003 bool install_code,
1004 SCCEntry* scc_entry) {
1005 VM_ENTRY_MARK;
1006 nmethod* nm = nullptr;
1007 {
1008 methodHandle method(THREAD, target->get_Method());
1009 bool preload = task()->preload(); // Code is preloaded before Java method execution
1010
1011 // We require method counters to store some method state (max compilation levels) required by the compilation policy.
1012 if (!preload && method->get_method_counters(THREAD) == nullptr) {
1013 record_failure("can't create method counters");
1014 // All buffers in the CodeBuffer are allocated in the CodeCache.
1015 // If the code buffer is created on each compile attempt
1016 // as in C2, then it must be freed.
1017 // But keep shared code.
1018 code_buffer->free_blob();
1019 return;
1020 }
1021
1022 // Check if memory should be freed before allocation
1023 CodeCache::gc_on_allocation();
1024
1025 // To prevent compile queue updates.
1026 MutexLocker locker(THREAD, MethodCompileQueue_lock);
1027
1028 // Prevent InstanceKlass::add_to_hierarchy from running
1029 // and invalidating our dependencies until we install this method.
1030 // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1031 MutexLocker ml(Compile_lock);
1032 NoSafepointVerifier nsv;
1033
1034 if (scc_entry != nullptr) {
1035 // Invalid compilation states:
1036 // - SCCache is closed, SCC entry is garbage.
1037 // - SCC entry indicates this shared code was marked invalid while it was loaded.
1038 if (!SCCache::is_on() || scc_entry->not_entrant()) {
1039 code_buffer->free_blob();
1040 return;
1041 }
1042 }
1043
1044 // Change in Jvmti state may invalidate compilation.
1045 if (!failing() && jvmti_state_changed()) {
1046 record_failure("Jvmti state change invalidated dependencies");
1047 }
1048
1049 // Change in DTrace flags may invalidate compilation.
1050 if (!failing() &&
1051 ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1052 (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1053 record_failure("DTrace flags change invalidated dependencies");
1054 }
1055
1056 if (!preload && !failing() && target->needs_clinit_barrier() &&
1057 target->holder()->is_in_error_state()) {
1058 record_failure("method holder is in error state");
1059 }
1060
1061 if (!failing() && (scc_entry == nullptr)) {
1062 if (log() != nullptr) {
1063 // Log the dependencies which this compilation declares.
1064 dependencies()->log_all_dependencies();
1065 }
1066
1067 // Encode the dependencies now, so we can check them right away.
1068 dependencies()->encode_content_bytes();
1069 }
1070 // Check for {class loads, evolution, breakpoints, ...} during compilation
1071 if (!failing() && install_code) {
1072 // Check for {class loads, evolution, breakpoints, ...} during compilation
1073 validate_compile_task_dependencies(target);
1074 if (failing() && preload) {
1075 ResourceMark rm;
1076 char *method_name = method->name_and_sig_as_C_string();
1077 log_info(scc)("preload code for '%s' failed dependency check", method_name);
1078 }
1079 }
1080
1081 if (failing()) {
1082 // While not a true deoptimization, it is a preemptive decompile.
1083 MethodData* mdo = method()->method_data();
1084 if (mdo != nullptr && _inc_decompile_count_on_failure) {
1085 mdo->inc_decompile_count();
1086 }
1087
1088 // All buffers in the CodeBuffer are allocated in the CodeCache.
1089 // If the code buffer is created on each compile attempt
1090 // as in C2, then it must be freed.
1091 code_buffer->free_blob();
1092 return;
1093 }
1094
1095 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1096 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1097
1098 if (scc_entry == nullptr) {
1099 scc_entry = SCCache::store_nmethod(method,
1100 compile_id(),
1101 entry_bci,
1102 offsets,
1103 orig_pc_offset,
1104 debug_info(), dependencies(), code_buffer,
1105 frame_words, oop_map_set,
1106 handler_table, inc_table,
1107 compiler,
1108 CompLevel(task()->comp_level()),
1109 has_clinit_barriers,
1110 for_preload,
1111 has_unsafe_access,
1112 has_wide_vectors,
1113 has_monitors,
1114 has_scoped_access);
1115 if (scc_entry != nullptr) {
1116 scc_entry->set_inlined_bytecodes(num_inlined_bytecodes());
1117 if (has_clinit_barriers) {
1118 set_scc_clinit_barriers_entry(scc_entry); // Record it
1119 // Build second version of code without class initialization barriers
1120 code_buffer->free_blob();
1121 return;
1122 } else if (!for_preload) {
1123 SCCEntry* previous_entry = scc_clinit_barriers_entry();
1124 scc_entry->set_next(previous_entry); // Link it for case of deoptimization
1125 }
1126 }
1127 }
1128 if (install_code) {
1129 nm = nmethod::new_nmethod(method,
1130 compile_id(),
1131 entry_bci,
1132 offsets,
1133 orig_pc_offset,
1134 debug_info(), dependencies(), code_buffer,
1135 frame_words, oop_map_set,
1136 handler_table, inc_table,
1137 compiler, CompLevel(task()->comp_level()),
1138 scc_entry);
1139 }
1140 // Free codeBlobs
1141 code_buffer->free_blob();
1142
1143 if (nm != nullptr) {
1144 nm->set_has_unsafe_access(has_unsafe_access);
1145 nm->set_has_wide_vectors(has_wide_vectors);
1146 nm->set_has_monitors(has_monitors);
1147 nm->set_has_scoped_access(has_scoped_access);
1148 nm->set_preloaded(preload);
1149 nm->set_has_clinit_barriers(has_clinit_barriers);
1150 assert(!method->is_synchronized() || nm->has_monitors(), "");
1151
1152 if (entry_bci == InvocationEntryBci) {
1153 if (TieredCompilation) {
1154 // If there is an old version we're done with it
1155 nmethod* old = method->code();
1156 if (TraceMethodReplacement && old != nullptr) {
1157 ResourceMark rm;
1158 char *method_name = method->name_and_sig_as_C_string();
1159 tty->print_cr("Replacing method %s", method_name);
1160 }
1161 if (old != nullptr) {
1162 old->make_not_used();
1163 }
1164 }
1165
1166 LogTarget(Info, nmethod, install) lt;
1167 if (lt.is_enabled()) {
1168 ResourceMark rm;
1169 char *method_name = method->name_and_sig_as_C_string();
1170 lt.print("Installing method (L%d) %s id=%d scc=%s%s%u",
1171 task()->comp_level(), method_name, compile_id(),
1172 task()->is_scc() ? "A" : "", preload ? "P" : "",
1173 (scc_entry != nullptr ? scc_entry->offset() : 0));
1174 }
1175 // Allow the code to be executed
1176 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1177 if (nm->make_in_use()) {
1178 #ifdef ASSERT
1179 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1180 if (bs_nm != nullptr && bs_nm->supports_entry_barrier(nm)) {
1181 if (!bs_nm->is_armed(nm)) {
1182 log_info(init)("nmethod %d %d not armed", nm->compile_id(), nm->comp_level());
1183 }
1184 }
1185 #endif // ASSERT
1186 if (preload) {
1187 method->set_preload_code(nm);
1188 }
1189 if (!preload || target->holder()->is_linked()) {
1190 method->set_code(method, nm);
1191 }
1192 }
1193 } else {
1194 LogTarget(Info, nmethod, install) lt;
1195 if (lt.is_enabled()) {
1196 ResourceMark rm;
1197 char *method_name = method->name_and_sig_as_C_string();
1198 lt.print("Installing osr method (L%d) %s @ %d id=%u scc=%s%u",
1199 task()->comp_level(), method_name, entry_bci, compile_id(),
1200 task()->is_scc() ? "A" : "",
1201 (scc_entry != nullptr ? scc_entry->offset() : 0));
1202 }
1203 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1204 if (nm->make_in_use()) {
1205 method->method_holder()->add_osr_nmethod(nm);
1206 }
1207 }
1208 }
1209 }
1210
1211 NoSafepointVerifier nsv;
1212 if (nm != nullptr) {
1213 // Compilation succeeded, post what we know about it
1214 nm->post_compiled_method(task());
1215 task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1216 } else if (install_code) {
1217 // The CodeCache is full.
1218 record_failure("code cache is full");
1219 } else {
1220 task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1221 }
1222
1223 // safepoints are allowed again
1224 }
1225
1226 // ------------------------------------------------------------------
1227 // ciEnv::find_system_klass
1228 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1229 VM_ENTRY_MARK;
1230 return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1231 }
1232
1233 // ------------------------------------------------------------------
1234 // ciEnv::comp_level
1235 int ciEnv::comp_level() {
1236 if (task() == nullptr) return CompilationPolicy::highest_compile_level();
1237 return task()->comp_level();
1238 }
1239
1240 // ------------------------------------------------------------------
1241 // ciEnv::compile_id
1242 int ciEnv::compile_id() {
1243 if (task() == nullptr) return 0;
1244 return task()->compile_id();
1245 }
1246
1247 // ------------------------------------------------------------------
1248 // ciEnv::notice_inlined_method()
1249 void ciEnv::notice_inlined_method(ciMethod* method) {
1250 _num_inlined_bytecodes += method->code_size_for_inlining();
1251 CompileTrainingData* tdata = task()->training_data();
1252 if (tdata != nullptr) {
1253 GUARDED_VM_ENTRY({
1254 methodHandle mh(Thread::current(), method->get_Method());
1255 tdata->notice_inlined_method(task(), mh);
1256 });
1257 }
1258 }
1259
1260 // ------------------------------------------------------------------
1261 // ciEnv::num_inlined_bytecodes()
1262 int ciEnv::num_inlined_bytecodes() const {
1263 return _num_inlined_bytecodes;
1264 }
1265
1266 // ------------------------------------------------------------------
1267 // ciEnv::record_failure()
1268 void ciEnv::record_failure(const char* reason) {
1269 if (_failure_reason.get() == nullptr) {
1270 // Record the first failure reason.
1271 _failure_reason.set(reason);
1272 }
1273 }
1274
1275 void ciEnv::report_failure(const char* reason) {
1276 EventCompilationFailure event;
1277 if (event.should_commit()) {
1762 fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1763 GUARDED_VM_ENTRY(
1764 MutexLocker ml(Compile_lock);
1765 dump_replay_data_version(&replay_data_stream);
1766 dump_compile_data(&replay_data_stream);
1767 )
1768 replay_data_stream.flush();
1769 tty->print("# Compiler inline data is saved as: ");
1770 tty->print_cr("%s", buffer);
1771 } else {
1772 tty->print_cr("# Can't open file to dump inline data.");
1773 close(fd);
1774 }
1775 }
1776 }
1777 }
1778
1779 void ciEnv::dump_replay_data_version(outputStream* out) {
1780 out->print_cr("version %d", REPLAY_VERSION);
1781 }
1782
1783 bool ciEnv::is_precompiled() {
1784 return (task() != nullptr) && (task()->compile_reason() == CompileTask::Reason_Precompile ||
1785 task()->compile_reason() == CompileTask::Reason_PrecompileForPreload);
1786 }
1787
1788 bool ciEnv::is_fully_initialized(InstanceKlass* ik) {
1789 assert(is_precompiled(), "");
1790 if (task()->method()->method_holder() == ik) {
1791 return true; // FIXME: may be too strong; being_initialized, at least
1792 }
1793 switch (task()->compile_reason()) {
1794 case CompileTask::Reason_Precompile: {
1795 // check init dependencies
1796 MethodTrainingData* mtd = TrainingData::lookup_for(task()->method());
1797 if (mtd != nullptr) {
1798 CompileTrainingData* ctd = mtd->last_toplevel_compile(task()->comp_level());
1799 if (ctd != nullptr) {
1800 for (int i = 0; i < ctd->init_dep_count(); i++) {
1801 KlassTrainingData* ktd = ctd->init_dep(i);
1802 if (ktd->has_holder() && (ktd->holder() == ik)) {
1803 log_trace(precompile)("%d: init_dependency: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1804 return true; // init dependency present
1805 }
1806 }
1807 }
1808 }
1809 return false; // no init dependency
1810 }
1811 case CompileTask::Reason_PrecompileForPreload: {
1812 // FIXME: assumes that all shared classes are initialized
1813 if (ik->is_shared()) {
1814 return true; // class init barriers
1815 }
1816 if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active() &&
1817 ArchiveBuilder::current()->has_been_archived((address)ik)) {
1818 return true; // class init barriers
1819 }
1820 return false;
1821 }
1822 default: fatal("%s", CompileTask::reason_name(task()->compile_reason()));
1823 }
1824 return false;
1825 }
1826
1827 InstanceKlass::ClassState ciEnv::compute_init_state_for_precompiled(InstanceKlass* ik) {
1828 ASSERT_IN_VM;
1829 assert(is_precompiled(), "");
1830 ResourceMark rm;
1831 if (is_fully_initialized(ik)) {
1832 log_trace(precompile)("%d: fully_initialized: %s", task()->compile_id(), ik->external_name());
1833 return InstanceKlass::ClassState::fully_initialized;
1834 } else if (MetaspaceObj::is_shared(ik)) {
1835 guarantee(ik->is_loaded(), ""); // FIXME: assumes pre-loading by CDS; ik->is_linked() requires pre-linking
1836 log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1837 return ik->init_state(); // not yet initialized
1838 } else if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active()) {
1839 if (!ArchiveBuilder::current()->has_been_archived((address)ik)) {
1840 fatal("New workflow: should not compile code for unarchived class: %s", ik->external_name());
1841 }
1842 guarantee(ik->is_loaded(), "");
1843 log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1844 return ik->init_state(); // not yet initialized
1845 } else {
1846 // Not present in the archive.
1847 fatal("unloaded: %s", ik->external_name());
1848 // guarantee(SystemDictionaryShared::lookup_init_state(ik) == ik->init_state(), "");
1849 log_trace(precompile)("%d: allocated: %s", task()->compile_id(), ik->external_name());
1850 return InstanceKlass::ClassState::allocated; // not yet linked
1851 }
1852 }
|