1 /*
   2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "ci/ciConstant.hpp"
  28 #include "ci/ciEnv.hpp"
  29 #include "ci/ciField.hpp"
  30 #include "ci/ciInstance.hpp"
  31 #include "ci/ciInstanceKlass.hpp"
  32 #include "ci/ciMethod.hpp"
  33 #include "ci/ciNullObject.hpp"
  34 #include "ci/ciReplay.hpp"
  35 #include "ci/ciUtilities.inline.hpp"
  36 #include "classfile/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/compileLog.hpp"
  43 #include "compiler/disassembler.hpp"
  44 #include "gc/shared/collectedHeap.inline.hpp"
  45 #include "interpreter/linkResolver.hpp"
  46 #include "jfr/jfrEvents.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 (sym->char_at(0) == JVM_SIGNATURE_CLASS &&
 417       sym->char_at(sym->utf8_length()-1) == JVM_SIGNATURE_ENDCLASS) {
 418     // This is a name from a signature.  Strip off the trimmings.
 419     // Call recursive to keep scope of strippedsym.
 420     TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
 421                                                         sym->utf8_length()-2);
 422     ciSymbol* strippedname = get_symbol(strippedsym);
 423     return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local);
 424   }
 425 
 426   // Check for prior unloaded klass.  The SystemDictionary's answers
 427   // can vary over time but the compiler needs consistency.
 428   ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name);
 429   if (unloaded_klass != NULL) {
 430     if (require_local)  return NULL;
 431     return unloaded_klass;
 432   }
 433 
 434   Handle loader(THREAD, (oop)NULL);
 435   Handle domain(THREAD, (oop)NULL);
 436   if (accessing_klass != NULL) {
 437     loader = Handle(THREAD, accessing_klass->loader());
 438     domain = Handle(THREAD, accessing_klass->protection_domain());
 439   }
 440 
 441   // setup up the proper type to return on OOM
 442   ciKlass* fail_type;
 443   if (sym->char_at(0) == JVM_SIGNATURE_ARRAY) {
 444     fail_type = _unloaded_ciobjarrayklass;
 445   } else {
 446     fail_type = _unloaded_ciinstance_klass;
 447   }
 448   Klass* found_klass;
 449   {
 450     ttyUnlocker ttyul;  // release tty lock to avoid ordering problems
 451     MutexLocker ml(Compile_lock);
 452     Klass* kls;
 453     if (!require_local) {
 454       kls = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader,
 455                                                                        KILL_COMPILE_ON_FATAL_(fail_type));
 456     } else {
 457       kls = SystemDictionary::find_instance_or_array_klass(sym, loader, domain,
 458                                                            KILL_COMPILE_ON_FATAL_(fail_type));
 459     }
 460     found_klass = kls;
 461   }
 462 
 463   // If we fail to find an array klass, look again for its element type.
 464   // The element type may be available either locally or via constraints.
 465   // In either case, if we can find the element type in the system dictionary,
 466   // we must build an array type around it.  The CI requires array klasses
 467   // to be loaded if their element klasses are loaded, except when memory
 468   // is exhausted.
 469   if (sym->char_at(0) == JVM_SIGNATURE_ARRAY &&
 470       (sym->char_at(1) == JVM_SIGNATURE_ARRAY || sym->char_at(1) == JVM_SIGNATURE_CLASS)) {
 471     // We have an unloaded array.
 472     // Build it on the fly if the element class exists.
 473     TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
 474                                                      sym->utf8_length()-1);
 475 
 476     // Get element ciKlass recursively.
 477     ciKlass* elem_klass =
 478       get_klass_by_name_impl(accessing_klass,
 479                              cpool,
 480                              get_symbol(elem_sym),
 481                              require_local);
 482     if (elem_klass != NULL && elem_klass->is_loaded()) {
 483       // Now make an array for it
 484       return ciObjArrayKlass::make_impl(elem_klass);
 485     }
 486   }
 487 
 488   if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) {
 489     // Look inside the constant pool for pre-resolved class entries.
 490     for (int i = cpool->length() - 1; i >= 1; i--) {
 491       if (cpool->tag_at(i).is_klass()) {
 492         Klass* kls = cpool->resolved_klass_at(i);
 493         if (kls->name() == sym) {
 494           found_klass = kls;
 495           break;
 496         }
 497       }
 498     }
 499   }
 500 
 501   if (found_klass != NULL) {
 502     // Found it.  Build a CI handle.
 503     return get_klass(found_klass);
 504   }
 505 
 506   if (require_local)  return NULL;
 507 
 508   // Not yet loaded into the VM, or not governed by loader constraints.
 509   // Make a CI representative for it.
 510   return get_unloaded_klass(accessing_klass, name);
 511 }
 512 
 513 // ------------------------------------------------------------------
 514 // ciEnv::get_klass_by_name
 515 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass,
 516                                   ciSymbol* klass_name,
 517                                   bool require_local) {
 518   GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass,
 519                                                  constantPoolHandle(),
 520                                                  klass_name,
 521                                                  require_local);)
 522 }
 523 
 524 // ------------------------------------------------------------------
 525 // ciEnv::get_klass_by_index_impl
 526 //
 527 // Implementation of get_klass_by_index.
 528 ciKlass* ciEnv::get_klass_by_index_impl(const constantPoolHandle& cpool,
 529                                         int index,
 530                                         bool& is_accessible,
 531                                         ciInstanceKlass* accessor) {
 532   EXCEPTION_CONTEXT;
 533   Klass* klass = NULL;
 534   Symbol* klass_name = NULL;
 535 
 536   if (cpool->tag_at(index).is_symbol()) {
 537     klass_name = cpool->symbol_at(index);
 538   } else {
 539     // Check if it's resolved if it's not a symbol constant pool entry.
 540     klass =  ConstantPool::klass_at_if_loaded(cpool, index);
 541     // Try to look it up by name.
 542     if (klass == NULL) {
 543       klass_name = cpool->klass_name_at(index);
 544     }
 545   }
 546 
 547   if (klass == NULL) {
 548     // Not found in constant pool.  Use the name to do the lookup.
 549     ciKlass* k = get_klass_by_name_impl(accessor,
 550                                         cpool,
 551                                         get_symbol(klass_name),
 552                                         false);
 553     // Calculate accessibility the hard way.
 554     if (!k->is_loaded()) {
 555       is_accessible = false;
 556     } else if (k->loader() != accessor->loader() &&
 557                get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
 558       // Loaded only remotely.  Not linked yet.
 559       is_accessible = false;
 560     } else {
 561       // Linked locally, and we must also check public/private, etc.
 562       is_accessible = check_klass_accessibility(accessor, k->get_Klass());
 563     }
 564     return k;
 565   }
 566 
 567   // Check for prior unloaded klass.  The SystemDictionary's answers
 568   // can vary over time but the compiler needs consistency.
 569   ciSymbol* name = get_symbol(klass->name());
 570   ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name);
 571   if (unloaded_klass != NULL) {
 572     is_accessible = false;
 573     return unloaded_klass;
 574   }
 575 
 576   // It is known to be accessible, since it was found in the constant pool.
 577   is_accessible = true;
 578   return get_klass(klass);
 579 }
 580 
 581 // ------------------------------------------------------------------
 582 // ciEnv::get_klass_by_index
 583 //
 584 // Get a klass from the constant pool.
 585 ciKlass* ciEnv::get_klass_by_index(const constantPoolHandle& cpool,
 586                                    int index,
 587                                    bool& is_accessible,
 588                                    ciInstanceKlass* accessor) {
 589   GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);)
 590 }
 591 
 592 // ------------------------------------------------------------------
 593 // ciEnv::get_constant_by_index_impl
 594 //
 595 // Implementation of get_constant_by_index().
 596 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 597                                              int pool_index, int cache_index,
 598                                              ciInstanceKlass* accessor) {
 599   bool ignore_will_link;
 600   EXCEPTION_CONTEXT;
 601   int index = pool_index;
 602   if (cache_index >= 0) {
 603     assert(index < 0, "only one kind of index at a time");
 604     index = cpool->object_to_cp_index(cache_index);
 605     oop obj = cpool->resolved_references()->obj_at(cache_index);
 606     if (obj != NULL) {
 607       if (obj == Universe::the_null_sentinel()) {
 608         return ciConstant(T_OBJECT, get_object(NULL));
 609       }
 610       BasicType bt = T_OBJECT;
 611       if (cpool->tag_at(index).is_dynamic_constant())
 612         bt = FieldType::basic_type(cpool->uncached_signature_ref_at(index));
 613       if (is_reference_type(bt)) {
 614       } else {
 615         // we have to unbox the primitive value
 616         if (!is_java_primitive(bt))  return ciConstant();
 617         jvalue value;
 618         BasicType bt2 = java_lang_boxing_object::get_value(obj, &value);
 619         assert(bt2 == bt, "");
 620         switch (bt2) {
 621         case T_DOUBLE:  return ciConstant(value.d);
 622         case T_FLOAT:   return ciConstant(value.f);
 623         case T_LONG:    return ciConstant(value.j);
 624         case T_INT:     return ciConstant(bt2, value.i);
 625         case T_SHORT:   return ciConstant(bt2, value.s);
 626         case T_BYTE:    return ciConstant(bt2, value.b);
 627         case T_CHAR:    return ciConstant(bt2, value.c);
 628         case T_BOOLEAN: return ciConstant(bt2, value.z);
 629         default:  return ciConstant();
 630         }
 631       }
 632       ciObject* ciobj = get_object(obj);
 633       if (ciobj->is_array()) {
 634         return ciConstant(T_ARRAY, ciobj);
 635       } else {
 636         assert(ciobj->is_instance(), "should be an instance");
 637         return ciConstant(T_OBJECT, ciobj);
 638       }
 639     }
 640   }
 641   constantTag tag = cpool->tag_at(index);
 642   if (tag.is_int()) {
 643     return ciConstant(T_INT, (jint)cpool->int_at(index));
 644   } else if (tag.is_long()) {
 645     return ciConstant((jlong)cpool->long_at(index));
 646   } else if (tag.is_float()) {
 647     return ciConstant((jfloat)cpool->float_at(index));
 648   } else if (tag.is_double()) {
 649     return ciConstant((jdouble)cpool->double_at(index));
 650   } else if (tag.is_string()) {
 651     oop string = NULL;
 652     assert(cache_index >= 0, "should have a cache index");
 653     if (cpool->is_pseudo_string_at(index)) {
 654       string = cpool->pseudo_string_at(index, cache_index);
 655     } else {
 656       string = cpool->string_at(index, cache_index, THREAD);
 657       if (HAS_PENDING_EXCEPTION) {
 658         CLEAR_PENDING_EXCEPTION;
 659         record_out_of_memory_failure();
 660         return ciConstant();
 661       }
 662     }
 663     ciObject* constant = get_object(string);
 664     if (constant->is_array()) {
 665       return ciConstant(T_ARRAY, constant);
 666     } else {
 667       assert (constant->is_instance(), "must be an instance, or not? ");
 668       return ciConstant(T_OBJECT, constant);
 669     }
 670   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
 671     // 4881222: allow ldc to take a class type
 672     ciKlass* klass = get_klass_by_index_impl(cpool, index, ignore_will_link, accessor);
 673     if (HAS_PENDING_EXCEPTION) {
 674       CLEAR_PENDING_EXCEPTION;
 675       record_out_of_memory_failure();
 676       return ciConstant();
 677     }
 678     assert (klass->is_instance_klass() || klass->is_array_klass(),
 679             "must be an instance or array klass ");
 680     return ciConstant(T_OBJECT, klass->java_mirror());
 681   } else if (tag.is_method_type()) {
 682     // must execute Java code to link this CP entry into cache[i].f1
 683     ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index));
 684     ciObject* ciobj = get_unloaded_method_type_constant(signature);
 685     return ciConstant(T_OBJECT, ciobj);
 686   } else if (tag.is_method_handle()) {
 687     // must execute Java code to link this CP entry into cache[i].f1
 688     int ref_kind        = cpool->method_handle_ref_kind_at(index);
 689     int callee_index    = cpool->method_handle_klass_index_at(index);
 690     ciKlass* callee     = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor);
 691     ciSymbol* name      = get_symbol(cpool->method_handle_name_ref_at(index));
 692     ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index));
 693     ciObject* ciobj     = get_unloaded_method_handle_constant(callee, name, signature, ref_kind);
 694     return ciConstant(T_OBJECT, ciobj);
 695   } else if (tag.is_dynamic_constant()) {
 696     return ciConstant();
 697   } else {
 698     ShouldNotReachHere();
 699     return ciConstant();
 700   }
 701 }
 702 
 703 // ------------------------------------------------------------------
 704 // ciEnv::get_constant_by_index
 705 //
 706 // Pull a constant out of the constant pool.  How appropriate.
 707 //
 708 // Implementation note: this query is currently in no way cached.
 709 ciConstant ciEnv::get_constant_by_index(const constantPoolHandle& cpool,
 710                                         int pool_index, int cache_index,
 711                                         ciInstanceKlass* accessor) {
 712   GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);)
 713 }
 714 
 715 // ------------------------------------------------------------------
 716 // ciEnv::get_field_by_index_impl
 717 //
 718 // Implementation of get_field_by_index.
 719 //
 720 // Implementation note: the results of field lookups are cached
 721 // in the accessor klass.
 722 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor,
 723                                         int index) {
 724   ciConstantPoolCache* cache = accessor->field_cache();
 725   if (cache == NULL) {
 726     ciField* field = new (arena()) ciField(accessor, index);
 727     return field;
 728   } else {
 729     ciField* field = (ciField*)cache->get(index);
 730     if (field == NULL) {
 731       field = new (arena()) ciField(accessor, index);
 732       cache->insert(index, field);
 733     }
 734     return field;
 735   }
 736 }
 737 
 738 // ------------------------------------------------------------------
 739 // ciEnv::get_field_by_index
 740 //
 741 // Get a field by index from a klass's constant pool.
 742 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor,
 743                                    int index) {
 744   GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);)
 745 }
 746 
 747 // ------------------------------------------------------------------
 748 // ciEnv::lookup_method
 749 //
 750 // Perform an appropriate method lookup based on accessor, holder,
 751 // name, signature, and bytecode.
 752 Method* ciEnv::lookup_method(ciInstanceKlass* accessor,
 753                              ciKlass*         holder,
 754                              Symbol*          name,
 755                              Symbol*          sig,
 756                              Bytecodes::Code  bc,
 757                              constantTag      tag) {
 758   // Accessibility checks are performed in ciEnv::get_method_by_index_impl.
 759   assert(check_klass_accessibility(accessor, holder->get_Klass()), "holder not accessible");
 760 
 761   InstanceKlass* accessor_klass = accessor->get_instanceKlass();
 762   Klass* holder_klass = holder->get_Klass();
 763   methodHandle dest_method;
 764   LinkInfo link_info(holder_klass, name, sig, accessor_klass, LinkInfo::needs_access_check, tag);
 765   switch (bc) {
 766   case Bytecodes::_invokestatic:
 767     dest_method =
 768       LinkResolver::resolve_static_call_or_null(link_info);
 769     break;
 770   case Bytecodes::_invokespecial:
 771     dest_method =
 772       LinkResolver::resolve_special_call_or_null(link_info);
 773     break;
 774   case Bytecodes::_invokeinterface:
 775     dest_method =
 776       LinkResolver::linktime_resolve_interface_method_or_null(link_info);
 777     break;
 778   case Bytecodes::_invokevirtual:
 779     dest_method =
 780       LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
 781     break;
 782   default: ShouldNotReachHere();
 783   }
 784 
 785   return dest_method();
 786 }
 787 
 788 
 789 // ------------------------------------------------------------------
 790 // ciEnv::get_method_by_index_impl
 791 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool,
 792                                           int index, Bytecodes::Code bc,
 793                                           ciInstanceKlass* accessor) {
 794   if (bc == Bytecodes::_invokedynamic) {
 795     ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index);
 796     bool is_resolved = !cpce->is_f1_null();
 797     // FIXME: code generation could allow for null (unlinked) call site
 798     // The call site could be made patchable as follows:
 799     // Load the appendix argument from the constant pool.
 800     // Test the appendix argument and jump to a known deopt routine if it is null.
 801     // Jump through a patchable call site, which is initially a deopt routine.
 802     // Patch the call site to the nmethod entry point of the static compiled lambda form.
 803     // As with other two-component call sites, both values must be independently verified.
 804 
 805     if (is_resolved) {
 806       // Get the invoker Method* from the constant pool.
 807       // (The appendix argument, if any, will be noted in the method's signature.)
 808       Method* adapter = cpce->f1_as_method();
 809       return get_method(adapter);
 810     }
 811 
 812     // Fake a method that is equivalent to a declared method.
 813     ciInstanceKlass* holder    = get_instance_klass(SystemDictionary::MethodHandle_klass());
 814     ciSymbol*        name      = ciSymbol::invokeBasic_name();
 815     ciSymbol*        signature = get_symbol(cpool->signature_ref_at(index));
 816     return get_unloaded_method(holder, name, signature, accessor);
 817   } else {
 818     const int holder_index = cpool->klass_ref_index_at(index);
 819     bool holder_is_accessible;
 820     ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
 821 
 822     // Get the method's name and signature.
 823     Symbol* name_sym = cpool->name_ref_at(index);
 824     Symbol* sig_sym  = cpool->signature_ref_at(index);
 825 
 826     if (cpool->has_preresolution()
 827         || ((holder == ciEnv::MethodHandle_klass() || holder == ciEnv::VarHandle_klass()) &&
 828             MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) {
 829       // Short-circuit lookups for JSR 292-related call sites.
 830       // That is, do not rely only on name-based lookups, because they may fail
 831       // if the names are not resolvable in the boot class loader (7056328).
 832       switch (bc) {
 833       case Bytecodes::_invokevirtual:
 834       case Bytecodes::_invokeinterface:
 835       case Bytecodes::_invokespecial:
 836       case Bytecodes::_invokestatic:
 837         {
 838           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 839           if (m != NULL) {
 840             return get_method(m);
 841           }
 842         }
 843         break;
 844       default:
 845         break;
 846       }
 847     }
 848 
 849     if (holder_is_accessible) {  // Our declared holder is loaded.
 850       constantTag tag = cpool->tag_ref_at(index);
 851       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 852       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 853       if (m != NULL &&
 854           (bc == Bytecodes::_invokestatic
 855            ?  m->method_holder()->is_not_initialized()
 856            : !m->method_holder()->is_loaded())) {
 857         m = NULL;
 858       }
 859 #ifdef ASSERT
 860       if (m != NULL && ReplayCompiles && !ciReplay::is_loaded(m)) {
 861         m = NULL;
 862       }
 863 #endif
 864       if (m != NULL) {
 865         // We found the method.
 866         return get_method(m);
 867       }
 868     }
 869 
 870     // Either the declared holder was not loaded, or the method could
 871     // not be found.  Create a dummy ciMethod to represent the failed
 872     // lookup.
 873     ciSymbol* name      = get_symbol(name_sym);
 874     ciSymbol* signature = get_symbol(sig_sym);
 875     return get_unloaded_method(holder, name, signature, accessor);
 876   }
 877 }
 878 
 879 
 880 // ------------------------------------------------------------------
 881 // ciEnv::get_instance_klass_for_declared_method_holder
 882 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) {
 883   // For the case of <array>.clone(), the method holder can be a ciArrayKlass
 884   // instead of a ciInstanceKlass.  For that case simply pretend that the
 885   // declared holder is Object.clone since that's where the call will bottom out.
 886   // A more correct fix would trickle out through many interfaces in CI,
 887   // requiring ciInstanceKlass* to become ciKlass* and many more places would
 888   // require checks to make sure the expected type was found.  Given that this
 889   // only occurs for clone() the more extensive fix seems like overkill so
 890   // instead we simply smear the array type into Object.
 891   guarantee(method_holder != NULL, "no method holder");
 892   if (method_holder->is_instance_klass()) {
 893     return method_holder->as_instance_klass();
 894   } else if (method_holder->is_array_klass()) {
 895     return current()->Object_klass();
 896   } else {
 897     ShouldNotReachHere();
 898   }
 899   return NULL;
 900 }
 901 
 902 
 903 // ------------------------------------------------------------------
 904 // ciEnv::get_method_by_index
 905 ciMethod* ciEnv::get_method_by_index(const constantPoolHandle& cpool,
 906                                      int index, Bytecodes::Code bc,
 907                                      ciInstanceKlass* accessor) {
 908   GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);)
 909 }
 910 
 911 
 912 // ------------------------------------------------------------------
 913 // ciEnv::name_buffer
 914 char *ciEnv::name_buffer(int req_len) {
 915   if (_name_buffer_len < req_len) {
 916     if (_name_buffer == NULL) {
 917       _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len);
 918       _name_buffer_len = req_len;
 919     } else {
 920       _name_buffer =
 921         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 922       _name_buffer_len = req_len;
 923     }
 924   }
 925   return _name_buffer;
 926 }
 927 
 928 // ------------------------------------------------------------------
 929 // ciEnv::is_in_vm
 930 bool ciEnv::is_in_vm() {
 931   return JavaThread::current()->thread_state() == _thread_in_vm;
 932 }
 933 
 934 // ------------------------------------------------------------------
 935 // ciEnv::validate_compile_task_dependencies
 936 //
 937 // Check for changes during compilation (e.g. class loads, evolution,
 938 // breakpoints, call site invalidation).
 939 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 940   if (failing())  return;  // no need for further checks
 941 
 942   Dependencies::DepType result = dependencies()->validate_dependencies(_task);
 943   if (result != Dependencies::end_marker) {
 944     if (result == Dependencies::call_site_target_value) {
 945       _inc_decompile_count_on_failure = false;
 946       record_failure("call site target change");
 947     } else if (Dependencies::is_klass_type(result)) {
 948       record_failure("concurrent class loading");
 949     } else {
 950       record_failure("invalid non-klass dependency");
 951     }
 952   }
 953 }
 954 
 955 // ------------------------------------------------------------------
 956 // ciEnv::register_method
 957 void ciEnv::register_method(ciMethod* target,
 958                             int entry_bci,
 959                             CodeOffsets* offsets,
 960                             int orig_pc_offset,
 961                             CodeBuffer* code_buffer,
 962                             int frame_words,
 963                             OopMapSet* oop_map_set,
 964                             ExceptionHandlerTable* handler_table,
 965                             ImplicitExceptionTable* inc_table,
 966                             AbstractCompiler* compiler,
 967                             bool has_unsafe_access,
 968                             bool has_wide_vectors,
 969                             RTMState  rtm_state) {
 970   VM_ENTRY_MARK;
 971   nmethod* nm = NULL;
 972   {
 973     // To prevent compile queue updates.
 974     MutexLocker locker(MethodCompileQueue_lock, THREAD);
 975 
 976     // Prevent SystemDictionary::add_to_hierarchy from running
 977     // and invalidating our dependencies until we install this method.
 978     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
 979     MutexLocker ml(Compile_lock);
 980     NoSafepointVerifier nsv;
 981 
 982     // Change in Jvmti state may invalidate compilation.
 983     if (!failing() && jvmti_state_changed()) {
 984       record_failure("Jvmti state change invalidated dependencies");
 985     }
 986 
 987     // Change in DTrace flags may invalidate compilation.
 988     if (!failing() &&
 989         ( (!dtrace_extended_probes() && ExtendedDTraceProbes) ||
 990           (!dtrace_method_probes() && DTraceMethodProbes) ||
 991           (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
 992       record_failure("DTrace flags change invalidated dependencies");
 993     }
 994 
 995     if (!failing() && target->needs_clinit_barrier() &&
 996         target->holder()->is_in_error_state()) {
 997       record_failure("method holder is in error state");
 998     }
 999 
1000     if (!failing()) {
1001       if (log() != NULL) {
1002         // Log the dependencies which this compilation declares.
1003         dependencies()->log_all_dependencies();
1004       }
1005 
1006       // Encode the dependencies now, so we can check them right away.
1007       dependencies()->encode_content_bytes();
1008 
1009       // Check for {class loads, evolution, breakpoints, ...} during compilation
1010       validate_compile_task_dependencies(target);
1011     }
1012 
1013     methodHandle method(THREAD, target->get_Method());
1014 
1015 #if INCLUDE_RTM_OPT
1016     if (!failing() && (rtm_state != NoRTM) &&
1017         (method()->method_data() != NULL) &&
1018         (method()->method_data()->rtm_state() != rtm_state)) {
1019       // Preemptive decompile if rtm state was changed.
1020       record_failure("RTM state change invalidated rtm code");
1021     }
1022 #endif
1023 
1024     if (failing()) {
1025       // While not a true deoptimization, it is a preemptive decompile.
1026       MethodData* mdo = method()->method_data();
1027       if (mdo != NULL && _inc_decompile_count_on_failure) {
1028         mdo->inc_decompile_count();
1029       }
1030 
1031       // All buffers in the CodeBuffer are allocated in the CodeCache.
1032       // If the code buffer is created on each compile attempt
1033       // as in C2, then it must be freed.
1034       code_buffer->free_blob();
1035       return;
1036     }
1037 
1038     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1039     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1040 
1041     nm =  nmethod::new_nmethod(method,
1042                                compile_id(),
1043                                entry_bci,
1044                                offsets,
1045                                orig_pc_offset,
1046                                debug_info(), dependencies(), code_buffer,
1047                                frame_words, oop_map_set,
1048                                handler_table, inc_table,
1049                                compiler, task()->comp_level());
1050 
1051     // Free codeBlobs
1052     code_buffer->free_blob();
1053 
1054     if (nm != NULL) {
1055       nm->set_has_unsafe_access(has_unsafe_access);
1056       nm->set_has_wide_vectors(has_wide_vectors);
1057 #if INCLUDE_RTM_OPT
1058       nm->set_rtm_state(rtm_state);
1059 #endif
1060 
1061       // Record successful registration.
1062       // (Put nm into the task handle *before* publishing to the Java heap.)
1063       if (task() != NULL) {
1064         task()->set_code(nm);
1065       }
1066 
1067       if (entry_bci == InvocationEntryBci) {
1068         if (TieredCompilation) {
1069           // If there is an old version we're done with it
1070           CompiledMethod* old = method->code();
1071           if (TraceMethodReplacement && old != NULL) {
1072             ResourceMark rm;
1073             char *method_name = method->name_and_sig_as_C_string();
1074             tty->print_cr("Replacing method %s", method_name);
1075           }
1076           if (old != NULL) {
1077             old->make_not_used();
1078           }
1079         }
1080 
1081         LogTarget(Info, nmethod, install) lt;
1082         if (lt.is_enabled()) {
1083           ResourceMark rm;
1084           char *method_name = method->name_and_sig_as_C_string();
1085           lt.print("Installing method (%d) %s ",
1086                     task()->comp_level(), method_name);
1087         }
1088         // Allow the code to be executed
1089         MutexLocker ml(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1090         if (nm->make_in_use()) {
1091           method->set_code(method, nm);
1092         }
1093       } else {
1094         LogTarget(Info, nmethod, install) lt;
1095         if (lt.is_enabled()) {
1096           ResourceMark rm;
1097           char *method_name = method->name_and_sig_as_C_string();
1098           lt.print("Installing osr method (%d) %s @ %d",
1099                     task()->comp_level(), method_name, entry_bci);
1100         }
1101         MutexLocker ml(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1102         if (nm->make_in_use()) {
1103           method->method_holder()->add_osr_nmethod(nm);
1104         }
1105       }
1106     }
1107   }  // safepoints are allowed again
1108 
1109   if (nm != NULL) {
1110     // JVMTI -- compiled method notification (must be done outside lock)
1111     nm->post_compiled_method_load_event();
1112   } else {
1113     // The CodeCache is full.
1114     record_failure("code cache is full");
1115   }
1116 }
1117 
1118 
1119 // ------------------------------------------------------------------
1120 // ciEnv::find_system_klass
1121 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1122   VM_ENTRY_MARK;
1123   return get_klass_by_name_impl(NULL, constantPoolHandle(), klass_name, false);
1124 }
1125 
1126 // ------------------------------------------------------------------
1127 // ciEnv::comp_level
1128 int ciEnv::comp_level() {
1129   if (task() == NULL)  return CompLevel_highest_tier;
1130   return task()->comp_level();
1131 }
1132 
1133 // ------------------------------------------------------------------
1134 // ciEnv::compile_id
1135 uint ciEnv::compile_id() {
1136   if (task() == NULL)  return 0;
1137   return task()->compile_id();
1138 }
1139 
1140 // ------------------------------------------------------------------
1141 // ciEnv::notice_inlined_method()
1142 void ciEnv::notice_inlined_method(ciMethod* method) {
1143   _num_inlined_bytecodes += method->code_size_for_inlining();
1144 }
1145 
1146 // ------------------------------------------------------------------
1147 // ciEnv::num_inlined_bytecodes()
1148 int ciEnv::num_inlined_bytecodes() const {
1149   return _num_inlined_bytecodes;
1150 }
1151 
1152 // ------------------------------------------------------------------
1153 // ciEnv::record_failure()
1154 void ciEnv::record_failure(const char* reason) {
1155   if (_failure_reason == NULL) {
1156     // Record the first failure reason.
1157     _failure_reason = reason;
1158   }
1159 }
1160 
1161 void ciEnv::report_failure(const char* reason) {
1162   EventCompilationFailure event;
1163   if (event.should_commit()) {
1164     event.set_compileId(compile_id());
1165     event.set_failureMessage(reason);
1166     event.commit();
1167   }
1168 }
1169 
1170 // ------------------------------------------------------------------
1171 // ciEnv::record_method_not_compilable()
1172 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) {
1173   int new_compilable =
1174     all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ;
1175 
1176   // Only note transitions to a worse state
1177   if (new_compilable > _compilable) {
1178     if (log() != NULL) {
1179       if (all_tiers) {
1180         log()->elem("method_not_compilable");
1181       } else {
1182         log()->elem("method_not_compilable_at_tier level='%d'",
1183                     current()->task()->comp_level());
1184       }
1185     }
1186     _compilable = new_compilable;
1187 
1188     // Reset failure reason; this one is more important.
1189     _failure_reason = NULL;
1190     record_failure(reason);
1191   }
1192 }
1193 
1194 // ------------------------------------------------------------------
1195 // ciEnv::record_out_of_memory_failure()
1196 void ciEnv::record_out_of_memory_failure() {
1197   // If memory is low, we stop compiling methods.
1198   record_method_not_compilable("out of memory");
1199 }
1200 
1201 ciInstance* ciEnv::unloaded_ciinstance() {
1202   GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();)
1203 }
1204 
1205 // ------------------------------------------------------------------
1206 // ciEnv::dump_replay_data*
1207 
1208 // Don't change thread state and acquire any locks.
1209 // Safe to call from VM error reporter.
1210 
1211 void ciEnv::dump_compile_data(outputStream* out) {
1212   CompileTask* task = this->task();
1213   if (task) {
1214     Method* method = task->method();
1215     int entry_bci = task->osr_bci();
1216     int comp_level = task->comp_level();
1217     out->print("compile %s %s %s %d %d",
1218                method->klass_name()->as_quoted_ascii(),
1219                method->name()->as_quoted_ascii(),
1220                method->signature()->as_quoted_ascii(),
1221                entry_bci, comp_level);
1222     if (compiler_data() != NULL) {
1223       if (is_c2_compile(comp_level)) {
1224 #ifdef COMPILER2
1225         // Dump C2 inlining data.
1226         ((Compile*)compiler_data())->dump_inline_data(out);
1227 #endif
1228       } else if (is_c1_compile(comp_level)) {
1229 #ifdef COMPILER1
1230         // Dump C1 inlining data.
1231         ((Compilation*)compiler_data())->dump_inline_data(out);
1232 #endif
1233       }
1234     }
1235     out->cr();
1236   }
1237 }
1238 
1239 void ciEnv::dump_replay_data_unsafe(outputStream* out) {
1240   ResourceMark rm;
1241 #if INCLUDE_JVMTI
1242   out->print_cr("JvmtiExport can_access_local_variables %d",     _jvmti_can_access_local_variables);
1243   out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint);
1244   out->print_cr("JvmtiExport can_post_on_exceptions %d",         _jvmti_can_post_on_exceptions);
1245 #endif // INCLUDE_JVMTI
1246 
1247   GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata();
1248   out->print_cr("# %d ciObject found", objects->length());
1249   for (int i = 0; i < objects->length(); i++) {
1250     objects->at(i)->dump_replay_data(out);
1251   }
1252   dump_compile_data(out);
1253   out->flush();
1254 }
1255 
1256 void ciEnv::dump_replay_data(outputStream* out) {
1257   GUARDED_VM_ENTRY(
1258     MutexLocker ml(Compile_lock);
1259     dump_replay_data_unsafe(out);
1260   )
1261 }
1262 
1263 void ciEnv::dump_replay_data(int compile_id) {
1264   static char buffer[O_BUFLEN];
1265   int ret = jio_snprintf(buffer, O_BUFLEN, "replay_pid%p_compid%d.log", os::current_process_id(), compile_id);
1266   if (ret > 0) {
1267     int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1268     if (fd != -1) {
1269       FILE* replay_data_file = os::open(fd, "w");
1270       if (replay_data_file != NULL) {
1271         fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1272         dump_replay_data(&replay_data_stream);
1273         tty->print_cr("# Compiler replay data is saved as: %s", buffer);
1274       } else {
1275         tty->print_cr("# Can't open file to dump replay data.");
1276       }
1277     }
1278   }
1279 }
1280 
1281 void ciEnv::dump_inline_data(int compile_id) {
1282   static char buffer[O_BUFLEN];
1283   int ret = jio_snprintf(buffer, O_BUFLEN, "inline_pid%p_compid%d.log", os::current_process_id(), compile_id);
1284   if (ret > 0) {
1285     int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
1286     if (fd != -1) {
1287       FILE* inline_data_file = os::open(fd, "w");
1288       if (inline_data_file != NULL) {
1289         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1290         GUARDED_VM_ENTRY(
1291           MutexLocker ml(Compile_lock);
1292           dump_compile_data(&replay_data_stream);
1293         )
1294         replay_data_stream.flush();
1295         tty->print("# Compiler inline data is saved as: ");
1296         tty->print_cr("%s", buffer);
1297       } else {
1298         tty->print_cr("# Can't open file to dump inline data.");
1299       }
1300     }
1301   }
1302 }