1 /*
   2  * Copyright (c) 1999, 2018, 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.hpp"
  36 #include "ci/ciValueKlass.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/vmSymbols.hpp"
  39 #include "code/codeCache.hpp"
  40 #include "code/scopeDesc.hpp"
  41 #include "compiler/compileBroker.hpp"
  42 #include "compiler/compileLog.hpp"
  43 #include "compiler/disassembler.hpp"
  44 #include "gc/shared/collectedHeap.inline.hpp"
  45 #include "interpreter/linkResolver.hpp"
  46 #include "memory/allocation.inline.hpp"
  47 #include "memory/oopFactory.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "memory/universe.inline.hpp"
  50 #include "oops/methodData.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.inline.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "prims/jvmtiExport.hpp"
  55 #include "runtime/init.hpp"
  56 #include "runtime/reflection.hpp"
  57 #include "runtime/sharedRuntime.hpp"
  58 #include "runtime/thread.inline.hpp"
  59 #include "trace/tracing.hpp"
  60 #include "utilities/dtrace.hpp"
  61 #include "utilities/macros.hpp"
  62 #ifdef COMPILER1
  63 #include "c1/c1_Runtime1.hpp"
  64 #endif
  65 #ifdef COMPILER2
  66 #include "opto/runtime.hpp"
  67 #endif
  68 
  69 // ciEnv
  70 //
  71 // This class is the top level broker for requests from the compiler
  72 // to the VM.
  73 
  74 ciObject*              ciEnv::_null_object_instance;
  75 
  76 #define WK_KLASS_DEFN(name, ignore_s, ignore_o) ciInstanceKlass* ciEnv::_##name = NULL;
  77 WK_KLASSES_DO(WK_KLASS_DEFN)
  78 #undef WK_KLASS_DEFN
  79 
  80 ciSymbol*        ciEnv::_unloaded_cisymbol = NULL;
  81 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = NULL;
  82 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = NULL;
  83 
  84 jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = NULL;
  85 jobject ciEnv::_ArrayStoreException_handle = NULL;
  86 jobject ciEnv::_ClassCastException_handle = NULL;
  87 
  88 #ifndef PRODUCT
  89 static bool firstEnv = true;
  90 #endif /* PRODUCT */
  91 
  92 // ------------------------------------------------------------------
  93 // ciEnv::ciEnv
  94 ciEnv::ciEnv(CompileTask* task, int system_dictionary_modification_counter)
  95   : _ciEnv_arena(mtCompiler) {
  96   VM_ENTRY_MARK;
  97 
  98   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
  99   thread->set_env(this);
 100   assert(ciEnv::current() == this, "sanity");
 101 
 102   _oop_recorder = NULL;
 103   _debug_info = NULL;
 104   _dependencies = NULL;
 105   _failure_reason = NULL;
 106   _inc_decompile_count_on_failure = true;
 107   _compilable = MethodCompilable;
 108   _break_at_compile = false;
 109   _compiler_data = NULL;
 110 #ifndef PRODUCT
 111   assert(!firstEnv, "not initialized properly");
 112 #endif /* !PRODUCT */
 113 
 114   _system_dictionary_modification_counter = system_dictionary_modification_counter;
 115   _num_inlined_bytecodes = 0;
 116   assert(task == NULL || thread->task() == task, "sanity");
 117   _task = task;
 118   _log = NULL;
 119 
 120   // Temporary buffer for creating symbols and such.
 121   _name_buffer = NULL;
 122   _name_buffer_len = 0;
 123 
 124   _arena   = &_ciEnv_arena;
 125   _factory = new (_arena) ciObjectFactory(_arena, 128);
 126 
 127   // Preload commonly referenced system ciObjects.
 128 
 129   // During VM initialization, these instances have not yet been created.
 130   // Assertions ensure that these instances are not accessed before
 131   // their initialization.
 132 
 133   assert(Universe::is_fully_initialized(), "should be complete");
 134 
 135   oop o = Universe::null_ptr_exception_instance();
 136   assert(o != NULL, "should have been initialized");
 137   _NullPointerException_instance = get_object(o)->as_instance();
 138   o = Universe::arithmetic_exception_instance();
 139   assert(o != NULL, "should have been initialized");
 140   _ArithmeticException_instance = get_object(o)->as_instance();
 141 
 142   _ArrayIndexOutOfBoundsException_instance = NULL;
 143   _ArrayStoreException_instance = NULL;
 144   _ClassCastException_instance = NULL;
 145   _the_null_string = NULL;
 146   _the_min_jint_string = NULL;
 147 
 148   _jvmti_can_hotswap_or_post_breakpoint = false;
 149   _jvmti_can_access_local_variables = false;
 150   _jvmti_can_post_on_exceptions = false;
 151   _jvmti_can_pop_frame = false;
 152 }
 153 
 154 ciEnv::ciEnv(Arena* arena) : _ciEnv_arena(mtCompiler) {
 155   ASSERT_IN_VM;
 156 
 157   // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
 158   CompilerThread* current_thread = CompilerThread::current();
 159   assert(current_thread->env() == NULL, "must be");
 160   current_thread->set_env(this);
 161   assert(ciEnv::current() == this, "sanity");
 162 
 163   _oop_recorder = NULL;
 164   _debug_info = NULL;
 165   _dependencies = NULL;
 166   _failure_reason = NULL;
 167   _inc_decompile_count_on_failure = true;
 168   _compilable = MethodCompilable_never;
 169   _break_at_compile = false;
 170   _compiler_data = NULL;
 171 #ifndef PRODUCT
 172   assert(firstEnv, "must be first");
 173   firstEnv = false;
 174 #endif /* !PRODUCT */
 175 
 176   _system_dictionary_modification_counter = 0;
 177   _num_inlined_bytecodes = 0;
 178   _task = NULL;
 179   _log = NULL;
 180 
 181   // Temporary buffer for creating symbols and such.
 182   _name_buffer = NULL;
 183   _name_buffer_len = 0;
 184 
 185   _arena   = arena;
 186   _factory = new (_arena) ciObjectFactory(_arena, 128);
 187 
 188   // Preload commonly referenced system ciObjects.
 189 
 190   // During VM initialization, these instances have not yet been created.
 191   // Assertions ensure that these instances are not accessed before
 192   // their initialization.
 193 
 194   assert(Universe::is_fully_initialized(), "must be");
 195 
 196   _NullPointerException_instance = NULL;
 197   _ArithmeticException_instance = NULL;
 198   _ArrayIndexOutOfBoundsException_instance = NULL;
 199   _ArrayStoreException_instance = NULL;
 200   _ClassCastException_instance = NULL;
 201   _the_null_string = NULL;
 202   _the_min_jint_string = NULL;
 203 
 204   _jvmti_can_hotswap_or_post_breakpoint = false;
 205   _jvmti_can_access_local_variables = false;
 206   _jvmti_can_post_on_exceptions = false;
 207   _jvmti_can_pop_frame = false;
 208 }
 209 
 210 ciEnv::~ciEnv() {
 211   GUARDED_VM_ENTRY(
 212       CompilerThread* current_thread = CompilerThread::current();
 213       _factory->remove_symbols();
 214       // Need safepoint to clear the env on the thread.  RedefineClasses might
 215       // be reading it.
 216       current_thread->set_env(NULL);
 217   )
 218 }
 219 
 220 // ------------------------------------------------------------------
 221 // Cache Jvmti state
 222 void ciEnv::cache_jvmti_state() {
 223   VM_ENTRY_MARK;
 224   // Get Jvmti capabilities under lock to get consistant values.
 225   MutexLocker mu(JvmtiThreadState_lock);
 226   _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint();
 227   _jvmti_can_access_local_variables     = JvmtiExport::can_access_local_variables();
 228   _jvmti_can_post_on_exceptions         = JvmtiExport::can_post_on_exceptions();
 229   _jvmti_can_pop_frame                  = JvmtiExport::can_pop_frame();
 230 }
 231 
 232 bool ciEnv::should_retain_local_variables() const {
 233   return _jvmti_can_access_local_variables || _jvmti_can_pop_frame;
 234 }
 235 
 236 bool ciEnv::jvmti_state_changed() const {
 237   if (!_jvmti_can_access_local_variables &&
 238       JvmtiExport::can_access_local_variables()) {
 239     return true;
 240   }
 241   if (!_jvmti_can_hotswap_or_post_breakpoint &&
 242       JvmtiExport::can_hotswap_or_post_breakpoint()) {
 243     return true;
 244   }
 245   if (!_jvmti_can_post_on_exceptions &&
 246       JvmtiExport::can_post_on_exceptions()) {
 247     return true;
 248   }
 249   if (!_jvmti_can_pop_frame &&
 250       JvmtiExport::can_pop_frame()) {
 251     return true;
 252   }
 253   return false;
 254 }
 255 
 256 // ------------------------------------------------------------------
 257 // Cache DTrace flags
 258 void ciEnv::cache_dtrace_flags() {
 259   // Need lock?
 260   _dtrace_extended_probes = ExtendedDTraceProbes;
 261   if (_dtrace_extended_probes) {
 262     _dtrace_monitor_probes  = true;
 263     _dtrace_method_probes   = true;
 264     _dtrace_alloc_probes    = true;
 265   } else {
 266     _dtrace_monitor_probes  = DTraceMonitorProbes;
 267     _dtrace_method_probes   = DTraceMethodProbes;
 268     _dtrace_alloc_probes    = DTraceAllocProbes;
 269   }
 270 }
 271 
 272 // ------------------------------------------------------------------
 273 // helper for lazy exception creation
 274 ciInstance* ciEnv::get_or_create_exception(jobject& handle, Symbol* name) {
 275   VM_ENTRY_MARK;
 276   if (handle == NULL) {
 277     // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance.
 278     Klass* k = SystemDictionary::find(name, Handle(), Handle(), THREAD);
 279     jobject objh = NULL;
 280     if (!HAS_PENDING_EXCEPTION && k != NULL) {
 281       oop obj = InstanceKlass::cast(k)->allocate_instance(THREAD);
 282       if (!HAS_PENDING_EXCEPTION)
 283         objh = JNIHandles::make_global(Handle(THREAD, obj));
 284     }
 285     if (HAS_PENDING_EXCEPTION) {
 286       CLEAR_PENDING_EXCEPTION;
 287     } else {
 288       handle = objh;
 289     }
 290   }
 291   oop obj = JNIHandles::resolve(handle);
 292   return obj == NULL? NULL: get_object(obj)->as_instance();
 293 }
 294 
 295 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
 296   if (_ArrayIndexOutOfBoundsException_instance == NULL) {
 297     _ArrayIndexOutOfBoundsException_instance
 298           = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle,
 299           vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 300   }
 301   return _ArrayIndexOutOfBoundsException_instance;
 302 }
 303 ciInstance* ciEnv::ArrayStoreException_instance() {
 304   if (_ArrayStoreException_instance == NULL) {
 305     _ArrayStoreException_instance
 306           = get_or_create_exception(_ArrayStoreException_handle,
 307           vmSymbols::java_lang_ArrayStoreException());
 308   }
 309   return _ArrayStoreException_instance;
 310 }
 311 ciInstance* ciEnv::ClassCastException_instance() {
 312   if (_ClassCastException_instance == NULL) {
 313     _ClassCastException_instance
 314           = get_or_create_exception(_ClassCastException_handle,
 315           vmSymbols::java_lang_ClassCastException());
 316   }
 317   return _ClassCastException_instance;
 318 }
 319 
 320 ciInstance* ciEnv::the_null_string() {
 321   if (_the_null_string == NULL) {
 322     VM_ENTRY_MARK;
 323     _the_null_string = get_object(Universe::the_null_string())->as_instance();
 324   }
 325   return _the_null_string;
 326 }
 327 
 328 ciInstance* ciEnv::the_min_jint_string() {
 329   if (_the_min_jint_string == NULL) {
 330     VM_ENTRY_MARK;
 331     _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance();
 332   }
 333   return _the_min_jint_string;
 334 }
 335 
 336 // ------------------------------------------------------------------
 337 // ciEnv::get_method_from_handle
 338 ciMethod* ciEnv::get_method_from_handle(Method* method) {
 339   VM_ENTRY_MARK;
 340   return get_metadata(method)->as_method();
 341 }
 342 
 343 // ------------------------------------------------------------------
 344 // ciEnv::array_element_offset_in_bytes
 345 int ciEnv::array_element_offset_in_bytes(ciArray* a_h, ciObject* o_h) {
 346   VM_ENTRY_MARK;
 347   objArrayOop a = (objArrayOop)a_h->get_oop();
 348   assert(a->is_objArray(), "");
 349   int length = a->length();
 350   oop o = o_h->get_oop();
 351   for (int i = 0; i < length; i++) {
 352     if (a->obj_at(i) == o)  return i;
 353   }
 354   return -1;
 355 }
 356 
 357 
 358 // ------------------------------------------------------------------
 359 // ciEnv::check_klass_accessiblity
 360 //
 361 // Note: the logic of this method should mirror the logic of
 362 // ConstantPool::verify_constant_pool_resolve.
 363 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass,
 364                                       Klass* resolved_klass) {
 365   if (accessing_klass == NULL || !accessing_klass->is_loaded()) {
 366     return true;
 367   }
 368   if (accessing_klass->is_obj_array_klass()) {
 369     accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass();
 370   }
 371   if (!accessing_klass->is_instance_klass()) {
 372     return true;
 373   }
 374 
 375   if (resolved_klass->is_objArray_klass()) {
 376     // Find the element klass, if this is an array.
 377     resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
 378   }
 379   if (resolved_klass->is_instance_klass()) {
 380     return (Reflection::verify_class_access(accessing_klass->get_Klass(),
 381                                             InstanceKlass::cast(resolved_klass),
 382                                             true) == Reflection::ACCESS_OK);
 383   }
 384   return true;
 385 }
 386 
 387 // ------------------------------------------------------------------
 388 // ciEnv::get_klass_by_name_impl
 389 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass,
 390                                        const constantPoolHandle& cpool,
 391                                        ciSymbol* name,
 392                                        bool require_local) {
 393   ASSERT_IN_VM;
 394   EXCEPTION_CONTEXT;
 395 
 396   // Now we need to check the SystemDictionary
 397   Symbol* sym = name->get_symbol();
 398   if (sym->byte_at(0) == 'L' &&
 399     sym->byte_at(sym->utf8_length()-1) == ';') {
 400     // This is a name from a signature.  Strip off the trimmings.
 401     // Call recursive to keep scope of strippedsym.
 402     TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
 403                     sym->utf8_length()-2,
 404                     KILL_COMPILE_ON_FATAL_(_unloaded_ciinstance_klass));
 405     ciSymbol* strippedname = get_symbol(strippedsym);
 406     return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local);
 407   }
 408 
 409   // Check for prior unloaded klass.  The SystemDictionary's answers
 410   // can vary over time but the compiler needs consistency.
 411   ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name);
 412   if (unloaded_klass != NULL) {
 413     if (require_local)  return NULL;
 414     return unloaded_klass;
 415   }
 416 
 417   Handle loader(THREAD, (oop)NULL);
 418   Handle domain(THREAD, (oop)NULL);
 419   if (accessing_klass != NULL) {
 420     loader = Handle(THREAD, accessing_klass->loader());
 421     domain = Handle(THREAD, accessing_klass->protection_domain());
 422   }
 423 
 424   // setup up the proper type to return on OOM
 425   ciKlass* fail_type;
 426   if (sym->byte_at(0) == '[') {
 427     fail_type = _unloaded_ciobjarrayklass;
 428   } else {
 429     fail_type = _unloaded_ciinstance_klass;
 430   }
 431   Klass* found_klass;
 432   {
 433     ttyUnlocker ttyul;  // release tty lock to avoid ordering problems
 434     MutexLocker ml(Compile_lock);
 435     Klass* kls;
 436     if (!require_local) {
 437       kls = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader,
 438                                                                        KILL_COMPILE_ON_FATAL_(fail_type));
 439     } else {
 440       kls = SystemDictionary::find_instance_or_array_klass(sym, loader, domain,
 441                                                            KILL_COMPILE_ON_FATAL_(fail_type));
 442     }
 443     found_klass = kls;
 444   }
 445 
 446   // If we fail to find an array klass, look again for its element type.
 447   // The element type may be available either locally or via constraints.
 448   // In either case, if we can find the element type in the system dictionary,
 449   // we must build an array type around it.  The CI requires array klasses
 450   // to be loaded if their element klasses are loaded, except when memory
 451   // is exhausted.
 452   if (sym->byte_at(0) == '[' &&
 453       (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L')) {
 454     // We have an unloaded array.
 455     // Build it on the fly if the element class exists.
 456     TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
 457                                                  sym->utf8_length()-1,
 458                                                  KILL_COMPILE_ON_FATAL_(fail_type));
 459 
 460     // Get element ciKlass recursively.
 461     ciKlass* elem_klass =
 462       get_klass_by_name_impl(accessing_klass,
 463                              cpool,
 464                              get_symbol(elem_sym),
 465                              require_local);
 466     if (elem_klass != NULL && elem_klass->is_loaded()) {
 467       // Now make an array for it
 468       if (elem_klass->is_valuetype() && elem_klass->as_value_klass()->flatten_array()) {
 469         return ciValueArrayKlass::make_impl(elem_klass);
 470       } else {
 471         return ciObjArrayKlass::make_impl(elem_klass);
 472       }
 473     }
 474   }
 475 
 476   if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) {
 477     // Look inside the constant pool for pre-resolved class entries.
 478     for (int i = cpool->length() - 1; i >= 1; i--) {
 479       if (cpool->tag_at(i).is_klass()) {
 480         Klass* kls = cpool->resolved_klass_at(i);
 481         if (kls->name() == sym) {
 482           found_klass = kls;
 483           break;
 484         }
 485       }
 486     }
 487   }
 488 
 489   if (found_klass != NULL) {
 490     // Found it.  Build a CI handle.
 491     return get_klass(found_klass);
 492   }
 493 
 494   if (require_local)  return NULL;
 495 
 496   // Not yet loaded into the VM, or not governed by loader constraints.
 497   // Make a CI representative for it.
 498   int i = 0;
 499   while (sym->byte_at(i) == '[') {
 500     i++;
 501   }
 502   return get_unloaded_klass(accessing_klass, name);
 503 }
 504 
 505 // ------------------------------------------------------------------
 506 // ciEnv::get_klass_by_name
 507 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass,
 508                                   ciSymbol* klass_name,
 509                                   bool require_local) {
 510   GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass,
 511                                                  constantPoolHandle(),
 512                                                  klass_name,
 513                                                  require_local);)
 514 }
 515 
 516 // ------------------------------------------------------------------
 517 // ciEnv::get_klass_by_index_impl
 518 //
 519 // Implementation of get_klass_by_index.
 520 ciKlass* ciEnv::get_klass_by_index_impl(const constantPoolHandle& cpool,
 521                                         int index,
 522                                         bool& is_accessible,
 523                                         ciInstanceKlass* accessor) {
 524   EXCEPTION_CONTEXT;
 525   Klass* klass = NULL;
 526   Symbol* klass_name = NULL;
 527 
 528   if (cpool->tag_at(index).is_symbol()) {
 529     klass_name = cpool->symbol_at(index);
 530   } else {
 531     // Check if it's resolved if it's not a symbol constant pool entry.
 532     klass =  ConstantPool::klass_at_if_loaded(cpool, index);
 533     // Try to look it up by name.
 534     if (klass == NULL) {
 535       klass_name = cpool->klass_name_at(index);
 536     }
 537   }
 538 
 539   if (klass == NULL) {
 540     // Not found in constant pool.  Use the name to do the lookup.
 541     ciKlass* k = get_klass_by_name_impl(accessor,
 542                                         cpool,
 543                                         get_symbol(klass_name),
 544                                         false);
 545     // Calculate accessibility the hard way.
 546     if (!k->is_loaded()) {
 547       is_accessible = false;
 548     } else if (k->loader() != accessor->loader() &&
 549                get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
 550       // Loaded only remotely.  Not linked yet.
 551       is_accessible = false;
 552     } else {
 553       // Linked locally, and we must also check public/private, etc.
 554       is_accessible = check_klass_accessibility(accessor, k->get_Klass());
 555     }
 556     return k;
 557   }
 558 
 559   // Check for prior unloaded klass.  The SystemDictionary's answers
 560   // can vary over time but the compiler needs consistency.
 561   ciSymbol* name = get_symbol(klass->name());
 562   ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name);
 563   if (unloaded_klass != NULL) {
 564     is_accessible = false;
 565     return unloaded_klass;
 566   }
 567 
 568   // It is known to be accessible, since it was found in the constant pool.
 569   is_accessible = true;
 570   return get_klass(klass);
 571 }
 572 
 573 // ------------------------------------------------------------------
 574 // ciEnv::get_klass_by_index
 575 //
 576 // Get a klass from the constant pool.
 577 ciKlass* ciEnv::get_klass_by_index(const constantPoolHandle& cpool,
 578                                    int index,
 579                                    bool& is_accessible,
 580                                    ciInstanceKlass* accessor) {
 581   GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);)
 582 }
 583 
 584 // ------------------------------------------------------------------
 585 // ciEnv::get_constant_by_index_impl
 586 //
 587 // Implementation of get_constant_by_index().
 588 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 589                                              int pool_index, int cache_index,
 590                                              ciInstanceKlass* accessor) {
 591   bool ignore_will_link;
 592   EXCEPTION_CONTEXT;
 593   int index = pool_index;
 594   if (cache_index >= 0) {
 595     assert(index < 0, "only one kind of index at a time");
 596     index = cpool->object_to_cp_index(cache_index);
 597     oop obj = cpool->resolved_references()->obj_at(cache_index);
 598     if (obj != NULL) {
 599       if (obj == Universe::the_null_sentinel()) {
 600         return ciConstant(T_OBJECT, get_object(NULL));
 601       }
 602       BasicType bt = T_OBJECT;
 603       if (cpool->tag_at(index).is_dynamic_constant())
 604         bt = FieldType::basic_type(cpool->uncached_signature_ref_at(index));
 605       if (is_reference_type(bt)) {
 606       } else {
 607         // we have to unbox the primitive value
 608         if (!is_java_primitive(bt))  return ciConstant();
 609         jvalue value;
 610         BasicType bt2 = java_lang_boxing_object::get_value(obj, &value);
 611         assert(bt2 == bt, "");
 612         switch (bt2) {
 613         case T_DOUBLE:  return ciConstant(value.d);
 614         case T_FLOAT:   return ciConstant(value.f);
 615         case T_LONG:    return ciConstant(value.j);
 616         case T_INT:     return ciConstant(bt2, value.i);
 617         case T_SHORT:   return ciConstant(bt2, value.s);
 618         case T_BYTE:    return ciConstant(bt2, value.b);
 619         case T_CHAR:    return ciConstant(bt2, value.c);
 620         case T_BOOLEAN: return ciConstant(bt2, value.z);
 621         default:  return ciConstant();
 622         }
 623       }
 624       ciObject* ciobj = get_object(obj);
 625       if (ciobj->is_array()) {
 626         return ciConstant(T_ARRAY, ciobj);
 627       } else {
 628         assert(ciobj->is_instance(), "should be an instance");
 629         return ciConstant(T_OBJECT, ciobj);
 630       }
 631     }
 632   }
 633   constantTag tag = cpool->tag_at(index);
 634   if (tag.is_int()) {
 635     return ciConstant(T_INT, (jint)cpool->int_at(index));
 636   } else if (tag.is_long()) {
 637     return ciConstant((jlong)cpool->long_at(index));
 638   } else if (tag.is_float()) {
 639     return ciConstant((jfloat)cpool->float_at(index));
 640   } else if (tag.is_double()) {
 641     return ciConstant((jdouble)cpool->double_at(index));
 642   } else if (tag.is_string()) {
 643     oop string = NULL;
 644     assert(cache_index >= 0, "should have a cache index");
 645     if (cpool->is_pseudo_string_at(index)) {
 646       string = cpool->pseudo_string_at(index, cache_index);
 647     } else {
 648       string = cpool->string_at(index, cache_index, THREAD);
 649       if (HAS_PENDING_EXCEPTION) {
 650         CLEAR_PENDING_EXCEPTION;
 651         record_out_of_memory_failure();
 652         return ciConstant();
 653       }
 654     }
 655     ciObject* constant = get_object(string);
 656     if (constant->is_array()) {
 657       return ciConstant(T_ARRAY, constant);
 658     } else {
 659       assert (constant->is_instance(), "must be an instance, or not? ");
 660       return ciConstant(T_OBJECT, constant);
 661     }
 662   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
 663     // 4881222: allow ldc to take a class type
 664     ciKlass* klass = get_klass_by_index_impl(cpool, index, ignore_will_link, accessor);
 665     if (HAS_PENDING_EXCEPTION) {
 666       CLEAR_PENDING_EXCEPTION;
 667       record_out_of_memory_failure();
 668       return ciConstant();
 669     }
 670     assert (klass->is_instance_klass() || klass->is_array_klass(),
 671             "must be an instance or array klass ");
 672     return ciConstant(T_OBJECT, klass->java_mirror());
 673   } else if (tag.is_method_type()) {
 674     // must execute Java code to link this CP entry into cache[i].f1
 675     ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index));
 676     ciObject* ciobj = get_unloaded_method_type_constant(signature);
 677     return ciConstant(T_OBJECT, ciobj);
 678   } else if (tag.is_method_handle()) {
 679     // must execute Java code to link this CP entry into cache[i].f1
 680     int ref_kind        = cpool->method_handle_ref_kind_at(index);
 681     int callee_index    = cpool->method_handle_klass_index_at(index);
 682     ciKlass* callee     = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor);
 683     ciSymbol* name      = get_symbol(cpool->method_handle_name_ref_at(index));
 684     ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index));
 685     ciObject* ciobj     = get_unloaded_method_handle_constant(callee, name, signature, ref_kind);
 686     return ciConstant(T_OBJECT, ciobj);
 687   } else if (tag.is_dynamic_constant()) {
 688     return ciConstant();
 689   } else {
 690     ShouldNotReachHere();
 691     return ciConstant();
 692   }
 693 }
 694 
 695 // ------------------------------------------------------------------
 696 // ciEnv::get_constant_by_index
 697 //
 698 // Pull a constant out of the constant pool.  How appropriate.
 699 //
 700 // Implementation note: this query is currently in no way cached.
 701 ciConstant ciEnv::get_constant_by_index(const constantPoolHandle& cpool,
 702                                         int pool_index, int cache_index,
 703                                         ciInstanceKlass* accessor) {
 704   GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);)
 705 }
 706 
 707 // ------------------------------------------------------------------
 708 // ciEnv::get_field_by_index_impl
 709 //
 710 // Implementation of get_field_by_index.
 711 //
 712 // Implementation note: the results of field lookups are cached
 713 // in the accessor klass.
 714 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor,
 715                                         int index) {
 716   ciConstantPoolCache* cache = accessor->field_cache();
 717   if (cache == NULL) {
 718     ciField* field = new (arena()) ciField(accessor, index);
 719     return field;
 720   } else {
 721     ciField* field = (ciField*)cache->get(index);
 722     if (field == NULL) {
 723       field = new (arena()) ciField(accessor, index);
 724       cache->insert(index, field);
 725     }
 726     return field;
 727   }
 728 }
 729 
 730 // ------------------------------------------------------------------
 731 // ciEnv::get_field_by_index
 732 //
 733 // Get a field by index from a klass's constant pool.
 734 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor,
 735                                    int index) {
 736   GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);)
 737 }
 738 
 739 // ------------------------------------------------------------------
 740 // ciEnv::lookup_method
 741 //
 742 // Perform an appropriate method lookup based on accessor, holder,
 743 // name, signature, and bytecode.
 744 Method* ciEnv::lookup_method(ciInstanceKlass* accessor,
 745                              ciKlass*         holder,
 746                              Symbol*          name,
 747                              Symbol*          sig,
 748                              Bytecodes::Code  bc,
 749                              constantTag      tag) {
 750   // Accessibility checks are performed in ciEnv::get_method_by_index_impl.
 751   assert(check_klass_accessibility(accessor, holder->get_Klass()), "holder not accessible");
 752 
 753   InstanceKlass* accessor_klass = accessor->get_instanceKlass();
 754   Klass* holder_klass = holder->get_Klass();
 755   methodHandle dest_method;
 756   LinkInfo link_info(holder_klass, name, sig, accessor_klass, LinkInfo::needs_access_check, tag);
 757   switch (bc) {
 758   case Bytecodes::_invokestatic:
 759     dest_method =
 760       LinkResolver::resolve_static_call_or_null(link_info);
 761     break;
 762   case Bytecodes::_invokespecial:
 763     dest_method =
 764       LinkResolver::resolve_special_call_or_null(link_info);
 765     break;
 766   case Bytecodes::_invokeinterface:
 767     dest_method =
 768       LinkResolver::linktime_resolve_interface_method_or_null(link_info);
 769     break;
 770   case Bytecodes::_invokevirtual:
 771     dest_method =
 772       LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
 773     break;
 774   default: ShouldNotReachHere();
 775   }
 776 
 777   return dest_method();
 778 }
 779 
 780 
 781 // ------------------------------------------------------------------
 782 // ciEnv::get_method_by_index_impl
 783 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool,
 784                                           int index, Bytecodes::Code bc,
 785                                           ciInstanceKlass* accessor) {
 786   if (bc == Bytecodes::_invokedynamic) {
 787     ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index);
 788     bool is_resolved = !cpce->is_f1_null();
 789     // FIXME: code generation could allow for null (unlinked) call site
 790     // The call site could be made patchable as follows:
 791     // Load the appendix argument from the constant pool.
 792     // Test the appendix argument and jump to a known deopt routine if it is null.
 793     // Jump through a patchable call site, which is initially a deopt routine.
 794     // Patch the call site to the nmethod entry point of the static compiled lambda form.
 795     // As with other two-component call sites, both values must be independently verified.
 796 
 797     if (is_resolved) {
 798       // Get the invoker Method* from the constant pool.
 799       // (The appendix argument, if any, will be noted in the method's signature.)
 800       Method* adapter = cpce->f1_as_method();
 801       return get_method(adapter);
 802     }
 803 
 804     // Fake a method that is equivalent to a declared method.
 805     ciInstanceKlass* holder    = get_instance_klass(SystemDictionary::MethodHandle_klass());
 806     ciSymbol*        name      = ciSymbol::invokeBasic_name();
 807     ciSymbol*        signature = get_symbol(cpool->signature_ref_at(index));
 808     return get_unloaded_method(holder, name, signature, accessor);
 809   } else {
 810     const int holder_index = cpool->klass_ref_index_at(index);
 811     bool holder_is_accessible;
 812     ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
 813 
 814     // Get the method's name and signature.
 815     Symbol* name_sym = cpool->name_ref_at(index);
 816     Symbol* sig_sym  = cpool->signature_ref_at(index);
 817 
 818     if (cpool->has_preresolution()
 819         || ((holder == ciEnv::MethodHandle_klass() || holder == ciEnv::VarHandle_klass()) &&
 820             MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) {
 821       // Short-circuit lookups for JSR 292-related call sites.
 822       // That is, do not rely only on name-based lookups, because they may fail
 823       // if the names are not resolvable in the boot class loader (7056328).
 824       switch (bc) {
 825       case Bytecodes::_invokevirtual:
 826       case Bytecodes::_invokeinterface:
 827       case Bytecodes::_invokespecial:
 828       case Bytecodes::_invokestatic:
 829         {
 830           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 831           if (m != NULL) {
 832             return get_method(m);
 833           }
 834         }
 835         break;
 836       default:
 837         break;
 838       }
 839     }
 840 
 841     if (holder_is_accessible) {  // Our declared holder is loaded.
 842       constantTag tag = cpool->tag_ref_at(index);
 843       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 844       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 845       if (m != NULL &&
 846           (bc == Bytecodes::_invokestatic
 847            ?  m->method_holder()->is_not_initialized()
 848            : !m->method_holder()->is_loaded())) {
 849         m = NULL;
 850       }
 851 #ifdef ASSERT
 852       if (m != NULL && ReplayCompiles && !ciReplay::is_loaded(m)) {
 853         m = NULL;
 854       }
 855 #endif
 856       if (m != NULL) {
 857         // We found the method.
 858         return get_method(m);
 859       }
 860     }
 861 
 862     // Either the declared holder was not loaded, or the method could
 863     // not be found.  Create a dummy ciMethod to represent the failed
 864     // lookup.
 865     ciSymbol* name      = get_symbol(name_sym);
 866     ciSymbol* signature = get_symbol(sig_sym);
 867     return get_unloaded_method(holder, name, signature, accessor);
 868   }
 869 }
 870 
 871 
 872 // ------------------------------------------------------------------
 873 // ciEnv::get_instance_klass_for_declared_method_holder
 874 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) {
 875   // For the case of <array>.clone(), the method holder can be a ciArrayKlass
 876   // instead of a ciInstanceKlass.  For that case simply pretend that the
 877   // declared holder is Object.clone since that's where the call will bottom out.
 878   // A more correct fix would trickle out through many interfaces in CI,
 879   // requiring ciInstanceKlass* to become ciKlass* and many more places would
 880   // require checks to make sure the expected type was found.  Given that this
 881   // only occurs for clone() the more extensive fix seems like overkill so
 882   // instead we simply smear the array type into Object.
 883   guarantee(method_holder != NULL, "no method holder");
 884   if (method_holder->is_instance_klass()) {
 885     return method_holder->as_instance_klass();
 886   } else if (method_holder->is_array_klass()) {
 887     return current()->Object_klass();
 888   } else {
 889     ShouldNotReachHere();
 890   }
 891   return NULL;
 892 }
 893 
 894 
 895 // ------------------------------------------------------------------
 896 // ciEnv::get_method_by_index
 897 ciMethod* ciEnv::get_method_by_index(const constantPoolHandle& cpool,
 898                                      int index, Bytecodes::Code bc,
 899                                      ciInstanceKlass* accessor) {
 900   GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);)
 901 }
 902 
 903 
 904 // ------------------------------------------------------------------
 905 // ciEnv::name_buffer
 906 char *ciEnv::name_buffer(int req_len) {
 907   if (_name_buffer_len < req_len) {
 908     if (_name_buffer == NULL) {
 909       _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len);
 910       _name_buffer_len = req_len;
 911     } else {
 912       _name_buffer =
 913         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 914       _name_buffer_len = req_len;
 915     }
 916   }
 917   return _name_buffer;
 918 }
 919 
 920 // ------------------------------------------------------------------
 921 // ciEnv::is_in_vm
 922 bool ciEnv::is_in_vm() {
 923   return JavaThread::current()->thread_state() == _thread_in_vm;
 924 }
 925 
 926 bool ciEnv::system_dictionary_modification_counter_changed() {
 927   return _system_dictionary_modification_counter != SystemDictionary::number_of_modifications();
 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();
 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("invalid non-klass dependency");
 946     } else {
 947       record_failure("concurrent class loading");
 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   // Create and fire JFR event
1152   EventCompilationFailure event;
1153   if (event.should_commit()) {
1154     event.set_compileId(compile_id());
1155     event.set_failureMessage(reason);
1156     event.commit();
1157   }
1158 }
1159 
1160 // ------------------------------------------------------------------
1161 // ciEnv::record_method_not_compilable()
1162 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) {
1163   int new_compilable =
1164     all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ;
1165 
1166   // Only note transitions to a worse state
1167   if (new_compilable > _compilable) {
1168     if (log() != NULL) {
1169       if (all_tiers) {
1170         log()->elem("method_not_compilable");
1171       } else {
1172         log()->elem("method_not_compilable_at_tier level='%d'",
1173                     current()->task()->comp_level());
1174       }
1175     }
1176     _compilable = new_compilable;
1177 
1178     // Reset failure reason; this one is more important.
1179     _failure_reason = NULL;
1180     record_failure(reason);
1181   }
1182 }
1183 
1184 // ------------------------------------------------------------------
1185 // ciEnv::record_out_of_memory_failure()
1186 void ciEnv::record_out_of_memory_failure() {
1187   // If memory is low, we stop compiling methods.
1188   record_method_not_compilable("out of memory");
1189 }
1190 
1191 ciInstance* ciEnv::unloaded_ciinstance() {
1192   GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();)
1193 }
1194 
1195 // ------------------------------------------------------------------
1196 // ciEnv::dump_replay_data*
1197 
1198 // Don't change thread state and acquire any locks.
1199 // Safe to call from VM error reporter.
1200 
1201 void ciEnv::dump_compile_data(outputStream* out) {
1202   CompileTask* task = this->task();
1203   if (task) {
1204     Method* method = task->method();
1205     int entry_bci = task->osr_bci();
1206     int comp_level = task->comp_level();
1207     out->print("compile %s %s %s %d %d",
1208                method->klass_name()->as_quoted_ascii(),
1209                method->name()->as_quoted_ascii(),
1210                method->signature()->as_quoted_ascii(),
1211                entry_bci, comp_level);
1212     if (compiler_data() != NULL) {
1213       if (is_c2_compile(comp_level)) {
1214 #ifdef COMPILER2
1215         // Dump C2 inlining data.
1216         ((Compile*)compiler_data())->dump_inline_data(out);
1217 #endif
1218       } else if (is_c1_compile(comp_level)) {
1219 #ifdef COMPILER1
1220         // Dump C1 inlining data.
1221         ((Compilation*)compiler_data())->dump_inline_data(out);
1222 #endif
1223       }
1224     }
1225     out->cr();
1226   }
1227 }
1228 
1229 void ciEnv::dump_replay_data_unsafe(outputStream* out) {
1230   ResourceMark rm;
1231 #if INCLUDE_JVMTI
1232   out->print_cr("JvmtiExport can_access_local_variables %d",     _jvmti_can_access_local_variables);
1233   out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint);
1234   out->print_cr("JvmtiExport can_post_on_exceptions %d",         _jvmti_can_post_on_exceptions);
1235 #endif // INCLUDE_JVMTI
1236 
1237   GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata();
1238   out->print_cr("# %d ciObject found", objects->length());
1239   for (int i = 0; i < objects->length(); i++) {
1240     objects->at(i)->dump_replay_data(out);
1241   }
1242   dump_compile_data(out);
1243   out->flush();
1244 }
1245 
1246 void ciEnv::dump_replay_data(outputStream* out) {
1247   GUARDED_VM_ENTRY(
1248     MutexLocker ml(Compile_lock);
1249     dump_replay_data_unsafe(out);
1250   )
1251 }
1252 
1253 void ciEnv::dump_replay_data(int compile_id) {
1254   static char buffer[O_BUFLEN];
1255   int ret = jio_snprintf(buffer, O_BUFLEN, "replay_pid%p_compid%d.log", os::current_process_id(), compile_id);
1256   if (ret > 0) {
1257     int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1258     if (fd != -1) {
1259       FILE* replay_data_file = os::open(fd, "w");
1260       if (replay_data_file != NULL) {
1261         fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1262         dump_replay_data(&replay_data_stream);
1263         tty->print_cr("# Compiler replay data is saved as: %s", buffer);
1264       } else {
1265         tty->print_cr("# Can't open file to dump replay data.");
1266       }
1267     }
1268   }
1269 }
1270 
1271 void ciEnv::dump_inline_data(int compile_id) {
1272   static char buffer[O_BUFLEN];
1273   int ret = jio_snprintf(buffer, O_BUFLEN, "inline_pid%p_compid%d.log", os::current_process_id(), compile_id);
1274   if (ret > 0) {
1275     int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1276     if (fd != -1) {
1277       FILE* inline_data_file = os::open(fd, "w");
1278       if (inline_data_file != NULL) {
1279         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1280         GUARDED_VM_ENTRY(
1281           MutexLocker ml(Compile_lock);
1282           dump_compile_data(&replay_data_stream);
1283         )
1284         replay_data_stream.flush();
1285         tty->print("# Compiler inline data is saved as: ");
1286         tty->print_cr("%s", buffer);
1287       } else {
1288         tty->print_cr("# Can't open file to dump inline data.");
1289       }
1290     }
1291   }
1292 }