src/share/vm/code/icBuffer.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/code/icBuffer.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,236 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "code/codeCache.hpp"
    1.30 +#include "code/compiledIC.hpp"
    1.31 +#include "code/icBuffer.hpp"
    1.32 +#include "code/nmethod.hpp"
    1.33 +#include "code/scopeDesc.hpp"
    1.34 +#include "gc_interface/collectedHeap.inline.hpp"
    1.35 +#include "interpreter/interpreter.hpp"
    1.36 +#include "interpreter/linkResolver.hpp"
    1.37 +#include "memory/resourceArea.hpp"
    1.38 +#include "memory/universe.inline.hpp"
    1.39 +#include "oops/method.hpp"
    1.40 +#include "oops/oop.inline.hpp"
    1.41 +#include "oops/oop.inline2.hpp"
    1.42 +#include "runtime/mutexLocker.hpp"
    1.43 +#include "runtime/stubRoutines.hpp"
    1.44 +
    1.45 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    1.46 +
    1.47 +DEF_STUB_INTERFACE(ICStub);
    1.48 +
    1.49 +StubQueue* InlineCacheBuffer::_buffer    = NULL;
    1.50 +ICStub*    InlineCacheBuffer::_next_stub = NULL;
    1.51 +
    1.52 +CompiledICHolder* InlineCacheBuffer::_pending_released = NULL;
    1.53 +int InlineCacheBuffer::_pending_count = 0;
    1.54 +
    1.55 +void ICStub::finalize() {
    1.56 +  if (!is_empty()) {
    1.57 +    ResourceMark rm;
    1.58 +    CompiledIC *ic = CompiledIC_at(CodeCache::find_nmethod(ic_site()), ic_site());
    1.59 +    assert(CodeCache::find_nmethod(ic->instruction_address()) != NULL, "inline cache in non-nmethod?");
    1.60 +
    1.61 +    assert(this == ICStub_from_destination_address(ic->stub_address()), "wrong owner of ic buffer");
    1.62 +    ic->set_ic_destination_and_value(destination(), cached_value());
    1.63 +  }
    1.64 +}
    1.65 +
    1.66 +
    1.67 +address ICStub::destination() const {
    1.68 +  return InlineCacheBuffer::ic_buffer_entry_point(code_begin());
    1.69 +}
    1.70 +
    1.71 +void* ICStub::cached_value() const {
    1.72 +  return InlineCacheBuffer::ic_buffer_cached_value(code_begin());
    1.73 +}
    1.74 +
    1.75 +
    1.76 +void ICStub::set_stub(CompiledIC *ic, void* cached_val, address dest_addr) {
    1.77 +  // We cannot store a pointer to the 'ic' object, since it is resource allocated. Instead we
    1.78 +  // store the location of the inline cache. Then we have enough information recreate the CompiledIC
    1.79 +  // object when we need to remove the stub.
    1.80 +  _ic_site = ic->instruction_address();
    1.81 +
    1.82 +  // Assemble new stub
    1.83 +  InlineCacheBuffer::assemble_ic_buffer_code(code_begin(), cached_val, dest_addr);
    1.84 +  assert(destination() == dest_addr,   "can recover destination");
    1.85 +  assert(cached_value() == cached_val, "can recover destination");
    1.86 +}
    1.87 +
    1.88 +
    1.89 +void ICStub::clear() {
    1.90 +  if (CompiledIC::is_icholder_entry(destination())) {
    1.91 +    InlineCacheBuffer::queue_for_release((CompiledICHolder*)cached_value());
    1.92 +  }
    1.93 +  _ic_site = NULL;
    1.94 +}
    1.95 +
    1.96 +
    1.97 +#ifndef PRODUCT
    1.98 +// anybody calling to this stub will trap
    1.99 +
   1.100 +void ICStub::verify() {
   1.101 +}
   1.102 +
   1.103 +void ICStub::print() {
   1.104 +  tty->print_cr("ICStub: site: " INTPTR_FORMAT, _ic_site);
   1.105 +}
   1.106 +#endif
   1.107 +
   1.108 +//-----------------------------------------------------------------------------------------------
   1.109 +// Implementation of InlineCacheBuffer
   1.110 +
   1.111 +void InlineCacheBuffer::init_next_stub() {
   1.112 +  ICStub* ic_stub = (ICStub*)buffer()->request_committed (ic_stub_code_size());
   1.113 +  assert (ic_stub != NULL, "no room for a single stub");
   1.114 +  set_next_stub(ic_stub);
   1.115 +}
   1.116 +
   1.117 +void InlineCacheBuffer::initialize() {
   1.118 +  if (_buffer != NULL) return; // already initialized
   1.119 +  _buffer = new StubQueue(new ICStubInterface, 10*K, InlineCacheBuffer_lock, "InlineCacheBuffer");
   1.120 +  assert (_buffer != NULL, "cannot allocate InlineCacheBuffer");
   1.121 +  init_next_stub();
   1.122 +}
   1.123 +
   1.124 +
   1.125 +ICStub* InlineCacheBuffer::new_ic_stub() {
   1.126 +  while (true) {
   1.127 +    ICStub* ic_stub = (ICStub*)buffer()->request_committed(ic_stub_code_size());
   1.128 +    if (ic_stub != NULL) {
   1.129 +      return ic_stub;
   1.130 +    }
   1.131 +    // we ran out of inline cache buffer space; must enter safepoint.
   1.132 +    // We do this by forcing a safepoint
   1.133 +    EXCEPTION_MARK;
   1.134 +
   1.135 +    VM_ForceSafepoint vfs;
   1.136 +    VMThread::execute(&vfs);
   1.137 +    // We could potential get an async. exception at this point.
   1.138 +    // In that case we will rethrow it to ourselvs.
   1.139 +    if (HAS_PENDING_EXCEPTION) {
   1.140 +      oop exception = PENDING_EXCEPTION;
   1.141 +      CLEAR_PENDING_EXCEPTION;
   1.142 +      Thread::send_async_exception(JavaThread::current()->threadObj(), exception);
   1.143 +    }
   1.144 +  }
   1.145 +  ShouldNotReachHere();
   1.146 +  return NULL;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +void InlineCacheBuffer::update_inline_caches() {
   1.151 +  if (buffer()->number_of_stubs() > 1) {
   1.152 +    if (TraceICBuffer) {
   1.153 +      tty->print_cr("[updating inline caches with %d stubs]", buffer()->number_of_stubs());
   1.154 +    }
   1.155 +    buffer()->remove_all();
   1.156 +    init_next_stub();
   1.157 +  }
   1.158 +  release_pending_icholders();
   1.159 +}
   1.160 +
   1.161 +
   1.162 +bool InlineCacheBuffer::contains(address instruction_address) {
   1.163 +  return buffer()->contains(instruction_address);
   1.164 +}
   1.165 +
   1.166 +
   1.167 +bool InlineCacheBuffer::is_empty() {
   1.168 +  return buffer()->number_of_stubs() == 1;    // always has sentinel
   1.169 +}
   1.170 +
   1.171 +
   1.172 +void InlineCacheBuffer_init() {
   1.173 +  InlineCacheBuffer::initialize();
   1.174 +}
   1.175 +
   1.176 +
   1.177 +void InlineCacheBuffer::create_transition_stub(CompiledIC *ic, void* cached_value, address entry) {
   1.178 +  assert(!SafepointSynchronize::is_at_safepoint(), "should not be called during a safepoint");
   1.179 +  assert (CompiledIC_lock->is_locked(), "");
   1.180 +  if (TraceICBuffer) {
   1.181 +    tty->print_cr("  create transition stub for " INTPTR_FORMAT " destination " INTPTR_FORMAT " cached value " INTPTR_FORMAT,
   1.182 +                  ic->instruction_address(), entry, cached_value);
   1.183 +  }
   1.184 +
   1.185 +  // If an transition stub is already associate with the inline cache, then we remove the association.
   1.186 +  if (ic->is_in_transition_state()) {
   1.187 +    ICStub* old_stub = ICStub_from_destination_address(ic->stub_address());
   1.188 +    old_stub->clear();
   1.189 +  }
   1.190 +
   1.191 +  // allocate and initialize new "out-of-line" inline-cache
   1.192 +  ICStub* ic_stub = get_next_stub();
   1.193 +  ic_stub->set_stub(ic, cached_value, entry);
   1.194 +
   1.195 +  // Update inline cache in nmethod to point to new "out-of-line" allocated inline cache
   1.196 +  ic->set_ic_destination(ic_stub);
   1.197 +
   1.198 +  set_next_stub(new_ic_stub()); // can cause safepoint synchronization
   1.199 +}
   1.200 +
   1.201 +
   1.202 +address InlineCacheBuffer::ic_destination_for(CompiledIC *ic) {
   1.203 +  ICStub* stub = ICStub_from_destination_address(ic->stub_address());
   1.204 +  return stub->destination();
   1.205 +}
   1.206 +
   1.207 +
   1.208 +void* InlineCacheBuffer::cached_value_for(CompiledIC *ic) {
   1.209 +  ICStub* stub = ICStub_from_destination_address(ic->stub_address());
   1.210 +  return stub->cached_value();
   1.211 +}
   1.212 +
   1.213 +
   1.214 +// Free CompiledICHolder*s that are no longer in use
   1.215 +void InlineCacheBuffer::release_pending_icholders() {
   1.216 +  assert(SafepointSynchronize::is_at_safepoint(), "should only be called during a safepoint");
   1.217 +  CompiledICHolder* holder = _pending_released;
   1.218 +  _pending_released = NULL;
   1.219 +  while (holder != NULL) {
   1.220 +    CompiledICHolder* next = holder->next();
   1.221 +    delete holder;
   1.222 +    holder = next;
   1.223 +    _pending_count--;
   1.224 +  }
   1.225 +  assert(_pending_count == 0, "wrong count");
   1.226 +}
   1.227 +
   1.228 +// Enqueue this icholder for release during the next safepoint.  It's
   1.229 +// not safe to free them until them since they might be visible to
   1.230 +// another thread.
   1.231 +void InlineCacheBuffer::queue_for_release(CompiledICHolder* icholder) {
   1.232 +  MutexLockerEx mex(InlineCacheBuffer_lock);
   1.233 +  icholder->set_next(_pending_released);
   1.234 +  _pending_released = icholder;
   1.235 +  _pending_count++;
   1.236 +  if (TraceICBuffer) {
   1.237 +    tty->print_cr("enqueueing icholder " INTPTR_FORMAT " to be freed", icholder);
   1.238 +  }
   1.239 +}

mercurial