Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/icBuffer.cpp
32285 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/codeCache.hpp"26#include "code/compiledIC.hpp"27#include "code/icBuffer.hpp"28#include "code/nmethod.hpp"29#include "code/scopeDesc.hpp"30#include "gc_interface/collectedHeap.inline.hpp"31#include "interpreter/interpreter.hpp"32#include "interpreter/linkResolver.hpp"33#include "memory/resourceArea.hpp"34#include "memory/universe.inline.hpp"35#include "oops/method.hpp"36#include "oops/oop.inline.hpp"37#include "oops/oop.inline2.hpp"38#include "runtime/mutexLocker.hpp"39#include "runtime/stubRoutines.hpp"4041PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC4243DEF_STUB_INTERFACE(ICStub);4445StubQueue* InlineCacheBuffer::_buffer = NULL;46ICStub* InlineCacheBuffer::_next_stub = NULL;4748CompiledICHolder* InlineCacheBuffer::_pending_released = NULL;49int InlineCacheBuffer::_pending_count = 0;5051void ICStub::finalize() {52if (!is_empty()) {53ResourceMark rm;54CompiledIC *ic = CompiledIC_at(CodeCache::find_nmethod(ic_site()), ic_site());55assert(CodeCache::find_nmethod(ic->instruction_address()) != NULL, "inline cache in non-nmethod?");5657assert(this == ICStub_from_destination_address(ic->stub_address()), "wrong owner of ic buffer");58ic->set_ic_destination_and_value(destination(), cached_value());59}60}616263address ICStub::destination() const {64return InlineCacheBuffer::ic_buffer_entry_point(code_begin());65}6667void* ICStub::cached_value() const {68return InlineCacheBuffer::ic_buffer_cached_value(code_begin());69}707172void ICStub::set_stub(CompiledIC *ic, void* cached_val, address dest_addr) {73// We cannot store a pointer to the 'ic' object, since it is resource allocated. Instead we74// store the location of the inline cache. Then we have enough information recreate the CompiledIC75// object when we need to remove the stub.76_ic_site = ic->instruction_address();7778// Assemble new stub79InlineCacheBuffer::assemble_ic_buffer_code(code_begin(), cached_val, dest_addr);80assert(destination() == dest_addr, "can recover destination");81assert(cached_value() == cached_val, "can recover destination");82}838485void ICStub::clear() {86if (CompiledIC::is_icholder_entry(destination())) {87InlineCacheBuffer::queue_for_release((CompiledICHolder*)cached_value());88}89_ic_site = NULL;90}919293#ifndef PRODUCT94// anybody calling to this stub will trap9596void ICStub::verify() {97}9899void ICStub::print() {100tty->print_cr("ICStub: site: " INTPTR_FORMAT, _ic_site);101}102#endif103104//-----------------------------------------------------------------------------------------------105// Implementation of InlineCacheBuffer106107void InlineCacheBuffer::init_next_stub() {108ICStub* ic_stub = (ICStub*)buffer()->request_committed (ic_stub_code_size());109assert (ic_stub != NULL, "no room for a single stub");110set_next_stub(ic_stub);111}112113void InlineCacheBuffer::initialize() {114if (_buffer != NULL) return; // already initialized115_buffer = new StubQueue(new ICStubInterface, 10*K, InlineCacheBuffer_lock, "InlineCacheBuffer");116assert (_buffer != NULL, "cannot allocate InlineCacheBuffer");117init_next_stub();118}119120121ICStub* InlineCacheBuffer::new_ic_stub() {122while (true) {123ICStub* ic_stub = (ICStub*)buffer()->request_committed(ic_stub_code_size());124if (ic_stub != NULL) {125return ic_stub;126}127// we ran out of inline cache buffer space; must enter safepoint.128// We do this by forcing a safepoint129EXCEPTION_MARK;130131VM_ForceSafepoint vfs;132VMThread::execute(&vfs);133// We could potential get an async. exception at this point.134// In that case we will rethrow it to ourselvs.135if (HAS_PENDING_EXCEPTION) {136oop exception = PENDING_EXCEPTION;137CLEAR_PENDING_EXCEPTION;138Thread::send_async_exception(JavaThread::current()->threadObj(), exception);139}140}141ShouldNotReachHere();142return NULL;143}144145146void InlineCacheBuffer::update_inline_caches() {147if (buffer()->number_of_stubs() > 1) {148if (TraceICBuffer) {149tty->print_cr("[updating inline caches with %d stubs]", buffer()->number_of_stubs());150}151buffer()->remove_all();152init_next_stub();153}154release_pending_icholders();155}156157158bool InlineCacheBuffer::contains(address instruction_address) {159return buffer()->contains(instruction_address);160}161162163bool InlineCacheBuffer::is_empty() {164return buffer()->number_of_stubs() == 1; // always has sentinel165}166167168void InlineCacheBuffer_init() {169InlineCacheBuffer::initialize();170}171172173void InlineCacheBuffer::create_transition_stub(CompiledIC *ic, void* cached_value, address entry) {174assert(!SafepointSynchronize::is_at_safepoint(), "should not be called during a safepoint");175assert (CompiledIC_lock->is_locked(), "");176if (TraceICBuffer) {177tty->print_cr(" create transition stub for " INTPTR_FORMAT " destination " INTPTR_FORMAT " cached value " INTPTR_FORMAT,178ic->instruction_address(), entry, cached_value);179}180181// If an transition stub is already associate with the inline cache, then we remove the association.182if (ic->is_in_transition_state()) {183ICStub* old_stub = ICStub_from_destination_address(ic->stub_address());184old_stub->clear();185}186187// allocate and initialize new "out-of-line" inline-cache188ICStub* ic_stub = get_next_stub();189ic_stub->set_stub(ic, cached_value, entry);190191// Update inline cache in nmethod to point to new "out-of-line" allocated inline cache192ic->set_ic_destination(ic_stub);193194set_next_stub(new_ic_stub()); // can cause safepoint synchronization195}196197198address InlineCacheBuffer::ic_destination_for(CompiledIC *ic) {199ICStub* stub = ICStub_from_destination_address(ic->stub_address());200return stub->destination();201}202203204void* InlineCacheBuffer::cached_value_for(CompiledIC *ic) {205ICStub* stub = ICStub_from_destination_address(ic->stub_address());206return stub->cached_value();207}208209210// Free CompiledICHolder*s that are no longer in use211void InlineCacheBuffer::release_pending_icholders() {212assert(SafepointSynchronize::is_at_safepoint(), "should only be called during a safepoint");213CompiledICHolder* holder = _pending_released;214_pending_released = NULL;215while (holder != NULL) {216CompiledICHolder* next = holder->next();217delete holder;218holder = next;219_pending_count--;220}221assert(_pending_count == 0, "wrong count");222}223224// Enqueue this icholder for release during the next safepoint. It's225// not safe to free them until them since they might be visible to226// another thread.227void InlineCacheBuffer::queue_for_release(CompiledICHolder* icholder) {228MutexLockerEx mex(InlineCacheBuffer_lock);229icholder->set_next(_pending_released);230_pending_released = icholder;231_pending_count++;232if (TraceICBuffer) {233tty->print_cr("enqueueing icholder " INTPTR_FORMAT " to be freed", icholder);234}235}236237238