compiledIC.cpp 27.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, 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 26 27 28 29 30 31 32 33
#include "precompiled.hpp"
#include "classfile/systemDictionary.hpp"
#include "code/codeCache.hpp"
#include "code/compiledIC.hpp"
#include "code/icBuffer.hpp"
#include "code/nmethod.hpp"
#include "code/vtableStubs.hpp"
#include "interpreter/interpreter.hpp"
#include "interpreter/linkResolver.hpp"
34
#include "memory/metadataFactory.hpp"
35
#include "memory/oopFactory.hpp"
36
#include "oops/method.hpp"
37
#include "oops/oop.inline.hpp"
38
#include "oops/symbol.hpp"
39 40 41 42
#include "runtime/icache.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/stubRoutines.hpp"
#include "utilities/events.hpp"
D
duke 已提交
43 44 45 46 47 48


// Every time a compiled IC is changed or its type is being accessed,
// either the CompiledIC_lock must be set or we must be at a safe point.


49 50 51 52 53 54 55
// Release the CompiledICHolder* associated with this call site is there is one.
void CompiledIC::cleanup_call_site(virtual_call_Relocation* call_site) {
  // This call site might have become stale so inspect it carefully.
  NativeCall* call = nativeCall_at(call_site->addr());
  if (is_icholder_entry(call->destination())) {
    NativeMovConstReg* value = nativeMovConstReg_at(call_site->cached_value());
    InlineCacheBuffer::queue_for_release((CompiledICHolder*)value->data());
D
duke 已提交
56
  }
57
}
D
duke 已提交
58 59


60 61 62 63
bool CompiledIC::is_icholder_call_site(virtual_call_Relocation* call_site) {
  // This call site might have become stale so inspect it carefully.
  NativeCall* call = nativeCall_at(call_site->addr());
  return is_icholder_entry(call->destination());
D
duke 已提交
64 65 66
}


67 68 69 70 71
//-----------------------------------------------------------------------------
// Low-level access to an inline cache. Private, since they might not be
// MT-safe to use.

void* CompiledIC::cached_value() const {
D
duke 已提交
72
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
73
  assert (!is_optimized(), "an optimized virtual call does not have a cached metadata");
D
duke 已提交
74 75

  if (!is_in_transition_state()) {
76 77
    void* data = (void*)_value->data();
    // If we let the metadata value here be initialized to zero...
D
duke 已提交
78
    assert(data != NULL || Universe::non_oop_word() == NULL,
79 80
           "no raw nulls in CompiledIC metadatas, because of patching races");
    return (data == (void*)Universe::non_oop_word()) ? NULL : data;
D
duke 已提交
81
  } else {
82
    return InlineCacheBuffer::cached_value_for((CompiledIC *)this);
D
duke 已提交
83 84 85 86
  }
}


87
void CompiledIC::internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder) {
D
duke 已提交
88 89
  assert(entry_point != NULL, "must set legal entry point");
  assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  assert (!is_optimized() || cache == NULL, "an optimized virtual call does not have a cached metadata");
  assert (cache == NULL || cache != (Metadata*)badOopVal, "invalid metadata");

  assert(!is_icholder || is_icholder_entry(entry_point), "must be");

  // Don't use ic_destination for this test since that forwards
  // through ICBuffer instead of returning the actual current state of
  // the CompiledIC.
  if (is_icholder_entry(_ic_call->destination())) {
    // When patching for the ICStub case the cached value isn't
    // overwritten until the ICStub copied into the CompiledIC during
    // the next safepoint.  Make sure that the CompiledICHolder* is
    // marked for release at this point since it won't be identifiable
    // once the entry point is overwritten.
    InlineCacheBuffer::queue_for_release((CompiledICHolder*)_value->data());
  }

D
duke 已提交
107 108 109
  if (TraceCompiledIC) {
    tty->print("  ");
    print_compiled_ic();
110 111 112 113 114 115 116 117
    tty->print(" changing destination to " INTPTR_FORMAT, entry_point);
    if (!is_optimized()) {
      tty->print(" changing cached %s to " INTPTR_FORMAT, is_icholder ? "icholder" : "metadata", (address)cache);
    }
    if (is_icstub) {
      tty->print(" (icstub)");
    }
    tty->cr();
D
duke 已提交
118
  }
119 120

  {
D
duke 已提交
121 122 123 124 125 126 127 128
  MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
#ifdef ASSERT
  CodeBlob* cb = CodeCache::find_blob_unsafe(_ic_call);
  assert(cb != NULL && cb->is_nmethod(), "must be nmethod");
#endif
  _ic_call->set_destination_mt_safe(entry_point);
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  if (is_optimized() || is_icstub) {
    // Optimized call sites don't have a cache value and ICStub call
    // sites only change the entry point.  Changing the value in that
    // case could lead to MT safety issues.
    assert(cache == NULL, "must be null");
    return;
  }

  if (cache == NULL)  cache = (void*)Universe::non_oop_word();

  _value->set_data((intptr_t)cache);
}


void CompiledIC::set_ic_destination(ICStub* stub) {
  internal_set_ic_destination(stub->code_begin(), true, NULL, false);
}


D
duke 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

address CompiledIC::ic_destination() const {
 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
 if (!is_in_transition_state()) {
   return _ic_call->destination();
 } else {
   return InlineCacheBuffer::ic_destination_for((CompiledIC *)this);
 }
}


bool CompiledIC::is_in_transition_state() const {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
  return InlineCacheBuffer::contains(_ic_call->destination());
}


165 166 167 168 169
bool CompiledIC::is_icholder_call() const {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
  return !_is_optimized && is_icholder_entry(ic_destination());
}

D
duke 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
// Returns native address of 'call' instruction in inline-cache. Used by
// the InlineCacheBuffer when it needs to find the stub.
address CompiledIC::stub_address() const {
  assert(is_in_transition_state(), "should only be called when we are in a transition state");
  return _ic_call->destination();
}


//-----------------------------------------------------------------------------
// High-level access to an inline cache. Guaranteed to be MT-safe.


void CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS) {
  methodHandle method = call_info->selected_method();
  bool is_invoke_interface = (bytecode == Bytecodes::_invokeinterface && !call_info->has_vtable_index());
  assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
  assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic");
  assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?");

  address entry;
  if (is_invoke_interface) {
    int index = klassItable::compute_itable_index(call_info->resolved_method()());
    entry = VtableStubs::create_stub(false, index, method());
    assert(entry != NULL, "entry not computed");
194
    Klass* k = call_info->resolved_method()->method_holder();
D
duke 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    assert(Klass::cast(k)->is_interface(), "sanity check");
    InlineCacheBuffer::create_transition_stub(this, k, entry);
  } else {
    // Can be different than method->vtable_index(), due to package-private etc.
    int vtable_index = call_info->vtable_index();
    entry = VtableStubs::create_stub(true, vtable_index, method());
    InlineCacheBuffer::create_transition_stub(this, method(), entry);
  }

  if (TraceICs) {
    ResourceMark rm;
    tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
                   instruction_address(), method->print_value_string(), entry);
  }

  // We can't check this anymore. With lazy deopt we could have already
  // cleaned this IC entry before we even return. This is possible if
  // we ran out of space in the inline cache buffer trying to do the
  // set_next and we safepointed to free up space. This is a benign
  // race because the IC entry was complete when we safepointed so
  // cleaning it immediately is harmless.
  // assert(is_megamorphic(), "sanity check");
}


// true if destination is megamorphic stub
bool CompiledIC::is_megamorphic() const {
  assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
  assert(!is_optimized(), "an optimized call cannot be megamorphic");

225
  // Cannot rely on cached_value. It is either an interface or a method.
D
duke 已提交
226 227 228 229 230 231 232 233 234 235 236
  return VtableStubs::is_entry_point(ic_destination());
}

bool CompiledIC::is_call_to_compiled() const {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");

  // Use unsafe, since an inline cache might point to a zombie method. However, the zombie
  // method is guaranteed to still exist, since we only remove methods after all inline caches
  // has been cleaned up
  CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
  bool is_monomorphic = (cb != NULL && cb->is_nmethod());
237
  // Check that the cached_value is a klass for non-optimized monomorphic calls
D
duke 已提交
238
  // This assertion is invalid for compiler1: a call that does not look optimized (no static stub) can be used
239
  // for calling directly to vep without using the inline cache (i.e., cached_value == NULL)
D
duke 已提交
240 241 242 243 244 245
#ifdef ASSERT
  CodeBlob* caller = CodeCache::find_blob_unsafe(instruction_address());
  bool is_c1_method = caller->is_compiled_by_c1();
  assert( is_c1_method ||
         !is_monomorphic ||
         is_optimized() ||
246
         (cached_metadata() != NULL && cached_metadata()->is_klass()), "sanity check");
D
duke 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
#endif // ASSERT
  return is_monomorphic;
}


bool CompiledIC::is_call_to_interpreted() const {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
  // Call to interpreter if destination is either calling to a stub (if it
  // is optimized), or calling to an I2C blob
  bool is_call_to_interpreted = false;
  if (!is_optimized()) {
    // must use unsafe because the destination can be a zombie (and we're cleaning)
    // and the print_compiled_ic code wants to know if site (in the non-zombie)
    // is to the interpreter.
    CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
    is_call_to_interpreted = (cb != NULL && cb->is_adapter_blob());
263
    assert(!is_call_to_interpreted || (is_icholder_call() && cached_icholder() != NULL), "sanity check");
D
duke 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  } else {
    // Check if we are calling into our own codeblob (i.e., to a stub)
    CodeBlob* cb = CodeCache::find_blob(_ic_call->instruction_address());
    address dest = ic_destination();
#ifdef ASSERT
    {
      CodeBlob* db = CodeCache::find_blob_unsafe(dest);
      assert(!db->is_adapter_blob(), "must use stub!");
    }
#endif /* ASSERT */
    is_call_to_interpreted = cb->contains(dest);
  }
  return is_call_to_interpreted;
}


void CompiledIC::set_to_clean() {
  assert(SafepointSynchronize::is_at_safepoint() || CompiledIC_lock->is_locked() , "MT-unsafe call");
  if (TraceInlineCacheClearing || TraceICs) {
    tty->print_cr("IC@" INTPTR_FORMAT ": set to clean", instruction_address());
    print();
  }

  address entry;
  if (is_optimized()) {
    entry = SharedRuntime::get_resolve_opt_virtual_call_stub();
  } else {
    entry = SharedRuntime::get_resolve_virtual_call_stub();
  }

294
  // A zombie transition will always be safe, since the metadata has already been set to NULL, so
D
duke 已提交
295 296 297 298 299 300 301 302 303
  // we only need to patch the destination
  bool safe_transition = is_optimized() || SafepointSynchronize::is_at_safepoint();

  if (safe_transition) {
    // Kill any leftover stub we might have too
    if (is_in_transition_state()) {
      ICStub* old_stub = ICStub_from_destination_address(stub_address());
      old_stub->clear();
    }
304
    if (is_optimized()) {
D
duke 已提交
305
    set_ic_destination(entry);
306 307 308
  } else {
      set_ic_destination_and_value(entry, (void*)NULL);
    }
D
duke 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  } else {
    // Unsafe transition - create stub.
    InlineCacheBuffer::create_transition_stub(this, NULL, entry);
  }
  // We can't check this anymore. With lazy deopt we could have already
  // cleaned this IC entry before we even return. This is possible if
  // we ran out of space in the inline cache buffer trying to do the
  // set_next and we safepointed to free up space. This is a benign
  // race because the IC entry was complete when we safepointed so
  // cleaning it immediately is harmless.
  // assert(is_clean(), "sanity check");
}


bool CompiledIC::is_clean() const {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
  bool is_clean = false;
  address dest = ic_destination();
  is_clean = dest == SharedRuntime::get_resolve_opt_virtual_call_stub() ||
             dest == SharedRuntime::get_resolve_virtual_call_stub();
329
  assert(!is_clean || is_optimized() || cached_value() == NULL, "sanity check");
D
duke 已提交
330 331 332 333
  return is_clean;
}


334
void CompiledIC::set_to_monomorphic(CompiledICInfo& info) {
D
duke 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
  // Updating a cache to the wrong entry can cause bugs that are very hard
  // to track down - if cache entry gets invalid - we just clean it. In
  // this way it is always the same code path that is responsible for
  // updating and resolving an inline cache
  //
  // The above is no longer true. SharedRuntime::fixup_callers_callsite will change optimized
  // callsites. In addition ic_miss code will update a site to monomorphic if it determines
  // that an monomorphic call to the interpreter can now be monomorphic to compiled code.
  //
  // In both of these cases the only thing being modifed is the jump/call target and these
  // transitions are mt_safe

  Thread *thread = Thread::current();
349
  if (info.to_interpreter()) {
D
duke 已提交
350 351 352 353 354 355 356 357
    // Call to interpreter
    if (info.is_optimized() && is_optimized()) {
       assert(is_clean(), "unsafe IC path");
       MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
      // the call analysis (callee structure) specifies that the call is optimized
      // (either because of CHA or the static target is final)
      // At code generation time, this call has been emitted as static call
      // Call via stub
358
      assert(info.cached_metadata() != NULL && info.cached_metadata()->is_method(), "sanity check");
D
duke 已提交
359
      CompiledStaticCall* csc = compiledStaticCall_at(instruction_address());
360
      methodHandle method (thread, (Method*)info.cached_metadata());
D
duke 已提交
361 362 363 364 365 366 367 368 369
      csc->set_to_interpreted(method, info.entry());
      if (TraceICs) {
         ResourceMark rm(thread);
         tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter: %s",
           instruction_address(),
           method->print_value_string());
      }
    } else {
      // Call via method-klass-holder
370
      InlineCacheBuffer::create_transition_stub(this, info.claim_cached_icholder(), info.entry());
D
duke 已提交
371 372
      if (TraceICs) {
         ResourceMark rm(thread);
373
         tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter via icholder ", instruction_address());
D
duke 已提交
374 375 376 377
      }
    }
  } else {
    // Call to compiled code
378
    bool static_bound = info.is_optimized() || (info.cached_metadata() == NULL);
D
duke 已提交
379 380 381 382 383 384 385 386 387 388 389
#ifdef ASSERT
    CodeBlob* cb = CodeCache::find_blob_unsafe(info.entry());
    assert (cb->is_nmethod(), "must be compiled!");
#endif /* ASSERT */

    // This is MT safe if we come from a clean-cache and go through a
    // non-verified entry point
    bool safe = SafepointSynchronize::is_at_safepoint() ||
                (!is_in_transition_state() && (info.is_optimized() || static_bound || is_clean()));

    if (!safe) {
390
      InlineCacheBuffer::create_transition_stub(this, info.cached_metadata(), info.entry());
D
duke 已提交
391
    } else {
392
      if (is_optimized()) {
D
duke 已提交
393
      set_ic_destination(info.entry());
394 395 396
      } else {
        set_ic_destination_and_value(info.entry(), info.cached_metadata());
      }
D
duke 已提交
397 398 399 400
    }

    if (TraceICs) {
      ResourceMark rm(thread);
401
      assert(info.cached_metadata() == NULL || info.cached_metadata()->is_klass(), "must be");
D
duke 已提交
402 403
      tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to compiled (rcvr klass) %s: %s",
        instruction_address(),
404
        ((Klass*)info.cached_metadata())->print_value_string(),
D
duke 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        (safe) ? "" : "via stub");
    }
  }
  // We can't check this anymore. With lazy deopt we could have already
  // cleaned this IC entry before we even return. This is possible if
  // we ran out of space in the inline cache buffer trying to do the
  // set_next and we safepointed to free up space. This is a benign
  // race because the IC entry was complete when we safepointed so
  // cleaning it immediately is harmless.
  // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
}


// is_optimized: Compiler has generated an optimized call (i.e., no inline
// cache) static_bound: The call can be static bound (i.e, no need to use
// inline cache)
void CompiledIC::compute_monomorphic_entry(methodHandle method,
                                           KlassHandle receiver_klass,
                                           bool is_optimized,
                                           bool static_bound,
                                           CompiledICInfo& info,
                                           TRAPS) {
  nmethod* method_code = method->code();
  address entry = NULL;
  if (method_code != NULL) {
    // Call to compiled code
    if (static_bound || is_optimized) {
      entry      = method_code->verified_entry_point();
    } else {
      entry      = method_code->entry_point();
    }
  }
  if (entry != NULL) {
    // Call to compiled code
439
    info.set_compiled_entry(entry, (static_bound || is_optimized) ? NULL : receiver_klass(), is_optimized);
D
duke 已提交
440 441 442 443
  } else {
    // Note: the following problem exists with Compiler1:
    //   - at compile time we may or may not know if the destination is final
    //   - if we know that the destination is final, we will emit an optimized
444
    //     virtual call (no inline cache), and need a Method* to make a call
D
duke 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    //     to the interpreter
    //   - if we do not know if the destination is final, we emit a standard
    //     virtual call, and use CompiledICHolder to call interpreted code
    //     (no static call stub has been generated)
    //     However in that case we will now notice it is static_bound
    //     and convert the call into what looks to be an optimized
    //     virtual call. This causes problems in verifying the IC because
    //     it look vanilla but is optimized. Code in is_call_to_interpreted
    //     is aware of this and weakens its asserts.

    // static_bound should imply is_optimized -- otherwise we have a
    // performance bug (statically-bindable method is called via
    // dynamically-dispatched call note: the reverse implication isn't
    // necessarily true -- the call may have been optimized based on compiler
    // analysis (static_bound is only based on "final" etc.)
#ifdef COMPILER2
#ifdef TIERED
#if defined(ASSERT)
    // can't check the assert because we don't have the CompiledIC with which to
    // find the address if the call instruction.
    //
    // CodeBlob* cb = find_blob_unsafe(instruction_address());
    // assert(cb->is_compiled_by_c1() || !static_bound || is_optimized, "static_bound should imply is_optimized");
#endif // ASSERT
#else
    assert(!static_bound || is_optimized, "static_bound should imply is_optimized");
#endif // TIERED
#endif // COMPILER2
    if (is_optimized) {
      // Use stub entry
475
      info.set_interpreter_entry(method()->get_c2i_entry(), method());
D
duke 已提交
476
    } else {
477 478 479
      // Use icholder entry
      CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass());
      info.set_icholder_entry(method()->get_c2i_unverified_entry(), holder);
D
duke 已提交
480 481
    }
  }
482
  assert(info.is_optimized() == is_optimized, "must agree");
D
duke 已提交
483 484 485
}


486 487 488
bool CompiledIC::is_icholder_entry(address entry) {
  CodeBlob* cb = CodeCache::find_blob_unsafe(entry);
  return (cb != NULL && cb->is_adapter_blob());
D
duke 已提交
489 490 491
}


492 493
CompiledIC::CompiledIC(nmethod* nm, NativeCall* call)
  : _ic_call(call)
D
duke 已提交
494
{
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  address ic_call = call->instruction_address();

  assert(ic_call != NULL, "ic_call address must be set");
  assert(nm != NULL, "must pass nmethod");
  assert(nm->contains(ic_call),   "must be in nmethod");

  // search for the ic_call at the given address
  RelocIterator iter(nm, ic_call, ic_call+1);
  bool ret = iter.next();
  assert(ret == true, "relocInfo must exist at this address");
  assert(iter.addr() == ic_call, "must find ic_call");
  if (iter.type() == relocInfo::virtual_call_type) {
    virtual_call_Relocation* r = iter.virtual_call_reloc();
    _is_optimized = false;
    _value = nativeMovConstReg_at(r->cached_value());
  } else {
    assert(iter.type() == relocInfo::opt_virtual_call_type, "must be a virtual call");
    _is_optimized = true;
    _value = NULL;
}
D
duke 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
}


// ----------------------------------------------------------------------------

void CompiledStaticCall::set_to_clean() {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
  // Reset call site
  MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
#ifdef ASSERT
  CodeBlob* cb = CodeCache::find_blob_unsafe(this);
  assert(cb != NULL && cb->is_nmethod(), "must be nmethod");
#endif
  set_destination_mt_safe(SharedRuntime::get_resolve_static_call_stub());

  // Do not reset stub here:  It is too expensive to call find_stub.
  // Instead, rely on caller (nmethod::clear_inline_caches) to clear
  // both the call and its stub.
}


bool CompiledStaticCall::is_clean() const {
  return destination() == SharedRuntime::get_resolve_static_call_stub();
}

bool CompiledStaticCall::is_call_to_compiled() const {
  return CodeCache::contains(destination());
}


bool CompiledStaticCall::is_call_to_interpreted() const {
  // It is a call to interpreted, if it calls to a stub. Hence, the destination
  // must be in the stub part of the nmethod that contains the call
  nmethod* nm = CodeCache::find_nmethod(instruction_address());
  return nm->stub_contains(destination());
}


void CompiledStaticCall::set_to_interpreted(methodHandle callee, address entry) {
  address stub=find_stub();
  assert(stub!=NULL, "stub not found");

  if (TraceICs) {
    ResourceMark rm;
    tty->print_cr("CompiledStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
                  instruction_address(),
                  callee->name_and_sig_as_C_string());
  }

  NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);   // creation also verifies the object
  NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());

  assert(method_holder->data()    == 0           || method_holder->data()    == (intptr_t)callee(), "a) MT-unsafe modification of inline cache");
  assert(jump->jump_destination() == (address)-1 || jump->jump_destination() == entry, "b) MT-unsafe modification of inline cache");

  // Update stub
  method_holder->set_data((intptr_t)callee());
  jump->set_jump_destination(entry);

  // Update jump to call
  set_destination_mt_safe(stub);
}


void CompiledStaticCall::set(const StaticCallInfo& info) {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
  MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
  // Updating a cache to the wrong entry can cause bugs that are very hard
  // to track down - if cache entry gets invalid - we just clean it. In
  // this way it is always the same code path that is responsible for
  // updating and resolving an inline cache
  assert(is_clean(), "do not update a call entry - use clean");

  if (info._to_interpreter) {
    // Call to interpreted code
    set_to_interpreted(info.callee(), info.entry());
  } else {
    if (TraceICs) {
      ResourceMark rm;
      tty->print_cr("CompiledStaticCall@" INTPTR_FORMAT ": set_to_compiled " INTPTR_FORMAT,
                    instruction_address(),
                    info.entry());
    }
    // Call to compiled code
    assert (CodeCache::contains(info.entry()), "wrong entry point");
    set_destination_mt_safe(info.entry());
  }
}


// Compute settings for a CompiledStaticCall. Since we might have to set
// the stub when calling to the interpreter, we need to return arguments.
void CompiledStaticCall::compute_entry(methodHandle m, StaticCallInfo& info) {
  nmethod* m_code = m->code();
  info._callee = m;
  if (m_code != NULL) {
    info._to_interpreter = false;
    info._entry  = m_code->verified_entry_point();
  } else {
    // Callee is interpreted code.  In any case entering the interpreter
    // puts a converter-frame on the stack to save arguments.
    info._to_interpreter = true;
    info._entry      = m()->get_c2i_entry();
  }
}


void CompiledStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
  // Reset stub
  address stub = static_stub->addr();
  assert(stub!=NULL, "stub not found");
  NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);   // creation also verifies the object
  NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
  method_holder->set_data(0);
  jump->set_jump_destination((address)-1);
}


address CompiledStaticCall::find_stub() {
  // Find reloc. information containing this call-site
  RelocIterator iter((nmethod*)NULL, instruction_address());
  while (iter.next()) {
    if (iter.addr() == instruction_address()) {
      switch(iter.type()) {
        case relocInfo::static_call_type:
          return iter.static_call_reloc()->static_stub();
        // We check here for opt_virtual_call_type, since we reuse the code
        // from the CompiledIC implementation
        case relocInfo::opt_virtual_call_type:
          return iter.opt_virtual_call_reloc()->static_stub();
        case relocInfo::poll_type:
        case relocInfo::poll_return_type: // A safepoint can't overlap a call.
        default:
          ShouldNotReachHere();
      }
    }
  }
  return NULL;
}


//-----------------------------------------------------------------------------
// Non-product mode code
#ifndef PRODUCT

void CompiledIC::verify() {
  // make sure code pattern is actually a call imm32 instruction
  _ic_call->verify();
  if (os::is_MP()) {
    _ic_call->verify_alignment();
  }
  assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted()
          || is_optimized() || is_megamorphic(), "sanity check");
}


void CompiledIC::print() {
  print_compiled_ic();
  tty->cr();
}


void CompiledIC::print_compiled_ic() {
679 680
  tty->print("Inline cache at " INTPTR_FORMAT ", calling %s " INTPTR_FORMAT " cached_value " INTPTR_FORMAT,
             instruction_address(), is_call_to_interpreted() ? "interpreted " : "", ic_destination(), is_optimized() ? NULL : cached_value());
D
duke 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
}


void CompiledStaticCall::print() {
  tty->print("static call at " INTPTR_FORMAT " -> ", instruction_address());
  if (is_clean()) {
    tty->print("clean");
  } else if (is_call_to_compiled()) {
    tty->print("compiled");
  } else if (is_call_to_interpreted()) {
    tty->print("interpreted");
  }
  tty->cr();
}

void CompiledStaticCall::verify() {
  // Verify call
  NativeCall::verify();
  if (os::is_MP()) {
    verify_alignment();
  }

  // Verify stub
  address stub = find_stub();
  assert(stub != NULL, "no stub found for static call");
  NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);   // creation also verifies the object
  NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());

  // Verify state
  assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
}

#endif