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