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