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