src/share/vm/ci/ciEnv.cpp

Wed, 27 Aug 2014 08:19:12 -0400

author
zgu
date
Wed, 27 Aug 2014 08:19:12 -0400
changeset 7074
833b0f92429a
parent 6911
ce8f6bb717c9
child 7183
dd89808e49ba
permissions
-rw-r--r--

8046598: Scalable Native memory tracking development
Summary: Enhance scalability of native memory tracking
Reviewed-by: coleenp, ctornqvi, gtriantafill

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "ci/ciConstant.hpp"
stefank@2314 27 #include "ci/ciEnv.hpp"
stefank@2314 28 #include "ci/ciField.hpp"
stefank@2314 29 #include "ci/ciInstance.hpp"
stefank@2314 30 #include "ci/ciInstanceKlass.hpp"
stefank@2314 31 #include "ci/ciMethod.hpp"
stefank@2314 32 #include "ci/ciNullObject.hpp"
minqi@4267 33 #include "ci/ciReplay.hpp"
stefank@2314 34 #include "ci/ciUtilities.hpp"
stefank@2314 35 #include "classfile/systemDictionary.hpp"
stefank@2314 36 #include "classfile/vmSymbols.hpp"
stefank@2314 37 #include "code/scopeDesc.hpp"
stefank@2314 38 #include "compiler/compileBroker.hpp"
stefank@2314 39 #include "compiler/compileLog.hpp"
stefank@2314 40 #include "compiler/compilerOracle.hpp"
stefank@2314 41 #include "gc_interface/collectedHeap.inline.hpp"
stefank@2314 42 #include "interpreter/linkResolver.hpp"
stefank@2314 43 #include "memory/allocation.inline.hpp"
stefank@2314 44 #include "memory/oopFactory.hpp"
stefank@2314 45 #include "memory/universe.inline.hpp"
coleenp@4037 46 #include "oops/methodData.hpp"
stefank@2314 47 #include "oops/objArrayKlass.hpp"
stefank@2314 48 #include "oops/oop.inline.hpp"
stefank@2314 49 #include "oops/oop.inline2.hpp"
stefank@2314 50 #include "prims/jvmtiExport.hpp"
stefank@2314 51 #include "runtime/init.hpp"
stefank@2314 52 #include "runtime/reflection.hpp"
stefank@2314 53 #include "runtime/sharedRuntime.hpp"
goetz@6911 54 #include "runtime/thread.inline.hpp"
stefank@2314 55 #include "utilities/dtrace.hpp"
jprovino@4542 56 #include "utilities/macros.hpp"
stefank@2314 57 #ifdef COMPILER1
stefank@2314 58 #include "c1/c1_Runtime1.hpp"
stefank@2314 59 #endif
stefank@2314 60 #ifdef COMPILER2
stefank@2314 61 #include "opto/runtime.hpp"
stefank@2314 62 #endif
duke@435 63
duke@435 64 // ciEnv
duke@435 65 //
duke@435 66 // This class is the top level broker for requests from the compiler
duke@435 67 // to the VM.
duke@435 68
duke@435 69 ciObject* ciEnv::_null_object_instance;
duke@435 70
never@1577 71 #define WK_KLASS_DEFN(name, ignore_s, ignore_o) ciInstanceKlass* ciEnv::_##name = NULL;
never@1577 72 WK_KLASSES_DO(WK_KLASS_DEFN)
never@1577 73 #undef WK_KLASS_DEFN
duke@435 74
duke@435 75 ciSymbol* ciEnv::_unloaded_cisymbol = NULL;
duke@435 76 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = NULL;
duke@435 77 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = NULL;
duke@435 78
duke@435 79 jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = NULL;
duke@435 80 jobject ciEnv::_ArrayStoreException_handle = NULL;
duke@435 81 jobject ciEnv::_ClassCastException_handle = NULL;
duke@435 82
duke@435 83 #ifndef PRODUCT
duke@435 84 static bool firstEnv = true;
duke@435 85 #endif /* PRODUCT */
duke@435 86
duke@435 87 // ------------------------------------------------------------------
duke@435 88 // ciEnv::ciEnv
zgu@7074 89 ciEnv::ciEnv(CompileTask* task, int system_dictionary_modification_counter)
zgu@7074 90 : _ciEnv_arena(mtCompiler) {
duke@435 91 VM_ENTRY_MARK;
duke@435 92
duke@435 93 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
duke@435 94 thread->set_env(this);
duke@435 95 assert(ciEnv::current() == this, "sanity");
duke@435 96
duke@435 97 _oop_recorder = NULL;
duke@435 98 _debug_info = NULL;
duke@435 99 _dependencies = NULL;
duke@435 100 _failure_reason = NULL;
duke@435 101 _compilable = MethodCompilable;
duke@435 102 _break_at_compile = false;
duke@435 103 _compiler_data = NULL;
duke@435 104 #ifndef PRODUCT
duke@435 105 assert(!firstEnv, "not initialized properly");
duke@435 106 #endif /* !PRODUCT */
duke@435 107
duke@435 108 _system_dictionary_modification_counter = system_dictionary_modification_counter;
duke@435 109 _num_inlined_bytecodes = 0;
duke@435 110 assert(task == NULL || thread->task() == task, "sanity");
duke@435 111 _task = task;
duke@435 112 _log = NULL;
duke@435 113
duke@435 114 // Temporary buffer for creating symbols and such.
duke@435 115 _name_buffer = NULL;
duke@435 116 _name_buffer_len = 0;
duke@435 117
duke@435 118 _arena = &_ciEnv_arena;
duke@435 119 _factory = new (_arena) ciObjectFactory(_arena, 128);
duke@435 120
duke@435 121 // Preload commonly referenced system ciObjects.
duke@435 122
duke@435 123 // During VM initialization, these instances have not yet been created.
duke@435 124 // Assertions ensure that these instances are not accessed before
duke@435 125 // their initialization.
duke@435 126
duke@435 127 assert(Universe::is_fully_initialized(), "should be complete");
duke@435 128
duke@435 129 oop o = Universe::null_ptr_exception_instance();
duke@435 130 assert(o != NULL, "should have been initialized");
duke@435 131 _NullPointerException_instance = get_object(o)->as_instance();
duke@435 132 o = Universe::arithmetic_exception_instance();
duke@435 133 assert(o != NULL, "should have been initialized");
duke@435 134 _ArithmeticException_instance = get_object(o)->as_instance();
duke@435 135
duke@435 136 _ArrayIndexOutOfBoundsException_instance = NULL;
duke@435 137 _ArrayStoreException_instance = NULL;
duke@435 138 _ClassCastException_instance = NULL;
never@1515 139 _the_null_string = NULL;
never@1515 140 _the_min_jint_string = NULL;
duke@435 141 }
duke@435 142
zgu@7074 143 ciEnv::ciEnv(Arena* arena) : _ciEnv_arena(mtCompiler) {
duke@435 144 ASSERT_IN_VM;
duke@435 145
duke@435 146 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
duke@435 147 CompilerThread* current_thread = CompilerThread::current();
duke@435 148 assert(current_thread->env() == NULL, "must be");
duke@435 149 current_thread->set_env(this);
duke@435 150 assert(ciEnv::current() == this, "sanity");
duke@435 151
duke@435 152 _oop_recorder = NULL;
duke@435 153 _debug_info = NULL;
duke@435 154 _dependencies = NULL;
duke@435 155 _failure_reason = NULL;
duke@435 156 _compilable = MethodCompilable_never;
duke@435 157 _break_at_compile = false;
duke@435 158 _compiler_data = NULL;
duke@435 159 #ifndef PRODUCT
duke@435 160 assert(firstEnv, "must be first");
duke@435 161 firstEnv = false;
duke@435 162 #endif /* !PRODUCT */
duke@435 163
duke@435 164 _system_dictionary_modification_counter = 0;
duke@435 165 _num_inlined_bytecodes = 0;
duke@435 166 _task = NULL;
duke@435 167 _log = NULL;
duke@435 168
duke@435 169 // Temporary buffer for creating symbols and such.
duke@435 170 _name_buffer = NULL;
duke@435 171 _name_buffer_len = 0;
duke@435 172
duke@435 173 _arena = arena;
duke@435 174 _factory = new (_arena) ciObjectFactory(_arena, 128);
duke@435 175
duke@435 176 // Preload commonly referenced system ciObjects.
duke@435 177
duke@435 178 // During VM initialization, these instances have not yet been created.
duke@435 179 // Assertions ensure that these instances are not accessed before
duke@435 180 // their initialization.
duke@435 181
duke@435 182 assert(Universe::is_fully_initialized(), "must be");
duke@435 183
coleenp@4037 184 _NullPointerException_instance = NULL;
coleenp@4037 185 _ArithmeticException_instance = NULL;
duke@435 186 _ArrayIndexOutOfBoundsException_instance = NULL;
duke@435 187 _ArrayStoreException_instance = NULL;
duke@435 188 _ClassCastException_instance = NULL;
never@1515 189 _the_null_string = NULL;
never@1515 190 _the_min_jint_string = NULL;
duke@435 191 }
duke@435 192
duke@435 193 ciEnv::~ciEnv() {
duke@435 194 CompilerThread* current_thread = CompilerThread::current();
coleenp@2497 195 _factory->remove_symbols();
coleenp@4037 196 // Need safepoint to clear the env on the thread. RedefineClasses might
coleenp@4037 197 // be reading it.
coleenp@4037 198 GUARDED_VM_ENTRY(current_thread->set_env(NULL);)
duke@435 199 }
duke@435 200
duke@435 201 // ------------------------------------------------------------------
kvn@1215 202 // Cache Jvmti state
kvn@1215 203 void ciEnv::cache_jvmti_state() {
kvn@1215 204 VM_ENTRY_MARK;
kvn@1215 205 // Get Jvmti capabilities under lock to get consistant values.
kvn@1215 206 MutexLocker mu(JvmtiThreadState_lock);
kvn@1215 207 _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint();
kvn@1215 208 _jvmti_can_access_local_variables = JvmtiExport::can_access_local_variables();
dcubed@1648 209 _jvmti_can_post_on_exceptions = JvmtiExport::can_post_on_exceptions();
kvn@1215 210 }
kvn@1215 211
kvn@1215 212 // ------------------------------------------------------------------
kvn@1215 213 // Cache DTrace flags
kvn@1215 214 void ciEnv::cache_dtrace_flags() {
kvn@1215 215 // Need lock?
kvn@1215 216 _dtrace_extended_probes = ExtendedDTraceProbes;
kvn@1215 217 if (_dtrace_extended_probes) {
kvn@1215 218 _dtrace_monitor_probes = true;
kvn@1215 219 _dtrace_method_probes = true;
kvn@1215 220 _dtrace_alloc_probes = true;
kvn@1215 221 } else {
kvn@1215 222 _dtrace_monitor_probes = DTraceMonitorProbes;
kvn@1215 223 _dtrace_method_probes = DTraceMethodProbes;
kvn@1215 224 _dtrace_alloc_probes = DTraceAllocProbes;
kvn@1215 225 }
kvn@1215 226 }
kvn@1215 227
kvn@1215 228 // ------------------------------------------------------------------
duke@435 229 // helper for lazy exception creation
coleenp@2497 230 ciInstance* ciEnv::get_or_create_exception(jobject& handle, Symbol* name) {
duke@435 231 VM_ENTRY_MARK;
duke@435 232 if (handle == NULL) {
duke@435 233 // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance.
coleenp@4037 234 Klass* k = SystemDictionary::find(name, Handle(), Handle(), THREAD);
duke@435 235 jobject objh = NULL;
duke@435 236 if (!HAS_PENDING_EXCEPTION && k != NULL) {
coleenp@4037 237 oop obj = InstanceKlass::cast(k)->allocate_instance(THREAD);
duke@435 238 if (!HAS_PENDING_EXCEPTION)
duke@435 239 objh = JNIHandles::make_global(obj);
duke@435 240 }
duke@435 241 if (HAS_PENDING_EXCEPTION) {
duke@435 242 CLEAR_PENDING_EXCEPTION;
duke@435 243 } else {
duke@435 244 handle = objh;
duke@435 245 }
duke@435 246 }
duke@435 247 oop obj = JNIHandles::resolve(handle);
duke@435 248 return obj == NULL? NULL: get_object(obj)->as_instance();
duke@435 249 }
duke@435 250
duke@435 251 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
duke@435 252 if (_ArrayIndexOutOfBoundsException_instance == NULL) {
duke@435 253 _ArrayIndexOutOfBoundsException_instance
duke@435 254 = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle,
coleenp@2497 255 vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
duke@435 256 }
duke@435 257 return _ArrayIndexOutOfBoundsException_instance;
duke@435 258 }
duke@435 259 ciInstance* ciEnv::ArrayStoreException_instance() {
duke@435 260 if (_ArrayStoreException_instance == NULL) {
duke@435 261 _ArrayStoreException_instance
duke@435 262 = get_or_create_exception(_ArrayStoreException_handle,
coleenp@2497 263 vmSymbols::java_lang_ArrayStoreException());
duke@435 264 }
duke@435 265 return _ArrayStoreException_instance;
duke@435 266 }
duke@435 267 ciInstance* ciEnv::ClassCastException_instance() {
duke@435 268 if (_ClassCastException_instance == NULL) {
duke@435 269 _ClassCastException_instance
duke@435 270 = get_or_create_exception(_ClassCastException_handle,
coleenp@2497 271 vmSymbols::java_lang_ClassCastException());
duke@435 272 }
duke@435 273 return _ClassCastException_instance;
duke@435 274 }
duke@435 275
never@1515 276 ciInstance* ciEnv::the_null_string() {
never@1515 277 if (_the_null_string == NULL) {
never@1515 278 VM_ENTRY_MARK;
never@1515 279 _the_null_string = get_object(Universe::the_null_string())->as_instance();
never@1515 280 }
never@1515 281 return _the_null_string;
never@1515 282 }
never@1515 283
never@1515 284 ciInstance* ciEnv::the_min_jint_string() {
never@1515 285 if (_the_min_jint_string == NULL) {
never@1515 286 VM_ENTRY_MARK;
never@1515 287 _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance();
never@1515 288 }
never@1515 289 return _the_min_jint_string;
never@1515 290 }
never@1515 291
duke@435 292 // ------------------------------------------------------------------
duke@435 293 // ciEnv::get_method_from_handle
coleenp@4037 294 ciMethod* ciEnv::get_method_from_handle(Method* method) {
duke@435 295 VM_ENTRY_MARK;
coleenp@4037 296 return get_metadata(method)->as_method();
duke@435 297 }
duke@435 298
duke@435 299 // ------------------------------------------------------------------
duke@435 300 // ciEnv::array_element_offset_in_bytes
duke@435 301 int ciEnv::array_element_offset_in_bytes(ciArray* a_h, ciObject* o_h) {
duke@435 302 VM_ENTRY_MARK;
duke@435 303 objArrayOop a = (objArrayOop)a_h->get_oop();
duke@435 304 assert(a->is_objArray(), "");
duke@435 305 int length = a->length();
duke@435 306 oop o = o_h->get_oop();
duke@435 307 for (int i = 0; i < length; i++) {
duke@435 308 if (a->obj_at(i) == o) return i;
duke@435 309 }
duke@435 310 return -1;
duke@435 311 }
duke@435 312
duke@435 313
duke@435 314 // ------------------------------------------------------------------
duke@435 315 // ciEnv::check_klass_accessiblity
duke@435 316 //
duke@435 317 // Note: the logic of this method should mirror the logic of
coleenp@4037 318 // ConstantPool::verify_constant_pool_resolve.
duke@435 319 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass,
coleenp@4037 320 Klass* resolved_klass) {
duke@435 321 if (accessing_klass == NULL || !accessing_klass->is_loaded()) {
duke@435 322 return true;
duke@435 323 }
coleenp@4037 324 if (accessing_klass->is_obj_array_klass()) {
duke@435 325 accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass();
duke@435 326 }
duke@435 327 if (!accessing_klass->is_instance_klass()) {
duke@435 328 return true;
duke@435 329 }
duke@435 330
coleenp@4037 331 if (resolved_klass->oop_is_objArray()) {
duke@435 332 // Find the element klass, if this is an array.
coleenp@4142 333 resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
duke@435 334 }
coleenp@4037 335 if (resolved_klass->oop_is_instance()) {
coleenp@4037 336 return Reflection::verify_class_access(accessing_klass->get_Klass(),
duke@435 337 resolved_klass,
duke@435 338 true);
duke@435 339 }
duke@435 340 return true;
duke@435 341 }
duke@435 342
duke@435 343 // ------------------------------------------------------------------
duke@435 344 // ciEnv::get_klass_by_name_impl
duke@435 345 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass,
jrose@2982 346 constantPoolHandle cpool,
duke@435 347 ciSymbol* name,
duke@435 348 bool require_local) {
duke@435 349 ASSERT_IN_VM;
duke@435 350 EXCEPTION_CONTEXT;
duke@435 351
duke@435 352 // Now we need to check the SystemDictionary
coleenp@2497 353 Symbol* sym = name->get_symbol();
duke@435 354 if (sym->byte_at(0) == 'L' &&
duke@435 355 sym->byte_at(sym->utf8_length()-1) == ';') {
duke@435 356 // This is a name from a signature. Strip off the trimmings.
coleenp@2497 357 // Call recursive to keep scope of strippedsym.
coleenp@2497 358 TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
coleenp@2497 359 sym->utf8_length()-2,
coleenp@2497 360 KILL_COMPILE_ON_FATAL_(_unloaded_ciinstance_klass));
coleenp@2497 361 ciSymbol* strippedname = get_symbol(strippedsym);
jrose@2982 362 return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local);
duke@435 363 }
duke@435 364
duke@435 365 // Check for prior unloaded klass. The SystemDictionary's answers
duke@435 366 // can vary over time but the compiler needs consistency.
duke@435 367 ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name);
duke@435 368 if (unloaded_klass != NULL) {
duke@435 369 if (require_local) return NULL;
duke@435 370 return unloaded_klass;
duke@435 371 }
duke@435 372
duke@435 373 Handle loader(THREAD, (oop)NULL);
duke@435 374 Handle domain(THREAD, (oop)NULL);
duke@435 375 if (accessing_klass != NULL) {
duke@435 376 loader = Handle(THREAD, accessing_klass->loader());
duke@435 377 domain = Handle(THREAD, accessing_klass->protection_domain());
duke@435 378 }
duke@435 379
duke@435 380 // setup up the proper type to return on OOM
duke@435 381 ciKlass* fail_type;
duke@435 382 if (sym->byte_at(0) == '[') {
duke@435 383 fail_type = _unloaded_ciobjarrayklass;
duke@435 384 } else {
duke@435 385 fail_type = _unloaded_ciinstance_klass;
duke@435 386 }
kamg@2467 387 KlassHandle found_klass;
never@2551 388 {
never@2570 389 ttyUnlocker ttyul; // release tty lock to avoid ordering problems
never@2551 390 MutexLocker ml(Compile_lock);
coleenp@4037 391 Klass* kls;
never@2551 392 if (!require_local) {
never@2551 393 kls = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader,
never@2551 394 KILL_COMPILE_ON_FATAL_(fail_type));
never@2551 395 } else {
never@2551 396 kls = SystemDictionary::find_instance_or_array_klass(sym, loader, domain,
never@2551 397 KILL_COMPILE_ON_FATAL_(fail_type));
never@2551 398 }
kamg@2467 399 found_klass = KlassHandle(THREAD, kls);
duke@435 400 }
duke@435 401
duke@435 402 // If we fail to find an array klass, look again for its element type.
duke@435 403 // The element type may be available either locally or via constraints.
duke@435 404 // In either case, if we can find the element type in the system dictionary,
duke@435 405 // we must build an array type around it. The CI requires array klasses
duke@435 406 // to be loaded if their element klasses are loaded, except when memory
duke@435 407 // is exhausted.
duke@435 408 if (sym->byte_at(0) == '[' &&
duke@435 409 (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L')) {
duke@435 410 // We have an unloaded array.
duke@435 411 // Build it on the fly if the element class exists.
coleenp@2497 412 TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
coleenp@2497 413 sym->utf8_length()-1,
coleenp@2497 414 KILL_COMPILE_ON_FATAL_(fail_type));
coleenp@2497 415
duke@435 416 // Get element ciKlass recursively.
duke@435 417 ciKlass* elem_klass =
duke@435 418 get_klass_by_name_impl(accessing_klass,
jrose@2982 419 cpool,
coleenp@2497 420 get_symbol(elem_sym),
duke@435 421 require_local);
duke@435 422 if (elem_klass != NULL && elem_klass->is_loaded()) {
duke@435 423 // Now make an array for it
duke@435 424 return ciObjArrayKlass::make_impl(elem_klass);
duke@435 425 }
duke@435 426 }
duke@435 427
jrose@2982 428 if (found_klass() == NULL && !cpool.is_null() && cpool->has_preresolution()) {
jrose@2982 429 // Look inside the constant pool for pre-resolved class entries.
jrose@2982 430 for (int i = cpool->length() - 1; i >= 1; i--) {
jrose@2982 431 if (cpool->tag_at(i).is_klass()) {
coleenp@4037 432 Klass* kls = cpool->resolved_klass_at(i);
hseigel@4278 433 if (kls->name() == sym) {
jrose@2982 434 found_klass = KlassHandle(THREAD, kls);
jrose@2982 435 break;
jrose@2982 436 }
jrose@2982 437 }
jrose@2982 438 }
jrose@2982 439 }
jrose@2982 440
kamg@2467 441 if (found_klass() != NULL) {
jrose@1424 442 // Found it. Build a CI handle.
coleenp@4037 443 return get_klass(found_klass());
jrose@1424 444 }
jrose@1424 445
duke@435 446 if (require_local) return NULL;
twisti@3197 447
duke@435 448 // Not yet loaded into the VM, or not governed by loader constraints.
duke@435 449 // Make a CI representative for it.
duke@435 450 return get_unloaded_klass(accessing_klass, name);
duke@435 451 }
duke@435 452
duke@435 453 // ------------------------------------------------------------------
duke@435 454 // ciEnv::get_klass_by_name
duke@435 455 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass,
duke@435 456 ciSymbol* klass_name,
duke@435 457 bool require_local) {
duke@435 458 GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass,
jrose@2982 459 constantPoolHandle(),
duke@435 460 klass_name,
duke@435 461 require_local);)
duke@435 462 }
duke@435 463
duke@435 464 // ------------------------------------------------------------------
duke@435 465 // ciEnv::get_klass_by_index_impl
duke@435 466 //
duke@435 467 // Implementation of get_klass_by_index.
twisti@1573 468 ciKlass* ciEnv::get_klass_by_index_impl(constantPoolHandle cpool,
duke@435 469 int index,
twisti@1573 470 bool& is_accessible,
twisti@1573 471 ciInstanceKlass* accessor) {
duke@435 472 EXCEPTION_CONTEXT;
coleenp@4037 473 KlassHandle klass; // = NULL;
coleenp@2497 474 Symbol* klass_name = NULL;
coleenp@4037 475
coleenp@4037 476 if (cpool->tag_at(index).is_symbol()) {
coleenp@4037 477 klass_name = cpool->symbol_at(index);
coleenp@4037 478 } else {
coleenp@4037 479 // Check if it's resolved if it's not a symbol constant pool entry.
coleenp@4037 480 klass = KlassHandle(THREAD, ConstantPool::klass_at_if_loaded(cpool, index));
coleenp@4037 481
duke@435 482 if (klass.is_null()) {
duke@435 483 // The klass has not been inserted into the constant pool.
duke@435 484 // Try to look it up by name.
duke@435 485 {
duke@435 486 // We have to lock the cpool to keep the oop from being resolved
duke@435 487 // while we are accessing it.
iklam@5971 488 MonitorLockerEx ml(cpool->lock());
duke@435 489 constantTag tag = cpool->tag_at(index);
duke@435 490 if (tag.is_klass()) {
duke@435 491 // The klass has been inserted into the constant pool
duke@435 492 // very recently.
duke@435 493 klass = KlassHandle(THREAD, cpool->resolved_klass_at(index));
duke@435 494 } else {
duke@435 495 assert(cpool->tag_at(index).is_unresolved_klass(), "wrong tag");
coleenp@2497 496 klass_name = cpool->unresolved_klass_at(index);
duke@435 497 }
duke@435 498 }
duke@435 499 }
coleenp@4037 500 }
duke@435 501
duke@435 502 if (klass.is_null()) {
duke@435 503 // Not found in constant pool. Use the name to do the lookup.
duke@435 504 ciKlass* k = get_klass_by_name_impl(accessor,
jrose@2982 505 cpool,
coleenp@2497 506 get_symbol(klass_name),
duke@435 507 false);
duke@435 508 // Calculate accessibility the hard way.
duke@435 509 if (!k->is_loaded()) {
duke@435 510 is_accessible = false;
duke@435 511 } else if (k->loader() != accessor->loader() &&
jrose@2982 512 get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
duke@435 513 // Loaded only remotely. Not linked yet.
duke@435 514 is_accessible = false;
duke@435 515 } else {
duke@435 516 // Linked locally, and we must also check public/private, etc.
coleenp@4037 517 is_accessible = check_klass_accessibility(accessor, k->get_Klass());
duke@435 518 }
duke@435 519 return k;
duke@435 520 }
duke@435 521
duke@435 522 // Check for prior unloaded klass. The SystemDictionary's answers
duke@435 523 // can vary over time but the compiler needs consistency.
coleenp@4037 524 ciSymbol* name = get_symbol(klass()->name());
duke@435 525 ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name);
duke@435 526 if (unloaded_klass != NULL) {
duke@435 527 is_accessible = false;
duke@435 528 return unloaded_klass;
duke@435 529 }
duke@435 530
duke@435 531 // It is known to be accessible, since it was found in the constant pool.
duke@435 532 is_accessible = true;
coleenp@4037 533 return get_klass(klass());
duke@435 534 }
duke@435 535
duke@435 536 // ------------------------------------------------------------------
duke@435 537 // ciEnv::get_klass_by_index
duke@435 538 //
duke@435 539 // Get a klass from the constant pool.
twisti@1573 540 ciKlass* ciEnv::get_klass_by_index(constantPoolHandle cpool,
duke@435 541 int index,
twisti@1573 542 bool& is_accessible,
twisti@1573 543 ciInstanceKlass* accessor) {
twisti@1573 544 GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);)
duke@435 545 }
duke@435 546
duke@435 547 // ------------------------------------------------------------------
duke@435 548 // ciEnv::get_constant_by_index_impl
duke@435 549 //
duke@435 550 // Implementation of get_constant_by_index().
twisti@1573 551 ciConstant ciEnv::get_constant_by_index_impl(constantPoolHandle cpool,
jrose@1957 552 int pool_index, int cache_index,
twisti@1573 553 ciInstanceKlass* accessor) {
jrose@1957 554 bool ignore_will_link;
duke@435 555 EXCEPTION_CONTEXT;
jrose@1957 556 int index = pool_index;
jrose@1957 557 if (cache_index >= 0) {
jrose@1957 558 assert(index < 0, "only one kind of index at a time");
coleenp@4037 559 oop obj = cpool->resolved_references()->obj_at(cache_index);
jrose@1957 560 if (obj != NULL) {
jrose@1957 561 ciObject* ciobj = get_object(obj);
jrose@1957 562 return ciConstant(T_OBJECT, ciobj);
jrose@1957 563 }
coleenp@4037 564 index = cpool->object_to_cp_index(cache_index);
jrose@1957 565 }
duke@435 566 constantTag tag = cpool->tag_at(index);
duke@435 567 if (tag.is_int()) {
duke@435 568 return ciConstant(T_INT, (jint)cpool->int_at(index));
duke@435 569 } else if (tag.is_long()) {
duke@435 570 return ciConstant((jlong)cpool->long_at(index));
duke@435 571 } else if (tag.is_float()) {
duke@435 572 return ciConstant((jfloat)cpool->float_at(index));
duke@435 573 } else if (tag.is_double()) {
duke@435 574 return ciConstant((jdouble)cpool->double_at(index));
coleenp@4037 575 } else if (tag.is_string()) {
jrose@866 576 oop string = NULL;
coleenp@4037 577 assert(cache_index >= 0, "should have a cache index");
jrose@866 578 if (cpool->is_pseudo_string_at(index)) {
coleenp@4037 579 string = cpool->pseudo_string_at(index, cache_index);
jrose@866 580 } else {
coleenp@4037 581 string = cpool->string_at(index, cache_index, THREAD);
jrose@866 582 if (HAS_PENDING_EXCEPTION) {
jrose@866 583 CLEAR_PENDING_EXCEPTION;
jrose@866 584 record_out_of_memory_failure();
jrose@866 585 return ciConstant();
jrose@866 586 }
duke@435 587 }
duke@435 588 ciObject* constant = get_object(string);
duke@435 589 assert (constant->is_instance(), "must be an instance, or not? ");
duke@435 590 return ciConstant(T_OBJECT, constant);
duke@435 591 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
duke@435 592 // 4881222: allow ldc to take a class type
jrose@1957 593 ciKlass* klass = get_klass_by_index_impl(cpool, index, ignore_will_link, accessor);
duke@435 594 if (HAS_PENDING_EXCEPTION) {
duke@435 595 CLEAR_PENDING_EXCEPTION;
duke@435 596 record_out_of_memory_failure();
duke@435 597 return ciConstant();
duke@435 598 }
duke@435 599 assert (klass->is_instance_klass() || klass->is_array_klass(),
duke@435 600 "must be an instance or array klass ");
jrose@1957 601 return ciConstant(T_OBJECT, klass->java_mirror());
jrose@1957 602 } else if (tag.is_method_type()) {
jrose@1957 603 // must execute Java code to link this CP entry into cache[i].f1
coleenp@2497 604 ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index));
jrose@1957 605 ciObject* ciobj = get_unloaded_method_type_constant(signature);
jrose@1957 606 return ciConstant(T_OBJECT, ciobj);
jrose@1957 607 } else if (tag.is_method_handle()) {
jrose@1957 608 // must execute Java code to link this CP entry into cache[i].f1
jrose@1957 609 int ref_kind = cpool->method_handle_ref_kind_at(index);
jrose@1957 610 int callee_index = cpool->method_handle_klass_index_at(index);
jrose@1957 611 ciKlass* callee = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor);
coleenp@2497 612 ciSymbol* name = get_symbol(cpool->method_handle_name_ref_at(index));
coleenp@2497 613 ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index));
jrose@1957 614 ciObject* ciobj = get_unloaded_method_handle_constant(callee, name, signature, ref_kind);
jrose@1957 615 return ciConstant(T_OBJECT, ciobj);
duke@435 616 } else {
duke@435 617 ShouldNotReachHere();
duke@435 618 return ciConstant();
duke@435 619 }
duke@435 620 }
duke@435 621
duke@435 622 // ------------------------------------------------------------------
duke@435 623 // ciEnv::get_constant_by_index
duke@435 624 //
duke@435 625 // Pull a constant out of the constant pool. How appropriate.
duke@435 626 //
duke@435 627 // Implementation note: this query is currently in no way cached.
twisti@1573 628 ciConstant ciEnv::get_constant_by_index(constantPoolHandle cpool,
jrose@1957 629 int pool_index, int cache_index,
twisti@1573 630 ciInstanceKlass* accessor) {
jrose@1957 631 GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);)
duke@435 632 }
duke@435 633
duke@435 634 // ------------------------------------------------------------------
duke@435 635 // ciEnv::get_field_by_index_impl
duke@435 636 //
duke@435 637 // Implementation of get_field_by_index.
duke@435 638 //
duke@435 639 // Implementation note: the results of field lookups are cached
duke@435 640 // in the accessor klass.
duke@435 641 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor,
duke@435 642 int index) {
duke@435 643 ciConstantPoolCache* cache = accessor->field_cache();
duke@435 644 if (cache == NULL) {
duke@435 645 ciField* field = new (arena()) ciField(accessor, index);
duke@435 646 return field;
duke@435 647 } else {
duke@435 648 ciField* field = (ciField*)cache->get(index);
duke@435 649 if (field == NULL) {
duke@435 650 field = new (arena()) ciField(accessor, index);
duke@435 651 cache->insert(index, field);
duke@435 652 }
duke@435 653 return field;
duke@435 654 }
duke@435 655 }
duke@435 656
duke@435 657 // ------------------------------------------------------------------
duke@435 658 // ciEnv::get_field_by_index
duke@435 659 //
duke@435 660 // Get a field by index from a klass's constant pool.
duke@435 661 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor,
duke@435 662 int index) {
duke@435 663 GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);)
duke@435 664 }
duke@435 665
duke@435 666 // ------------------------------------------------------------------
duke@435 667 // ciEnv::lookup_method
duke@435 668 //
duke@435 669 // Perform an appropriate method lookup based on accessor, holder,
duke@435 670 // name, signature, and bytecode.
coleenp@4037 671 Method* ciEnv::lookup_method(InstanceKlass* accessor,
coleenp@4037 672 InstanceKlass* holder,
coleenp@2497 673 Symbol* name,
coleenp@2497 674 Symbol* sig,
duke@435 675 Bytecodes::Code bc) {
duke@435 676 EXCEPTION_CONTEXT;
duke@435 677 KlassHandle h_accessor(THREAD, accessor);
duke@435 678 KlassHandle h_holder(THREAD, holder);
duke@435 679 LinkResolver::check_klass_accessability(h_accessor, h_holder, KILL_COMPILE_ON_FATAL_(NULL));
duke@435 680 methodHandle dest_method;
duke@435 681 switch (bc) {
duke@435 682 case Bytecodes::_invokestatic:
duke@435 683 dest_method =
coleenp@2497 684 LinkResolver::resolve_static_call_or_null(h_holder, name, sig, h_accessor);
duke@435 685 break;
duke@435 686 case Bytecodes::_invokespecial:
duke@435 687 dest_method =
coleenp@2497 688 LinkResolver::resolve_special_call_or_null(h_holder, name, sig, h_accessor);
duke@435 689 break;
duke@435 690 case Bytecodes::_invokeinterface:
duke@435 691 dest_method =
coleenp@2497 692 LinkResolver::linktime_resolve_interface_method_or_null(h_holder, name, sig,
duke@435 693 h_accessor, true);
duke@435 694 break;
duke@435 695 case Bytecodes::_invokevirtual:
duke@435 696 dest_method =
coleenp@2497 697 LinkResolver::linktime_resolve_virtual_method_or_null(h_holder, name, sig,
duke@435 698 h_accessor, true);
duke@435 699 break;
duke@435 700 default: ShouldNotReachHere();
duke@435 701 }
duke@435 702
duke@435 703 return dest_method();
duke@435 704 }
duke@435 705
duke@435 706
duke@435 707 // ------------------------------------------------------------------
duke@435 708 // ciEnv::get_method_by_index_impl
twisti@1573 709 ciMethod* ciEnv::get_method_by_index_impl(constantPoolHandle cpool,
twisti@1573 710 int index, Bytecodes::Code bc,
twisti@1573 711 ciInstanceKlass* accessor) {
twisti@4021 712 if (bc == Bytecodes::_invokedynamic) {
coleenp@4037 713 ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index);
coleenp@4037 714 bool is_resolved = !cpce->is_f1_null();
twisti@4021 715 // FIXME: code generation could allow for null (unlinked) call site
twisti@4021 716 // The call site could be made patchable as follows:
twisti@4021 717 // Load the appendix argument from the constant pool.
twisti@4021 718 // Test the appendix argument and jump to a known deopt routine if it is null.
twisti@4021 719 // Jump through a patchable call site, which is initially a deopt routine.
twisti@4021 720 // Patch the call site to the nmethod entry point of the static compiled lambda form.
twisti@4021 721 // As with other two-component call sites, both values must be independently verified.
duke@435 722
twisti@4021 723 if (is_resolved) {
coleenp@4037 724 // Get the invoker Method* from the constant pool.
coleenp@4037 725 // (The appendix argument, if any, will be noted in the method's signature.)
coleenp@4037 726 Method* adapter = cpce->f1_as_method();
coleenp@4037 727 return get_method(adapter);
twisti@4021 728 }
duke@435 729
twisti@4021 730 // Fake a method that is equivalent to a declared method.
coleenp@4037 731 ciInstanceKlass* holder = get_instance_klass(SystemDictionary::MethodHandle_klass());
twisti@3969 732 ciSymbol* name = ciSymbol::invokeBasic_name();
twisti@3197 733 ciSymbol* signature = get_symbol(cpool->signature_ref_at(index));
twisti@3197 734 return get_unloaded_method(holder, name, signature, accessor);
twisti@4021 735 } else {
twisti@4021 736 const int holder_index = cpool->klass_ref_index_at(index);
twisti@4021 737 bool holder_is_accessible;
twisti@4021 738 ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
twisti@4021 739 ciInstanceKlass* declared_holder = get_instance_klass_for_declared_method_holder(holder);
twisti@4021 740
twisti@4021 741 // Get the method's name and signature.
twisti@4021 742 Symbol* name_sym = cpool->name_ref_at(index);
twisti@4021 743 Symbol* sig_sym = cpool->signature_ref_at(index);
twisti@4021 744
twisti@4021 745 if (cpool->has_preresolution()
twisti@4021 746 || (holder == ciEnv::MethodHandle_klass() &&
coleenp@4037 747 MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) {
twisti@4021 748 // Short-circuit lookups for JSR 292-related call sites.
twisti@4021 749 // That is, do not rely only on name-based lookups, because they may fail
twisti@4021 750 // if the names are not resolvable in the boot class loader (7056328).
twisti@4021 751 switch (bc) {
twisti@4021 752 case Bytecodes::_invokevirtual:
twisti@4021 753 case Bytecodes::_invokeinterface:
twisti@4021 754 case Bytecodes::_invokespecial:
twisti@4021 755 case Bytecodes::_invokestatic:
twisti@4021 756 {
coleenp@4037 757 Method* m = ConstantPool::method_at_if_loaded(cpool, index);
twisti@4021 758 if (m != NULL) {
coleenp@4037 759 return get_method(m);
twisti@4021 760 }
twisti@4021 761 }
twisti@4021 762 break;
twisti@4021 763 }
twisti@4021 764 }
twisti@4021 765
twisti@4021 766 if (holder_is_accessible) { // Our declared holder is loaded.
coleenp@4037 767 InstanceKlass* lookup = declared_holder->get_instanceKlass();
coleenp@4037 768 Method* m = lookup_method(accessor->get_instanceKlass(), lookup, name_sym, sig_sym, bc);
twisti@4021 769 if (m != NULL &&
twisti@4021 770 (bc == Bytecodes::_invokestatic
coleenp@4251 771 ? m->method_holder()->is_not_initialized()
coleenp@4251 772 : !m->method_holder()->is_loaded())) {
twisti@4021 773 m = NULL;
twisti@4021 774 }
minqi@4267 775 #ifdef ASSERT
minqi@4267 776 if (m != NULL && ReplayCompiles && !ciReplay::is_loaded(m)) {
minqi@4267 777 m = NULL;
minqi@4267 778 }
minqi@4267 779 #endif
twisti@4021 780 if (m != NULL) {
twisti@4021 781 // We found the method.
coleenp@4037 782 return get_method(m);
twisti@4021 783 }
twisti@4021 784 }
twisti@4021 785
twisti@4021 786 // Either the declared holder was not loaded, or the method could
twisti@4021 787 // not be found. Create a dummy ciMethod to represent the failed
twisti@4021 788 // lookup.
twisti@4021 789 ciSymbol* name = get_symbol(name_sym);
twisti@4021 790 ciSymbol* signature = get_symbol(sig_sym);
twisti@4021 791 return get_unloaded_method(declared_holder, name, signature, accessor);
twisti@1572 792 }
twisti@1572 793 }
twisti@1572 794
twisti@1572 795
twisti@1572 796 // ------------------------------------------------------------------
duke@435 797 // ciEnv::get_instance_klass_for_declared_method_holder
duke@435 798 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) {
duke@435 799 // For the case of <array>.clone(), the method holder can be a ciArrayKlass
duke@435 800 // instead of a ciInstanceKlass. For that case simply pretend that the
duke@435 801 // declared holder is Object.clone since that's where the call will bottom out.
duke@435 802 // A more correct fix would trickle out through many interfaces in CI,
duke@435 803 // requiring ciInstanceKlass* to become ciKlass* and many more places would
duke@435 804 // require checks to make sure the expected type was found. Given that this
duke@435 805 // only occurs for clone() the more extensive fix seems like overkill so
duke@435 806 // instead we simply smear the array type into Object.
morris@4777 807 guarantee(method_holder != NULL, "no method holder");
duke@435 808 if (method_holder->is_instance_klass()) {
duke@435 809 return method_holder->as_instance_klass();
duke@435 810 } else if (method_holder->is_array_klass()) {
duke@435 811 return current()->Object_klass();
duke@435 812 } else {
duke@435 813 ShouldNotReachHere();
duke@435 814 }
duke@435 815 return NULL;
duke@435 816 }
duke@435 817
duke@435 818
duke@435 819 // ------------------------------------------------------------------
duke@435 820 // ciEnv::get_method_by_index
twisti@1573 821 ciMethod* ciEnv::get_method_by_index(constantPoolHandle cpool,
twisti@1573 822 int index, Bytecodes::Code bc,
twisti@1573 823 ciInstanceKlass* accessor) {
twisti@4021 824 GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);)
duke@435 825 }
duke@435 826
twisti@1572 827
duke@435 828 // ------------------------------------------------------------------
duke@435 829 // ciEnv::name_buffer
duke@435 830 char *ciEnv::name_buffer(int req_len) {
duke@435 831 if (_name_buffer_len < req_len) {
duke@435 832 if (_name_buffer == NULL) {
duke@435 833 _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len);
duke@435 834 _name_buffer_len = req_len;
duke@435 835 } else {
duke@435 836 _name_buffer =
duke@435 837 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
duke@435 838 _name_buffer_len = req_len;
duke@435 839 }
duke@435 840 }
duke@435 841 return _name_buffer;
duke@435 842 }
duke@435 843
duke@435 844 // ------------------------------------------------------------------
duke@435 845 // ciEnv::is_in_vm
duke@435 846 bool ciEnv::is_in_vm() {
duke@435 847 return JavaThread::current()->thread_state() == _thread_in_vm;
duke@435 848 }
duke@435 849
duke@435 850 bool ciEnv::system_dictionary_modification_counter_changed() {
duke@435 851 return _system_dictionary_modification_counter != SystemDictionary::number_of_modifications();
duke@435 852 }
duke@435 853
duke@435 854 // ------------------------------------------------------------------
twisti@3094 855 // ciEnv::validate_compile_task_dependencies
twisti@3094 856 //
twisti@3094 857 // Check for changes during compilation (e.g. class loads, evolution,
twisti@3094 858 // breakpoints, call site invalidation).
twisti@3094 859 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
duke@435 860 if (failing()) return; // no need for further checks
duke@435 861
twisti@3094 862 // First, check non-klass dependencies as we might return early and
twisti@3094 863 // not check klass dependencies if the system dictionary
twisti@3094 864 // modification counter hasn't changed (see below).
twisti@3094 865 for (Dependencies::DepStream deps(dependencies()); deps.next(); ) {
twisti@3094 866 if (deps.is_klass_type()) continue; // skip klass dependencies
coleenp@4037 867 Klass* witness = deps.check_dependency();
twisti@3094 868 if (witness != NULL) {
twisti@3094 869 record_failure("invalid non-klass dependency");
twisti@3094 870 return;
twisti@3094 871 }
twisti@3094 872 }
duke@435 873
twisti@3094 874 // Klass dependencies must be checked when the system dictionary
twisti@3094 875 // changes. If logging is enabled all violated dependences will be
twisti@3094 876 // recorded in the log. In debug mode check dependencies even if
twisti@3094 877 // the system dictionary hasn't changed to verify that no invalid
twisti@3094 878 // dependencies were inserted. Any violated dependences in this
twisti@3094 879 // case are dumped to the tty.
duke@435 880 bool counter_changed = system_dictionary_modification_counter_changed();
duke@435 881
never@3155 882 bool verify_deps = trueInDebug;
never@3155 883 if (!counter_changed && !verify_deps) return;
never@3155 884
twisti@3094 885 int klass_violations = 0;
duke@435 886 for (Dependencies::DepStream deps(dependencies()); deps.next(); ) {
twisti@3094 887 if (!deps.is_klass_type()) continue; // skip non-klass dependencies
coleenp@4037 888 Klass* witness = deps.check_dependency();
duke@435 889 if (witness != NULL) {
twisti@3094 890 klass_violations++;
never@3155 891 if (!counter_changed) {
never@3155 892 // Dependence failed but counter didn't change. Log a message
never@3155 893 // describing what failed and allow the assert at the end to
never@3155 894 // trigger.
never@3155 895 deps.print_dependency(witness);
never@3155 896 } else if (xtty == NULL) {
never@3155 897 // If we're not logging then a single violation is sufficient,
never@3155 898 // otherwise we want to log all the dependences which were
never@3155 899 // violated.
never@3155 900 break;
never@3155 901 }
duke@435 902 }
duke@435 903 }
duke@435 904
twisti@3094 905 if (klass_violations != 0) {
never@3155 906 #ifdef ASSERT
never@3155 907 if (!counter_changed && !PrintCompilation) {
never@3155 908 // Print out the compile task that failed
never@3155 909 _task->print_line();
never@3155 910 }
never@3155 911 #endif
duke@435 912 assert(counter_changed, "failed dependencies, but counter didn't change");
duke@435 913 record_failure("concurrent class loading");
duke@435 914 }
duke@435 915 }
duke@435 916
duke@435 917 // ------------------------------------------------------------------
duke@435 918 // ciEnv::register_method
duke@435 919 void ciEnv::register_method(ciMethod* target,
duke@435 920 int entry_bci,
duke@435 921 CodeOffsets* offsets,
duke@435 922 int orig_pc_offset,
duke@435 923 CodeBuffer* code_buffer,
duke@435 924 int frame_words,
duke@435 925 OopMapSet* oop_map_set,
duke@435 926 ExceptionHandlerTable* handler_table,
duke@435 927 ImplicitExceptionTable* inc_table,
duke@435 928 AbstractCompiler* compiler,
duke@435 929 int comp_level,
kvn@4103 930 bool has_unsafe_access,
kvn@6429 931 bool has_wide_vectors,
kvn@6429 932 RTMState rtm_state) {
duke@435 933 VM_ENTRY_MARK;
duke@435 934 nmethod* nm = NULL;
duke@435 935 {
duke@435 936 // To prevent compile queue updates.
duke@435 937 MutexLocker locker(MethodCompileQueue_lock, THREAD);
duke@435 938
duke@435 939 // Prevent SystemDictionary::add_to_hierarchy from running
duke@435 940 // and invalidating our dependencies until we install this method.
vlivanov@6096 941 // No safepoints are allowed. Otherwise, class redefinition can occur in between.
duke@435 942 MutexLocker ml(Compile_lock);
vlivanov@6096 943 No_Safepoint_Verifier nsv;
duke@435 944
kvn@1215 945 // Change in Jvmti state may invalidate compilation.
kvn@1215 946 if (!failing() &&
kvn@1215 947 ( (!jvmti_can_hotswap_or_post_breakpoint() &&
kvn@1215 948 JvmtiExport::can_hotswap_or_post_breakpoint()) ||
kvn@1215 949 (!jvmti_can_access_local_variables() &&
kvn@1215 950 JvmtiExport::can_access_local_variables()) ||
dcubed@1648 951 (!jvmti_can_post_on_exceptions() &&
dcubed@1648 952 JvmtiExport::can_post_on_exceptions()) )) {
kvn@1215 953 record_failure("Jvmti state change invalidated dependencies");
duke@435 954 }
duke@435 955
kvn@1215 956 // Change in DTrace flags may invalidate compilation.
kvn@1215 957 if (!failing() &&
kvn@1215 958 ( (!dtrace_extended_probes() && ExtendedDTraceProbes) ||
kvn@1215 959 (!dtrace_method_probes() && DTraceMethodProbes) ||
kvn@1215 960 (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
kvn@1215 961 record_failure("DTrace flags change invalidated dependencies");
kvn@1215 962 }
duke@435 963
kvn@1215 964 if (!failing()) {
kvn@1215 965 if (log() != NULL) {
kvn@1215 966 // Log the dependencies which this compilation declares.
kvn@1215 967 dependencies()->log_all_dependencies();
kvn@1215 968 }
kvn@1215 969
kvn@1215 970 // Encode the dependencies now, so we can check them right away.
kvn@1215 971 dependencies()->encode_content_bytes();
kvn@1215 972
twisti@3094 973 // Check for {class loads, evolution, breakpoints, ...} during compilation
twisti@3094 974 validate_compile_task_dependencies(target);
kvn@1215 975 }
duke@435 976
coleenp@4037 977 methodHandle method(THREAD, target->get_Method());
duke@435 978
kvn@6429 979 #if INCLUDE_RTM_OPT
kvn@6429 980 if (!failing() && (rtm_state != NoRTM) &&
kvn@6429 981 (method()->method_data() != NULL) &&
kvn@6429 982 (method()->method_data()->rtm_state() != rtm_state)) {
kvn@6429 983 // Preemptive decompile if rtm state was changed.
kvn@6429 984 record_failure("RTM state change invalidated rtm code");
kvn@6429 985 }
kvn@6429 986 #endif
kvn@6429 987
duke@435 988 if (failing()) {
duke@435 989 // While not a true deoptimization, it is a preemptive decompile.
coleenp@4037 990 MethodData* mdo = method()->method_data();
duke@435 991 if (mdo != NULL) {
duke@435 992 mdo->inc_decompile_count();
duke@435 993 }
duke@435 994
duke@435 995 // All buffers in the CodeBuffer are allocated in the CodeCache.
duke@435 996 // If the code buffer is created on each compile attempt
duke@435 997 // as in C2, then it must be freed.
duke@435 998 code_buffer->free_blob();
duke@435 999 return;
duke@435 1000 }
duke@435 1001
duke@435 1002 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
duke@435 1003 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
duke@435 1004
duke@435 1005 nm = nmethod::new_nmethod(method,
duke@435 1006 compile_id(),
duke@435 1007 entry_bci,
duke@435 1008 offsets,
duke@435 1009 orig_pc_offset,
duke@435 1010 debug_info(), dependencies(), code_buffer,
duke@435 1011 frame_words, oop_map_set,
duke@435 1012 handler_table, inc_table,
duke@435 1013 compiler, comp_level);
duke@435 1014 // Free codeBlobs
duke@435 1015 code_buffer->free_blob();
duke@435 1016
anoll@6114 1017 if (nm != NULL) {
duke@435 1018 nm->set_has_unsafe_access(has_unsafe_access);
kvn@4103 1019 nm->set_has_wide_vectors(has_wide_vectors);
kvn@6431 1020 #if INCLUDE_RTM_OPT
kvn@6431 1021 nm->set_rtm_state(rtm_state);
kvn@6431 1022 #endif
duke@435 1023
duke@435 1024 // Record successful registration.
duke@435 1025 // (Put nm into the task handle *before* publishing to the Java heap.)
anoll@6114 1026 if (task() != NULL) {
anoll@6114 1027 task()->set_code(nm);
anoll@6114 1028 }
duke@435 1029
duke@435 1030 if (entry_bci == InvocationEntryBci) {
iveresov@2138 1031 if (TieredCompilation) {
iveresov@2138 1032 // If there is an old version we're done with it
iveresov@2138 1033 nmethod* old = method->code();
iveresov@2138 1034 if (TraceMethodReplacement && old != NULL) {
iveresov@2138 1035 ResourceMark rm;
iveresov@2138 1036 char *method_name = method->name_and_sig_as_C_string();
iveresov@2138 1037 tty->print_cr("Replacing method %s", method_name);
iveresov@2138 1038 }
vlivanov@6096 1039 if (old != NULL) {
iveresov@2138 1040 old->make_not_entrant();
iveresov@2138 1041 }
duke@435 1042 }
vlivanov@6096 1043 if (TraceNMethodInstalls) {
duke@435 1044 ResourceMark rm;
duke@435 1045 char *method_name = method->name_and_sig_as_C_string();
duke@435 1046 ttyLocker ttyl;
duke@435 1047 tty->print_cr("Installing method (%d) %s ",
duke@435 1048 comp_level,
duke@435 1049 method_name);
duke@435 1050 }
duke@435 1051 // Allow the code to be executed
duke@435 1052 method->set_code(method, nm);
duke@435 1053 } else {
vlivanov@6096 1054 if (TraceNMethodInstalls) {
duke@435 1055 ResourceMark rm;
duke@435 1056 char *method_name = method->name_and_sig_as_C_string();
duke@435 1057 ttyLocker ttyl;
duke@435 1058 tty->print_cr("Installing osr method (%d) %s @ %d",
duke@435 1059 comp_level,
duke@435 1060 method_name,
duke@435 1061 entry_bci);
duke@435 1062 }
coleenp@4251 1063 method->method_holder()->add_osr_nmethod(nm);
duke@435 1064 }
duke@435 1065 }
anoll@6114 1066 } // safepoints are allowed again
anoll@6114 1067
anoll@6114 1068 if (nm != NULL) {
anoll@6114 1069 // JVMTI -- compiled method notification (must be done outside lock)
anoll@6114 1070 nm->post_compiled_method_load_event();
anoll@6114 1071 } else {
anoll@6114 1072 // The CodeCache is full. Print out warning and disable compilation.
anoll@6114 1073 record_failure("code cache is full");
anoll@6114 1074 CompileBroker::handle_full_code_cache();
duke@435 1075 }
duke@435 1076 }
duke@435 1077
duke@435 1078
duke@435 1079 // ------------------------------------------------------------------
duke@435 1080 // ciEnv::find_system_klass
duke@435 1081 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
duke@435 1082 VM_ENTRY_MARK;
jrose@2982 1083 return get_klass_by_name_impl(NULL, constantPoolHandle(), klass_name, false);
duke@435 1084 }
duke@435 1085
duke@435 1086 // ------------------------------------------------------------------
duke@435 1087 // ciEnv::comp_level
duke@435 1088 int ciEnv::comp_level() {
iveresov@2138 1089 if (task() == NULL) return CompLevel_highest_tier;
duke@435 1090 return task()->comp_level();
duke@435 1091 }
duke@435 1092
duke@435 1093 // ------------------------------------------------------------------
duke@435 1094 // ciEnv::compile_id
duke@435 1095 uint ciEnv::compile_id() {
duke@435 1096 if (task() == NULL) return 0;
duke@435 1097 return task()->compile_id();
duke@435 1098 }
duke@435 1099
duke@435 1100 // ------------------------------------------------------------------
duke@435 1101 // ciEnv::notice_inlined_method()
duke@435 1102 void ciEnv::notice_inlined_method(ciMethod* method) {
twisti@3969 1103 _num_inlined_bytecodes += method->code_size_for_inlining();
duke@435 1104 }
duke@435 1105
duke@435 1106 // ------------------------------------------------------------------
duke@435 1107 // ciEnv::num_inlined_bytecodes()
duke@435 1108 int ciEnv::num_inlined_bytecodes() const {
duke@435 1109 return _num_inlined_bytecodes;
duke@435 1110 }
duke@435 1111
duke@435 1112 // ------------------------------------------------------------------
duke@435 1113 // ciEnv::record_failure()
duke@435 1114 void ciEnv::record_failure(const char* reason) {
duke@435 1115 if (log() != NULL) {
duke@435 1116 log()->elem("failure reason='%s'", reason);
duke@435 1117 }
duke@435 1118 if (_failure_reason == NULL) {
duke@435 1119 // Record the first failure reason.
duke@435 1120 _failure_reason = reason;
duke@435 1121 }
duke@435 1122 }
duke@435 1123
duke@435 1124 // ------------------------------------------------------------------
duke@435 1125 // ciEnv::record_method_not_compilable()
duke@435 1126 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) {
duke@435 1127 int new_compilable =
duke@435 1128 all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ;
duke@435 1129
duke@435 1130 // Only note transitions to a worse state
duke@435 1131 if (new_compilable > _compilable) {
duke@435 1132 if (log() != NULL) {
duke@435 1133 if (all_tiers) {
duke@435 1134 log()->elem("method_not_compilable");
duke@435 1135 } else {
vlivanov@4154 1136 log()->elem("method_not_compilable_at_tier level='%d'",
vlivanov@4154 1137 current()->task()->comp_level());
duke@435 1138 }
duke@435 1139 }
duke@435 1140 _compilable = new_compilable;
duke@435 1141
duke@435 1142 // Reset failure reason; this one is more important.
duke@435 1143 _failure_reason = NULL;
duke@435 1144 record_failure(reason);
duke@435 1145 }
duke@435 1146 }
duke@435 1147
duke@435 1148 // ------------------------------------------------------------------
duke@435 1149 // ciEnv::record_out_of_memory_failure()
duke@435 1150 void ciEnv::record_out_of_memory_failure() {
duke@435 1151 // If memory is low, we stop compiling methods.
duke@435 1152 record_method_not_compilable("out of memory");
duke@435 1153 }
minqi@4267 1154
roland@5628 1155 ciInstance* ciEnv::unloaded_ciinstance() {
roland@5628 1156 GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();)
roland@5628 1157 }
roland@5628 1158
vlivanov@5904 1159 // ------------------------------------------------------------------
vlivanov@5904 1160 // ciEnv::dump_replay_data*
vlivanov@5904 1161
vlivanov@5904 1162 // Don't change thread state and acquire any locks.
vlivanov@5904 1163 // Safe to call from VM error reporter.
kvn@6217 1164
kvn@6217 1165 void ciEnv::dump_compile_data(outputStream* out) {
kvn@6217 1166 CompileTask* task = this->task();
kvn@6217 1167 Method* method = task->method();
kvn@6217 1168 int entry_bci = task->osr_bci();
kvn@6217 1169 int comp_level = task->comp_level();
kvn@6217 1170 out->print("compile %s %s %s %d %d",
kvn@6217 1171 method->klass_name()->as_quoted_ascii(),
kvn@6217 1172 method->name()->as_quoted_ascii(),
kvn@6217 1173 method->signature()->as_quoted_ascii(),
kvn@6217 1174 entry_bci, comp_level);
kvn@6217 1175 if (compiler_data() != NULL) {
kvn@6217 1176 if (is_c2_compile(comp_level)) { // C2 or Shark
kvn@6217 1177 #ifdef COMPILER2
kvn@6217 1178 // Dump C2 inlining data.
kvn@6217 1179 ((Compile*)compiler_data())->dump_inline_data(out);
kvn@6217 1180 #endif
kvn@6217 1181 } else if (is_c1_compile(comp_level)) { // C1
kvn@6217 1182 #ifdef COMPILER1
kvn@6217 1183 // Dump C1 inlining data.
kvn@6217 1184 ((Compilation*)compiler_data())->dump_inline_data(out);
kvn@6217 1185 #endif
kvn@6217 1186 }
kvn@6217 1187 }
kvn@6217 1188 out->cr();
kvn@6217 1189 }
kvn@6217 1190
vlivanov@5904 1191 void ciEnv::dump_replay_data_unsafe(outputStream* out) {
vlivanov@4531 1192 ResourceMark rm;
minqi@4267 1193 #if INCLUDE_JVMTI
minqi@4267 1194 out->print_cr("JvmtiExport can_access_local_variables %d", _jvmti_can_access_local_variables);
minqi@4267 1195 out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint);
minqi@4267 1196 out->print_cr("JvmtiExport can_post_on_exceptions %d", _jvmti_can_post_on_exceptions);
minqi@4267 1197 #endif // INCLUDE_JVMTI
minqi@4267 1198
minqi@4267 1199 GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata();
minqi@4267 1200 out->print_cr("# %d ciObject found", objects->length());
minqi@4267 1201 for (int i = 0; i < objects->length(); i++) {
minqi@4267 1202 objects->at(i)->dump_replay_data(out);
minqi@4267 1203 }
kvn@6217 1204 dump_compile_data(out);
minqi@4267 1205 out->flush();
minqi@4267 1206 }
vlivanov@5904 1207
vlivanov@5904 1208 void ciEnv::dump_replay_data(outputStream* out) {
vlivanov@5904 1209 GUARDED_VM_ENTRY(
vlivanov@5904 1210 MutexLocker ml(Compile_lock);
vlivanov@5904 1211 dump_replay_data_unsafe(out);
vlivanov@5904 1212 )
vlivanov@5904 1213 }
kvn@6217 1214
kvn@6217 1215 void ciEnv::dump_replay_data(int compile_id) {
kvn@6217 1216 static char buffer[O_BUFLEN];
kvn@6217 1217 int ret = jio_snprintf(buffer, O_BUFLEN, "replay_pid%p_compid%d.log", os::current_process_id(), compile_id);
kvn@6217 1218 if (ret > 0) {
kvn@6217 1219 int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
kvn@6217 1220 if (fd != -1) {
kvn@6217 1221 FILE* replay_data_file = os::open(fd, "w");
kvn@6217 1222 if (replay_data_file != NULL) {
kvn@6217 1223 fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
kvn@6217 1224 dump_replay_data(&replay_data_stream);
drchase@6680 1225 tty->print_cr("# Compiler replay data is saved as: %s", buffer);
kvn@6217 1226 } else {
kvn@6217 1227 tty->print_cr("# Can't open file to dump replay data.");
kvn@6217 1228 }
kvn@6217 1229 }
kvn@6217 1230 }
kvn@6217 1231 }
kvn@6217 1232
kvn@6217 1233 void ciEnv::dump_inline_data(int compile_id) {
kvn@6217 1234 static char buffer[O_BUFLEN];
kvn@6217 1235 int ret = jio_snprintf(buffer, O_BUFLEN, "inline_pid%p_compid%d.log", os::current_process_id(), compile_id);
kvn@6217 1236 if (ret > 0) {
kvn@6217 1237 int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
kvn@6217 1238 if (fd != -1) {
kvn@6217 1239 FILE* inline_data_file = os::open(fd, "w");
kvn@6217 1240 if (inline_data_file != NULL) {
kvn@6217 1241 fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
kvn@6217 1242 GUARDED_VM_ENTRY(
kvn@6217 1243 MutexLocker ml(Compile_lock);
kvn@6217 1244 dump_compile_data(&replay_data_stream);
kvn@6217 1245 )
kvn@6217 1246 replay_data_stream.flush();
kvn@6217 1247 tty->print("# Compiler inline data is saved as: ");
drchase@6680 1248 tty->print_cr("%s", buffer);
kvn@6217 1249 } else {
kvn@6217 1250 tty->print_cr("# Can't open file to dump inline data.");
kvn@6217 1251 }
kvn@6217 1252 }
kvn@6217 1253 }
kvn@6217 1254 }

mercurial