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