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