1 /*
2 * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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 "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);
651 if (is_java_primitive(bt)) {
652 assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
653 return unbox_primitive_value(ciobj, bt);
654 } else {
655 assert(ciobj->is_instance(), "should be an instance");
656 return ciConstant(T_OBJECT, ciobj);
657 }
658 }
659 }
660 }
661
662 // ------------------------------------------------------------------
663 // ciEnv::get_constant_by_index_impl
664 //
665 // Implementation of get_constant_by_index().
666 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
667 int index, int obj_index,
668 ciInstanceKlass* accessor) {
669 if (obj_index >= 0) {
670 ciConstant con = get_resolved_constant(cpool, obj_index);
671 if (con.is_valid()) {
672 return con;
673 }
674 }
675 constantTag tag = cpool->tag_at(index);
676 if (tag.is_int()) {
677 return ciConstant(T_INT, (jint)cpool->int_at(index));
678 } else if (tag.is_long()) {
679 return ciConstant((jlong)cpool->long_at(index));
680 } else if (tag.is_float()) {
681 return ciConstant((jfloat)cpool->float_at(index));
682 } else if (tag.is_double()) {
683 return ciConstant((jdouble)cpool->double_at(index));
684 } else if (tag.is_string()) {
685 EXCEPTION_CONTEXT;
686 assert(obj_index >= 0, "should have an object index");
687 oop string = cpool->string_at(index, obj_index, THREAD);
688 if (HAS_PENDING_EXCEPTION) {
689 CLEAR_PENDING_EXCEPTION;
690 record_out_of_memory_failure();
691 return ciConstant();
852 case Bytecodes::_invokevirtual:
853 case Bytecodes::_invokeinterface:
854 case Bytecodes::_invokespecial:
855 case Bytecodes::_invokestatic:
856 {
857 Method* m = ConstantPool::method_at_if_loaded(cpool, index);
858 if (m != nullptr) {
859 return get_method(m);
860 }
861 }
862 break;
863 default:
864 break;
865 }
866 }
867
868 if (holder_is_accessible) { // Our declared holder is loaded.
869 constantTag tag = cpool->tag_ref_at(index, bc);
870 assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
871 Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
872 if (m != nullptr &&
873 (bc == Bytecodes::_invokestatic
874 ? m->method_holder()->is_not_initialized()
875 : !m->method_holder()->is_loaded())) {
876 m = nullptr;
877 }
878 if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
879 m = nullptr;
880 }
881 if (m != nullptr) {
882 // We found the method.
883 return get_method(m);
884 }
885 }
886
887 // Either the declared holder was not loaded, or the method could
888 // not be found. Create a dummy ciMethod to represent the failed
889 // lookup.
890 ciSymbol* name = get_symbol(name_sym);
891 ciSymbol* signature = get_symbol(sig_sym);
892 return get_unloaded_method(holder, name, signature, accessor);
893 }
894 }
895
896
937 _name_buffer =
938 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
939 _name_buffer_len = req_len;
940 }
941 }
942 return _name_buffer;
943 }
944
945 // ------------------------------------------------------------------
946 // ciEnv::is_in_vm
947 bool ciEnv::is_in_vm() {
948 return JavaThread::current()->thread_state() == _thread_in_vm;
949 }
950
951 // ------------------------------------------------------------------
952 // ciEnv::validate_compile_task_dependencies
953 //
954 // Check for changes during compilation (e.g. class loads, evolution,
955 // breakpoints, call site invalidation).
956 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
957 if (failing()) return; // no need for further checks
958
959 Dependencies::DepType result = dependencies()->validate_dependencies(_task);
960 if (result != Dependencies::end_marker) {
961 if (result == Dependencies::call_site_target_value) {
962 _inc_decompile_count_on_failure = false;
963 record_failure("call site target change");
964 } else if (Dependencies::is_klass_type(result)) {
965 record_failure("concurrent class loading");
966 } else {
967 record_failure("invalid non-klass dependency");
968 }
969 }
970 }
971
972 // ------------------------------------------------------------------
973 // ciEnv::register_method
974 void ciEnv::register_method(ciMethod* target,
975 int entry_bci,
976 CodeOffsets* offsets,
977 int orig_pc_offset,
978 CodeBuffer* code_buffer,
979 int frame_words,
980 OopMapSet* oop_map_set,
981 ExceptionHandlerTable* handler_table,
982 ImplicitExceptionTable* inc_table,
983 AbstractCompiler* compiler,
984 bool has_unsafe_access,
985 bool has_wide_vectors,
986 bool has_monitors,
987 bool has_scoped_access,
988 int immediate_oops_patched) {
989 VM_ENTRY_MARK;
990 nmethod* nm = nullptr;
991 {
992 methodHandle method(THREAD, target->get_Method());
993
994 // We require method counters to store some method state (max compilation levels) required by the compilation policy.
995 if (method->get_method_counters(THREAD) == nullptr) {
996 record_failure("can't create method counters");
997 // All buffers in the CodeBuffer are allocated in the CodeCache.
998 // If the code buffer is created on each compile attempt
999 // as in C2, then it must be freed.
1000 code_buffer->free_blob();
1001 return;
1002 }
1003
1004 // Check if memory should be freed before allocation
1005 CodeCache::gc_on_allocation();
1006
1007 // To prevent compile queue updates.
1008 MutexLocker locker(THREAD, MethodCompileQueue_lock);
1009
1010 // Prevent InstanceKlass::add_to_hierarchy from running
1011 // and invalidating our dependencies until we install this method.
1012 // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1013 MutexLocker ml(Compile_lock);
1014 NoSafepointVerifier nsv;
1015
1016 // Change in Jvmti state may invalidate compilation.
1017 if (!failing() && jvmti_state_changed()) {
1018 record_failure("Jvmti state change invalidated dependencies");
1019 }
1020
1021 // Change in DTrace flags may invalidate compilation.
1022 if (!failing() &&
1023 ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1024 (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1025 record_failure("DTrace flags change invalidated dependencies");
1026 }
1027
1028 if (!failing() && target->needs_clinit_barrier() &&
1029 target->holder()->is_in_error_state()) {
1030 record_failure("method holder is in error state");
1031 }
1032
1033 if (!failing()) {
1034 if (log() != nullptr) {
1035 // Log the dependencies which this compilation declares.
1036 dependencies()->log_all_dependencies();
1037 }
1038
1039 // Encode the dependencies now, so we can check them right away.
1040 dependencies()->encode_content_bytes();
1041
1042 // Check for {class loads, evolution, breakpoints, ...} during compilation
1043 validate_compile_task_dependencies(target);
1044 }
1045
1046 if (failing()) {
1047 // While not a true deoptimization, it is a preemptive decompile.
1048 MethodData* mdo = method()->method_data();
1049 if (mdo != nullptr && _inc_decompile_count_on_failure) {
1050 mdo->inc_decompile_count();
1051 }
1052
1053 // All buffers in the CodeBuffer are allocated in the CodeCache.
1054 // If the code buffer is created on each compile attempt
1055 // as in C2, then it must be freed.
1056 code_buffer->free_blob();
1057 return;
1058 }
1059
1060 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1061 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1062
1063 nm = nmethod::new_nmethod(method,
1064 compile_id(),
1065 entry_bci,
1066 offsets,
1067 orig_pc_offset,
1068 debug_info(), dependencies(), code_buffer,
1069 frame_words, oop_map_set,
1070 handler_table, inc_table,
1071 compiler, CompLevel(task()->comp_level()));
1072
1073 // Free codeBlobs
1074 code_buffer->free_blob();
1075
1076 if (nm != nullptr) {
1077 nm->set_has_unsafe_access(has_unsafe_access);
1078 nm->set_has_wide_vectors(has_wide_vectors);
1079 nm->set_has_monitors(has_monitors);
1080 nm->set_has_scoped_access(has_scoped_access);
1081 assert(!method->is_synchronized() || nm->has_monitors(), "");
1082
1083 if (entry_bci == InvocationEntryBci) {
1084 if (TieredCompilation) {
1085 // If there is an old version we're done with it
1086 nmethod* old = method->code();
1087 if (TraceMethodReplacement && old != nullptr) {
1088 ResourceMark rm;
1089 char *method_name = method->name_and_sig_as_C_string();
1090 tty->print_cr("Replacing method %s", method_name);
1091 }
1092 if (old != nullptr) {
1093 old->make_not_used();
1094 }
1095 }
1096
1097 LogTarget(Info, nmethod, install) lt;
1098 if (lt.is_enabled()) {
1099 ResourceMark rm;
1100 char *method_name = method->name_and_sig_as_C_string();
1101 lt.print("Installing method (%d) %s ",
1102 task()->comp_level(), method_name);
1103 }
1104 // Allow the code to be executed
1105 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1106 if (nm->make_in_use()) {
1107 method->set_code(method, nm);
1108 }
1109 } else {
1110 LogTarget(Info, nmethod, install) lt;
1111 if (lt.is_enabled()) {
1112 ResourceMark rm;
1113 char *method_name = method->name_and_sig_as_C_string();
1114 lt.print("Installing osr method (%d) %s @ %d",
1115 task()->comp_level(), method_name, entry_bci);
1116 }
1117 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1118 if (nm->make_in_use()) {
1119 method->method_holder()->add_osr_nmethod(nm);
1120 }
1121 }
1122 }
1123 }
1124
1125 NoSafepointVerifier nsv;
1126 if (nm != nullptr) {
1127 // Compilation succeeded, post what we know about it
1128 nm->post_compiled_method(task());
1129 task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1130 } else {
1131 // The CodeCache is full.
1132 record_failure("code cache is full");
1133 }
1134
1135 // safepoints are allowed again
1136 }
1137
1138 // ------------------------------------------------------------------
1139 // ciEnv::find_system_klass
1140 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1141 VM_ENTRY_MARK;
1142 return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1143 }
1144
1145 // ------------------------------------------------------------------
1146 // ciEnv::comp_level
1147 int ciEnv::comp_level() {
1148 if (task() == nullptr) return CompilationPolicy::highest_compile_level();
1149 return task()->comp_level();
1150 }
1151
1152 // ------------------------------------------------------------------
1153 // ciEnv::compile_id
1154 int ciEnv::compile_id() {
1155 if (task() == nullptr) return 0;
1156 return task()->compile_id();
1157 }
1158
1159 // ------------------------------------------------------------------
1160 // ciEnv::notice_inlined_method()
1161 void ciEnv::notice_inlined_method(ciMethod* method) {
1162 _num_inlined_bytecodes += method->code_size_for_inlining();
1163 }
1164
1165 // ------------------------------------------------------------------
1166 // ciEnv::num_inlined_bytecodes()
1167 int ciEnv::num_inlined_bytecodes() const {
1168 return _num_inlined_bytecodes;
1169 }
1170
1171 // ------------------------------------------------------------------
1172 // ciEnv::record_failure()
1173 void ciEnv::record_failure(const char* reason) {
1174 if (_failure_reason.get() == nullptr) {
1175 // Record the first failure reason.
1176 _failure_reason.set(reason);
1177 }
1178 }
1179
1180 void ciEnv::report_failure(const char* reason) {
1181 EventCompilationFailure event;
1182 if (event.should_commit()) {
1666 fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1667 GUARDED_VM_ENTRY(
1668 MutexLocker ml(Compile_lock);
1669 dump_replay_data_version(&replay_data_stream);
1670 dump_compile_data(&replay_data_stream);
1671 )
1672 replay_data_stream.flush();
1673 tty->print("# Compiler inline data is saved as: ");
1674 tty->print_cr("%s", buffer);
1675 } else {
1676 tty->print_cr("# Can't open file to dump inline data.");
1677 close(fd);
1678 }
1679 }
1680 }
1681 }
1682
1683 void ciEnv::dump_replay_data_version(outputStream* out) {
1684 out->print_cr("version %d", REPLAY_VERSION);
1685 }
|
1 /*
2 * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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 "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);
661 if (is_java_primitive(bt)) {
662 assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
663 return unbox_primitive_value(ciobj, bt);
664 } else {
665 assert(ciobj->is_instance(), "should be an instance");
666 return ciConstant(T_OBJECT, ciobj);
667 }
668 }
669 }
670 }
671
672 // ------------------------------------------------------------------
673 // ciEnv::get_constant_by_index_impl
674 //
675 // Implementation of get_constant_by_index().
676 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
677 int index, int obj_index,
678 ciInstanceKlass* accessor) {
679 if (obj_index >= 0) {
680 ciConstant con = get_resolved_constant(cpool, obj_index);
681 if (con.should_be_constant()) {
682 return con;
683 }
684 }
685 constantTag tag = cpool->tag_at(index);
686 if (tag.is_int()) {
687 return ciConstant(T_INT, (jint)cpool->int_at(index));
688 } else if (tag.is_long()) {
689 return ciConstant((jlong)cpool->long_at(index));
690 } else if (tag.is_float()) {
691 return ciConstant((jfloat)cpool->float_at(index));
692 } else if (tag.is_double()) {
693 return ciConstant((jdouble)cpool->double_at(index));
694 } else if (tag.is_string()) {
695 EXCEPTION_CONTEXT;
696 assert(obj_index >= 0, "should have an object index");
697 oop string = cpool->string_at(index, obj_index, THREAD);
698 if (HAS_PENDING_EXCEPTION) {
699 CLEAR_PENDING_EXCEPTION;
700 record_out_of_memory_failure();
701 return ciConstant();
862 case Bytecodes::_invokevirtual:
863 case Bytecodes::_invokeinterface:
864 case Bytecodes::_invokespecial:
865 case Bytecodes::_invokestatic:
866 {
867 Method* m = ConstantPool::method_at_if_loaded(cpool, index);
868 if (m != nullptr) {
869 return get_method(m);
870 }
871 }
872 break;
873 default:
874 break;
875 }
876 }
877
878 if (holder_is_accessible) { // Our declared holder is loaded.
879 constantTag tag = cpool->tag_ref_at(index, bc);
880 assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
881 Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
882 if (m != nullptr) {
883 ciInstanceKlass* cik = get_instance_klass(m->method_holder());
884 if ((bc == Bytecodes::_invokestatic && cik->is_not_initialized()) || !cik->is_loaded()) {
885 m = nullptr;
886 }
887 }
888 if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
889 m = nullptr;
890 }
891 if (m != nullptr) {
892 // We found the method.
893 return get_method(m);
894 }
895 }
896
897 // Either the declared holder was not loaded, or the method could
898 // not be found. Create a dummy ciMethod to represent the failed
899 // lookup.
900 ciSymbol* name = get_symbol(name_sym);
901 ciSymbol* signature = get_symbol(sig_sym);
902 return get_unloaded_method(holder, name, signature, accessor);
903 }
904 }
905
906
947 _name_buffer =
948 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
949 _name_buffer_len = req_len;
950 }
951 }
952 return _name_buffer;
953 }
954
955 // ------------------------------------------------------------------
956 // ciEnv::is_in_vm
957 bool ciEnv::is_in_vm() {
958 return JavaThread::current()->thread_state() == _thread_in_vm;
959 }
960
961 // ------------------------------------------------------------------
962 // ciEnv::validate_compile_task_dependencies
963 //
964 // Check for changes during compilation (e.g. class loads, evolution,
965 // breakpoints, call site invalidation).
966 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
967 assert(!failing(), "should not call this when failing");
968
969 Dependencies::DepType result = dependencies()->validate_dependencies(_task);
970 if (result != Dependencies::end_marker) {
971 if (result == Dependencies::call_site_target_value) {
972 _inc_decompile_count_on_failure = false;
973 record_failure("call site target change");
974 } else if (Dependencies::is_klass_type(result)) {
975 record_failure("concurrent class loading");
976 } else {
977 record_failure("invalid non-klass dependency");
978 }
979 }
980 }
981
982 // ------------------------------------------------------------------
983 // ciEnv::register_method
984 void ciEnv::register_method(ciMethod* target,
985 int entry_bci,
986 CodeOffsets* offsets,
987 int orig_pc_offset,
988 CodeBuffer* code_buffer,
989 int frame_words,
990 OopMapSet* oop_map_set,
991 ExceptionHandlerTable* handler_table,
992 ImplicitExceptionTable* inc_table,
993 AbstractCompiler* compiler,
994 bool has_clinit_barriers,
995 bool for_preload,
996 bool has_unsafe_access,
997 bool has_wide_vectors,
998 bool has_monitors,
999 bool has_scoped_access,
1000 int immediate_oops_patched,
1001 bool install_code,
1002 SCCEntry* scc_entry) {
1003 VM_ENTRY_MARK;
1004 nmethod* nm = nullptr;
1005 {
1006 methodHandle method(THREAD, target->get_Method());
1007 bool preload = task()->preload(); // Code is preloaded before Java method execution
1008
1009 // We require method counters to store some method state (max compilation levels) required by the compilation policy.
1010 if (!preload && method->get_method_counters(THREAD) == nullptr) {
1011 record_failure("can't create method counters");
1012 // All buffers in the CodeBuffer are allocated in the CodeCache.
1013 // If the code buffer is created on each compile attempt
1014 // as in C2, then it must be freed.
1015 // But keep shared code.
1016 code_buffer->free_blob();
1017 return;
1018 }
1019
1020 // Check if memory should be freed before allocation
1021 CodeCache::gc_on_allocation();
1022
1023 // To prevent compile queue updates.
1024 MutexLocker locker(THREAD, MethodCompileQueue_lock);
1025
1026 // Prevent InstanceKlass::add_to_hierarchy from running
1027 // and invalidating our dependencies until we install this method.
1028 // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1029 MutexLocker ml(Compile_lock);
1030 NoSafepointVerifier nsv;
1031
1032 if (scc_entry != nullptr) {
1033 // Invalid compilation states:
1034 // - SCCache is closed, SCC entry is garbage.
1035 // - SCC entry indicates this shared code was marked invalid while it was loaded.
1036 if (!SCCache::is_on() || scc_entry->not_entrant()) {
1037 code_buffer->free_blob();
1038 return;
1039 }
1040 }
1041
1042 // Change in Jvmti state may invalidate compilation.
1043 if (!failing() && jvmti_state_changed()) {
1044 record_failure("Jvmti state change invalidated dependencies");
1045 }
1046
1047 // Change in DTrace flags may invalidate compilation.
1048 if (!failing() &&
1049 ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1050 (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1051 record_failure("DTrace flags change invalidated dependencies");
1052 }
1053
1054 if (!preload && !failing() && target->needs_clinit_barrier() &&
1055 target->holder()->is_in_error_state()) {
1056 record_failure("method holder is in error state");
1057 }
1058
1059 if (!failing() && (scc_entry == nullptr)) {
1060 if (log() != nullptr) {
1061 // Log the dependencies which this compilation declares.
1062 dependencies()->log_all_dependencies();
1063 }
1064
1065 // Encode the dependencies now, so we can check them right away.
1066 dependencies()->encode_content_bytes();
1067 }
1068 // Check for {class loads, evolution, breakpoints, ...} during compilation
1069 if (!failing() && install_code) {
1070 // Check for {class loads, evolution, breakpoints, ...} during compilation
1071 validate_compile_task_dependencies(target);
1072 if (failing() && preload) {
1073 ResourceMark rm;
1074 char *method_name = method->name_and_sig_as_C_string();
1075 log_info(scc)("preload code for '%s' failed dependency check", method_name);
1076 }
1077 }
1078
1079 if (failing()) {
1080 // While not a true deoptimization, it is a preemptive decompile.
1081 MethodData* mdo = method()->method_data();
1082 if (mdo != nullptr && _inc_decompile_count_on_failure) {
1083 mdo->inc_decompile_count();
1084 }
1085
1086 // All buffers in the CodeBuffer are allocated in the CodeCache.
1087 // If the code buffer is created on each compile attempt
1088 // as in C2, then it must be freed.
1089 code_buffer->free_blob();
1090 return;
1091 }
1092
1093 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1094 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1095
1096 if (scc_entry == nullptr) {
1097 scc_entry = SCCache::store_nmethod(method,
1098 compile_id(),
1099 entry_bci,
1100 offsets,
1101 orig_pc_offset,
1102 debug_info(), dependencies(), code_buffer,
1103 frame_words, oop_map_set,
1104 handler_table, inc_table,
1105 compiler,
1106 CompLevel(task()->comp_level()),
1107 has_clinit_barriers,
1108 for_preload,
1109 has_unsafe_access,
1110 has_wide_vectors,
1111 has_monitors,
1112 has_scoped_access);
1113 if (scc_entry != nullptr) {
1114 scc_entry->set_inlined_bytecodes(num_inlined_bytecodes());
1115 if (has_clinit_barriers) {
1116 set_scc_clinit_barriers_entry(scc_entry); // Record it
1117 // Build second version of code without class initialization barriers
1118 code_buffer->free_blob();
1119 return;
1120 } else if (!for_preload) {
1121 SCCEntry* previous_entry = scc_clinit_barriers_entry();
1122 scc_entry->set_next(previous_entry); // Link it for case of deoptimization
1123 }
1124 }
1125 }
1126 if (install_code) {
1127 nm = nmethod::new_nmethod(method,
1128 compile_id(),
1129 entry_bci,
1130 offsets,
1131 orig_pc_offset,
1132 debug_info(), dependencies(), code_buffer,
1133 frame_words, oop_map_set,
1134 handler_table, inc_table,
1135 compiler, CompLevel(task()->comp_level()),
1136 scc_entry);
1137 }
1138 // Free codeBlobs
1139 code_buffer->free_blob();
1140
1141 if (nm != nullptr) {
1142 nm->set_has_unsafe_access(has_unsafe_access);
1143 nm->set_has_wide_vectors(has_wide_vectors);
1144 nm->set_has_monitors(has_monitors);
1145 nm->set_has_scoped_access(has_scoped_access);
1146 nm->set_preloaded(preload);
1147 nm->set_has_clinit_barriers(has_clinit_barriers);
1148 assert(!method->is_synchronized() || nm->has_monitors(), "");
1149
1150 if (entry_bci == InvocationEntryBci) {
1151 if (TieredCompilation) {
1152 // If there is an old version we're done with it
1153 nmethod* old = method->code();
1154 if (TraceMethodReplacement && old != nullptr) {
1155 ResourceMark rm;
1156 char *method_name = method->name_and_sig_as_C_string();
1157 tty->print_cr("Replacing method %s", method_name);
1158 }
1159 if (old != nullptr) {
1160 old->make_not_used();
1161 }
1162 }
1163
1164 LogTarget(Info, nmethod, install) lt;
1165 if (lt.is_enabled()) {
1166 ResourceMark rm;
1167 char *method_name = method->name_and_sig_as_C_string();
1168 lt.print("Installing method (L%d) %s id=%d scc=%s%s%u",
1169 task()->comp_level(), method_name, compile_id(),
1170 task()->is_scc() ? "A" : "", preload ? "P" : "",
1171 (scc_entry != nullptr ? scc_entry->offset() : 0));
1172 }
1173 // Allow the code to be executed
1174 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1175 if (nm->make_in_use()) {
1176 #ifdef ASSERT
1177 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1178 if (bs_nm != nullptr && bs_nm->supports_entry_barrier(nm)) {
1179 if (!bs_nm->is_armed(nm)) {
1180 log_info(init)("nmethod %d %d not armed", nm->compile_id(), nm->comp_level());
1181 }
1182 }
1183 #endif // ASSERT
1184 if (preload) {
1185 method->set_preload_code(nm);
1186 }
1187 if (!preload || target->holder()->is_linked()) {
1188 method->set_code(method, nm);
1189 }
1190 }
1191 } else {
1192 LogTarget(Info, nmethod, install) lt;
1193 if (lt.is_enabled()) {
1194 ResourceMark rm;
1195 char *method_name = method->name_and_sig_as_C_string();
1196 lt.print("Installing osr method (L%d) %s @ %d id=%u scc=%s%u",
1197 task()->comp_level(), method_name, entry_bci, compile_id(),
1198 task()->is_scc() ? "A" : "",
1199 (scc_entry != nullptr ? scc_entry->offset() : 0));
1200 }
1201 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1202 if (nm->make_in_use()) {
1203 method->method_holder()->add_osr_nmethod(nm);
1204 }
1205 }
1206 }
1207 }
1208
1209 NoSafepointVerifier nsv;
1210 if (nm != nullptr) {
1211 // Compilation succeeded, post what we know about it
1212 nm->post_compiled_method(task());
1213 task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1214 } else if (install_code) {
1215 // The CodeCache is full.
1216 record_failure("code cache is full");
1217 } else {
1218 task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1219 }
1220
1221 // safepoints are allowed again
1222 }
1223
1224 // ------------------------------------------------------------------
1225 // ciEnv::find_system_klass
1226 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1227 VM_ENTRY_MARK;
1228 return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1229 }
1230
1231 // ------------------------------------------------------------------
1232 // ciEnv::comp_level
1233 int ciEnv::comp_level() {
1234 if (task() == nullptr) return CompilationPolicy::highest_compile_level();
1235 return task()->comp_level();
1236 }
1237
1238 // ------------------------------------------------------------------
1239 // ciEnv::compile_id
1240 int ciEnv::compile_id() {
1241 if (task() == nullptr) return 0;
1242 return task()->compile_id();
1243 }
1244
1245 // ------------------------------------------------------------------
1246 // ciEnv::notice_inlined_method()
1247 void ciEnv::notice_inlined_method(ciMethod* method) {
1248 _num_inlined_bytecodes += method->code_size_for_inlining();
1249 CompileTrainingData* tdata = task()->training_data();
1250 if (tdata != nullptr) {
1251 GUARDED_VM_ENTRY({
1252 methodHandle mh(Thread::current(), method->get_Method());
1253 tdata->notice_inlined_method(task(), mh);
1254 });
1255 }
1256 }
1257
1258 // ------------------------------------------------------------------
1259 // ciEnv::num_inlined_bytecodes()
1260 int ciEnv::num_inlined_bytecodes() const {
1261 return _num_inlined_bytecodes;
1262 }
1263
1264 // ------------------------------------------------------------------
1265 // ciEnv::record_failure()
1266 void ciEnv::record_failure(const char* reason) {
1267 if (_failure_reason.get() == nullptr) {
1268 // Record the first failure reason.
1269 _failure_reason.set(reason);
1270 }
1271 }
1272
1273 void ciEnv::report_failure(const char* reason) {
1274 EventCompilationFailure event;
1275 if (event.should_commit()) {
1759 fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1760 GUARDED_VM_ENTRY(
1761 MutexLocker ml(Compile_lock);
1762 dump_replay_data_version(&replay_data_stream);
1763 dump_compile_data(&replay_data_stream);
1764 )
1765 replay_data_stream.flush();
1766 tty->print("# Compiler inline data is saved as: ");
1767 tty->print_cr("%s", buffer);
1768 } else {
1769 tty->print_cr("# Can't open file to dump inline data.");
1770 close(fd);
1771 }
1772 }
1773 }
1774 }
1775
1776 void ciEnv::dump_replay_data_version(outputStream* out) {
1777 out->print_cr("version %d", REPLAY_VERSION);
1778 }
1779
1780 bool ciEnv::is_precompiled() {
1781 return (task() != nullptr) && (task()->compile_reason() == CompileTask::Reason_Precompile ||
1782 task()->compile_reason() == CompileTask::Reason_PrecompileForPreload);
1783 }
1784
1785 bool ciEnv::is_fully_initialized(InstanceKlass* ik) {
1786 assert(is_precompiled(), "");
1787 if (task()->method()->method_holder() == ik) {
1788 return true; // FIXME: may be too strong; being_initialized, at least
1789 }
1790 switch (task()->compile_reason()) {
1791 case CompileTask::Reason_Precompile: {
1792 // check init dependencies
1793 MethodTrainingData* mtd = nullptr;
1794 GUARDED_VM_ENTRY(mtd = MethodTrainingData::find(methodHandle(Thread::current(), task()->method())); )
1795 if (mtd != nullptr) {
1796 CompileTrainingData* ctd = mtd->last_toplevel_compile(task()->comp_level());
1797 if (ctd != nullptr) {
1798 for (int i = 0; i < ctd->init_dep_count(); i++) {
1799 KlassTrainingData* ktd = ctd->init_dep(i);
1800 if (ktd->has_holder() && (ktd->holder() == ik)) {
1801 log_trace(precompile)("%d: init_dependency: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1802 return true; // init dependency present
1803 }
1804 }
1805 }
1806 }
1807 return false; // no init dependency
1808 }
1809 case CompileTask::Reason_PrecompileForPreload: {
1810 // FIXME: assumes that all shared classes are initialized
1811 if (ik->is_shared()) {
1812 return true; // class init barriers
1813 }
1814 if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active() &&
1815 ArchiveBuilder::current()->has_been_archived((address)ik)) {
1816 return true; // class init barriers
1817 }
1818 return false;
1819 }
1820 default: fatal("%s", CompileTask::reason_name(task()->compile_reason()));
1821 }
1822 return false;
1823 }
1824
1825 InstanceKlass::ClassState ciEnv::compute_init_state_for_precompiled(InstanceKlass* ik) {
1826 ASSERT_IN_VM;
1827 assert(is_precompiled(), "");
1828 ResourceMark rm;
1829 if (is_fully_initialized(ik)) {
1830 log_trace(precompile)("%d: fully_initialized: %s", task()->compile_id(), ik->external_name());
1831 return InstanceKlass::ClassState::fully_initialized;
1832 } else if (MetaspaceObj::is_shared(ik)) {
1833 guarantee(ik->is_loaded(), ""); // FIXME: assumes pre-loading by CDS; ik->is_linked() requires pre-linking
1834 log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1835 return ik->init_state(); // not yet initialized
1836 } else if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active()) {
1837 if (!ArchiveBuilder::current()->has_been_archived((address)ik)) {
1838 fatal("New workflow: should not compile code for unarchived class: %s", ik->external_name());
1839 }
1840 guarantee(ik->is_loaded(), "");
1841 log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1842 return ik->init_state(); // not yet initialized
1843 } else {
1844 // Not present in the archive.
1845 fatal("unloaded: %s", ik->external_name());
1846 // guarantee(SystemDictionaryShared::lookup_init_state(ik) == ik->init_state(), "");
1847 log_trace(precompile)("%d: allocated: %s", task()->compile_id(), ik->external_name());
1848 return InstanceKlass::ClassState::allocated; // not yet linked
1849 }
1850 }
|