ciEnv.cpp 45.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25
#include "precompiled.hpp"
26
#include "jvm.h"
27 28 29 30 31 32 33
#include "ci/ciConstant.hpp"
#include "ci/ciEnv.hpp"
#include "ci/ciField.hpp"
#include "ci/ciInstance.hpp"
#include "ci/ciInstanceKlass.hpp"
#include "ci/ciMethod.hpp"
#include "ci/ciNullObject.hpp"
34
#include "ci/ciReplay.hpp"
35
#include "ci/ciUtilities.inline.hpp"
36 37
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
38
#include "code/codeCache.hpp"
39 40 41
#include "code/scopeDesc.hpp"
#include "compiler/compileBroker.hpp"
#include "compiler/compileLog.hpp"
42
#include "compiler/disassembler.hpp"
P
pliden 已提交
43
#include "gc/shared/collectedHeap.inline.hpp"
44
#include "interpreter/linkResolver.hpp"
E
egahlin 已提交
45
#include "jfr/jfrEvents.hpp"
46 47
#include "memory/allocation.inline.hpp"
#include "memory/oopFactory.hpp"
48
#include "memory/resourceArea.hpp"
49
#include "memory/universe.hpp"
50 51 52
#include "oops/constantPool.inline.hpp"
#include "oops/cpCache.inline.hpp"
#include "oops/method.inline.hpp"
53
#include "oops/methodData.hpp"
54
#include "oops/objArrayKlass.hpp"
55
#include "oops/objArrayOop.inline.hpp"
56 57 58 59
#include "oops/oop.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "runtime/init.hpp"
#include "runtime/reflection.hpp"
60
#include "runtime/jniHandles.inline.hpp"
61
#include "runtime/safepointVerifiers.hpp"
62
#include "runtime/sharedRuntime.hpp"
63
#include "runtime/thread.inline.hpp"
64
#include "utilities/dtrace.hpp"
65
#include "utilities/macros.hpp"
66 67 68 69 70 71
#ifdef COMPILER1
#include "c1/c1_Runtime1.hpp"
#endif
#ifdef COMPILER2
#include "opto/runtime.hpp"
#endif
D
duke 已提交
72 73 74 75 76 77 78 79

// ciEnv
//
// This class is the top level broker for requests from the compiler
// to the VM.

ciObject*              ciEnv::_null_object_instance;

80 81 82
#define WK_KLASS_DEFN(name, ignore_s, ignore_o) ciInstanceKlass* ciEnv::_##name = NULL;
WK_KLASSES_DO(WK_KLASS_DEFN)
#undef WK_KLASS_DEFN
D
duke 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

ciSymbol*        ciEnv::_unloaded_cisymbol = NULL;
ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = NULL;
ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = NULL;

jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = NULL;
jobject ciEnv::_ArrayStoreException_handle = NULL;
jobject ciEnv::_ClassCastException_handle = NULL;

#ifndef PRODUCT
static bool firstEnv = true;
#endif /* PRODUCT */

// ------------------------------------------------------------------
// ciEnv::ciEnv
98 99
ciEnv::ciEnv(CompileTask* task, int system_dictionary_modification_counter)
  : _ciEnv_arena(mtCompiler) {
D
duke 已提交
100 101 102 103 104 105 106 107 108 109
  VM_ENTRY_MARK;

  // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
  thread->set_env(this);
  assert(ciEnv::current() == this, "sanity");

  _oop_recorder = NULL;
  _debug_info = NULL;
  _dependencies = NULL;
  _failure_reason = NULL;
110
  _inc_decompile_count_on_failure = true;
D
duke 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  _compilable = MethodCompilable;
  _break_at_compile = false;
  _compiler_data = NULL;
#ifndef PRODUCT
  assert(!firstEnv, "not initialized properly");
#endif /* !PRODUCT */

  _system_dictionary_modification_counter = system_dictionary_modification_counter;
  _num_inlined_bytecodes = 0;
  assert(task == NULL || thread->task() == task, "sanity");
  _task = task;
  _log = NULL;

  // Temporary buffer for creating symbols and such.
  _name_buffer = NULL;
  _name_buffer_len = 0;

  _arena   = &_ciEnv_arena;
  _factory = new (_arena) ciObjectFactory(_arena, 128);

  // Preload commonly referenced system ciObjects.

  // During VM initialization, these instances have not yet been created.
  // Assertions ensure that these instances are not accessed before
  // their initialization.

  assert(Universe::is_fully_initialized(), "should be complete");

  oop o = Universe::null_ptr_exception_instance();
  assert(o != NULL, "should have been initialized");
  _NullPointerException_instance = get_object(o)->as_instance();
  o = Universe::arithmetic_exception_instance();
  assert(o != NULL, "should have been initialized");
  _ArithmeticException_instance = get_object(o)->as_instance();

  _ArrayIndexOutOfBoundsException_instance = NULL;
  _ArrayStoreException_instance = NULL;
  _ClassCastException_instance = NULL;
149 150
  _the_null_string = NULL;
  _the_min_jint_string = NULL;
151 152 153 154 155

  _jvmti_can_hotswap_or_post_breakpoint = false;
  _jvmti_can_access_local_variables = false;
  _jvmti_can_post_on_exceptions = false;
  _jvmti_can_pop_frame = false;
D
duke 已提交
156 157
}

158
ciEnv::ciEnv(Arena* arena) : _ciEnv_arena(mtCompiler) {
D
duke 已提交
159 160 161 162 163 164 165 166 167 168 169 170
  ASSERT_IN_VM;

  // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc.
  CompilerThread* current_thread = CompilerThread::current();
  assert(current_thread->env() == NULL, "must be");
  current_thread->set_env(this);
  assert(ciEnv::current() == this, "sanity");

  _oop_recorder = NULL;
  _debug_info = NULL;
  _dependencies = NULL;
  _failure_reason = NULL;
171
  _inc_decompile_count_on_failure = true;
D
duke 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  _compilable = MethodCompilable_never;
  _break_at_compile = false;
  _compiler_data = NULL;
#ifndef PRODUCT
  assert(firstEnv, "must be first");
  firstEnv = false;
#endif /* !PRODUCT */

  _system_dictionary_modification_counter = 0;
  _num_inlined_bytecodes = 0;
  _task = NULL;
  _log = NULL;

  // Temporary buffer for creating symbols and such.
  _name_buffer = NULL;
  _name_buffer_len = 0;

  _arena   = arena;
  _factory = new (_arena) ciObjectFactory(_arena, 128);

  // Preload commonly referenced system ciObjects.

  // During VM initialization, these instances have not yet been created.
  // Assertions ensure that these instances are not accessed before
  // their initialization.

  assert(Universe::is_fully_initialized(), "must be");

200 201
  _NullPointerException_instance = NULL;
  _ArithmeticException_instance = NULL;
D
duke 已提交
202 203 204
  _ArrayIndexOutOfBoundsException_instance = NULL;
  _ArrayStoreException_instance = NULL;
  _ClassCastException_instance = NULL;
205 206
  _the_null_string = NULL;
  _the_min_jint_string = NULL;
207 208 209 210 211

  _jvmti_can_hotswap_or_post_breakpoint = false;
  _jvmti_can_access_local_variables = false;
  _jvmti_can_post_on_exceptions = false;
  _jvmti_can_pop_frame = false;
D
duke 已提交
212 213 214
}

ciEnv::~ciEnv() {
215 216 217 218 219 220 221
  GUARDED_VM_ENTRY(
      CompilerThread* current_thread = CompilerThread::current();
      _factory->remove_symbols();
      // Need safepoint to clear the env on the thread.  RedefineClasses might
      // be reading it.
      current_thread->set_env(NULL);
  )
D
duke 已提交
222 223
}

224 225 226 227 228 229 230 231
// ------------------------------------------------------------------
// Cache Jvmti state
void ciEnv::cache_jvmti_state() {
  VM_ENTRY_MARK;
  // Get Jvmti capabilities under lock to get consistant values.
  MutexLocker mu(JvmtiThreadState_lock);
  _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint();
  _jvmti_can_access_local_variables     = JvmtiExport::can_access_local_variables();
232
  _jvmti_can_post_on_exceptions         = JvmtiExport::can_post_on_exceptions();
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  _jvmti_can_pop_frame                  = JvmtiExport::can_pop_frame();
}

bool ciEnv::should_retain_local_variables() const {
  return _jvmti_can_access_local_variables || _jvmti_can_pop_frame;
}

bool ciEnv::jvmti_state_changed() const {
  if (!_jvmti_can_access_local_variables &&
      JvmtiExport::can_access_local_variables()) {
    return true;
  }
  if (!_jvmti_can_hotswap_or_post_breakpoint &&
      JvmtiExport::can_hotswap_or_post_breakpoint()) {
    return true;
  }
  if (!_jvmti_can_post_on_exceptions &&
      JvmtiExport::can_post_on_exceptions()) {
    return true;
  }
  if (!_jvmti_can_pop_frame &&
      JvmtiExport::can_pop_frame()) {
    return true;
  }
  return false;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
}

// ------------------------------------------------------------------
// Cache DTrace flags
void ciEnv::cache_dtrace_flags() {
  // Need lock?
  _dtrace_extended_probes = ExtendedDTraceProbes;
  if (_dtrace_extended_probes) {
    _dtrace_monitor_probes  = true;
    _dtrace_method_probes   = true;
    _dtrace_alloc_probes    = true;
  } else {
    _dtrace_monitor_probes  = DTraceMonitorProbes;
    _dtrace_method_probes   = DTraceMethodProbes;
    _dtrace_alloc_probes    = DTraceAllocProbes;
  }
}

D
duke 已提交
276 277
// ------------------------------------------------------------------
// helper for lazy exception creation
278
ciInstance* ciEnv::get_or_create_exception(jobject& handle, Symbol* name) {
D
duke 已提交
279 280 281
  VM_ENTRY_MARK;
  if (handle == NULL) {
    // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance.
282
    Klass* k = SystemDictionary::find(name, Handle(), Handle(), THREAD);
D
duke 已提交
283 284
    jobject objh = NULL;
    if (!HAS_PENDING_EXCEPTION && k != NULL) {
285
      oop obj = InstanceKlass::cast(k)->allocate_instance(THREAD);
D
duke 已提交
286
      if (!HAS_PENDING_EXCEPTION)
287
        objh = JNIHandles::make_global(Handle(THREAD, obj));
D
duke 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    }
    if (HAS_PENDING_EXCEPTION) {
      CLEAR_PENDING_EXCEPTION;
    } else {
      handle = objh;
    }
  }
  oop obj = JNIHandles::resolve(handle);
  return obj == NULL? NULL: get_object(obj)->as_instance();
}

ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
  if (_ArrayIndexOutOfBoundsException_instance == NULL) {
    _ArrayIndexOutOfBoundsException_instance
          = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle,
303
          vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
D
duke 已提交
304 305 306 307 308 309 310
  }
  return _ArrayIndexOutOfBoundsException_instance;
}
ciInstance* ciEnv::ArrayStoreException_instance() {
  if (_ArrayStoreException_instance == NULL) {
    _ArrayStoreException_instance
          = get_or_create_exception(_ArrayStoreException_handle,
311
          vmSymbols::java_lang_ArrayStoreException());
D
duke 已提交
312 313 314 315 316 317 318
  }
  return _ArrayStoreException_instance;
}
ciInstance* ciEnv::ClassCastException_instance() {
  if (_ClassCastException_instance == NULL) {
    _ClassCastException_instance
          = get_or_create_exception(_ClassCastException_handle,
319
          vmSymbols::java_lang_ClassCastException());
D
duke 已提交
320 321 322 323
  }
  return _ClassCastException_instance;
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
ciInstance* ciEnv::the_null_string() {
  if (_the_null_string == NULL) {
    VM_ENTRY_MARK;
    _the_null_string = get_object(Universe::the_null_string())->as_instance();
  }
  return _the_null_string;
}

ciInstance* ciEnv::the_min_jint_string() {
  if (_the_min_jint_string == NULL) {
    VM_ENTRY_MARK;
    _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance();
  }
  return _the_min_jint_string;
}

D
duke 已提交
340 341
// ------------------------------------------------------------------
// ciEnv::get_method_from_handle
342
ciMethod* ciEnv::get_method_from_handle(Method* method) {
D
duke 已提交
343
  VM_ENTRY_MARK;
344
  return get_metadata(method)->as_method();
D
duke 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
}

// ------------------------------------------------------------------
// ciEnv::array_element_offset_in_bytes
int ciEnv::array_element_offset_in_bytes(ciArray* a_h, ciObject* o_h) {
  VM_ENTRY_MARK;
  objArrayOop a = (objArrayOop)a_h->get_oop();
  assert(a->is_objArray(), "");
  int length = a->length();
  oop o = o_h->get_oop();
  for (int i = 0; i < length; i++) {
    if (a->obj_at(i) == o)  return i;
  }
  return -1;
}


// ------------------------------------------------------------------
// ciEnv::check_klass_accessiblity
//
// Note: the logic of this method should mirror the logic of
366
// ConstantPool::verify_constant_pool_resolve.
D
duke 已提交
367
bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass,
368
                                      Klass* resolved_klass) {
D
duke 已提交
369 370 371
  if (accessing_klass == NULL || !accessing_klass->is_loaded()) {
    return true;
  }
372
  if (accessing_klass->is_obj_array_klass()) {
D
duke 已提交
373 374 375 376 377 378
    accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass();
  }
  if (!accessing_klass->is_instance_klass()) {
    return true;
  }

379
  if (resolved_klass->is_objArray_klass()) {
D
duke 已提交
380
    // Find the element klass, if this is an array.
381
    resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
D
duke 已提交
382
  }
383
  if (resolved_klass->is_instance_klass()) {
A
alanb 已提交
384
    return (Reflection::verify_class_access(accessing_klass->get_Klass(),
385
                                            InstanceKlass::cast(resolved_klass),
A
alanb 已提交
386
                                            true) == Reflection::ACCESS_OK);
D
duke 已提交
387 388 389 390 391 392 393
  }
  return true;
}

// ------------------------------------------------------------------
// ciEnv::get_klass_by_name_impl
ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass,
394
                                       const constantPoolHandle& cpool,
D
duke 已提交
395 396 397 398 399 400
                                       ciSymbol* name,
                                       bool require_local) {
  ASSERT_IN_VM;
  EXCEPTION_CONTEXT;

  // Now we need to check the SystemDictionary
401
  Symbol* sym = name->get_symbol();
D
duke 已提交
402 403 404
  if (sym->byte_at(0) == 'L' &&
    sym->byte_at(sym->utf8_length()-1) == ';') {
    // This is a name from a signature.  Strip off the trimmings.
405 406 407 408 409
    // Call recursive to keep scope of strippedsym.
    TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
                    sym->utf8_length()-2,
                    KILL_COMPILE_ON_FATAL_(_unloaded_ciinstance_klass));
    ciSymbol* strippedname = get_symbol(strippedsym);
410
    return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local);
D
duke 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  }

  // Check for prior unloaded klass.  The SystemDictionary's answers
  // can vary over time but the compiler needs consistency.
  ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name);
  if (unloaded_klass != NULL) {
    if (require_local)  return NULL;
    return unloaded_klass;
  }

  Handle loader(THREAD, (oop)NULL);
  Handle domain(THREAD, (oop)NULL);
  if (accessing_klass != NULL) {
    loader = Handle(THREAD, accessing_klass->loader());
    domain = Handle(THREAD, accessing_klass->protection_domain());
  }

  // setup up the proper type to return on OOM
  ciKlass* fail_type;
  if (sym->byte_at(0) == '[') {
    fail_type = _unloaded_ciobjarrayklass;
  } else {
    fail_type = _unloaded_ciinstance_klass;
  }
435
  Klass* found_klass;
436
  {
437
    ttyUnlocker ttyul;  // release tty lock to avoid ordering problems
438
    MutexLocker ml(Compile_lock);
439
    Klass* kls;
440 441 442 443 444 445 446
    if (!require_local) {
      kls = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader,
                                                                       KILL_COMPILE_ON_FATAL_(fail_type));
    } else {
      kls = SystemDictionary::find_instance_or_array_klass(sym, loader, domain,
                                                           KILL_COMPILE_ON_FATAL_(fail_type));
    }
447
    found_klass = kls;
D
duke 已提交
448 449
  }

450 451 452 453 454 455 456 457 458 459
  // If we fail to find an array klass, look again for its element type.
  // The element type may be available either locally or via constraints.
  // In either case, if we can find the element type in the system dictionary,
  // we must build an array type around it.  The CI requires array klasses
  // to be loaded if their element klasses are loaded, except when memory
  // is exhausted.
  if (sym->byte_at(0) == '[' &&
      (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L')) {
    // We have an unloaded array.
    // Build it on the fly if the element class exists.
460 461 462 463
    TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
                                                 sym->utf8_length()-1,
                                                 KILL_COMPILE_ON_FATAL_(fail_type));

464 465 466
    // Get element ciKlass recursively.
    ciKlass* elem_klass =
      get_klass_by_name_impl(accessing_klass,
467
                             cpool,
468
                             get_symbol(elem_sym),
469 470 471 472 473 474 475
                             require_local);
    if (elem_klass != NULL && elem_klass->is_loaded()) {
      // Now make an array for it
      return ciObjArrayKlass::make_impl(elem_klass);
    }
  }

476
  if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) {
477 478 479
    // Look inside the constant pool for pre-resolved class entries.
    for (int i = cpool->length() - 1; i >= 1; i--) {
      if (cpool->tag_at(i).is_klass()) {
480
        Klass* kls = cpool->resolved_klass_at(i);
H
hseigel 已提交
481
        if (kls->name() == sym) {
482
          found_klass = kls;
483 484 485 486 487 488
          break;
        }
      }
    }
  }

489
  if (found_klass != NULL) {
D
duke 已提交
490
    // Found it.  Build a CI handle.
491
    return get_klass(found_klass);
D
duke 已提交
492 493 494
  }

  if (require_local)  return NULL;
495

D
duke 已提交
496 497 498 499 500 501 502 503 504 505 506
  // Not yet loaded into the VM, or not governed by loader constraints.
  // Make a CI representative for it.
  return get_unloaded_klass(accessing_klass, name);
}

// ------------------------------------------------------------------
// ciEnv::get_klass_by_name
ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass,
                                  ciSymbol* klass_name,
                                  bool require_local) {
  GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass,
507
                                                 constantPoolHandle(),
D
duke 已提交
508 509 510 511 512 513 514 515
                                                 klass_name,
                                                 require_local);)
}

// ------------------------------------------------------------------
// ciEnv::get_klass_by_index_impl
//
// Implementation of get_klass_by_index.
516
ciKlass* ciEnv::get_klass_by_index_impl(const constantPoolHandle& cpool,
D
duke 已提交
517
                                        int index,
518 519
                                        bool& is_accessible,
                                        ciInstanceKlass* accessor) {
D
duke 已提交
520
  EXCEPTION_CONTEXT;
521
  Klass* klass = NULL;
522
  Symbol* klass_name = NULL;
523 524 525 526 527

  if (cpool->tag_at(index).is_symbol()) {
    klass_name = cpool->symbol_at(index);
  } else {
    // Check if it's resolved if it's not a symbol constant pool entry.
528
    klass =  ConstantPool::klass_at_if_loaded(cpool, index);
D
duke 已提交
529
    // Try to look it up by name.
530
    if (klass == NULL) {
531
      klass_name = cpool->klass_name_at(index);
532
    }
533
  }
D
duke 已提交
534

535
  if (klass == NULL) {
D
duke 已提交
536 537
    // Not found in constant pool.  Use the name to do the lookup.
    ciKlass* k = get_klass_by_name_impl(accessor,
538
                                        cpool,
539
                                        get_symbol(klass_name),
D
duke 已提交
540 541 542 543
                                        false);
    // Calculate accessibility the hard way.
    if (!k->is_loaded()) {
      is_accessible = false;
544
    } else if (!oopDesc::equals(k->loader(), accessor->loader()) &&
545
               get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
D
duke 已提交
546 547 548 549
      // Loaded only remotely.  Not linked yet.
      is_accessible = false;
    } else {
      // Linked locally, and we must also check public/private, etc.
550
      is_accessible = check_klass_accessibility(accessor, k->get_Klass());
D
duke 已提交
551 552 553 554 555 556
    }
    return k;
  }

  // Check for prior unloaded klass.  The SystemDictionary's answers
  // can vary over time but the compiler needs consistency.
557
  ciSymbol* name = get_symbol(klass->name());
D
duke 已提交
558 559 560 561 562 563 564 565
  ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name);
  if (unloaded_klass != NULL) {
    is_accessible = false;
    return unloaded_klass;
  }

  // It is known to be accessible, since it was found in the constant pool.
  is_accessible = true;
566
  return get_klass(klass);
D
duke 已提交
567 568 569 570 571 572
}

// ------------------------------------------------------------------
// ciEnv::get_klass_by_index
//
// Get a klass from the constant pool.
573
ciKlass* ciEnv::get_klass_by_index(const constantPoolHandle& cpool,
D
duke 已提交
574
                                   int index,
575 576 577
                                   bool& is_accessible,
                                   ciInstanceKlass* accessor) {
  GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);)
D
duke 已提交
578 579 580 581 582 583
}

// ------------------------------------------------------------------
// ciEnv::get_constant_by_index_impl
//
// Implementation of get_constant_by_index().
584
ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
585
                                             int pool_index, int cache_index,
586
                                             ciInstanceKlass* accessor) {
587
  bool ignore_will_link;
D
duke 已提交
588
  EXCEPTION_CONTEXT;
589 590 591
  int index = pool_index;
  if (cache_index >= 0) {
    assert(index < 0, "only one kind of index at a time");
592
    index = cpool->object_to_cp_index(cache_index);
593
    oop obj = cpool->resolved_references()->obj_at(cache_index);
594
    if (obj != NULL) {
595
      if (oopDesc::equals(obj, Universe::the_null_sentinel())) {
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
        return ciConstant(T_OBJECT, get_object(NULL));
      }
      BasicType bt = T_OBJECT;
      if (cpool->tag_at(index).is_dynamic_constant())
        bt = FieldType::basic_type(cpool->uncached_signature_ref_at(index));
      if (is_reference_type(bt)) {
      } else {
        // we have to unbox the primitive value
        if (!is_java_primitive(bt))  return ciConstant();
        jvalue value;
        BasicType bt2 = java_lang_boxing_object::get_value(obj, &value);
        assert(bt2 == bt, "");
        switch (bt2) {
        case T_DOUBLE:  return ciConstant(value.d);
        case T_FLOAT:   return ciConstant(value.f);
        case T_LONG:    return ciConstant(value.j);
        case T_INT:     return ciConstant(bt2, value.i);
        case T_SHORT:   return ciConstant(bt2, value.s);
        case T_BYTE:    return ciConstant(bt2, value.b);
        case T_CHAR:    return ciConstant(bt2, value.c);
        case T_BOOLEAN: return ciConstant(bt2, value.z);
        default:  return ciConstant();
        }
      }
620
      ciObject* ciobj = get_object(obj);
621 622 623 624 625 626
      if (ciobj->is_array()) {
        return ciConstant(T_ARRAY, ciobj);
      } else {
        assert(ciobj->is_instance(), "should be an instance");
        return ciConstant(T_OBJECT, ciobj);
      }
627 628
    }
  }
D
duke 已提交
629 630 631 632 633 634 635 636 637
  constantTag tag = cpool->tag_at(index);
  if (tag.is_int()) {
    return ciConstant(T_INT, (jint)cpool->int_at(index));
  } else if (tag.is_long()) {
    return ciConstant((jlong)cpool->long_at(index));
  } else if (tag.is_float()) {
    return ciConstant((jfloat)cpool->float_at(index));
  } else if (tag.is_double()) {
    return ciConstant((jdouble)cpool->double_at(index));
638
  } else if (tag.is_string()) {
639
    oop string = NULL;
640
    assert(cache_index >= 0, "should have a cache index");
641
    if (cpool->is_pseudo_string_at(index)) {
642
      string = cpool->pseudo_string_at(index, cache_index);
643
    } else {
644
      string = cpool->string_at(index, cache_index, THREAD);
645 646 647 648 649
      if (HAS_PENDING_EXCEPTION) {
        CLEAR_PENDING_EXCEPTION;
        record_out_of_memory_failure();
        return ciConstant();
      }
D
duke 已提交
650 651
    }
    ciObject* constant = get_object(string);
652 653 654 655 656 657
    if (constant->is_array()) {
      return ciConstant(T_ARRAY, constant);
    } else {
      assert (constant->is_instance(), "must be an instance, or not? ");
      return ciConstant(T_OBJECT, constant);
    }
D
duke 已提交
658 659
  } else if (tag.is_klass() || tag.is_unresolved_klass()) {
    // 4881222: allow ldc to take a class type
660
    ciKlass* klass = get_klass_by_index_impl(cpool, index, ignore_will_link, accessor);
D
duke 已提交
661 662 663 664 665 666 667
    if (HAS_PENDING_EXCEPTION) {
      CLEAR_PENDING_EXCEPTION;
      record_out_of_memory_failure();
      return ciConstant();
    }
    assert (klass->is_instance_klass() || klass->is_array_klass(),
            "must be an instance or array klass ");
668 669 670
    return ciConstant(T_OBJECT, klass->java_mirror());
  } else if (tag.is_method_type()) {
    // must execute Java code to link this CP entry into cache[i].f1
671
    ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index));
672 673 674 675 676 677 678
    ciObject* ciobj = get_unloaded_method_type_constant(signature);
    return ciConstant(T_OBJECT, ciobj);
  } else if (tag.is_method_handle()) {
    // must execute Java code to link this CP entry into cache[i].f1
    int ref_kind        = cpool->method_handle_ref_kind_at(index);
    int callee_index    = cpool->method_handle_klass_index_at(index);
    ciKlass* callee     = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor);
679 680
    ciSymbol* name      = get_symbol(cpool->method_handle_name_ref_at(index));
    ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index));
681 682
    ciObject* ciobj     = get_unloaded_method_handle_constant(callee, name, signature, ref_kind);
    return ciConstant(T_OBJECT, ciobj);
683 684
  } else if (tag.is_dynamic_constant()) {
    return ciConstant();
D
duke 已提交
685 686 687 688 689 690 691 692 693 694 695 696
  } else {
    ShouldNotReachHere();
    return ciConstant();
  }
}

// ------------------------------------------------------------------
// ciEnv::get_constant_by_index
//
// Pull a constant out of the constant pool.  How appropriate.
//
// Implementation note: this query is currently in no way cached.
697
ciConstant ciEnv::get_constant_by_index(const constantPoolHandle& cpool,
698
                                        int pool_index, int cache_index,
699
                                        ciInstanceKlass* accessor) {
700
  GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);)
D
duke 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
}

// ------------------------------------------------------------------
// ciEnv::get_field_by_index_impl
//
// Implementation of get_field_by_index.
//
// Implementation note: the results of field lookups are cached
// in the accessor klass.
ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor,
                                        int index) {
  ciConstantPoolCache* cache = accessor->field_cache();
  if (cache == NULL) {
    ciField* field = new (arena()) ciField(accessor, index);
    return field;
  } else {
    ciField* field = (ciField*)cache->get(index);
    if (field == NULL) {
      field = new (arena()) ciField(accessor, index);
      cache->insert(index, field);
    }
    return field;
  }
}

// ------------------------------------------------------------------
// ciEnv::get_field_by_index
//
// Get a field by index from a klass's constant pool.
ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor,
                                   int index) {
  GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);)
}

// ------------------------------------------------------------------
// ciEnv::lookup_method
//
// Perform an appropriate method lookup based on accessor, holder,
// name, signature, and bytecode.
740 741 742 743 744 745 746 747 748
Method* ciEnv::lookup_method(ciInstanceKlass* accessor,
                             ciKlass*         holder,
                             Symbol*          name,
                             Symbol*          sig,
                             Bytecodes::Code  bc,
                             constantTag      tag) {
  // Accessibility checks are performed in ciEnv::get_method_by_index_impl.
  assert(check_klass_accessibility(accessor, holder->get_Klass()), "holder not accessible");

749 750
  InstanceKlass* accessor_klass = accessor->get_instanceKlass();
  Klass* holder_klass = holder->get_Klass();
D
duke 已提交
751
  methodHandle dest_method;
752
  LinkInfo link_info(holder_klass, name, sig, accessor_klass, LinkInfo::needs_access_check, tag);
D
duke 已提交
753 754 755
  switch (bc) {
  case Bytecodes::_invokestatic:
    dest_method =
C
coleenp 已提交
756
      LinkResolver::resolve_static_call_or_null(link_info);
D
duke 已提交
757 758 759
    break;
  case Bytecodes::_invokespecial:
    dest_method =
C
coleenp 已提交
760
      LinkResolver::resolve_special_call_or_null(link_info);
D
duke 已提交
761 762 763
    break;
  case Bytecodes::_invokeinterface:
    dest_method =
C
coleenp 已提交
764
      LinkResolver::linktime_resolve_interface_method_or_null(link_info);
D
duke 已提交
765 766 767
    break;
  case Bytecodes::_invokevirtual:
    dest_method =
C
coleenp 已提交
768
      LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
D
duke 已提交
769 770 771 772 773 774 775 776 777 778
    break;
  default: ShouldNotReachHere();
  }

  return dest_method();
}


// ------------------------------------------------------------------
// ciEnv::get_method_by_index_impl
779
ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool,
780 781
                                          int index, Bytecodes::Code bc,
                                          ciInstanceKlass* accessor) {
782
  if (bc == Bytecodes::_invokedynamic) {
783 784
    ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index);
    bool is_resolved = !cpce->is_f1_null();
785 786 787 788 789 790 791 792 793
    // FIXME: code generation could allow for null (unlinked) call site
    // The call site could be made patchable as follows:
    // Load the appendix argument from the constant pool.
    // Test the appendix argument and jump to a known deopt routine if it is null.
    // Jump through a patchable call site, which is initially a deopt routine.
    // Patch the call site to the nmethod entry point of the static compiled lambda form.
    // As with other two-component call sites, both values must be independently verified.

    if (is_resolved) {
794 795 796 797
      // Get the invoker Method* from the constant pool.
      // (The appendix argument, if any, will be noted in the method's signature.)
      Method* adapter = cpce->f1_as_method();
      return get_method(adapter);
798 799
    }

800
    // Fake a method that is equivalent to a declared method.
801
    ciInstanceKlass* holder    = get_instance_klass(SystemDictionary::MethodHandle_klass());
802
    ciSymbol*        name      = ciSymbol::invokeBasic_name();
803 804
    ciSymbol*        signature = get_symbol(cpool->signature_ref_at(index));
    return get_unloaded_method(holder, name, signature, accessor);
805 806 807 808 809 810 811 812 813 814
  } else {
    const int holder_index = cpool->klass_ref_index_at(index);
    bool holder_is_accessible;
    ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);

    // Get the method's name and signature.
    Symbol* name_sym = cpool->name_ref_at(index);
    Symbol* sig_sym  = cpool->signature_ref_at(index);

    if (cpool->has_preresolution()
P
psandoz 已提交
815
        || ((holder == ciEnv::MethodHandle_klass() || holder == ciEnv::VarHandle_klass()) &&
816
            MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) {
817 818 819 820 821 822 823 824 825
      // Short-circuit lookups for JSR 292-related call sites.
      // That is, do not rely only on name-based lookups, because they may fail
      // if the names are not resolvable in the boot class loader (7056328).
      switch (bc) {
      case Bytecodes::_invokevirtual:
      case Bytecodes::_invokeinterface:
      case Bytecodes::_invokespecial:
      case Bytecodes::_invokestatic:
        {
826
          Method* m = ConstantPool::method_at_if_loaded(cpool, index);
827
          if (m != NULL) {
828
            return get_method(m);
829 830 831
          }
        }
        break;
832 833
      default:
        break;
834 835 836 837
      }
    }

    if (holder_is_accessible) {  // Our declared holder is loaded.
838 839
      constantTag tag = cpool->tag_ref_at(index);
      assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
840
      Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
841 842
      if (m != NULL &&
          (bc == Bytecodes::_invokestatic
843 844
           ?  m->method_holder()->is_not_initialized()
           : !m->method_holder()->is_loaded())) {
845 846
        m = NULL;
      }
847 848 849 850 851
#ifdef ASSERT
      if (m != NULL && ReplayCompiles && !ciReplay::is_loaded(m)) {
        m = NULL;
      }
#endif
852 853
      if (m != NULL) {
        // We found the method.
854
        return get_method(m);
855 856
      }
    }
857

858 859 860 861 862
    // Either the declared holder was not loaded, or the method could
    // not be found.  Create a dummy ciMethod to represent the failed
    // lookup.
    ciSymbol* name      = get_symbol(name_sym);
    ciSymbol* signature = get_symbol(sig_sym);
863
    return get_unloaded_method(holder, name, signature, accessor);
864
  }
865 866 867
}


D
duke 已提交
868 869 870 871 872 873 874 875 876 877 878
// ------------------------------------------------------------------
// ciEnv::get_instance_klass_for_declared_method_holder
ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) {
  // For the case of <array>.clone(), the method holder can be a ciArrayKlass
  // instead of a ciInstanceKlass.  For that case simply pretend that the
  // declared holder is Object.clone since that's where the call will bottom out.
  // A more correct fix would trickle out through many interfaces in CI,
  // requiring ciInstanceKlass* to become ciKlass* and many more places would
  // require checks to make sure the expected type was found.  Given that this
  // only occurs for clone() the more extensive fix seems like overkill so
  // instead we simply smear the array type into Object.
879
  guarantee(method_holder != NULL, "no method holder");
D
duke 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892
  if (method_holder->is_instance_klass()) {
    return method_holder->as_instance_klass();
  } else if (method_holder->is_array_klass()) {
    return current()->Object_klass();
  } else {
    ShouldNotReachHere();
  }
  return NULL;
}


// ------------------------------------------------------------------
// ciEnv::get_method_by_index
893
ciMethod* ciEnv::get_method_by_index(const constantPoolHandle& cpool,
894 895
                                     int index, Bytecodes::Code bc,
                                     ciInstanceKlass* accessor) {
896
  GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);)
D
duke 已提交
897 898
}

899

D
duke 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
// ------------------------------------------------------------------
// ciEnv::name_buffer
char *ciEnv::name_buffer(int req_len) {
  if (_name_buffer_len < req_len) {
    if (_name_buffer == NULL) {
      _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len);
      _name_buffer_len = req_len;
    } else {
      _name_buffer =
        (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
      _name_buffer_len = req_len;
    }
  }
  return _name_buffer;
}

// ------------------------------------------------------------------
// ciEnv::is_in_vm
bool ciEnv::is_in_vm() {
  return JavaThread::current()->thread_state() == _thread_in_vm;
}

bool ciEnv::system_dictionary_modification_counter_changed() {
  return _system_dictionary_modification_counter != SystemDictionary::number_of_modifications();
}

// ------------------------------------------------------------------
927 928 929 930 931
// ciEnv::validate_compile_task_dependencies
//
// Check for changes during compilation (e.g. class loads, evolution,
// breakpoints, call site invalidation).
void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
D
duke 已提交
932 933 934
  if (failing())  return;  // no need for further checks

  bool counter_changed = system_dictionary_modification_counter_changed();
935 936 937 938 939 940 941
  Dependencies::DepType result = dependencies()->validate_dependencies(_task, counter_changed);
  if (result != Dependencies::end_marker) {
    if (result == Dependencies::call_site_target_value) {
      _inc_decompile_count_on_failure = false;
      record_failure("call site target change");
    } else if (Dependencies::is_klass_type(result)) {
      record_failure("concurrent class loading");
942 943
    } else {
      record_failure("invalid non-klass dependency");
944
    }
D
duke 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  }
}

// ------------------------------------------------------------------
// ciEnv::register_method
void ciEnv::register_method(ciMethod* target,
                            int entry_bci,
                            CodeOffsets* offsets,
                            int orig_pc_offset,
                            CodeBuffer* code_buffer,
                            int frame_words,
                            OopMapSet* oop_map_set,
                            ExceptionHandlerTable* handler_table,
                            ImplicitExceptionTable* inc_table,
                            AbstractCompiler* compiler,
960
                            bool has_unsafe_access,
961 962
                            bool has_wide_vectors,
                            RTMState  rtm_state) {
D
duke 已提交
963 964 965 966 967 968 969 970
  VM_ENTRY_MARK;
  nmethod* nm = NULL;
  {
    // To prevent compile queue updates.
    MutexLocker locker(MethodCompileQueue_lock, THREAD);

    // Prevent SystemDictionary::add_to_hierarchy from running
    // and invalidating our dependencies until we install this method.
971
    // No safepoints are allowed. Otherwise, class redefinition can occur in between.
D
duke 已提交
972
    MutexLocker ml(Compile_lock);
973
    NoSafepointVerifier nsv;
D
duke 已提交
974

975
    // Change in Jvmti state may invalidate compilation.
976
    if (!failing() && jvmti_state_changed()) {
977
      record_failure("Jvmti state change invalidated dependencies");
D
duke 已提交
978 979
    }

980 981 982 983 984 985 986
    // Change in DTrace flags may invalidate compilation.
    if (!failing() &&
        ( (!dtrace_extended_probes() && ExtendedDTraceProbes) ||
          (!dtrace_method_probes() && DTraceMethodProbes) ||
          (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
      record_failure("DTrace flags change invalidated dependencies");
    }
D
duke 已提交
987

988 989 990 991 992 993 994 995 996
    if (!failing()) {
      if (log() != NULL) {
        // Log the dependencies which this compilation declares.
        dependencies()->log_all_dependencies();
      }

      // Encode the dependencies now, so we can check them right away.
      dependencies()->encode_content_bytes();

997 998
      // Check for {class loads, evolution, breakpoints, ...} during compilation
      validate_compile_task_dependencies(target);
999
    }
D
duke 已提交
1000

1001
    methodHandle method(THREAD, target->get_Method());
D
duke 已提交
1002

1003 1004 1005 1006 1007 1008 1009 1010 1011
#if INCLUDE_RTM_OPT
    if (!failing() && (rtm_state != NoRTM) &&
        (method()->method_data() != NULL) &&
        (method()->method_data()->rtm_state() != rtm_state)) {
      // Preemptive decompile if rtm state was changed.
      record_failure("RTM state change invalidated rtm code");
    }
#endif

D
duke 已提交
1012 1013
    if (failing()) {
      // While not a true deoptimization, it is a preemptive decompile.
1014
      MethodData* mdo = method()->method_data();
1015
      if (mdo != NULL && _inc_decompile_count_on_failure) {
D
duke 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
        mdo->inc_decompile_count();
      }

      // All buffers in the CodeBuffer are allocated in the CodeCache.
      // If the code buffer is created on each compile attempt
      // as in C2, then it must be freed.
      code_buffer->free_blob();
      return;
    }

    assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
    assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");

    nm =  nmethod::new_nmethod(method,
                               compile_id(),
                               entry_bci,
                               offsets,
                               orig_pc_offset,
                               debug_info(), dependencies(), code_buffer,
                               frame_words, oop_map_set,
                               handler_table, inc_table,
1037 1038
                               compiler, task()->comp_level());

D
duke 已提交
1039 1040 1041
    // Free codeBlobs
    code_buffer->free_blob();

1042
    if (nm != NULL) {
D
duke 已提交
1043
      nm->set_has_unsafe_access(has_unsafe_access);
1044
      nm->set_has_wide_vectors(has_wide_vectors);
1045 1046 1047
#if INCLUDE_RTM_OPT
      nm->set_rtm_state(rtm_state);
#endif
D
duke 已提交
1048 1049 1050

      // Record successful registration.
      // (Put nm into the task handle *before* publishing to the Java heap.)
1051 1052 1053
      if (task() != NULL) {
        task()->set_code(nm);
      }
D
duke 已提交
1054 1055

      if (entry_bci == InvocationEntryBci) {
I
iveresov 已提交
1056 1057
        if (TieredCompilation) {
          // If there is an old version we're done with it
1058
          CompiledMethod* old = method->code();
I
iveresov 已提交
1059 1060 1061 1062 1063
          if (TraceMethodReplacement && old != NULL) {
            ResourceMark rm;
            char *method_name = method->name_and_sig_as_C_string();
            tty->print_cr("Replacing method %s", method_name);
          }
1064
          if (old != NULL) {
1065
            old->make_not_used();
I
iveresov 已提交
1066
          }
D
duke 已提交
1067
        }
1068
        if (TraceNMethodInstalls) {
D
duke 已提交
1069 1070 1071 1072
          ResourceMark rm;
          char *method_name = method->name_and_sig_as_C_string();
          ttyLocker ttyl;
          tty->print_cr("Installing method (%d) %s ",
1073
                        task()->comp_level(),
D
duke 已提交
1074 1075 1076 1077 1078
                        method_name);
        }
        // Allow the code to be executed
        method->set_code(method, nm);
      } else {
1079
        if (TraceNMethodInstalls) {
D
duke 已提交
1080 1081 1082 1083
          ResourceMark rm;
          char *method_name = method->name_and_sig_as_C_string();
          ttyLocker ttyl;
          tty->print_cr("Installing osr method (%d) %s @ %d",
1084
                        task()->comp_level(),
D
duke 已提交
1085 1086 1087
                        method_name,
                        entry_bci);
        }
1088
        method->method_holder()->add_osr_nmethod(nm);
D
duke 已提交
1089
      }
1090
      nm->make_in_use();
D
duke 已提交
1091
    }
1092 1093
  }  // safepoints are allowed again

D
duke 已提交
1094
  if (nm != NULL) {
1095
    // JVMTI -- compiled method notification (must be done outside lock)
D
duke 已提交
1096
    nm->post_compiled_method_load_event();
1097
  } else {
1098
    // The CodeCache is full.
1099
    record_failure("code cache is full");
D
duke 已提交
1100 1101 1102 1103 1104 1105 1106 1107
  }
}


// ------------------------------------------------------------------
// ciEnv::find_system_klass
ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
  VM_ENTRY_MARK;
1108
  return get_klass_by_name_impl(NULL, constantPoolHandle(), klass_name, false);
D
duke 已提交
1109 1110 1111 1112 1113
}

// ------------------------------------------------------------------
// ciEnv::comp_level
int ciEnv::comp_level() {
I
iveresov 已提交
1114
  if (task() == NULL)  return CompLevel_highest_tier;
D
duke 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
  return task()->comp_level();
}

// ------------------------------------------------------------------
// ciEnv::compile_id
uint ciEnv::compile_id() {
  if (task() == NULL)  return 0;
  return task()->compile_id();
}

// ------------------------------------------------------------------
// ciEnv::notice_inlined_method()
void ciEnv::notice_inlined_method(ciMethod* method) {
1128
  _num_inlined_bytecodes += method->code_size_for_inlining();
D
duke 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
}

// ------------------------------------------------------------------
// ciEnv::num_inlined_bytecodes()
int ciEnv::num_inlined_bytecodes() const {
  return _num_inlined_bytecodes;
}

// ------------------------------------------------------------------
// ciEnv::record_failure()
void ciEnv::record_failure(const char* reason) {
  if (_failure_reason == NULL) {
    // Record the first failure reason.
    _failure_reason = reason;
  }
}

1146
void ciEnv::report_failure(const char* reason) {
1147
  EventCompilationFailure event;
1148
  if (event.should_commit()) {
1149 1150
    event.set_compileId(compile_id());
    event.set_failureMessage(reason);
1151 1152 1153 1154
    event.commit();
  }
}

D
duke 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
// ------------------------------------------------------------------
// ciEnv::record_method_not_compilable()
void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) {
  int new_compilable =
    all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ;

  // Only note transitions to a worse state
  if (new_compilable > _compilable) {
    if (log() != NULL) {
      if (all_tiers) {
        log()->elem("method_not_compilable");
      } else {
V
vlivanov 已提交
1167 1168
        log()->elem("method_not_compilable_at_tier level='%d'",
                    current()->task()->comp_level());
D
duke 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
      }
    }
    _compilable = new_compilable;

    // Reset failure reason; this one is more important.
    _failure_reason = NULL;
    record_failure(reason);
  }
}

// ------------------------------------------------------------------
// ciEnv::record_out_of_memory_failure()
void ciEnv::record_out_of_memory_failure() {
  // If memory is low, we stop compiling methods.
  record_method_not_compilable("out of memory");
}
1185

1186 1187 1188 1189
ciInstance* ciEnv::unloaded_ciinstance() {
  GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();)
}

1190 1191 1192 1193 1194
// ------------------------------------------------------------------
// ciEnv::dump_replay_data*

// Don't change thread state and acquire any locks.
// Safe to call from VM error reporter.
1195 1196 1197

void ciEnv::dump_compile_data(outputStream* out) {
  CompileTask* task = this->task();
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  if (task) {
    Method* method = task->method();
    int entry_bci = task->osr_bci();
    int comp_level = task->comp_level();
    out->print("compile %s %s %s %d %d",
               method->klass_name()->as_quoted_ascii(),
               method->name()->as_quoted_ascii(),
               method->signature()->as_quoted_ascii(),
               entry_bci, comp_level);
    if (compiler_data() != NULL) {
      if (is_c2_compile(comp_level)) {
1209
#ifdef COMPILER2
1210 1211
        // Dump C2 inlining data.
        ((Compile*)compiler_data())->dump_inline_data(out);
1212
#endif
1213
      } else if (is_c1_compile(comp_level)) {
1214
#ifdef COMPILER1
1215 1216
        // Dump C1 inlining data.
        ((Compilation*)compiler_data())->dump_inline_data(out);
1217
#endif
1218
      }
1219
    }
1220
    out->cr();
1221 1222 1223
  }
}

1224
void ciEnv::dump_replay_data_unsafe(outputStream* out) {
1225
  ResourceMark rm;
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
#if INCLUDE_JVMTI
  out->print_cr("JvmtiExport can_access_local_variables %d",     _jvmti_can_access_local_variables);
  out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint);
  out->print_cr("JvmtiExport can_post_on_exceptions %d",         _jvmti_can_post_on_exceptions);
#endif // INCLUDE_JVMTI

  GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata();
  out->print_cr("# %d ciObject found", objects->length());
  for (int i = 0; i < objects->length(); i++) {
    objects->at(i)->dump_replay_data(out);
  }
1237
  dump_compile_data(out);
1238 1239
  out->flush();
}
1240 1241 1242 1243 1244 1245 1246

void ciEnv::dump_replay_data(outputStream* out) {
  GUARDED_VM_ENTRY(
    MutexLocker ml(Compile_lock);
    dump_replay_data_unsafe(out);
  )
}
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

void ciEnv::dump_replay_data(int compile_id) {
  static char buffer[O_BUFLEN];
  int ret = jio_snprintf(buffer, O_BUFLEN, "replay_pid%p_compid%d.log", os::current_process_id(), compile_id);
  if (ret > 0) {
    int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
    if (fd != -1) {
      FILE* replay_data_file = os::open(fd, "w");
      if (replay_data_file != NULL) {
        fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
        dump_replay_data(&replay_data_stream);
1258
        tty->print_cr("# Compiler replay data is saved as: %s", buffer);
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
      } else {
        tty->print_cr("# Can't open file to dump replay data.");
      }
    }
  }
}

void ciEnv::dump_inline_data(int compile_id) {
  static char buffer[O_BUFLEN];
  int ret = jio_snprintf(buffer, O_BUFLEN, "inline_pid%p_compid%d.log", os::current_process_id(), compile_id);
  if (ret > 0) {
    int fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
    if (fd != -1) {
      FILE* inline_data_file = os::open(fd, "w");
      if (inline_data_file != NULL) {
        fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
        GUARDED_VM_ENTRY(
          MutexLocker ml(Compile_lock);
          dump_compile_data(&replay_data_stream);
        )
        replay_data_stream.flush();
        tty->print("# Compiler inline data is saved as: ");
1281
        tty->print_cr("%s", buffer);
1282 1283 1284 1285 1286 1287
      } else {
        tty->print_cr("# Can't open file to dump inline data.");
      }
    }
  }
}