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